-
Notifications
You must be signed in to change notification settings - Fork 0
/
stm32_lock.h
376 lines (329 loc) · 11.8 KB
/
stm32_lock.h
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
/**
******************************************************************************
* @file stm32_lock.h
* @author STMicroelectronics
* @brief STMicroelectronics lock mechanisms
*
* @details
* This implementation supports the following strategies for handling
* thread-safe locks. The strategy can be explicitly selected by
* defining <tt>\STM32_THREAD_SAFE_STRATEGY = \<number></tt> in the project.
* Please look at the '<toolchain/library>_lock_glue.c' file for more details.
*
* 1. User defined thread-safe implementation.
* User defined solution for handling thread-safety.
* <br>
* <b>NOTE:</b> The stubs in stm32_lock_user.h needs to be implemented to gain
* thread-safety.
*
* 2. [<b>DEFAULT</b>] Allow lock usage from interrupts.
* This implementation will ensure thread-safety by disabling all interrupts
* during e.g. calls to malloc.
* <br>
* <b>NOTE:</b> Disabling all interrupts creates interrupt latency which
* might not be desired for this application!
*
* 3. Deny lock usage from interrupts.
* This implementation assumes single thread of execution.
* <br>
* <b>NOTE:</b> Thread-safety dependent functions will enter an infinity loop
* if used in interrupt context.
*
* 4. Allow lock usage from interrupts. Implemented using FreeRTOS locks.
* This implementation will ensure thread-safety by entering RTOS ISR capable
* critical sections during e.g. calls to malloc.
* By default this implementation supports 2 levels of recursive locking.
* Adding additional levels requires 4 bytes per lock per level of RAM.
* <br>
* <b>NOTE:</b> Interrupts with high priority are not disabled. This implies
* that the lock is not thread-safe from high priority interrupts!
*
* 5. Deny lock usage from interrupts. Implemented using FreeRTOS locks.
* This implementation will ensure thread-safety by suspending all tasks
* during e.g. calls to malloc.
* <br>
* <b>NOTE:</b> Thread-safety dependent functions will enter an infinity loop
* if used in interrupt context.
*
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
#ifndef __STM32_LOCK_H__
#define __STM32_LOCK_H__
/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stddef.h>
#include <cmsis_compiler.h>
#ifndef STM32_THREAD_SAFE_STRATEGY
#define STM32_THREAD_SAFE_STRATEGY 2 /**< Assume strategy 2 if not specified */
#endif /* STM32_THREAD_SAFE_STRATEGY */
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* Function prototypes -------------------------------------------------------*/
void Error_Handler(void);
/* Public macros -------------------------------------------------------------*/
/** Blocks execution */
#define STM32_LOCK_BLOCK() \
do \
{ \
__disable_irq(); \
Error_Handler(); \
while (1); \
} while (0)
/** Blocks execution if argument is NULL */
#define STM32_LOCK_BLOCK_IF_NULL_ARGUMENT(x) \
do \
{ \
if ((x) == NULL) \
{ \
STM32_LOCK_BLOCK(); \
} \
} while (0)
/** Blocks execution if in interrupt context */
#define STM32_LOCK_BLOCK_IF_INTERRUPT_CONTEXT() \
do \
{ \
if (__get_IPSR()) \
{ \
STM32_LOCK_BLOCK(); \
} \
} while (0)
/** Hide unused parameter warning from compiler */
#define STM32_LOCK_UNUSED(var) (void)var
/** Size of array */
#define STM32_LOCK_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
#if STM32_THREAD_SAFE_STRATEGY == 1
/*
* User defined thread-safe implementation.
*/
/* Includes ----------------------------------------------------------------*/
/** STM32 lock API version */
#define STM32_LOCK_API 1
#include "stm32_lock_user.h"
#undef STM32_LOCK_API
#elif STM32_THREAD_SAFE_STRATEGY == 2
/*
* Allow lock usage from interrupts.
*/
/* Private defines ---------------------------------------------------------*/
/** Initialize members in instance of <code>LockingData_t</code> structure */
#define LOCKING_DATA_INIT { 0, 0 }
/* Private typedef ---------------------------------------------------------*/
typedef struct
{
uint8_t flag; /**< Backup of PRIMASK.PM at nesting level 0 */
uint8_t counter; /**< Nesting level */
} LockingData_t;
/* Private functions -------------------------------------------------------*/
/**
* @brief Initialize STM32 lock
* @param lock The lock to init
*/
static inline void stm32_lock_init(LockingData_t *lock)
{
STM32_LOCK_BLOCK_IF_NULL_ARGUMENT(lock);
lock->flag = 0;
lock->counter = 0;
}
/**
* @brief Acquire STM32 lock
* @param lock The lock to acquire
*/
static inline void stm32_lock_acquire(LockingData_t *lock)
{
uint8_t flag = (uint8_t)(__get_PRIMASK() & 0x1); /* PRIMASK.PM */
__disable_irq();
__DSB();
__ISB();
STM32_LOCK_BLOCK_IF_NULL_ARGUMENT(lock);
if (lock->counter == 0)
{
lock->flag = flag;
}
else if (lock->counter == UINT8_MAX)
{
STM32_LOCK_BLOCK();
}
lock->counter++;
}
/**
* @brief Release STM32 lock
* @param lock The lock to release
*/
static inline void stm32_lock_release(LockingData_t *lock)
{
STM32_LOCK_BLOCK_IF_NULL_ARGUMENT(lock);
if (lock->counter == 0)
{
STM32_LOCK_BLOCK();
}
lock->counter--;
if (lock->counter == 0 && lock->flag == 0)
{
__enable_irq();
}
}
#elif STM32_THREAD_SAFE_STRATEGY == 3
/*
* Deny lock usage from interrupts.
*/
/* Private defines ---------------------------------------------------------*/
/** Initialize members in instance of <code>LockingData_t</code> structure */
#define LOCKING_DATA_INIT 0
/* Private typedef ---------------------------------------------------------*/
typedef uint8_t LockingData_t; /**< Unused */
/* Private functions -------------------------------------------------------*/
/**
* @brief Initialize STM32 lock
* @param lock The lock to init
*/
static inline void stm32_lock_init(LockingData_t *lock)
{
STM32_LOCK_BLOCK_IF_NULL_ARGUMENT(lock);
}
/**
* @brief Acquire STM32 lock
* @param lock The lock to acquire
*/
static inline void stm32_lock_acquire(LockingData_t *lock)
{
STM32_LOCK_BLOCK_IF_NULL_ARGUMENT(lock);
STM32_LOCK_BLOCK_IF_INTERRUPT_CONTEXT();
}
/**
* @brief Release ST lock
* @param lock The lock to release
*/
static inline void stm32_lock_release(LockingData_t *lock)
{
STM32_LOCK_BLOCK_IF_NULL_ARGUMENT(lock);
STM32_LOCK_BLOCK_IF_INTERRUPT_CONTEXT();
}
#elif STM32_THREAD_SAFE_STRATEGY == 4
/*
* Allow lock usage from interrupts. Implemented using FreeRTOS locks.
*/
/* Includes ----------------------------------------------------------------*/
#include <FreeRTOS.h>
#include <task.h>
#if defined (__GNUC__) && !defined (__CC_ARM) && configUSE_NEWLIB_REENTRANT == 0
#warning Please set configUSE_NEWLIB_REENTRANT to 1 in FreeRTOSConfig.h, otherwise newlib will not be thread-safe
#endif /* defined (__GNUC__) && !defined (__CC_ARM) && configUSE_NEWLIB_REENTRANT == 0 */
/* Private defines ---------------------------------------------------------*/
/** Initialize members in instance of <code>LockingData_t</code> structure */
#define LOCKING_DATA_INIT { {0, 0}, 0 }
#define STM32_LOCK_MAX_NESTED_LEVELS 2 /**< Max nesting level of interrupts */
typedef struct
{
uint32_t basepri[STM32_LOCK_MAX_NESTED_LEVELS];
uint8_t nesting_level;
} LockingData_t;
/* Private macros ----------------------------------------------------------*/
/** Blocks execution if reached max nesting level */
#define STM32_LOCK_ASSERT_VALID_NESTING_LEVEL(lock) \
do \
{ \
if (lock->nesting_level >= STM32_LOCK_ARRAY_SIZE(lock->basepri)) \
{ \
STM32_LOCK_BLOCK(); \
} \
} while (0)
/* Private functions -------------------------------------------------------*/
/**
* @brief Initialize STM32 lock
* @param lock The lock to init
*/
static inline void stm32_lock_init(LockingData_t *lock)
{
STM32_LOCK_BLOCK_IF_NULL_ARGUMENT(lock);
for (size_t i = 0; i < STM32_LOCK_ARRAY_SIZE(lock->basepri); i++)
{
lock->basepri[i] = 0;
}
lock->nesting_level = 0;
}
/**
* @brief Acquire STM32 lock
* @param lock The lock to acquire
*/
static inline void stm32_lock_acquire(LockingData_t *lock)
{
STM32_LOCK_BLOCK_IF_NULL_ARGUMENT(lock);
STM32_LOCK_ASSERT_VALID_NESTING_LEVEL(lock);
lock->basepri[lock->nesting_level++] = taskENTER_CRITICAL_FROM_ISR();
}
/**
* @brief Release STM32 lock
* @param lock The lock to release
*/
static inline void stm32_lock_release(LockingData_t *lock)
{
STM32_LOCK_BLOCK_IF_NULL_ARGUMENT(lock);
lock->nesting_level--;
STM32_LOCK_ASSERT_VALID_NESTING_LEVEL(lock);
taskEXIT_CRITICAL_FROM_ISR(lock->basepri[lock->nesting_level]);
}
#undef STM32_LOCK_ASSERT_VALID_NESTING_LEVEL
#undef STM32_LOCK_MAX_NESTED_LEVELS
#elif STM32_THREAD_SAFE_STRATEGY == 5
/*
* Deny lock usage from interrupts. Implemented using FreeRTOS locks.
*/
/* Includes ----------------------------------------------------------------*/
#include <FreeRTOS.h>
#include <task.h>
#if defined (__GNUC__) && !defined (__CC_ARM) && configUSE_NEWLIB_REENTRANT == 0
#warning Please set configUSE_NEWLIB_REENTRANT to 1 in FreeRTOSConfig.h, otherwise newlib will not be thread-safe
#endif /* defined (__GNUC__) && !defined (__CC_ARM) && configUSE_NEWLIB_REENTRANT == 0 */
/* Private defines ---------------------------------------------------------*/
/** Initialize members in instance of <code>LockingData_t</code> structure */
#define LOCKING_DATA_INIT 0
/* Private typedef ---------------------------------------------------------*/
typedef uint8_t LockingData_t; /**< Unused */
/* Private functions -------------------------------------------------------*/
/**
* @brief Initialize STM32 lock
* @param lock The lock to init
*/
static inline void stm32_lock_init(LockingData_t *lock)
{
STM32_LOCK_BLOCK_IF_NULL_ARGUMENT(lock);
}
/**
* @brief Acquire STM32 lock
* @param lock The lock to acquire
*/
static inline void stm32_lock_acquire(LockingData_t *lock)
{
STM32_LOCK_BLOCK_IF_NULL_ARGUMENT(lock);
STM32_LOCK_BLOCK_IF_INTERRUPT_CONTEXT();
vTaskSuspendAll();
}
/**
* @brief Release STM32 lock
* @param lock The lock to release
*/
static inline void stm32_lock_release(LockingData_t *lock)
{
STM32_LOCK_BLOCK_IF_NULL_ARGUMENT(lock);
STM32_LOCK_BLOCK_IF_INTERRUPT_CONTEXT();
xTaskResumeAll();
}
#else
#error Invalid STM32_THREAD_SAFE_STRATEGY specified
#endif /* STM32_THREAD_SAFE_STRATEGY */
#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */
#endif /* __STM32_LOCK_H__ */