2023-05-25 21:27:26 +07:00
include_guard ( )
2023-04-20 16:55:48 +07:00
2023-05-19 16:27:07 +07:00
include ( CMakePrintHelpers )
2023-04-20 16:55:48 +07:00
2023-05-26 14:56:22 +07:00
# TOP is path to root directory
set ( TOP "${CMAKE_CURRENT_LIST_DIR}/../.." )
2023-06-07 14:46:14 -05:00
get_filename_component ( TOP ${ TOP } ABSOLUTE )
2023-05-26 14:56:22 +07:00
2023-05-19 16:27:07 +07:00
# Default to gcc
2023-05-25 21:27:26 +07:00
if ( NOT DEFINED TOOLCHAIN )
2023-06-01 12:36:08 +07:00
set ( TOOLCHAIN gcc )
2023-05-25 21:27:26 +07:00
endif ( )
2021-06-10 10:11:20 -05:00
2023-05-19 16:27:07 +07:00
if ( NOT FAMILY )
2023-06-01 12:36:08 +07:00
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" )
2023-05-25 21:27:26 +07:00
endif ( )
2021-06-10 10:11:20 -05:00
2023-05-19 16:27:07 +07:00
if ( NOT EXISTS ${ CMAKE_CURRENT_LIST_DIR } / ${ FAMILY } /family.cmake )
2023-06-01 12:36:08 +07:00
message ( FATAL_ERROR "Family '${FAMILY}' is not known/supported" )
2023-05-19 16:27:07 +07:00
endif ( )
2022-01-13 13:42:28 +00:00
2023-06-16 15:29:34 +07:00
set ( WARNING_FLAGS_GNU
- W a l l
- W e x t r a
- W e r r o r
- W f a t a l - e r r o r s
- W d o u b l e - p r o m o t i o n
- W s t r i c t - p r o t o t y p e s
- W s t r i c t - o v e r f l o w
- W e r r o r - i m p l i c i t - f u n c t i o n - d e c l a r a t i o n
- W f l o a t - e q u a l
- W u n d e f
- W s h a d o w
- W w r i t e - s t r i n g s
- W s i g n - c o m p a r e
- W m i s s i n g - f o r m a t - a t t r i b u t e
- W u n r e a c h a b l e - c o d e
- W c a s t - a l i g n
- W c a s t - f u n c t i o n - t y p e
- W c a s t - q u a l
- W n u l l - d e r e f e r e n c e
- W u n i n i t i a l i z e d
- W u n u s e d
- W r e t u r n - t y p e
- W r e d u n d a n t - d e c l s
)
set ( WARNINGS_FLAGS_IAR "" )
2023-06-01 12:36:08 +07:00
2023-05-19 16:27:07 +07:00
function ( family_filter RESULT DIR )
2023-06-01 12:36:08 +07:00
get_filename_component ( DIR ${ DIR } ABSOLUTE BASE_DIR ${ CMAKE_CURRENT_SOURCE_DIR } )
if ( EXISTS "${DIR}/only.txt" )
file ( READ "${DIR}/only.txt" ONLYS )
# Replace newlines with semicolon so that it is treated as a list by CMake
string ( REPLACE "\n" ";" ONLYS_LINES ${ ONLYS } )
# For each mcu
foreach ( MCU IN LISTS FAMILY_MCUS )
# For each line in only.txt
foreach ( _line ${ ONLYS_LINES } )
# If mcu:xxx exists for this mcu or board:xxx then include
if ( ${ _line } STREQUAL "mcu:${MCU}" OR ${ _line } STREQUAL "board:${BOARD}" )
set ( ${ RESULT } 1 PARENT_SCOPE )
return ( )
endif ( )
endforeach ( )
endforeach ( )
# Didn't find it in only file so don't build
set ( ${ RESULT } 0 PARENT_SCOPE )
elseif ( EXISTS "${DIR}/skip.txt" )
file ( READ "${DIR}/skip.txt" SKIPS )
# Replace newlines with semicolon so that it is treated as a list by CMake
string ( REPLACE "\n" ";" SKIPS_LINES ${ SKIPS } )
# For each mcu
foreach ( MCU IN LISTS FAMILY_MCUS )
# For each line in only.txt
foreach ( _line ${ SKIPS_LINES } )
# If mcu:xxx exists for this mcu then skip
if ( ${ _line } STREQUAL "mcu:${MCU}" )
set ( ${ RESULT } 0 PARENT_SCOPE )
return ( )
endif ( )
endforeach ( )
endforeach ( )
2022-01-13 13:42:28 +00:00
2023-06-01 12:36:08 +07:00
# Didn't find in skip file so build
set ( ${ RESULT } 1 PARENT_SCOPE )
else ( )
2022-01-13 13:42:28 +00:00
2023-06-01 12:36:08 +07:00
# Didn't find skip or only file so build
set ( ${ RESULT } 1 PARENT_SCOPE )
endif ( )
2023-05-19 16:27:07 +07:00
endfunction ( )
2021-06-10 10:11:20 -05:00
2023-06-01 12:36:08 +07:00
2023-05-19 16:27:07 +07:00
function ( family_add_subdirectory DIR )
2023-06-01 12:36:08 +07:00
family_filter ( SHOULD_ADD "${DIR}" )
if ( SHOULD_ADD )
add_subdirectory ( ${ DIR } )
endif ( )
2023-05-19 16:27:07 +07:00
endfunction ( )
2023-06-01 12:36:08 +07:00
2023-05-19 16:27:07 +07:00
function ( family_get_project_name OUTPUT_NAME DIR )
2023-06-01 12:36:08 +07:00
get_filename_component ( SHORT_NAME ${ DIR } NAME )
set ( ${ OUTPUT_NAME } ${ TINYUSB_FAMILY_PROJECT_NAME_PREFIX } ${ SHORT_NAME } PARENT_SCOPE )
2023-05-19 16:27:07 +07:00
endfunction ( )
2021-06-10 10:11:20 -05:00
2023-06-01 12:36:08 +07:00
2023-05-19 16:27:07 +07:00
function ( family_initialize_project PROJECT DIR )
2023-06-02 11:42:17 +07:00
# set output suffix to .elf (skip espressif and rp2040)
if ( NOT FAMILY STREQUAL "espressif" AND NOT FAMILY STREQUAL "rp2040" )
2023-06-01 12:50:32 +07:00
set ( CMAKE_EXECUTABLE_SUFFIX .elf PARENT_SCOPE )
endif ( )
2023-06-01 12:36:08 +07:00
family_filter ( ALLOWED "${DIR}" )
if ( NOT ALLOWED )
get_filename_component ( SHORT_NAME ${ DIR } NAME )
message ( FATAL_ERROR "${SHORT_NAME} is not supported on FAMILY=${FAMILY}" )
endif ( )
2023-05-19 16:27:07 +07:00
endfunction ( )
2023-06-01 12:36:08 +07:00
2023-06-06 23:55:33 +07:00
# Add segger rtt to example
function ( family_add_segger_rtt TARGET )
if ( NOT TARGET segger_rtt )
add_library ( segger_rtt STATIC
$ { T O P } / l i b / S E G G E R _ R T T / R T T / S E G G E R _ R T T . c
)
target_include_directories ( segger_rtt PUBLIC ${ TOP } /lib/SEGGER_RTT/RTT )
endif ( )
target_link_libraries ( ${ TARGET } PUBLIC segger_rtt )
endfunction ( )
2023-06-02 11:42:17 +07:00
#------------------------------------
# Main target configure
#------------------------------------
# Add common configuration to example
2023-06-01 12:36:08 +07:00
function ( family_configure_common TARGET )
2023-05-25 21:27:26 +07:00
# run size after build
add_custom_command ( TARGET ${ TARGET } POST_BUILD
2023-06-01 12:36:08 +07:00
C O M M A N D $ { C M A K E _ S I Z E } $ < T A R G E T _ F I L E : $ { T A R G E T } >
2023-05-25 21:27:26 +07:00
)
2023-06-16 15:29:34 +07:00
target_compile_options ( ${ TARGET } PUBLIC ${ WARNING_FLAGS_${CMAKE_C_COMPILER_ID } } )
2023-06-02 15:26:16 +07:00
if ( CMAKE_C_COMPILER_ID STREQUAL "GNU" )
2023-06-02 13:27:18 +07:00
# Generate map file
target_link_options ( ${ TARGET } PUBLIC
# link map
" L I N K E R : - M a p = $ < T A R G E T _ F I L E : $ { T A R G E T } > . m a p "
)
endif ( )
2023-06-05 15:41:35 +07:00
# LOGGER
if ( DEFINED LOGGER )
target_compile_definitions ( ${ TARGET } PUBLIC LOGGER_ ${ LOGGER } )
2023-06-06 23:55:33 +07:00
if ( LOGGER STREQUAL "RTT" OR LOGGER STREQUAL "rtt" )
family_add_segger_rtt ( ${ TARGET } )
endif ( )
2023-06-05 15:41:35 +07:00
endif ( )
2023-06-19 16:33:47 +07:00
# ETM Trace
if ( TRACE_ETM STREQUAL "1" )
target_compile_definitions ( ${ TARGET } PUBLIC TRACE_ETM )
endif ( )
2023-05-25 17:27:05 +07:00
endfunction ( )
2023-06-01 12:36:08 +07:00
2023-06-02 11:42:17 +07:00
# configure an executable target to link to tinyusb in device mode, and add the board implementation
function ( family_configure_device_example TARGET )
# default implementation is empty, the function should be redefined in the FAMILY/family.cmake
endfunction ( )
# configure an executable target to link to tinyusb in host mode, and add the board implementation
function ( family_configure_host_example TARGET )
# default implementation is empty, the function should be redefined in the FAMILY/family.cmake
endfunction ( )
# Add tinyusb to example
function ( family_add_tinyusb TARGET OPT_MCU )
# tinyusb target is built for each example since it depends on example's tusb_config.h
set ( TINYUSB_TARGET_PREFIX ${ TARGET } - )
add_library ( ${ TARGET } -tinyusb_config INTERFACE )
2023-06-05 15:41:35 +07:00
target_include_directories ( ${ TARGET } -tinyusb_config INTERFACE ${ CMAKE_CURRENT_SOURCE_DIR } /src )
target_compile_definitions ( ${ TARGET } -tinyusb_config INTERFACE CFG_TUSB_MCU= ${ OPT_MCU } )
if ( DEFINED LOG )
target_compile_definitions ( ${ TARGET } -tinyusb_config INTERFACE CFG_TUSB_DEBUG= ${ LOG } )
endif ( )
2023-06-02 11:42:17 +07:00
# tinyusb's CMakeList.txt
add_subdirectory ( ${ TOP } /src ${ CMAKE_CURRENT_BINARY_DIR } /tinyusb )
endfunction ( )
# Add freeRTOS support to example
function ( family_add_freertos TARGET )
# freeros config
if ( NOT TARGET freertos_config )
add_library ( freertos_config INTERFACE )
2023-06-02 13:27:18 +07:00
target_include_directories ( freertos_config INTERFACE
2023-06-02 11:42:17 +07:00
$ { C M A K E _ C U R R E N T _ F U N C T I O N _ L I S T _ D I R } / $ { F A M I L Y } / F r e e R T O S C o n f i g
)
2023-06-16 12:02:42 +07:00
# add board definition to freertos_config mostly for SystemCoreClock
target_link_libraries ( freertos_config INTERFACE board_ ${ BOARD } )
2023-06-02 11:42:17 +07:00
endif ( )
# freertos kernel should be generic as freertos_config however, CMAKE complains with missing variable
# such as CMAKE_C_COMPILE_OBJECT
if ( NOT TARGET freertos_kernel )
add_subdirectory ( ${ TOP } /lib/FreeRTOS-Kernel ${ CMAKE_BINARY_DIR } /lib/freertos_kernel )
endif ( )
# Add FreeRTOS option to tinyusb_config
target_compile_definitions ( ${ TARGET } -tinyusb_config INTERFACE
C F G _ T U S B _ O S = O P T _ O S _ F R E E R T O S
)
# link tinyusb with freeRTOS kernel
2023-06-16 14:17:25 +07:00
target_link_libraries ( ${ TARGET } -tinyusb PUBLIC freertos_kernel )
target_link_libraries ( ${ TARGET } PUBLIC freertos_kernel )
2023-06-02 11:42:17 +07:00
endfunction ( )
2023-06-02 13:27:18 +07:00
function ( family_add_default_example_warnings TARGET )
target_compile_options ( ${ TARGET } PUBLIC
- W a l l
- W e x t r a
- W e r r o r
- W f a t a l - e r r o r s
- W d o u b l e - p r o m o t i o n
- W f l o a t - e q u a l
- W s h a d o w
- W w r i t e - s t r i n g s
- W s i g n - c o m p a r e
- W m i s s i n g - f o r m a t - a t t r i b u t e
- W u n r e a c h a b l e - c o d e
- W c a s t - a l i g n
- W c a s t - q u a l
- W n u l l - d e r e f e r e n c e
- W u n i n i t i a l i z e d
- W u n u s e d
- W r e d u n d a n t - d e c l s
#-Wstrict-prototypes
#-Werror-implicit-function-declaration
#-Wundef
)
if ( CMAKE_C_COMPILER_ID STREQUAL "GNU" )
if ( CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 12.0 )
target_link_options ( ${ TARGET } PUBLIC "LINKER:--no-warn-rwx-segments" )
endif ( )
# GCC 10
if ( CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 10.0 )
target_compile_options ( ${ TARGET } PUBLIC -Wconversion )
endif ( )
# GCC 8
if ( CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 8.0 )
target_compile_options ( ${ TARGET } PUBLIC -Wcast-function-type -Wstrict-overflow )
endif ( )
# GCC 6
if ( CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 6.0 )
target_compile_options ( ${ TARGET } PUBLIC -Wno-strict-aliasing )
endif ( )
endif ( )
endfunction ( )
2023-06-01 12:36:08 +07:00
# Add bin/hex output
function ( family_add_bin_hex TARGET )
add_custom_command ( TARGET ${ TARGET } POST_BUILD
C O M M A N D $ { C M A K E _ O B J C O P Y } - O b i n a r y $ < T A R G E T _ F I L E : $ { T A R G E T } > $ < T A R G E T _ F I L E _ D I R : $ { T A R G E T } > / $ { T A R G E T } . b i n
C O M M A N D $ { C M A K E _ O B J C O P Y } - O i h e x $ < T A R G E T _ F I L E : $ { T A R G E T } > $ < T A R G E T _ F I L E _ D I R : $ { T A R G E T } > / $ { T A R G E T } . h e x
V E R B A T I M )
endfunction ( )
2023-05-23 21:45:00 +07:00
2023-06-02 11:42:17 +07:00
#----------------------------------
# Flashing target
#----------------------------------
2023-05-19 16:27:07 +07:00
# Add flash jlink target
function ( family_flash_jlink TARGET )
2023-05-25 21:27:26 +07:00
if ( NOT DEFINED JLINKEXE )
set ( JLINKEXE JLinkExe )
endif ( )
2023-05-19 16:27:07 +07:00
2023-05-25 21:27:26 +07:00
file ( GENERATE
O U T P U T $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } / $ { T A R G E T } . j l i n k
C O N T E N T " h a l t
2023-05-19 16:27:07 +07:00
l o a d f i l e $ < T A R G E T _ F I L E : $ { T A R G E T } >
r
g o
e x i t "
2023-05-25 21:27:26 +07:00
)
2023-05-19 16:27:07 +07:00
2023-05-25 21:27:26 +07:00
add_custom_target ( ${ TARGET } -jlink
D E P E N D S $ { T A R G E T }
C O M M A N D $ { J L I N K E X E } - d e v i c e $ { J L I N K _ D E V I C E } - i f s w d - J T A G C o n f - 1 , - 1 - s p e e d a u t o - C o m m a n d F i l e $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } / $ { T A R G E T } . j l i n k
)
2023-05-19 16:27:07 +07:00
endfunction ( )
2023-06-01 12:36:08 +07:00
2023-06-01 17:03:04 +07:00
# Add flash stlink target
function ( family_flash_stlink TARGET )
if ( NOT DEFINED STM32_PROGRAMMER_CLI )
set ( STM32_PROGRAMMER_CLI STM32_Programmer_CLI )
endif ( )
add_custom_target ( ${ TARGET } -stlink
D E P E N D S $ { T A R G E T }
C O M M A N D $ { S T M 3 2 _ P R O G R A M M E R _ C L I } - - c o n n e c t p o r t = s w d - - w r i t e $ < T A R G E T _ F I L E : $ { T A R G E T } > - - g o
)
endfunction ( )
2023-05-19 16:27:07 +07:00
# Add flash pycod target
function ( family_flash_pyocd TARGET )
2023-06-01 12:36:08 +07:00
if ( NOT DEFINED PYOC )
set ( PYOCD pyocd )
endif ( )
2023-05-19 16:27:07 +07:00
2023-06-01 12:36:08 +07:00
add_custom_target ( ${ TARGET } -pyocd
D E P E N D S $ { T A R G E T }
C O M M A N D $ { P Y O C D } f l a s h - t $ { P Y O C D _ T A R G E T } $ < T A R G E T _ F I L E : $ { T A R G E T } >
)
2023-05-19 16:27:07 +07:00
endfunction ( )
2023-06-01 12:36:08 +07:00
2023-05-19 16:27:07 +07:00
# Add flash using NXP's LinkServer (redserver)
# https://www.nxp.com/design/software/development-software/mcuxpresso-software-and-tools-/linkserver-for-microcontrollers:LINKERSERVER
function ( family_flash_nxplink TARGET )
2023-06-01 12:36:08 +07:00
if ( NOT DEFINED LINKSERVER )
set ( LINKSERVER LinkServer )
endif ( )
2023-05-19 16:27:07 +07:00
2023-06-01 12:36:08 +07:00
# LinkServer has a bug that can only execute with full path otherwise it throws:
# realpath error: No such file or directory
execute_process ( COMMAND which ${ LINKSERVER } OUTPUT_VARIABLE LINKSERVER_PATH OUTPUT_STRIP_TRAILING_WHITESPACE )
2023-05-19 16:27:07 +07:00
2023-06-01 12:36:08 +07:00
add_custom_target ( ${ TARGET } -nxplink
D E P E N D S $ { T A R G E T }
C O M M A N D $ { L I N K S E R V E R _ P A T H } f l a s h $ { N X P L I N K _ D E V I C E } l o a d $ < T A R G E T _ F I L E : $ { T A R G E T } >
)
2023-05-19 16:27:07 +07:00
endfunction ( )
2023-06-01 12:36:08 +07:00
2023-06-02 11:42:17 +07:00
# family specific: can override above functions
2023-05-19 16:27:07 +07:00
include ( ${ CMAKE_CURRENT_LIST_DIR } / ${ FAMILY } /family.cmake )
if ( NOT FAMILY_MCUS )
2023-06-01 12:36:08 +07:00
set ( FAMILY_MCUS ${ FAMILY } )
2023-03-17 16:12:49 +07:00
endif ( )
2023-05-19 16:27:07 +07:00
# save it in case of re-inclusion
set ( FAMILY_MCUS ${ FAMILY_MCUS } CACHE INTERNAL "" )