xref: /nrf52832-nimble/rt-thread/components/net/freemodbus/port/porttimer_m.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * FreeModbus Libary: RT-Thread Port
3  * Copyright (C) 2013 Armink <[email protected]>
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 
22 /* ----------------------- Platform includes --------------------------------*/
23 #include "port.h"
24 
25 /* ----------------------- Modbus includes ----------------------------------*/
26 #include "mb.h"
27 #include "mb_m.h"
28 #include "mbport.h"
29 
30 #if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
31 /* ----------------------- Variables ----------------------------------------*/
32 static USHORT usT35TimeOut50us;
33 static struct rt_timer timer;
34 static void prvvTIMERExpiredISR(void);
35 static void timer_timeout_ind(void* parameter);
36 
37 /* ----------------------- static functions ---------------------------------*/
38 static void prvvTIMERExpiredISR(void);
39 
40 /* ----------------------- Start implementation -----------------------------*/
xMBMasterPortTimersInit(USHORT usTimeOut50us)41 BOOL xMBMasterPortTimersInit(USHORT usTimeOut50us)
42 {
43     /* backup T35 ticks */
44     usT35TimeOut50us = usTimeOut50us;
45 
46     rt_timer_init(&timer, "master timer",
47                    timer_timeout_ind, /* bind timeout callback function */
48                    RT_NULL,
49                    (50 * usT35TimeOut50us) / (1000 * 1000 / RT_TICK_PER_SECOND) + 1,
50                    RT_TIMER_FLAG_ONE_SHOT); /* one shot */
51 
52     return TRUE;
53 }
54 
vMBMasterPortTimersT35Enable()55 void vMBMasterPortTimersT35Enable()
56 {
57     rt_tick_t timer_tick = (50 * usT35TimeOut50us)
58             / (1000 * 1000 / RT_TICK_PER_SECOND);
59 
60     /* Set current timer mode, don't change it.*/
61     vMBMasterSetCurTimerMode(MB_TMODE_T35);
62 
63     rt_timer_control(&timer, RT_TIMER_CTRL_SET_TIME, &timer_tick);
64 
65     rt_timer_start(&timer);
66 }
67 
vMBMasterPortTimersConvertDelayEnable()68 void vMBMasterPortTimersConvertDelayEnable()
69 {
70     rt_tick_t timer_tick = MB_MASTER_DELAY_MS_CONVERT * RT_TICK_PER_SECOND / 1000;
71 
72     /* Set current timer mode, don't change it.*/
73     vMBMasterSetCurTimerMode(MB_TMODE_CONVERT_DELAY);
74 
75     rt_timer_control(&timer, RT_TIMER_CTRL_SET_TIME, &timer_tick);
76 
77     rt_timer_start(&timer);
78 }
79 
vMBMasterPortTimersRespondTimeoutEnable()80 void vMBMasterPortTimersRespondTimeoutEnable()
81 {
82     rt_tick_t timer_tick = MB_MASTER_TIMEOUT_MS_RESPOND * RT_TICK_PER_SECOND / 1000;
83 
84     /* Set current timer mode, don't change it.*/
85     vMBMasterSetCurTimerMode(MB_TMODE_RESPOND_TIMEOUT);
86 
87     rt_timer_control(&timer, RT_TIMER_CTRL_SET_TIME, &timer_tick);
88 
89     rt_timer_start(&timer);
90 }
91 
vMBMasterPortTimersDisable()92 void vMBMasterPortTimersDisable()
93 {
94     rt_timer_stop(&timer);
95 }
96 
prvvTIMERExpiredISR(void)97 void prvvTIMERExpiredISR(void)
98 {
99     (void) pxMBMasterPortCBTimerExpired();
100 }
101 
timer_timeout_ind(void * parameter)102 static void timer_timeout_ind(void* parameter)
103 {
104     prvvTIMERExpiredISR();
105 }
106 
107 #endif
108