cputime_cortexm.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * File : cputime_cortexm.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-12-23 Bernard first version
  23. */
  24. #include <rthw.h>
  25. #include <rtdevice.h>
  26. #include <rtthread.h>
  27. #include <board.h>
  28. /* Use Cycle counter of Data Watchpoint and Trace Register for CPU time */
  29. static float cortexm_cputime_getres(void)
  30. {
  31. float ret = 1000 * 1000 * 1000;
  32. ret = ret / SystemCoreClock;
  33. return ret;
  34. }
  35. static uint32_t cortexm_cputime_gettime(void)
  36. {
  37. return DWT->CYCCNT;
  38. }
  39. const static struct rt_clock_cputime_ops _cortexm_ops =
  40. {
  41. cortexm_cputime_getres,
  42. cortexm_cputime_gettime
  43. };
  44. int cortexm_cputime_init(void)
  45. {
  46. /* check support bit */
  47. if ((DWT->CTRL & (1UL << DWT_CTRL_NOCYCCNT_Pos)) == 0)
  48. {
  49. /* enable trace*/
  50. CoreDebug->DEMCR |= (1UL << CoreDebug_DEMCR_TRCENA_Pos);
  51. /* whether cycle counter not enabled */
  52. if ((DWT->CTRL & (1UL << DWT_CTRL_CYCCNTENA_Pos)) == 0)
  53. {
  54. /* enable cycle counter */
  55. DWT->CTRL |= (1UL << DWT_CTRL_CYCCNTENA_Pos);
  56. }
  57. clock_cpu_setops(&_cortexm_ops);
  58. }
  59. return 0;
  60. }
  61. INIT_BOARD_EXPORT(cortexm_cputime_init);