-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
emsuart_esp32.cpp
158 lines (142 loc) · 5.29 KB
/
emsuart_esp32.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* EMS-ESP - https://github.com/emsesp/EMS-ESP
* Copyright 2020 Paul Derbyshire
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* ESP32 UART port by @ArwedL and improved by @MichaelDvP. See https://github.com/emsesp/EMS-ESP/issues/380
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/uart.h"
#include "soc/uart_reg.h"
#include "uart/emsuart_esp32.h"
#include "emsesp.h"
namespace emsesp {
static QueueHandle_t uart_queue;
uint8_t tx_mode_;
/*
* receive task, wait for break and call incoming_telegram
*/
void EMSuart::uart_event_task(void * pvParameters) {
uart_event_t event;
uint8_t telegram[EMS_MAXBUFFERSIZE];
size_t length;
while (1) {
//Waiting for UART event.
if (xQueueReceive(uart_queue, (void *)&event, portMAX_DELAY)) {
if (event.type == UART_BREAK) {
uart_get_buffered_data_len(EMSUART_NUM, &length);
if (length == 2 || (length >= 6 && length <= EMS_MAXBUFFERSIZE)) {
uart_read_bytes(EMSUART_NUM, telegram, length, portMAX_DELAY);
if (telegram[0] && !telegram[length - 1]) {
EMSESP::incoming_telegram(telegram, (uint8_t)(length - 1));
}
} else {
uart_flush_input(EMSUART_NUM);
}
}
}
}
vTaskDelete(NULL);
}
/*
* init UART driver
*/
void EMSuart::start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t tx_gpio) {
if (!uart_is_driver_installed(EMSUART_NUM)) {
uart_config_t uart_config = {
.baud_rate = EMSUART_BAUD,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB,
};
uart_driver_install(EMSUART_NUM, 129, 0, EMS_MAXBUFFERSIZE * 3, &uart_queue, 0); // buffer must be > fifo
uart_param_config(EMSUART_NUM, &uart_config);
uart_set_pin(EMSUART_NUM, tx_gpio, rx_gpio, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_set_rx_full_threshold(EMSUART_NUM, 1);
xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, configMAX_PRIORITIES - 1, NULL);
}
tx_mode_ = tx_mode;
uart_enable_intr_mask(EMSUART_NUM, UART_BRK_DET_INT_ENA | UART_RXFIFO_FULL_INT_ENA);
}
/*
* Stop, disable interrupt
*/
void EMSuart::stop() {
uart_disable_intr_mask(EMSUART_NUM, UART_BRK_DET_INT_ENA | UART_RXFIFO_FULL_INT_ENA);
};
/*
* Sends a 1-byte poll, ending with a <BRK>
*/
void EMSuart::send_poll(const uint8_t data) {
transmit(&data, 1);
}
/*
* Send data to Tx line, ending with a <BRK>
* buf contains the CRC and len is #bytes including the CRC
* returns code, 1=success
*/
uint16_t EMSuart::transmit(const uint8_t * buf, const uint8_t len) {
if (len == 0 || len >= EMS_MAXBUFFERSIZE) {
return EMS_TX_STATUS_ERR;
}
if (tx_mode_ == 0) {
return EMS_TX_STATUS_OK;
}
if (tx_mode_ == EMS_TXMODE_HW) { // hardware controlled mode
uart_write_bytes_with_break(EMSUART_NUM, buf, len, 10);
return EMS_TX_STATUS_OK;
}
if (tx_mode_ == EMS_TXMODE_EMSPLUS) { // EMS+ with long delay
for (uint8_t i = 0; i < len; i++) {
uart_write_bytes(EMSUART_NUM, &buf[i], 1);
delayMicroseconds(EMSUART_TX_WAIT_PLUS);
}
uart_set_line_inverse(EMSUART_NUM, UART_SIGNAL_TXD_INV);
delayMicroseconds(EMSUART_TX_BRK_PLUS);
uart_set_line_inverse(EMSUART_NUM, 0);
return EMS_TX_STATUS_OK;
}
if (tx_mode_ == EMS_TXMODE_HT3) { // HT3 with 7 bittimes delay
for (uint8_t i = 0; i < len; i++) {
uart_write_bytes(EMSUART_NUM, &buf[i], 1);
delayMicroseconds(EMSUART_TX_WAIT_HT3);
}
uart_set_line_inverse(EMSUART_NUM, UART_SIGNAL_TXD_INV);
delayMicroseconds(EMSUART_TX_BRK_HT3);
uart_set_line_inverse(EMSUART_NUM, 0);
return EMS_TX_STATUS_OK;
}
// mode 1: wait for echo after each byte
for (uint8_t i = 0; i < len; i++) {
size_t rx0, rx1;
uart_get_buffered_data_len(EMSUART_NUM, &rx0);
uart_write_bytes(EMSUART_NUM, &buf[i], 1);
uint16_t timeoutcnt = EMSUART_TX_TIMEOUT;
do {
delayMicroseconds(EMSUART_TX_BUSY_WAIT); // burn CPU cycles...
uart_get_buffered_data_len(EMSUART_NUM, &rx1);
} while ((rx1 == rx0) && (--timeoutcnt));
}
uart_set_line_inverse(EMSUART_NUM, UART_SIGNAL_TXD_INV);
delayMicroseconds(EMSUART_TX_BRK_EMS);
uart_set_line_inverse(EMSUART_NUM, 0);
return EMS_TX_STATUS_OK;
}
} // namespace emsesp