Skip to content
Snippets Groups Projects
Commit 481d714b authored by Damien George's avatar Damien George
Browse files

stmhal: Overhaul UART class to use read/write, and improve it.

UART object now uses a stream-like interface: read, readall, readline,
readinto, readchar, write, writechar.

Timeouts are configured when the UART object is initialised, using
timeout and timeout_char keyword args.

The object includes optional read buffering, using interrupts.  You can set
the buffer size dynamically using read_buf_len keyword arg.  A size of 0
disables buffering.
parent 20f59e18
No related branches found
No related tags found
No related merge requests found
......@@ -320,6 +320,7 @@ soft_reset:
pin_init0();
extint_init0();
timer_init0();
uart_init0();
#if MICROPY_HW_ENABLE_RNG
rng_init0();
......@@ -543,6 +544,7 @@ soft_reset:
printf("PYB: soft reboot\n");
timer_deinit();
uart_deinit();
first_soft_reset = false;
goto soft_reset;
......
......@@ -143,11 +143,17 @@ Q(baudrate)
Q(bits)
Q(stop)
Q(parity)
Q(read_buf_len)
Q(buf)
Q(len)
Q(timeout)
Q(timeout_char)
Q(init)
Q(deinit)
Q(all)
Q(send)
Q(recv)
Q(any)
Q(writechar)
Q(readchar)
Q(readinto)
// for CAN class
Q(CAN)
......
......@@ -76,6 +76,7 @@
#include "obj.h"
#include "extint.h"
#include "timer.h"
#include "uart.h"
#include "storage.h"
extern void __fatal_error(const char*);
......@@ -395,3 +396,24 @@ void TIM8_UP_TIM13_IRQHandler(void) {
void TIM8_TRG_COM_TIM14_IRQHandler(void) {
timer_irq_handler(14);
}
// UART/USART IRQ handlers
void USART1_IRQHandler(void) {
uart_irq_handler(1);
}
void USART2_IRQHandler(void) {
uart_irq_handler(2);
}
void USART3_IRQHandler(void) {
uart_irq_handler(3);
}
void UART4_IRQHandler(void) {
uart_irq_handler(4);
}
void USART6_IRQHandler(void) {
uart_irq_handler(6);
}
This diff is collapsed.
......@@ -45,9 +45,11 @@ typedef enum {
typedef struct _pyb_uart_obj_t pyb_uart_obj_t;
extern const mp_obj_type_t pyb_uart_type;
bool uart_init(pyb_uart_obj_t *uart_obj, uint32_t baudrate);
void uart_init0(void);
void uart_deinit(void);
void uart_irq_handler(mp_uint_t uart_id);
bool uart_rx_any(pyb_uart_obj_t *uart_obj);
int uart_rx_char(pyb_uart_obj_t *uart_obj);
void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len);
void uart_tx_strn_cooked(pyb_uart_obj_t *uart_obj, const char *str, uint len);
......@@ -177,35 +177,6 @@ bool uart_init(pyb_uart_obj_t *uart_obj, uint32_t baudrate) {
return uart_init2(uart_obj);
}
void uart_deinit(pyb_uart_obj_t *uart_obj) {
#if 0
uart_obj->is_enabled = false;
UART_HandleTypeDef *uart = &uart_obj->uart;
HAL_UART_DeInit(uart);
if (uart->Instance == USART1) {
__USART1_FORCE_RESET();
__USART1_RELEASE_RESET();
__USART1_CLK_DISABLE();
} else if (uart->Instance == USART2) {
__USART2_FORCE_RESET();
__USART2_RELEASE_RESET();
__USART2_CLK_DISABLE();
} else if (uart->Instance == USART3) {
__USART3_FORCE_RESET();
__USART3_RELEASE_RESET();
__USART3_CLK_DISABLE();
} else if (uart->Instance == UART4) {
__UART4_FORCE_RESET();
__UART4_RELEASE_RESET();
__UART4_CLK_DISABLE();
} else if (uart->Instance == USART6) {
__USART6_FORCE_RESET();
__USART6_RELEASE_RESET();
__USART6_CLK_DISABLE();
}
#endif
}
bool uart_rx_any(pyb_uart_obj_t *uart_obj) {
#if 0
return __HAL_UART_GET_FLAG(&uart_obj->uart, UART_FLAG_RXNE);
......@@ -390,8 +361,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_uart_init_obj, 1, pyb_uart_init);
/// \method deinit()
/// Turn off the UART bus.
STATIC mp_obj_t pyb_uart_deinit(mp_obj_t self_in) {
pyb_uart_obj_t *self = self_in;
uart_deinit(self);
//pyb_uart_obj_t *self = self_in;
// TODO
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_uart_deinit_obj, pyb_uart_deinit);
......
......@@ -2,11 +2,13 @@ from pyb import UART
uart = UART(1)
uart = UART(1, 9600)
uart = UART(1, 9600, bits=8, stop=1, parity=None)
uart = UART(1, 9600, bits=8, parity=None, stop=1)
print(uart)
uart.init(1200)
print(uart)
uart.any()
uart.send(1, timeout=500)
print(uart.any())
print(uart.write('123'))
print(uart.write(b'abcd'))
print(uart.writechar(1))
UART(1, baudrate=9600, bits=8, stop=1, parity=None)
UART(1, baudrate=1200, bits=8, stop=1, parity=None)
UART(1, baudrate=9600, bits=8, parity=None, stop=1, timeout=1000, timeout_char=0, read_buf_len=64)
UART(1, baudrate=1200, bits=8, parity=None, stop=1, timeout=1000, timeout_char=0, read_buf_len=64)
False
3
4
None
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment