timer.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @file timer.c
  3. * @author chipsea
  4. * @brief
  5. * @version 0.1
  6. * @date 2020-11-30
  7. * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
  8. * @note
  9. */
  10. #include "sdk_config.h"
  11. #include "rom_sym_def.h"
  12. #include "timer.h"
  13. #include "error.h"
  14. #include "clock.h"
  15. #include "pwrmgr.h"
  16. #include "jump_function.h"
  17. AP_TIM_TypeDef* const TimerIndex[FREE_TIMER_NUMBER]= {AP_TIM5,AP_TIM6};
  18. static ap_tm_hdl_t s_ap_callback = NULL;
  19. __ATTR_SECTION_SRAM__ static int HalTiemrClearInt(AP_TIM_TypeDef *TIMx)
  20. {
  21. return TIMx->EOI;
  22. }
  23. static void HalTimerStopCounter(AP_TIM_TypeDef *TIMx)
  24. {
  25. TIMx->ControlReg = 0;
  26. TIMx->LoadCount = 0; //0x0
  27. TIMx->CurrentCount = 0;//0x4
  28. }
  29. static void HalTimerSetLoadTimer(AP_TIM_TypeDef *TIMx, int time)
  30. {
  31. if(time>0)
  32. {
  33. TIMx->ControlReg = 0x0;
  34. TIMx->ControlReg = 0x2;
  35. TIMx->LoadCount = 4*time;// 4MHz system timer, * 4 to convert to 1MHz timer
  36. TIMx->ControlReg = 0x3;
  37. }
  38. else
  39. {
  40. TIMx->ControlReg = 0x0;
  41. }
  42. }
  43. __ATTR_SECTION_SRAM__ void HalTimer5IRQHandler(void)
  44. {
  45. if(AP_TIM5->status & 0x1)
  46. {
  47. HalTiemrClearInt(AP_TIM5);
  48. if(s_ap_callback)
  49. s_ap_callback(HAL_EVT_TIMER_5);
  50. }
  51. }
  52. __ATTR_SECTION_SRAM__ void HalTimer6IRQHandler(void)
  53. {
  54. if(AP_TIM6->status & 0x1)
  55. {
  56. HalTiemrClearInt(AP_TIM6);
  57. if(s_ap_callback)
  58. s_ap_callback(HAL_EVT_TIMER_6);
  59. }
  60. }
  61. static void HalTimerWakeupHandler(void)
  62. {
  63. if(s_ap_callback)
  64. s_ap_callback(HAL_EVT_WAKEUP);
  65. }
  66. void HalTimerSleepHandler(void)
  67. {
  68. if(s_ap_callback)
  69. s_ap_callback(HAL_EVT_SLEEP);
  70. }
  71. int HalTimerMaskInt(UserTimer_e timeId, bool en)
  72. {
  73. volatile AP_TIM_TypeDef *TIMx;
  74. if(timeId != AP_TIMER_ID_5 && timeId != AP_TIMER_ID_6)
  75. {
  76. return ERR_NOT_SUPPORTED;
  77. }
  78. TIMx = TimerIndex[timeId-AP_TIMER_ID_5];
  79. if(en)
  80. TIMx->ControlReg |= (1 << 2);
  81. else
  82. TIMx->ControlReg &= ~(1 << 2);
  83. return ERR_NONE;
  84. }
  85. int HalTimerSet(UserTimer_e timeId, uint32_t us)
  86. {
  87. uint32_t time = us;
  88. switch(timeId)
  89. {
  90. case AP_TIMER_ID_5:
  91. JUMP_FUNCTION(V24_IRQ_HANDLER) = (uint32_t)&HalTimer5IRQHandler;
  92. NVIC_EnableIRQ((IRQn_Type)TIM5_IRQn);
  93. NVIC_SetPriority((IRQn_Type)TIM5_IRQn, IRQ_PRIO_HAL);
  94. HalTimerSetLoadTimer(AP_TIM5, time);
  95. hal_clk_gate_enable(MOD_TIMER5);
  96. break;
  97. case AP_TIMER_ID_6:
  98. JUMP_FUNCTION(V25_IRQ_HANDLER) = (uint32_t)&HalTimer6IRQHandler;
  99. NVIC_EnableIRQ((IRQn_Type)TIM6_IRQn);
  100. NVIC_SetPriority((IRQn_Type)TIM6_IRQn, IRQ_PRIO_HAL);
  101. HalTimerSetLoadTimer(AP_TIM6, time);
  102. hal_clk_gate_enable(MOD_TIMER6);
  103. break;
  104. default:
  105. return ERR_INVALID_PARAM;
  106. }
  107. return ERR_NONE;
  108. }
  109. int HalTimerStop(UserTimer_e timeId)
  110. {
  111. switch(timeId)
  112. {
  113. case AP_TIMER_ID_5:
  114. JUMP_FUNCTION(V24_IRQ_HANDLER) = 0;
  115. HalTimerStopCounter(AP_TIM5);
  116. NVIC_DisableIRQ((IRQn_Type)TIM5_IRQn);
  117. hal_clk_gate_disable(MOD_TIMER5);
  118. break;
  119. case AP_TIMER_ID_6:
  120. JUMP_FUNCTION(V25_IRQ_HANDLER) = 0;
  121. HalTimerStopCounter(AP_TIM6);
  122. NVIC_DisableIRQ((IRQn_Type)TIM6_IRQn);
  123. hal_clk_gate_disable(MOD_TIMER6);
  124. break;
  125. default:
  126. return ERR_INVALID_PARAM;
  127. }
  128. return ERR_NONE;
  129. }
  130. int HalTimerInit(ap_tm_hdl_t callback)
  131. {
  132. s_ap_callback = callback;
  133. HalTimerStop(AP_TIMER_ID_5);
  134. HalTimerStop(AP_TIMER_ID_6);
  135. return hal_pwrmgr_register(MOD_TIMER, HalTimerSleepHandler, HalTimerWakeupHandler);
  136. }
  137. int HalTimerDeinit(void)
  138. {
  139. s_ap_callback = NULL;
  140. return hal_pwrmgr_unregister(MOD_TIMER);
  141. }