8000 Merge branch 'uart-thread-safe' · a2retro/arduino-esp32@cb2110c · GitHub
[go: up one dir, main page]

Skip to content

Commit cb2110c

Browse files
committed
Merge branch 'uart-thread-safe'
* uart-thread-safe: do not lock for ets_printf Implement thread-safe uart
2 parents 0ccde6c + 6327bbd commit cb2110c

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

cores/esp32/esp32-hal-uart.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,18 @@ static int s_uart_debug_nr = 0;
4040

4141
struct uart_struct_t {
4242
uart_dev_t * dev;
43+
xSemaphoreHandle lock;
4344
uint8_t num;
4445
xQueueHandle queue;
4546
};
4647

48+
#define UART_MUTEX_LOCK() do {} while (xSemaphoreTake(uart->lock, portMAX_DELAY) != pdPASS)
49+
#define UART_MUTEX_UNLOCK() xSemaphoreGive(uart->lock)
50+
4751
static uart_t _uart_bus_array[3] = {
48-
{(volatile uart_dev_t *)(DR_REG_UART_BASE), 0, NULL},
49-
{(volatile uart_dev_t *)(DR_REG_UART1_BASE), 1, NULL},
50-
{(volatile uart_dev_t *)(DR_REG_UART2_BASE), 2, NULL}
52+
{(volatile uart_dev_t *)(DR_REG_UART_BASE), NULL, 0, NULL},
53+
{(volatile uart_dev_t *)(DR_REG_UART1_BASE), NULL, 1, NULL},
54+
{(volatile uart_dev_t *)(DR_REG_UART2_BASE), NULL, 2, NULL}
5155
};
5256

5357
static void IRAM_ATTR _uart_isr(void *arg)
@@ -88,6 +92,7 @@ void uartDisableGlobalInterrupt()
8892

8993
void uartEnableInterrupt(uart_t* uart)
9094
{
95+
UART_MUTEX_LOCK();
9196
uart->dev->conf1.rxfifo_full_thrhd = 112;
9297
uart->dev->conf1.rx_tout_thrhd = 2;
9398
uart->dev->conf1.rx_tout_en = 1;
@@ -97,13 +102,16 @@ void uartEnableInterrupt(uart_t* uart)
97102
uart->dev->int_clr.val = 0xffffffff;
98103

99104
intr_matrix_set(xPortGetCoreID(), UART_INTR_SOURCE(uart->num), ETS_UART_INUM);
105+
UART_MUTEX_UNLOCK();
100106
}
101107

102108
void uartDisableInterrupt(uart_t* uart)
103109
{
110+
UART_MUTEX_LOCK();
104111
uart->dev->conf1.val = 0;
105112
uart->dev->int_ena.val = 0;
106113
uart->dev->int_clr.val = 0xffffffff;
114+
UART_MUTEX_UNLOCK();
107115
}
108116

109117
void uartDetachRx(uart_t* uart)
@@ -154,7 +162,14 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
154162
}
155163

156164
uart_t* uart = &_uart_bus_array[uart_nr];
157-
165+
166+
if(uart->lock == NULL) {
167+
uart->lock = xSemaphoreCreateMutex();
168+
if(uart->lock == NULL) {
169+
return NULL;
170+
}
171+
}
172+
158173
if(queueLen && uart->queue == NULL) {
159174
uart->queue = xQueueCreate(queueLen, sizeof(uint8_t)); //initialize the queue
160175
if(uart->queue == NULL) {
@@ -164,7 +179,9 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
164179

165180
uartFlush(uart);
166181
uartSetBaudRate(uart, baudrate);
182+
UART_MUTEX_LOCK();
167183
uart->dev->conf0.val = config;
184+
UART_MUTEX_UNLOCK();
168185

169186
if(rxPin != -1) {
170187
uartAttachRx(uart, rxPin, inverted);
@@ -183,6 +200,7 @@ void uartEnd(uart_t* uart)
183200
return;
184201
}
185202

203+
UART_MUTEX_LOCK();
186204
if(uart->queue != NULL) {
187205
vQueueDelete(uart->queue);
188206
}
@@ -191,6 +209,7 @@ void uartEnd(uart_t* uart)
191209
uartDetachTx(uart);
192210

193211
uart->dev->conf0.val = 0;
212+
UART_MUTEX_UNLOCK();
194213
}
195214

196215
uint32_t uartAvailable(uart_t* uart)
@@ -230,21 +249,25 @@ void uartWrite(uart_t* uart, uint8_t c)
230249
if(uart == NULL) {
231250
return;
232251
}
252+
UART_MUTEX_LOCK();
233253
while(uart->dev->status.rxfifo_cnt == 0x7F);
234254
uart->dev->fifo.rw_byte = c;
255+
UART_MUTEX_UNLOCK();
235256
}
236257

237258
void uartWriteBuf(uart_t* uart, const uint8_t * data, size_t len)
238259
{
239260
if(uart == NULL) {
240261
return;
241262
}
263+
UART_MUTEX_LOCK();
242264
while(len) {
243265
while(len && uart->dev->status.rxfifo_cnt < 0x7F) {
244266
uart->dev->fifo.rw_byte = *data++;
245267
len--;
246268
}
247269
}
270+
UART_MUTEX_UNLOCK();
248271
}
249272

250273
void uartFlush(uart_t* uart)
@@ -253,23 +276,27 @@ void uartFlush(uart_t* uart)
253276
return;
254277
}
255278

279+
UART_MUTEX_LOCK();
256280
while(uart->dev->status.txfifo_cnt);
257281

258282
uart->dev->conf0.txfifo_rst = 1;
259283
uart->dev->conf0.txfifo_rst = 0;
260284

261285
uart->dev->conf0.rxfifo_rst = 1;
262286
uart->dev->conf0.rxfifo_rst = 0;
287+
UART_MUTEX_UNLOCK();
263288
}
264289

265290
void uartSetBaudRate(uart_t* uart, uint32_t baud_rate)
266291
{
267292
if(uart == NULL) {
268293
return;
269294
}
295+
UART_MUTEX_LOCK();
270296
uint32_t clk_div = ((UART_CLK_FREQ<<4)/baud_rate);
271297
uart->dev->clk_div.div_int = clk_div>>4 ;
272298
uart->dev->clk_div.div_frag = clk_div & 0xf;
299+
UART_MUTEX_UNLOCK();
273300
}
274301

275302
uint32_t uartGetBaudRate(uart_t* uart)

0 commit comments

Comments
 (0)
0