From 55cb2009d9a9f71a606ff4b6c871e854dce7bb9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Sun, 17 Jan 2016 15:03:10 +0100 Subject: [PATCH] example now echos back UART communication --- lib/usart.c | 7 +++---- lib/usart.h | 2 +- main.c | 20 +++++++++----------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/usart.c b/lib/usart.c index f1fb42d..4c862a8 100644 --- a/lib/usart.c +++ b/lib/usart.c @@ -13,9 +13,7 @@ * */ /* Copyright (c) 2016 King Kévin */ -/* this library handles the USART - * it uses the TX and RX pins (depending on which USART is used) - */ +/* this library handles the USART */ /* standard libraries */ #include // standard integer types @@ -55,7 +53,8 @@ void usart_setup(void) usart_set_parity(USART, USART_PARITY_NONE); usart_set_flow_control(USART, USART_FLOWCONTROL_NONE); - nvic_enable_irq(USART_IRQ); // enable the USART2 interrupt + nvic_enable_irq(USART_IRQ); // enable the USART interrupt + usart_enable_rx_interrupt(USART); // enable receive interrupt usart_enable(USART); // enable USART /* reset buffer states */ diff --git a/lib/usart.h b/lib/usart.h index 22933a4..0fd167e 100644 --- a/lib/usart.h +++ b/lib/usart.h @@ -13,7 +13,7 @@ * */ /* Copyright (c) 2016 King Kévin */ -/* This library handles the USART */ +/* this library handles the USART */ /* which USART to use */ #define USART USART1 diff --git a/main.c b/main.c index 45da4fe..eeaa246 100644 --- a/main.c +++ b/main.c @@ -61,24 +61,22 @@ static void gpio_setup(void) } int main(void) -{ - int i, c = 0; - +{ SCB_VTOR = (uint32_t) 0x08002000; // relocate vector table because of the bootloader - clock_setup(); - gpio_setup(); - usart_setup(); + clock_setup(); // setup main clock + gpio_setup(); // setup main inputs/ouputs + usart_setup(); // setup USART (for printing) setbuf(stdout, NULL); // set standard out buffer to NULL to immediately print setbuf(stderr, NULL); // set standard error buffer to NULL to immediately print /* blink the LED with every transmitted character */ while (1) { - gpio_toggle(LED_PORT, LED_PIN); /* LED on/off */ - printf("%c",c+'0'); - c = (c == 9) ? 0 : c + 1; /* increment c */ - for (i = 0; i < 8000000; i++) /* wait a bit */ - __asm__("nop"); + while (usart_received) { // echo every received character + gpio_toggle(LED_PORT, LED_PIN); // toggle LED + printf("%c",usart_getchar()); // transmit receive character + } + __asm__("wfi"); // go to sleep } return 0;