Async uart
This commit is contained in:
parent
6635f07e8d
commit
22461eaff4
78
src/main.c
78
src/main.c
@ -1,38 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/pgmspace.h>
|
||||
#include <util/delay.h>
|
||||
#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,17 +59,16 @@ 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);
|
||||
|
||||
/* Month search and print */
|
||||
char letter;
|
||||
fprintf_P(stdout, PSTR(GET_MONTH_MSG));
|
||||
}
|
||||
|
||||
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 */
|
||||
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]));
|
||||
@ -60,10 +78,24 @@ int main (void)
|
||||
}
|
||||
}
|
||||
|
||||
lcd_puts_P(PSTR(" ")); /* Clear the end of the line */
|
||||
lcd_puts_P(PSTR(" ")); // Clear the end of the line
|
||||
fprintf_P(stdout, PSTR(GET_MONTH_MSG));
|
||||
}
|
||||
|
||||
/* set pin 3 low to turn led off */
|
||||
PORTA &= ~_BV(PORTA3);
|
||||
_delay_ms(BLINK_DELAY_MS);
|
||||
int main (void)
|
||||
{
|
||||
init_hw();
|
||||
start_ui();
|
||||
|
||||
while (1) {
|
||||
LED_ON;
|
||||
LED_DELAY;
|
||||
|
||||
if (uart0_available()) {
|
||||
search_month();
|
||||
}
|
||||
|
||||
LED_OFF;
|
||||
LED_DELAY;
|
||||
}
|
||||
}
|
||||
|
72
src/uart.c
72
src/uart.c
@ -1,72 +0,0 @@
|
||||
#include <avr/io.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef F_CPU
|
||||
#define F_CPU 16000000UL
|
||||
#endif
|
||||
|
||||
#ifndef BAUD
|
||||
#define BAUD 9600
|
||||
#endif
|
||||
#include <util/setbaud.h>
|
||||
|
||||
/* 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;
|
||||
}
|
17
src/uart.h
17
src/uart.h
@ -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_ */
|
33
src/uart_wrap.c
Normal file
33
src/uart_wrap.c
Normal file
@ -0,0 +1,33 @@
|
||||
#include <avr/io.h>
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
15
src/uart_wrap.h
Normal file
15
src/uart_wrap.h
Normal file
@ -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_ */
|
Loading…
Reference in New Issue
Block a user