porttimer.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. crm_clocks_freq_type crm_clocks_freq_struct = {0};
  13. crm_periph_clock_enable(CRM_TMR7_PERIPH_CLOCK, TRUE);
  14. crm_clocks_freq_get(&crm_clocks_freq_struct);
  15. tmr_base_init(TMR7, 1, 5999);
  16. tmr_cnt_dir_set(TMR7, TMR_COUNT_UP);
  17. tmr_flag_clear(TMR7, TMR_OVF_FLAG);
  18. nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  19. nvic_irq_enable(TMR7_GLOBAL_IRQn, 5, 0);
  20. timeout = usTim1Timerout50us;
  21. tmr_counter_enable(TMR7, FALSE);
  22. tmr_interrupt_enable(TMR7, TMR_OVF_INT, TRUE);
  23. return TRUE;
  24. }
  25. /* Create an ISR which is called whenever the timer has expired. This function
  26. * must then call pxMBPortCBTimerExpired( ) to notify the protocol stack that
  27. * the timer has expired.
  28. */
  29. static void prvvTIMERExpiredISR( void )
  30. {
  31. ( void )pxMBPortCBTimerExpired( );
  32. }
  33. //
  34. void TMR7_GLOBAL_IRQHandler(void)
  35. {
  36. if(tmr_flag_get(TMR7, TMR_OVF_FLAG) != RESET)
  37. {
  38. tmr_flag_clear(TMR7, TMR_OVF_FLAG);
  39. if (!--downcounter) {
  40. vMBPortSetWithinException(TRUE);
  41. prvvTIMERExpiredISR();
  42. vMBPortSetWithinException(FALSE);
  43. }
  44. }
  45. }
  46. //
  47. void vMBPortTimerClose( void )
  48. {
  49. tmr_interrupt_enable(TMR7, TMR_OVF_INT, FALSE);
  50. tmr_counter_enable(TMR7, FALSE);
  51. #if 0
  52. HAL_NVIC_DisableIRQ(TIM6_IRQn);
  53. __HAL_RCC_TIM6_CLK_DISABLE();
  54. #endif
  55. }
  56. //
  57. void vMBPortTimersEnable()
  58. {
  59. downcounter = timeout;
  60. tmr_counter_enable(TMR7, TRUE);
  61. tmr_interrupt_enable(TMR7, TMR_OVF_INT, TRUE);
  62. #if 0
  63. downcounter = timeout;
  64. HAL_TIM_Base_Start_IT(&htim);
  65. #endif
  66. }
  67. //
  68. void vMBPortTimersDisable()
  69. {
  70. tmr_interrupt_enable(TMR7, TMR_OVF_INT, FALSE);
  71. // HAL_TIM_Base_Stop_IT(&htim);
  72. }
  73. //
  74. void vMBPortTimersDelay( USHORT usTimeOutMS )
  75. {
  76. vTaskDelay( usTimeOutMS / portTICK_RATE_MS );
  77. }