Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work on PAL events and PAL time #690

Merged
merged 4 commits into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/PAL/AsyncProcCall/AsyncCompletions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void HAL_COMPLETION::Abort()

//--//

void HAL_COMPLETION::WaitForInterrupts(uint64_t expire, uint32_t sleepLevel, uint64_t wakeEvents)
void HAL_COMPLETION::WaitForInterrupts(uint64_t expireTicks, uint32_t sleepLevel, uint64_t wakeEvents)
{
NATIVE_PROFILE_PAL_ASYNC_PROC_CALL();

Expand All @@ -186,7 +186,7 @@ void HAL_COMPLETION::WaitForInterrupts(uint64_t expire, uint32_t sleepLevel, uin
{
state = setCompare | nilCompare;
}
else if(ptr->EventTimeTicks > expire)
else if(ptr->EventTimeTicks > expireTicks)
{
state = setCompare | resetCompare;
}
Expand All @@ -197,7 +197,7 @@ void HAL_COMPLETION::WaitForInterrupts(uint64_t expire, uint32_t sleepLevel, uin

if(state & setCompare)
{
Time_SetCompare(expire);
Time_SetCompare(expireTicks);
}

CPU_Sleep((SLEEP_LEVEL_type)sleepLevel, wakeEvents);
Expand Down
2 changes: 1 addition & 1 deletion src/PAL/AsyncProcCall/Async_stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ __nfweak void HAL_COMPLETION::DequeueAndExec()
NATIVE_PROFILE_PAL_ASYNC_PROC_CALL();
}

__nfweak void HAL_COMPLETION::WaitForInterrupts( uint64_t Expire, uint32_t sleepLevel, uint64_t wakeEvents )
__nfweak void HAL_COMPLETION::WaitForInterrupts( uint64_t expireTicks, uint32_t sleepLevel, uint64_t wakeEvents )
{
NATIVE_PROFILE_PAL_ASYNC_PROC_CALL();
}
2 changes: 1 addition & 1 deletion src/PAL/Include/nanoPAL_AsyncProcCalls_decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ struct HAL_COMPLETION : public HAL_CONTINUATION

static void DequeueAndExec();

static void WaitForInterrupts( uint64_t expire, uint32_t sleepLevel, uint64_t wakeEvents );
static void WaitForInterrupts( uint64_t expireTicks, uint32_t sleepLevel, uint64_t wakeEvents );
};

//--//
Expand Down
6 changes: 3 additions & 3 deletions src/PAL/Include/nanoPAL_Time.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ extern "C" {
/// Initializes PAL Time drivers, must be called before any of the Time_* PAL
/// methods could be used.
/// </summary>
HRESULT Time_Initialize ( );
HRESULT Time_Uninitialize ( );
void Time_SetCompare ( uint64_t compareValue );
HRESULT Time_Initialize ( );
HRESULT Time_Uninitialize ( );
void Time_SetCompare ( uint64_t compareValueTicks );

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,6 @@
*/
#define CH_CFG_SYSTEM_TICK_HOOK() { \
/* System tick event code here.*/ \
extern void Time_Interrupt_Hook(); \
Time_Interrupt_Hook(); \
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,6 @@
*/
#define CH_CFG_SYSTEM_TICK_HOOK() { \
/* System tick event code here.*/ \
extern void Time_Interrupt_Hook(); \
Time_Interrupt_Hook(); \
}

/**
Expand Down
23 changes: 11 additions & 12 deletions targets/CMSIS-OS/ChibiOS/nanoCLR/targetPAL_Events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

uint64_t CPU_MiliSecondsToSysTicks(uint64_t miliSeconds);

// events timer
static virtual_timer_t eventsBoolTimer;
volatile uint32_t systemEvents;
// timer for bool events
static virtual_timer_t boolEventsTimer;
uint32_t systemEvents;

set_Event_Callback g_Event_Callback = NULL;
void* g_Event_Callback_Arg = NULL;
Expand All @@ -33,7 +33,7 @@ bool Events_Uninitialize()
{
NATIVE_PROFILE_PAL_EVENTS();

chVTResetI(&eventsBoolTimer);
chVTResetI(&boolEventsTimer);

return true;
}
Expand Down Expand Up @@ -96,11 +96,11 @@ void Events_SetBoolTimer( bool* timerCompleteFlag, uint32_t millisecondsFromNow
NATIVE_PROFILE_PAL_EVENTS();

// we assume only 1 can be active, abort previous just in case
chVTResetI(&eventsBoolTimer);
chVTResetI(&boolEventsTimer);

if(timerCompleteFlag != NULL)
{
chVTSetI(&eventsBoolTimer, TIME_MS2I(millisecondsFromNow), local_Events_SetBoolTimer_Callback, timerCompleteFlag);
chVTSetI(&boolEventsTimer, TIME_MS2I(millisecondsFromNow), local_Events_SetBoolTimer_Callback, timerCompleteFlag);
}
}

Expand All @@ -114,7 +114,7 @@ uint32_t Events_WaitForEvents( uint32_t powerLevel, uint32_t wakeupSystemEvents,
Events_WaitForEvents_Calls++;
#endif

uint64_t expire = HAL_Time_CurrentSysTicks() + countsRemaining;
uint64_t expireTicks = HAL_Time_CurrentTime() + countsRemaining;
bool runContinuations = true;

while(true)
Expand All @@ -125,7 +125,7 @@ uint32_t Events_WaitForEvents( uint32_t powerLevel, uint32_t wakeupSystemEvents,
return events;
}

if(expire <= HAL_Time_CurrentSysTicks())
if(expireTicks <= HAL_Time_CurrentTime())
{
break;
}
Expand All @@ -142,12 +142,11 @@ uint32_t Events_WaitForEvents( uint32_t powerLevel, uint32_t wakeupSystemEvents,
// try stalled continuations again after sleeping
runContinuations = true;

HAL_COMPLETION::WaitForInterrupts(expire, powerLevel, wakeupSystemEvents );
HAL_COMPLETION::WaitForInterrupts(expireTicks, powerLevel, wakeupSystemEvents );
}


// no events, release time to OS
osDelay(10);
// no events, pass control to the OS
osThreadYield();
}

return 0;
Expand Down
47 changes: 25 additions & 22 deletions targets/CMSIS-OS/ChibiOS/nanoCLR/targetPAL_Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,43 @@
#include <hal.h>
#include <ch.h>

static uint64_t g_nextEvent; // tick time of next event to be scheduled
// timer for next event
static virtual_timer_t nextEventTimer;

HRESULT Time_Initialize()

static void NextEventTimer_Callback( void* arg )
{
g_nextEvent = 0xFFFFFFFFFFFF; // never
(bool*)arg;

// nothing to do here has time management is handled by ChibiOS
return S_OK;
// this call also schedules the next one, if there is one
HAL_COMPLETION::DequeueAndExec();
}

HRESULT Time_Uninitialize()
HRESULT Time_Initialize()
{
// nothing to do here has time management is handled by ChibiOS
return S_OK;
}

void Time_SetCompare ( uint64_t compareValue )
HRESULT Time_Uninitialize()
{
// TODO
// setup timer with callback that calls HAL_COMPLETION::DequeueAndExec( );
// see Events_SetBoolTimer

g_nextEvent = compareValue;
// nothing to do here has time management is handled by ChibiOS
return S_OK;
}

extern "C" {

void Time_Interrupt_Hook()
void Time_SetCompare ( uint64_t compareValueTicks )
{
if (HAL_Time_CurrentSysTicks() >= g_nextEvent && g_nextEvent > 0)
{
// handle event
HAL_COMPLETION::DequeueAndExec(); // this also schedules the next one, if there is one
}
}

// can have only one event timer setup, abort previous just in case
chVTResetI(&nextEventTimer);

if(compareValueTicks == 0)
{
// compare value is 0 so dequeue and schedule immediately
NextEventTimer_Callback(NULL);
}
else
{
// need to convert from ticks to milliseconds
chVTSetI(&nextEventTimer, TIME_MS2I(compareValueTicks * TIME_CONVERSION__TO_MILLISECONDS), NextEventTimer_Callback, NULL);
}
}
41 changes: 14 additions & 27 deletions targets/FreeRTOS/ESP32_DevKitC/nanoCLR/targetPAL_Events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@

uint64_t CPU_MiliSecondsToSysTicks(uint64_t miliSeconds);

// events timer
static TimerHandle_t eventsBoolTimer;
// timer for bool events
static TimerHandle_t boolEventsTimer;
static bool* saveTimerCompleteFlag = 0;

volatile uint32_t systemEvents;


static void local_Events_SetBoolTimer_Callback( TimerHandle_t xTimer );

#define chSysLock()
#define chSysUnlock()

set_Event_Callback g_Event_Callback = NULL;
void* g_Event_Callback_Arg = NULL;

Expand All @@ -30,11 +26,9 @@ bool Events_Initialize()
NATIVE_PROFILE_PAL_EVENTS();

// init events
// chSysLock();
systemEvents = 0;
// chSysUnlock();

eventsBoolTimer = xTimerCreate( "eventsTimer", 10, pdFALSE, (void *)0, local_Events_SetBoolTimer_Callback);
boolEventsTimer = xTimerCreate( "boolEventsTimer", 10, pdFALSE, (void *)0, local_Events_SetBoolTimer_Callback);

return true;
}
Expand All @@ -43,7 +37,7 @@ bool Events_Uninitialize()
{
NATIVE_PROFILE_PAL_EVENTS();

xTimerDelete(eventsBoolTimer,0);
xTimerDelete(boolEventsTimer,0);

return true;
}
Expand All @@ -53,9 +47,7 @@ void Events_Set( uint32_t events )
NATIVE_PROFILE_PAL_EVENTS();

// set events
chSysLock();
systemEvents |= events;
chSysUnlock();

if( g_Event_Callback != NULL )
{
Expand All @@ -71,9 +63,7 @@ uint32_t Events_Get( uint32_t eventsOfInterest )
uint32_t returnEvents = (systemEvents & eventsOfInterest);

// ... clear the requested flags atomically
chSysLock();
systemEvents &= ~eventsOfInterest;
chSysUnlock();

// give the caller notice of just the events they asked for ( and were cleared already )
return returnEvents;
Expand All @@ -89,9 +79,6 @@ static void local_Events_SetBoolTimer_Callback( TimerHandle_t xTimer )
{
NATIVE_PROFILE_PAL_EVENTS();

// bool* timerCompleteFlag = (bool*)pvTimerGetTimerID( xTimer );
// *timerCompleteFlag = true;

*saveTimerCompleteFlag = true;
}

Expand All @@ -108,18 +95,18 @@ void Events_SetBoolTimer( bool* timerCompleteFlag, uint32_t millisecondsFromNow
NATIVE_PROFILE_PAL_EVENTS();

// we assume only 1 can be active, abort previous just in case
xTimerStop( eventsBoolTimer, 0 );
xTimerStop( boolEventsTimer, 0 );

if(timerCompleteFlag != NULL)
{

xTimerChangePeriod( eventsBoolTimer, millisecondsFromNow / portTICK_PERIOD_MS, 0 );
xTimerChangePeriod( boolEventsTimer, millisecondsFromNow / portTICK_PERIOD_MS, 0 );

// Was going to just change existing timer but vTimerSetTimerID() does not exist in this version of FreeRtos
// Was going to just change existing timer but vTimerSetTimerID() does not exist in this version of FreeRTOS
// As only one timer running at a time we will just save it in global memory
saveTimerCompleteFlag = timerCompleteFlag;
// vTimerSetTimerID( eventsBoolTimer, (void *)timerCompleteFlag );
xTimerStart(eventsBoolTimer, 0);
// vTimerSetTimerID( boolEventsTimer, (void *)timerCompleteFlag );
xTimerStart(boolEventsTimer, 0);
}
}

Expand All @@ -133,7 +120,7 @@ uint32_t Events_WaitForEvents( uint32_t powerLevel, uint32_t wakeupSystemEvents,
Events_WaitForEvents_Calls++;
#endif

uint64_t expire = HAL_Time_CurrentSysTicks() + countsRemaining;
uint64_t expireTicks = HAL_Time_CurrentTime() + countsRemaining;
bool runContinuations = true;

while(true)
Expand All @@ -144,7 +131,7 @@ uint32_t Events_WaitForEvents( uint32_t powerLevel, uint32_t wakeupSystemEvents,
return events;
}

if(expire <= HAL_Time_CurrentSysTicks())
if(expireTicks <= HAL_Time_CurrentTime())
{
break;
}
Expand All @@ -161,11 +148,11 @@ uint32_t Events_WaitForEvents( uint32_t powerLevel, uint32_t wakeupSystemEvents,
// try stalled continuations again after sleeping
runContinuations = true;

HAL_COMPLETION::WaitForInterrupts(expire, powerLevel, wakeupSystemEvents );
HAL_COMPLETION::WaitForInterrupts(expireTicks, powerLevel, wakeupSystemEvents );
}

// no events, release time to OS
vTaskDelay(0);
// no events, pass control to the OS
taskYIELD();
}

return 0;
Expand Down
Loading