1 /* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * FreeModbus Libary: ESP32 Port Demo Application
17  * Copyright (C) 2010 Christian Walter <cwalter@embedded-solutions.at>
18  *
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  * 1. Redistributions of source code must retain the above copyright
24  *   notice, this list of conditions and the following disclaimer.
25  * 2. Redistributions in binary form must reproduce the above copyright
26  *   notice, this list of conditions and the following disclaimer in the
27  *   documentation and/or other materials provided with the distribution.
28  * 3. The name of the author may not be used to endorse or promote products
29  *   derived from this software without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33  * IF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  *
42  * File: $Id: portother.c,v 1.1 2010/06/06 13:07:20 wolti Exp $
43  */
44 /* ----------------------- Platform includes --------------------------------*/
45 #include "port.h"
46 
47 /* ----------------------- Modbus includes ----------------------------------*/
48 #include "sdkconfig.h"
49 #include "mb.h"
50 #include "mbport.h"
51 #include "driver/timer.h"
52 #include "port_serial_slave.h"
53 #include "sdkconfig.h"
54 
55 #if CONFIG_FMB_TIMER_PORT_ENABLED
56 
57 #define MB_US50_FREQ            (20000) // 20kHz 1/20000 = 50mks
58 #define MB_DISCR_TIME_US        (50)    // 50uS = one discreet for timer
59 
60 #define MB_TIMER_PRESCALLER     ((TIMER_BASE_CLK / MB_US50_FREQ) - 1)
61 #define MB_TIMER_SCALE          (TIMER_BASE_CLK / TIMER_DIVIDER)  // convert counter value to seconds
62 #define MB_TIMER_DIVIDER        ((TIMER_BASE_CLK / 1000000UL) * MB_DISCR_TIME_US - 1) // divider for 50uS
63 #define MB_TIMER_WITH_RELOAD    (1)
64 
65 static const USHORT usTimerIndex = CONFIG_FMB_TIMER_INDEX;      // Modbus Timer index used by stack
66 static const USHORT usTimerGroupIndex = CONFIG_FMB_TIMER_GROUP; // Modbus Timer group index used by stack
67 static timer_isr_handle_t xTimerIntHandle;                      // Timer interrupt handle
68 
69 /* ----------------------- Start implementation -----------------------------*/
vTimerGroupIsr(void * param)70 static void IRAM_ATTR vTimerGroupIsr(void *param)
71 {
72     assert((int)param == usTimerIndex);
73     // Retrieve the counter value from the timer that reported the interrupt
74     timer_group_clr_intr_status_in_isr(usTimerGroupIndex, usTimerIndex);
75     (void)pxMBPortCBTimerExpired(); // Timer callback function
76     // Enable alarm
77     timer_group_enable_alarm_in_isr(usTimerGroupIndex, usTimerIndex);
78 }
79 #endif
80 
xMBPortTimersInit(USHORT usTim1Timerout50us)81 BOOL xMBPortTimersInit(USHORT usTim1Timerout50us)
82 {
83 #if CONFIG_FMB_TIMER_PORT_ENABLED
84     MB_PORT_CHECK((usTim1Timerout50us > 0), FALSE,
85             "Modbus timeout discreet is incorrect.");
86     esp_err_t xErr;
87     timer_config_t config = {
88         .alarm_en = TIMER_ALARM_EN,
89         .auto_reload = MB_TIMER_WITH_RELOAD,
90         .counter_dir = TIMER_COUNT_UP,
91         .divider = MB_TIMER_PRESCALLER,
92         .intr_type = TIMER_INTR_LEVEL,
93         .counter_en = TIMER_PAUSE,
94     };
95     // Configure timer
96     xErr = timer_init(usTimerGroupIndex, usTimerIndex, &config);
97     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
98             "timer init failure, timer_init() returned (0x%x).", xErr);
99     // Stop timer counter
100     xErr = timer_pause(usTimerGroupIndex, usTimerIndex);
101     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
102                     "stop timer failure, timer_pause() returned (0x%x).", xErr);
103     // Reset counter value
104     xErr = timer_set_counter_value(usTimerGroupIndex, usTimerIndex, 0x00000000ULL);
105     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
106                     "timer set value failure, timer_set_counter_value() returned (0x%x).", xErr);
107     // wait3T5_us = 35 * 11 * 100000 / baud; // the 3.5T symbol time for baudrate
108     // Set alarm value for usTim1Timerout50us * 50uS
109     xErr = timer_set_alarm_value(usTimerGroupIndex, usTimerIndex, (uint32_t)(usTim1Timerout50us));
110     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
111                     "failure to set alarm failure, timer_set_alarm_value() returned (0x%x).",
112                     (uint32_t)xErr);
113     // Register ISR for timer
114     xErr = timer_isr_register(usTimerGroupIndex, usTimerIndex, vTimerGroupIsr,
115                                 (void*)(uint32_t)usTimerIndex, MB_PORT_TIMER_ISR_FLAG, &xTimerIntHandle);
116     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
117                     "timer set value failure, timer_isr_register() returned (0x%x).",
118                     (uint32_t)xErr);
119 #endif
120     return TRUE;
121 }
122 
vMBPortTimersEnable(void)123 void vMBPortTimersEnable(void)
124 {
125 #if CONFIG_FMB_TIMER_PORT_ENABLED
126     ESP_ERROR_CHECK(timer_pause(usTimerGroupIndex, usTimerIndex));
127     ESP_ERROR_CHECK(timer_set_counter_value(usTimerGroupIndex, usTimerIndex, 0ULL));
128     ESP_ERROR_CHECK(timer_enable_intr(usTimerGroupIndex, usTimerIndex));
129     ESP_ERROR_CHECK(timer_start(usTimerGroupIndex, usTimerIndex));
130 #endif
131 }
132 
133 void MB_PORT_ISR_ATTR
vMBPortTimersDisable(void)134 vMBPortTimersDisable(void)
135 {
136 #if CONFIG_FMB_TIMER_PORT_ENABLED
137     if( (BOOL)xPortInIsrContext() ) {
138         timer_group_set_counter_enable_in_isr(usTimerGroupIndex, usTimerIndex, TIMER_PAUSE);
139     } else {
140         ESP_ERROR_CHECK(timer_pause(usTimerGroupIndex, usTimerIndex));
141         ESP_ERROR_CHECK(timer_set_counter_value(usTimerGroupIndex, usTimerIndex, 0ULL));
142         // Disable timer interrupt
143         ESP_ERROR_CHECK(timer_disable_intr(usTimerGroupIndex, usTimerIndex));
144     }
145 #endif
146 }
147 
vMBPortTimerClose(void)148 void vMBPortTimerClose(void)
149 {
150 #ifdef CONFIG_FMB_TIMER_PORT_ENABLED
151     ESP_ERROR_CHECK(timer_deinit(usTimerGroupIndex, usTimerIndex));
152     ESP_ERROR_CHECK(esp_intr_free(xTimerIntHandle));
153 #endif
154 }
155