1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
/**
* @file lv_freertos.c
*
*/
/**
* Copyright 2023 NXP
*
* SPDX-License-Identifier: MIT
*/
/*********************
* INCLUDES
*********************/
#include "lv_os.h"
#if LV_USE_OS == LV_OS_FREERTOS
#if (ESP_PLATFORM)
#include "freertos/atomic.h"
#else
#include "atomic.h"
#endif
#include "../misc/lv_log.h"
/*********************
* DEFINES
*********************/
#define ulMAX_COUNT 10U
#ifndef pcTASK_NAME
#define pcTASK_NAME "lvglDraw"
#endif
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static void prvRunThread(void * pxArg);
static void prvMutexInit(lv_mutex_t * pxMutex);
static void prvCheckMutexInit(lv_mutex_t * pxMutex);
#if !USE_FREERTOS_TASK_NOTIFY
static void prvCondInit(lv_thread_sync_t * pxCond);
static void prvCheckCondInit(lv_thread_sync_t * pxCond);
static void prvTestAndDecrement(lv_thread_sync_t * pxCond,
uint32_t ulLocalWaitingThreads);
#endif
/**********************
* STATIC VARIABLES
**********************/
#if (ESP_PLATFORM)
static portMUX_TYPE critSectionMux = portMUX_INITIALIZER_UNLOCKED;
#endif
/**********************
* MACROS
**********************/
#if (ESP_PLATFORM)
#define _enter_critical() taskENTER_CRITICAL(&critSectionMux);
#define _exit_critical() taskEXIT_CRITICAL(&critSectionMux);
#else
#define _enter_critical() taskENTER_CRITICAL();
#define _exit_critical() taskEXIT_CRITICAL();
#endif
/**********************
* GLOBAL FUNCTIONS
**********************/
lv_result_t lv_thread_init(lv_thread_t * pxThread, lv_thread_prio_t xSchedPriority,
void (*pvStartRoutine)(void *), size_t usStackSize,
void * xAttr)
{
pxThread->xTaskArg = xAttr;
pxThread->pvStartRoutine = pvStartRoutine;
BaseType_t xTaskCreateStatus = xTaskCreate(
prvRunThread,
pcTASK_NAME,
(configSTACK_DEPTH_TYPE)(usStackSize / sizeof(StackType_t)),
(void *)pxThread,
tskIDLE_PRIORITY + xSchedPriority,
&pxThread->xTaskHandle);
/* Ensure that the FreeRTOS task was successfully created. */
if(xTaskCreateStatus != pdPASS) {
LV_LOG_ERROR("xTaskCreate failed!");
return LV_RESULT_INVALID;
}
return LV_RESULT_OK;
}
lv_result_t lv_thread_delete(lv_thread_t * pxThread)
{
vTaskDelete(pxThread->xTaskHandle);
return LV_RESULT_OK;
}
lv_result_t lv_mutex_init(lv_mutex_t * pxMutex)
{
/* If mutex in uninitialized, perform initialization. */
prvCheckMutexInit(pxMutex);
return LV_RESULT_OK;
}
lv_result_t lv_mutex_lock(lv_mutex_t * pxMutex)
{
/* If mutex in uninitialized, perform initialization. */
prvCheckMutexInit(pxMutex);
BaseType_t xMutexTakeStatus = xSemaphoreTake(pxMutex->xMutex, portMAX_DELAY);
if(xMutexTakeStatus != pdTRUE) {
LV_LOG_ERROR("xSemaphoreTake failed!");
return LV_RESULT_INVALID;
}
return LV_RESULT_OK;
}
lv_result_t lv_mutex_lock_isr(lv_mutex_t * pxMutex)
{
/* If mutex in uninitialized, perform initialization. */
prvCheckMutexInit(pxMutex);
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
BaseType_t xMutexTakeStatus = xSemaphoreTakeFromISR(pxMutex->xMutex, &xHigherPriorityTaskWoken);
if(xMutexTakeStatus != pdTRUE) {
LV_LOG_ERROR("xSemaphoreTake failed!");
return LV_RESULT_INVALID;
}
/* If xHigherPriorityTaskWoken is now set to pdTRUE then a context switch
should be performed to ensure the interrupt returns directly to the highest
priority task. The macro used for this purpose is dependent on the port in
use and may be called portEND_SWITCHING_ISR(). */
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
return LV_RESULT_OK;
}
lv_result_t lv_mutex_unlock(lv_mutex_t * pxMutex)
{
/* If mutex in uninitialized, perform initialization. */
prvCheckMutexInit(pxMutex);
BaseType_t xMutexGiveStatus = xSemaphoreGive(pxMutex->xMutex);
if(xMutexGiveStatus != pdTRUE) {
LV_LOG_ERROR("xSemaphoreGive failed!");
return LV_RESULT_INVALID;
}
return LV_RESULT_OK;
}
lv_result_t lv_mutex_delete(lv_mutex_t * pxMutex)
{
vSemaphoreDelete(pxMutex->xMutex);
pxMutex->xIsInitialized = pdFALSE;
return LV_RESULT_OK;
}
lv_result_t lv_thread_sync_init(lv_thread_sync_t * pxCond)
{
#if USE_FREERTOS_TASK_NOTIFY
/* Store the handle of the calling task. */
pxCond->xTaskToNotify = xTaskGetCurrentTaskHandle();
#else
/* If the cond is uninitialized, perform initialization. */
prvCheckCondInit(pxCond);
#endif
return LV_RESULT_OK;
}
lv_result_t lv_thread_sync_wait(lv_thread_sync_t * pxCond)
{
lv_result_t lvRes = LV_RESULT_OK;
#if USE_FREERTOS_TASK_NOTIFY
LV_UNUSED(pxCond);
/* Wait for other task to notify this task. */
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
#else
uint32_t ulLocalWaitingThreads;
/* If the cond is uninitialized, perform initialization. */
prvCheckCondInit(pxCond);
/* Acquire the mutex. */
xSemaphoreTake(pxCond->xSyncMutex, portMAX_DELAY);
while(!pxCond->xSyncSignal) {
/* Increase the counter of threads blocking on condition variable, then
* release the mutex. */
/* Atomically increments thread waiting by 1, and
* stores number of threads waiting before increment. */
ulLocalWaitingThreads = Atomic_Increment_u32(&pxCond->ulWaitingThreads);
BaseType_t xMutexStatus = xSemaphoreGive(pxCond->xSyncMutex);
/* Wait on the condition variable. */
if(xMutexStatus == pdTRUE) {
BaseType_t xCondWaitStatus = xSemaphoreTake(
pxCond->xCondWaitSemaphore,
portMAX_DELAY);
/* Relock the mutex. */
xSemaphoreTake(pxCond->xSyncMutex, portMAX_DELAY);
if(xCondWaitStatus != pdTRUE) {
LV_LOG_ERROR("xSemaphoreTake(xCondWaitSemaphore) failed!");
lvRes = LV_RESULT_INVALID;
/* Atomically decrements thread waiting by 1.
* If iLocalWaitingThreads is updated by other thread(s) in between,
* this implementation guarantees to decrement by 1 based on the
* value currently in pxCond->ulWaitingThreads. */
prvTestAndDecrement(pxCond, ulLocalWaitingThreads + 1);
}
}
else {
LV_LOG_ERROR("xSemaphoreGive(xSyncMutex) failed!");
lvRes = LV_RESULT_INVALID;
/* Atomically decrements thread waiting by 1.
* If iLocalWaitingThreads is updated by other thread(s) in between,
* this implementation guarantees to decrement by 1 based on the
* value currently in pxCond->ulWaitingThreads. */
prvTestAndDecrement(pxCond, ulLocalWaitingThreads + 1);
}
}
pxCond->xSyncSignal = pdFALSE;
/* Release the mutex. */
xSemaphoreGive(pxCond->xSyncMutex);
#endif
return lvRes;
}
lv_result_t lv_thread_sync_signal(lv_thread_sync_t * pxCond)
{
#if USE_FREERTOS_TASK_NOTIFY
/* Send a notification to the task waiting. */
xTaskNotifyGive(pxCond->xTaskToNotify);
#else
/* If the cond is uninitialized, perform initialization. */
prvCheckCondInit(pxCond);
/* Acquire the mutex. */
xSemaphoreTake(pxCond->xSyncMutex, portMAX_DELAY);
pxCond->xSyncSignal = pdTRUE;
/* Local copy of number of threads waiting. */
uint32_t ulLocalWaitingThreads = pxCond->ulWaitingThreads;
/* Test local copy of threads waiting is larger than zero. */
while(ulLocalWaitingThreads > 0) {
/* Atomically check whether the copy in memory has changed.
* If not, set the copy of threads waiting in memory to zero. */
if(ATOMIC_COMPARE_AND_SWAP_SUCCESS == Atomic_CompareAndSwap_u32(
&pxCond->ulWaitingThreads,
0,
ulLocalWaitingThreads)) {
/* Unblock all. */
for(uint32_t i = 0; i < ulLocalWaitingThreads; i++) {
xSemaphoreGive(pxCond->xCondWaitSemaphore);
}
break;
}
/* Local copy is out dated. Reload from memory and retry. */
ulLocalWaitingThreads = pxCond->ulWaitingThreads;
}
/* Release the mutex. */
xSemaphoreGive(pxCond->xSyncMutex);
#endif
return LV_RESULT_OK;
}
lv_result_t lv_thread_sync_delete(lv_thread_sync_t * pxCond)
{
#if USE_FREERTOS_TASK_NOTIFY
LV_UNUSED(pxCond);
#else
/* Cleanup all resources used by the cond. */
vSemaphoreDelete(pxCond->xCondWaitSemaphore);
vSemaphoreDelete(pxCond->xSyncMutex);
pxCond->ulWaitingThreads = 0;
pxCond->xSyncSignal = pdFALSE;
pxCond->xIsInitialized = pdFALSE;
#endif
return LV_RESULT_OK;
}
/**********************
* STATIC FUNCTIONS
**********************/
static void prvRunThread(void * pxArg)
{
lv_thread_t * pxThread = (lv_thread_t *)pxArg;
/* Run the thread routine. */
pxThread->pvStartRoutine((void *)pxThread->xTaskArg);
vTaskDelete(NULL);
}
static void prvMutexInit(lv_mutex_t * pxMutex)
{
pxMutex->xMutex = xSemaphoreCreateMutex();
/* Ensure that the FreeRTOS mutex was successfully created. */
if(pxMutex->xMutex == NULL) {
LV_LOG_ERROR("xSemaphoreCreateMutex failed!");
return;
}
/* Mutex successfully created. */
pxMutex->xIsInitialized = pdTRUE;
}
static void prvCheckMutexInit(lv_mutex_t * pxMutex)
{
/* Check if the mutex needs to be initialized. */
if(pxMutex->xIsInitialized == pdFALSE) {
/* Mutex initialization must be in a critical section to prevent two threads
* from initializing it at the same time. */
_enter_critical();
/* Check again that the mutex is still uninitialized, i.e. it wasn't
* initialized while this function was waiting to enter the critical
* section. */
if(pxMutex->xIsInitialized == pdFALSE) {
prvMutexInit(pxMutex);
}
/* Exit the critical section. */
_exit_critical();
}
}
#if !USE_FREERTOS_TASK_NOTIFY
static void prvCondInit(lv_thread_sync_t * pxCond)
{
pxCond->xCondWaitSemaphore = xSemaphoreCreateCounting(ulMAX_COUNT, 0U);
/* Ensure that the FreeRTOS semaphore was successfully created. */
if(pxCond->xCondWaitSemaphore == NULL) {
LV_LOG_ERROR("xSemaphoreCreateCounting failed!");
return;
}
pxCond->xSyncMutex = xSemaphoreCreateMutex();
/* Ensure that the FreeRTOS mutex was successfully created. */
if(pxCond->xSyncMutex == NULL) {
LV_LOG_ERROR("xSemaphoreCreateMutex failed!");
/* Cleanup. */
vSemaphoreDelete(pxCond->xCondWaitSemaphore);
return;
}
/* Condition variable successfully created. */
pxCond->ulWaitingThreads = 0;
pxCond->xSyncSignal = pdFALSE;
pxCond->xIsInitialized = pdTRUE;
}
static void prvCheckCondInit(lv_thread_sync_t * pxCond)
{
BaseType_t xSemCreateStatus = pdTRUE;
/* Check if the condition variable needs to be initialized. */
if(pxCond->xIsInitialized == pdFALSE) {
/* Cond initialization must be in a critical section to prevent two
* threads from initializing it at the same time. */
_enter_critical();
/* Check again that the condition is still uninitialized, i.e. it wasn't
* initialized while this function was waiting to enter the critical
* section. */
if(pxCond->xIsInitialized == pdFALSE) {
prvCondInit(pxCond);
}
/* Exit the critical section. */
_exit_critical();
}
}
static void prvTestAndDecrement(lv_thread_sync_t * pxCond,
uint32_t ulLocalWaitingThreads)
{
/* Test local copy of threads waiting is larger than zero. */
while(ulLocalWaitingThreads > 0) {
/* Atomically check whether the copy in memory has changed.
* If not, decrease the copy of threads waiting in memory. */
if(ATOMIC_COMPARE_AND_SWAP_SUCCESS == Atomic_CompareAndSwap_u32(
&pxCond->ulWaitingThreads,
ulLocalWaitingThreads - 1,
ulLocalWaitingThreads)) {
/* Signal one succeeded. Break. */
break;
}
/* Local copy may be out dated. Reload from memory and retry. */
ulLocalWaitingThreads = pxCond->ulWaitingThreads;
}
}
#endif
#endif /*LV_USE_OS == LV_OS_FREERTOS*/
|