Просмотр исходного кода

update freertos example

work with samd21 samd51 and nrf
hathach 6 лет назад
Родитель
Сommit
65578dec44

+ 1 - 1
examples/device/cdc_msc_freertos/Makefile

@@ -20,7 +20,7 @@ SRC_C += \
 	$(FREERTOS_SRC)/tasks.c \
 	$(FREERTOS_SRC)/timers.c \
 	$(FREERTOS_SRC)/portable/MemMang/heap_4.c \
-	$(FREERTOS_SRC)/portable/GCC/$(FREERTOS_PORT)/port.c \
+	$(FREERTOS_SRC)/portable/GCC/$(FREERTOS_PORT)/port.c
 
 # FreeRTOS (lto + Os) linker issue
 LDFLAGS += -Wl,--undefined=vTaskSwitchContext

+ 21 - 21
hw/mcu/nordic/FreeRTOSConfig.h → examples/device/cdc_msc_freertos/src/FreeRTOSConfig.h

@@ -41,8 +41,17 @@
  *
  * See http://www.freertos.org/a00110.html.
  *----------------------------------------------------------*/
-#include "nrf.h"
 
+// for OPT_MCU_
+#include "tusb_option.h"
+
+#if CFG_TUSB_MCU == OPT_MCU_NRF5X
+  #include "nrf.h"
+#elif CFG_TUSB_MCU == OPT_MCU_SAMD21 || CFG_TUSB_MCU == OPT_MCU_SAMD51
+  #include "sam.h"
+#else
+  #error "FreeRTOSConfig.h need to include low level mcu header for configuration"
+#endif
 
 #define configUSE_PREEMPTION                    1
 #define configUSE_PORT_OPTIMISED_TASK_SELECTION 0
@@ -50,7 +59,7 @@
 #define configTICK_RATE_HZ                      ( 1000 )
 #define configMAX_PRIORITIES                    ( 5 )
 #define configMINIMAL_STACK_SIZE                ( 128 )
-#define configTOTAL_HEAP_SIZE                   ( 16*1024 )
+#define configTOTAL_HEAP_SIZE                   ( 4*1024 )
 #define configMAX_TASK_NAME_LEN                 16
 #define configUSE_16_BIT_TICKS                  0
 #define configIDLE_SHOULD_YIELD                 1
@@ -108,18 +117,18 @@
 /* Define to trap errors during development. */
 // Halt CPU (breakpoint) when hitting error, only apply for Cortex M3, M4, M7
 #if defined(__ARM_ARCH_7M__) || defined (__ARM_ARCH_7EM__)
-#define configASSERT(_exp) \
-  do {\
-    if ( !(_exp) ) { \
-      volatile uint32_t* ARM_CM_DHCSR =  ((volatile uint32_t*) 0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */ \
-      if ( (*ARM_CM_DHCSR) & 1UL ) {  /* Only halt mcu if debugger is attached */ \
-        taskDISABLE_INTERRUPTS(); \
-         __asm("BKPT #0\n"); \
+  #define configASSERT(_exp) \
+    do {\
+      if ( !(_exp) ) { \
+        volatile uint32_t* ARM_CM_DHCSR =  ((volatile uint32_t*) 0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */ \
+        if ( (*ARM_CM_DHCSR) & 1UL ) {  /* Only halt mcu if debugger is attached */ \
+          taskDISABLE_INTERRUPTS(); \
+           __asm("BKPT #0\n"); \
+        }\
       }\
-    }\
-  } while(0)
+    } while(0)
 #else
-#define configASSERT( x )
+  #define configASSERT( x )
 #endif
 
 /* FreeRTOS hooks to NVIC vectors */
@@ -144,15 +153,6 @@
 routine that makes calls to interrupt safe FreeRTOS API functions.  DO NOT CALL
 INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER
 PRIORITY THAN THIS! (higher priorities are lower numeric values. */
-
-/* SD priority
- * 0: SD timing critical
- * 1: SD memory protection
- * 2: App Highest
- * 3: App High
- * 4: SD non-time-critical
- * 5+ Remaining Application
- */
 #define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY	2
 
 /* Interrupt priorities used by the kernel port layer itself.  These are generic

+ 12 - 2
examples/device/cdc_msc_freertos/src/main.c

@@ -53,6 +53,16 @@ enum  {
 
 TimerHandle_t blink_tm;
 
+// static task
+#define USBD_STACK_SIZE     150
+StackType_t  stack_usbd[USBD_STACK_SIZE];
+StaticTask_t static_task_usbd;
+
+#define CDC_STACK_SZIE      128
+StackType_t  stack_cdc[CDC_STACK_SZIE];
+StaticTask_t static_task_cdc;
+
+
 void led_blinky_cb(TimerHandle_t xTimer);
 void usb_device_task(void* param);
 void cdc_task(void* params);
@@ -69,11 +79,11 @@ int main(void)
   tusb_init();
 
   // Create a task for tinyusb device stack
-  xTaskCreate( usb_device_task, "usbd", 150, NULL, configMAX_PRIORITIES-1, NULL);
+  (void) xTaskCreateStatic( usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, stack_usbd, &static_task_usbd);
 
   // Create task
 #if CFG_TUD_CDC
-  xTaskCreate( cdc_task, "cdc", 128, NULL, configMAX_PRIORITIES-2, NULL);
+  (void) xTaskCreateStatic( cdc_task, "cdc", CDC_STACK_SZIE, NULL, configMAX_PRIORITIES-2, stack_cdc, &static_task_cdc);
 #endif
 
   vTaskStartScheduler();

+ 2 - 1
hw/bsp/circuitplayground_express/board.mk

@@ -41,4 +41,5 @@ JLINK_DEVICE = ATSAMD21G18
 JLINK_IF = swd
 
 # flash using jlink
-flash: flash-jlink
+flash: $(BUILD)/$(BOARD)-firmware.uf2
+	cp $< /media/$(USER)/CPLAYBOOT/

+ 8 - 3
hw/bsp/circuitplayground_express/circuitplayground_express.c

@@ -60,6 +60,11 @@ void board_init(void)
   _sysctrl_init_referenced_generators();
   _gclk_init_generators_by_fref(_GCLK_INIT_LAST);
 
+  // Update SystemCoreClock since it is hard coded with asf4 and not correct
+  // Init 1ms tick timer (samd SystemCoreClock may not correct)
+  SystemCoreClock = CONF_CPU_FREQUENCY;
+  SysTick_Config(CONF_CPU_FREQUENCY / 1000);
+
   // Led init
   gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT);
   gpio_set_pin_level(LED_PIN, 0);
@@ -68,9 +73,9 @@ void board_init(void)
   gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN);
   gpio_set_pin_pull_mode(BUTTON_PIN, GPIO_PULL_DOWN);
 
-#if CFG_TUSB_OS  == OPT_OS_NONE
-  // 1ms tick timer (samd SystemCoreClock may not correct)
-  SysTick_Config(CONF_CPU_FREQUENCY / 1000);
+#if CFG_TUSB_OS  == OPT_OS_FREERTOS
+  // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
+  NVIC_SetPriority(USB_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
 #endif
 
   /* USB Clock init

+ 8 - 3
hw/bsp/feather_m0_express/feather_m0_express.c

@@ -60,6 +60,11 @@ void board_init(void)
   _sysctrl_init_referenced_generators();
   _gclk_init_generators_by_fref(_GCLK_INIT_LAST);
 
+  // Update SystemCoreClock since it is hard coded with asf4 and not correct
+  // Init 1ms tick timer (samd SystemCoreClock may not correct)
+  SystemCoreClock = CONF_CPU_FREQUENCY;
+  SysTick_Config(CONF_CPU_FREQUENCY / 1000);
+
   // Led init
   gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT);
   gpio_set_pin_level(LED_PIN, 0);
@@ -68,9 +73,9 @@ void board_init(void)
   gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN);
   gpio_set_pin_pull_mode(BUTTON_PIN, GPIO_PULL_UP);
 
-#if CFG_TUSB_OS  == OPT_OS_NONE
-  // 1ms tick timer (samd SystemCoreClock may not correct)
-  SysTick_Config(CONF_CPU_FREQUENCY / 1000);
+#if CFG_TUSB_OS  == OPT_OS_FREERTOS
+  // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
+  NVIC_SetPriority(USB_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
 #endif
 
   /* USB Clock init

+ 11 - 3
hw/bsp/feather_m4_express/feather_m4_express.c

@@ -58,6 +58,11 @@ void board_init(void)
   _oscctrl_init_referenced_generators();
   _gclk_init_generators_by_fref(_GCLK_INIT_LAST);
 
+  // Update SystemCoreClock since it is hard coded with asf4 and not correct
+  // Init 1ms tick timer (samd SystemCoreClock may not correct)
+  SystemCoreClock = CONF_CPU_FREQUENCY;
+  SysTick_Config(CONF_CPU_FREQUENCY / 1000);
+
   // Led init
   gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT);
   gpio_set_pin_level(LED_PIN, 0);
@@ -66,9 +71,12 @@ void board_init(void)
   gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN);
   gpio_set_pin_pull_mode(BUTTON_PIN, GPIO_PULL_UP);
 
-#if CFG_TUSB_OS  == OPT_OS_NONE
-  // 1ms tick timer (samd SystemCoreClock may not correct)
-  SysTick_Config(CONF_CPU_FREQUENCY / 1000);
+#if CFG_TUSB_OS  == OPT_OS_FREERTOS
+  // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
+  NVIC_SetPriority(USB_0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
+  NVIC_SetPriority(USB_1_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
+  NVIC_SetPriority(USB_2_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
+  NVIC_SetPriority(USB_3_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
 #endif
 
   /* USB Clock init

+ 0 - 3
hw/bsp/feather_nrf52840_express/feather_nrf52840_express.c

@@ -71,10 +71,8 @@ void board_init(void)
   // Button
   nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_PULLUP);
 
-#if CFG_TUSB_OS == OPT_OS_NONE
   // 1ms tick timer
   SysTick_Config(SystemCoreClock/1000);
-#endif
 
   // UART
   nrfx_uarte_config_t uart_cfg =
@@ -99,7 +97,6 @@ void board_init(void)
   // 2 is highest for application
   NVIC_SetPriority(USBD_IRQn, 2);
 
-
   // USB power may already be ready at this time -> no event generated
   // We need to invoke the handler based on the status initially
   uint32_t usb_reg;

+ 0 - 3
hw/bsp/feather_nrf52840_sense/feather_nrf52840_sense.c

@@ -62,17 +62,14 @@ void board_init(void)
   // Button
   nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_PULLUP);
 
-#if CFG_TUSB_OS == OPT_OS_NONE
   // 1ms tick timer
   SysTick_Config(SystemCoreClock/1000);
-#endif
 
 #if TUSB_OPT_DEVICE_ENABLED
   // Priorities 0, 1, 4 (nRF52) are reserved for SoftDevice
   // 2 is highest for application
   NVIC_SetPriority(USBD_IRQn, 2);
 
-
   // USB power may already be ready at this time -> no event generated
   // We need to invoke the handler based on the status initially
   uint32_t usb_reg;

+ 8 - 3
hw/bsp/itsybitsy_m0/itsybitsy_m0.c

@@ -60,6 +60,11 @@ void board_init(void)
   _sysctrl_init_referenced_generators();
   _gclk_init_generators_by_fref(_GCLK_INIT_LAST);
 
+  // Update SystemCoreClock since it is hard coded with asf4 and not correct
+  // Init 1ms tick timer (samd SystemCoreClock may not correct)
+  SystemCoreClock = CONF_CPU_FREQUENCY;
+  SysTick_Config(CONF_CPU_FREQUENCY / 1000);
+
   // Led init
   gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT);
   gpio_set_pin_level(LED_PIN, 0);
@@ -68,9 +73,9 @@ void board_init(void)
   gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN);
   gpio_set_pin_pull_mode(BUTTON_PIN, GPIO_PULL_UP);
 
-#if CFG_TUSB_OS  == OPT_OS_NONE
-  // 1ms tick timer (samd SystemCoreClock may not correct)
-  SysTick_Config(CONF_CPU_FREQUENCY / 1000);
+#if CFG_TUSB_OS  == OPT_OS_FREERTOS
+  // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
+  NVIC_SetPriority(USB_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
 #endif
 
   /* USB Clock init

+ 11 - 3
hw/bsp/itsybitsy_m4/itsybitsy_m4.c

@@ -58,6 +58,11 @@ void board_init(void)
   _oscctrl_init_referenced_generators();
   _gclk_init_generators_by_fref(_GCLK_INIT_LAST);
 
+  // Update SystemCoreClock since it is hard coded with asf4 and not correct
+  // Init 1ms tick timer (samd SystemCoreClock may not correct)
+  SystemCoreClock = CONF_CPU_FREQUENCY;
+  SysTick_Config(CONF_CPU_FREQUENCY / 1000);
+
   // Led init
   gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT);
   gpio_set_pin_level(LED_PIN, 0);
@@ -66,9 +71,12 @@ void board_init(void)
   gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN);
   gpio_set_pin_pull_mode(BUTTON_PIN, GPIO_PULL_UP);
 
-#if CFG_TUSB_OS  == OPT_OS_NONE
-  // 1ms tick timer (samd SystemCoreClock may not correct)
-  SysTick_Config(CONF_CPU_FREQUENCY / 1000);
+#if CFG_TUSB_OS  == OPT_OS_FREERTOS
+  // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
+  NVIC_SetPriority(USB_0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
+  NVIC_SetPriority(USB_1_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
+  NVIC_SetPriority(USB_2_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
+  NVIC_SetPriority(USB_3_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
 #endif
 
   /* USB Clock init

+ 8 - 3
hw/bsp/metro_m0_express/metro_m0_express.c

@@ -60,6 +60,11 @@ void board_init(void)
   _sysctrl_init_referenced_generators();
   _gclk_init_generators_by_fref(_GCLK_INIT_LAST);
 
+  // Update SystemCoreClock since it is hard coded with asf4 and not correct
+  // Init 1ms tick timer (samd SystemCoreClock may not correct)
+  SystemCoreClock = CONF_CPU_FREQUENCY;
+  SysTick_Config(CONF_CPU_FREQUENCY / 1000);
+
   // Led init
   gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT);
   gpio_set_pin_level(LED_PIN, 0);
@@ -68,9 +73,9 @@ void board_init(void)
   gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN);
   gpio_set_pin_pull_mode(BUTTON_PIN, GPIO_PULL_UP);
 
-#if CFG_TUSB_OS  == OPT_OS_NONE
-  // 1ms tick timer (samd SystemCoreClock may not correct)
-  SysTick_Config(CONF_CPU_FREQUENCY / 1000);
+#if CFG_TUSB_OS  == OPT_OS_FREERTOS
+  // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
+  NVIC_SetPriority(USB_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
 #endif
 
   /* USB Clock init

+ 11 - 3
hw/bsp/metro_m4_express/metro_m4_express.c

@@ -58,6 +58,11 @@ void board_init(void)
   _oscctrl_init_referenced_generators();
   _gclk_init_generators_by_fref(_GCLK_INIT_LAST);
 
+  // Update SystemCoreClock since it is hard coded with asf4 and not correct
+  // Init 1ms tick timer (samd SystemCoreClock may not correct)
+  SystemCoreClock = CONF_CPU_FREQUENCY;
+  SysTick_Config(CONF_CPU_FREQUENCY / 1000);
+
   // Led init
   gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT);
   gpio_set_pin_level(LED_PIN, 0);
@@ -66,9 +71,12 @@ void board_init(void)
   gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN);
   gpio_set_pin_pull_mode(BUTTON_PIN, GPIO_PULL_UP);
 
-#if CFG_TUSB_OS  == OPT_OS_NONE
-  // 1ms tick timer (samd SystemCoreClock may not correct)
-  SysTick_Config(CONF_CPU_FREQUENCY / 1000);
+#if CFG_TUSB_OS  == OPT_OS_FREERTOS
+  // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
+  NVIC_SetPriority(USB_0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
+  NVIC_SetPriority(USB_1_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
+  NVIC_SetPriority(USB_2_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
+  NVIC_SetPriority(USB_3_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
 #endif
 
   /* USB Clock init

+ 0 - 2
hw/bsp/pca10056/pca10056.c

@@ -71,10 +71,8 @@ void board_init(void)
   // Button
   nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_PULLUP);
 
-#if CFG_TUSB_OS == OPT_OS_NONE
   // 1ms tick timer
   SysTick_Config(SystemCoreClock/1000);
-#endif
 
   // UART
   nrfx_uarte_config_t uart_cfg =

+ 0 - 2
hw/bsp/pca10059/pca10059.c

@@ -62,10 +62,8 @@ void board_init(void)
   // Button
   nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_PULLUP);
 
-#if CFG_TUSB_OS == OPT_OS_NONE
   // 1ms tick timer
   SysTick_Config(SystemCoreClock/1000);
-#endif
 
 #if TUSB_OPT_DEVICE_ENABLED
   // Priorities 0, 1, 4 (nRF52) are reserved for SoftDevice

+ 0 - 2
hw/bsp/pca10100/pca10100.c

@@ -71,10 +71,8 @@ void board_init(void)
   // Button
   nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_PULLUP);
 
-#if CFG_TUSB_OS == OPT_OS_NONE
   // 1ms tick timer
   SysTick_Config(SystemCoreClock/1000);
-#endif
 
   // UART
   nrfx_uarte_config_t uart_cfg =