caffeine-hal 0.6.6
A Header-Defined Interface c library, it provides the hal layer for the Caffeine framework
Loading...
Searching...
No Matches
cfn_hal.h
Go to the documentation of this file.
1
26#ifndef CAFFEINE_HAL_HAL_H
27#define CAFFEINE_HAL_HAL_H
28
29#ifdef __cplusplus
30extern "C"
31{
32#endif
33
34/* Includes ---------------------------------------------------------*/
35#include "cfn_hal_types.h"
36#include <stddef.h>
37
51#ifndef CFN_HAL_USE_LOCK
52#define CFN_HAL_USE_LOCK 0
53#endif
54
55#ifndef CFN_HAL_INLINE
61#define CFN_HAL_INLINE static inline
62#endif
63
64/* Defines ----------------------------------------------------------*/
65#define CFN_HAL_MAX_DELAY (UINT32_MAX)
66
67/* Macro ------------------------------------------------------------*/
73#ifdef __cplusplus
74#define CFN_HAL_STATIC_ASSERT static_assert
75#else
76#define CFN_HAL_STATIC_ASSERT _Static_assert
77#endif
78
79#define CFN_HAL_BIT(x) ((1UL) << (uint32_t) (x))
80#define CFN_HAL_BIT_SET(x, y) ((x) |= (CFN_HAL_BIT(y)))
81#define CFN_HAL_BIT_CLEAR(x, y) ((x) &= ~(CFN_HAL_BIT(y)))
82#define CFN_HAL_BIT_TOGGLE(x, y) ((x) ^= (CFN_HAL_BIT(y)))
83#define CFN_HAL_BIT_CHECK(x, y) ((x) & (CFN_HAL_BIT(y)))
84#define CFN_HAL_BIT_IS_SET(x, y) ((CFN_HAL_BIT_CHECK(x, y)) != 0)
85#define CFN_HAL_STR_STRINGIFY(x) #x
86#define CFN_HAL_INTERNAL_CONCAT(x, y) x##y
87#define CFN_HAL_CONCAT(x, y) CFN_HAL_INTERNAL_CONCAT(x, y)
88#define CFN_HAL_UNUSED(x) (void) (x)
89
90#define CFN_HAL_CONTAINER_OF(ptr, type, member) ((type *) ((char *) (ptr) - offsetof(type, member)))
91
100#define CFN_HAL_GET_DRIVER_FROM_BASE(ptr, type) CFN_HAL_CONTAINER_OF(ptr, type, base)
101
102#define CFN_HAL_CREATE_DRIVER_TYPE(prefix, config_type, api_type, phy_type, cb_type) \
103 struct cfn_hal_##prefix##_s \
104 { \
105 cfn_hal_driver_t base; \
106 const config_type *config; \
107 const api_type *api; \
108 const phy_type *phy; \
109 cb_type cb; \
110 void *cb_user_arg; \
111 }; \
112 typedef struct cfn_hal_##prefix##_s cfn_hal_##prefix##_t
113
114#define CFN_HAL_CHECK_AND_CALL_FUNC(expected_peripheral_type, func, driver, result) \
115 do \
116 { \
117 if ((driver) && (driver)->base.type == (expected_peripheral_type) && (driver)->api) \
118 { \
119 if ((driver)->api->func) \
120 { \
121 (result) = (driver)->api->func((driver)); \
122 } \
123 else \
124 { \
125 (result) = CFN_HAL_ERROR_NOT_SUPPORTED; \
126 } \
127 } \
128 else \
129 { \
130 (result) = CFN_HAL_ERROR_BAD_PARAM; \
131 } \
132 } while (0)
133
134#define CFN_HAL_CHECK_AND_CALL_FUNC_VARG(expected_peripheral_type, func, driver, result, ...) \
135 do \
136 { \
137 if ((driver) && (driver)->base.type == (expected_peripheral_type) && (driver)->api) \
138 { \
139 if ((driver)->api->func) \
140 { \
141 (result) = (driver)->api->func((driver), __VA_ARGS__); \
142 } \
143 else \
144 { \
145 (result) = CFN_HAL_ERROR_NOT_SUPPORTED; \
146 } \
147 } \
148 else \
149 { \
150 (result) = CFN_HAL_ERROR_BAD_PARAM; \
151 } \
152 } while (0)
153
154#define CFN_HAL_GET_LOCK_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, NAME, ...) NAME
155
156#if (CFN_HAL_USE_LOCK == 1)
157#define CFN_HAL_LOCK(driver, timeout) cfn_hal_base_lock((cfn_hal_driver_t *) (driver), timeout)
158#define CFN_HAL_UNLOCK(driver) cfn_hal_base_unlock((cfn_hal_driver_t *) (driver))
159
160#define CFN_HAL_WITH_LOCK_0(driver, timeout, result, function) \
161 do \
162 { \
163 if ((driver) != NULL) \
164 { \
165 (result) = cfn_hal_base_lock((cfn_hal_driver_t *) (driver), (timeout)); \
166 if ((result) == CFN_HAL_ERROR_OK) \
167 { \
168 (result) = function((driver)); \
169 (void) cfn_hal_base_unlock((cfn_hal_driver_t *) (driver)); \
170 } \
171 } \
172 else \
173 { \
174 (result) = CFN_HAL_ERROR_BAD_PARAM; \
175 } \
176 } while (0)
177
178#define CFN_HAL_WITH_LOCK_N(driver, timeout, result, function, ...) \
179 do \
180 { \
181 if ((driver) != NULL) \
182 { \
183 (result) = cfn_hal_base_lock((cfn_hal_driver_t *) (driver), (timeout)); \
184 if ((result) == CFN_HAL_ERROR_OK) \
185 { \
186 (result) = function((driver), __VA_ARGS__); \
187 (void) cfn_hal_base_unlock((cfn_hal_driver_t *) (driver)); \
188 } \
189 } \
190 else \
191 { \
192 (result) = CFN_HAL_ERROR_BAD_PARAM; \
193 } \
194 } while (0)
195
205#define CFN_HAL_WITH_LOCK(...) \
206 CFN_HAL_GET_LOCK_MACRO(__VA_ARGS__, \
207 CFN_HAL_WITH_LOCK_N, \
208 CFN_HAL_WITH_LOCK_N, \
209 CFN_HAL_WITH_LOCK_N, \
210 CFN_HAL_WITH_LOCK_N, \
211 CFN_HAL_WITH_LOCK_N, \
212 CFN_HAL_WITH_LOCK_0) \
213 (__VA_ARGS__)
214#else
215#define CFN_HAL_LOCK(driver, timeout) (CFN_HAL_ERROR_OK)
216#define CFN_HAL_UNLOCK(driver) (CFN_HAL_ERROR_OK)
217
218#define CFN_HAL_WITH_LOCK_NO_LOCK_0(driver, timeout, result, function) \
219 do \
220 { \
221 (void) (timeout); \
222 if ((driver) != NULL) \
223 { \
224 (result) = function((driver)); \
225 } \
226 else \
227 { \
228 (result) = CFN_HAL_ERROR_BAD_PARAM; \
229 } \
230 } while (0)
231
232#define CFN_HAL_WITH_LOCK_NO_LOCK_N(driver, timeout, result, function, ...) \
233 do \
234 { \
235 (void) (timeout); \
236 if ((driver) != NULL) \
237 { \
238 (result) = function((driver), __VA_ARGS__); \
239 } \
240 else \
241 { \
242 (result) = CFN_HAL_ERROR_BAD_PARAM; \
243 } \
244 } while (0)
245
249#define CFN_HAL_WITH_LOCK(...) \
250 CFN_HAL_GET_LOCK_MACRO(__VA_ARGS__, \
251 CFN_HAL_WITH_LOCK_NO_LOCK_N, \
252 CFN_HAL_WITH_LOCK_NO_LOCK_N, \
253 CFN_HAL_WITH_LOCK_NO_LOCK_N, \
254 CFN_HAL_WITH_LOCK_NO_LOCK_N, \
255 CFN_HAL_WITH_LOCK_NO_LOCK_N, \
256 CFN_HAL_WITH_LOCK_NO_LOCK_0) \
257 (__VA_ARGS__)
258#endif
259
260/* Types Enums ------------------------------------------------------*/
261/* Types Structs ----------------------------------------------------*/
262/* Functions prototypes ---------------------------------------------*/
263
264#ifdef __cplusplus
265}
266#endif
267
268#endif // CAFFEINE_HAL_HAL_H
Common type definitions and enums for the HAL.