3737#include "irq.h"
3838#include "pendsv.h"
3939
40+ #if defined(STM32H7 )
41+ #define MICROPY_PY_MACHINE_UART_INV_ENTRY \
42+ { MP_ROM_QSTR(MP_QSTR_INV_TX), MP_ROM_INT(UART_ADVFEATURE_TXINVERT_INIT) }, \
43+ { MP_ROM_QSTR(MP_QSTR_INV_RX), MP_ROM_INT(UART_ADVFEATURE_RXINVERT_INIT) },
44+ #else
45+ #define MICROPY_PY_MACHINE_UART_INV_ENTRY
46+ #endif
47+
4048#define MICROPY_PY_MACHINE_UART_CLASS_CONSTANTS \
4149 { MP_ROM_QSTR(MP_QSTR_RTS), MP_ROM_INT(UART_HWCONTROL_RTS) }, \
4250 { MP_ROM_QSTR(MP_QSTR_CTS), MP_ROM_INT(UART_HWCONTROL_CTS) }, \
4351 { MP_ROM_QSTR(MP_QSTR_IRQ_RXIDLE), MP_ROM_INT(UART_FLAG_IDLE) }, \
4452 { MP_ROM_QSTR(MP_QSTR_IRQ_RX), MP_ROM_INT(UART_FLAG_RXNE) }, \
53+ MICROPY_PY_MACHINE_UART_INV_ENTRY \
4554
4655static void mp_machine_uart_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
4756 machine_uart_obj_t * self = MP_OBJ_TO_PTR (self_in );
@@ -125,11 +134,27 @@ static void mp_machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_
125134 if (self -> mp_irq_trigger != 0 ) {
126135 mp_printf (print , "; irq=0x%x" , self -> mp_irq_trigger );
127136 }
137+ #if defined(STM32H7 )
138+ mp_print_str (print , ", invert=" );
139+ if (!(cr2 & (USART_CR2_TXINV | USART_CR2_RXINV ))) {
140+ mp_print_str (print , "0" );
141+ } else {
142+ if (cr2 & USART_CR2_TXINV ) {
143+ mp_print_str (print , "INV_TX" );
144+ if (cr2 & USART_CR2_RXINV ) {
145+ mp_print_str (print , "|" );
146+ }
147+ }
148+ if (cr2 & USART_CR2_RXINV ) {
149+ mp_print_str (print , "INV_RX" );
150+ }
151+ }
152+ #endif
128153 mp_print_str (print , ")" );
129154 }
130155}
131156
132- /// \method init(baudrate, bits=8, parity=None, stop=1, *, timeout=1000, timeout_char=0, flow=0, read_buf_len=64)
157+ /// \method init(baudrate, bits=8, parity=None, stop=1, *, timeout=1000, timeout_char=0, flow=0, read_buf_len=64, invert=0 )
133158///
134159/// Initialise the UART bus with the given parameters:
135160///
@@ -141,6 +166,7 @@ static void mp_machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_
141166/// - `timeout_char` is the timeout in milliseconds to wait between characters.
142167/// - `flow` is RTS | CTS where RTS == 256, CTS == 512
143168/// - `read_buf_len` is the character length of the read buffer (0 to disable).
169+ /// - `invert` specifies which lines to invert. INV_TX | INV_RX (0 to disable). --> Only for STM32H7
144170static void mp_machine_uart_init_helper (machine_uart_obj_t * self , size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
145171 static const mp_arg_t allowed_args [] = {
146172 { MP_QSTR_baudrate , MP_ARG_REQUIRED | MP_ARG_INT , {.u_int = 9600 } },
@@ -152,11 +178,17 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
152178 { MP_QSTR_timeout_char , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
153179 { MP_QSTR_rxbuf , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
154180 { MP_QSTR_read_buf_len , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 64 } }, // legacy
181+ #if defined(STM32H7 )
182+ { MP_QSTR_invert , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
183+ #endif
155184 };
156185
157186 // parse args
158187 struct {
159188 mp_arg_val_t baudrate , bits , parity , stop , flow , timeout , timeout_char , rxbuf , read_buf_len ;
189+ #if defined(STM32H7 )
190+ mp_arg_val_t invert ;
191+ #endif
160192 } args ;
161193 mp_arg_parse_all (n_args , pos_args , kw_args ,
162194 MP_ARRAY_SIZE (allowed_args ), allowed_args , (mp_arg_val_t * )& args );
@@ -202,11 +234,18 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
202234 // flow control
203235 uint32_t flow = args .flow .u_int ;
204236
237+ // inverted
238+ #if defined(STM32H7 )
239+ uint32_t invert = args .invert .u_int ;
240+ #else
241+ uint32_t invert = 0 ;
242+ #endif
243+
205244 // Save attach_to_repl setting because uart_init will disable it.
206245 bool attach_to_repl = self -> attached_to_repl ;
207246
208247 // init UART (if it fails, it's because the port doesn't exist)
209- if (!uart_init (self , baudrate , bits , parity , stop , flow )) {
248+ if (!uart_init (self , baudrate , bits , parity , stop , flow , invert )) {
210249 mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("UART(%d) doesn't exist" ), self -> uart_id );
211250 }
212251
0 commit comments