|
| 1 | +/* The code in this example is mostly derived from the official FreeRTOS |
| 2 | + * code examples. |
| 3 | + * |
| 4 | + * For more information on static allocation and to read the original |
| 5 | + * code visit the following links: |
| 6 | + * https://www.freertos.org/Static_Vs_Dynamic_Memory_Allocation.html |
| 7 | + * https://www.freertos.org/xTaskCreateStatic.html |
| 8 | + * https://www.freertos.org/xSemaphoreCreateMutexStatic.html |
| 9 | + */ |
| 10 | + |
| 11 | +#include <FreeRTOS.h> |
| 12 | +#include <task.h> |
| 13 | +#include <semphr.h> |
| 14 | + |
| 15 | +#define SERIAL_PORT Serial1 |
| 16 | +#define BLINK_ON_TIME 250 |
| 17 | +#define BLINK_OFF_TIME 500 |
| 18 | + |
| 19 | +/* Dimensions of the buffer that the task being created will use as its stack. |
| 20 | + NOTE: This is the number of words the stack will hold, not the number of |
| 21 | + bytes. For example, if each stack item is 32-bits, and this is set to 100, |
| 22 | + then 400 bytes (100 * 32-bits) will be allocated. */ |
| 23 | +#define STACK_SIZE 200 |
| 24 | + |
| 25 | +/* Structure that will hold the TCB of the task being created. */ |
| 26 | +StaticTask_t xTaskBuffer_A; |
| 27 | +StaticTask_t xTaskBuffer_B; |
| 28 | + |
| 29 | +/* Buffer that the task being created will use as its stack. Note this is |
| 30 | + an array of StackType_t variables. The size of StackType_t is dependent on |
| 31 | + the RTOS port. */ |
| 32 | +StackType_t xStack_A[ STACK_SIZE ]; |
| 33 | +StackType_t xStack_B[ STACK_SIZE ]; |
| 34 | + |
| 35 | +SemaphoreHandle_t xSemaphore = NULL; |
| 36 | +StaticSemaphore_t xMutexBuffer; |
| 37 | + |
| 38 | +void setup() { |
| 39 | + SERIAL_PORT.begin(115200); |
| 40 | + pinMode(LED_BUILTIN, OUTPUT); |
| 41 | + |
| 42 | + /* Create a mutex semaphore without using any dynamic memory |
| 43 | + allocation. The mutex's data structures will be saved into |
| 44 | + the xMutexBuffer variable. */ |
| 45 | + xSemaphore = xSemaphoreCreateMutexStatic( &xMutexBuffer ); |
| 46 | + |
| 47 | + xTaskCreateStatic(led_ON, "led_ON", STACK_SIZE, NULL, configMAX_PRIORITIES - 1, xStack_A, &xTaskBuffer_A); |
| 48 | + xTaskCreateStatic(led_OFF, "led_OFF", STACK_SIZE, NULL, configMAX_PRIORITIES - 1, xStack_B, &xTaskBuffer_B); |
| 49 | +} |
| 50 | + |
| 51 | +void led_ON(void *pvParameters) |
| 52 | +{ |
| 53 | + (void) pvParameters; |
| 54 | + while (1) |
| 55 | + { |
| 56 | + xSemaphoreTake( xSemaphore, ( TickType_t ) portMAX_DELAY ); |
| 57 | + SERIAL_PORT.println("LED ON!"); |
| 58 | + digitalWrite(LED_BUILTIN, HIGH); |
| 59 | + delay(BLINK_ON_TIME); |
| 60 | + xSemaphoreGive( xSemaphore ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +void led_OFF(void *pvParameters) |
| 65 | +{ |
| 66 | + (void) pvParameters; |
| 67 | + while (1) |
| 68 | + { |
| 69 | + xSemaphoreTake( xSemaphore, ( TickType_t ) portMAX_DELAY ); |
| 70 | + SERIAL_PORT.println("LED OFF!"); |
| 71 | + digitalWrite(LED_BUILTIN, LOW); |
| 72 | + delay(BLINK_OFF_TIME); |
| 73 | + xSemaphoreGive( xSemaphore ); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +void loop() { |
| 78 | + SERIAL_PORT.println("Hello!"); |
| 79 | + delay(1000); |
| 80 | +} |
0 commit comments