| ISF 2.2 rev 5 Intelligent Sensing Framework for Kinetis with Processor Expert

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

isf_eventhandler.c
Go to the documentation of this file.
1 /*
2 *
3 * Copyright (c) 2015, Freescale Semiconductor, Inc.
4 *
5 */
6 /* ###################################################################
7 ** Filename : isf_eventhandler.c
8 ** Abstract :
9 ** This is the ISF Event Handler services.
10 **
11 ** ###################################################################*/
12 /*!
13 ** @file isf_eventhandler.c
14 ** @version 01.00
15 ** @brief
16 */
17 /*!
18 ** @addtogroup isf_eh_module isf_eh module documentation
19 ** @{
20 */
21
22 #include "Cpu.h"
23 #include "Events.h"
24 #include "isf_eventhandler.h"
25
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31
32 // TODO: Need to move this into a task defined by ISF_Core
33 // TODO: Need to define the Event Handler API
34 // TODO: Need to add semaphore protection of Event Handler Callback structure.
35 // Event Handler private variable definitions
36 event_t *pEventHandler_Events;
37 semaphore_t sema_ev_access;
38 static uint32 gEventHandlerIndex; // index into the eventHandlerCallbacks array
39 static isf_eh_event_t eventHandlerCallbacks[MAX_EVENTS]; //
40
41 // Event Handler Initialization function
42 void isf_EventHandler_Init(void)
43 {
44 // Initialize the events.
45 pEventHandler_Events = (event_t *)OSA_MemAlloc(sizeof(event_t));
46 OSA_EventCreate(pEventHandler_Events, kEventAutoClear);
47 OSA_EventClear(pEventHandler_Events, ALL_EVENTS);
48
49 // Create semaphore for access to event handler callback list.
50 OSA_SemaCreate(&sema_ev_access, 1);
51
52 // Initialize the next index
53 gEventHandlerIndex = 0;
54
55 // Initialize the signals and pEvents in the callback array
56 for (int i=0; i<MAX_EVENTS; i++)
57 {
58 eventHandlerCallbacks[i].Signal = (1 << i);
59 eventHandlerCallbacks[i].pEvent = pEventHandler_Events;
60 eventHandlerCallbacks[i].pCallback = NULL;
61 eventHandlerCallbacks[i].pUserData = NULL;
62 }
63 }
64
65 // Event Handler Register Event function
66 int32 isf_EventHandler_RegisterEvent(eventCallback_t *pCallback, void *pUserData)
67 {
68 int32 firstFreeIndex = -1;
69
70 // lock the access semaphore for the event handler callback structure.
71 OSA_SemaWait(&sema_ev_access, OSA_WAIT_FOREVER);
72
73 for (uint32 i=0; i<MAX_EVENTS; i++)
74 {
75 if ((eventHandlerCallbacks[i].pCallback == NULL) &&
76 (eventHandlerCallbacks[i].pUserData == NULL))
77 {
78 firstFreeIndex = i;
79 break;
80 }
81 }
82
83 if (firstFreeIndex >= 0)
84 {
85 // register the callback function and sensor handle
86 eventHandlerCallbacks[firstFreeIndex].pCallback = pCallback;
87 eventHandlerCallbacks[firstFreeIndex].pUserData = pUserData;
88 }
89
90 // release the access semaphore for the event handler callback structure.
91 OSA_SemaPost(&sema_ev_access);
92
93 // return the event handler callback structure
94 return firstFreeIndex;
95 }
96
97 // Event Handler Unregister Event function
98 int32 isf_EventHandler_UnregisterEvent(uint32 token)
99 {
100 int32 status = 0;
101
102 // lock the access semaphore for the event handler callback structure.
103 OSA_SemaWait(&sema_ev_access, OSA_WAIT_FOREVER);
104
105 if ((token >= 0) && (token < MAX_EVENTS))
106 {
107 // register the callback function and sensor handle
108 eventHandlerCallbacks[token].pCallback = NULL;
109 eventHandlerCallbacks[token].pUserData = NULL;
110 }
111 else
112 {
113 status = -1;
114 }
115
116 // release the access semaphore for the event handler callback structure.
117 OSA_SemaPost(&sema_ev_access);
118
119 return status;
120 }
121
122 // Get the Event Handler event signal.
123 int32 isf_EventHandler_GetSignal(uint32 token)
124 {
125 int32 status = 0;
126
127 // lock the access semaphore for the event handler callback structure.
128 OSA_SemaWait(&sema_ev_access, OSA_WAIT_FOREVER);
129
130 if ((token >= 0) && (token < MAX_EVENTS) && (eventHandlerCallbacks[token].pCallback != NULL))
131 {
132 status = eventHandlerCallbacks[token].Signal;
133 }
134 else
135 {
136 status = -1;
137 }
138
139 // release the access semaphore for the event handler callback structure.
140 OSA_SemaPost(&sema_ev_access);
141
142 return status;
143 }
144
145 // Event Handler task
146 void EventHandler_task(void)
147 {
148 uint32 callbacks = 0;
149 uint32 cbIndex;
150
151 while(1)
152 {
153 // Wait for an event to arrive.
154 OSA_EventWait(pEventHandler_Events, ALL_EVENTS, FALSE, OSA_WAIT_FOREVER, &callbacks);
155
156 // lock the access semaphore for the event handler callback structure.
157 OSA_SemaWait(&sema_ev_access, OSA_WAIT_FOREVER);
158
159 cbIndex = 0;
160 while(cbIndex < 32)
161 {
162 if (callbacks & (1 << cbIndex))
163 {
164 if (eventHandlerCallbacks[cbIndex].pCallback != NULL)
165 {
166 eventHandlerCallbacks[cbIndex].pCallback(eventHandlerCallbacks[cbIndex].pUserData);
167 }
168 }
169 cbIndex++;
170 }
171
172 // release the access semaphore for the event handler callback structure.
173 OSA_SemaPost(&sema_ev_access);
174
175 }
176 }
177
178 #ifdef __cplusplus
179 } /* extern "C" */
180 #endif
181
182 /*!
183 ** @}
184 */
185
186
187
188
189
190
191
Cpu.h
isf_EventHandler_RegisterEvent
int32 isf_EventHandler_RegisterEvent(eventCallback_t *pCallback, void *pUserData)
Definition: isf_eventhandler.c:66
EventHandler_task
void EventHandler_task(void)
Definition: isf_eventhandler.c:146
FALSE
#define FALSE
Definition: isf_types.h:86
isf_EventHandler_GetSignal
int32 isf_EventHandler_GetSignal(uint32 token)
Definition: isf_eventhandler.c:123
isf_eh_event_t::pEvent
event_t * pEvent
Definition: isf_eventhandler.h:42
sema_ev_access
semaphore_t sema_ev_access
Definition: isf_eventhandler.c:37
eventCallback_t
void( eventCallback_t)(void *)
Definition: isf_eventhandler.h:37
ALL_EVENTS
#define ALL_EVENTS
Definition: isf_eventhandler.h:34
isf_eh_event_t::pUserData
void * pUserData
Definition: isf_eventhandler.h:44
isf_EventHandler_UnregisterEvent
int32 isf_EventHandler_UnregisterEvent(uint32 token)
Definition: isf_eventhandler.c:98
MAX_EVENTS
#define MAX_EVENTS
Definition: isf_eventhandler.h:33
isf_eventhandler.h
pEventHandler_Events
event_t * pEventHandler_Events
Definition: isf_eventhandler.c:36
isf_eh_event_t
Definition: isf_eventhandler.h:39
Events.h
This is user's event module. Put your event handler code here.
isf_eh_event_t::pCallback
eventCallback_t * pCallback
Definition: isf_eventhandler.h:43
int32
signed long int int32
Definition: isf_types.h:74
uint32
unsigned long int uint32
Definition: isf_types.h:78
isf_eh_event_t::Signal
uint32 Signal
Definition: isf_eventhandler.h:41
isf_EventHandler_Init
void isf_EventHandler_Init(void)
Definition: isf_eventhandler.c:42
- © Freescale Semiconductor, Inc. 2016. All Rights Reserved.