eLooM for STM32 application  v3.3.0
A framework for multitasking low power embedded applications powerd by STM32
Loading...
Searching...
No Matches
ManagedTaskMap.h
Go to the documentation of this file.
1
26#ifndef INCLUDE_MANAGEDTASKMAP_H_
27#define INCLUDE_MANAGEDTASKMAP_H_
28
29
31
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37#define MTM_NULL_KEY (0U)
43#define MTME_IS_EMPTY(p_element) (((p_element)->value.p_mtask_obj == NULL) && ((p_element)->key == MTM_NULL_KEY))
44
45
49typedef struct _MTMapValue
50{
55
61
65typedef struct _MTMapElement
66{
70 uint32_t key;
71
76
78
82typedef struct _MTMap
83{
88
92 uint16_t size;
93
97 uint16_t element_count;
99
108static inline
109sys_error_code_t MTMap_Init(MTMap_t *_this, MTMapElement_t *p_elements, uint16_t size)
110{
111 assert_param(_this != NULL);
112 assert_param(p_elements != NULL);
113 uint32_t i = 0U;
114
115 _this->p_elements = p_elements;
116 _this->size = size;
117 _this->element_count = 0;
118
119 /* initialize all elements to NULL */
120 for(i = 0; i < size; i++)
121 {
122 p_elements[i].key = MTM_NULL_KEY;
123 p_elements[i].value.p_mtask_obj = NULL;
124 p_elements[i].value.p_static_param = NULL;
125 }
126
127 return SYS_NO_ERROR_CODE;
128}
129
137static inline
139{
140 assert_param(_this != NULL);
141 uint32_t i = 0U;
142
143 while(_this->p_elements[i].key != key)
144 {
145 i++;
146 if(i >= _this->size)
147 {
148 return NULL;
149 }
150 }
151 return &_this->p_elements[i].value;
152}
153
164static inline
165MTMapElement_t *MTMap_AddElement(MTMap_t *_this, uint32_t key, AManagedTask *p_instance)
166{
167 assert_param(_this != NULL);
168 uint32_t i = 0U;
169 MTMapElement_t *p_free_element = NULL;
170
171 while((_this->p_elements[i].key != key) && (i < _this->size))
172 {
173 if ((p_free_element == NULL) && MTME_IS_EMPTY(&_this->p_elements[i]))
174 {
175 /* we found a free element in the map.*/
176 p_free_element = &_this->p_elements[i];
177 }
178
179 i++;
180 }
181
182 if (i < _this->size)
183 {
184 /* the key is already present in the map*/
185 if (_this->p_elements[i].value.p_mtask_obj == p_instance)
186 {
187 /* the element is present in the map, so we return it.*/
188 p_free_element = &_this->p_elements[i];
189 }
190 else
191 {
192 /* the key is already present in the map, but it is mapped to a different IDriver instance, so we cannot add the new element.*/
193 p_free_element = NULL;
194 }
195 }
196 else
197 {
198 /* the key is not present in the map, so we try to add the new element*/
199 if (p_free_element != NULL)
200 {
201 /* add the new element in the free position*/
202 p_free_element->key = key;
203 p_free_element->value.p_mtask_obj = p_instance;
204 _this->element_count++;
205 }
206 }
207
208 return p_free_element;
209}
210
219static inline
220bool MTMap_RemoveElement(MTMap_t *_this, uint32_t key)
221{
222 assert_param(_this != NULL);
223 uint32_t i = 0U;
224 bool res = true;
225
226 while(res && (_this->p_elements[i].key != key))
227 {
228 if(++i >= _this->size)
229 {
230 res = false;
231 }
232 }
233
234 if (res)
235 {
236 _this->p_elements[i].key = MTM_NULL_KEY;
237 _this->p_elements[i].value.p_mtask_obj = NULL;
238 _this->p_elements[i].value.p_static_param = NULL;
239 _this->element_count--;
240 }
241
242 return res;
243}
244
252static inline
254{
255 assert_param(_this != NULL);
256
257 return _this->size != 0;
258}
259
266static inline
267bool MTMap_GetElementCount(const MTMap_t * const _this)
268{
269 assert_param(_this != NULL);
270
271 return _this->element_count;
272}
273
274
275/* MTMapValue public API */
276/*******************************/
277
278static inline
279void MTMapValue_SetStaticParam(MTMapValue_t *_this, void *p_static_param)
280{
281 assert_param(_this != NULL);
282
283 _this->p_static_param = p_static_param;
284}
285
286static inline
287void* MTMapValue_GetStaticParam(const MTMapValue_t * const _this)
288{
289 assert_param(_this != NULL);
290
291 return _this->p_static_param;
292}
293
294
295/* Utility functions */
296/*********************/
297
298static inline
299uint32_t MTMap_GetKeyForGPIO(GPIO_TypeDef *p_port, uint32_t pin)
300{
301 uint32_t port = (uint32_t)p_port;
302 uint32_t position = 0U;
303 uint32_t key = MTM_NULL_KEY;
304
305 /*find the pin position*/
306 uint32_t iocurrent = 0U;
307 while ((iocurrent == 0U) && (position < 16))
308 {
309 iocurrent = (pin) & (1UL << position++);
310 }
311
312 if (iocurrent != 0)
313 {
314 key = port + position;
315 }
316
317 return key;
318}
319
320#ifdef __cplusplus
321}
322#endif
323
324#endif /* INCLUDE_MANAGEDTASKMAP_H_ */
This file declare the Managed task Interface.
static sys_error_code_t MTMap_Init(MTMap_t *_this, MTMapElement_t *p_elements, uint16_t size)
Definition ManagedTaskMap.h:109
static MTMapElement_t * MTMap_AddElement(MTMap_t *_this, uint32_t key, AManagedTask *p_instance)
Definition ManagedTaskMap.h:165
struct _MTMap MTMap_t
#define MTM_NULL_KEY
Definition ManagedTaskMap.h:37
struct _MTMapValue MTMapValue_t
struct _MTMapElement MTMapElement_t
static bool MTMap_GetElementCount(const MTMap_t *const _this)
Definition ManagedTaskMap.h:267
static MTMapValue_t * MTMap_FindByKey(MTMap_t *_this, uint32_t key)
Definition ManagedTaskMap.h:138
static bool MTMap_IsInitialized(MTMap_t *_this)
Definition ManagedTaskMap.h:253
#define MTME_IS_EMPTY(p_element)
Definition ManagedTaskMap.h:43
static bool MTMap_RemoveElement(MTMap_t *_this, uint32_t key)
Definition ManagedTaskMap.h:220
Definition AManagedTask_vtbl.h:94
Definition ManagedTaskMap.h:66
MTMapValue_t value
Definition ManagedTaskMap.h:75
uint32_t key
Definition ManagedTaskMap.h:70
Definition ManagedTaskMap.h:83
uint16_t element_count
Definition ManagedTaskMap.h:97
MTMapElement_t * p_elements
Definition ManagedTaskMap.h:87
uint16_t size
Definition ManagedTaskMap.h:92
Definition ManagedTaskMap.h:50
AManagedTask * p_mtask_obj
Definition ManagedTaskMap.h:54
void * p_static_param
Definition ManagedTaskMap.h:59
uint16_t sys_error_code_t
Definition syserror.h:41