@@ -112,10 +112,6 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
112
112
mp_raise_ValueError (translate ("Invalid buffer size" ));
113
113
}
114
114
115
- if ((rs485_dir != NULL ) || (rs485_invert )) {
116
- mp_raise_NotImplementedError (translate ("RS485 Not yet supported on this device" ));
117
- }
118
-
119
115
uint8_t uart_id = ((((tx != NULL ) ? tx -> number : rx -> number ) + 4 ) / 8 ) % NUM_UARTS ;
120
116
121
117
if (uart_status [uart_id ] != STATUS_FREE ) {
@@ -126,6 +122,29 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
126
122
self -> rx_pin = pin_init (uart_id , rx , 1 );
127
123
self -> cts_pin = pin_init (uart_id , cts , 2 );
128
124
self -> rts_pin = pin_init (uart_id , rts , 3 );
125
+
126
+ if (rs485_dir != NULL ) {
127
+ uint8_t pin = rs485_dir -> number ;
128
+ self -> rs485_dir_pin = pin ;
129
+ self -> rs485_invert = rs485_invert ;
130
+
131
+ gpio_init (pin );
132
+
133
+ claim_pin (rs485_dir );
134
+
135
+ gpio_disable_pulls (pin );
136
+
137
+ // Turn on "strong" pin driving (more current available).
138
+ hw_write_masked (& padsbank0_hw -> io [pin ],
139
+ PADS_BANK0_GPIO0_DRIVE_VALUE_12MA << PADS_BANK0_GPIO0_DRIVE_LSB ,
140
+ PADS_BANK0_GPIO0_DRIVE_BITS );
141
+
142
+ gpio_put (self -> rs485_dir_pin , rs485_invert );
143
+ gpio_set_dir (self -> rs485_dir_pin , GPIO_OUT );
144
+ } else {
145
+ self -> rs485_dir_pin = NO_PIN ;
146
+ }
147
+
129
148
uart_status [uart_id ] = STATUS_BUSY ;
130
149
131
150
@@ -179,10 +198,12 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
179
198
reset_pin_number (self -> rx_pin );
180
199
reset_pin_number (self -> cts_pin );
181
200
reset_pin_number (self -> rts_pin );
201
+ reset_pin_number (self -> rs485_dir_pin );
182
202
self -> tx_pin = NO_PIN ;
183
203
self -> rx_pin = NO_PIN ;
184
204
self -> cts_pin = NO_PIN ;
185
205
self -> rts_pin = NO_PIN ;
206
+ self -> rs485_dir_pin = NO_PIN ;
186
207
}
187
208
188
209
// Write characters.
@@ -191,6 +212,11 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
191
212
mp_raise_ValueError (translate ("No TX pin" ));
192
213
}
193
214
215
+ if (self -> rs485_dir_pin != NO_PIN ) {
216
+ uart_tx_wait_blocking (self -> uart );
217
+ gpio_put (self -> rs485_dir_pin , !self -> rs485_invert );
218
+ }
219
+
194
220
size_t left_to_write = len ;
195
221
while (left_to_write > 0 ) {
196
222
while (uart_is_writable (self -> uart ) && left_to_write > 0 ) {
@@ -201,7 +227,10 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
201
227
}
202
228
RUN_BACKGROUND_TASKS ;
203
229
}
204
-
230
+ if (self -> rs485_dir_pin != NO_PIN ) {
231
+ uart_tx_wait_blocking (self -> uart );
232
+ gpio_put (self -> rs485_dir_pin , self -> rs485_invert );
233
+ }
205
234
return len ;
206
235
}
207
236
0 commit comments