porttimer_m.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * FreeModbus Libary: RT-Thread 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 > 0
  28. /* ----------------------- Variables ----------------------------------------*/
  29. static USHORT usT35TimeOut50us;
  30. static struct rt_timer timer;
  31. static void prvvTIMERExpiredISR(void);
  32. static void timer_timeout_ind(void* parameter);
  33. /* ----------------------- static functions ---------------------------------*/
  34. static void prvvTIMERExpiredISR(void);
  35. /* ----------------------- Start implementation -----------------------------*/
  36. BOOL xMBMasterPortTimersInit(USHORT usTimeOut50us)
  37. {
  38. /* backup T35 ticks */
  39. usT35TimeOut50us = usTimeOut50us;
  40. rt_timer_init(&timer, "master timer",
  41. timer_timeout_ind, /* bind timeout callback function */
  42. RT_NULL,
  43. (50 * usT35TimeOut50us) / (1000 * 1000 / RT_TICK_PER_SECOND) + 1,
  44. RT_TIMER_FLAG_ONE_SHOT); /* one shot */
  45. return TRUE;
  46. }
  47. void vMBMasterPortTimersT35Enable()
  48. {
  49. rt_tick_t timer_tick = (50 * usT35TimeOut50us)
  50. / (1000 * 1000 / RT_TICK_PER_SECOND);
  51. /* Set current timer mode, don't change it.*/
  52. vMBMasterSetCurTimerMode(MB_TMODE_T35);
  53. rt_timer_control(&timer, RT_TIMER_CTRL_SET_TIME, &timer_tick);
  54. rt_timer_start(&timer);
  55. }
  56. void vMBMasterPortTimersConvertDelayEnable()
  57. {
  58. rt_tick_t timer_tick = MB_MASTER_DELAY_MS_CONVERT * RT_TICK_PER_SECOND / 1000;
  59. /* Set current timer mode, don't change it.*/
  60. vMBMasterSetCurTimerMode(MB_TMODE_CONVERT_DELAY);
  61. rt_timer_control(&timer, RT_TIMER_CTRL_SET_TIME, &timer_tick);
  62. rt_timer_start(&timer);
  63. }
  64. void vMBMasterPortTimersRespondTimeoutEnable()
  65. {
  66. rt_tick_t timer_tick = MB_MASTER_TIMEOUT_MS_RESPOND * RT_TICK_PER_SECOND / 1000;
  67. /* Set current timer mode, don't change it.*/
  68. vMBMasterSetCurTimerMode(MB_TMODE_RESPOND_TIMEOUT);
  69. rt_timer_control(&timer, RT_TIMER_CTRL_SET_TIME, &timer_tick);
  70. rt_timer_start(&timer);
  71. }
  72. void vMBMasterPortTimersDisable()
  73. {
  74. rt_timer_stop(&timer);
  75. }
  76. void prvvTIMERExpiredISR(void)
  77. {
  78. (void) pxMBMasterPortCBTimerExpired();
  79. }
  80. static void timer_timeout_ind(void* parameter)
  81. {
  82. prvvTIMERExpiredISR();
  83. }
  84. #endif