From a549503ade8bb29d7234c3d4c61ed6cab433dd0b Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Fri, 20 Oct 2017 21:56:28 +0000 Subject: basic uart: f4 and f3, prepping tests for usart-v2 use parity to at least test a little more of the common code --- tests/uart-basic/uart-basic.c | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tests/uart-basic/uart-basic.c (limited to 'tests/uart-basic/uart-basic.c') diff --git a/tests/uart-basic/uart-basic.c b/tests/uart-basic/uart-basic.c new file mode 100644 index 0000000..4dae0e1 --- /dev/null +++ b/tests/uart-basic/uart-basic.c @@ -0,0 +1,71 @@ +/* + * Karl Palsson Oct 2017 + * Considered to be available under your choice of: + * BSD2 clause, Apache2, MIT, X11 or ISC licenses + */ + +#include +#include +#include +#include +#include +#include +#include "uart-basic.h" + +/* prototype to make linking happy */ +int _write(int file, char *ptr, int len); + +static struct ub_hw *ub; +static uint8_t last_rxb; + +/* Implement _write for newlib to use printf */ +int _write(int file, char *ptr, int len) +{ + int i; + + if (file == STDOUT_FILENO || file == STDERR_FILENO) { + for (i = 0; i < len; i++) { + if (ptr[i] == '\n') { + usart_send_blocking(ub->uart, '\r'); + } + usart_send_blocking(ub->uart, ptr[i]); + } + return i; + } + errno = EIO; + return -1; +} + +void ub_init(struct ub_hw *ub_input) +{ + ub = ub_input; + rcc_periph_clock_enable(ub->uart_rcc); + + usart_set_baudrate(ub->uart, 115200); + usart_set_databits(ub->uart, 9); + usart_set_stopbits(ub->uart, USART_STOPBITS_1); + usart_set_mode(ub->uart, USART_MODE_TX_RX); + usart_set_parity(ub->uart, USART_PARITY_EVEN); + usart_set_flow_control(ub->uart, USART_FLOWCONTROL_NONE); + + usart_enable_rx_interrupt(ub->uart); + nvic_enable_irq(ub->uart_nvic); + usart_enable(ub->uart); +} + +void ub_task(void) +{ + if (last_rxb) { + printf("Last rx char was: <%c>\n", last_rxb); + last_rxb = 0; + } else { + printf("enter a character!\n"); + } +} + +void ub_irq_handler(void) +{ + if (usart_get_flag(ub->uart, USART_SR_RXNE)) { + last_rxb = usart_recv(ub->uart); + } +} -- cgit