furi_hal_delay.c 871 B

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