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 config.alarm_en = TIMER_ALARM_EN;
89 config.auto_reload = MB_TIMER_WITH_RELOAD;
90 config.counter_dir = TIMER_COUNT_UP;
91 config.divider = MB_TIMER_PRESCALLER;
92 config.intr_type = TIMER_INTR_LEVEL;
93 config.counter_en = TIMER_PAUSE;
94 // Configure timer
95 xErr = timer_init(usTimerGroupIndex, usTimerIndex, &config);
96 MB_PORT_CHECK((xErr == ESP_OK), FALSE,
97 "timer init failure, timer_init() returned (0x%x).", xErr);
98 // Stop timer counter
99 xErr = timer_pause(usTimerGroupIndex, usTimerIndex);
100 MB_PORT_CHECK((xErr == ESP_OK), FALSE,
101 "stop timer failure, timer_pause() returned (0x%x).", xErr);
102 // Reset counter value
103 xErr = timer_set_counter_value(usTimerGroupIndex, usTimerIndex, 0x00000000ULL);
104 MB_PORT_CHECK((xErr == ESP_OK), FALSE,
105 "timer set value failure, timer_set_counter_value() returned (0x%x).", xErr);
106 // wait3T5_us = 35 * 11 * 100000 / baud; // the 3.5T symbol time for baudrate
107 // Set alarm value for usTim1Timerout50us * 50uS
108 xErr = timer_set_alarm_value(usTimerGroupIndex, usTimerIndex, (uint32_t)(usTim1Timerout50us));
109 MB_PORT_CHECK((xErr == ESP_OK), FALSE,
110 "failure to set alarm failure, timer_set_alarm_value() returned (0x%x).",
111 (uint32_t)xErr);
112 // Register ISR for timer
113 xErr = timer_isr_register(usTimerGroupIndex, usTimerIndex, vTimerGroupIsr,
114 (void*)(uint32_t)usTimerIndex, MB_PORT_TIMER_ISR_FLAG, &xTimerIntHandle);
115 MB_PORT_CHECK((xErr == ESP_OK), FALSE,
116 "timer set value failure, timer_isr_register() returned (0x%x).",
117 (uint32_t)xErr);
118 #endif
119 return TRUE;
120 }
121
vMBPortTimersEnable(void)122 void vMBPortTimersEnable(void)
123 {
124 #if CONFIG_FMB_TIMER_PORT_ENABLED
125 ESP_ERROR_CHECK(timer_pause(usTimerGroupIndex, usTimerIndex));
126 ESP_ERROR_CHECK(timer_set_counter_value(usTimerGroupIndex, usTimerIndex, 0ULL));
127 ESP_ERROR_CHECK(timer_enable_intr(usTimerGroupIndex, usTimerIndex));
128 ESP_ERROR_CHECK(timer_start(usTimerGroupIndex, usTimerIndex));
129 #endif
130 }
131
132 void MB_PORT_ISR_ATTR
vMBPortTimersDisable(void)133 vMBPortTimersDisable(void)
134 {
135 #if CONFIG_FMB_TIMER_PORT_ENABLED
136 if( (BOOL)xPortInIsrContext() ) {
137 timer_group_set_counter_enable_in_isr(usTimerGroupIndex, usTimerIndex, TIMER_PAUSE);
138 } else {
139 ESP_ERROR_CHECK(timer_pause(usTimerGroupIndex, usTimerIndex));
140 ESP_ERROR_CHECK(timer_set_counter_value(usTimerGroupIndex, usTimerIndex, 0ULL));
141 // Disable timer interrupt
142 ESP_ERROR_CHECK(timer_disable_intr(usTimerGroupIndex, usTimerIndex));
143 }
144 #endif
145 }
146
vMBPortTimerClose(void)147 void vMBPortTimerClose(void)
148 {
149 #ifdef CONFIG_FMB_TIMER_PORT_ENABLED
150 ESP_ERROR_CHECK(timer_pause(usTimerGroupIndex, usTimerIndex));
151 ESP_ERROR_CHECK(timer_disable_intr(usTimerGroupIndex, usTimerIndex));
152 ESP_ERROR_CHECK(esp_intr_free(xTimerIntHandle));
153 #endif
154 }
155