cputime_cortexm.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-12-23 Bernard first version
  9. */
  10. #include <rthw.h>
  11. #include <rtdevice.h>
  12. #include <rtthread.h>
  13. #include <board.h>
  14. /* Use Cycle counter of Data Watchpoint and Trace Register for CPU time */
  15. static float cortexm_cputime_getres(void)
  16. {
  17. float ret = 1000 * 1000 * 1000;
  18. ret = ret / SystemCoreClock;
  19. return ret;
  20. }
  21. static uint32_t cortexm_cputime_gettime(void)
  22. {
  23. return DWT->CYCCNT;
  24. }
  25. const static struct rt_clock_cputime_ops _cortexm_ops =
  26. {
  27. cortexm_cputime_getres,
  28. cortexm_cputime_gettime
  29. };
  30. int cortexm_cputime_init(void)
  31. {
  32. /* check support bit */
  33. if ((DWT->CTRL & (1UL << DWT_CTRL_NOCYCCNT_Pos)) == 0)
  34. {
  35. /* enable trace*/
  36. CoreDebug->DEMCR |= (1UL << CoreDebug_DEMCR_TRCENA_Pos);
  37. /* whether cycle counter not enabled */
  38. if ((DWT->CTRL & (1UL << DWT_CTRL_CYCCNTENA_Pos)) == 0)
  39. {
  40. /* enable cycle counter */
  41. DWT->CTRL |= (1UL << DWT_CTRL_CYCCNTENA_Pos);
  42. }
  43. clock_cpu_setops(&_cortexm_ops);
  44. }
  45. return 0;
  46. }
  47. INIT_BOARD_EXPORT(cortexm_cputime_init);