Skip to content

Commit b7a0d1f

Browse files
0haxglmnet
authored andcommitted
Uart improvments (esphome#1024)
* uart: Add support for specifying the number of bits and parity. ESP8266SwSerial doesn't really check parity but just read the parity bit and ignore it when receiving data. Signed-off-by: 0hax <[email protected]> * uart: support begin and end methods. A component may need to reset uart buffer/status by using begin() and end() methods. This is useful for example when a component needs to be sure it is not reading garbage from previously received data over uart. For end() methods with software serial, disabling interrupt is currently impossible because of a bug in esp8266 Core: esp8266/Arduino#6049 Signed-off-by: 0hax <[email protected]> * esphal: add support for detaching an interrupt. That's needed when a component needs to enable/disable interrupt on a gpio. Signed-off-by: 0hax <[email protected]> * uart: rename CONF_NR_BITS to CONF_DATA_BITS_NUMBER. Signed-off-by: 0hax <[email protected]> * uart: use static const uint32_t instead of #define. Signed-off-by: 0hax <[email protected]> * uart: use an enum to handle parity. Signed-off-by: 0hax <[email protected]> * uart: split between esp32 and esp8266. Signed-off-by: 0hax <[email protected]> * uart: check_uart_settings for parity and number of data bits. Signed-off-by: 0hax <[email protected]> * name param data_bits * add new params to test Co-authored-by: Guillermo Ruffino <[email protected]>
1 parent 7a7ccf4 commit b7a0d1f

File tree

8 files changed

+572
-343
lines changed

8 files changed

+572
-343
lines changed

esphome/components/uart/__init__.py

+14
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,25 @@ def validate_rx_pin(value):
2828
return value
2929

3030

31+
UARTParityOptions = uart_ns.enum('UARTParityOptions')
32+
UART_PARITY_OPTIONS = {
33+
'NONE': UARTParityOptions.UART_CONFIG_PARITY_NONE,
34+
'EVEN': UARTParityOptions.UART_CONFIG_PARITY_EVEN,
35+
'ODD': UARTParityOptions.UART_CONFIG_PARITY_ODD,
36+
}
37+
3138
CONF_STOP_BITS = 'stop_bits'
39+
CONF_DATA_BITS = 'data_bits'
40+
CONF_PARITY = 'parity'
41+
3242
CONFIG_SCHEMA = cv.All(cv.Schema({
3343
cv.GenerateID(): cv.declare_id(UARTComponent),
3444
cv.Required(CONF_BAUD_RATE): cv.int_range(min=1),
3545
cv.Optional(CONF_TX_PIN): pins.output_pin,
3646
cv.Optional(CONF_RX_PIN): validate_rx_pin,
3747
cv.Optional(CONF_STOP_BITS, default=1): cv.one_of(1, 2, int=True),
48+
cv.Optional(CONF_DATA_BITS, default=8): cv.int_range(min=5, max=8),
49+
cv.Optional(CONF_PARITY, default="NONE"): cv.enum(UART_PARITY_OPTIONS, upper=True)
3850
}).extend(cv.COMPONENT_SCHEMA), cv.has_at_least_one_key(CONF_TX_PIN, CONF_RX_PIN))
3951

4052

@@ -50,6 +62,8 @@ def to_code(config):
5062
if CONF_RX_PIN in config:
5163
cg.add(var.set_rx_pin(config[CONF_RX_PIN]))
5264
cg.add(var.set_stop_bits(config[CONF_STOP_BITS]))
65+
cg.add(var.set_data_bits(config[CONF_DATA_BITS]))
66+
cg.add(var.set_parity(config[CONF_PARITY]))
5367

5468

5569
# A schema to use for all UART devices, all UART integrations must extend this!

0 commit comments

Comments
 (0)