1 /*
2  * SPDX-FileCopyrightText: 2013 Armink
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
7  */
8 /*
9  * FreeModbus Libary: ESP32 Port
10  * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *   notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *   notice, this list of conditions and the following disclaimer in the
19  *   documentation and/or other materials provided with the distribution.
20  * 3. The name of the author may not be used to endorse or promote products
21  *   derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * IF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * File: $Id: porttimer_m.c,v 1.60 2013/08/13 15:07:05 Armink add Master Functions$
35  */
36 
37 /* ----------------------- Platform includes --------------------------------*/
38 #include "port.h"
39 
40 /* ----------------------- Modbus includes ----------------------------------*/
41 #include "mb_m.h"
42 #include "mbport.h"
43 #include "port_serial_master.h"
44 #include "sdkconfig.h"
45 
46 #define MB_US50_FREQ            (20000) // 20kHz 1/20000 = 50mks
47 #define MB_TICK_TIME_US         (50)    // 50uS = one tick for timer
48 
49 #define MB_TIMER_PRESCALLER     ((TIMER_BASE_CLK / MB_US50_FREQ) - 1)
50 #define MB_TIMER_SCALE          (TIMER_BASE_CLK / TIMER_DIVIDER)
51 #define MB_TIMER_DIVIDER        ((TIMER_BASE_CLK / 1000000UL) * MB_TICK_TIME_US - 1) // divider for 50uS
52 #define MB_TIMER_WITH_RELOAD    (1)
53 
54 /* ----------------------- Variables ----------------------------------------*/
55 static USHORT usT35TimeOut50us;
56 
57 // Initialize Modbus Timer group and index used by stack
58 static const USHORT usTimerIndex = CONFIG_FMB_MASTER_TIMER_INDEX;
59 static const USHORT usTimerGroupIndex = CONFIG_FMB_MASTER_TIMER_GROUP;
60 static timer_isr_handle_t xTimerIntHandle;  // Timer interrupt handle
61 
62 /* ----------------------- static functions ---------------------------------*/
vTimerGroupIsr(void * param)63 static void IRAM_ATTR vTimerGroupIsr(void *param)
64 {
65     assert((int)param == usTimerIndex);
66     // Retrieve the the counter value from the timer that reported the interrupt
67     timer_group_clr_intr_status_in_isr(usTimerGroupIndex, usTimerIndex);
68     (void)pxMBMasterPortCBTimerExpired(); // Timer expired callback function
69     // Enable alarm
70     timer_group_enable_alarm_in_isr(usTimerGroupIndex, usTimerIndex);
71 }
72 
73 /* ----------------------- Start implementation -----------------------------*/
xMBMasterPortTimersInit(USHORT usTimeOut50us)74 BOOL xMBMasterPortTimersInit(USHORT usTimeOut50us)
75 {
76     MB_PORT_CHECK((usTimeOut50us > 0), FALSE,
77             "Modbus timeout discreet is incorrect.");
78     // Save timer reload value for Modbus T35 period
79     usT35TimeOut50us = usTimeOut50us;
80     esp_err_t xErr;
81     timer_config_t config = {
82         .alarm_en = TIMER_ALARM_EN,
83         .auto_reload = MB_TIMER_WITH_RELOAD,
84         .counter_dir = TIMER_COUNT_UP,
85         .divider = MB_TIMER_PRESCALLER,
86         .intr_type = TIMER_INTR_LEVEL,
87         .counter_en = TIMER_PAUSE,
88     };
89     // Configure timer
90     xErr = timer_init(usTimerGroupIndex, usTimerIndex, &config);
91     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
92             "timer init failure, timer_init() returned (0x%x).", (uint32_t)xErr);
93     // Stop timer counter
94     xErr = timer_pause(usTimerGroupIndex, usTimerIndex);
95     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
96                     "stop timer failure, timer_pause() returned (0x%x).", (uint32_t)xErr);
97     // Reset counter value
98     xErr = timer_set_counter_value(usTimerGroupIndex, usTimerIndex, 0x00000000ULL);
99     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
100                     "timer set value failure, timer_set_counter_value() returned (0x%x).",
101                     (uint32_t)xErr);
102     // wait3T5_us = 35 * 11 * 100000 / baud; // the 3.5T symbol time for baudrate
103     // Set alarm value for usTimeOut50us * 50uS
104     xErr = timer_set_alarm_value(usTimerGroupIndex, usTimerIndex, (uint32_t)(usTimeOut50us));
105     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
106                     "failure to set alarm failure, timer_set_alarm_value() returned (0x%x).",
107                     (uint32_t)xErr);
108     // Register ISR for timer
109     xErr = timer_isr_register(usTimerGroupIndex, usTimerIndex,
110                                 vTimerGroupIsr, (void*)(uint32_t)usTimerIndex, MB_PORT_TIMER_ISR_FLAG, &xTimerIntHandle);
111     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
112                     "timer set value failure, timer_isr_register() returned (0x%x).",
113                     (uint32_t)xErr);
114     return TRUE;
115 }
116 
117 // Set alarm value for usTimerTimeOut50us * 50uS
xMBMasterPortTimersEnable(USHORT usTimerTics50us)118 static BOOL xMBMasterPortTimersEnable(USHORT usTimerTics50us)
119 {
120     MB_PORT_CHECK((usTimerTics50us > 0), FALSE,
121                             "incorrect tick value for timer = (0x%x).",
122                             (uint32_t)usTimerTics50us);
123     esp_err_t xErr;
124     xErr = timer_pause(usTimerGroupIndex, usTimerIndex); // stop timer
125     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
126                         "timer pause failure, timer_pause() returned (0x%x).",
127                         (uint32_t)xErr);
128     xErr = timer_set_counter_value(usTimerGroupIndex, usTimerIndex, 0ULL); // reset timer
129     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
130                         "timer set counter failure, timer_set_counter_value() returned (0x%x).",
131                         (uint32_t)xErr);
132     // Set alarm value to number of 50uS ticks
133     xErr = timer_set_alarm_value(usTimerGroupIndex, usTimerIndex,
134                                             (uint32_t)(usTimerTics50us));
135     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
136                     "timer set alarm failure, timer_set_alarm_value() returned (0x%x).",
137                     (uint32_t)xErr);
138     xErr = timer_enable_intr(usTimerGroupIndex, usTimerIndex);
139     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
140                             "timer enable interrupt failure, timer_enable_intr() returned (0x%x).",
141                             (uint32_t)xErr);
142     xErr = timer_start(usTimerGroupIndex, usTimerIndex); // start timer
143     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
144                             "timer start failure, timer_start() returned (0x%x).",
145                             (uint32_t)xErr);
146     return TRUE;
147 }
148 
vMBMasterPortTimersT35Enable(void)149 void vMBMasterPortTimersT35Enable(void)
150 {
151     USHORT usTimerTicks = usT35TimeOut50us;
152 
153     // Set current timer mode, don't change it.
154     vMBMasterSetCurTimerMode(MB_TMODE_T35);
155     // Set timer period
156     (void)xMBMasterPortTimersEnable(usTimerTicks);
157 }
158 
vMBMasterPortTimersConvertDelayEnable(void)159 void vMBMasterPortTimersConvertDelayEnable(void)
160 {
161     // Covert time in milliseconds into ticks
162     USHORT usTimerTicks = ((MB_MASTER_DELAY_MS_CONVERT * 1000) / MB_TICK_TIME_US);
163 
164     // Set current timer mode
165     vMBMasterSetCurTimerMode(MB_TMODE_CONVERT_DELAY);
166     ESP_LOGD(MB_PORT_TAG,"%s Convert delay enable.", __func__);
167     (void)xMBMasterPortTimersEnable(usTimerTicks);
168 }
169 
vMBMasterPortTimersRespondTimeoutEnable(void)170 void vMBMasterPortTimersRespondTimeoutEnable(void)
171 {
172     USHORT usTimerTicks = (MB_MASTER_TIMEOUT_MS_RESPOND * 1000 / MB_TICK_TIME_US);
173 
174     vMBMasterSetCurTimerMode(MB_TMODE_RESPOND_TIMEOUT);
175     ESP_LOGD(MB_PORT_TAG,"%s Respond enable timeout.", __func__);
176     (void)xMBMasterPortTimersEnable(usTimerTicks);
177 }
178 
179 void MB_PORT_ISR_ATTR
vMBMasterPortTimersDisable()180 vMBMasterPortTimersDisable()
181 {
182     if( (BOOL)xPortInIsrContext() ) {
183         timer_group_set_counter_enable_in_isr(usTimerGroupIndex, usTimerIndex, TIMER_PAUSE);
184     } else {
185         // Stop timer and then reload timer counter value
186         ESP_ERROR_CHECK(timer_pause(usTimerGroupIndex, usTimerIndex));
187         ESP_ERROR_CHECK(timer_set_counter_value(usTimerGroupIndex, usTimerIndex, 0ULL));
188         // Disable timer interrupt
189         ESP_ERROR_CHECK(timer_disable_intr(usTimerGroupIndex, usTimerIndex));
190     }
191 }
192 
vMBMasterPortTimerClose(void)193 void vMBMasterPortTimerClose(void)
194 {
195     ESP_ERROR_CHECK(timer_deinit(usTimerGroupIndex, usTimerIndex));
196     ESP_ERROR_CHECK(esp_intr_free(xTimerIntHandle));
197 }
198