summaryrefslogtreecommitdiffstatshomepage
path: root/cc3200/mods/pybuart.c
diff options
context:
space:
mode:
Diffstat (limited to 'cc3200/mods/pybuart.c')
-rw-r--r--cc3200/mods/pybuart.c193
1 files changed, 94 insertions, 99 deletions
diff --git a/cc3200/mods/pybuart.c b/cc3200/mods/pybuart.c
index 8d054bb088..ccaf3a31e0 100644
--- a/cc3200/mods/pybuart.c
+++ b/cc3200/mods/pybuart.c
@@ -93,6 +93,8 @@
/******************************************************************************
DECLARE PRIVATE FUNCTIONS
******************************************************************************/
+STATIC void uart_init (pyb_uart_obj_t *self);
+STATIC bool uart_rx_wait (pyb_uart_obj_t *self, uint32_t timeout);
STATIC pyb_uart_obj_t* pyb_uart_add (pyb_uart_id_t uart_id);
STATIC pyb_uart_obj_t* pyb_uart_find (pyb_uart_id_t uart_id);
STATIC void UARTGenericIntHandler(uint32_t uart_id);
@@ -137,8 +139,57 @@ void uart_deinit(void) {
}
}
+bool uart_rx_any(pyb_uart_obj_t *self) {
+ return (self->read_buf_tail != self->read_buf_head || MAP_UARTCharsAvail(self->reg));
+}
+
+int uart_rx_char(pyb_uart_obj_t *self) {
+ if (self->read_buf_tail != self->read_buf_head) {
+ // buffering via IRQ
+ int data = self->read_buf[self->read_buf_tail];
+ self->read_buf_tail = (self->read_buf_tail + 1) % self->read_buf_len;
+ return data;
+ } else {
+ // no buffering
+ return MAP_UARTCharGetNonBlocking(self->reg);
+ }
+}
+
+bool uart_tx_char(pyb_uart_obj_t *self, int c) {
+ uint32_t timeout = 0;
+
+ while (!MAP_UARTCharPutNonBlocking(self->reg, c)) {
+ if (timeout++ > (PYBUART_TX_MAX_TIMEOUT_MS / PYBUART_TX_WAIT_MS)) {
+ return false;
+ }
+ HAL_Delay (PYBUART_TX_WAIT_MS);
+ }
+ return true;
+}
+
+bool uart_tx_strn(pyb_uart_obj_t *self, const char *str, uint len) {
+ for (const char *top = str + len; str < top; str++) {
+ if (!uart_tx_char(self, *str)) {
+ return false;
+ }
+ }
+ return true;
+}
+
+void uart_tx_strn_cooked(pyb_uart_obj_t *self, const char *str, uint len) {
+ for (const char *top = str + len; str < top; str++) {
+ if (*str == '\n') {
+ uart_tx_char(self, '\r');
+ }
+ uart_tx_char(self, *str);
+ }
+}
+
+/******************************************************************************
+ DEFINE PRIVATE FUNCTIONS
+ ******************************************************************************/
// assumes init parameters have been set up correctly
-bool uart_init2(pyb_uart_obj_t *self) {
+STATIC void uart_init (pyb_uart_obj_t *self) {
uint uartPerh;
switch (self->uart_id) {
@@ -155,7 +206,7 @@ bool uart_init2(pyb_uart_obj_t *self) {
MAP_IntPrioritySet(INT_UARTA1, INT_PRIORITY_LVL_3);
break;
default:
- return false;
+ return;
}
// Enable the peripheral clock
@@ -173,35 +224,23 @@ bool uart_init2(pyb_uart_obj_t *self) {
// Configure the FIFO interrupt levels
MAP_UARTFIFOLevelSet(self->reg, UART_FIFO_TX4_8, UART_FIFO_RX4_8);
-
+
// Configure the flow control mode
UARTFlowControlSet(self->reg, self->flowcontrol);
- // Enable the RX and RX timeout interrupts
- MAP_UARTIntEnable(self->reg, UART_INT_RX | UART_INT_RT);
-
- self->enabled = true;
-
- // register it with the sleep module
- pybsleep_add (self, (WakeUpCB_t)uart_init2);
- return true;
-}
-
-bool uart_init(pyb_uart_obj_t *self, uint baudrate) {
- self->baudrate = baudrate;
- self->config = UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE;
- self->flowcontrol = UART_FLOWCONTROL_NONE;
- return uart_init2(self);
-}
-
-bool uart_rx_any(pyb_uart_obj_t *self) {
- return (self->read_buf_tail != self->read_buf_head || MAP_UARTCharsAvail(self->reg));
+ // Setup the RX interrupts
+ if (self->read_buf != NULL) {
+ MAP_UARTIntEnable(self->reg, UART_INT_RX | UART_INT_RT);
+ }
+ else {
+ MAP_UARTIntDisable(self->reg, UART_INT_RX | UART_INT_RT);
+ }
}
// Waits at most timeout milliseconds for at least 1 char to become ready for
// reading (from buf or for direct reading).
// Returns true if something available, false if not.
-STATIC bool uart_rx_wait(pyb_uart_obj_t *self, uint32_t timeout) {
+STATIC bool uart_rx_wait (pyb_uart_obj_t *self, uint32_t timeout) {
for (;;) {
if (uart_rx_any(self)) {
return true; // have at least 1 char ready for reading
@@ -216,51 +255,6 @@ STATIC bool uart_rx_wait(pyb_uart_obj_t *self, uint32_t timeout) {
}
}
-int uart_rx_char(pyb_uart_obj_t *self) {
- if (self->read_buf_tail != self->read_buf_head) {
- // buffering via IRQ
- int data = self->read_buf[self->read_buf_tail];
- self->read_buf_tail = (self->read_buf_tail + 1) % self->read_buf_len;
- return data;
- } else {
- // no buffering
- return MAP_UARTCharGetNonBlocking(self->reg);
- }
-}
-
-bool uart_tx_char(pyb_uart_obj_t *self, int c) {
- uint32_t timeout = 0;
-
- while (!MAP_UARTCharPutNonBlocking(self->reg, c)) {
- if (timeout++ > (PYBUART_TX_MAX_TIMEOUT_MS / PYBUART_TX_WAIT_MS)) {
- return false;
- }
- HAL_Delay (PYBUART_TX_WAIT_MS);
- }
- return true;
-}
-
-bool uart_tx_strn(pyb_uart_obj_t *self, const char *str, uint len) {
- for (const char *top = str + len; str < top; str++) {
- if (!uart_tx_char(self, *str)) {
- return false;
- }
- }
- return true;
-}
-
-void uart_tx_strn_cooked(pyb_uart_obj_t *self, const char *str, uint len) {
- for (const char *top = str + len; str < top; str++) {
- if (*str == '\n') {
- uart_tx_char(self, '\r');
- }
- uart_tx_char(self, *str);
- }
-}
-
-/******************************************************************************
- DEFINE PRIVATE FUNCTIONS
- ******************************************************************************/
STATIC pyb_uart_obj_t* pyb_uart_add (pyb_uart_id_t uart_id) {
// create a new uart object
pyb_uart_obj_t *self = m_new_obj(pyb_uart_obj_t);
@@ -327,7 +321,7 @@ STATIC void pyb_uart_print(void (*print)(void *env, const char *fmt, ...), void
if (!self->enabled) {
print(env, "<UART%u>", self->uart_id);
} else {
- print(env, "<UART%u baudrate=%u, bits=", self->uart_id, self->baudrate);
+ print(env, "<UART%u, baudrate=%u, bits=", self->uart_id, self->baudrate);
switch (self->config & UART_CONFIG_WLEN_MASK) {
case UART_CONFIG_WLEN_5:
print(env, "5");
@@ -380,15 +374,35 @@ STATIC const mp_arg_t pyb_uart_init_args[] = {
};
STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
- bool success;
-
// parse args
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_uart_init_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(pyb_uart_init_args), pyb_uart_init_args, args);
+ // set timeouts
+ self->timeout = args[5].u_int;
+ self->timeout_char = args[6].u_int;
+
+ // setup the read buffer
+ m_del(byte, self->read_buf, self->read_buf_len);
+ self->read_buf_head = 0;
+ self->read_buf_tail = 0;
+
+ if (args[7].u_int <= 0) {
+ // no read buffer
+ self->read_buf_len = 0;
+ self->read_buf = NULL;
+ }
+ else {
+ // read buffer using interrupts
+ self->read_buf_len = args[7].u_int;
+ self->read_buf = m_new(byte, args[7].u_int);
+ }
+
+ // get the baudrate
+ self->baudrate = args[0].u_int;
+
// set the UART configuration values
if (n_args > 1) {
- self->baudrate = args[0].u_int;
switch (args[1].u_int) {
case 5:
self->config = UART_CONFIG_WLEN_5;
@@ -417,36 +431,17 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con
// Flow control
self->flowcontrol = args[4].u_int;
- success = uart_init2(self);
- } else {
- success = uart_init(self, args[0].u_int);
}
-
- // init UART (if it fails, something weird happened)
- if (!success) {
- nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
+ else {
+ self->config = UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE;
+ self->flowcontrol = UART_FLOWCONTROL_NONE;
}
+ // initialize and enable the uart
+ uart_init (self);
+ self->enabled = true;
+ // register it with the sleep module
+ pybsleep_add ((const mp_obj_t)self, (WakeUpCB_t)uart_init);
- // set timeouts
- self->timeout = args[5].u_int;
- self->timeout_char = args[6].u_int;
-
- // setup the read buffer
- m_del(byte, self->read_buf, self->read_buf_len);
- self->read_buf_head = 0;
- self->read_buf_tail = 0;
-
- if (args[7].u_int <= 0) {
- // no read buffer
- self->read_buf_len = 0;
- self->read_buf = NULL;
- MAP_UARTIntDisable(self->reg, UART_INT_RX | UART_INT_RT);
- } else {
- // read buffer using interrupts
- self->read_buf_len = args[7].u_int;
- self->read_buf = m_new(byte, args[7].u_int);
- }
-
return mp_const_none;
}