Skip to content

Commit d662897

Browse files
godarioDario Gogliandolo
andauthored
Enabled static memory allocation with example (#842)
Co-authored-by: Dario Gogliandolo <[email protected]>
1 parent b0d0e29 commit d662897

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

libraries/FreeRTOS/src/FreeRTOSConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define configUSE_COUNTING_SEMAPHORES 1
2525
#define configUSE_QUEUE_SETS 1
2626
#define configSUPPORT_DYNAMIC_ALLOCATION 1
27-
#define configSUPPORT_STATIC_ALLOCATION 0
27+
#define configSUPPORT_STATIC_ALLOCATION 1
2828
#define configSTACK_DEPTH_TYPE uint32_t
2929
#define configUSE_TASK_PREEMPTION_DISABLE 1
3030

0 commit comments

Comments
 (0)