From e8dd200fed6adeebd98ebc7abf09d45285da7abf Mon Sep 17 00:00:00 2001
From: hathach <thach@tinyusb.org>
Date: Fri, 19 May 2023 14:46:39 +0700
Subject: [PATCH 1/4] move cmake folder to tools/

---
 .idea/vcs.xml                                 | 61 -------------------
 hw/bsp/imxrt/family.cmake                     |  8 +--
 hw/bsp/lpc55/family.cmake                     | 13 ++--
 hw/bsp/nrf/family.cmake                       |  3 +-
 .../cmake/cpu/cortex-m33.cmake                |  0
 {examples => tools}/cmake/cpu/cortex-m4.cmake |  0
 {examples => tools}/cmake/cpu/cortex-m7.cmake |  0
 .../cmake/toolchain/arm_gcc.cmake             |  0
 .../cmake/toolchain/set_flags.cmake           |  0
 9 files changed, 12 insertions(+), 73 deletions(-)
 rename {examples => tools}/cmake/cpu/cortex-m33.cmake (100%)
 rename {examples => tools}/cmake/cpu/cortex-m4.cmake (100%)
 rename {examples => tools}/cmake/cpu/cortex-m7.cmake (100%)
 rename {examples => tools}/cmake/toolchain/arm_gcc.cmake (100%)
 rename {examples => tools}/cmake/toolchain/set_flags.cmake (100%)

diff --git a/.idea/vcs.xml b/.idea/vcs.xml
index 63371256f..94a25f7f4 100644
--- a/.idea/vcs.xml
+++ b/.idea/vcs.xml
@@ -1,67 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
   <component name="VcsDirectoryMappings">
-    <mapping directory="$PROJECT_DIR$/../pico/pico-sdk" vcs="Git" />
     <mapping directory="$PROJECT_DIR$" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/allwinner" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/bridgetek/ft9xx/ft90x-sdk" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/broadcom" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/gd/nuclei-sdk" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/infineon/mtb-xmclib-cat3" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/microchip" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/mindmotion/mm32sdk" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/nordic/nrfx" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/nuvoton" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/nxp/lpcopen" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/nxp/mcux-sdk" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/nxp/nxp_sdk" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/raspberry_pi/Pico-PIO-USB" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/raspberry_pi/pico-sdk" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/raspberry_pi/pico-sdk/lib/btstack" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/raspberry_pi/pico-sdk/lib/cyw43-driver" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/raspberry_pi/pico-sdk/lib/lwip" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/raspberry_pi/pico-sdk/lib/mbedtls" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/raspberry_pi/pico-sdk/lib/tinyusb" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/renesas/fsp" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/renesas/rx" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/silabs/cmsis-dfp-efm32gg12b" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/sony/cxd56/spresense-exported-sdk" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/cmsis_device_f0" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/cmsis_device_f1" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/cmsis_device_f2" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/cmsis_device_f3" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/cmsis_device_f4" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/cmsis_device_f7" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/cmsis_device_g0" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/cmsis_device_g4" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/cmsis_device_h7" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/cmsis_device_l0" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/cmsis_device_l1" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/cmsis_device_l4" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/cmsis_device_l5" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/cmsis_device_u5" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/cmsis_device_wb" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/stm32f0xx_hal_driver" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/stm32f1xx_hal_driver" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/stm32f2xx_hal_driver" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/stm32f3xx_hal_driver" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/stm32f4xx_hal_driver" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/stm32f7xx_hal_driver" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/stm32g0xx_hal_driver" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/stm32g4xx_hal_driver" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/stm32h7xx_hal_driver" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/stm32l0xx_hal_driver" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/stm32l1xx_hal_driver" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/stm32l4xx_hal_driver" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/stm32l5xx_hal_driver" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/stm32u5xx_hal_driver" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/st/stm32wbxx_hal_driver" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/ti" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/hw/mcu/wch/ch32v307" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/lib/CMSIS_5" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/lib/FreeRTOS-Kernel" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/lib/lwip" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/lib/sct_neopixel" vcs="Git" />
-    <mapping directory="$PROJECT_DIR$/tools/uf2" vcs="Git" />
   </component>
 </project>
\ No newline at end of file
diff --git a/hw/bsp/imxrt/family.cmake b/hw/bsp/imxrt/family.cmake
index a475b4721..e9be7acc8 100644
--- a/hw/bsp/imxrt/family.cmake
+++ b/hw/bsp/imxrt/family.cmake
@@ -8,9 +8,12 @@ if (NOT BOARD)
   message(FATAL_ERROR "BOARD not specified")
 endif ()
 
+# TOP is path to root directory
+set(TOP "${CMAKE_CURRENT_LIST_DIR}/../../..")
+
 # toolchain set up
 set(CMAKE_SYSTEM_PROCESSOR cortex-m7 CACHE INTERNAL "System Processor")
-set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_LIST_DIR}/../../../examples/cmake/toolchain/arm_${TOOLCHAIN}.cmake)
+set(CMAKE_TOOLCHAIN_FILE ${TOP}/tools/cmake/toolchain/arm_${TOOLCHAIN}.cmake)
 
 set(FAMILY_MCUS MIMXRT CACHE INTERNAL "")
 
@@ -24,9 +27,6 @@ include(${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}/board.cmake)
 # only need to be built ONCE for all examples
 set(BOARD_TARGET board_${BOARD})
 if (NOT TARGET ${BOARD_TARGET})
-  # TOP is path to root directory
-  set(TOP "${CMAKE_CURRENT_LIST_DIR}/../../..")
-
   set(SDK_DIR ${TOP}/hw/mcu/nxp/mcux-sdk)
   set(CMSIS_DIR ${TOP}/lib/CMSIS_5)
 
diff --git a/hw/bsp/lpc55/family.cmake b/hw/bsp/lpc55/family.cmake
index 0ac2b6ce3..439f54170 100644
--- a/hw/bsp/lpc55/family.cmake
+++ b/hw/bsp/lpc55/family.cmake
@@ -8,9 +8,14 @@ if (NOT BOARD)
   message(FATAL_ERROR "BOARD not specified")
 endif ()
 
+# TOP is path to root directory
+set(TOP "${CMAKE_CURRENT_LIST_DIR}/../../..")
+set(SDK_DIR ${TOP}/hw/mcu/nxp/mcux-sdk)
+set(CMSIS_DIR ${TOP}/lib/CMSIS_5)
+
 # toolchain set up
 set(CMAKE_SYSTEM_PROCESSOR cortex-m33 CACHE INTERNAL "System Processor")
-set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_LIST_DIR}/../../../examples/cmake/toolchain/arm_${TOOLCHAIN}.cmake)
+set(CMAKE_TOOLCHAIN_FILE ${TOP}/tools/cmake/toolchain/arm_${TOOLCHAIN}.cmake)
 
 set(FAMILY_MCUS LPC55XX CACHE INTERNAL "")
 
@@ -24,12 +29,6 @@ include(${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}/board.cmake)
 # only need to be built ONCE for all examples
 set(BOARD_TARGET board_${BOARD})
 if (NOT TARGET ${BOARD_TARGET})
-  # TOP is path to root directory
-  set(TOP "${CMAKE_CURRENT_LIST_DIR}/../../..")
-
-  set(SDK_DIR ${TOP}/hw/mcu/nxp/mcux-sdk)
-  set(CMSIS_DIR ${TOP}/lib/CMSIS_5)
-
   add_library(${BOARD_TARGET} STATIC
     # external driver
     #lib/sct_neopixel/sct_neopixel.c
diff --git a/hw/bsp/nrf/family.cmake b/hw/bsp/nrf/family.cmake
index c8faa23cc..ca28c4f5d 100644
--- a/hw/bsp/nrf/family.cmake
+++ b/hw/bsp/nrf/family.cmake
@@ -25,10 +25,11 @@ else ()
   set(JLINK_DEVICE ${MCU_VARIANT}_xxaa)
 endif ()
 
-set(CMAKE_TOOLCHAIN_FILE ${TOP}/examples/cmake/toolchain/arm_${TOOLCHAIN}.cmake)
+set(CMAKE_TOOLCHAIN_FILE ${TOP}/tools/cmake/toolchain/arm_${TOOLCHAIN}.cmake)
 
 set(FAMILY_MCUS NRF5X CACHE INTERNAL "")
 
+
 #------------------------------------
 # BOARD_TARGET
 #------------------------------------
diff --git a/examples/cmake/cpu/cortex-m33.cmake b/tools/cmake/cpu/cortex-m33.cmake
similarity index 100%
rename from examples/cmake/cpu/cortex-m33.cmake
rename to tools/cmake/cpu/cortex-m33.cmake
diff --git a/examples/cmake/cpu/cortex-m4.cmake b/tools/cmake/cpu/cortex-m4.cmake
similarity index 100%
rename from examples/cmake/cpu/cortex-m4.cmake
rename to tools/cmake/cpu/cortex-m4.cmake
diff --git a/examples/cmake/cpu/cortex-m7.cmake b/tools/cmake/cpu/cortex-m7.cmake
similarity index 100%
rename from examples/cmake/cpu/cortex-m7.cmake
rename to tools/cmake/cpu/cortex-m7.cmake
diff --git a/examples/cmake/toolchain/arm_gcc.cmake b/tools/cmake/toolchain/arm_gcc.cmake
similarity index 100%
rename from examples/cmake/toolchain/arm_gcc.cmake
rename to tools/cmake/toolchain/arm_gcc.cmake
diff --git a/examples/cmake/toolchain/set_flags.cmake b/tools/cmake/toolchain/set_flags.cmake
similarity index 100%
rename from examples/cmake/toolchain/set_flags.cmake
rename to tools/cmake/toolchain/set_flags.cmake

From 270136e84af4f01c394be17fc0688461cf3d1f1c Mon Sep 17 00:00:00 2001
From: hathach <thach@tinyusb.org>
Date: Fri, 19 May 2023 16:02:26 +0700
Subject: [PATCH 2/4] adding cpu core .mk for makefile

fix cortex m33 for nrf5340 dk
---
 examples/make.mk                    |  3 +++
 hw/bsp/nrf/boards/pca10095/board.mk |  1 +
 hw/bsp/nrf/family.mk                |  9 ++++-----
 tools/make/cpu/cortex-m33.mk        | 11 +++++++++++
 tools/make/cpu/cortex-m4.mk         | 11 +++++++++++
 tools/make/cpu/cortex-m7.mk         | 11 +++++++++++
 6 files changed, 41 insertions(+), 5 deletions(-)
 create mode 100644 tools/make/cpu/cortex-m33.mk
 create mode 100644 tools/make/cpu/cortex-m4.mk
 create mode 100644 tools/make/cpu/cortex-m7.mk

diff --git a/examples/make.mk b/examples/make.mk
index 2ce6fb398..28ebc62da 100644
--- a/examples/make.mk
+++ b/examples/make.mk
@@ -2,6 +2,8 @@
 # Common make definition for all examples
 # ---------------------------------------
 
+TOOLCHAIN ?= gcc
+
 #-------------- TOP and CURRENT_PATH ------------
 
 # Set TOP to be the path to get from the current directory (where make was
@@ -75,6 +77,7 @@ else
 endif
 
 #-------------- Cross Compiler  ------------
+
 # Can be set by board, default to ARM GCC
 CROSS_COMPILE ?= arm-none-eabi-
 
diff --git a/hw/bsp/nrf/boards/pca10095/board.mk b/hw/bsp/nrf/boards/pca10095/board.mk
index 5ad103d62..9c4edbafc 100644
--- a/hw/bsp/nrf/boards/pca10095/board.mk
+++ b/hw/bsp/nrf/boards/pca10095/board.mk
@@ -1,3 +1,4 @@
+CPU_CORE = cortex-m33
 MCU_VARIANT = nrf5340_application
 CFLAGS += -DNRF5340_XXAA -DNRF5340_XXAA_APPLICATION
 
diff --git a/hw/bsp/nrf/family.mk b/hw/bsp/nrf/family.mk
index d5042a160..13258c9e0 100644
--- a/hw/bsp/nrf/family.mk
+++ b/hw/bsp/nrf/family.mk
@@ -3,13 +3,12 @@ DEPS_SUBMODULES += lib/CMSIS_5 hw/mcu/nordic/nrfx
 
 include $(TOP)/$(BOARD_PATH)/board.mk
 
+CPU_CORE ?= cortex-m4
+
+include $(TOP)/tools/make/cpu/$(CPU_CORE).mk
+
 CFLAGS += \
   -flto \
-  -mthumb \
-  -mabi=aapcs \
-  -mcpu=cortex-m4 \
-  -mfloat-abi=hard \
-  -mfpu=fpv4-sp-d16 \
   -DCFG_TUSB_MCU=OPT_MCU_NRF5X \
   -DCONFIG_GPIO_AS_PINRESET
 
diff --git a/tools/make/cpu/cortex-m33.mk b/tools/make/cpu/cortex-m33.mk
new file mode 100644
index 000000000..cae8c6e3a
--- /dev/null
+++ b/tools/make/cpu/cortex-m33.mk
@@ -0,0 +1,11 @@
+ifeq ($(TOOLCHAIN),gcc)
+  CFLAGS += \
+    -mthumb \
+    -mcpu=cortex-m33 \
+    -mfloat-abi=hard \
+    -mfpu=fpv5-d16 \
+
+  #set(FREERTOS_PORT GCC_ARM_CM33_NONSECURE CACHE INTERNAL "")
+else ifeq ($(TOOLCHAIN),iar)
+  # TODO support IAR
+endif
diff --git a/tools/make/cpu/cortex-m4.mk b/tools/make/cpu/cortex-m4.mk
new file mode 100644
index 000000000..7a3e204e2
--- /dev/null
+++ b/tools/make/cpu/cortex-m4.mk
@@ -0,0 +1,11 @@
+ifeq ($(TOOLCHAIN),gcc)
+  CFLAGS += \
+    -mthumb \
+    -mcpu=cortex-m4 \
+    -mfloat-abi=hard \
+    -mfpu=fpv4-sp-d16 \
+
+  #set(FREERTOS_PORT GCC_ARM_CM4F CACHE INTERNAL "")
+else ifeq ($(TOOLCHAIN),iar)
+  # TODO support IAR
+endif
diff --git a/tools/make/cpu/cortex-m7.mk b/tools/make/cpu/cortex-m7.mk
new file mode 100644
index 000000000..47de4e07d
--- /dev/null
+++ b/tools/make/cpu/cortex-m7.mk
@@ -0,0 +1,11 @@
+ifeq ($(TOOLCHAIN),gcc)
+  CFLAGS += \
+    -mthumb \
+    -mcpu=cortex-m7 \
+    -mfloat-abi=hard \
+    -mfpu=fpv5-d16 \
+
+  #set(FREERTOS_PORT GCC_ARM_CM7 CACHE INTERNAL "")
+else ifeq ($(TOOLCHAIN),iar)
+  # TODO support IAR
+endif

From 49d8d27770133ba4fe2f5d5798560e75df7824ce Mon Sep 17 00:00:00 2001
From: hathach <thach@tinyusb.org>
Date: Fri, 19 May 2023 16:27:07 +0700
Subject: [PATCH 3/4] improve flash target

---
 examples/rules.mk           |  17 +-
 hw/bsp/family_support.cmake | 346 +++++++++++++++++++++---------------
 hw/bsp/imxrt/family.cmake   |  34 +---
 hw/bsp/lpc55/family.cmake   |  17 +-
 hw/bsp/nrf/family.cmake     |  21 +--
 5 files changed, 219 insertions(+), 216 deletions(-)

diff --git a/examples/rules.mk b/examples/rules.mk
index 426000128..f6422092a 100644
--- a/examples/rules.mk
+++ b/examples/rules.mk
@@ -227,14 +227,19 @@ endif
 # Jlink Interface
 JLINK_IF ?= swd
 
+# Jlink script
+define jlink_script
+halt
+loadfile $^
+r
+go
+exit
+endef
+export jlink_script
+
 # Flash using jlink
 flash-jlink: $(BUILD)/$(PROJECT).hex
-	@echo halt > $(BUILD)/$(BOARD).jlink
-	@echo r >> $(BUILD)/$(BOARD).jlink
-	@echo loadfile $^ >> $(BUILD)/$(BOARD).jlink
-	@echo r >> $(BUILD)/$(BOARD).jlink
-	@echo go >> $(BUILD)/$(BOARD).jlink
-	@echo exit >> $(BUILD)/$(BOARD).jlink
+	@echo "$$jlink_script" > $(BUILD)/$(BOARD).jlink
 	$(JLINKEXE) -device $(JLINK_DEVICE) -if $(JLINK_IF) -JTAGConf -1,-1 -speed auto -CommandFile $(BUILD)/$(BOARD).jlink
 
 # Flash STM32 MCU using stlink with STM32 Cube Programmer CLI
diff --git a/hw/bsp/family_support.cmake b/hw/bsp/family_support.cmake
index 01e7bb018..442aba955 100644
--- a/hw/bsp/family_support.cmake
+++ b/hw/bsp/family_support.cmake
@@ -1,149 +1,205 @@
-if (NOT TARGET _family_support_marker)
-    add_library(_family_support_marker INTERFACE)
+if (TARGET _family_support_marker)
+    return()
+endif ()
 
-    include(CMakePrintHelpers)
+add_library(_family_support_marker INTERFACE)
 
-    # Default to gcc
-    if(NOT DEFINED TOOLCHAIN)
-        set(TOOLCHAIN gcc)
-    endif()
+include(CMakePrintHelpers)
 
-    if (NOT FAMILY)
-        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")
-    endif()
-
-    if (NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/${FAMILY}/family.cmake)
-        message(FATAL_ERROR "Family '${FAMILY}' is not known/supported")
-    endif()
-
-    function(family_filter RESULT DIR)
-        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()
-
-            # Didn't find in skip file so build
-            set(${RESULT} 1 PARENT_SCOPE)
-
-        else()
-
-            # Didn't find skip or only file so build
-            set(${RESULT} 1 PARENT_SCOPE)
-
-        endif()
-
-    endfunction()
-
-    function(family_add_subdirectory DIR)
-        family_filter(SHOULD_ADD "${DIR}")
-        if (SHOULD_ADD)
-            add_subdirectory(${DIR})
-        endif()
-    endfunction()
-
-    function(family_get_project_name OUTPUT_NAME DIR)
-        get_filename_component(SHORT_NAME ${DIR} NAME)
-        set(${OUTPUT_NAME} ${TINYUSB_FAMILY_PROJECT_NAME_PREFIX}${SHORT_NAME} PARENT_SCOPE)
-    endfunction()
-
-    function(family_initialize_project PROJECT DIR)
-        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()
-    endfunction()
-
-    function(family_add_default_example_warnings TARGET)
-        target_compile_options(${TARGET} PUBLIC
-                -Wall
-                -Wextra
-                -Werror
-                -Wfatal-errors
-                -Wdouble-promotion
-                -Wfloat-equal
-                -Wshadow
-                -Wwrite-strings
-                -Wsign-compare
-                -Wmissing-format-attribute
-                -Wunreachable-code
-                -Wcast-align
-                -Wcast-qual
-                -Wnull-dereference
-                -Wuninitialized
-                -Wunused
-                -Wredundant-decls
-                #-Wstrict-prototypes
-                #-Werror-implicit-function-declaration
-                #-Wundef
-                )
-
-        if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
-            # 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()
-
-    # 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()
-
-    include(${CMAKE_CURRENT_LIST_DIR}/${FAMILY}/family.cmake)
-
-    if (NOT FAMILY_MCUS)
-        set(FAMILY_MCUS ${FAMILY})
-    endif()
-
-    # save it in case of re-inclusion
-    set(FAMILY_MCUS ${FAMILY_MCUS} CACHE INTERNAL "")
+# Default to gcc
+if(NOT DEFINED TOOLCHAIN)
+    set(TOOLCHAIN gcc)
 endif()
+
+if (NOT FAMILY)
+    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")
+endif()
+
+if (NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/${FAMILY}/family.cmake)
+    message(FATAL_ERROR "Family '${FAMILY}' is not known/supported")
+endif()
+
+function(family_filter RESULT DIR)
+    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()
+
+        # Didn't find in skip file so build
+        set(${RESULT} 1 PARENT_SCOPE)
+
+    else()
+
+        # Didn't find skip or only file so build
+        set(${RESULT} 1 PARENT_SCOPE)
+
+    endif()
+
+endfunction()
+
+function(family_add_subdirectory DIR)
+    family_filter(SHOULD_ADD "${DIR}")
+    if (SHOULD_ADD)
+        add_subdirectory(${DIR})
+    endif()
+endfunction()
+
+function(family_get_project_name OUTPUT_NAME DIR)
+    get_filename_component(SHORT_NAME ${DIR} NAME)
+    set(${OUTPUT_NAME} ${TINYUSB_FAMILY_PROJECT_NAME_PREFIX}${SHORT_NAME} PARENT_SCOPE)
+endfunction()
+
+function(family_initialize_project PROJECT DIR)
+    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()
+endfunction()
+
+function(family_add_default_example_warnings TARGET)
+    target_compile_options(${TARGET} PUBLIC
+            -Wall
+            -Wextra
+            -Werror
+            -Wfatal-errors
+            -Wdouble-promotion
+            -Wfloat-equal
+            -Wshadow
+            -Wwrite-strings
+            -Wsign-compare
+            -Wmissing-format-attribute
+            -Wunreachable-code
+            -Wcast-align
+            -Wcast-qual
+            -Wnull-dereference
+            -Wuninitialized
+            -Wunused
+            -Wredundant-decls
+            #-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()
+
+# Add flash jlink target
+function(family_flash_jlink TARGET)
+    if (NOT DEFINED JLINKEXE)
+        set(JLINKEXE JLinkExe)
+    endif ()
+
+    file(GENERATE
+      OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.jlink
+      CONTENT "halt
+loadfile $<TARGET_FILE:${TARGET}>
+r
+go
+exit"
+      )
+
+    add_custom_target(${TARGET}-jlink
+      DEPENDS ${TARGET}
+      COMMAND ${JLINKEXE} -device ${JLINK_DEVICE} -if swd -JTAGConf -1,-1 -speed auto -CommandFile ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.jlink
+      )
+endfunction()
+
+# Add flash pycod target
+function(family_flash_pyocd TARGET)
+    if (NOT DEFINED PYOC)
+        set(PYOCD pyocd)
+    endif ()
+
+    add_custom_target(${TARGET}-pyocd
+      DEPENDS ${TARGET}
+      COMMAND ${PYOCD} flash -t ${PYOCD_TARGET} $<TARGET_FILE:${TARGET}>
+      )
+endfunction()
+
+# 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)
+    if (NOT DEFINED LINKSERVER)
+        set(LINKSERVER LinkServer)
+    endif ()
+
+    # 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)
+
+    add_custom_target(${TARGET}-nxplink
+      DEPENDS ${TARGET}
+      COMMAND ${LINKSERVER_PATH} flash ${NXPLINK_DEVICE} load $<TARGET_FILE:${TARGET}>
+      )
+endfunction()
+
+# 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()
+
+include(${CMAKE_CURRENT_LIST_DIR}/${FAMILY}/family.cmake)
+
+if (NOT FAMILY_MCUS)
+    set(FAMILY_MCUS ${FAMILY})
+endif()
+
+# save it in case of re-inclusion
+set(FAMILY_MCUS ${FAMILY_MCUS} CACHE INTERNAL "")
diff --git a/hw/bsp/imxrt/family.cmake b/hw/bsp/imxrt/family.cmake
index e9be7acc8..5261e5810 100644
--- a/hw/bsp/imxrt/family.cmake
+++ b/hw/bsp/imxrt/family.cmake
@@ -132,37 +132,9 @@ function(family_configure_target TARGET)
     )
 
   #---------- Flash ----------
-  # Flash using pyocd
-  add_custom_target(${TARGET}-pyocd
-    DEPENDS ${TARGET}
-    COMMAND pyocd flash -t ${PYOCD_TARGET} $<TARGET_FILE:${TARGET}>
-    )
-
-  # Flash using NXP LinkServer (redlink)
-  # https://www.nxp.com/design/software/development-software/mcuxpresso-software-and-tools-/linkserver-for-microcontrollers:LINKERSERVER
-  # 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)
-  add_custom_target(${TARGET}-nxplink
-    DEPENDS ${TARGET}
-    COMMAND ${LINKSERVER_PATH} flash ${NXPLINK_DEVICE} load $<TARGET_FILE:${TARGET}>
-    )
-
-  # Flash using jlink
-  set(JLINKEXE JLinkExe)
-  file(GENERATE
-    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.jlink
-    CONTENT "halt
-loadfile $<TARGET_FILE:${TARGET}>
-r
-go
-exit"
-    )
-  add_custom_target(${TARGET}-jlink
-    DEPENDS ${TARGET}
-    COMMAND ${JLINKEXE} -device ${JLINK_DEVICE} -if swd -JTAGConf -1,-1 -speed auto -CommandFile ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.jlink
-    )
-
+  family_flash_jlink(${TARGET})
+  family_flash_nxplink(${TARGET})
+  family_flash_pyocd(${TARGET})
 endfunction()
 
 
diff --git a/hw/bsp/lpc55/family.cmake b/hw/bsp/lpc55/family.cmake
index 439f54170..611c88eb5 100644
--- a/hw/bsp/lpc55/family.cmake
+++ b/hw/bsp/lpc55/family.cmake
@@ -128,20 +128,9 @@ function(family_configure_target TARGET)
     )
 
   #---------- Flash ----------
-  # Flash using pyocd
-  add_custom_target(${TARGET}-pyocd
-    COMMAND pyocd flash -t ${PYOCD_TARGET} $<TARGET_FILE:${TARGET}>
-    )
-
-  # Flash using NXP LinkServer (redlink)
-  # https://www.nxp.com/design/software/development-software/mcuxpresso-software-and-tools-/linkserver-for-microcontrollers:LINKERSERVER
-  # 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)
-  add_custom_target(${TARGET}-nxplink
-    COMMAND ${LINKSERVER_PATH} flash ${NXPLINK_DEVICE} load $<TARGET_FILE:${TARGET}>
-    )
-
+  family_flash_jlink(${TARGET})
+  family_flash_nxplink(${TARGET})
+  family_flash_pyocd(${TARGET})
 endfunction()
 
 
diff --git a/hw/bsp/nrf/family.cmake b/hw/bsp/nrf/family.cmake
index ca28c4f5d..9c43478e8 100644
--- a/hw/bsp/nrf/family.cmake
+++ b/hw/bsp/nrf/family.cmake
@@ -132,26 +132,7 @@ function(family_configure_target TARGET)
     )
 
   #---------- Flash ----------
-  # Flash using pyocd
-  add_custom_target(${TARGET}-pyocd
-    DEPENDS ${TARGET}
-    COMMAND pyocd flash -t ${PYOCD_TARGET} $<TARGET_FILE:${TARGET}>
-    )
-
-  # Flash using jlink
-  set(JLINKEXE JLinkExe)
-  file(GENERATE
-    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.jlink
-    CONTENT "halt
-loadfile $<TARGET_FILE:${TARGET}>
-r
-go
-exit"
-    )
-  add_custom_target(${TARGET}-jlink
-    DEPENDS ${TARGET}
-    COMMAND ${JLINKEXE} -device ${JLINK_DEVICE} -if swd -JTAGConf -1,-1 -speed auto -CommandFile ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.jlink
-    )
+  family_flash_jlink(${TARGET})
 
 endfunction()
 

From 9f0bae4c3fb687b8a3c1e6df84512ec48690369f Mon Sep 17 00:00:00 2001
From: hathach <thach@tinyusb.org>
Date: Fri, 19 May 2023 18:04:08 +0700
Subject: [PATCH 4/4] fix freertos build with nrf

---
 hw/bsp/nrf/family.mk         | 5 +----
 tools/make/cpu/cortex-m33.mk | 1 +
 tools/make/cpu/cortex-m4.mk  | 1 +
 tools/make/cpu/cortex-m7.mk  | 1 +
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/hw/bsp/nrf/family.mk b/hw/bsp/nrf/family.mk
index 13258c9e0..6d067e1c2 100644
--- a/hw/bsp/nrf/family.mk
+++ b/hw/bsp/nrf/family.mk
@@ -3,8 +3,8 @@ DEPS_SUBMODULES += lib/CMSIS_5 hw/mcu/nordic/nrfx
 
 include $(TOP)/$(BOARD_PATH)/board.mk
 
+# nRF52 is cortex-m4, nRF53 is cortex-m33
 CPU_CORE ?= cortex-m4
-
 include $(TOP)/tools/make/cpu/$(CPU_CORE).mk
 
 CFLAGS += \
@@ -36,8 +36,5 @@ SRC_S += hw/mcu/nordic/nrfx/mdk/gcc_startup_$(MCU_VARIANT).S
 
 ASFLAGS += -D__HEAP_SIZE=0
 
-# For freeRTOS port source
-FREERTOS_PORTABLE_SRC = $(FREERTOS_PORTABLE_PATH)/ARM_CM4F
-
 # For flash-jlink target
 JLINK_DEVICE ?= $(MCU_VARIANT)_xxaa
diff --git a/tools/make/cpu/cortex-m33.mk b/tools/make/cpu/cortex-m33.mk
index cae8c6e3a..2ea3a0753 100644
--- a/tools/make/cpu/cortex-m33.mk
+++ b/tools/make/cpu/cortex-m33.mk
@@ -6,6 +6,7 @@ ifeq ($(TOOLCHAIN),gcc)
     -mfpu=fpv5-d16 \
 
   #set(FREERTOS_PORT GCC_ARM_CM33_NONSECURE CACHE INTERNAL "")
+  FREERTOS_PORTABLE_SRC = $(FREERTOS_PORTABLE_PATH)/ARM_CM33_NTZ/non_secure
 else ifeq ($(TOOLCHAIN),iar)
   # TODO support IAR
 endif
diff --git a/tools/make/cpu/cortex-m4.mk b/tools/make/cpu/cortex-m4.mk
index 7a3e204e2..890feefe3 100644
--- a/tools/make/cpu/cortex-m4.mk
+++ b/tools/make/cpu/cortex-m4.mk
@@ -6,6 +6,7 @@ ifeq ($(TOOLCHAIN),gcc)
     -mfpu=fpv4-sp-d16 \
 
   #set(FREERTOS_PORT GCC_ARM_CM4F CACHE INTERNAL "")
+  FREERTOS_PORTABLE_SRC = $(FREERTOS_PORTABLE_PATH)/ARM_CM4F
 else ifeq ($(TOOLCHAIN),iar)
   # TODO support IAR
 endif
diff --git a/tools/make/cpu/cortex-m7.mk b/tools/make/cpu/cortex-m7.mk
index 47de4e07d..504ffd486 100644
--- a/tools/make/cpu/cortex-m7.mk
+++ b/tools/make/cpu/cortex-m7.mk
@@ -6,6 +6,7 @@ ifeq ($(TOOLCHAIN),gcc)
     -mfpu=fpv5-d16 \
 
   #set(FREERTOS_PORT GCC_ARM_CM7 CACHE INTERNAL "")
+  FREERTOS_PORTABLE_SRC = $(FREERTOS_PORTABLE_PATH)/ARM_CM7/r0p1
 else ifeq ($(TOOLCHAIN),iar)
   # TODO support IAR
 endif