From ece6e7a3c4d09e2b9e8844107a4ddf136322f5f3 Mon Sep 17 00:00:00 2001 From: josesimoes Date: Tue, 10 Mar 2020 13:20:08 +0000 Subject: [PATCH] Fix events and timer compare - Port changes from #1566 to ESP32, NXP and TI platforms. - Improve check of "no delay" condition and dequeue and execute immediately instead of calling the timer API just because. Signed-off-by: josesimoes --- targets/CMSIS-OS/ChibiOS/nanoCLR/targetPAL_Time.cpp | 10 +++++++--- targets/FreeRTOS/common/nanoCLR/targetPAL_Events.cpp | 7 +++++-- targets/FreeRTOS/common/nanoCLR/targetPAL_Time.cpp | 6 ++++-- .../ESP32_WROOM_32/nanoCLR/targetPAL_Events.cpp | 4 ++-- .../ESP32_WROOM_32/nanoCLR/targetPAL_Time.cpp | 12 ++++++++++-- targets/TI-SimpleLink/nanoCLR/targetPAL_Events.cpp | 4 ++-- targets/TI-SimpleLink/nanoCLR/targetPAL_Time.cpp | 10 +++++++--- 7 files changed, 37 insertions(+), 16 deletions(-) diff --git a/targets/CMSIS-OS/ChibiOS/nanoCLR/targetPAL_Time.cpp b/targets/CMSIS-OS/ChibiOS/nanoCLR/targetPAL_Time.cpp index 4b7d1b1630..42a86153f2 100644 --- a/targets/CMSIS-OS/ChibiOS/nanoCLR/targetPAL_Time.cpp +++ b/targets/CMSIS-OS/ChibiOS/nanoCLR/targetPAL_Time.cpp @@ -64,10 +64,14 @@ void Time_SetCompare ( uint64_t compareValueTicks ) // compareValueTicks is in CMSIS ticks (which equals to ms), so we use TIME_MS2I only to round compareValueTicks -= HAL_Time_CurrentSysTicks(); uint64_t delay = TIME_MS2I(compareValueTicks); - // make sure that chVTSet does not get called with zero delay - if (delay == 0) + + // make sure that chVTSet does not get called with zero delay + if (delay == 0) { - delay = 1; + // compare value is 0 so dequeue and execute immediately + // no need to call the timer + HAL_COMPLETION::DequeueAndExec(); + return; } // no need to stop the timer if it's running because the API does it anyway diff --git a/targets/FreeRTOS/common/nanoCLR/targetPAL_Events.cpp b/targets/FreeRTOS/common/nanoCLR/targetPAL_Events.cpp index cb2451a8ad..37fa7bfe9d 100644 --- a/targets/FreeRTOS/common/nanoCLR/targetPAL_Events.cpp +++ b/targets/FreeRTOS/common/nanoCLR/targetPAL_Events.cpp @@ -122,7 +122,7 @@ uint32_t Events_WaitForEvents( uint32_t powerLevel, uint32_t wakeupSystemEvents, Events_WaitForEvents_Calls++; #endif - uint64_t expireTimeInTicks = HAL_Time_CurrentTime() + countsRemaining; + uint64_t expireTimeInTicks = HAL_Time_CurrentSysTicks() + countsRemaining; bool runContinuations = true; while(true) @@ -135,7 +135,7 @@ uint32_t Events_WaitForEvents( uint32_t powerLevel, uint32_t wakeupSystemEvents, return events; } - if(expireTimeInTicks <= HAL_Time_CurrentTime()) + if(expireTimeInTicks <= HAL_Time_CurrentSysTicks()) { break; } @@ -163,6 +163,9 @@ uint32_t Events_WaitForEvents( uint32_t powerLevel, uint32_t wakeupSystemEvents, { break; } + + // feed the watchdog... + Watchdog_Reset(); } return 0; diff --git a/targets/FreeRTOS/common/nanoCLR/targetPAL_Time.cpp b/targets/FreeRTOS/common/nanoCLR/targetPAL_Time.cpp index 17d8c04af4..3c250d25a4 100644 --- a/targets/FreeRTOS/common/nanoCLR/targetPAL_Time.cpp +++ b/targets/FreeRTOS/common/nanoCLR/targetPAL_Time.cpp @@ -58,8 +58,10 @@ void Time_SetCompare ( uint64_t compareValueTicks ) // need to subtract the current system time to set when the timer will fire compareValueTicks -= HAL_Time_CurrentTime(); - if (compareValueTicks == 0) { - // compare value is 0 so dequeue and schedule immediately + if (compareValueTicks == 0) + { + // compare value is 0 so dequeue and execute immediately + // no need to call the timer HAL_COMPLETION::DequeueAndExec(); return; } diff --git a/targets/FreeRTOS_ESP32/ESP32_WROOM_32/nanoCLR/targetPAL_Events.cpp b/targets/FreeRTOS_ESP32/ESP32_WROOM_32/nanoCLR/targetPAL_Events.cpp index c83b173644..5ed1a6c1ca 100644 --- a/targets/FreeRTOS_ESP32/ESP32_WROOM_32/nanoCLR/targetPAL_Events.cpp +++ b/targets/FreeRTOS_ESP32/ESP32_WROOM_32/nanoCLR/targetPAL_Events.cpp @@ -121,7 +121,7 @@ uint32_t Events_WaitForEvents( uint32_t powerLevel, uint32_t wakeupSystemEvents, Events_WaitForEvents_Calls++; #endif - uint64_t expireTimeInTicks = HAL_Time_CurrentTime() + countsRemaining; + uint64_t expireTimeInTicks = HAL_Time_CurrentSysTicks() + countsRemaining; bool runContinuations = true; while(true) @@ -134,7 +134,7 @@ uint32_t Events_WaitForEvents( uint32_t powerLevel, uint32_t wakeupSystemEvents, return events; } - if(expireTimeInTicks <= HAL_Time_CurrentTime()) + if(expireTimeInTicks <= HAL_Time_CurrentSysTicks()) { break; } diff --git a/targets/FreeRTOS_ESP32/ESP32_WROOM_32/nanoCLR/targetPAL_Time.cpp b/targets/FreeRTOS_ESP32/ESP32_WROOM_32/nanoCLR/targetPAL_Time.cpp index 30ec29360d..fb1d356c47 100644 --- a/targets/FreeRTOS_ESP32/ESP32_WROOM_32/nanoCLR/targetPAL_Time.cpp +++ b/targets/FreeRTOS_ESP32/ESP32_WROOM_32/nanoCLR/targetPAL_Time.cpp @@ -45,7 +45,7 @@ void Time_SetCompare ( uint64_t compareValueTicks ) } else { - if (HAL_Time_CurrentTime() >= compareValueTicks) + if (HAL_Time_CurrentSysTicks() >= compareValueTicks) { // already missed the event, dequeue and execute immediately HAL_COMPLETION::DequeueAndExec(); @@ -56,7 +56,15 @@ void Time_SetCompare ( uint64_t compareValueTicks ) // 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(); + compareValueTicks -= HAL_Time_CurrentSysTicks(); + + if (compareValueTicks == 0) + { + // compare value is 0 so dequeue and execute immediately + // no need to call the timer + HAL_COMPLETION::DequeueAndExec(); + return; + } // 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 diff --git a/targets/TI-SimpleLink/nanoCLR/targetPAL_Events.cpp b/targets/TI-SimpleLink/nanoCLR/targetPAL_Events.cpp index 6e445275db..06ee3a8968 100644 --- a/targets/TI-SimpleLink/nanoCLR/targetPAL_Events.cpp +++ b/targets/TI-SimpleLink/nanoCLR/targetPAL_Events.cpp @@ -151,7 +151,7 @@ uint32_t Events_WaitForEvents( uint32_t powerLevel, uint32_t wakeupSystemEvents, Events_WaitForEvents_Calls++; #endif - uint64_t expireTimeInTicks = HAL_Time_CurrentTime() + countsRemaining; + uint64_t expireTimeInTicks = HAL_Time_CurrentSysTicks() + countsRemaining; bool runContinuations = true; while(true) @@ -164,7 +164,7 @@ uint32_t Events_WaitForEvents( uint32_t powerLevel, uint32_t wakeupSystemEvents, return events; } - if(expireTimeInTicks <= HAL_Time_CurrentTime()) + if(expireTimeInTicks <= HAL_Time_CurrentSysTicks()) { break; } diff --git a/targets/TI-SimpleLink/nanoCLR/targetPAL_Time.cpp b/targets/TI-SimpleLink/nanoCLR/targetPAL_Time.cpp index 14e143bcc2..d3bcb86773 100644 --- a/targets/TI-SimpleLink/nanoCLR/targetPAL_Time.cpp +++ b/targets/TI-SimpleLink/nanoCLR/targetPAL_Time.cpp @@ -76,9 +76,13 @@ void Time_SetCompare ( uint64_t compareValueTicks ) // 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, 0); + if (compareValueTicks == 0) + { + // compare value is 0 so dequeue and execute immediately + // no need to call the timer + HAL_COMPLETION::DequeueAndExec(); + return; + } Clock_setPeriod(nextEventTimer, compareValueTicks); Clock_start(nextEventTimer);