ISF  2.2 rev 5
Intelligent Sensing Framework for Kinetis with Processor Expert
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
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
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
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
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.
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
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 
int32 isf_EventHandler_RegisterEvent(eventCallback_t *pCallback, void *pUserData)
void EventHandler_task(void)
#define FALSE
Definition: isf_types.h:86
int32 isf_EventHandler_GetSignal(uint32 token)
semaphore_t sema_ev_access
void( eventCallback_t)(void *)
#define ALL_EVENTS
int32 isf_EventHandler_UnregisterEvent(uint32 token)
#define MAX_EVENTS
event_t * pEventHandler_Events
This is user's event module. Put your event handler code here.
eventCallback_t * pCallback
signed long int int32
Definition: isf_types.h:74
unsigned long int uint32
Definition: isf_types.h:78
void isf_EventHandler_Init(void)