porttimer.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "at32f403a_407.h"
  2. #include "FreeRTOS.h"
  3. #include "task.h"
  4. #include "port.h"
  5. #include "mb.h"
  6. #include "mbport.h"
  7. static void prvvTIMERExpiredISR( void );
  8. static uint16_t timeout = 0;
  9. static uint16_t downcounter = 0;
  10. BOOL xMBPortTimersInit( USHORT usTim1Timerout50us )
  11. {
  12. #if 1
  13. crm_clocks_freq_type crm_clocks_freq_struct = {0};
  14. crm_periph_clock_enable(CRM_TMR7_PERIPH_CLOCK, TRUE);
  15. crm_clocks_freq_get(&crm_clocks_freq_struct);
  16. tmr_base_init(TMR7, 1, 5999);
  17. tmr_cnt_dir_set(TMR7, TMR_COUNT_UP);
  18. tmr_flag_clear(TMR7, TMR_OVF_FLAG);
  19. nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  20. nvic_irq_enable(TMR7_GLOBAL_IRQn, 5, 0);
  21. timeout = usTim1Timerout50us;
  22. tmr_counter_enable(TMR7, FALSE);
  23. tmr_interrupt_enable(TMR7, TMR_OVF_INT, TRUE);
  24. return TRUE;
  25. #endif
  26. #if 0
  27. __HAL_RCC_TIM6_CLK_ENABLE();
  28. __HAL_RCC_TIM6_FORCE_RESET();
  29. __HAL_RCC_TIM6_RELEASE_RESET();
  30. htim.Instance = TIM6;
  31. htim.Init.Prescaler = (HAL_RCC_GetPCLK1Freq() / 1000000) - 1;
  32. htim.Init.CounterMode = TIM_COUNTERMODE_UP;
  33. htim.Init.Period = 50 - 1;
  34. htim.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  35. htim.Init.RepetitionCounter = 0;
  36. htim.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  37. timeout = usTim1Timerout50us;
  38. HAL_TIM_Base_Init(&htim);
  39. HAL_NVIC_SetPriority(TIM6_IRQn, 5, 0);
  40. HAL_NVIC_EnableIRQ(TIM6_IRQn);
  41. return TRUE;
  42. #endif
  43. }
  44. /* Create an ISR which is called whenever the timer has expired. This function
  45. * must then call pxMBPortCBTimerExpired( ) to notify the protocol stack that
  46. * the timer has expired.
  47. */
  48. static void prvvTIMERExpiredISR( void )
  49. {
  50. ( void )pxMBPortCBTimerExpired( );
  51. }
  52. void TMR7_GLOBAL_IRQHandler(void)
  53. {
  54. if(tmr_flag_get(TMR7, TMR_OVF_FLAG) != RESET)
  55. {
  56. tmr_flag_clear(TMR7, TMR_OVF_FLAG);
  57. if (!--downcounter)
  58. prvvTIMERExpiredISR();
  59. }
  60. }
  61. #if 0
  62. void TIM6_IRQHandler(void)
  63. {
  64. if(__HAL_TIM_GET_FLAG(&htim, TIM_FLAG_UPDATE) != RESET && __HAL_TIM_GET_IT_SOURCE(&htim, TIM_IT_UPDATE) != RESET)
  65. {
  66. __HAL_TIM_CLEAR_IT(&htim, TIM_IT_UPDATE);
  67. if (!--downcounter)
  68. prvvTIMERExpiredISR();
  69. }
  70. }
  71. #endif
  72. void
  73. vMBPortTimerClose( void )
  74. {
  75. tmr_interrupt_enable(TMR7, TMR_OVF_INT, FALSE);
  76. tmr_counter_enable(TMR7, FALSE);
  77. #if 0
  78. HAL_NVIC_DisableIRQ(TIM6_IRQn);
  79. __HAL_RCC_TIM6_CLK_DISABLE();
  80. #endif
  81. }
  82. void
  83. vMBPortTimersEnable( )
  84. {
  85. downcounter = timeout;
  86. tmr_counter_enable(TMR7, TRUE);
  87. #if 0
  88. downcounter = timeout;
  89. HAL_TIM_Base_Start_IT(&htim);
  90. #endif
  91. }
  92. void
  93. vMBPortTimersDisable( )
  94. {
  95. tmr_interrupt_enable(TMR7, TMR_OVF_INT, FALSE);
  96. // HAL_TIM_Base_Stop_IT(&htim);
  97. }
  98. void
  99. vMBPortTimersDelay( USHORT usTimeOutMS )
  100. {
  101. vTaskDelay( usTimeOutMS / portTICK_RATE_MS );
  102. }