porttimer.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "gd32e230x_conf.h"
  27. /* ----------------------- static functions ---------------------------------*/
  28. static void prvvTIMERExpiredISR( void );
  29. /* ----------------------- Start implementation -----------------------------*/
  30. BOOL
  31. xMBPortTimersInit(USHORT usTim1Timerout50us)
  32. {
  33. timer_parameter_struct timer_initpara; //timer_initpara用于存放定时器的参数
  34. //使能RCU相关时钟
  35. rcu_periph_clock_enable(RCU_TIMER13); //使能TIMER13的时钟
  36. timer_deinit(TIMER13); //设置TIMER13参数恢复默认值
  37. timer_struct_para_init(&timer_initpara); //初始化timer_initpara
  38. //配置TIMER16
  39. timer_initpara.prescaler = 3599; //设置预分频器值 //72M/3600=20Khz 对应基准50us
  40. timer_initpara.counterdirection = TIMER_COUNTER_UP; //设置向上计数模式
  41. timer_initpara.period = usTim1Timerout50us -1; //设置自动重装载值
  42. timer_initpara.clockdivision = TIMER_CKDIV_DIV1; //设置时钟分割
  43. timer_init(TIMER13, &timer_initpara); //根据参数初始化定时器
  44. timer_interrupt_enable(TIMER13, TIMER_INT_UP); //使能定时器的更新中断
  45. nvic_irq_enable(TIMER13_IRQn, 1); //配置NVIC设置优先级
  46. timer_enable(TIMER13); //使能定时器
  47. return TRUE;
  48. //return FALSE;
  49. }
  50. inline void
  51. vMBPortTimersEnable( )
  52. {
  53. /* Enable the timer with the timeout passed to xMBPortTimersInit( ) */
  54. timer_counter_value_config(TIMER13,0); //清零定时器
  55. timer_interrupt_flag_clear(TIMER13,TIMER_INT_UP); //清除定时器更新中断
  56. timer_interrupt_enable(TIMER13,TIMER_INT_UP); //使能定时器更新中断
  57. timer_enable(TIMER13); //使能定时器
  58. }
  59. inline void
  60. vMBPortTimersDisable( )
  61. {
  62. /* Disable any pending timers. */
  63. timer_counter_value_config(TIMER13,0); //清零定时器
  64. timer_interrupt_flag_clear(TIMER13,TIMER_INT_UP); //清除定时器更新中断
  65. timer_interrupt_disable(TIMER13,TIMER_INT_UP); //禁用定时器更新中断
  66. timer_disable(TIMER13); //失能定时器
  67. }
  68. /* Create an ISR which is called whenever the timer has expired. This function
  69. * must then call pxMBPortCBTimerExpired( ) to notify the protocol stack that
  70. * the timer has expired.
  71. */
  72. static void prvvTIMERExpiredISR( void )
  73. {
  74. ( void )pxMBPortCBTimerExpired( );
  75. }
  76. void TIMER13_IRQHandler(void)
  77. {
  78. if(RESET != timer_interrupt_flag_get(TIMER13, TIMER_INT_UP))
  79. {
  80. /* clear timer update interrupt bit */
  81. timer_interrupt_flag_clear(TIMER13, TIMER_INT_UP);
  82. /* clear count number*/
  83. timer_counter_value_config(TIMER13,0);
  84. /* use this function to call pxMBPortCBTimerExpired( ) */
  85. prvvTIMERExpiredISR();
  86. }
  87. }