example now echos back UART communication

This commit is contained in:
King Kévin 2016-01-17 15:03:10 +01:00
parent f0d2933ff8
commit 55cb2009d9
3 changed files with 13 additions and 16 deletions

View File

@ -13,9 +13,7 @@
*
*/
/* Copyright (c) 2016 King Kévin <kingkevin@cuvoodoo.info> */
/* 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 <stdint.h> // 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 */

View File

@ -13,7 +13,7 @@
*
*/
/* Copyright (c) 2016 King Kévin <kingkevin@cuvoodoo.info> */
/* This library handles the USART */
/* this library handles the USART */
/* which USART to use */
#define USART USART1

20
main.c
View File

@ -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;