Skip to content

Commit d388899

Browse files
committed
Addition of RS485 support
1 parent 84ad3d8 commit d388899

File tree

5 files changed

+56
-13
lines changed

5 files changed

+56
-13
lines changed

ports/mimxrt10xx/common-hal/busio/UART.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ void LPUART_UserCallback(LPUART_Type *base, lpuart_handle_t *handle, status_t st
7373

7474
void common_hal_busio_uart_construct(busio_uart_obj_t *self,
7575
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx,
76-
const mcu_pin_obj_t * rts, const mcu_pin_obj_t * cts, bool rs485_mode,
76+
const mcu_pin_obj_t * rts, const mcu_pin_obj_t * cts,
77+
const mcu_pin_obj_t * rs485_dir, bool rs485_invert,
7778
uint32_t baudrate, uint8_t bits, uart_parity_t parity, uint8_t stop,
7879
mp_float_t timeout, uint16_t receiver_buffer_size) {
7980

@@ -114,7 +115,22 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
114115
mp_raise_RuntimeError(translate("Invalid UART pin selection"));
115116
}
116117

117-
// Now check for RTS/CTS pin(s)
118+
// Filter for sane settings for RS485
119+
if (rs485_dir != mp_const_none) {
120+
if ((rts != mp_const_none) || (cts != mp_const_none)) {
121+
mp_raise_ValueError(translate("Cannot specify RTS or CTS in RS485 mode"));
122+
}
123+
// For IMXRT the RTS pin is used for RS485 direction
124+
rts = rs485_dir;
125+
}
126+
else
127+
{
128+
if (rs485_invert == true) {
129+
mp_raise_ValueError(translate("RS485 inversion specified when not in RS485 mode"));
130+
}
131+
}
132+
133+
// Now check for RTS/CTS (or overloaded RS485 direction) pin(s)
118134
const uint32_t rts_count = sizeof(mcu_uart_rts_list) / sizeof(mcu_periph_obj_t);
119135
const uint32_t cts_count = sizeof(mcu_uart_cts_list) / sizeof(mcu_periph_obj_t);
120136

@@ -163,10 +179,24 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
163179
config.enableTx = self->tx_pin != NULL;
164180
config.enableRx = self->rx_pin != NULL;
165181
config.enableRxRTS = self->rts_pin != NULL;
166-
config.enableTxCTS = self->cts_pin != NULL;
167-
182+
config.enableTxCTS = self->cts_pin != NULL;
183+
if (self->rts_pin != NULL)
184+
claim_pin(self->rts_pin->pin);
185+
if (self->cts_pin != NULL)
186+
claim_pin(self->cts_pin->pin);
187+
168188
LPUART_Init(self->uart, &config, UART_CLOCK_FREQ);
169189

190+
// Before we init, setup RS485 direction pin
191+
// ..unfortunately this isn't done by the driver library
192+
uint32_t modir = (self->uart->MODIR) & ~(LPUART_MODIR_TXRTSPOL_MASK | LPUART_MODIR_TXRTSE_MASK);
193+
if (rs485_dir != mp_const_none) {
194+
modir |= LPUART_MODIR_TXRTSE_MASK;
195+
if ( rs485_invert == true )
196+
modir |= LPUART_MODIR_TXRTSPOL_MASK;
197+
}
198+
self->uart->MODIR = modir;
199+
170200
if (self->tx_pin != NULL)
171201
claim_pin(self->tx_pin->pin);
172202

ports/mimxrt10xx/supervisor/flexspi_nor_flash_ops.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ void flexspi_nor_flash_init(FLEXSPI_Type *base)
324324
config.ahbConfig.enableAHBBufferable = true;
325325
config.ahbConfig.enableReadAddressOpt = true;
326326
config.ahbConfig.enableAHBCachable = true;
327-
config.rxSampleClock = kFLEXSPI_ReadSampleClkLoopbackFromDqsPad;
327+
config.rxSampleClock = kFLEXSPI_ReadSampleClkLoopbackInternally; //kFLEXSPI_ReadSampleClkLoopbackFromDqsPad;
328328
FLEXSPI_Init(base, &config);
329329

330330
/* Configure flash settings according to serial flash feature. */

shared-bindings/busio/UART.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
//|
5454
//| :param ~microcontroller.Pin tx: the pin to transmit with, or ``None`` if this ``UART`` is receive-only.
5555
//| :param ~microcontroller.Pin rx: the pin to receive on, or ``None`` if this ``UART`` is transmit-only.
56+
//| :param ~microcontroller.Pin rts: the pin for rts, or ``None`` if rts not in use.
57+
//| :param ~microcontroller.Pin cts: the pin for cts, or ``None`` if cts not in use.
58+
//| :param ~microcontroller.Pin rs485_dir: the pin for rs485 direction setting, or ``None`` if rs485 not in use.
59+
//| :param bool rs485_invert: set to invert the sense of the rs485_dir pin.
5660
//| :param int baudrate: the transmit and receive speed.
5761
//| :param int bits: the number of bits per byte, 7, 8 or 9.
5862
//| :param Parity parity: the parity used for error checking.
@@ -82,7 +86,8 @@ STATIC mp_obj_t busio_uart_make_new(const mp_obj_type_t *type, size_t n_args, co
8286
// https://github.com/adafruit/circuitpython/issues/1056)
8387
busio_uart_obj_t *self = m_new_ll_obj(busio_uart_obj_t);
8488
self->base.type = &busio_uart_type;
85-
enum { ARG_tx, ARG_rx, ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_timeout, ARG_receiver_buffer_size, ARG_rts, ARG_cts, ARG_rs485};
89+
enum { ARG_tx, ARG_rx, ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_timeout, ARG_receiver_buffer_size,
90+
ARG_rts, ARG_cts, ARG_rs485_dir,ARG_rs485_invert};
8691
static const mp_arg_t allowed_args[] = {
8792
{ MP_QSTR_tx, MP_ARG_REQUIRED | MP_ARG_OBJ },
8893
{ MP_QSTR_rx, MP_ARG_REQUIRED | MP_ARG_OBJ },
@@ -94,7 +99,8 @@ STATIC mp_obj_t busio_uart_make_new(const mp_obj_type_t *type, size_t n_args, co
9499
{ MP_QSTR_receiver_buffer_size, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 64} },
95100
{ MP_QSTR_rts, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
96101
{ MP_QSTR_cts, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
97-
{ MP_QSTR_rs485, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false } },
102+
{ MP_QSTR_rs485_dir, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none } },
103+
{ MP_QSTR_rs485_invert, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false } },
98104
};
99105
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
100106
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -131,9 +137,10 @@ STATIC mp_obj_t busio_uart_make_new(const mp_obj_type_t *type, size_t n_args, co
131137

132138
const mcu_pin_obj_t* cts = MP_OBJ_TO_PTR(args[ARG_cts].u_obj);
133139

134-
bool rs485 = args[ARG_rs485].u_bool;
140+
const mcu_pin_obj_t* rs485_dir = args[ARG_rs485_dir].u_obj;
141+
bool rs485_invert = args[ARG_rs485_invert].u_bool;
135142

136-
common_hal_busio_uart_construct(self, tx, rx, rts, cts, rs485,
143+
common_hal_busio_uart_construct(self, tx, rx, rts, cts, rs485_dir, rs485_invert,
137144
args[ARG_baudrate].u_int, bits, parity, stop, timeout,
138145
args[ARG_receiver_buffer_size].u_int);
139146
return (mp_obj_t)self;

shared-bindings/busio/UART.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ typedef enum {
4141
// Construct an underlying UART object.
4242
extern void common_hal_busio_uart_construct(busio_uart_obj_t *self,
4343
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx,
44-
const mcu_pin_obj_t * rts, const mcu_pin_obj_t * cts, bool rs485_mode,
44+
const mcu_pin_obj_t * rts, const mcu_pin_obj_t * cts,
45+
const mcu_pin_obj_t * rs485_dir, bool rs485_invert,
4546
uint32_t baudrate, uint8_t bits, uart_parity_t parity, uint8_t stop,
4647
mp_float_t timeout, uint16_t receiver_buffer_size);
4748

shared-module/board/__init__.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,17 @@ mp_obj_t common_hal_board_create_uart(void) {
111111
const mcu_pin_obj_t* cts = mp_const_none;
112112
#endif
113113
#ifdef DEFAULT_UART_IS_RS485
114-
const bool rs485 = true;
114+
const mcu_pin_obj_t* rs485_dir = MP_OBJ_TO_PTR(DEFAULT_UART_BUS_RS485DIR);
115+
#ifdef DEFAULT_UART_RS485_INVERT
116+
const bool rs485_invert = true;
117+
#endif
115118
#else
116-
const bool rs485 = false;
119+
const mcu_pin_obj_t* rs485_dir = mp_const_none;
120+
const bool rs485_invert = true;
117121
#endif
118122

119-
common_hal_busio_uart_construct(self, tx, rx, rts, cts, rs485, 9600, 8, PARITY_NONE, 1, 1.0f, 64);
123+
common_hal_busio_uart_construct(self, tx, rx, rts, cts, rs485_dir, rs485_invert,
124+
9600, 8, PARITY_NONE, 1, 1.0f, 64);
120125
MP_STATE_VM(shared_uart_bus) = MP_OBJ_FROM_PTR(self);
121126
return MP_STATE_VM(shared_uart_bus);
122127
}

0 commit comments

Comments
 (0)