From d7bff70718130f03d9e41a79bdb7feef2f2bf2d3 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 6 Jan 2017 18:51:37 +0700 Subject: [PATCH] finish rtos blinky --- cores/nRF5/Arduino.h | 25 ++++++++++--- cores/nRF5/freertos/config/FreeRTOSConfig.h | 4 +-- cores/nRF5/main.cpp | 39 ++++++++++++++++++--- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/cores/nRF5/Arduino.h b/cores/nRF5/Arduino.h index 39fbc0fe..6d7ba3fe 100644 --- a/cores/nRF5/Arduino.h +++ b/cores/nRF5/Arduino.h @@ -35,6 +35,8 @@ void yield( void ) ; void setup( void ) ; void loop( void ) ; +void setLoopStacksize(uint32_t size); + #include "WVariant.h" #ifdef __cplusplus @@ -50,8 +52,10 @@ void loop( void ) ; #include "HardwareSerial.h" #include "pulse.h" #endif + #include "delay.h" #include "binary.h" + #ifdef __cplusplus #include "Uart.h" #endif @@ -65,6 +69,23 @@ void loop( void ) ; #include "wiring_shift.h" #include "WInterrupts.h" +// RTOS +#include "FreeRTOS.h" +#include "task.h" +#include "timers.h" +#include "queue.h" +#include "semphr.h" + +enum +{ + TASK_PRIO_HIGHEST = 0, + TASK_PRIO_HIGH = 1, + TASK_PRIO_NORMAL = 2, + TASK_PRIO_LOW = 3, + TASK_PRIO_LOWEST = 4 +}; + + // undefine stdlib's abs if encountered #ifdef abs #undef abs @@ -112,8 +133,4 @@ void loop( void ) ; // Interrupts #define digitalPinToInterrupt(P) ( P ) -#ifdef __cplusplus -#include "Uart.h" -#endif // __cplusplus - #endif // Arduino_h diff --git a/cores/nRF5/freertos/config/FreeRTOSConfig.h b/cores/nRF5/freertos/config/FreeRTOSConfig.h index 3d6bd47c..e665d2a4 100644 --- a/cores/nRF5/freertos/config/FreeRTOSConfig.h +++ b/cores/nRF5/freertos/config/FreeRTOSConfig.h @@ -96,9 +96,9 @@ #define configUSE_TICKLESS_IDLE 0 #define configCPU_CLOCK_HZ ( SystemCoreClock ) #define configTICK_RATE_HZ 1000 -#define configMAX_PRIORITIES ( 3 ) +#define configMAX_PRIORITIES ( 5 ) #define configMINIMAL_STACK_SIZE ( 60 ) -#define configTOTAL_HEAP_SIZE ( 4096 ) +#define configTOTAL_HEAP_SIZE ( 4096 ) /* not used since we use malloc */ #define configMAX_TASK_NAME_LEN ( 4 ) #define configUSE_16_BIT_TICKS 0 #define configIDLE_SHOULD_YIELD 1 diff --git a/cores/nRF5/main.cpp b/cores/nRF5/main.cpp index 2a5247a3..1282608b 100644 --- a/cores/nRF5/main.cpp +++ b/cores/nRF5/main.cpp @@ -21,6 +21,27 @@ void initVariant() __attribute__((weak)); void initVariant() { } +TaskHandle_t _loopHandle; +uint32_t _loopStacksize = 1024*2; + +void setLoopStacksize(uint32_t size) +{ + _loopStacksize = size; +} + +static void loop_task(void* arg) +{ + (void) arg; + + while (1) + { + loop(); + + // To compatible with most code where loop is not rtos-aware + taskYIELD(); + } +} + /* * \brief Main entry point of Arduino application */ @@ -34,11 +55,19 @@ int main( void ) setup(); - for (;;) - { - loop(); - if (serialEventRun) serialEventRun(); - } + // Create a task for loop() + xTaskCreate( loop_task, "loop", _loopStacksize, NULL, TASK_PRIO_NORMAL, &_loopHandle); + + // Start FreeRTOS scheduler. + vTaskStartScheduler(); + + NVIC_SystemReset(); + +// for (;;) +// { +// loop(); +// if (serialEventRun) serialEventRun(); +// } return 0; }