not working!!! A test of semaphore passing from Alexander Dean's FreeRTOS workshop 2016
Dependencies: FreeRTOS_V8_2_1 mbed
Diff: main.cpp
- Revision:
- 0:34537a2d87bb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Apr 22 18:48:57 2016 +0000 @@ -0,0 +1,158 @@ +#include "mbed.h" +/* Scheduler includes. */ +#include "FreeRTOS.h" +#include "task.h" +#include "queue.h" +#include "semphr.h" +#include "croutine.h" + +uint64_t u64Ticks=0; // Counts OS ticks (default = 1000Hz). +uint64_t u64IdleTicks=0; // Value of u64IdleTicksCnt is copied once per sec. +uint64_t u64IdleTicksCnt=0; // Counts when the OS has no task to execute. +#define LED_NUM 3 /* Number of user LEDs */ + +volatile uint32_t msTicks; /* counts 1ms timeTicks */ + +#define mainTask1_LED_Priority ( tskIDLE_PRIORITY + 1 ) +#define mainTask2_LED_Priority ( tskIDLE_PRIORITY + 2 ) +#define mainTask3_LED_Priority ( tskIDLE_PRIORITY + 3 ) + +static void toggleLED1Task( void *pvParameters ); +static void toggleLED2Task( void *pvParameters ); +static void toggleLED3Task( void *pvParameters ); + +xSemaphoreHandle xSemaphore; + +DigitalOut myled1(PB_3); +DigitalOut myled2(PB_4); +DigitalOut myled3(PA_8); + + + +/*---------------------------------------------------------------------------- + MAIN function + *----------------------------------------------------------------------------*/ +int main (void) { + + xTaskCreate( toggleLED1Task , "T1_LED", configMINIMAL_STACK_SIZE, NULL, mainTask1_LED_Priority, NULL ); + xTaskCreate( toggleLED2Task , "T2_LED", configMINIMAL_STACK_SIZE, NULL, mainTask2_LED_Priority, NULL ); + xTaskCreate( toggleLED3Task , "T3_LED", configMINIMAL_STACK_SIZE, NULL, mainTask3_LED_Priority, NULL ); + myled1 = 0; + myled2 = 0; + myled3 = 0; + wait(1); + + vSemaphoreCreateBinary( xSemaphore ); + + /* Start the scheduler. */ + vTaskStartScheduler(); + + /* Will only get here if there was not enough heap space to create the + idle task. */ + + while(1) { + myled1 = 0; + wait(0.2); + myled1 = 1; + } + + return 0; +} + +/*-----------------------------------------------------------*/ +static void toggleLED1Task( void *pvParameters ) +{ + int i=0; + for( ;; ) + { + for(i=0; i<4; i++) + { + myled1 = 1; + wait(1); + myled1 = 0; + wait(1); + } + if( xSemaphoreTake( xSemaphore, portMAX_DELAY ) == pdTRUE ) + { + for(i=0; i<10; i++) + { + myled1 = 1; + wait(0.3); + myled1 = 0; + wait(0.3); + } + xSemaphoreGive(xSemaphore); + } + } +} +/*-----------------------------------------------------------*/ +/*-----------------------------------------------------------*/ +static void toggleLED2Task( void *pvParameters ) +{ + int i=0; + for( ;; ) + { + vTaskDelay(18000); + for(i=0; i<4; i++) { + myled2 = 1; + wait(0.2); + myled2 = 0; + wait(0.2); + } + } +} +/*-----------------------------------------------------------*/ +/*-----------------------------------------------------------*/ +static void toggleLED3Task( void *pvParameters ) +{ + int i=0; + for( ;; ) { + vTaskDelay(9500); + for(i=0; i<4; i++) { + myled3 = 1; + wait(1); + myled3 = 0; + wait(1); + } + if( xSemaphoreTake( xSemaphore, portMAX_DELAY ) == pdTRUE ) { + for(i=0; i<10; i++) { + myled3 = 1; + wait(0.3); + myled3 = 0; + wait(0.3); + } + xSemaphoreGive(xSemaphore); + } + } +} +/*-----------------------------------------------------------*/ + +/*-----------------------------------------------------------*/ +// This FreeRTOS callback function gets called once per tick (default = 1000Hz). +// ---------------------------------------------------------------------------- +void vApplicationTickHook( void ) { + ++u64Ticks; +} + +// This FreeRTOS call-back function gets when no other task is ready to execute. +// On a completely unloaded system this is getting called at over 2.5MHz! +// ---------------------------------------------------------------------------- +void vApplicationIdleHook( void ) { + ++u64IdleTicksCnt; +} + +// A required FreeRTOS function. +// ---------------------------------------------------------------------------- +void vApplicationMallocFailedHook( void ) { + configASSERT( 0 ); // Latch on any failure / error. +} + +/*-----------------------------------------------------------*/ +// This FreeRTOS callback function gets called once per tick (default = 1000Hz). +// ---------------------------------------------------------------------------- +void vApplicationStackOverflowHook( void ) { + myled1 = 1; + wait(0.5); + myled1 = 0; + wait(0.5); +}