porttimer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * FreeModbus Libary: BARE Port
  3. * Copyright (C) 2006 Christian Walter <wolti@sil.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * File: $Id$
  20. */
  21. /* ----------------------- Platform includes --------------------------------*/
  22. #include "port.h"
  23. /* ----------------------- Modbus includes ----------------------------------*/
  24. #include "mb.h"
  25. #include "mbport.h"
  26. #include "gd32e23x_timer.h"
  27. #include "gd32e23x_rcu.h"
  28. #include "gd32e23x_misc.h"
  29. #include "gd32e23x_it.h"
  30. /* ----------------------- static functions ---------------------------------*/
  31. static void prvvTIMERExpiredISR( void );
  32. /* ----------------------- Start implementation -----------------------------*/
  33. BOOL
  34. xMBPortTimersInit(USHORT usTim1Timerout50us)
  35. {
  36. timer_parameter_struct timer_initpara; //timer_initpara用于存放定时器的参数
  37. //使能RCU相关时钟
  38. rcu_periph_clock_enable(RCU_TIMER13); //使能TIMER13的时钟
  39. timer_deinit(TIMER13); //设置TIMER13参数恢复默认值
  40. timer_struct_para_init(&timer_initpara); //初始化timer_initpara
  41. //配置TIMER16
  42. timer_initpara.prescaler = 3599; //设置预分频器值 //72M/3600=20Khz 对应基准50us
  43. timer_initpara.counterdirection = TIMER_COUNTER_UP; //设置向上计数模式
  44. timer_initpara.period = usTim1Timerout50us -1; //设置自动重装载值
  45. timer_initpara.clockdivision = TIMER_CKDIV_DIV1; //设置时钟分割
  46. timer_init(TIMER13, &timer_initpara); //根据参数初始化定时器
  47. timer_interrupt_enable(TIMER13, TIMER_INT_UP); //使能定时器的更新中断
  48. nvic_irq_enable(TIMER13_IRQn, 1); //配置NVIC设置优先级
  49. timer_enable(TIMER13); //使能定时器
  50. return TRUE;
  51. //return FALSE;
  52. }
  53. inline void
  54. vMBPortTimersEnable( )
  55. {
  56. /* Enable the timer with the timeout passed to xMBPortTimersInit( ) */
  57. timer_counter_value_config(TIMER13,0); //清零定时器
  58. timer_interrupt_flag_clear(TIMER13,TIMER_INT_UP); //清除定时器更新中断
  59. timer_interrupt_enable(TIMER13,TIMER_INT_UP); //使能定时器更新中断
  60. timer_enable(TIMER13); //使能定时器
  61. }
  62. inline void
  63. vMBPortTimersDisable( )
  64. {
  65. /* Disable any pending timers. */
  66. timer_counter_value_config(TIMER13,0); //清零定时器
  67. timer_interrupt_flag_clear(TIMER13,TIMER_INT_UP); //清除定时器更新中断
  68. timer_interrupt_disable(TIMER13,TIMER_INT_UP); //禁用定时器更新中断
  69. timer_disable(TIMER13); //失能定时器
  70. }
  71. /* Create an ISR which is called whenever the timer has expired. This function
  72. * must then call pxMBPortCBTimerExpired( ) to notify the protocol stack that
  73. * the timer has expired.
  74. */
  75. static void prvvTIMERExpiredISR( void )
  76. {
  77. ( void )pxMBPortCBTimerExpired( );
  78. }
  79. void TIMER13_IRQHandler(void)
  80. {
  81. if(RESET != timer_interrupt_flag_get(TIMER13, TIMER_INT_UP))
  82. {
  83. /* clear timer update interrupt bit */
  84. timer_interrupt_flag_clear(TIMER13, TIMER_INT_UP);
  85. /* clear count number*/
  86. timer_counter_value_config(TIMER13,0);
  87. /* use this function to call pxMBPortCBTimerExpired( ) */
  88. prvvTIMERExpiredISR();
  89. }
  90. }