drv_common.c 897 B

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-06-14 muaxiaohei first version
  9. */
  10. #include <rtthread.h>
  11. #include "drv_common.h"
  12. #include <board.h> /* for rt_hw_us_delay */
  13. #define DBG_TAG "drv.common"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. void rt_hw_us_delay(rt_uint32_t us)
  17. {
  18. rt_uint64_t total_delay_ticks, us_ticks, start, now, delta, reload;
  19. start = SysTick->CNT;
  20. reload = SysTick->CMP;
  21. us_ticks = SystemCoreClock / 8000000UL;
  22. total_delay_ticks = us * us_ticks;
  23. if (total_delay_ticks >= reload)
  24. {
  25. LOG_E("rt_hw_us_delay: the us parameter exceeds the maximum limit!");
  26. }
  27. do {
  28. now = SysTick->CNT;
  29. delta = start > now ? start - now : reload + start - now;
  30. } while(delta < total_delay_ticks);
  31. }