From d478064a80d81fbbeb7a2a3cf67152d6f61fbd9d Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 23 Aug 2018 14:45:22 +0700 Subject: [PATCH] freeRtos seems to work well with nrf52840 --- examples/device/nrf52840_freertos/src/main.c | 47 ++++++++++--------- .../nrf52840_freertos/src/tusb_config.h | 2 +- lib/FreeRTOS/FreeRTOSConfig.h | 2 +- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/examples/device/nrf52840_freertos/src/main.c b/examples/device/nrf52840_freertos/src/main.c index 644829a6..a0c41892 100644 --- a/examples/device/nrf52840_freertos/src/main.c +++ b/examples/device/nrf52840_freertos/src/main.c @@ -61,8 +61,8 @@ //--------------------------------------------------------------------+ void print_greeting(void); void led_blinky_cb(TimerHandle_t xTimer); -void virtual_com_task(void); -void usb_hid_task(void); +void cdc_task(void* params); +void usb_hid_task(void* params); /*------------- MAIN -------------*/ @@ -71,49 +71,54 @@ int main(void) board_init(); print_greeting(); - //tusb_init(); - // soft timer for blinky TimerHandle_t tm_hdl = xTimerCreate(NULL, pdMS_TO_TICKS(1000), true, NULL, led_blinky_cb); - xTimerStart(tm_hdl, 0); + xTimerStart(tm_hdl, 0); + + tusb_init(); + + // Create task + xTaskCreate( cdc_task, "cdc", 256, NULL, 2, NULL); vTaskStartScheduler(); NVIC_SystemReset(); -// while (1) -// { -// virtual_com_task(); -// -// usb_hid_task(); -// } - return 0; } //--------------------------------------------------------------------+ // USB CDC //--------------------------------------------------------------------+ -void virtual_com_task(void) +void cdc_task(void* params) { - // connected and there are data available - if ( tud_mounted() && tud_cdc_available() ) + (void) params; + + while ( 1 ) { - uint8_t buf[64]; + // connected and there are data available + if ( tud_mounted() && tud_cdc_available() ) + { + uint8_t buf[64]; - // read and echo back - uint32_t count = tud_cdc_read(buf, sizeof(buf)); + // read and echo back + uint32_t count = tud_cdc_read(buf, sizeof(buf)); - tud_cdc_write(buf, count); - tud_cdc_write_flush(); + tud_cdc_write(buf, count); + tud_cdc_write_flush(); + } + + taskYIELD(); } } //--------------------------------------------------------------------+ // USB HID //--------------------------------------------------------------------+ -void usb_hid_task(void) +void usb_hid_task(void* params) { + (void) params; + // Poll every 10ms static tu_timeout_t tm = { .start = 0, .interval = 10 }; diff --git a/examples/device/nrf52840_freertos/src/tusb_config.h b/examples/device/nrf52840_freertos/src/tusb_config.h index 6b8b0af9..bd27f58b 100644 --- a/examples/device/nrf52840_freertos/src/tusb_config.h +++ b/examples/device/nrf52840_freertos/src/tusb_config.h @@ -53,7 +53,7 @@ /*------------- RTOS -------------*/ #define CFG_TUSB_OS OPT_OS_FREERTOS -#define CFG_TUD_TASK_PRIO (configMAX_PRIORITIES-2) +#define CFG_TUD_TASK_PRIO (configMAX_PRIORITIES-3) //#define CFG_TUD_TASK_QUEUE_SZ 16 //#define CFG_TUD_TASK_STACK_SZ 150 diff --git a/lib/FreeRTOS/FreeRTOSConfig.h b/lib/FreeRTOS/FreeRTOSConfig.h index ef936727..721087bc 100644 --- a/lib/FreeRTOS/FreeRTOSConfig.h +++ b/lib/FreeRTOS/FreeRTOSConfig.h @@ -20,7 +20,7 @@ #define configUSE_PREEMPTION 1 #define configUSE_PORT_OPTIMISED_TASK_SELECTION 0 #define configTICK_RATE_HZ ( 1000 ) -#define configMAX_PRIORITIES (8 ) +#define configMAX_PRIORITIES (5) #define configMINIMAL_STACK_SIZE (128 ) #define configTOTAL_HEAP_SIZE ( ( size_t ) ( 16*1024 ) ) #define configMAX_TASK_NAME_LEN 32