drv_common.c 880 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. * 2022-09-13 xjy198903 first implementation
  9. */
  10. #include <rtthread.h>
  11. #include "clock_config.h"
  12. void rt_hw_us_delay(rt_uint32_t us)
  13. {
  14. rt_uint32_t ticks;
  15. rt_uint32_t told, tnow, tcnt = 0;
  16. rt_uint32_t reload = SysTick->LOAD;
  17. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  18. told = SysTick->VAL;
  19. while (1)
  20. {
  21. tnow = SysTick->VAL;
  22. if (tnow != told)
  23. {
  24. if (tnow < told)
  25. {
  26. tcnt += told - tnow;
  27. }
  28. else
  29. {
  30. tcnt += reload - tnow + told;
  31. }
  32. told = tnow;
  33. if (tcnt >= ticks)
  34. {
  35. break;
  36. }
  37. }
  38. }
  39. }