This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
/
u_port_private.c
1012 lines (868 loc) · 32.8 KB
/
u_port_private.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
/*
* Copyright 2019-2024 u-blox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @file
* @brief Stuff private to the STM32 porting layer.
*/
#ifdef U_CFG_OVERRIDE
# include "u_cfg_override.h" // For a customer's configuration override
#endif
#include "stddef.h" // NULL, size_t etc.
#include "stdint.h" // int32_t etc.
#include "stdbool.h"
#include "string.h" // strncpy()
#include "u_cfg_os_platform_specific.h"
#include "u_cfg_hw_platform_specific.h" // For U_CFG_HW_SWO_CLOCK_HZ
#include "u_error_common.h"
#include "u_assert.h"
#include "u_port.h"
#include "u_port_os.h"
#include "u_port_heap.h"
#include "u_port_event_queue.h"
#ifdef U_PORT_STM32_PURE_CMSIS
# include "cmsis_os2.h"
#endif
#if defined(U_PORT_STM32_PURE_CMSIS) && !defined(U_PORT_STM32_CMSIS_ON_FREERTOS)
# include "tx_api.h"
# include "tx_thread.h"
#else
# include "FreeRTOS.h"
# include "timers.h"
#endif
#ifdef STM32U575xx
# include "stm32u5xx_hal.h"
# include "stm32u5xx_ll_bus.h"
#else
# include "stm32f4xx_hal.h"
# include "stm32f4xx_ll_bus.h"
#endif
#include "u_port_private.h" // Down here 'cos it needs GPIO_TypeDef
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
#if defined(U_PORT_STM32_PURE_CMSIS) && !defined(U_PORT_STM32_CMSIS_ON_FREERTOS)
/** Convert an RTOS tick to milliseconds, ThreadX case.
*/
# define TICKS_TO_MS(ticks) (ticks * 1000 / TX_TIMER_TICKS_PER_SECOND)
#else
/** Convert an RTOS tick to milliseconds, FreeRTOS case.
*/
# define TICKS_TO_MS(ticks) (ticks * 1000 / configTICK_RATE_HZ)
#endif
/** Address of The ITM Enable register.
*/
#define ITM_ENA (*(volatile uint32_t *) 0xE0000E00)
/** Address of the ITM Trace Privilege register.
*/
#define ITM_TPR (*(volatile uint32_t *) 0xE0000E40)
/** Address of the ITM Trace Control register.
*/
#define ITM_TCR (*(volatile uint32_t *) 0xE0000E80)
/** Address of the ITM Lock Status register.
*/
#define ITM_LSR (*(volatile uint32_t *) 0xE0000FB0)
/** Address of the Debug register.
*/
#define DHCSR (*(volatile uint32_t *) 0xE000EDF0)
/** Address of another Debug register.
*/
#define DEMCR (*(volatile uint32_t *) 0xE000EDFC)
/** Address of the Trace Unit Async Clock prescaler register.
*/
#define TPIU_ACPR (*(volatile uint32_t *) 0xE0040010)
/** Address of the Trace Unit Selected Pin Protocol register.
*/
#define TPIU_SPPR (*(volatile uint32_t *) 0xE00400F0)
/** Address of the DWT Control register.
*/
#define DWT_CTRL (*(volatile uint32_t *) 0xE0001000)
/** Address of the Formattr And Flush Control register.
*/
#define FFCR (*(volatile uint32_t *) 0xE0040304)
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
/** Define a timer, intended to be used as part of a linked-list.
*/
typedef struct uPortPrivateTimer_t {
uPortTimerHandle_t handle;
char name[U_PORT_PRIVATE_TIMER_NAME_MAX_LEN_BYTES];
pTimerCallback_t *pCallback;
void *pCallbackParam;
#ifdef U_PORT_STM32_PURE_CMSIS
uint32_t intervalMs;
#endif
struct uPortPrivateTimer_t *pNext;
} uPortPrivateTimer_t;
/** Define a semaphore, intended to be used as part of a linked-list.
*/
typedef struct uPortPrivateSemaphore_t {
uPortSemaphoreHandle_t handle;
uint32_t limit;
uint32_t count;
struct uPortPrivateSemaphore_t *pNext;
} uPortPrivateSemaphore_t;
/* ----------------------------------------------------------------
* VARIABLES
* -------------------------------------------------------------- */
// Counter to keep track of RTOS ticks: NOT static
// so that the u_exception_handler.c can update it.
int32_t gTickTimerRtosCount;
// Get the GPIOx address for a given GPIO port.
static GPIO_TypeDef *const gpGpioReg[] = {GPIOA,
GPIOB,
GPIOC,
GPIOD,
GPIOE,
GPIOF,
GPIOG,
GPIOH,
GPIOI,
#ifdef GPIOJ
GPIOJ,
#endif
#ifdef GPIOK
GPIOK,
#endif
};
#ifdef STM32U575xx
// Get the LL driver peripheral number for a given GPIO port,
// all on AHB2 for STM32U5
static const int32_t gLlApbGrpPeriphGpioPort[] = {LL_AHB2_GRP1_PERIPH_GPIOA,
LL_AHB2_GRP1_PERIPH_GPIOB,
LL_AHB2_GRP1_PERIPH_GPIOC,
LL_AHB2_GRP1_PERIPH_GPIOD,
LL_AHB2_GRP1_PERIPH_GPIOE,
LL_AHB2_GRP1_PERIPH_GPIOF,
LL_AHB2_GRP1_PERIPH_GPIOG,
LL_AHB2_GRP1_PERIPH_GPIOH,
LL_AHB2_GRP1_PERIPH_GPIOI,
#ifdef GPIOJ
LL_AHB2_GRP1_PERIPH_GPIOJ,
#endif
#ifdef GPIOK
LL_AHB2_GRP1_PERIPH_GPIOK,
#endif
};
#else
// Get the LL driver peripheral number for a given GPIO port,
// all on AHB1 for STM32F4.
static const int32_t gLlApbGrpPeriphGpioPort[] = {LL_AHB1_GRP1_PERIPH_GPIOA,
LL_AHB1_GRP1_PERIPH_GPIOB,
LL_AHB1_GRP1_PERIPH_GPIOC,
LL_AHB1_GRP1_PERIPH_GPIOD,
LL_AHB1_GRP1_PERIPH_GPIOE,
LL_AHB1_GRP1_PERIPH_GPIOF,
LL_AHB1_GRP1_PERIPH_GPIOG,
LL_AHB1_GRP1_PERIPH_GPIOH,
LL_AHB1_GRP1_PERIPH_GPIOI,
#ifdef GPIOJ
LL_AHB1_GRP1_PERIPH_GPIOJ,
#endif
#ifdef GPIOK
LL_AHB1_GRP1_PERIPH_GPIOK,
#endif
};
#endif
/** Root of the linked list of timers.
*/
static uPortPrivateTimer_t *gpTimerList = NULL;
/** Mutex to protect the linked list of timers.
*/
static uPortMutexHandle_t gMutexForTimers = NULL;
/** Use an event queue to move the execution of the timer callback
* outside of the FreeRTOS/CMSIS timer task.
*/
static int32_t gEventQueueHandle = -1;
#if defined(U_PORT_STM32_PURE_CMSIS) && !defined(U_PORT_STM32_CMSIS_ON_FREERTOS)
/** A place to store the current pre-emption threshold of a CMSIS
* task, used as part of the implementation of critical sections
* when running CMSIS over ThreadX (since ThreadX does not support
* stopping the scheduler).
*/
static int32_t gSavedPremptionThreshold = -1;
/** A place to store the current priority of a CMSIS task, used
* as part of the implementation of critical sections when running
* CMSIS over ThreadX (since ThreadX does not support stopping the
* scheduler).
*/
static int32_t gSavedPriority = -1;
#endif
#if defined(U_PORT_STM32_PURE_CMSIS) && !defined(U_PORT_STM32_CMSIS_ON_FREERTOS)
/** Root of the linked list of semaphores.
*/
static uPortPrivateSemaphore_t *gpSemaphoreList = NULL;
/** Mutex to protect the list of semaphores.
*/
static uPortMutexHandle_t gMutexForSemaphores = NULL;
#endif
/* ----------------------------------------------------------------
* STATIC FUNCTIONS
* -------------------------------------------------------------- */
/** This code from https://wiki.segger.com/J-Link_SWO_Viewer.
* It enables SWO so that logging continues if the target resets
* without the external debug tool being aware.
* This can be switched off by overriding U_CFG_HW_SWO_CLOCK_HZ to
* -1, in which case the external debug tool will set it up instead.
*/
static void enableSwo()
{
#if U_CFG_HW_SWO_CLOCK_HZ >= 0
uint32_t stimulusRegs;
// Enable access to SWO registers
DEMCR |= (1 << 24);
ITM_LSR = 0xC5ACCE55;
// Initially disable ITM and stimulus port
// To make sure that nothing is transferred via SWO
// when changing the SWO prescaler etc.
stimulusRegs = ITM_ENA;
stimulusRegs &= ~(1 << 0); // Disable stimulus port 0
ITM_ENA = stimulusRegs;
ITM_TCR = 0; // Disable ITM
// Initialize SWO (prescaler, etc.)
#ifndef STM32U575xx
TPIU_SPPR = 0x00000002; // Select NRZ mode
TPIU_ACPR = (SystemCoreClock / U_CFG_HW_SWO_CLOCK_HZ) - 1;
#endif
ITM_TPR = 0x00000000;
DWT_CTRL = 0x400003FE;
#ifndef STM32U575xx
FFCR = 0x00000100;
#endif
// Enable ITM and stimulus port
ITM_TCR = 0x1000D; // Enable ITM
ITM_ENA = stimulusRegs | (1 << 0); // Enable stimulus port 0
#endif
}
// Find a timer entry in the list.
// gMutexForTimers should be locked before this is called.
static uPortPrivateTimer_t *pTimerFind(uPortTimerHandle_t handle)
{
uPortPrivateTimer_t *pTimer = gpTimerList;
while ((pTimer != NULL) && (pTimer->handle != handle)) {
pTimer = pTimer->pNext;
}
return pTimer;
}
// Remove an entry from the list.
// gMutexForTimers should be locked before this is called.
static void timerRemove(uPortTimerHandle_t handle)
{
uPortPrivateTimer_t *pTimer = gpTimerList;
uPortPrivateTimer_t *pPrevious = NULL;
// Find the entry in the list
while ((pTimer != NULL) && (pTimer->handle != handle)) {
pPrevious = pTimer;
pTimer = pTimer->pNext;
}
if (pTimer != NULL) {
// Remove the entry from the list
if (pPrevious != NULL) {
pPrevious->pNext = pTimer->pNext;
} else {
// Must be at head
gpTimerList = pTimer->pNext;
}
// Free the entry
uPortFree(pTimer);
}
}
// The timer event handler, where pParam is a pointer
// to the timer handle.
static void timerEventHandler(void *pParam, size_t paramLength)
{
uPortTimerHandle_t handle = *((uPortTimerHandle_t *) pParam);
uPortPrivateTimer_t *pTimer;
pTimerCallback_t *pCallback = NULL;
void *pCallbackParam;
(void) paramLength;
if (gMutexForTimers != NULL) {
U_PORT_MUTEX_LOCK(gMutexForTimers);
pTimer = pTimerFind(handle);
if (pTimer != NULL) {
pCallback = pTimer->pCallback;
pCallbackParam = pTimer->pCallbackParam;
}
U_PORT_MUTEX_UNLOCK(gMutexForTimers);
// Call the callback outside the locks so that the
// callback itself may call the timer API
if (pCallback != NULL) {
pCallback(handle, pCallbackParam);
}
}
}
// The timer expiry callback, called by FreeRTOS.
static void timerCallback(uPortTimerHandle_t handle)
{
#ifdef U_PORT_STM32_PURE_CMSIS
// In the pure CMSIS case we get a pointer to a pTimer
// structure rather than a handle so we need to convert it
uPortPrivateTimer_t *pTimer = (uPortPrivateTimer_t *) handle;
if (pTimer != NULL) {
handle = pTimer->handle;
}
#endif
if (gEventQueueHandle >= 0) {
// Send an event to our event task with the timer
// handle as the payload, IRQ version so as never
// to block
uPortEventQueueSendIrq(gEventQueueHandle,
// NOLINTNEXTLINE(bugprone-sizeof-expression)
&handle, sizeof(handle));
}
}
#ifdef U_PORT_STM32_PURE_CMSIS
# ifndef U_PORT_STM32_CMSIS_ON_FREERTOS
// Get a semaphore from the list; only required for the ThreadX
// under CMSIS case.
// Note: gMutexForSemaphores must be locked before this is called
static uPortPrivateSemaphore_t *pSemaphoreFind(uPortSemaphoreHandle_t handle)
{
uPortPrivateSemaphore_t *pTmp = gpSemaphoreList;
uPortPrivateSemaphore_t *pSemaphore = NULL;
while ((pTmp != NULL) && (pSemaphore == NULL)) {
if (pTmp->handle == handle) {
pSemaphore = pTmp;
}
pTmp = pTmp->pNext;
}
return pSemaphore;
}
# endif
// Increment the count of a semaphore, returning
// true if incrementing is allowed.
static bool semaphoreInc(uPortSemaphoreHandle_t handle)
{
// Incrementing is allowed if we do not
// have a stored record for this semaphore
// (the FreeRTOS case)
bool allowed = true;
# ifndef U_PORT_STM32_CMSIS_ON_FREERTOS
uPortPrivateSemaphore_t *pSemaphore;
U_PORT_MUTEX_LOCK(gMutexForSemaphores);
pSemaphore = pSemaphoreFind(handle);
if (pSemaphore != NULL) {
if (pSemaphore->count < pSemaphore->limit) {
pSemaphore->count++;
} else {
allowed = false;
}
}
U_PORT_MUTEX_UNLOCK(gMutexForSemaphores);
# else
(void) handle;
# endif
return allowed;
}
// Decrement the count of a semaphore.
static void semaphoreDec(uPortSemaphoreHandle_t handle)
{
# ifndef U_PORT_STM32_CMSIS_ON_FREERTOS
uPortPrivateSemaphore_t *pSemaphore;
U_PORT_MUTEX_LOCK(gMutexForSemaphores);
pSemaphore = pSemaphoreFind(handle);
if ((pSemaphore != NULL) && (pSemaphore->count > 0)) {
pSemaphore->count--;
}
U_PORT_MUTEX_UNLOCK(gMutexForSemaphores);
# else
(void) handle;
# endif
}
#endif // #ifdef U_PORT_STM32_PURE_CMSIS
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS SPECIFIC TO THIS PORT: INIT
* -------------------------------------------------------------- */
// Initalise the private stuff.
int32_t uPortPrivateInit()
{
int32_t errorCodeOrEventQueueHandle = (int32_t) U_ERROR_COMMON_SUCCESS;
if (gMutexForTimers == NULL) {
errorCodeOrEventQueueHandle = uPortMutexCreate(&gMutexForTimers);
}
#if defined(U_PORT_STM32_PURE_CMSIS) && !defined(U_PORT_STM32_CMSIS_ON_FREERTOS)
if ((errorCodeOrEventQueueHandle == 0) && (gMutexForSemaphores == NULL)) {
errorCodeOrEventQueueHandle = uPortMutexCreate(&gMutexForSemaphores);
}
#endif
if ((errorCodeOrEventQueueHandle == 0) && (gEventQueueHandle < 0)) {
// We need an event queue to offload the callback execution
// from the FreeRTOS/CMSIS timer task
errorCodeOrEventQueueHandle = uPortEventQueueOpen(timerEventHandler, "timerEvent",
#ifdef U_PORT_STM32_PURE_CMSIS
sizeof(osTimerId_t),
#else
sizeof(TimerHandle_t),
#endif
U_CFG_OS_TIMER_EVENT_TASK_STACK_SIZE_BYTES,
U_CFG_OS_TIMER_EVENT_TASK_PRIORITY,
U_CFG_OS_TIMER_EVENT_QUEUE_SIZE);
gTickTimerRtosCount = 0;
enableSwo();
}
if (errorCodeOrEventQueueHandle >= 0) {
gEventQueueHandle = errorCodeOrEventQueueHandle;
errorCodeOrEventQueueHandle = (int32_t) U_ERROR_COMMON_SUCCESS;
} else {
// Clean-up on error
#if defined(U_PORT_STM32_PURE_CMSIS) && !defined(U_PORT_STM32_CMSIS_ON_FREERTOS)
if (gMutexForSemaphores != NULL) {
uPortMutexDelete(gMutexForSemaphores);
}
#endif
if (gMutexForTimers != NULL) {
uPortMutexDelete(gMutexForTimers);
}
}
return errorCodeOrEventQueueHandle;
}
// Deinitialise the private stuff.
void uPortPrivateDeinit()
{
if (gMutexForTimers != NULL) {
U_PORT_MUTEX_LOCK(gMutexForTimers);
// Tidy away the timers
while (gpTimerList != NULL) {
#ifdef U_PORT_STM32_PURE_CMSIS
osTimerStop((osTimerId_t) gpTimerList->handle);
#else
xTimerStop((TimerHandle_t) gpTimerList->handle,
(portTickType) portMAX_DELAY);
#endif
timerRemove(gpTimerList->handle);
}
U_PORT_MUTEX_UNLOCK(gMutexForTimers);
// Close the event queue outside the mutex as it could be calling
// back into this API
if (gEventQueueHandle >= 0) {
uPortEventQueueClose(gEventQueueHandle);
gEventQueueHandle = -1;
}
uPortMutexDelete(gMutexForTimers);
gMutexForTimers = NULL;
}
#if defined(U_PORT_STM32_PURE_CMSIS) && !defined(U_PORT_STM32_CMSIS_ON_FREERTOS)
// Tidy away any semaphores
if (gMutexForSemaphores != NULL) {
uPortPrivateSemaphore_t *pTmp = NULL;
U_PORT_MUTEX_LOCK(gMutexForSemaphores);
while (gpSemaphoreList != NULL) {
osSemaphoreDelete((osSemaphoreId_t) gpSemaphoreList->handle);
pTmp = gpSemaphoreList;
uPortFree(gpSemaphoreList);
gpSemaphoreList = pTmp->pNext;
}
U_PORT_MUTEX_UNLOCK(gMutexForSemaphores);
uPortMutexDelete(gMutexForSemaphores);
gMutexForSemaphores = NULL;
}
#endif
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS SPECIFIC TO THIS PORT: TIMERS
* -------------------------------------------------------------- */
// Add a timer entry to the list.
int32_t uPortPrivateTimerCreate(uPortTimerHandle_t *pHandle,
const char *pName,
pTimerCallback_t *pCallback,
void *pCallbackParam,
uint32_t intervalMs,
bool periodic)
{
int32_t errorCode = (int32_t) U_ERROR_COMMON_NOT_INITIALISED;
uPortPrivateTimer_t *pTimer;
const char *_pName = NULL;
if (gMutexForTimers != NULL) {
U_PORT_MUTEX_LOCK(gMutexForTimers);
errorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
if (pHandle != NULL) {
// Create an entry in the list
pTimer = (uPortPrivateTimer_t *) pUPortMalloc(sizeof(uPortPrivateTimer_t));
errorCode = (int32_t) U_ERROR_COMMON_NO_MEMORY;
if (pTimer != NULL) {
// Populate the entry
pTimer->name[0] = 0;
if (pName != NULL) {
strncpy(pTimer->name, pName, sizeof(pTimer->name));
// Ensure terminator
pTimer->name[sizeof(pTimer->name) - 1] = 0;
_pName = pTimer->name;
}
pTimer->pCallback = pCallback;
pTimer->pCallbackParam = pCallbackParam;
#ifdef U_PORT_STM32_PURE_CMSIS
pTimer->intervalMs = intervalMs;
osTimerAttr_t attr = {0};
if (pName != NULL) {
attr.name = _pName;
}
pTimer->handle = (uPortTimerHandle_t) osTimerNew(timerCallback,
periodic ? osTimerPeriodic : osTimerOnce,
pTimer, &attr);
#else
pTimer->handle = (uPortTimerHandle_t) xTimerCreate(_pName,
MS_TO_TICKS(intervalMs),
periodic ? pdTRUE : pdFALSE,
NULL,
(void (*)(struct tmrTimerControl *)) timerCallback);
#endif
if (pTimer->handle != NULL) {
// Add the timer to the front of the list
pTimer->pNext = gpTimerList;
gpTimerList = pTimer;
*pHandle = pTimer->handle;
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
} else {
// Tidy up if the timer could not be created
uPortFree(pTimer);
}
}
}
U_PORT_MUTEX_UNLOCK(gMutexForTimers);
}
return errorCode;
}
// Start a CMSIS-based timer.
int32_t uPortPrivateTimerStartCmsis(const uPortTimerHandle_t handle)
{
#ifdef U_PORT_STM32_PURE_CMSIS
int32_t errorCode = (int32_t) U_ERROR_COMMON_NOT_INITIALISED;
uPortPrivateTimer_t *pTimer;
if (gMutexForTimers != NULL) {
U_PORT_MUTEX_LOCK(gMutexForTimers);
errorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
pTimer = pTimerFind((uPortTimerHandle_t) handle);
if (pTimer != NULL) {
errorCode = (int32_t) U_ERROR_COMMON_PLATFORM;
if (osTimerStart((osTimerId_t) handle, MS_TO_TICKS(pTimer->intervalMs)) == 0) {
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
}
}
U_PORT_MUTEX_UNLOCK(gMutexForTimers);
}
return errorCode;
#else
(void) handle;
return (int32_t) U_ERROR_COMMON_NOT_COMPILED;
#endif
}
// Change a CMSIS-based timer.
int32_t uPortPrivateTimerChangeCmsis(const uPortTimerHandle_t handle,
uint32_t intervalMs)
{
#ifdef U_PORT_STM32_PURE_CMSIS
int32_t errorCode = (int32_t) U_ERROR_COMMON_NOT_INITIALISED;
uPortPrivateTimer_t *pTimer;
if (gMutexForTimers != NULL) {
U_PORT_MUTEX_LOCK(gMutexForTimers);
errorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
pTimer = pTimerFind((uPortTimerHandle_t) handle);
if (pTimer != NULL) {
pTimer->intervalMs = intervalMs;
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
}
U_PORT_MUTEX_UNLOCK(gMutexForTimers);
}
return errorCode;
#else
(void) handle;
(void) intervalMs;
return (int32_t) U_ERROR_COMMON_NOT_COMPILED;
#endif
}
// Delete a timer entry from the list.
int32_t uPortPrivateTimerDelete(uPortTimerHandle_t handle)
{
int32_t errorCode = (int32_t) U_ERROR_COMMON_NOT_INITIALISED;
if (gMutexForTimers != NULL) {
// Delete the timer in the RTOS, outside the mutex as it can block
errorCode = (int32_t) U_ERROR_COMMON_PLATFORM;
#ifdef U_PORT_STM32_PURE_CMSIS
if (osTimerDelete((osTimerId_t) handle) == 0) {
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
}
#else
if (xTimerDelete((TimerHandle_t) handle,
(portTickType) portMAX_DELAY) == pdPASS) {
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
}
#endif
if (errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) {
U_PORT_MUTEX_LOCK(gMutexForTimers);
timerRemove(handle);
U_PORT_MUTEX_UNLOCK(gMutexForTimers);
}
}
return errorCode;
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS SPECIFIC TO THIS PORT: GET TIME TICK
* -------------------------------------------------------------- */
// Get the current tick converted to a time in milliseconds.
int64_t uPortPrivateGetTickTimeMs()
{
return TICKS_TO_MS(gTickTimerRtosCount);
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS SPECIFIC TO THIS PORT: SEMAPHORES FOR CMSIS
* -------------------------------------------------------------- */
// Create a semaphore, CMSIS case.
int32_t uPortPrivateSemaphoreCreateCmsis(uPortSemaphoreHandle_t *pSemaphoreHandle,
uint32_t initialCount,
uint32_t limit)
{
#ifdef U_PORT_STM32_PURE_CMSIS
uErrorCode_t errorCode = U_ERROR_COMMON_INVALID_PARAMETER;
// Note: the CMSIS API takes two parameters: max_count and initial_count,
// where initial_count is the number of tokens available at the outset
// and max_count is what it sounds like.
// The ThreadX API takes only initial_count.
// Our API has "limit" for max_count and initialCount as the number of
// tokens available at the outset.
if ((pSemaphoreHandle != NULL) && (limit != 0) && (initialCount <= limit)) {
# ifndef U_PORT_STM32_CMSIS_ON_FREERTOS
// ThreadX case
uPortPrivateSemaphore_t *pSemaphore;
errorCode = U_ERROR_COMMON_NOT_INITIALISED;
if (gMutexForSemaphores != NULL) {
U_PORT_MUTEX_LOCK(gMutexForSemaphores);
errorCode = U_ERROR_COMMON_NO_MEMORY;
pSemaphore = (uPortPrivateSemaphore_t *) pUPortMalloc(sizeof(*pSemaphore));
if (pSemaphore != NULL) {
errorCode = U_ERROR_COMMON_PLATFORM;
pSemaphore->limit = limit;
pSemaphore->count = initialCount;
*pSemaphoreHandle = (uPortSemaphoreHandle_t) osSemaphoreNew(limit,
initialCount,
NULL);
if (*pSemaphoreHandle != NULL) {
pSemaphore->handle = *pSemaphoreHandle;
// Add to the front of the list
pSemaphore->pNext = gpSemaphoreList;
gpSemaphoreList = pSemaphore;
errorCode = U_ERROR_COMMON_SUCCESS;
} else {
// Clean-up on error
uPortFree(pSemaphore);
}
}
U_PORT_MUTEX_UNLOCK(gMutexForSemaphores);
}
# else
// Normal CMSIS case
errorCode = U_ERROR_COMMON_PLATFORM;
*pSemaphoreHandle = (uPortSemaphoreHandle_t) osSemaphoreNew(limit,
initialCount,
NULL);
# endif
if (*pSemaphoreHandle != NULL) {
errorCode = U_ERROR_COMMON_SUCCESS;
}
}
return (int32_t) errorCode;
#else
(void) pSemaphoreHandle;
(void) initialCount;
(void) limit;
return (int32_t) U_ERROR_COMMON_NOT_COMPILED;
#endif
}
// Destroy a semaphore, CMSIS case.
int32_t uPortPrivateSemaphoreDeleteCmsis(const uPortSemaphoreHandle_t semaphoreHandle)
{
#ifdef U_PORT_STM32_PURE_CMSIS
uErrorCode_t errorCode = U_ERROR_COMMON_INVALID_PARAMETER;
if (semaphoreHandle != NULL) {
# ifndef U_PORT_STM32_CMSIS_ON_FREERTOS
// ThreadX case
uPortPrivateSemaphore_t *pSemaphore = gpSemaphoreList;
uPortPrivateSemaphore_t *pPrevious = NULL;
errorCode = U_ERROR_COMMON_NOT_INITIALISED;
if (gMutexForSemaphores != NULL) {
U_PORT_MUTEX_LOCK(gMutexForSemaphores);
errorCode = U_ERROR_COMMON_SUCCESS;
osSemaphoreDelete((osSemaphoreId_t) semaphoreHandle);
// Remove it from the list
while (pSemaphore != NULL) {
if (pSemaphore->handle == semaphoreHandle) {
if (pPrevious == NULL) {
// At head of list
gpSemaphoreList = pSemaphore->pNext;
} else {
pPrevious->pNext = pSemaphore->pNext;
}
uPortFree(pSemaphore);
}
pPrevious = pSemaphore;
pSemaphore = pSemaphore->pNext;
}
U_PORT_MUTEX_UNLOCK(gMutexForSemaphores);
}
# else
// Normal CMSIS case
errorCode = U_ERROR_COMMON_SUCCESS;
osSemaphoreDelete((osSemaphoreId_t) semaphoreHandle);
# endif
}
return (int32_t) errorCode;
#else
(void) semaphoreHandle;
return (int32_t) U_ERROR_COMMON_NOT_COMPILED;
#endif
}
// Take the given semaphore, CMSIS case.
int32_t uPortPrivateSemaphoreTakeCmsis(const uPortSemaphoreHandle_t semaphoreHandle)
{
#ifdef U_PORT_STM32_PURE_CMSIS
uErrorCode_t errorCode = U_ERROR_COMMON_INVALID_PARAMETER;
if (semaphoreHandle != NULL) {
errorCode = U_ERROR_COMMON_PLATFORM;
if (osSemaphoreAcquire((osSemaphoreId_t) semaphoreHandle,
osWaitForever) == osOK) {
semaphoreDec(semaphoreHandle);
errorCode = U_ERROR_COMMON_SUCCESS;
}
}
return (int32_t) errorCode;
#else
(void) semaphoreHandle;
return (int32_t) U_ERROR_COMMON_NOT_COMPILED;
#endif
}
// Try to take the given semaphore, CMSIS case.
int32_t uPortPrivateSemaphoreTryTakeCmsis(const uPortSemaphoreHandle_t semaphoreHandle,
int32_t delayMs)
{
#ifdef U_PORT_STM32_PURE_CMSIS
uErrorCode_t errorCode = U_ERROR_COMMON_INVALID_PARAMETER;
if (semaphoreHandle != NULL) {
errorCode = U_ERROR_COMMON_TIMEOUT;
if (osSemaphoreAcquire((osSemaphoreId_t) semaphoreHandle,
MS_TO_TICKS(delayMs)) == osOK) {
semaphoreDec(semaphoreHandle);
errorCode = U_ERROR_COMMON_SUCCESS;
}
}
return (int32_t) errorCode;
#else
(void) semaphoreHandle;
(void) delayMs;
return (int32_t) U_ERROR_COMMON_NOT_COMPILED;
#endif
}
// Give a semaphore, CMSIS case.
int32_t uPortPrivateSemaphoreGiveCmsis(const uPortSemaphoreHandle_t semaphoreHandle)
{
#ifdef U_PORT_STM32_PURE_CMSIS
uErrorCode_t errorCode = U_ERROR_COMMON_INVALID_PARAMETER;
osStatus_t status;
if (semaphoreHandle != NULL) {
// Increment first to avoid race conditions and
// return success if we have already given enough
errorCode = U_ERROR_COMMON_SUCCESS;
if (semaphoreInc(semaphoreHandle)) {
errorCode = U_ERROR_COMMON_PLATFORM;
status = osSemaphoreRelease((osSemaphoreId_t) semaphoreHandle);
// FreeRTOS _does_ obey the semaphore limit but it will
// return osErrorResource if we ask to release a semaphore
// more than limit times, whereas our API expects success
// in that case
if ((status == osOK) || (status == osErrorResource)) {
errorCode = U_ERROR_COMMON_SUCCESS;
} else {
semaphoreDec(semaphoreHandle);
}
}
}
return (int32_t) errorCode;
#else
(void) semaphoreHandle;
return (int32_t) U_ERROR_COMMON_NOT_COMPILED;
#endif
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS SPECIFIC TO THIS PORT: CRITICAL SECTION FOR CMSIS
* -------------------------------------------------------------- */
// Enter a critical section, CMSIS-wise.
int32_t uPortPrivateEnterCriticalCmsis()
{
#ifdef U_PORT_STM32_PURE_CMSIS
int32_t errorCode = (int32_t) U_ERROR_COMMON_PLATFORM;
# ifndef U_PORT_STM32_CMSIS_ON_FREERTOS
// ThreadX does not support osKernelLock(),
// Instead set the current thread to be top
// priority, which in ThreadX speak is 0, and
// change its preemption threshold to the same
TX_THREAD *pThread = tx_thread_identify();
if ((pThread != NULL) && (gSavedPremptionThreshold < 0) && (gSavedPriority < 0) &&
(tx_thread_preemption_change(pThread, 0, (UINT *) &gSavedPremptionThreshold) == 0) &&
(tx_thread_priority_change(pThread, 0, (UINT *) &gSavedPriority) == 0)) {
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
}
# else
if (osKernelLock() == 0) {
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
}
# endif
return errorCode;
#else
return (int32_t) U_ERROR_COMMON_NOT_COMPILED;
#endif
}
// Leave a critical section, CMSIS-wise.
void uPortPrivateExitCriticalCmsis()
{
#ifdef U_PORT_STM32_PURE_CMSIS
# ifndef U_PORT_STM32_CMSIS_ON_FREERTOS
TX_THREAD *pThread = tx_thread_identify();
UINT dummy;
if ((pThread != NULL) && (gSavedPremptionThreshold >= 0) && (gSavedPriority >= 0)) {
tx_thread_priority_change(pThread, (UINT) gSavedPriority, &dummy);
tx_thread_preemption_change(pThread, (UINT) gSavedPremptionThreshold, &dummy);
gSavedPriority = -1;
gSavedPremptionThreshold = -1;
}
# else
osKernelUnlock();
# endif
#endif
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS SPECIFIC TO THIS PORT: MISC
* -------------------------------------------------------------- */
// Return the base address for a given GPIO pin.
GPIO_TypeDef *pUPortPrivateGpioGetReg(int32_t pin)
{
int32_t port = U_PORT_STM32_GPIO_PORT(pin);
U_ASSERT(port >= 0);
U_ASSERT(port < sizeof(gpGpioReg) / sizeof(gpGpioReg[0]));
return gpGpioReg[port];
}
// Enable the clock to the register of the given GPIO pin.
void uPortPrivateGpioEnableClock(int32_t pin)
{
int32_t port = U_PORT_STM32_GPIO_PORT(pin);
U_ASSERT(port >= 0);
U_ASSERT(port < sizeof(gLlApbGrpPeriphGpioPort) /