Skip to content

Commit 35962ea

Browse files
committed
esp8266/modpybuart: allow setting baudrate and other params
1 parent d1b7ba5 commit 35962ea

3 files changed

Lines changed: 98 additions & 10 deletions

File tree

esp8266/modpybuart.c

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,39 @@
3636
#include "py/mperrno.h"
3737
#include "modpyb.h"
3838

39-
// baudrate is currently fixed to this value
40-
#define UART_BAUDRATE (115200)
39+
// UartDev is defined and initialized in rom code.
40+
extern UartDevice UartDev;
4141

4242
typedef struct _pyb_uart_obj_t {
4343
mp_obj_base_t base;
4444
uint8_t uart_id;
45+
uint8_t bits;
46+
uint8_t parity;
47+
uint8_t stop;
48+
uint32_t baudrate;
4549
uint16_t timeout; // timeout waiting for first char (in ms)
4650
uint16_t timeout_char; // timeout waiting between chars (in ms)
4751
} pyb_uart_obj_t;
4852

53+
STATIC const char *_parity_name[] = {"None", "1", "0"};
54+
4955
/******************************************************************************/
5056
// MicroPython bindings for UART
5157

5258
STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
5359
pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
54-
mp_printf(print, "UART(%u, baudrate=%u, timeout=%u, timeout_char=%u)",
55-
self->uart_id, UART_BAUDRATE, self->timeout, self->timeout_char);
60+
mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, timeout=%u, timeout_char=%u)",
61+
self->uart_id, self->baudrate, self->bits, _parity_name[self->parity],
62+
self->stop, self->timeout, self->timeout_char);
5663
}
5764

5865
STATIC void pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
59-
enum { ARG_timeout, ARG_timeout_char };
66+
enum { ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_timeout, ARG_timeout_char };
6067
static const mp_arg_t allowed_args[] = {
61-
//{ MP_QSTR_baudrate, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 9600} },
62-
//{ MP_QSTR_bits, MP_ARG_INT, {.u_int = 8} },
63-
//{ MP_QSTR_parity, MP_ARG_OBJ, {.u_obj = mp_const_none} },
64-
//{ MP_QSTR_stop, MP_ARG_INT, {.u_int = 1} },
68+
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 0} },
69+
{ MP_QSTR_bits, MP_ARG_INT, {.u_int = 0} },
70+
{ MP_QSTR_parity, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
71+
{ MP_QSTR_stop, MP_ARG_INT, {.u_int = 0} },
6572
//{ MP_QSTR_tx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
6673
//{ MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
6774
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
@@ -70,16 +77,84 @@ STATIC void pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const mp_o
7077
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
7178
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
7279

80+
// set baudrate
81+
if (args[ARG_baudrate].u_int > 0) {
82+
self->baudrate = args[ARG_baudrate].u_int;
83+
UartDev.baut_rate = self->baudrate; // Sic!
84+
}
85+
86+
// set data bits
87+
switch (args[ARG_bits].u_int) {
88+
case 0:
89+
break;
90+
case 5:
91+
UartDev.data_bits = UART_FIVE_BITS;
92+
self->bits = 5;
93+
break;
94+
case 6:
95+
UartDev.data_bits = UART_SIX_BITS;
96+
self->bits = 6;
97+
break;
98+
case 7:
99+
UartDev.data_bits = UART_SEVEN_BITS;
100+
self->bits = 7;
101+
break;
102+
case 8:
103+
UartDev.data_bits = UART_EIGHT_BITS;
104+
self->bits = 8;
105+
break;
106+
default:
107+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid data bits"));
108+
break;
109+
}
110+
111+
// set parity
112+
if (args[ARG_parity].u_obj != MP_OBJ_NULL) {
113+
if (args[ARG_parity].u_obj == mp_const_none) {
114+
UartDev.parity = UART_NONE_BITS;
115+
self->parity = 0;
116+
} else {
117+
mp_int_t parity = mp_obj_get_int(args[ARG_parity].u_obj);
118+
if (parity & 1) {
119+
UartDev.parity = UART_ODD_BITS;
120+
self->parity = 1;
121+
} else {
122+
UartDev.parity = UART_EVEN_BITS;
123+
self->parity = 2;
124+
}
125+
}
126+
}
127+
128+
// set stop bits
129+
switch (args[ARG_stop].u_int) {
130+
case 0:
131+
break;
132+
case 1:
133+
UartDev.stop_bits = UART_ONE_STOP_BIT;
134+
self->stop = 1;
135+
break;
136+
case 2:
137+
UartDev.stop_bits = UART_TWO_STOP_BIT;
138+
self->stop = 2;
139+
break;
140+
default:
141+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid stop bits"));
142+
break;
143+
}
144+
73145
// set timeout
74146
self->timeout = args[ARG_timeout].u_int;
75147

76148
// set timeout_char
77149
// make sure it is at least as long as a whole character (13 bits to be safe)
78150
self->timeout_char = args[ARG_timeout_char].u_int;
79-
uint32_t min_timeout_char = 13000 / UART_BAUDRATE + 1;
151+
uint32_t min_timeout_char = 13000 / self->baudrate + 1;
80152
if (self->timeout_char < min_timeout_char) {
81153
self->timeout_char = min_timeout_char;
82154
}
155+
156+
// setup
157+
uart_setup(self->uart_id);
83158
}
84159

85160
STATIC mp_obj_t pyb_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
@@ -95,6 +170,12 @@ STATIC mp_obj_t pyb_uart_make_new(const mp_obj_type_t *type, size_t n_args, size
95170
pyb_uart_obj_t *self = m_new_obj(pyb_uart_obj_t);
96171
self->base.type = &pyb_uart_type;
97172
self->uart_id = uart_id;
173+
self->baudrate = 115200;
174+
self->bits = 8;
175+
self->parity = 0;
176+
self->stop = 1;
177+
self->timeout = 0;
178+
self->timeout_char = 0;
98179

99180
// init the peripheral
100181
mp_map_t kw_args;

esp8266/uart.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ void ICACHE_FLASH_ATTR uart_reattach() {
237237
uart_init(UART_BIT_RATE_74880, UART_BIT_RATE_74880);
238238
}
239239

240+
void ICACHE_FLASH_ATTR uart_setup(uint8 uart) {
241+
ETS_UART_INTR_DISABLE();
242+
uart_config(uart);
243+
ETS_UART_INTR_ENABLE();
244+
}
245+
240246
// Task-based UART interface
241247

242248
#include "py/obj.h"

esp8266/uart.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,6 @@ int uart_rx_char(void);
9696
void uart_tx_one_char(uint8 uart, uint8 TxChar);
9797
void uart_flush(uint8 uart);
9898
void uart_os_config(int uart);
99+
void uart_setup(uint8 uart);
99100

100101
#endif // _INCLUDED_UART_H_

0 commit comments

Comments
 (0)