api-hal-delay.c 799 B

123456789101112131415161718192021222324252627282930
  1. #include "api-hal-delay.h"
  2. #include <furi.h>
  3. #include <cmsis_os2.h>
  4. static uint32_t clk_per_microsecond;
  5. void api_hal_delay_init(void) {
  6. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  7. DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
  8. DWT->CYCCNT = 0U;
  9. clk_per_microsecond = SystemCoreClock / 1000000.0f;
  10. FURI_LOG_I("FuriHalDelay", "Init OK");
  11. }
  12. void delay_us(float microseconds) {
  13. uint32_t start = DWT->CYCCNT;
  14. uint32_t time_ticks = microseconds * clk_per_microsecond;
  15. while((DWT->CYCCNT - start) < time_ticks) {
  16. };
  17. }
  18. // cannot be used in ISR
  19. // TODO add delay_ISR variant
  20. void delay(float milliseconds) {
  21. uint32_t ticks = milliseconds / (1000.0f / osKernelGetTickFreq());
  22. osStatus_t result = osDelay(ticks);
  23. (void)result;
  24. furi_assert(result == osOK);
  25. }