porttimer_m.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * FreeModbus Libary: STM32 Port
  3. * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
  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: porttimer_m.c,v 1.60 2013/08/13 15:07:05 Armink add Master Functions$
  20. */
  21. /* ----------------------- Platform includes --------------------------------*/
  22. #include "port.h"
  23. /* ----------------------- Modbus includes ----------------------------------*/
  24. #include "mb.h"
  25. #include "mb_m.h"
  26. #include "mbport.h"
  27. #if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED
  28. /* ----------------------- Variables ----------------------------------------*/
  29. static USHORT usT35TimeOut50us;
  30. static USHORT usPrescalerValue = 0;
  31. /* ----------------------- static functions ---------------------------------*/
  32. static void prvvTIMERExpiredISR(void);
  33. /* ----------------------- Start implementation -----------------------------*/
  34. BOOL xMBMasterPortTimersInit(USHORT usTimeOut50us)
  35. {
  36. NVIC_InitTypeDef NVIC_InitStructure;
  37. //====================================时钟初始化===========================
  38. //使能定时器2时钟
  39. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  40. //====================================定时器初始化===========================
  41. //定时器时间基配置说明
  42. //HCLK为72MHz,APB1经过2分频为36MHz
  43. //TIM2的时钟倍频后为72MHz(硬件自动倍频,达到最大)
  44. //TIM2的分频系数为3599,时间基频率为72 / (1 + Prescaler) = 20KHz,基准为50us
  45. //TIM最大计数值为usTim1Timerout50u
  46. usPrescalerValue = (uint16_t) (SystemCoreClock / 20000) - 1;
  47. //保存T35定时器计数值
  48. usT35TimeOut50us = usTimeOut50us;
  49. //预装载使能
  50. TIM_ARRPreloadConfig(TIM2, ENABLE);
  51. //====================================中断初始化===========================
  52. //设置NVIC优先级分组为Group2:0-3抢占式优先级,0-3的响应式优先级
  53. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  54. NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  55. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  56. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  57. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  58. NVIC_Init(&NVIC_InitStructure);
  59. //清除溢出中断标志位
  60. TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  61. //定时器3溢出中断关闭
  62. TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
  63. //定时器3禁能
  64. TIM_Cmd(TIM2, DISABLE);
  65. return TRUE;
  66. }
  67. void vMBMasterPortTimersT35Enable()
  68. {
  69. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  70. /* Set current timer mode,don't change it.*/
  71. vMBMasterSetCurTimerMode(MB_TMODE_T35);
  72. TIM_TimeBaseStructure.TIM_Prescaler = usPrescalerValue;
  73. TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  74. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  75. TIM_TimeBaseStructure.TIM_Period = (uint16_t) usT35TimeOut50us;
  76. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  77. TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  78. TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  79. TIM_SetCounter(TIM2, 0);
  80. TIM_Cmd(TIM2, ENABLE);
  81. }
  82. void vMBMasterPortTimersConvertDelayEnable()
  83. {
  84. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  85. /* Set current timer mode,don't change it.*/
  86. vMBMasterSetCurTimerMode(MB_TMODE_CONVERT_DELAY);
  87. TIM_TimeBaseStructure.TIM_Prescaler = usPrescalerValue;
  88. TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  89. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  90. TIM_TimeBaseStructure.TIM_Period = (uint16_t)(MB_MASTER_DELAY_MS_CONVERT * 1000 / 50);
  91. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  92. TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  93. TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  94. TIM_SetCounter(TIM2, 0);
  95. TIM_Cmd(TIM2, ENABLE);
  96. }
  97. void vMBMasterPortTimersRespondTimeoutEnable()
  98. {
  99. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  100. /* Set current timer mode,don't change it.*/
  101. vMBMasterSetCurTimerMode(MB_TMODE_RESPOND_TIMEOUT);
  102. TIM_TimeBaseStructure.TIM_Prescaler = usPrescalerValue;
  103. TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  104. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  105. TIM_TimeBaseStructure.TIM_Period = (uint16_t)(MB_MASTER_TIMEOUT_MS_RESPOND * 1000 / 50);
  106. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  107. TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  108. TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  109. TIM_SetCounter(TIM2, 0);
  110. TIM_Cmd(TIM2, ENABLE);
  111. }
  112. void vMBMasterPortTimersDisable()
  113. {
  114. TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  115. TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
  116. TIM_SetCounter(TIM2, 0);
  117. TIM_Cmd(TIM2, DISABLE);
  118. }
  119. void prvvTIMERExpiredISR(void)
  120. {
  121. (void) pxMBMasterPortCBTimerExpired();
  122. }
  123. void TIM2_IRQHandler(void)
  124. {
  125. rt_interrupt_enter();
  126. if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
  127. {
  128. TIM_ClearFlag(TIM2, TIM_FLAG_Update); //清中断标记
  129. TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //清除定时器TIM2溢出中断标志位
  130. prvvTIMERExpiredISR();
  131. }
  132. rt_interrupt_leave();
  133. }
  134. #endif