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

Fix issues with ESP32 time and event functions #700

Merged
merged 1 commit into from
May 24, 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
3 changes: 2 additions & 1 deletion targets/FreeRTOS/ESP32_DevKitC/Include/targetHAL_Time.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <esp32_os.h>

#define HAL_Time_CurrentSysTicks xTaskGetTickCount
#define HAL_Time_CurrentSysTicks xTaskGetTickCount
#define ESP32_TICKS_PER_MS(x) ((x * (uint64_t)configTICK_RATE_HZ) / 1000)

#endif //_TARGET_HAL_TIME_H_
8 changes: 3 additions & 5 deletions targets/FreeRTOS/ESP32_DevKitC/nanoCLR/targetHAL_Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
#include <target_platform.h>
#include <Esp32_os.h>

#define ESP32_TICKS_PER_MS(x) ( ((uint64_t)x * configTICK_RATE_HZ) / 1000)

// Converts Tickcount to .NET ticks (100 nanoseconds)
int64_t HAL_Time_SysTicksToTime(unsigned int sysTicks) {

// convert to microseconds from FreeRtyos Tickcount
// convert to microseconds from FreeRTOS Tickcount
int64_t microsecondsFromSysTicks = ((( xTaskGetTickCount() ) * 1000000ULL + (int64_t)configTICK_RATE_HZ - 1ULL) / (int64_t)configTICK_RATE_HZ);

// need to convert from microseconds to 100 nanoseconds
Expand Down Expand Up @@ -128,8 +126,8 @@ const char* HAL_Time_CurrentDateTimeToString()
return DateTimeToString(HAL_Time_CurrentDateTime(false));
}


uint64_t CPU_MillisecondsToTicks(uint64_t ticks)
{
//return ESP32_TICKS_PER_MS(milliSeconds);
return ticks * 1000;
return ESP32_TICKS_PER_MS(ticks);
}
6 changes: 3 additions & 3 deletions targets/FreeRTOS/ESP32_DevKitC/nanoCLR/targetPAL_Events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ uint32_t Events_WaitForEvents( uint32_t powerLevel, uint32_t wakeupSystemEvents,
Events_WaitForEvents_Calls++;
#endif

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

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

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

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

// no events, pass control to the OS
Expand Down
6 changes: 4 additions & 2 deletions targets/FreeRTOS/ESP32_DevKitC/nanoCLR/targetPAL_Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void Time_SetCompare ( uint64_t compareValueTicks )
if(compareValueTicks == 0)
{
// compare value is 0 so dequeue and schedule immediately
NextEventTimer_Callback(NULL);
HAL_COMPLETION::DequeueAndExec();
}
else if(compareValueTicks == HAL_COMPLETION_IDLE_VALUE)
{
Expand All @@ -50,13 +50,15 @@ void Time_SetCompare ( uint64_t compareValueTicks )
}
else
{
xTimerStop( nextEventTimer, 0 );

// compareValueTicks is the time (in sys ticks) that is being requested to fire an HAL_COMPLETION::DequeueAndExec()
// need to subtract the current system time to set when the timer will fire
compareValueTicks -= HAL_Time_CurrentTime();

// no need to stop the timer even if it's running because the API does it anyway
// need to convert from nF ticks to milliseconds and then to FreeRTOS sys ticks to load the timer
xTimerChangePeriod( nextEventTimer, ((compareValueTicks / TIME_CONVERSION__TO_MILLISECONDS) / portTICK_PERIOD_MS), 0 );
xTimerChangePeriod(nextEventTimer, compareValueTicks, 0);
}
}
}