Skip to content

Commit 04c6b3d

Browse files
committedApr 8, 2019
Created a blink example, also made temporary fix for linker script
1 parent 5b2b665 commit 04c6b3d

13 files changed

+350
-113
lines changed
 

‎.vscode/c_cpp_properties.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@
33
{
44
"name": "Win",
55
"includePath": [
6+
"${workspaceFolder}/include",
67
"${workspaceFolder}/libs/cmsis/include",
7-
"${workspaceFolder}/libs/ST/system/include",
8-
"${workspaceFolder}/libs/ST//low_layer/include",
9-
"${workspaceFolder}/include"
8+
"${workspaceFolder}/libs/ST/system/include"
109
],
1110
"browse": {
1211
"path": [
13-
"${workspaceFolder}"
12+
"${workspaceFolder}/"
1413
],
1514
"limitSymbolsToIncludedHeaders": false,
1615
"databaseFilename": ""
1716
},
1817
"defines": [
1918
"STM32F405xx"
2019
],
21-
"compilerPath": "C:/tools/gcc-arm-none-eabi-8-2018-q4-major-win32/bin/arm-none-eabi-gcc.exe",
20+
"compilerPath": "C:/tools/gcc-arm-none-eabi-8-2018-q4-major-win32/bin/arm-none-eabi-gcc.exe -mcpu=cortex-m4 --specs=nano.specs --specs=nosys.specs",
2221
"cStandard": "c11",
2322
"cppStandard": "c++17",
2423
"intelliSenseMode": "clang-x64"

‎.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{
88
"name": "Cortex Debug",
99
"cwd": "${workspaceRoot}",
10-
"executable": "${workspaceRoot}/build//src/STM32F4Template.elf",
10+
"executable": "${workspaceRoot}/build/src/blink.elf",
1111
"armToolchainPath": "C:/Program Files (x86)/GNU Tools ARM Embedded/8 2018-q4-major/bin",
1212
"svdFile": "${workspaceRoot}/docs/processor/STM32F405.svd",
1313
"device": "STM32F405RG",

‎.vscode/settings.json

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
{
22
"cortex-debug.JLinkGDBServerPath": "C:/Program Files (x86)/SEGGER/JLink_V644c/JLinkGDBServerCL.exe",
33
"cmake.cmakePath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin/cmake.exe",
4+
"C_Cpp.intelliSenseEngineFallback": "Disabled",
5+
"files.associations": {
6+
"test.h": "c",
7+
"t.h": "c",
8+
"stm32f4xx.h": "c",
9+
"stm32f405xx.h": "c",
10+
"core_cm4.h": "c",
11+
"cmsis_version.h": "c",
12+
"stdint.h": "c",
13+
"_intsup.h": "c"
14+
},
415
}

‎CMakeLists.txt

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
cmake_minimum_required (VERSION 3.10.2)
22

3-
set(app_name blink)
3+
# set the build type
4+
if(NOT CMAKE_BUILD_TYPE)
5+
set(CMAKE_BUILD_TYPE Release)
6+
endif(NOT CMAKE_BUILD_TYPE)
47

5-
project(${app_name} C CXX ASM)
8+
if(CMAKE_BUILD_TYPE MATCHES Debug)
9+
message("Debug build.")
10+
elseif(CMAKE_BUILD_TYPE MATCHES Release)
11+
message("Release build.")
12+
endif()
13+
14+
# Project specific settings
15+
set(application_name "blink")
16+
17+
project (${application_name} C CXX ASM)
618

719
# STM32-specific settings
820
set(DEVICE_FAMILY STM32F405xx)
921

1022
# Used linker file (just for testing)
11-
get_filename_component(LINKER_SCRIPT linker/stm32f405.ld ABSOLUTE)
23+
get_filename_component(LINKER_SCRIPT linker/stm32f407vgt.ld ABSOLUTE)
1224

1325
# add libraries
1426
add_subdirectory(libs)

‎build-debug.bat

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mkdir build
2+
cd build && cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug -D "CMAKE_TOOLCHAIN_FILE=../CMake/GNU-ARM-Toolchain.cmake" ../

‎build.bat

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mkdir build
2+
cd build && cmake -G "Unix Makefiles" -D "CMAKE_TOOLCHAIN_FILE=../CMake/GNU-ARM-Toolchain.cmake" ../

‎cmake/GNU-ARM-Toolchain.cmake

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
1-
set(CMAKE_SYSTEM_NAME Generic) # 'Generic' is used for embedded systems
1+
# the name of the target operating system
2+
set(CMAKE_SYSTEM_NAME Generic)
23
set(CMAKE_SYSTEM_VERSION 1)
34
set(CMAKE_SYSTEM_PROCESSOR ARM)
45

6+
# which compilers to use for C and C++
57
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
68
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
79
set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
10+
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
11+
set(CMAKE_OBJDUMP arm-none-eabi-objdump)
812

9-
# tells CMake not to try to link executables during its interal checks
10-
# things are not going to link properly without a linker script
1113
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
1214

15+
# core flags
16+
set(CORE_FLAGS "-mthumb -mcpu=cortex-m4 -mlittle-endian -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb-interwork --specs=nano.specs --specs=nosys.specs ${ADDITIONAL_CORE_FLAGS}")
17+
18+
# compiler: language specific flags
19+
set(CMAKE_C_FLAGS "${CORE_FLAGS} -fno-builtin -Wall -std=gnu99 -fdata-sections -ffunction-sections -g3 -gdwarf-2" CACHE INTERNAL "c compiler flags")
20+
set(CMAKE_C_FLAGS_DEBUG "" CACHE INTERNAL "c compiler flags: Debug")
21+
set(CMAKE_C_FLAGS_RELEASE "" CACHE INTERNAL "c compiler flags: Release")
22+
23+
set(CMAKE_CXX_FLAGS "${CORE_FLAGS} -fno-rtti -fno-exceptions -fno-builtin -Wall -std=gnu++11 -fdata-sections -ffunction-sections -g -ggdb3" CACHE INTERNAL "cxx compiler flags")
24+
set(CMAKE_CXX_FLAGS_DEBUG "" CACHE INTERNAL "cxx compiler flags: Debug")
25+
set(CMAKE_CXX_FLAGS_RELEASE "" CACHE INTERNAL "cxx compiler flags: Release")
26+
27+
set(CMAKE_ASM_FLAGS "${CORE_FLAGS} -g -ggdb3 -D__USES_CXX" CACHE INTERNAL "asm compiler flags")
28+
set(CMAKE_ASM_FLAGS_DEBUG "" CACHE INTERNAL "asm compiler flags: Debug")
29+
set(CMAKE_ASM_FLAGS_RELEASE "" CACHE INTERNAL "asm compiler flags: Release")
30+
31+
# search for programs in the build host directories
32+
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
33+
34+
# for libraries and headers in the target directories
35+
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
36+
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
37+
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
38+
1339
# find additional toolchain executables
1440
find_program(ARM_SIZE_EXECUTABLE arm-none-eabi-size)
1541
find_program(ARM_GDB_EXECUTABLE arm-none-eabi-gdb)

‎libs/ST/low_layer/CMakeLists.txt

Whitespace-only changes.

‎libs/ST/system/CMakeLists.txt

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
set(sources src/startup_stm32f405xx.s
22
src/system_stm32f4xx.c)
33

4-
add_library(stm32f4_system ${sources})
4+
set(system_lib stm32f4_system)
5+
add_library(${system_lib} ${sources})
56

6-
target_include_directories(stm32f4_system PUBLIC include)
7-
target_include_directories(stm32f4_system PUBLIC ../../cmsis/include)
7+
target_include_directories(${system_lib} PUBLIC include)
8+
target_include_directories(${system_lib} PUBLIC ../../cmsis/include)
89

9-
target_compile_definitions(stm32f4_system PUBLIC ${DEVICE_FAMILY})
10+
target_compile_definitions(${system_lib} PUBLIC ${DEVICE_FAMILY})
1011

11-
target_compile_options(stm32f4_system PRIVATE -O0)
12+
# additional compiler options: use size-optimized version of library in release build, use -O0 in debug build
13+
if(CMAKE_BUILD_TYPE MATCHES Debug)
14+
set(additional_flags -O0)
15+
else()
16+
set(additional_flags -Os)
17+
endif()
18+
19+
target_compile_options(${system_lib} PRIVATE ${additional_flags})

‎linker/stm32f405.ld

-63
This file was deleted.

‎linker/stm32f407vgt.ld

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/* Default STM32F4xx linker script.
2+
*
3+
* Modify memory sections according to the targeted platform.
4+
*/
5+
MEMORY
6+
{
7+
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 1024K
8+
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
9+
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K
10+
}
11+
12+
/* Linker script to place sections and symbol values. Should be used together
13+
* with other linker script that defines memory regions FLASH and RAM.
14+
* It references following symbols, which must be defined in code:
15+
* Reset_Handler : Entry of reset handler
16+
*
17+
* It defines following symbols, which code can use without definition:
18+
* __exidx_start
19+
* __exidx_end
20+
* __etext
21+
* _sdata
22+
* __preinit_array_start
23+
* __preinit_array_end
24+
* __init_array_start
25+
* __init_array_end
26+
* __fini_array_start
27+
* __fini_array_end
28+
* _edata
29+
* _sbss
30+
* __bss_start__
31+
* __bss_end__
32+
* _ebss
33+
* __end__
34+
* end
35+
* __HeapLimit
36+
* __StackLimit
37+
* __StackTop
38+
* __estack
39+
* _sidata
40+
* _siccmram
41+
*/
42+
ENTRY(Reset_Handler)
43+
44+
SECTIONS
45+
{
46+
.text :
47+
{
48+
KEEP(*(.isr_vector))
49+
*(.text*)
50+
51+
KEEP(*(.init))
52+
KEEP(*(.fini))
53+
54+
/* .ctors */
55+
*crtbegin.o(.ctors)
56+
*crtbegin?.o(.ctors)
57+
*(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
58+
*(SORT(.ctors.*))
59+
*(.ctors)
60+
61+
/* .dtors */
62+
*crtbegin.o(.dtors)
63+
*crtbegin?.o(.dtors)
64+
*(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
65+
*(SORT(.dtors.*))
66+
*(.dtors)
67+
68+
*(.rodata*)
69+
70+
KEEP(*(.eh_frame*))
71+
} > FLASH
72+
73+
.ARM.extab :
74+
{
75+
*(.ARM.extab* .gnu.linkonce.armextab.*)
76+
} > FLASH
77+
78+
__exidx_start = .;
79+
.ARM.exidx :
80+
{
81+
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
82+
} > FLASH
83+
__exidx_end = .;
84+
85+
__etext = .;
86+
87+
/* used by the startup to initialize data */
88+
_sidata = LOADADDR(.data);
89+
90+
.data : AT (__etext)
91+
{
92+
_sdata = .;
93+
*(vtable)
94+
*(.data*)
95+
96+
. = ALIGN(4);
97+
/* preinit data */
98+
PROVIDE_HIDDEN (__preinit_array_start = .);
99+
KEEP(*(.preinit_array))
100+
PROVIDE_HIDDEN (__preinit_array_end = .);
101+
102+
. = ALIGN(4);
103+
/* init data */
104+
PROVIDE_HIDDEN (__init_array_start = .);
105+
KEEP(*(SORT(.init_array.*)))
106+
KEEP(*(.init_array))
107+
PROVIDE_HIDDEN (__init_array_end = .);
108+
109+
110+
. = ALIGN(4);
111+
/* finit data */
112+
PROVIDE_HIDDEN (__fini_array_start = .);
113+
KEEP(*(SORT(.fini_array.*)))
114+
KEEP(*(.fini_array))
115+
PROVIDE_HIDDEN (__fini_array_end = .);
116+
117+
KEEP(*(.jcr*))
118+
. = ALIGN(4);
119+
/* All data end */
120+
_edata = .;
121+
122+
} > RAM
123+
124+
_siccmram = LOADADDR(.ccmram);
125+
126+
/* CCM-RAM section
127+
*
128+
* IMPORTANT NOTE!
129+
* If initialized variables will be placed in this section,
130+
* the startup code needs to be modified to copy the init-values.
131+
*/
132+
.ccmram :
133+
{
134+
. = ALIGN(4);
135+
_sccmram = .; /* create a global symbol at ccmram start */
136+
*(.ccmram)
137+
*(.ccmram*)
138+
139+
. = ALIGN(4);
140+
_eccmram = .; /* create a global symbol at ccmram end */
141+
} >CCMRAM AT> FLASH
142+
143+
.bss :
144+
{
145+
. = ALIGN(4);
146+
_sbss = .;
147+
__bss_start__ = _sbss;
148+
*(.bss*)
149+
*(COMMON)
150+
. = ALIGN(4);
151+
_ebss = .;
152+
__bss_end__ = _ebss;
153+
} > RAM
154+
155+
.heap (COPY):
156+
{
157+
__end__ = .;
158+
PROVIDE(end = .);
159+
*(.heap*)
160+
__HeapLimit = .;
161+
} > RAM
162+
163+
/* .stack_dummy section doesn't contains any symbols. It is only
164+
* used for linker to calculate size of stack sections, and assign
165+
* values to stack symbols later */
166+
.stack_dummy (COPY):
167+
{
168+
*(.stack*)
169+
} > RAM
170+
171+
/* Set stack top to end of RAM, and stack limit move down by
172+
* size of stack_dummy section */
173+
__StackTop = ORIGIN(RAM) + LENGTH(RAM);
174+
__StackLimit = __StackTop - SIZEOF(.stack_dummy);
175+
PROVIDE(_estack = __StackTop);
176+
177+
/* Check if data + heap + stack exceeds RAM limit */
178+
ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack")
179+
}

‎src/CMakeLists.txt

+28-30
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
1-
set (sources main.c)
1+
set(sources main.c)
22

3-
set(elf_file ${app_name}.elf)
4-
set(bin_file ${app_name}.bin)
5-
set(hex_file ${app_name}.hex)
6-
set(map_file ${app_name}.map)
7-
set(lss_file ${app_name}.lss)
3+
# GCC optimization level: use -O0 in debug build, otherwise -O2
4+
if(CMAKE_BUILD_TYPE MATCHES Debug)
5+
set(opt_level -O0)
6+
else()
7+
set(opt_level -Os)
8+
endif()
9+
10+
# set some project constants
11+
set(elf_file ${application_name}.elf)
12+
set(bin_file ${application_name}.bin)
13+
set(hex_file ${application_name}.hex)
14+
set(map_file ${application_name}.map)
15+
set(lss_file ${application_name}.lss)
816

9-
add_executable(${elf_file} ${sources})
1017

11-
target_compile_definitions(${elf_file} PUBLIC ${DEVICE_FAMILY})
18+
# add sources to elf file
19+
add_executable(${elf_file} ${sources})
1220
target_link_libraries(${elf_file} PUBLIC stm32f4_system)
1321

14-
# link with linker file
15-
target_link_libraries(${elf_file} PUBLIC -T${LINKER_SCRIPT})
1622

17-
target_link_libraries(${elf_file} PUBLIC
18-
"--specs=nano.specs \
19-
--specs=nosys.specs \
20-
-mthumb -mcpu=cortex-m4 \
21-
-Wl,--gc-sections \
22-
-Wl,-Map=${PROJECT_NAME}.map")
23-
24-
target_compile_options(${elf_file} PUBLIC
25-
-g
26-
-O0
27-
-mthumb
28-
-mcpu=cortex-m4
29-
-ffunction-sections
30-
-fdata-sections
31-
-D__STARTUP_CLEAR_BSS
32-
-D__START=main)
23+
# set additional for compiler and linker: optimization and generate map file
24+
set(additional_compiler_flags ${opt_level})
25+
set(additional_linker_flags -Wl,-Map=${map_file},--cref,--no-warn-mismatch)
26+
target_compile_options(${elf_file} PRIVATE ${additional_compiler_flags})
27+
target_link_libraries(${elf_file} PRIVATE ${additional_linker_flags})
28+
29+
# remove unused sections
30+
target_link_libraries(${elf_file} PUBLIC "-g -Wl,--gc-sections")
3331

3432
# link with linker file
3533
target_link_libraries(${elf_file} PUBLIC -T${LINKER_SCRIPT})
3634

3735
# show size of resulting firmware image
38-
add_custom_target(${elf_file}-size ALL DEPENDS ${elf_file} COMMAND ${ARM_SIZE_EXECUTABLE} -B ${elf_file})
36+
add_custom_target(${elf_file}-size DEPENDS ${elf_file} COMMAND ${ARM_SIZE_EXECUTABLE} -B ${elf_file})
3937

4038
# generate extended listing
41-
add_custom_target(${lss_file} ALL DEPENDS ${elf_file} COMMAND ${ARM_OBJDUMP_EXECUTABLE} -S ${elf_file} > ${lss_file})
39+
add_custom_target(${lss_file} DEPENDS ${elf_file} COMMAND ${ARM_OBJDUMP_EXECUTABLE} -S ${elf_file} > ${lss_file})
4240

4341
# create binary and hex files
44-
add_custom_target(${hex_file} ALL DEPENDS ${elf_file} COMMAND ${ARM_OBJCOPY_EXECUTABLE} -Oihex ${elf_file} ${hex_file})
45-
add_custom_target(${bin_file} ALL DEPENDS ${elf_file} COMMAND ${ARM_OBJCOPY_EXECUTABLE} -Obinary ${elf_file} ${bin_file})
42+
add_custom_target(${hex_file} DEPENDS ${elf_file} COMMAND ${ARM_OBJCOPY_EXECUTABLE} -Oihex ${elf_file} ${hex_file})
43+
add_custom_target(${bin_file} DEPENDS ${elf_file} COMMAND ${ARM_OBJCOPY_EXECUTABLE} -Obinary ${elf_file} ${bin_file})
4644
add_custom_target(${application_name} ALL DEPENDS ${elf_file}-size ${bin_file} ${hex_file} ${lss_file})

‎src/main.c

+66-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,68 @@
1-
int main() {
2-
while (1) {
3-
1+
#include "stm32f4xx.h"
2+
3+
void delay (int a)
4+
{
5+
volatile int i,j;
6+
7+
for (i=0 ; i < a ; i++)
8+
{
9+
j++;
410
}
11+
return;
12+
}
13+
14+
void turnLedOn(void) {
15+
GPIOB->ODR |= GPIO_ODR_OD4;
16+
}
17+
18+
void turnLedOff(void) {
19+
GPIOB->ODR |= GPIO_ODR_OD4;
20+
}
21+
22+
int isButtonPressed(void) {
23+
return GPIOB->IDR & GPIO_IDR_ID3;
24+
}
25+
26+
void setupLed(void) {
27+
// Set port B4 to general purpose output mode
28+
GPIOB->MODER &= ~(GPIO_MODER_MODE4_1);
29+
30+
// Output push-pull
31+
GPIOB->OTYPER &= ~(GPIO_OTYPER_OT4);
32+
33+
// Low output speed
34+
GPIOB->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR4;
35+
36+
// No pull up or pull down
37+
GPIOB->PUPDR &= GPIO_PUPDR_PUPD4_0;
38+
}
39+
40+
void setupButton(void) {
41+
// Set port B3 as input
42+
GPIOB->MODER &= ~(GPIO_MODER_MODE3);
43+
44+
// Pull upp
45+
GPIOB->PUPDR &= GPIO_PUPDR_PUPD3_0;
46+
}
47+
48+
void main()
49+
{
50+
// Enable the GPIO clock for port B
51+
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
52+
setupLed();
53+
setupButton();
54+
55+
56+
while(1) {
57+
// Turn LED on
58+
if (isButtonPressed()) {
59+
turnLedOn();
60+
delay(500000);
61+
turnLedOff();
62+
delay(500000);
63+
}
64+
else {
65+
turnLedOn();
66+
}
67+
}
568
}

0 commit comments

Comments
 (0)
Please sign in to comment.