diff --git a/hal/inc/hal_platform.h b/hal/inc/hal_platform.h index 70e6cc1cd7..db42ff3ce7 100644 --- a/hal/inc/hal_platform.h +++ b/hal/inc/hal_platform.h @@ -541,4 +541,9 @@ #define HAL_PLATFORM_I2C_BUFFER_SIZE(x) (HAL_PLATFORM_I2C_BUFFER_SIZE_DEFAULT) #endif // HAL_PLATFORM_I2C_BUFFER_SIZE +// hardware counter for System.ticks() +#ifndef HAL_PLATFORM_SYSTEM_HW_TICKS +#define HAL_PLATFORM_SYSTEM_HW_TICKS (0) +#endif // HAL_PLATFORM_SYSTEM_HW_TICKS + #endif /* HAL_PLATFORM_H */ diff --git a/hal/src/nRF52840/hal_platform_nrf52840_config.h b/hal/src/nRF52840/hal_platform_nrf52840_config.h index b4419a22e5..163554c7d1 100644 --- a/hal/src/nRF52840/hal_platform_nrf52840_config.h +++ b/hal/src/nRF52840/hal_platform_nrf52840_config.h @@ -107,3 +107,6 @@ #define HAL_PLATFORM_ERROR_MESSAGES (1) #define HAL_PLATFORM_PROHIBIT_XIP (1) + +// hardware counter for System.ticks() supported +#define HAL_PLATFORM_SYSTEM_HW_TICKS (1) diff --git a/platform/MCU/newhal-mcu/inc/hw_ticks.h b/platform/MCU/newhal-mcu/inc/hw_ticks.h index 37b936f130..f376bdbd1b 100644 --- a/platform/MCU/newhal-mcu/inc/hw_ticks.h +++ b/platform/MCU/newhal-mcu/inc/hw_ticks.h @@ -28,10 +28,10 @@ extern "C" { * The number of ticks per microsecond of the system counter. * SYSTEM_TICK_COUNTER */ -#define SYSTEM_US_TICKS 100 // cycles per microsecond +#define SYSTEM_US_TICKS (1) // cycles per microsecond /** - * Should return a value from a system counter. + * Should return a value from a system hardware counter if supported. */ #define SYSTEM_TICK_COUNTER 0 diff --git a/platform/MCU/rtl872x/inc/hw_ticks.h b/platform/MCU/rtl872x/inc/hw_ticks.h index adb07333cd..3b25a657bf 100644 --- a/platform/MCU/rtl872x/inc/hw_ticks.h +++ b/platform/MCU/rtl872x/inc/hw_ticks.h @@ -23,8 +23,8 @@ #include "platform_config.h" #include "system_tick_hal.h" -#define SYSTEM_US_TICKS (SystemCoreClock / 1000000) //cycles per microsecond -#define SYSTEM_TICK_COUNTER GetDwtCounter() +#define SYSTEM_US_TICKS (1) //cycles per microsecond +#define SYSTEM_TICK_COUNTER HAL_Timer_Get_Micro_Seconds() #ifdef __cplusplus extern "C" { @@ -62,8 +62,6 @@ uint64_t GetSystem1MsTick64(); */ system_tick_t GetSystem1UsTick(); -uint32_t GetDwtCounter(); - #ifdef __cplusplus } #endif diff --git a/platform/MCU/rtl872x/src/hw_ticks.c b/platform/MCU/rtl872x/src/hw_ticks.c index 72ceb0b160..6e14eec760 100644 --- a/platform/MCU/rtl872x/src/hw_ticks.c +++ b/platform/MCU/rtl872x/src/hw_ticks.c @@ -17,7 +17,6 @@ #include "hw_ticks.h" #include "timer_hal.h" -#include "rtl8721d.h" void System1MsTick(void) { @@ -50,7 +49,3 @@ void __advance_system1MsTick(uint64_t millis, system_tick_t micros_from_rollover { // Unsupported } - -uint32_t GetDwtCounter() { - return DWT->CYCCNT; -} diff --git a/user/tests/wiring/no_fixture/system.cpp b/user/tests/wiring/no_fixture/system.cpp index e9778900c2..aae38e922c 100644 --- a/user/tests/wiring/no_fixture/system.cpp +++ b/user/tests/wiring/no_fixture/system.cpp @@ -439,3 +439,51 @@ test(SYSTEM_08_hardware_info) { } #endif // HAL_PLATFORM_NCP } + +test(SYSTEM_09_system_ticks) { + // Serial.printlnf("ticksPerMicrosecond: %lu %lu", System.ticksPerMicrosecond(), System.ticksPerMicrosecond() * 100); + const uint32_t NUM_ITERATIONS = 100; + const uint32_t DURATION_US = 1000; // 10% higher than measured (includes for() loop overhead) + const uint32_t DURATION_TICKS = System.ticksPerMicrosecond() * DURATION_US; + const uint32_t MIN_DURATION_TICKS = DURATION_TICKS * 95 / 100; // 5% lower than measured (includes for() loop overhead) + const uint32_t MAX_DURATION_TICKS = DURATION_TICKS * 105 / 100; // 5% higher than measured (includes for() loop overhead) + uint32_t start; + uint32_t finish; + ATOMIC_BLOCK() { + start = System.ticks(); + for (uint32_t i = 0; i < NUM_ITERATIONS; i++) { + HAL_Delay_Microseconds(DURATION_US); + } + finish = System.ticks(); + } + uint32_t ticks = finish - start; + uint32_t expected_low = NUM_ITERATIONS * MIN_DURATION_TICKS; + uint32_t expected_high = NUM_ITERATIONS * MAX_DURATION_TICKS; + // Serial.printlnf("ticks: %lu <= %lu <= %lu", expected_low, ticks, expected_high); + assertLessOrEqual(ticks, expected_high); + assertMoreOrEqual(ticks, expected_low); +} + +test(SYSTEM_10_system_ticks_delay) { + // Serial.printlnf("ticksPerMicrosecond: %lu %lu", System.ticksPerMicrosecond(), System.ticksPerMicrosecond() * 100); + const uint32_t NUM_ITERATIONS = 100; + const uint32_t DURATION_US = 1000; // 10% higher than measured (includes for() loop overhead) + const uint32_t DURATION_TICKS = System.ticksPerMicrosecond() * DURATION_US; + const uint32_t MIN_DURATION_US = DURATION_US * 95 / 100; // 5% lower than measured (includes for() loop overhead) + const uint32_t MAX_DURATION_US = DURATION_US * 105 / 100; // 5% higher than measured (includes for() loop overhead) + uint32_t start; + uint32_t finish; + ATOMIC_BLOCK() { + start = HAL_Timer_Get_Micro_Seconds(); + for (uint32_t i = 0; i < NUM_ITERATIONS; i++) { + System.ticksDelay(DURATION_TICKS); + } + finish = HAL_Timer_Get_Micro_Seconds(); + } + uint32_t duration = finish - start; + uint32_t expected_low = NUM_ITERATIONS * MIN_DURATION_US; + uint32_t expected_high = NUM_ITERATIONS * MAX_DURATION_US; + // Serial.printlnf("duration: %lu <= %lu <= %lu", expected_low, duration, expected_high); + assertLessOrEqual(duration, expected_high); + assertMoreOrEqual(duration, expected_low); +} diff --git a/user/tests/wiring/no_fixture_spi/spix.cpp b/user/tests/wiring/no_fixture_spi/spix.cpp index 7bc98d187f..96d994c616 100644 --- a/user/tests/wiring/no_fixture_spi/spix.cpp +++ b/user/tests/wiring/no_fixture_spi/spix.cpp @@ -391,7 +391,12 @@ template