Components/arch_driver/src/include/driver/uart.h
Go to the documentation of this file.
1 /*
2  uart.h - UART HAL
3 
4  Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
5  This file is part of the esp8266 core for Arduino environment.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 
21 */
22 
23 /*
24  * @author 21/8/2018 mikee47 <mike@sillyhouse.net>
25  *
26  * SerialBuffer class added to manage FIFO for both RX and TX
27  * Buffering is optional; if size is 0 will just use Hardware FIFOS (128 bytes each)
28  * For uart_write_xxx functions, if there is no space in the tx FIFO it will return a short count. It will not wait.
29  * Trivial check routines have been moved into this header as static inlines.
30  * Code is now C++ only; reference to this header has been removed from esp_systemapi.h uart structure should be
31  * treated as opaque and only accessed using the functions defined in this header.
32  * Callback is invoked on transmit completion.
33  *
34  * Note: uart_detach_all() should be called at startup, i.e. from user_init().
35  */
36 
37 #pragma once
38 
39 #include <c_types.h>
40 #include <esp_attr.h>
41 #include <sming_attr.h>
42 
43 #if defined(__cplusplus)
44 extern "C" {
45 #endif
46 
53 #define UART_NO -1
54 
55 #define UART_PIN_DEFAULT (255)
56 #define UART_PIN_NO_CHANGE (-1)
57 
66 };
67 
79 };
80 
89 };
90 
92  UART_PARITY_MASK = 0B00000011,
94  UART_NB_BIT_MASK = 0B00001100,
96  UART_NB_STOP_BIT_MASK = 0B00110000,
98 };
99 
100 static inline constexpr uint8_t SMG_UART_FORMAT(smg_uart_bits_t databits, smg_uart_stop_bits_t stopbits,
101  smg_uart_parity_t parity)
102 {
103  return (databits << UART_NB_BIT_SHIFT) | (stopbits << UART_NB_STOP_BIT_SHIFT) | (parity << UART_PARITY_SHIFT);
104 }
105 
112  struct {
116  };
117  uint8_t val;
118 };
119 
148 };
149 
150 // Status values
162 };
163 
168  UART_TX_ONLY
169 };
170 
171 using uart_options_t = uint8_t;
172 
179 };
180 
181 #define UART_RX_FIFO_SIZE 0x80
182 #define UART_TX_FIFO_SIZE 0x80
183 
184 struct smg_uart_t;
185 
197 typedef void (*smg_uart_callback_t)(smg_uart_t* uart, uint32_t status);
198 
199 /*
200  * Port notifications
201  */
202 
208 
211 
214 
217 
220 };
221 
227 
234 
235 struct SerialBuffer;
236 
237 struct smg_uart_t {
238  uint8_t uart_nr;
239  uint32_t baud_rate;
241  uint8_t options;
242  uint8_t rx_pin;
243  uint8_t tx_pin;
244  uint8_t rx_headroom;
245  uint16_t status;
249  void* param;
250 };
251 
253  uint8_t uart_nr;
254  uint8_t tx_pin;
255  uint8_t rx_pin;
258  uint32_t baudrate;
260  size_t rx_size;
261  size_t tx_size;
262 };
263 
264 // @deprecated Use `smg_uart_init_ex()` instead
265 smg_uart_t* smg_uart_init(uint8_t uart_nr, uint32_t baudrate, smg_uart_format_t format, smg_uart_mode_t mode,
266  uint8_t tx_pin, size_t rx_size, size_t tx_size = 0);
267 
269 
271 
278 
284 typedef struct {
288  uint8_t reserved;
289  uint32_t intr_mask;
290  uint32_t intr_enable;
292 
300 
301 __forceinline int smg_uart_get_nr(smg_uart_t* uart)
302 {
303  return uart ? uart->uart_nr : -1;
304 }
305 
310 smg_uart_t* smg_uart_get_uart(uint8_t uart_nr);
311 
317 void smg_uart_set_callback(smg_uart_t* uart, smg_uart_callback_t callback, void* param);
318 
323 __forceinline void* smg_uart_get_callback_param(smg_uart_t* uart)
324 {
325  return uart ? uart->param : nullptr;
326 }
327 
332 static inline void smg_uart_set_options(smg_uart_t* uart, uart_options_t options)
333 {
334  if(uart)
335  uart->options = options;
336 }
337 
349 uint8_t IRAM_ATTR smg_uart_get_status(smg_uart_t* uart);
350 
352 {
353  return uart ? uart->options : 0;
354 }
355 
356 void smg_uart_swap(smg_uart_t* uart, int tx_pin);
357 bool smg_uart_set_tx(smg_uart_t* uart, int tx_pin);
358 bool smg_uart_set_pins(smg_uart_t* uart, int tx_pin, int rx_pin);
359 
360 __forceinline bool smg_uart_tx_enabled(smg_uart_t* uart)
361 {
362  return uart && uart->mode != UART_RX_ONLY;
363 }
364 
365 __forceinline bool smg_uart_rx_enabled(smg_uart_t* uart)
366 {
367  return uart && uart->mode != UART_TX_ONLY;
368 }
369 
375 uint32_t smg_uart_set_baudrate_reg(int uart_nr, uint32_t baud_rate);
376 
382 uint32_t smg_uart_set_baudrate(smg_uart_t* uart, uint32_t baud_rate);
383 
389 
390 size_t smg_uart_resize_rx_buffer(smg_uart_t* uart, size_t new_size);
392 size_t smg_uart_resize_tx_buffer(smg_uart_t* uart, size_t new_size);
394 
401 size_t smg_uart_write(smg_uart_t* uart, const void* buffer, size_t size);
402 
408 static inline size_t smg_uart_write_char(smg_uart_t* uart, char c)
409 {
410  return smg_uart_write(uart, &c, 1);
411 }
412 
419 size_t smg_uart_read(smg_uart_t* uart, void* buffer, size_t size);
420 
425 static inline int smg_uart_read_char(smg_uart_t* uart)
426 {
427  char c;
428  return smg_uart_read(uart, &c, 1) ? c : -1;
429 }
430 
438 
446 
447 /*
448  * @brief Find a character in the receive buffer
449  * @param uart
450  * @param char c character to search for
451  * @retval size_t position relative to start of buffer, -1 if not found
452  */
453 int smg_uart_rx_find(smg_uart_t* uart, char c);
454 
461 
464 
467 
472 void smg_uart_set_break(smg_uart_t* uart, bool state);
473 
480 
481 void smg_uart_set_debug(int uart_nr);
483 
488 
492 void __forceinline smg_uart_stop_isr(smg_uart_t* uart)
493 {
494  extern void smg_uart_detach(int);
495  if(uart != nullptr) {
496  smg_uart_detach(uart->uart_nr);
497  }
498 }
499 
503 void smg_uart_detach(int uart_nr);
504 
509 
514 
518 
521 // Internal routine
522 bool smg_uart_realloc_buffer(SerialBuffer*& buffer, size_t new_size);
523 
524 #if defined(__cplusplus)
525 } // extern "C"
526 #endif
bool smg_uart_realloc_buffer(SerialBuffer *&buffer, size_t new_size)
#define BIT(nr)
Definition: c_types.h:30
smg_uart_status_t
Definition: Components/arch_driver/src/include/driver/uart.h:151
smg_uart_notify_code_t
Indicates notification, parameters refer to uart_notify_info_t structure.
Definition: Components/arch_driver/src/include/driver/uart.h:205
smg_uart_mode_t
values for mode argument of uart_init
Definition: Components/arch_driver/src/include/driver/uart.h:165
static constexpr uint8_t SMG_UART_FORMAT(smg_uart_bits_t databits, smg_uart_stop_bits_t stopbits, smg_uart_parity_t parity)
Definition: Components/arch_driver/src/include/driver/uart.h:100
uint8_t smg_uart_get_status(smg_uart_t *uart)
Get error flags and clear them.
int smg_uart_peek_char(smg_uart_t *uart)
see what the next character in the rx buffer is
smg_uart_t * smg_uart_get_uart(uint8_t uart_nr)
Get the uart structure for the given number.
bool smg_uart_tx_enabled(smg_uart_t *uart)
Definition: Components/arch_driver/src/include/driver/uart.h:360
void smg_uart_detach(int uart_nr)
detach a UART interrupt service routine
static void smg_uart_set_options(smg_uart_t *uart, uart_options_t options)
Set option flags.
Definition: Components/arch_driver/src/include/driver/uart.h:332
void smg_uart_start_isr(smg_uart_t *uart)
enable interrupts for a UART
bool smg_uart_set_tx(smg_uart_t *uart, int tx_pin)
void smg_uart_set_format(smg_uart_t *uart, smg_uart_format_t format)
Set the UART data format.
uint32_t smg_uart_set_baudrate_reg(int uart_nr, uint32_t baud_rate)
set UART baud rate, given the UART number
void smg_uart_uninit(smg_uart_t *uart)
uint32_t smg_uart_set_baudrate(smg_uart_t *uart, uint32_t baud_rate)
set UART baud rate
void smg_uart_flush(smg_uart_t *uart, smg_uart_mode_t mode=UART_FULL)
discard any buffered data and reset hardware FIFOs
static size_t smg_uart_write_char(smg_uart_t *uart, char c)
queue a single character for output
Definition: Components/arch_driver/src/include/driver/uart.h:408
void smg_uart_set_debug(int uart_nr)
static uart_options_t smg_uart_get_options(smg_uart_t *uart)
Definition: Components/arch_driver/src/include/driver/uart.h:351
void smg_uart_stop_isr(smg_uart_t *uart)
disable interrupts for a UART
Definition: Components/arch_driver/src/include/driver/uart.h:492
void(*)(smg_uart_t *uart, smg_uart_notify_code_t code) smg_uart_notify_callback_t
Port notification callback function type.
Definition: Components/arch_driver/src/include/driver/uart.h:226
size_t smg_uart_tx_buffer_size(smg_uart_t *uart)
smg_uart_stop_bits_t
Number of UART stop bits.
Definition: Components/arch_driver/src/include/driver/uart.h:84
smg_uart_format_settings_t
Definition: Components/arch_driver/src/include/driver/uart.h:91
void smg_uart_wait_tx_empty(smg_uart_t *uart)
smg_uart_option_bits_t
bit values for options argument of uart_init
Definition: Components/arch_driver/src/include/driver/uart.h:176
void(* smg_uart_callback_t)(smg_uart_t *uart, uint32_t status)
callback invoked directly from ISR
Definition: Components/arch_driver/src/include/driver/uart.h:197
size_t smg_uart_resize_rx_buffer(smg_uart_t *uart, size_t new_size)
uint8_t smg_uart_disable_interrupts()
disable interrupts and return current interrupt state
uint32_t smg_uart_get_baudrate(smg_uart_t *uart)
get the actual baud rate in use
bool smg_uart_intr_config(smg_uart_t *uart, const smg_uart_intr_config_t *config)
Configure interrupt thresholds.
smg_uart_parity_t
UART parity setting.
Definition: Components/arch_driver/src/include/driver/uart.h:75
size_t smg_uart_rx_buffer_size(smg_uart_t *uart)
void smg_uart_restore_interrupts()
re-enable interrupts after calling uart_disable_interrupts()
smg_uart_bits_t
Number of UART data bits.
Definition: Components/arch_driver/src/include/driver/uart.h:61
bool smg_uart_set_pins(smg_uart_t *uart, int tx_pin, int rx_pin)
int smg_uart_rx_find(smg_uart_t *uart, char c)
size_t smg_uart_resize_tx_buffer(smg_uart_t *uart, size_t new_size)
void smg_uart_detach_all()
detach all UART interrupt service routines
int smg_uart_get_debug()
static int smg_uart_read_char(smg_uart_t *uart)
read a received character
Definition: Components/arch_driver/src/include/driver/uart.h:425
size_t smg_uart_rx_available(smg_uart_t *uart)
determine available data which can be read
int smg_uart_get_nr(smg_uart_t *uart)
Definition: Components/arch_driver/src/include/driver/uart.h:301
void smg_uart_set_callback(smg_uart_t *uart, smg_uart_callback_t callback, void *param)
Set callback handler for serial port.
bool smg_uart_rx_enabled(smg_uart_t *uart)
Definition: Components/arch_driver/src/include/driver/uart.h:365
int smg_uart_peek_last_char(smg_uart_t *uart)
fetch last character read out of FIFO
size_t smg_uart_read(smg_uart_t *uart, void *buffer, size_t size)
read a block of data
void smg_uart_swap(smg_uart_t *uart, int tx_pin)
smg_uart_t * smg_uart_init(uint8_t uart_nr, uint32_t baudrate, smg_uart_format_t format, smg_uart_mode_t mode, uint8_t tx_pin, size_t rx_size, size_t tx_size=0)
void smg_uart_set_break(smg_uart_t *uart, bool state)
Set or clear a break condition on the TX line.
void * smg_uart_get_callback_param(smg_uart_t *uart)
Get the callback parameter specified by uart_set_callback()
Definition: Components/arch_driver/src/include/driver/uart.h:323
bool smg_uart_set_notify(unsigned uart_nr, smg_uart_notify_callback_t callback)
Set the notification callback function.
smg_uart_format_t
Options for config argument of uart_init.
Definition: Components/arch_driver/src/include/driver/uart.h:123
size_t smg_uart_write(smg_uart_t *uart, const void *buffer, size_t size)
write a block of data
uint8_t uart_options_t
Definition: Components/arch_driver/src/include/driver/uart.h:171
smg_uart_t * smg_uart_init_ex(const smg_uart_config_t &cfg)
size_t smg_uart_tx_free(smg_uart_t *uart)
return free space in transmit buffer
@ UART_STATUS_FRM_ERR
Definition: Components/arch_driver/src/include/driver/uart.h:158
@ UART_STATUS_PARITY_ERR
Definition: Components/arch_driver/src/include/driver/uart.h:159
@ UART_STATUS_BRK_DET
Definition: Components/arch_driver/src/include/driver/uart.h:154
@ UART_STATUS_TXFIFO_EMPTY
Definition: Components/arch_driver/src/include/driver/uart.h:160
@ UART_STATUS_RXFIFO_OVF
Definition: Components/arch_driver/src/include/driver/uart.h:157
@ UART_STATUS_DSR_CHG
Definition: Components/arch_driver/src/include/driver/uart.h:156
@ UART_STATUS_CTS_CHG
Definition: Components/arch_driver/src/include/driver/uart.h:155
@ UART_STATUS_RXFIFO_FULL
Definition: Components/arch_driver/src/include/driver/uart.h:161
@ UART_STATUS_RXFIFO_TOUT
Definition: Components/arch_driver/src/include/driver/uart.h:153
@ UART_STATUS_TX_DONE
All data transmitted (ESP32 only)
Definition: Components/arch_driver/src/include/driver/uart.h:152
@ UART_NOTIFY_BEFORE_READ
Called before data is read from rx buffer.
Definition: Components/arch_driver/src/include/driver/uart.h:216
@ UART_NOTIFY_WAIT_TX
Called to ensure all buffered data is output.
Definition: Components/arch_driver/src/include/driver/uart.h:219
@ UART_NOTIFY_AFTER_WRITE
Called after data has been written into tx buffer.
Definition: Components/arch_driver/src/include/driver/uart.h:213
@ UART_NOTIFY_AFTER_OPEN
Called when uart has been initialised successfully.
Definition: Components/arch_driver/src/include/driver/uart.h:207
@ UART_NOTIFY_BEFORE_CLOSE
Called immediately before uart is closed and destroyed.
Definition: Components/arch_driver/src/include/driver/uart.h:210
@ UART_TX_ONLY
Transmit only.
Definition: Components/arch_driver/src/include/driver/uart.h:168
@ UART_FULL
Both receive and transmit - will revert to TX only if RX not supported.
Definition: Components/arch_driver/src/include/driver/uart.h:166
@ UART_RX_ONLY
Receive only.
Definition: Components/arch_driver/src/include/driver/uart.h:167
@ UART_NB_STOP_BIT_2
Definition: Components/arch_driver/src/include/driver/uart.h:88
@ UART_NB_STOP_BIT_1
Definition: Components/arch_driver/src/include/driver/uart.h:86
@ UART_NB_STOP_BIT_15
Definition: Components/arch_driver/src/include/driver/uart.h:87
@ UART_NB_STOP_BIT_0
Definition: Components/arch_driver/src/include/driver/uart.h:85
@ UART_NB_STOP_BIT_MASK
Definition: Components/arch_driver/src/include/driver/uart.h:96
@ UART_NB_BIT_MASK
Definition: Components/arch_driver/src/include/driver/uart.h:94
@ UART_PARITY_MASK
Definition: Components/arch_driver/src/include/driver/uart.h:92
@ UART_PARITY_SHIFT
Definition: Components/arch_driver/src/include/driver/uart.h:93
@ UART_NB_BIT_SHIFT
Definition: Components/arch_driver/src/include/driver/uart.h:95
@ UART_NB_STOP_BIT_SHIFT
Definition: Components/arch_driver/src/include/driver/uart.h:97
@ UART_OPT_TXWAIT
If buffers are full then uart_write() will wait for free space.
Definition: Components/arch_driver/src/include/driver/uart.h:177
@ UART_OPT_CALLBACK_RAW
ISR invokes user callback function with no pre-processing.
Definition: Components/arch_driver/src/include/driver/uart.h:178
@ UART_PARITY_NONE
Definition: Components/arch_driver/src/include/driver/uart.h:76
@ UART_PARITY_EVEN
Definition: Components/arch_driver/src/include/driver/uart.h:77
@ UART_PARITY_ODD
Definition: Components/arch_driver/src/include/driver/uart.h:78
@ UART_NB_BIT_6
Definition: Components/arch_driver/src/include/driver/uart.h:63
@ UART_NB_BIT_5
Definition: Components/arch_driver/src/include/driver/uart.h:62
@ UART_NB_BIT_7
Definition: Components/arch_driver/src/include/driver/uart.h:64
@ UART_NB_BIT_8
Definition: Components/arch_driver/src/include/driver/uart.h:65
@ UART_8N1
Definition: Components/arch_driver/src/include/driver/uart.h:127
@ UART_7O2
Definition: Components/arch_driver/src/include/driver/uart.h:146
@ UART_6O2
Definition: Components/arch_driver/src/include/driver/uart.h:145
@ UART_6E2
Definition: Components/arch_driver/src/include/driver/uart.h:137
@ UART_5O1
Definition: Components/arch_driver/src/include/driver/uart.h:140
@ UART_8O1
Definition: Components/arch_driver/src/include/driver/uart.h:143
@ UART_5E2
Definition: Components/arch_driver/src/include/driver/uart.h:136
@ UART_6O1
Definition: Components/arch_driver/src/include/driver/uart.h:141
@ UART_6E1
Definition: Components/arch_driver/src/include/driver/uart.h:133
@ UART_7O1
Definition: Components/arch_driver/src/include/driver/uart.h:142
@ UART_5N1
Definition: Components/arch_driver/src/include/driver/uart.h:124
@ UART_8O2
Definition: Components/arch_driver/src/include/driver/uart.h:147
@ UART_5E1
Definition: Components/arch_driver/src/include/driver/uart.h:132
@ UART_7E2
Definition: Components/arch_driver/src/include/driver/uart.h:138
@ UART_5N2
Definition: Components/arch_driver/src/include/driver/uart.h:128
@ UART_7N2
Definition: Components/arch_driver/src/include/driver/uart.h:130
@ UART_7E1
Definition: Components/arch_driver/src/include/driver/uart.h:134
@ UART_8E2
Definition: Components/arch_driver/src/include/driver/uart.h:139
@ UART_6N2
Definition: Components/arch_driver/src/include/driver/uart.h:129
@ UART_5O2
Definition: Components/arch_driver/src/include/driver/uart.h:144
@ UART_7N1
Definition: Components/arch_driver/src/include/driver/uart.h:126
@ UART_6N1
Definition: Components/arch_driver/src/include/driver/uart.h:125
@ UART_8E1
Definition: Components/arch_driver/src/include/driver/uart.h:135
@ UART_8N2
Definition: Components/arch_driver/src/include/driver/uart.h:131
FIFO buffer used for both receive and transmit data.
Definition: SerialBuffer.h:24
Definition: Components/arch_driver/src/include/driver/uart.h:252
size_t rx_size
Definition: Components/arch_driver/src/include/driver/uart.h:260
uint32_t baudrate
Requested baudrate; actual baudrate may differ.
Definition: Components/arch_driver/src/include/driver/uart.h:258
uint8_t uart_nr
Definition: Components/arch_driver/src/include/driver/uart.h:253
size_t tx_size
Definition: Components/arch_driver/src/include/driver/uart.h:261
uint8_t tx_pin
Specify 2 for alternate pin, otherwise defaults to pin 1.
Definition: Components/arch_driver/src/include/driver/uart.h:254
smg_uart_mode_t mode
Whether to enable receive, transmit or both.
Definition: Components/arch_driver/src/include/driver/uart.h:256
smg_uart_format_t format
UART CONF0 register bits.
Definition: Components/arch_driver/src/include/driver/uart.h:259
uint8_t rx_pin
Definition: Components/arch_driver/src/include/driver/uart.h:255
uart_options_t options
Definition: Components/arch_driver/src/include/driver/uart.h:257
UART interrupt configuration parameters for smg_uart_intr_config function.
Definition: Components/arch_driver/src/include/driver/uart.h:284
uint8_t rx_timeout_thresh
Definition: Components/arch_driver/src/include/driver/uart.h:285
uint8_t txfifo_empty_intr_thresh
Definition: Components/arch_driver/src/include/driver/uart.h:286
uint8_t rxfifo_full_thresh
Ignored if additional buffers are allocated.
Definition: Components/arch_driver/src/include/driver/uart.h:287
uint32_t intr_enable
State of interrupt bits.
Definition: Components/arch_driver/src/include/driver/uart.h:290
uint8_t reserved
Definition: Components/arch_driver/src/include/driver/uart.h:288
uint32_t intr_mask
Mask of smg_uart_status_t indicating which interrupt bits to change.
Definition: Components/arch_driver/src/include/driver/uart.h:289
Definition: Components/arch_driver/src/include/driver/uart.h:237
void * param
User-supplied callback parameter.
Definition: Components/arch_driver/src/include/driver/uart.h:249
uint8_t uart_nr
Definition: Components/arch_driver/src/include/driver/uart.h:238
uint8_t rx_pin
Definition: Components/arch_driver/src/include/driver/uart.h:242
uint16_t status
All status flags reported to callback since last uart_get_status() call.
Definition: Components/arch_driver/src/include/driver/uart.h:245
struct SerialBuffer * rx_buffer
Optional receive buffer.
Definition: Components/arch_driver/src/include/driver/uart.h:246
struct SerialBuffer * tx_buffer
Optional transmit buffer.
Definition: Components/arch_driver/src/include/driver/uart.h:247
uint8_t rx_headroom
Callback when rx_buffer free space <= headroom.
Definition: Components/arch_driver/src/include/driver/uart.h:244
uint32_t baud_rate
Definition: Components/arch_driver/src/include/driver/uart.h:239
smg_uart_mode_t mode
Definition: Components/arch_driver/src/include/driver/uart.h:240
uint8_t tx_pin
Definition: Components/arch_driver/src/include/driver/uart.h:243
uint8_t options
Definition: Components/arch_driver/src/include/driver/uart.h:241
smg_uart_callback_t callback
Optional User callback routine.
Definition: Components/arch_driver/src/include/driver/uart.h:248
Structure for easier decomposing of format value.
Definition: Components/arch_driver/src/include/driver/uart.h:111
smg_uart_bits_t bits
Definition: Components/arch_driver/src/include/driver/uart.h:114
smg_uart_stop_bits_t stop_bits
Definition: Components/arch_driver/src/include/driver/uart.h:115
uint8_t val
Definition: Components/arch_driver/src/include/driver/uart.h:117
smg_uart_parity_t parity
Definition: Components/arch_driver/src/include/driver/uart.h:113