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

[rtl872x] Poll micros instead of rom DelayUs function. Tweak 1ms test #2499

Merged
merged 2 commits into from
Aug 4, 2022
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
19 changes: 18 additions & 1 deletion hal/src/rtl872x/delay_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C" {
#include <limits.h>
#include <FreeRTOS.h>
#include <task.h>
#include "timer_hal.h"

void HAL_Delay_Milliseconds(uint32_t nTime)
{
Expand All @@ -33,5 +34,21 @@ void HAL_Delay_Milliseconds(uint32_t nTime)

void HAL_Delay_Microseconds(uint32_t uSec)
{
DelayUs(uSec);
system_tick_t start_micros = HAL_Timer_Get_Micro_Seconds();
system_tick_t current_micros = start_micros;
system_tick_t end_micros = start_micros + uSec;

// Handle case where micros rolls over
if (end_micros < start_micros) {
while(1) {
current_micros = HAL_Timer_Get_Micro_Seconds();
if (current_micros < start_micros) {
break;
}
}
}

while (current_micros < end_micros) {
current_micros = HAL_Timer_Get_Micro_Seconds();
}
}
18 changes: 11 additions & 7 deletions system/src/system_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,16 +544,20 @@ void system_delay_pump(unsigned long ms, bool force_no_background_loop=false)
break;
}
else if (elapsed_millis >= (ms-1)) {
// on the last millisecond, resolve using millis - we don't know how far in that millisecond had come
// on the last millisecond, resolve using micros - we don't know how far in that millisecond had come
// have to be careful with wrap around since start_micros can be greater than end_micros.

for (;;)
{
system_tick_t delay = end_micros-HAL_Timer_Get_Micro_Seconds();
if (delay>100000)
return;
HAL_Delay_Microseconds(min(delay/2, 1u));
system_tick_t start_micros = HAL_Timer_Get_Micro_Seconds();
system_tick_t delay = end_micros - start_micros;

// If delay duration extends across the micro rollover,
// account for micros remaining before rollover as well as micros after rollover
if (end_micros < start_micros) {
delay = (std::numeric_limits<system_tick_t>::max() - start_micros) + end_micros;
}

HAL_Delay_Microseconds(delay);
return;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion user/tests/wiring/no_fixture/delay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test(DELAY_01_delay_1ms_is_within_tolerance)
// These errors are mainly due to processing overhead, which is a greater factor on NRF with a slower clock speed
const uint32_t delay_us_error = 200; // 20%
#elif HAL_PLATFORM_RTL872X
const uint32_t delay_us_error = 60; // 6%
const uint32_t delay_us_error = 120; // 12%
#else
#error "Unsupported platform"
#endif
Expand Down