From 22461eaff4a82174f4412fff45314647dc9717e0 Mon Sep 17 00:00:00 2001 From: Arti Zirk Date: Mon, 7 Nov 2016 01:42:18 +0200 Subject: [PATCH] Async uart --- src/main.c | 98 ++++++++++++++++++++++++++++++++----------------- src/uart.c | 72 ------------------------------------ src/uart.h | 17 --------- src/uart_wrap.c | 33 +++++++++++++++++ src/uart_wrap.h | 15 ++++++++ 5 files changed, 113 insertions(+), 122 deletions(-) delete mode 100644 src/uart.c delete mode 100644 src/uart.h create mode 100644 src/uart_wrap.c create mode 100644 src/uart_wrap.h diff --git a/src/main.c b/src/main.c index 0840c83..2713fd0 100644 --- a/src/main.c +++ b/src/main.c @@ -1,38 +1,57 @@ #include #include #include +#include #include #include #include "hmi_msg.h" -#include "uart.h" +#include "../lib/andygock_avr-uart/uart.h" +#include "uart_wrap.h" #include "print_helper.h" #include "../lib/hd44780_111/hd44780.h" +#define BAUDRATE 9600 #define BLINK_DELAY_MS 100 -int main (void) +// For configuring arduino mega pin 25 +#define LED_INIT DDRA |= _BV(DDA3); +#define LED_ON PORTA |= _BV(PORTA3) +#define LED_OFF PORTA &= ~_BV(PORTA3) +#define LED_DELAY _delay_ms(BLINK_DELAY_MS) + +static inline void init_hw (void) { - /* Init */ - DDRA |= _BV(DDA3); - uart0_init(); - uart3_init(); + // IO init + /// Set arduino pin 25 as output + LED_INIT; + + // UART init + uart0_init(UART_BAUD_SELECT(BAUDRATE, F_CPU)); + uart3_init(UART_BAUD_SELECT(BAUDRATE, F_CPU)); stdout = stdin = &uart0_io; stderr = &uart3_out; + + // LCD init lcd_init(); lcd_clrscr(); - /* End init */ - /* Print version info to stderr */ + // Enable interupts + sei(); +} + +static inline void start_ui (void) +{ + // Print program and libc versions fprintf_P(stderr, PSTR(PROG_VERSION), PSTR(GIT_DESCR), PSTR(__DATE__), PSTR(__TIME__)); fprintf_P(stderr, PSTR(LIBC_VERSION), PSTR(__AVR_LIBC_VERSION_STRING__)); - /* End version print */ + // print student name fprintf_P(stdout, PSTR(STUD_NAME)); - fputc('\n', stdout); /* Add a new line to the uart printout */ + fputc('\n', stdout); // Add a new line to the uart printout lcd_puts_P(PSTR(STUD_NAME)); - /* ASCII table print */ + // ASCII table print print_ascii_tbl(stdout); unsigned char ascii[128]; for (unsigned char i = 0; i < sizeof(ascii); i++) { @@ -40,30 +59,43 @@ int main (void) } print_for_human(stdout, ascii, sizeof(ascii)); - while (1) { - /* set pin 3 high to turn led on */ - PORTA |= _BV(PORTA3); - _delay_ms(BLINK_DELAY_MS); + fprintf_P(stdout, PSTR(GET_MONTH_MSG)); +} - /* Month search and print */ - char letter; - fprintf_P(stdout, PSTR(GET_MONTH_MSG)); - fscanf(stdin, "%c", &letter); - fprintf(stdout, "%c\n", letter); - lcd_goto(0x40); /* Got to the beginning of the next line */ - for (int i = 0; i < 6; i++) { - if (!strncmp_P(&letter, (PGM_P)pgm_read_word(&months[i]), 1)) { - fprintf_P(stdout, (PGM_P)pgm_read_word(&months[i])); - fputc('\n', stdout); - lcd_puts_P((PGM_P)pgm_read_word(&months[i])); - lcd_putc(' '); - } +static inline void search_month (void) +{ + char letter; + + fscanf(stdin, "%c", &letter); + fprintf(stdout, "%c\n", letter); + lcd_goto(0x40); // Got to the beginning of the next line + for (int i = 0; i < 6; i++) { + if (!strncmp_P(&letter, (PGM_P)pgm_read_word(&months[i]), 1)) { + fprintf_P(stdout, (PGM_P)pgm_read_word(&months[i])); + fputc('\n', stdout); + lcd_puts_P((PGM_P)pgm_read_word(&months[i])); + lcd_putc(' '); + } + } + + lcd_puts_P(PSTR(" ")); // Clear the end of the line + fprintf_P(stdout, PSTR(GET_MONTH_MSG)); +} + +int main (void) +{ + init_hw(); + start_ui(); + + while (1) { + LED_ON; + LED_DELAY; + + if (uart0_available()) { + search_month(); } - lcd_puts_P(PSTR(" ")); /* Clear the end of the line */ - - /* set pin 3 low to turn led off */ - PORTA &= ~_BV(PORTA3); - _delay_ms(BLINK_DELAY_MS); + LED_OFF; + LED_DELAY; } } diff --git a/src/uart.c b/src/uart.c deleted file mode 100644 index d0923c2..0000000 --- a/src/uart.c +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include - -#ifndef F_CPU -#define F_CPU 16000000UL -#endif - -#ifndef BAUD -#define BAUD 9600 -#endif -#include - -/* http://www.cs.mun.ca/~rod/Winter2007/4723/notes/serial/serial.html */ - -void uart0_init(void) -{ - UBRR0H = UBRRH_VALUE; - UBRR0L = UBRRL_VALUE; -#if USE_2X - UCSR0A |= _BV(U2X0); -#else - UCSR0A &= ~(_BV(U2X0)); -#endif - UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */ - UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */ -} - -void uart3_init(void) -{ - UBRR3H = UBRRH_VALUE; - UBRR3L = UBRRL_VALUE; -#if USE_2X - UCSR3A |= _BV(U2X3); -#else - UCSR3A &= ~(_BV(U2X3)); -#endif - UCSR3C = _BV(UCSZ31) | _BV(UCSZ30); /* 8-bit data */ - UCSR3B = _BV(TXEN3); /* Enable TX */ -} - -int uart0_putchar(char c, FILE *stream) -{ - (void) stream; - - if (c == '\n') { - uart0_putchar('\r', stream); - } - - loop_until_bit_is_set(UCSR0A, UDRE0); - UDR0 = c; - return 0; -} - -int uart3_putchar(char c, FILE *stream) -{ - (void) stream; - - if (c == '\n') { - uart3_putchar('\r', stream); - } - - loop_until_bit_is_set(UCSR3A, UDRE3); - UDR3 = c; - return 0; -} - -int uart0_getchar(FILE *stream) -{ - (void) stream; - loop_until_bit_is_set(UCSR0A, RXC0); - return UDR0; -} diff --git a/src/uart.h b/src/uart.h deleted file mode 100644 index f1801b2..0000000 --- a/src/uart.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef _UART_H_ -#define _UART_H_ - -int uart0_putchar(char c, FILE *stream); -int uart0_getchar(FILE *stream); - -int uart3_putchar(char c, FILE *stream); - -void uart0_init(void); -void uart3_init(void); - -/* http://www.ermicro.com/blog/?p=325 */ - -FILE uart0_io = FDEV_SETUP_STREAM(uart0_putchar, uart0_getchar, _FDEV_SETUP_RW); -FILE uart3_out = FDEV_SETUP_STREAM(uart3_putchar, NULL, _FDEV_SETUP_WRITE); - -#endif /* _UART_H_ */ diff --git a/src/uart_wrap.c b/src/uart_wrap.c new file mode 100644 index 0000000..b4a3b39 --- /dev/null +++ b/src/uart_wrap.c @@ -0,0 +1,33 @@ +#include +#include +#include "../lib/andygock_avr-uart/uart.h" + +int uart0_putc_wrap(char c, FILE *stream) +{ + (void) stream; + + if (c == '\n') { + uart0_putc_wrap('\r', stream); + } + + uart0_putc(c); + return 0; +} + +int uart0_getc_wrap(FILE *stream) +{ + (void) stream; + return (char)uart0_getc(); +} + +int uart3_putc_wrap(char c, FILE *stream) +{ + (void) stream; + + if (c == '\n') { + uart3_putc_wrap('\r', stream); + } + + uart3_putc(c); + return 0; +} diff --git a/src/uart_wrap.h b/src/uart_wrap.h new file mode 100644 index 0000000..5981314 --- /dev/null +++ b/src/uart_wrap.h @@ -0,0 +1,15 @@ +#ifndef _UART_H_ +#define _UART_H_ + +int uart0_putc_wrap(char c, FILE *stream); +int uart0_getc_wrap(FILE *stream); + +int uart3_putc_wrap(char c, FILE *stream); + + +/* http://www.ermicro.com/blog/?p=325 */ + +FILE uart0_io = FDEV_SETUP_STREAM(uart0_putc_wrap, uart0_getc_wrap, _FDEV_SETUP_RW); +FILE uart3_out = FDEV_SETUP_STREAM(uart3_putc_wrap, NULL, _FDEV_SETUP_WRITE); + +#endif /* _UART_H_ */