Skip to content

Commit 49d8d27

Browse files
committed
improve flash target
1 parent 270136e commit 49d8d27

File tree

5 files changed

+202
-199
lines changed

5 files changed

+202
-199
lines changed

examples/rules.mk

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,19 @@ endif
227227
# Jlink Interface
228228
JLINK_IF ?= swd
229229

230+
# Jlink script
231+
define jlink_script
232+
halt
233+
loadfile $^
234+
r
235+
go
236+
exit
237+
endef
238+
export jlink_script
239+
230240
# Flash using jlink
231241
flash-jlink: $(BUILD)/$(PROJECT).hex
232-
@echo halt > $(BUILD)/$(BOARD).jlink
233-
@echo r >> $(BUILD)/$(BOARD).jlink
234-
@echo loadfile $^ >> $(BUILD)/$(BOARD).jlink
235-
@echo r >> $(BUILD)/$(BOARD).jlink
236-
@echo go >> $(BUILD)/$(BOARD).jlink
237-
@echo exit >> $(BUILD)/$(BOARD).jlink
242+
@echo "$$jlink_script" > $(BUILD)/$(BOARD).jlink
238243
$(JLINKEXE) -device $(JLINK_DEVICE) -if $(JLINK_IF) -JTAGConf -1,-1 -speed auto -CommandFile $(BUILD)/$(BOARD).jlink
239244

240245
# Flash STM32 MCU using stlink with STM32 Cube Programmer CLI

hw/bsp/family_support.cmake

Lines changed: 184 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,205 @@
1-
if (NOT TARGET _family_support_marker)
2-
add_library(_family_support_marker INTERFACE)
1+
if (TARGET _family_support_marker)
2+
return()
3+
endif ()
34

4-
include(CMakePrintHelpers)
5+
add_library(_family_support_marker INTERFACE)
56

6-
# Default to gcc
7-
if(NOT DEFINED TOOLCHAIN)
8-
set(TOOLCHAIN gcc)
9-
endif()
7+
include(CMakePrintHelpers)
108

11-
if (NOT FAMILY)
12-
message(FATAL_ERROR "You must set a FAMILY variable for the build (e.g. rp2040, eps32s2, esp32s3). You can do this via -DFAMILY=xxx on the cmake command line")
13-
endif()
9+
# Default to gcc
10+
if(NOT DEFINED TOOLCHAIN)
11+
set(TOOLCHAIN gcc)
12+
endif()
1413

15-
if (NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/${FAMILY}/family.cmake)
16-
message(FATAL_ERROR "Family '${FAMILY}' is not known/supported")
17-
endif()
14+
if (NOT FAMILY)
15+
message(FATAL_ERROR "You must set a FAMILY variable for the build (e.g. rp2040, eps32s2, esp32s3). You can do this via -DFAMILY=xxx on the cmake command line")
16+
endif()
1817

19-
function(family_filter RESULT DIR)
20-
get_filename_component(DIR ${DIR} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
21-
22-
if (EXISTS "${DIR}/only.txt")
23-
file(READ "${DIR}/only.txt" ONLYS)
24-
# Replace newlines with semicolon so that it is treated as a list by CMake
25-
string(REPLACE "\n" ";" ONLYS_LINES ${ONLYS})
26-
# For each mcu
27-
foreach(MCU IN LISTS FAMILY_MCUS)
28-
# For each line in only.txt
29-
foreach(_line ${ONLYS_LINES})
30-
# If mcu:xxx exists for this mcu or board:xxx then include
31-
if (${_line} STREQUAL "mcu:${MCU}" OR ${_line} STREQUAL "board:${BOARD}")
32-
set(${RESULT} 1 PARENT_SCOPE)
33-
return()
34-
endif()
35-
endforeach()
36-
endforeach()
18+
if (NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/${FAMILY}/family.cmake)
19+
message(FATAL_ERROR "Family '${FAMILY}' is not known/supported")
20+
endif()
3721

38-
# Didn't find it in only file so don't build
39-
set(${RESULT} 0 PARENT_SCOPE)
40-
41-
elseif (EXISTS "${DIR}/skip.txt")
42-
file(READ "${DIR}/skip.txt" SKIPS)
43-
# Replace newlines with semicolon so that it is treated as a list by CMake
44-
string(REPLACE "\n" ";" SKIPS_LINES ${SKIPS})
45-
# For each mcu
46-
foreach(MCU IN LISTS FAMILY_MCUS)
47-
# For each line in only.txt
48-
foreach(_line ${SKIPS_LINES})
49-
# If mcu:xxx exists for this mcu then skip
50-
if (${_line} STREQUAL "mcu:${MCU}")
51-
set(${RESULT} 0 PARENT_SCOPE)
52-
return()
53-
endif()
54-
endforeach()
22+
function(family_filter RESULT DIR)
23+
get_filename_component(DIR ${DIR} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
24+
25+
if (EXISTS "${DIR}/only.txt")
26+
file(READ "${DIR}/only.txt" ONLYS)
27+
# Replace newlines with semicolon so that it is treated as a list by CMake
28+
string(REPLACE "\n" ";" ONLYS_LINES ${ONLYS})
29+
# For each mcu
30+
foreach(MCU IN LISTS FAMILY_MCUS)
31+
# For each line in only.txt
32+
foreach(_line ${ONLYS_LINES})
33+
# If mcu:xxx exists for this mcu or board:xxx then include
34+
if (${_line} STREQUAL "mcu:${MCU}" OR ${_line} STREQUAL "board:${BOARD}")
35+
set(${RESULT} 1 PARENT_SCOPE)
36+
return()
37+
endif()
38+
endforeach()
39+
endforeach()
40+
41+
# Didn't find it in only file so don't build
42+
set(${RESULT} 0 PARENT_SCOPE)
43+
44+
elseif (EXISTS "${DIR}/skip.txt")
45+
file(READ "${DIR}/skip.txt" SKIPS)
46+
# Replace newlines with semicolon so that it is treated as a list by CMake
47+
string(REPLACE "\n" ";" SKIPS_LINES ${SKIPS})
48+
# For each mcu
49+
foreach(MCU IN LISTS FAMILY_MCUS)
50+
# For each line in only.txt
51+
foreach(_line ${SKIPS_LINES})
52+
# If mcu:xxx exists for this mcu then skip
53+
if (${_line} STREQUAL "mcu:${MCU}")
54+
set(${RESULT} 0 PARENT_SCOPE)
55+
return()
56+
endif()
5557
endforeach()
58+
endforeach()
5659

57-
# Didn't find in skip file so build
58-
set(${RESULT} 1 PARENT_SCOPE)
60+
# Didn't find in skip file so build
61+
set(${RESULT} 1 PARENT_SCOPE)
5962

60-
else()
63+
else()
6164

62-
# Didn't find skip or only file so build
63-
set(${RESULT} 1 PARENT_SCOPE)
65+
# Didn't find skip or only file so build
66+
set(${RESULT} 1 PARENT_SCOPE)
6467

65-
endif()
68+
endif()
6669

67-
endfunction()
70+
endfunction()
6871

69-
function(family_add_subdirectory DIR)
70-
family_filter(SHOULD_ADD "${DIR}")
71-
if (SHOULD_ADD)
72-
add_subdirectory(${DIR})
73-
endif()
74-
endfunction()
72+
function(family_add_subdirectory DIR)
73+
family_filter(SHOULD_ADD "${DIR}")
74+
if (SHOULD_ADD)
75+
add_subdirectory(${DIR})
76+
endif()
77+
endfunction()
78+
79+
function(family_get_project_name OUTPUT_NAME DIR)
80+
get_filename_component(SHORT_NAME ${DIR} NAME)
81+
set(${OUTPUT_NAME} ${TINYUSB_FAMILY_PROJECT_NAME_PREFIX}${SHORT_NAME} PARENT_SCOPE)
82+
endfunction()
7583

76-
function(family_get_project_name OUTPUT_NAME DIR)
84+
function(family_initialize_project PROJECT DIR)
85+
family_filter(ALLOWED "${DIR}")
86+
if (NOT ALLOWED)
7787
get_filename_component(SHORT_NAME ${DIR} NAME)
78-
set(${OUTPUT_NAME} ${TINYUSB_FAMILY_PROJECT_NAME_PREFIX}${SHORT_NAME} PARENT_SCOPE)
79-
endfunction()
80-
81-
function(family_initialize_project PROJECT DIR)
82-
family_filter(ALLOWED "${DIR}")
83-
if (NOT ALLOWED)
84-
get_filename_component(SHORT_NAME ${DIR} NAME)
85-
message(FATAL_ERROR "${SHORT_NAME} is not supported on FAMILY=${FAMILY}")
86-
endif()
87-
endfunction()
88-
89-
function(family_add_default_example_warnings TARGET)
90-
target_compile_options(${TARGET} PUBLIC
91-
-Wall
92-
-Wextra
93-
-Werror
94-
-Wfatal-errors
95-
-Wdouble-promotion
96-
-Wfloat-equal
97-
-Wshadow
98-
-Wwrite-strings
99-
-Wsign-compare
100-
-Wmissing-format-attribute
101-
-Wunreachable-code
102-
-Wcast-align
103-
-Wcast-qual
104-
-Wnull-dereference
105-
-Wuninitialized
106-
-Wunused
107-
-Wredundant-decls
108-
#-Wstrict-prototypes
109-
#-Werror-implicit-function-declaration
110-
#-Wundef
111-
)
112-
113-
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
114-
# GCC 10
115-
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 10.0)
116-
target_compile_options(${TARGET} PUBLIC -Wconversion)
117-
endif()
118-
119-
# GCC 8
120-
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 8.0)
121-
target_compile_options(${TARGET} PUBLIC -Wcast-function-type -Wstrict-overflow)
122-
endif()
123-
124-
# GCC 6
125-
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 6.0)
126-
target_compile_options(${TARGET} PUBLIC -Wno-strict-aliasing)
127-
endif()
88+
message(FATAL_ERROR "${SHORT_NAME} is not supported on FAMILY=${FAMILY}")
89+
endif()
90+
endfunction()
91+
92+
function(family_add_default_example_warnings TARGET)
93+
target_compile_options(${TARGET} PUBLIC
94+
-Wall
95+
-Wextra
96+
-Werror
97+
-Wfatal-errors
98+
-Wdouble-promotion
99+
-Wfloat-equal
100+
-Wshadow
101+
-Wwrite-strings
102+
-Wsign-compare
103+
-Wmissing-format-attribute
104+
-Wunreachable-code
105+
-Wcast-align
106+
-Wcast-qual
107+
-Wnull-dereference
108+
-Wuninitialized
109+
-Wunused
110+
-Wredundant-decls
111+
#-Wstrict-prototypes
112+
#-Werror-implicit-function-declaration
113+
#-Wundef
114+
)
115+
116+
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
117+
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 12.0)
118+
target_link_options(${TARGET} PUBLIC "LINKER:--no-warn-rwx-segments")
128119
endif()
129-
endfunction()
130-
131-
# configure an executable target to link to tinyusb in device mode, and add the board implementation
132-
function(family_configure_device_example TARGET)
133-
# default implementation is empty, the function should be redefined in the FAMILY/family.cmake
134-
endfunction()
135120

136-
# configure an executable target to link to tinyusb in host mode, and add the board implementation
137-
function(family_configure_host_example TARGET)
138-
# default implementation is empty, the function should be redefined in the FAMILY/family.cmake
139-
endfunction()
121+
# GCC 10
122+
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 10.0)
123+
target_compile_options(${TARGET} PUBLIC -Wconversion)
124+
endif()
140125

141-
include(${CMAKE_CURRENT_LIST_DIR}/${FAMILY}/family.cmake)
126+
# GCC 8
127+
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 8.0)
128+
target_compile_options(${TARGET} PUBLIC -Wcast-function-type -Wstrict-overflow)
129+
endif()
142130

143-
if (NOT FAMILY_MCUS)
144-
set(FAMILY_MCUS ${FAMILY})
131+
# GCC 6
132+
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 6.0)
133+
target_compile_options(${TARGET} PUBLIC -Wno-strict-aliasing)
134+
endif()
145135
endif()
146-
147-
# save it in case of re-inclusion
148-
set(FAMILY_MCUS ${FAMILY_MCUS} CACHE INTERNAL "")
136+
endfunction()
137+
138+
# Add flash jlink target
139+
function(family_flash_jlink TARGET)
140+
if (NOT DEFINED JLINKEXE)
141+
set(JLINKEXE JLinkExe)
142+
endif ()
143+
144+
file(GENERATE
145+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.jlink
146+
CONTENT "halt
147+
loadfile $<TARGET_FILE:${TARGET}>
148+
r
149+
go
150+
exit"
151+
)
152+
153+
add_custom_target(${TARGET}-jlink
154+
DEPENDS ${TARGET}
155+
COMMAND ${JLINKEXE} -device ${JLINK_DEVICE} -if swd -JTAGConf -1,-1 -speed auto -CommandFile ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.jlink
156+
)
157+
endfunction()
158+
159+
# Add flash pycod target
160+
function(family_flash_pyocd TARGET)
161+
if (NOT DEFINED PYOC)
162+
set(PYOCD pyocd)
163+
endif ()
164+
165+
add_custom_target(${TARGET}-pyocd
166+
DEPENDS ${TARGET}
167+
COMMAND ${PYOCD} flash -t ${PYOCD_TARGET} $<TARGET_FILE:${TARGET}>
168+
)
169+
endfunction()
170+
171+
# Add flash using NXP's LinkServer (redserver)
172+
# https://www.nxp.com/design/software/development-software/mcuxpresso-software-and-tools-/linkserver-for-microcontrollers:LINKERSERVER
173+
function(family_flash_nxplink TARGET)
174+
if (NOT DEFINED LINKSERVER)
175+
set(LINKSERVER LinkServer)
176+
endif ()
177+
178+
# LinkServer has a bug that can only execute with full path otherwise it throws:
179+
# realpath error: No such file or directory
180+
execute_process(COMMAND which ${LINKSERVER} OUTPUT_VARIABLE LINKSERVER_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
181+
182+
add_custom_target(${TARGET}-nxplink
183+
DEPENDS ${TARGET}
184+
COMMAND ${LINKSERVER_PATH} flash ${NXPLINK_DEVICE} load $<TARGET_FILE:${TARGET}>
185+
)
186+
endfunction()
187+
188+
# configure an executable target to link to tinyusb in device mode, and add the board implementation
189+
function(family_configure_device_example TARGET)
190+
# default implementation is empty, the function should be redefined in the FAMILY/family.cmake
191+
endfunction()
192+
193+
# configure an executable target to link to tinyusb in host mode, and add the board implementation
194+
function(family_configure_host_example TARGET)
195+
# default implementation is empty, the function should be redefined in the FAMILY/family.cmake
196+
endfunction()
197+
198+
include(${CMAKE_CURRENT_LIST_DIR}/${FAMILY}/family.cmake)
199+
200+
if (NOT FAMILY_MCUS)
201+
set(FAMILY_MCUS ${FAMILY})
149202
endif()
203+
204+
# save it in case of re-inclusion
205+
set(FAMILY_MCUS ${FAMILY_MCUS} CACHE INTERNAL "")

0 commit comments

Comments
 (0)