/******************************************************************************/ /* FILENAME : xf.h */ /*----------------------------------------------------------------------------*/ /* GOAL : Offers the femto XF functions (for PIC CPU) */ /*----------------------------------------------------------------------------*/ /* AUTHOR : Medard Rieder / Pascal Sartoretti */ /*----------------------------------------------------------------------------*/ /* DATE: : original (Medard Rieder 08.2011) */ /* corrections & simplified (Pascal Sartoretti 06.2016) */ /******************************************************************************/ #include // boolean types #include "xf.h" #include "../mcc_generated_files/mcc.h" /* * private methods of the XF */ /******************************************************************************/ /* FUNCTION : Push an event on the events queue */ /* INPUT : ev - the event number (not 0) */ /* inISR - (true if called in an ISR, else false) */ /* OUTPUT : return false if the queue was full, else true */ /* COMMENTS : - */ /******************************************************************************/ bool XF_pushEvent(Event ev, bool inISR, TimerID* tmid); /******************************************************************************/ /* FUNCTION : Pop an event on the events queue */ /* INPUT : inISR - (true if called in an ISR, else false) */ /* OUTPUT : return the next waiting event if any, else 0 */ /* COMMENTS : - */ /******************************************************************************/ Event XF_popEvent(bool inISR); /******************************************************************************/ /* FUNCTION : Post a timer in timers queue */ /* INPUT : tm - time before event arrives */ /* ev - event to post */ /* inISR - (true if called in an ISR, else false) */ /* OUTPUT : return the timer Id used */ /* COMMENTS : - */ /******************************************************************************/ TimerID XF_scheduleTimer(Time tm, Event ev, bool inISR); /******************************************************************************/ /* FUNCTION : Switch of the interrupts */ /* INPUT : inISR - (true if called in an ISR, else f */ /* OUTPUT : none */ /* COMMENTS : - */ /******************************************************************************/ static void ENTERCRITICAL(bool inISR); /******************************************************************************/ /* FUNCTION : Switch on the interrupts */ /* INPUT : inISR - (true if called in an ISR, else f */ /* OUTPUT : none */ /* COMMENTS : - */ /******************************************************************************/ static void LEAVECRITICAL(bool inISR); /* * the XF instance */ XF theXF; // really the XF /******************************************************************************/ /* FUNCTION : Init the XF structure */ /* INPUT : - */ /* OUTPUT : - */ /* COMMENTS : Have to be called once */ /******************************************************************************/ void XF_init() { int i; for (i=0; i 0) { Event_setDelay(&ev,0); *tmid = XF_scheduleTimer(tm, ev, inISR); } else { ENTERCRITICAL(inISR); temp = (theXF.in+1) % (uint8_t)(sizeof(theXF.eventQueue) / sizeof(Event)); if(temp == theXF.out) { LEAVECRITICAL(inISR); return false; } theXF.eventQueue[theXF.in] = ev; theXF.in = temp; LEAVECRITICAL(inISR); } return true; } /******************************************************************************/ /* FUNCTION : Pop an event on the events queue */ /* INPUT : inISR - (true if called in an ISR, else false) */ /* OUTPUT : return the next waiting event if any, else 0 */ /* COMMENTS : - */ /******************************************************************************/ Event XF_popEvent(bool inISR) { Event ev; ev.id = NULLEVENT; ev.target = NULL; ev.processEvent = NULL; ENTERCRITICAL(inISR); if(theXF.in == theXF.out) { LEAVECRITICAL(inISR); return ev; } ev = theXF.eventQueue[theXF.out]; theXF.out = (theXF.out + 1)%(uint8_t)(sizeof(theXF.eventQueue) / sizeof(Event)); LEAVECRITICAL(inISR); return ev; } /******************************************************************************/ /* FUNCTION : Post a timer in timers queue */ /* INPUT : tm - time before event arrives */ /* ev - event to post */ /* inISR - (true if called in an ISR, else false) */ /* OUTPUT : return the timer Id used */ /* COMMENTS : - */ /******************************************************************************/ TimerID XF_scheduleTimer(Time tm, Event ev, bool inISR) { uint8_t i; ENTERCRITICAL(inISR); for (i=0; i