-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
npl_os_freertos.c
1201 lines (1020 loc) · 32.5 KB
/
npl_os_freertos.c
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-FileCopyrightText: 2015-2022 The Apache Software Foundation (ASF)
*
* SPDX-License-Identifier: Apache-2.0
*
* SPDX-FileContributor: 2019-2022 Espressif Systems (Shanghai) CO LTD
*/
#include <assert.h>
#include <stddef.h>
#include <string.h>
#include "nimble/nimble_npl.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/portable.h"
#include "nimble/npl_freertos.h"
#include "nimble/nimble_port.h"
#include "os/os_mempool.h"
#include "esp_log.h"
#include "soc/soc_caps.h"
#include "esp_bt.h"
portMUX_TYPE ble_port_mutex = portMUX_INITIALIZER_UNLOCKED;
#if BLE_NPL_USE_ESP_TIMER
static const char *TAG = "Timer";
#endif
#define OS_MEM_ALLOC (1)
#if (!defined(SOC_ESP_NIMBLE_CONTROLLER) || !SOC_ESP_NIMBLE_CONTROLLER)
#error "not defined SOC_ESP_NIMBLE_CONTROLLER or SOC_ESP_NIMBLE_CONTROLLER is zero"
#endif
#define BLE_HOST_CO_COUNT (8)
#define BLE_HOST_EV_COUNT (11 + BLE_HOST_CO_COUNT)
#define BLE_HOST_EVQ_COUNT (3)
#define BLE_HOST_SEM_COUNT (1)
#define BLE_HOST_MUTEX_COUNT (4)
struct os_mempool ble_freertos_ev_pool;
static os_membuf_t *ble_freertos_ev_buf = NULL;
struct os_mempool ble_freertos_evq_pool;
static os_membuf_t *ble_freertos_evq_buf = NULL;
struct os_mempool ble_freertos_co_pool;
static os_membuf_t *ble_freertos_co_buf = NULL;
struct os_mempool ble_freertos_sem_pool;
static os_membuf_t *ble_freertos_sem_buf = NULL;
struct os_mempool ble_freertos_mutex_pool;
static os_membuf_t *ble_freertos_mutex_buf = NULL;
static uint16_t ble_freertos_total_event_cnt = 0;
static ble_npl_count_info_t g_ctrl_npl_info = {
.co_count = 0,
.evt_count = 0,
.evtq_count = 0,
.mutex_count = 0,
.sem_count = 0,
};
bool
IRAM_ATTR npl_freertos_os_started(void)
{
return xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
}
void *
IRAM_ATTR npl_freertos_get_current_task_id(void)
{
return xTaskGetCurrentTaskHandle();
}
void
IRAM_ATTR npl_freertos_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn,
void *arg)
{
struct ble_npl_event_freertos *event = NULL;
#if OS_MEM_ALLOC
if (!os_memblock_from(&ble_freertos_ev_pool,ev->event)) {
ev->event = os_memblock_get(&ble_freertos_ev_pool);
}
#else
if(!ev->event) {
ev->event = malloc(sizeof(struct ble_npl_event_freertos));
}
#endif
event = (struct ble_npl_event_freertos *)ev->event;
BLE_LL_ASSERT(event);
memset(event, 0, sizeof(*event));
event->fn = fn;
event->arg = arg;
}
void
IRAM_ATTR npl_freertos_event_deinit(struct ble_npl_event *ev)
{
BLE_LL_ASSERT(ev->event);
#if OS_MEM_ALLOC
os_memblock_put(&ble_freertos_ev_pool,ev->event);
#else
free(ev->event);
#endif
ev->event = NULL;
}
void
IRAM_ATTR npl_freertos_event_reset(struct ble_npl_event *ev)
{
struct ble_npl_event_freertos *event = (struct ble_npl_event_freertos *)ev->event;
BLE_LL_ASSERT(event);
event->queued = 0;
}
void
npl_freertos_eventq_init(struct ble_npl_eventq *evq)
{
struct ble_npl_eventq_freertos *eventq = NULL;
#if OS_MEM_ALLOC
if (!os_memblock_from(&ble_freertos_evq_pool,evq->eventq)) {
evq->eventq = os_memblock_get(&ble_freertos_evq_pool);
eventq = (struct ble_npl_eventq_freertos*)evq->eventq;
BLE_LL_ASSERT(eventq);
memset(eventq, 0, sizeof(*eventq));
eventq->q = xQueueCreate(ble_freertos_total_event_cnt, sizeof(struct ble_npl_eventq *));
BLE_LL_ASSERT(eventq->q);
}
#else
if(!evq->eventq) {
evq->eventq = malloc(sizeof(struct ble_npl_eventq_freertos));
eventq = (struct ble_npl_eventq_freertos*)evq->eventq;
BLE_LL_ASSERT(eventq);
memset(eventq, 0, sizeof(*eventq));
eventq->q = xQueueCreate(ble_freertos_total_event_cnt, sizeof(struct ble_npl_eventq *));
BLE_LL_ASSERT(eventq->q);
}
#endif
}
void
npl_freertos_eventq_deinit(struct ble_npl_eventq *evq)
{
struct ble_npl_eventq_freertos *eventq = (struct ble_npl_eventq_freertos *)evq->eventq;
BLE_LL_ASSERT(eventq);
vQueueDelete(eventq->q);
#if OS_MEM_ALLOC
os_memblock_put(&ble_freertos_evq_pool,eventq);
#else
free((void *)eventq);
#endif
evq->eventq = NULL;
}
void
IRAM_ATTR npl_freertos_callout_mem_reset(struct ble_npl_callout *co)
{
struct ble_npl_callout_freertos *callout = (struct ble_npl_callout_freertos *)co->co;
BLE_LL_ASSERT(callout);
BLE_LL_ASSERT(callout->handle);
ble_npl_event_reset(&callout->ev);
}
static inline bool
IRAM_ATTR in_isr(void)
{
/* XXX hw specific! */
return xPortInIsrContext() != 0;
}
struct ble_npl_event *
IRAM_ATTR npl_freertos_eventq_get(struct ble_npl_eventq *evq, ble_npl_time_t tmo)
{
struct ble_npl_event *ev = NULL;
struct ble_npl_eventq_freertos *eventq = (struct ble_npl_eventq_freertos *)evq->eventq;
BaseType_t woken;
BaseType_t ret;
if (in_isr()) {
BLE_LL_ASSERT(tmo == 0);
ret = xQueueReceiveFromISR(eventq->q, &ev, &woken);
if( woken == pdTRUE ) {
portYIELD_FROM_ISR();
}
} else {
ret = xQueueReceive(eventq->q, &ev, tmo);
}
BLE_LL_ASSERT(ret == pdPASS || ret == errQUEUE_EMPTY);
if (ev) {
struct ble_npl_event_freertos *event = (struct ble_npl_event_freertos *)ev->event;
if (event) {
event->queued = false;
}
}
return ev;
}
void
IRAM_ATTR npl_freertos_eventq_put(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
{
BaseType_t woken;
BaseType_t ret;
struct ble_npl_eventq_freertos *eventq = (struct ble_npl_eventq_freertos *)evq->eventq;
struct ble_npl_event_freertos *event = (struct ble_npl_event_freertos *)ev->event;
if (event->queued) {
return;
}
event->queued = true;
if (in_isr()) {
ret = xQueueSendToBackFromISR(eventq->q, &ev, &woken);
if( woken == pdTRUE ) {
portYIELD_FROM_ISR();
}
} else {
ret = xQueueSendToBack(eventq->q, &ev, portMAX_DELAY);
}
BLE_LL_ASSERT(ret == pdPASS);
}
void
IRAM_ATTR npl_freertos_eventq_remove(struct ble_npl_eventq *evq,
struct ble_npl_event *ev)
{
struct ble_npl_event *tmp_ev;
BaseType_t ret;
int i;
int count;
BaseType_t woken, woken2;
struct ble_npl_eventq_freertos *eventq = (struct ble_npl_eventq_freertos *)evq->eventq;
struct ble_npl_event_freertos *event = (struct ble_npl_event_freertos *)ev->event;
if (!event->queued) {
return;
}
/*
* XXX We cannot extract element from inside FreeRTOS queue so as a quick
* workaround we'll just remove all elements and add them back except the
* one we need to remove. This is silly, but works for now - we probably
* better use counting semaphore with os_queue to handle this in future.
*/
if (in_isr()) {
woken = pdFALSE;
count = uxQueueMessagesWaitingFromISR(eventq->q);
for (i = 0; i < count; i++) {
ret = xQueueReceiveFromISR(eventq->q, &tmp_ev, &woken2);
BLE_LL_ASSERT(ret == pdPASS);
woken |= woken2;
if (tmp_ev == ev) {
continue;
}
ret = xQueueSendToBackFromISR(eventq->q, &tmp_ev, &woken2);
BLE_LL_ASSERT(ret == pdPASS);
woken |= woken2;
}
if( woken == pdTRUE ) {
portYIELD_FROM_ISR();
}
} else {
portMUX_TYPE ble_npl_mut = portMUX_INITIALIZER_UNLOCKED;
portENTER_CRITICAL(&ble_npl_mut);
count = uxQueueMessagesWaiting(eventq->q);
for (i = 0; i < count; i++) {
ret = xQueueReceive(eventq->q, &tmp_ev, 0);
BLE_LL_ASSERT(ret == pdPASS);
if (tmp_ev == ev) {
continue;
}
ret = xQueueSendToBack(eventq->q, &tmp_ev, 0);
BLE_LL_ASSERT(ret == pdPASS);
}
portEXIT_CRITICAL(&ble_npl_mut);
}
event->queued = 0;
}
ble_npl_error_t
npl_freertos_mutex_init(struct ble_npl_mutex *mu)
{
struct ble_npl_mutex_freertos *mutex = NULL;
#if OS_MEM_ALLOC
if (!os_memblock_from(&ble_freertos_mutex_pool,mu->mutex)) {
mu->mutex = os_memblock_get(&ble_freertos_mutex_pool);
mutex = (struct ble_npl_mutex_freertos *)mu->mutex;
if (!mutex) {
return BLE_NPL_INVALID_PARAM;
}
memset(mutex, 0, sizeof(*mutex));
mutex->handle = xSemaphoreCreateRecursiveMutex();
BLE_LL_ASSERT(mutex->handle);
}
#else
if(!mu->mutex) {
mu->mutex = malloc(sizeof(struct ble_npl_mutex_freertos));
mutex = (struct ble_npl_mutex_freertos *)mu->mutex;
if (!mutex) {
return BLE_NPL_INVALID_PARAM;
}
memset(mutex, 0, sizeof(*mutex));
mutex->handle = xSemaphoreCreateRecursiveMutex();
BLE_LL_ASSERT(mutex->handle);
}
#endif
return BLE_NPL_OK;
}
ble_npl_error_t
npl_freertos_mutex_deinit(struct ble_npl_mutex *mu)
{
struct ble_npl_mutex_freertos *mutex = (struct ble_npl_mutex_freertos *)mu->mutex;
if (!mutex) {
return BLE_NPL_INVALID_PARAM;
}
BLE_LL_ASSERT(mutex->handle);
vSemaphoreDelete(mutex->handle);
#if OS_MEM_ALLOC
os_memblock_put(&ble_freertos_mutex_pool,mutex);
#else
free((void *)mutex);
#endif
mu->mutex = NULL;
return BLE_NPL_OK;
}
void
IRAM_ATTR npl_freertos_event_run(struct ble_npl_event *ev)
{
struct ble_npl_event_freertos *event = (struct ble_npl_event_freertos *)ev->event;
event->fn(ev);
}
bool
IRAM_ATTR npl_freertos_eventq_is_empty(struct ble_npl_eventq *evq)
{
struct ble_npl_eventq_freertos *eventq = (struct ble_npl_eventq_freertos *)evq->eventq;
return xQueueIsQueueEmptyFromISR(eventq->q);
}
bool
IRAM_ATTR npl_freertos_event_is_queued(struct ble_npl_event *ev)
{
struct ble_npl_event_freertos *event = (struct ble_npl_event_freertos *)ev->event;
return event->queued;
}
void *
IRAM_ATTR npl_freertos_event_get_arg(struct ble_npl_event *ev)
{
struct ble_npl_event_freertos *event = (struct ble_npl_event_freertos *)ev->event;
return event->arg;
}
void
IRAM_ATTR npl_freertos_event_set_arg(struct ble_npl_event *ev, void *arg)
{
struct ble_npl_event_freertos *event = (struct ble_npl_event_freertos *)ev->event;
event->arg = arg;
}
ble_npl_error_t
IRAM_ATTR npl_freertos_mutex_pend(struct ble_npl_mutex *mu, ble_npl_time_t timeout)
{
BaseType_t ret;
struct ble_npl_mutex_freertos *mutex = (struct ble_npl_mutex_freertos *)mu->mutex;
if (!mutex) {
return BLE_NPL_INVALID_PARAM;
}
BLE_LL_ASSERT(mutex->handle);
if (in_isr()) {
ret = pdFAIL;
BLE_LL_ASSERT(0);
} else {
ret = xSemaphoreTakeRecursive(mutex->handle, timeout);
}
return ret == pdPASS ? BLE_NPL_OK : BLE_NPL_TIMEOUT;
}
ble_npl_error_t
IRAM_ATTR npl_freertos_mutex_release(struct ble_npl_mutex *mu)
{
struct ble_npl_mutex_freertos *mutex = (struct ble_npl_mutex_freertos *)mu->mutex;
if (!mutex) {
return BLE_NPL_INVALID_PARAM;
}
BLE_LL_ASSERT(mutex->handle);
if (in_isr()) {
BLE_LL_ASSERT(0);
} else {
if (xSemaphoreGiveRecursive(mutex->handle) != pdPASS) {
return BLE_NPL_BAD_MUTEX;
}
}
return BLE_NPL_OK;
}
ble_npl_error_t
npl_freertos_sem_init(struct ble_npl_sem *sem, uint16_t tokens)
{
struct ble_npl_sem_freertos *semaphor = NULL;
#if OS_MEM_ALLOC
if (!os_memblock_from(&ble_freertos_sem_pool,sem->sem)) {
sem->sem = os_memblock_get(&ble_freertos_sem_pool);
semaphor = (struct ble_npl_sem_freertos *)sem->sem;
if (!semaphor) {
return BLE_NPL_INVALID_PARAM;
}
memset(semaphor, 0, sizeof(*semaphor));
semaphor->handle = xSemaphoreCreateCounting(128, tokens);
BLE_LL_ASSERT(semaphor->handle);
}
#else
if(!sem->sem) {
sem->sem = malloc(sizeof(struct ble_npl_sem_freertos));
semaphor = (struct ble_npl_sem_freertos *)sem->sem;
if (!semaphor) {
return BLE_NPL_INVALID_PARAM;
}
memset(semaphor, 0, sizeof(*semaphor));
semaphor->handle = xSemaphoreCreateCounting(128, tokens);
BLE_LL_ASSERT(semaphor->handle);
}
#endif
return BLE_NPL_OK;
}
ble_npl_error_t
npl_freertos_sem_deinit(struct ble_npl_sem *sem)
{
struct ble_npl_sem_freertos *semaphor = (struct ble_npl_sem_freertos *)sem->sem;
if (!semaphor) {
return BLE_NPL_INVALID_PARAM;
}
BLE_LL_ASSERT(semaphor->handle);
vSemaphoreDelete(semaphor->handle);
#if OS_MEM_ALLOC
os_memblock_put(&ble_freertos_sem_pool,semaphor);
#else
free((void *)semaphor);
#endif
sem->sem = NULL;
return BLE_NPL_OK;
}
ble_npl_error_t
IRAM_ATTR npl_freertos_sem_pend(struct ble_npl_sem *sem, ble_npl_time_t timeout)
{
BaseType_t woken;
BaseType_t ret;
struct ble_npl_sem_freertos *semaphor = (struct ble_npl_sem_freertos *)sem->sem;
if (!semaphor) {
return BLE_NPL_INVALID_PARAM;
}
BLE_LL_ASSERT(semaphor->handle);
if (in_isr()) {
BLE_LL_ASSERT(timeout == 0);
ret = xSemaphoreTakeFromISR(semaphor->handle, &woken);
if( woken == pdTRUE ) {
portYIELD_FROM_ISR();
}
} else {
ret = xSemaphoreTake(semaphor->handle, timeout);
}
return ret == pdPASS ? BLE_NPL_OK : BLE_NPL_TIMEOUT;
}
ble_npl_error_t
IRAM_ATTR npl_freertos_sem_release(struct ble_npl_sem *sem)
{
BaseType_t ret;
BaseType_t woken;
struct ble_npl_sem_freertos *semaphor = (struct ble_npl_sem_freertos *)sem->sem;
if (!semaphor) {
return BLE_NPL_INVALID_PARAM;
}
BLE_LL_ASSERT(semaphor->handle);
if (in_isr()) {
ret = xSemaphoreGiveFromISR(semaphor->handle, &woken);
if( woken == pdTRUE ) {
portYIELD_FROM_ISR();
}
} else {
ret = xSemaphoreGive(semaphor->handle);
}
BLE_LL_ASSERT(ret == pdPASS);
return BLE_NPL_OK;
}
#if BLE_NPL_USE_ESP_TIMER
static void
IRAM_ATTR ble_npl_event_fn_wrapper(void *arg)
{
struct ble_npl_callout_freertos *callout = (struct ble_npl_callout_freertos *)arg;
BLE_LL_ASSERT(callout);
if (callout->evq) {
ble_npl_eventq_put(callout->evq, &callout->ev);
} else {
struct ble_npl_event_freertos *event = (struct ble_npl_event_freertos *)callout->ev.event;
event->fn(&callout->ev);
}
}
static
IRAM_ATTR ble_npl_error_t esp_err_to_npl_error(esp_err_t err)
{
switch(err) {
case ESP_ERR_INVALID_ARG:
return BLE_NPL_INVALID_PARAM;
case ESP_ERR_INVALID_STATE:
return BLE_NPL_EINVAL;
case ESP_OK:
return BLE_NPL_OK;
default:
return BLE_NPL_ERROR;
}
}
#else
static void
IRAM_ATTR os_callout_timer_cb(TimerHandle_t timer)
{
struct ble_npl_callout_freertos *callout;
callout = pvTimerGetTimerID(timer);
BLE_LL_ASSERT(callout);
if (callout->evq) {
ble_npl_eventq_put(callout->evq, &callout->ev);
} else {
struct ble_npl_event_freertos *event = (struct ble_npl_event_freertos *)callout->ev.event;
event->fn(&callout->ev);
}
}
#endif
int
npl_freertos_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq,
ble_npl_event_fn *ev_cb, void *ev_arg)
{
struct ble_npl_callout_freertos *callout = NULL;
#if OS_MEM_ALLOC
if (!os_memblock_from(&ble_freertos_co_pool, co->co)) {
co->co = os_memblock_get(&ble_freertos_co_pool);
callout = (struct ble_npl_callout_freertos *)co->co;
BLE_LL_ASSERT(callout);
memset(callout, 0, sizeof(*callout));
ble_npl_event_init(&callout->ev, ev_cb, ev_arg);
#if BLE_NPL_USE_ESP_TIMER
callout->evq = evq;
esp_timer_create_args_t create_args = {
.callback = ble_npl_event_fn_wrapper,
.arg = callout,
.name = "nimble_timer"
};
if (esp_timer_create(&create_args, &callout->handle) != ESP_OK) {
ble_npl_event_deinit(&callout->ev);
os_memblock_put(&ble_freertos_co_pool,callout);
co->co = NULL;
return -1;
}
#else
callout->handle = xTimerCreate("co", 1, pdFALSE, callout, os_callout_timer_cb);
if (!callout->handle) {
ble_npl_event_deinit(&callout->ev);
os_memblock_put(&ble_freertos_co_pool,callout);
co->co = NULL;
return -1;
}
#endif // BLE_NPL_USE_ESP_TIMER
} else {
callout = (struct ble_npl_callout_freertos *)co->co;
BLE_LL_ASSERT(callout);
callout->evq = evq;
ble_npl_event_init(&callout->ev, ev_cb, ev_arg);
}
#else
if(!co->co) {
co->co = malloc(sizeof(struct ble_npl_callout_freertos));
callout = (struct ble_npl_callout_freertos *)co->co;
if (!callout) {
return -1;
}
memset(callout, 0, sizeof(*callout));
ble_npl_event_init(&callout->ev, ev_cb, ev_arg);
#if BLE_NPL_USE_ESP_TIMER
callout->evq = evq;
esp_timer_create_args_t create_args = {
.callback = ble_npl_event_fn_wrapper,
.arg = callout,
.name = "nimble_timer"
};
if (esp_timer_create(&create_args, &callout->handle) != ESP_OK) {
ble_npl_event_deinit(&callout->ev);
free((void *)callout);
co->co = NULL;
return -1;
}
#else
callout->handle = xTimerCreate("co", 1, pdFALSE, callout, os_callout_timer_cb);
if (!callout->handle) {
ble_npl_event_deinit(&callout->ev);
free((void *)callout);
co->co = NULL;
return -1;
}
#endif // BLE_NPL_USE_ESP_TIMER
}
else {
callout = (struct ble_npl_callout_freertos *)co->co;
BLE_LL_ASSERT(callout);
callout->evq = evq;
ble_npl_event_init(&callout->ev, ev_cb, ev_arg);
}
#endif // OS_MEM_ALLOC
return 0;
}
void
npl_freertos_callout_deinit(struct ble_npl_callout *co)
{
struct ble_npl_callout_freertos *callout = (struct ble_npl_callout_freertos *)co->co;
/* Since we dynamically deinit timers, function can be called for NULL timers. Return for such scenarios */
if (!callout) {
return;
}
if (!callout->handle) {
return;
}
ble_npl_event_deinit(&callout->ev);
#if BLE_NPL_USE_ESP_TIMER
esp_err_t err = esp_timer_stop(callout->handle);
if(err != ESP_OK) {
if (err != ESP_ERR_INVALID_STATE) { // ESP_ERR_INVALID_STATE is expected when timer is already stopped
ESP_LOGD(TAG, "Timer not stopped");
}
}
err = esp_timer_delete(callout->handle);
if(err != ESP_OK) {
ESP_LOGW(TAG, "Timer not deleted");
}
#else
xTimerDelete(callout->handle, portMAX_DELAY);
#if OS_MEM_ALLOC
os_memblock_put(&ble_freertos_co_pool,callout);
#else
free((void *)callout);
#endif // OS_MEM_ALLOC
#endif // BLE_NPL_USE_ESP_TIMER
co->co = NULL;
memset(co, 0, sizeof(struct ble_npl_callout));
}
uint16_t
IRAM_ATTR npl_freertos_sem_get_count(struct ble_npl_sem *sem)
{
struct ble_npl_sem_freertos *semaphor = (struct ble_npl_sem_freertos *)sem->sem;
return uxSemaphoreGetCount(semaphor->handle);
}
ble_npl_error_t
IRAM_ATTR npl_freertos_callout_reset(struct ble_npl_callout *co, ble_npl_time_t ticks)
{
struct ble_npl_callout_freertos *callout = (struct ble_npl_callout_freertos *)co->co;
#if BLE_NPL_USE_ESP_TIMER
esp_timer_stop(callout->handle);
return esp_err_to_npl_error(esp_timer_start_once(callout->handle, ticks*1000));
#else
BaseType_t woken1, woken2, woken3;
if (ticks == 0) {
ticks = 1;
}
if (in_isr()) {
xTimerStopFromISR(callout->handle, &woken1);
xTimerChangePeriodFromISR(callout->handle, ticks, &woken2);
xTimerResetFromISR(callout->handle, &woken3);
if( woken1 == pdTRUE || woken2 == pdTRUE || woken3 == pdTRUE) {
portYIELD_FROM_ISR();
}
} else {
xTimerStop(callout->handle, portMAX_DELAY);
xTimerChangePeriod(callout->handle, ticks, portMAX_DELAY);
xTimerReset(callout->handle, portMAX_DELAY);
}
return BLE_NPL_OK;
#endif
}
void
IRAM_ATTR npl_freertos_callout_stop(struct ble_npl_callout *co)
{
struct ble_npl_callout_freertos *callout = (struct ble_npl_callout_freertos *)co->co;
if (!callout) {
return;
}
#if BLE_NPL_USE_ESP_TIMER
esp_timer_stop(callout->handle);
#else
xTimerStop(callout->handle, portMAX_DELAY);
#endif
}
bool
IRAM_ATTR npl_freertos_callout_is_active(struct ble_npl_callout *co)
{
struct ble_npl_callout_freertos *callout = (struct ble_npl_callout_freertos *)co->co;
#if BLE_NPL_USE_ESP_TIMER
return esp_timer_is_active(callout->handle);
#else
return xTimerIsTimerActive(callout->handle) == pdTRUE;
#endif
}
ble_npl_time_t
IRAM_ATTR npl_freertos_callout_get_ticks(struct ble_npl_callout *co)
{
#if BLE_NPL_USE_ESP_TIMER
/* Currently, esp_timer does not support an API which gets the expiry time for
* current timer.
* Returning 0 from here should not cause any effect.
* Drawback of this approach is that existing code to reset timer would be called
* more often (since the if condition to invoke reset timer would always succeed if
* timer is active).
*/
return 0;
#else
struct ble_npl_callout_freertos *callout = (struct ble_npl_callout_freertos *)co->co;
return xTimerGetExpiryTime(callout->handle);
#endif
}
ble_npl_time_t
IRAM_ATTR npl_freertos_callout_remaining_ticks(struct ble_npl_callout *co,
ble_npl_time_t now)
{
ble_npl_time_t rt;
uint32_t exp = 0;
struct ble_npl_callout_freertos *callout = (struct ble_npl_callout_freertos *)co->co;
#if BLE_NPL_USE_ESP_TIMER
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
uint64_t expiry = 0;
esp_err_t err;
//Fetch expiry time in microseconds
err = esp_timer_get_expiry_time((esp_timer_handle_t)(callout->handle), &expiry);
if (err != ESP_OK) {
//Error. Could not fetch the expiry time
return 0;
}
//Convert microseconds to ticks
npl_freertos_time_ms_to_ticks((uint32_t)(expiry / 1000), &exp);
#else
//esp_timer_get_expiry_time() is only available from IDF 5.0 onwards
//Set expiry to 0
exp = 0;
#endif //ESP_IDF_VERSION
#else
exp = xTimerGetExpiryTime(callout->handle);
#endif
if (exp > now) {
rt = exp - now;
} else {
rt = 0;
}
return rt;
}
void
IRAM_ATTR npl_freertos_callout_set_arg(struct ble_npl_callout *co, void *arg)
{
struct ble_npl_callout_freertos *callout = (struct ble_npl_callout_freertos *)co->co;
struct ble_npl_event_freertos *event = (struct ble_npl_event_freertos *)callout->ev.event;
event->arg = arg;
}
uint32_t
IRAM_ATTR npl_freertos_time_get(void)
{
#if BLE_NPL_USE_ESP_TIMER
return esp_timer_get_time() / 1000;
#else
return xTaskGetTickCountFromISR();
#endif
}
ble_npl_error_t
IRAM_ATTR npl_freertos_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks)
{
uint64_t ticks;
#if BLE_NPL_USE_ESP_TIMER
ticks = (uint64_t)ms;
#else
ticks = ((uint64_t)ms * configTICK_RATE_HZ) / 1000;
#endif
if (ticks > UINT32_MAX) {
return BLE_NPL_EINVAL;
}
*out_ticks = ticks;
return 0;
}
ble_npl_error_t
IRAM_ATTR npl_freertos_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms)
{
uint64_t ms;
#if BLE_NPL_USE_ESP_TIMER
ms = ((uint64_t)ticks);
#else
ms = ((uint64_t)ticks * 1000) / configTICK_RATE_HZ;
#endif
if (ms > UINT32_MAX) {
return BLE_NPL_EINVAL;
}
*out_ms = ms;
return 0;
}
ble_npl_time_t
IRAM_ATTR npl_freertos_time_ms_to_ticks32(uint32_t ms)
{
#if BLE_NPL_USE_ESP_TIMER
return ms;
#else
return ms * configTICK_RATE_HZ / 1000;
#endif
}
uint32_t
IRAM_ATTR npl_freertos_time_ticks_to_ms32(ble_npl_time_t ticks)
{
#if BLE_NPL_USE_ESP_TIMER
return ticks;
#else
return ticks * 1000 / configTICK_RATE_HZ;
#endif
}
void
IRAM_ATTR npl_freertos_time_delay(ble_npl_time_t ticks)
{
#if BLE_NPL_USE_ESP_TIMER
vTaskDelay(ticks / portTICK_PERIOD_MS);
#else
vTaskDelay(ticks);
#endif
}
uint8_t hw_critical_state_status = 0;
uint32_t
IRAM_ATTR npl_freertos_hw_enter_critical(void)
{
++hw_critical_state_status;
portENTER_CRITICAL(&ble_port_mutex);
return 0;
}
uint8_t
IRAM_ATTR npl_freertos_hw_is_in_critical(void)
{
return hw_critical_state_status;
}
void
IRAM_ATTR npl_freertos_hw_exit_critical(uint32_t ctx)
{
--hw_critical_state_status;
portEXIT_CRITICAL(&ble_port_mutex);
}
uint32_t
IRAM_ATTR npl_freertos_get_time_forever(void)
{
return portMAX_DELAY;
}
const struct npl_funcs_t npl_funcs_ro = {
.p_ble_npl_os_started = npl_freertos_os_started,
.p_ble_npl_get_current_task_id = npl_freertos_get_current_task_id,
.p_ble_npl_eventq_init = npl_freertos_eventq_init,
.p_ble_npl_eventq_deinit = npl_freertos_eventq_deinit,
.p_ble_npl_eventq_get = npl_freertos_eventq_get,
.p_ble_npl_eventq_put = npl_freertos_eventq_put,
.p_ble_npl_eventq_remove = npl_freertos_eventq_remove,
.p_ble_npl_event_run = npl_freertos_event_run,
.p_ble_npl_eventq_is_empty = npl_freertos_eventq_is_empty,
.p_ble_npl_event_init = npl_freertos_event_init,
.p_ble_npl_event_deinit = npl_freertos_event_deinit,
.p_ble_npl_event_reset = npl_freertos_event_reset,
.p_ble_npl_event_is_queued = npl_freertos_event_is_queued,
.p_ble_npl_event_get_arg = npl_freertos_event_get_arg,
.p_ble_npl_event_set_arg = npl_freertos_event_set_arg,
.p_ble_npl_mutex_init = npl_freertos_mutex_init,
.p_ble_npl_mutex_deinit = npl_freertos_mutex_deinit,
.p_ble_npl_mutex_pend = npl_freertos_mutex_pend,
.p_ble_npl_mutex_release = npl_freertos_mutex_release,
.p_ble_npl_sem_init = npl_freertos_sem_init,
.p_ble_npl_sem_deinit = npl_freertos_sem_deinit,
.p_ble_npl_sem_pend = npl_freertos_sem_pend,
.p_ble_npl_sem_release = npl_freertos_sem_release,
.p_ble_npl_sem_get_count = npl_freertos_sem_get_count,