1
0

porttimer.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.c,v 1.60 2013/08/13 15:07:05 Armink $
  20. */
  21. /* ----------------------- Platform includes --------------------------------*/
  22. #include "port.h"
  23. /* ----------------------- Modbus includes ----------------------------------*/
  24. #include "mb.h"
  25. #include "mbport.h"
  26. /* ----------------------- static functions ---------------------------------*/
  27. static void prvvTIMERExpiredISR(void);
  28. /* ----------------------- Start implementation -----------------------------*/
  29. BOOL xMBPortTimersInit(USHORT usTim1Timerout50us)
  30. {
  31. uint16_t PrescalerValue = 0;
  32. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  33. NVIC_InitTypeDef NVIC_InitStructure;
  34. //====================================时钟初始化===========================
  35. //使能定时器3时钟
  36. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  37. //====================================定时器初始化===========================
  38. //定时器时间基配置说明
  39. //HCLK为72MHz,APB1经过2分频为36MHz
  40. //TIM3的时钟倍频后为72MHz(硬件自动倍频,达到最大)
  41. //TIM3的分频系数为3599,时间基频率为72 / (1 + Prescaler) = 20KHz,基准为50us
  42. //TIM最大计数值为usTim1Timerout50u
  43. PrescalerValue = (uint16_t) (SystemCoreClock / 20000) - 1;
  44. //定时器1初始化
  45. TIM_TimeBaseStructure.TIM_Period = (uint16_t) usTim1Timerout50us;
  46. TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  47. TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  48. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  49. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  50. //预装载使能
  51. TIM_ARRPreloadConfig(TIM3, ENABLE);
  52. //====================================中断初始化===========================
  53. //设置NVIC优先级分组为Group2:0-3抢占式优先级,0-3的响应式优先级
  54. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  55. NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  56. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  57. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  58. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  59. NVIC_Init(&NVIC_InitStructure);
  60. //清除溢出中断标志位
  61. TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  62. //定时器3溢出中断关闭
  63. TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE);
  64. //定时器3禁能
  65. TIM_Cmd(TIM3, DISABLE);
  66. return TRUE;
  67. }
  68. void vMBPortTimersEnable()
  69. {
  70. TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  71. TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
  72. TIM_SetCounter(TIM3, 0);
  73. TIM_Cmd(TIM3, ENABLE);
  74. }
  75. void vMBPortTimersDisable()
  76. {
  77. TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  78. TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE);
  79. TIM_SetCounter(TIM3, 0);
  80. TIM_Cmd(TIM3, DISABLE);
  81. }
  82. void prvvTIMERExpiredISR(void)
  83. {
  84. (void) pxMBPortCBTimerExpired();
  85. }
  86. void TIM3_IRQHandler(void)
  87. {
  88. rt_interrupt_enter();
  89. if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  90. {
  91. TIM_ClearFlag(TIM3, TIM_FLAG_Update); //清中断标记
  92. TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //清除定时器T3溢出中断标志位
  93. prvvTIMERExpiredISR();
  94. }
  95. rt_interrupt_leave();
  96. }