Hardware Abstraction Layer for FreeRTOS
hal.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Author: Andreas Werner <kernel@andy89.org>
4  * Date: 2021
5  */
6 #ifndef HAL_H_
7 #define HAL_H_
8 #include <FreeRTOS.h>
9 #include <semphr.h>
10 #include <system.h>
11 #include <stdbool.h>
12 #include <os.h>
13 #ifdef __FRAMAC__
14 /* include FRAMAC FreeROTS specification */
15 # include <framaC/freeRTOS.h>
16 #endif
17 
48 struct hal {
52  bool init;
53 #ifdef CONFIG_INSTANCE_NAME
54 
57  const char *name;
58 #endif
59 #ifdef CONFIG_USE_RECURSIVE_MUTEXES
60 
64 #endif
65 };
71 inline int32_t hal_init(void *data) {
72 #ifdef CONFIG_USE_RECURSIVE_MUTEXES
73  struct hal *hal = data;
74  hal->lock = OS_CREATE_MUTEX_RECURSIVE(hal->lock);
75  if (hal->lock == NULL) {
76  return -1;
77  }
78 #endif
79  return 0;
80 }
86 inline int32_t hal_deinit(void *data) {
87 #ifdef CONFIG_USE_RECURSIVE_MUTEXES
88  struct hal *hal = data;
89  vSemaphoreDelete(hal->lock);
90 #endif
91  return 0;
92 }
98 inline bool hal_isInit(void *data) {
99  struct hal *hal = data;
100  return hal->init;
101 }
108 inline int32_t hal_lock(void *data, TickType_t waittime) {
109 #ifdef CONFIG_USE_RECURSIVE_MUTEXES
110  struct hal *hal = data;
111  return xSemaphoreTakeRecursive(hal->lock, waittime);
112 #else
113  return 1;
114 #endif
115 }
116 
122 inline int32_t hal_unlock(void *data) {
123 #ifdef CONFIG_USE_RECURSIVE_MUTEXES
124  struct hal *hal = data;
125  return xSemaphoreGiveRecursive(hal->lock);
126 #else
127  return 1;
128 #endif
129 }
130 #ifdef CONFIG_INSTANCE_NAME
131 
136 int32_t hal_printNames();
137 #endif
138 
139 #ifdef __FRAMAC__
140 #define HAL_DEFINE_GLOBAL_ARRAY(gns) \
141  extern uintptr_t _devs[]; \
142  extern uintptr_t *_devs_end
143 extern uint32_t _devs_size;
144 #else
145 
149 #define HAL_DEFINE_GLOBAL_ARRAY(gns) \
150  extern uintptr_t *_devs; \
151  extern uintptr_t *_devs_end
152 #endif
153 
161 inline uintptr_t *hal_getDev(uintptr_t **devs, uintptr_t **end, uint32_t index) {
162 #ifdef __FRAMAC__
163  if (index >= _devs_size) {
164  return NULL;
165  }
166 #else
167  /* Eva has problems to handle with this protection mechanism */
168  if ((devs + index) >= end) {
169  return NULL;
170  }
171 #endif
172  return devs[index];
173 }
182 #define HAL_GET_DEV(gns, index) (void *) hal_getDev((uintptr_t **) &_devs, &_devs_end, index)
183 
189 #define HAL_LOCK(data, waittime, errcode) do { \
190  int32_t lock_ret = hal_lock(data, waittime); \
191  if (lock_ret != 1) { \
192  return errcode; \
193  }; \
194 } while(0)
195 
201 #define HAL_UNLOCK(data, errcode) do { \
202  int32_t unlock_ret = hal_unlock(data); \
203  if (unlock_ret != 1) { \
204  return errcode; \
205  } \
206 } while(0)
207 
213 #define HAL_DEFINE_DEVICE_ENTRY(gns, ns, p) extern struct gns##_generic const * const ns##_##p
214 
221 #define HAL_ADDDEV(gns, ns, p) struct gns##_generic SECTION(".rodata.devs") USED NO_REORDER const * const ns##_##p = (void const *) &p
222 
226 #define HAL_ADD(ns, p) HAL_ADDDEV(hal, ns, p)
231 #define HAL_GET_GLOBAL_DEV(index) HAL_GET_DEV(hal, index)
232 #ifdef CONFIG_INSTANCE_NAME
233 
237 # define HAL_NAME(n) .gen.name = n,
238 #else
239 # define HAL_NAME(n)
240 #endif
241 
247 #define HAL_GET_ID(gns, ns, p) ({ \
248  HAL_DEFINE_DEVICE_ENTRY(gns, ns, p); \
249  uint32_t ret; \
250  ret = (uint32_t) ((((uintptr_t) (&ns##_##p)) - ((uintptr_t) (&_devs))) / sizeof(uintptr_t)); \
251  ret; \
252  })
253 
254 #ifdef CONFIG_GEN_VERSION
255 extern const char *versionDriver;
256 extern const char *versionMach;
257 extern const char *versionArch;
258 extern const char *versionKernel;
259 #endif
260 
262 #endif
os.h
OS_DEFINE_MUTEX_RECURSIVE
#define OS_DEFINE_MUTEX_RECURSIVE(name)
Definition: os.h:59
HAL_DEFINE_GLOBAL_ARRAY
#define HAL_DEFINE_GLOBAL_ARRAY(gns)
Definition: hal.h:149
hal
Definition: hal.h:48
OS_CREATE_MUTEX_RECURSIVE
#define OS_CREATE_MUTEX_RECURSIVE(handleName)
Definition: os.h:68
hal_getDev
uintptr_t * hal_getDev(uintptr_t **devs, uintptr_t **end, uint32_t index)
Definition: hal.h:161
hal::init
bool init
Definition: hal.h:52
hal_deinit
int32_t hal_deinit(void *data)
Definition: hal.h:86
hal_lock
int32_t hal_lock(void *data, TickType_t waittime)
Definition: hal.h:108
system.h
hal_init
int32_t hal_init(void *data)
Definition: hal.h:71
hal_isInit
bool hal_isInit(void *data)
Definition: hal.h:98
hal_unlock
int32_t hal_unlock(void *data)
Definition: hal.h:122