api-hal-delay.c 756 B

1234567891011121314151617181920212223242526272829
  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. }
  11. void delay_us(float microseconds) {
  12. uint32_t start = DWT->CYCCNT;
  13. uint32_t time_ticks = microseconds * clk_per_microsecond;
  14. while((DWT->CYCCNT - start) < time_ticks) {
  15. };
  16. }
  17. // cannot be used in ISR
  18. // TODO add delay_ISR variant
  19. void delay(float milliseconds) {
  20. uint32_t ticks = milliseconds / (1000.0f / osKernelGetTickFreq());
  21. osStatus_t result = osDelay(ticks);
  22. (void)result;
  23. furi_assert(result == osOK);
  24. }