porttimer.c 3.9 KB

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