eLooM for STM32 application  v3.3.0
A framework for multitasking low power embedded applications powerd by STM32
Loading...
Searching...
No Matches
HWDriverMap.h
Go to the documentation of this file.
1
28#ifndef INCLUDE_HWDRIVERMAP_H_
29#define INCLUDE_HWDRIVERMAP_H_
30
31
32#include "drivers/IDriver.h"
33#include "drivers/IDriver_vtbl.h"
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39#define HDM_NULL_KEY (0U)
45#define HDME_IS_EMPTY(p_element) (((p_element)->value.p_driver_obj == NULL) && ((p_element)->key == HDM_NULL_KEY))
46
47
51typedef struct _HWDriverMapValue
52{
57
63
67typedef struct _HWDriverMapElement
68{
72 uint32_t key;
73
78
80
84typedef struct _HWDriverMap
85{
90
94 uint16_t size;
95
99 uint16_t element_count;
101
110static inline
112{
113 assert_param(_this != NULL);
114 assert_param(p_elements != NULL);
115 uint32_t i = 0U;
116
117 _this->p_elements = p_elements;
118 _this->size = size;
119 _this->element_count = 0;
120
121 /* initialize all elements to NULL */
122 for(i = 0; i < size; i++)
123 {
124 p_elements[i].key = HDM_NULL_KEY;
125 p_elements[i].value.p_driver_obj = NULL;
126 p_elements[i].value.p_static_param = NULL;
127 }
128
129 return SYS_NO_ERROR_CODE;
130}
131
139static inline
141{
142 assert_param(_this != NULL);
143 uint32_t i = 0U;
144
145 while(_this->p_elements[i].key != key)
146 {
147 i++;
148 if(i >= _this->size)
149 {
150 return NULL;
151 }
152 }
153 return &_this->p_elements[i].value;
154}
155
166static inline
168{
169 assert_param(_this != NULL);
170 uint32_t i = 0U;
171 HWDriverMapElement_t *p_free_element = NULL;
172
173 while((_this->p_elements[i].key != key) && (i < _this->size))
174 {
175 if ((p_free_element == NULL) && HDME_IS_EMPTY(&_this->p_elements[i]))
176 {
177 /* we found a free element in the map.*/
178 p_free_element = &_this->p_elements[i];
179 }
180
181 i++;
182 }
183
184 if (i < _this->size)
185 {
186 /* the key is already present in the map*/
187 if (_this->p_elements[i].value.p_driver_obj == p_instance)
188 {
189 /* the element is present in the map, so we return it.*/
190 p_free_element = &_this->p_elements[i];
191 }
192 else
193 {
194 /* the key is already present in the map, but it is mapped to a different IDriver instance, so we cannot add the new element.*/
195 p_free_element = NULL;
196 }
197 }
198 else
199 {
200 /* the key is not present in the map, so we try to add the new element*/
201 if (p_free_element != NULL)
202 {
203 /* add the new element in the free position*/
204 p_free_element->key = key;
205 p_free_element->value.p_driver_obj = p_instance;
206 _this->element_count++;
207 }
208 }
209
210 return p_free_element;
211}
212
221static inline
222bool HWDriverMap_RemoveElement(HWDriverMap_t *_this, uint32_t key)
223{
224 assert_param(_this != NULL);
225 uint32_t i = 0U;
226 bool res = true;
227
228 while(res && (_this->p_elements[i].key != key))
229 {
230 if(++i >= _this->size)
231 {
232 res = false;
233 }
234 }
235
236 if (res)
237 {
238 _this->p_elements[i].key = HDM_NULL_KEY;
239 _this->p_elements[i].value.p_driver_obj = NULL;
240 _this->p_elements[i].value.p_static_param = NULL;
241 _this->element_count--;
242 }
243
244 return res;
245}
246
254static inline
256{
257 assert_param(_this != NULL);
258
259 return _this->size != 0;
260}
261
268static inline
270{
271 assert_param(_this != NULL);
272
273 return _this->element_count;
274}
275
276
277/* HWDriverMapValue public API */
278/*******************************/
279
280static inline
281void HWDriverMapValue_SetStaticParam(HWDriverMapValue_t *_this, void *p_static_param)
282{
283 assert_param(_this != NULL);
284
285 _this->p_static_param = p_static_param;
286}
287
288static inline
289void* HWDriverMapValue_GetStaticParam(const HWDriverMapValue_t * const _this)
290{
291 assert_param(_this != NULL);
292
293 return _this->p_static_param;
294}
295
296
297/* Utility functions */
298/*********************/
299
300static inline
301uint32_t HWDriverMap_GetKeyForGPIO(GPIO_TypeDef *p_port, uint32_t pin)
302{
303 uint32_t port = (uint32_t)p_port;
304 uint32_t position = 0U;
305 uint32_t key = HDM_NULL_KEY;
306
307 /*find the pin position*/
308 uint32_t iocurrent = 0U;
309 while ((iocurrent == 0U) && (position < 16))
310 {
311 iocurrent = (pin) & (1UL << position++);
312 }
313
314 if (iocurrent != 0)
315 {
316 key = port + position;
317 }
318
319 return key;
320}
321
322#ifdef __cplusplus
323}
324#endif
325
326#endif /* INCLUDE_HWDRIVERMAP_H_ */
static sys_error_code_t HWDriverMap_Init(HWDriverMap_t *_this, HWDriverMapElement_t *p_elements, uint16_t size)
Definition HWDriverMap.h:111
struct _HWDriverMapElement HWDriverMapElement_t
static HWDriverMapValue_t * HWDriverMap_FindByKey(HWDriverMap_t *_this, uint32_t key)
Definition HWDriverMap.h:140
#define HDM_NULL_KEY
Definition HWDriverMap.h:39
static HWDriverMapElement_t * HWDriverMap_AddElement(HWDriverMap_t *_this, uint32_t key, IDriver *p_instance)
Definition HWDriverMap.h:167
struct _HWDriverMapValue HWDriverMapValue_t
static bool HWDriverMap_RemoveElement(HWDriverMap_t *_this, uint32_t key)
Definition HWDriverMap.h:222
#define HDME_IS_EMPTY(p_element)
Definition HWDriverMap.h:45
static bool HWDriverMap_IsInitialized(HWDriverMap_t *_this)
Definition HWDriverMap.h:255
struct _HWDriverMap HWDriverMap_t
static bool HWDriverMap_GetElementCount(const HWDriverMap_t *const _this)
Definition HWDriverMap.h:269
Public API for the Driver Interface.
Definition HWDriverMap.h:68
HWDriverMapValue_t value
Definition HWDriverMap.h:77
uint32_t key
Definition HWDriverMap.h:72
Definition HWDriverMap.h:85
HWDriverMapElement_t * p_elements
Definition HWDriverMap.h:89
uint16_t element_count
Definition HWDriverMap.h:99
uint16_t size
Definition HWDriverMap.h:94
Definition HWDriverMap.h:52
void * p_static_param
Definition HWDriverMap.h:61
IDriver * p_driver_obj
Definition HWDriverMap.h:56
Definition IDriver_vtbl.h:49
uint16_t sys_error_code_t
Definition syserror.h:41