From b248b82033d10519c7f1485bafe2e1b5b6b64dde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Mon, 18 Jan 2016 15:50:36 +0100 Subject: [PATCH] reimplement UART with interrupt support (now USART) --- lib/usart.c | 172 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/usart.h | 44 ++++++++++++++ 2 files changed, 216 insertions(+) create mode 100644 lib/usart.c create mode 100644 lib/usart.h diff --git a/lib/usart.c b/lib/usart.c new file mode 100644 index 0000000..0190b58 --- /dev/null +++ b/lib/usart.c @@ -0,0 +1,172 @@ +/* This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +/* Copyright (c) 2015 King Kévin */ +/* this library handles the USART + * it uses the TX and RX pins + * it can be used with or without interrupts + */ + +#include // Standard Integer Types +#include // Standard IO facilities +#include // General utilities + +#include // AVR device-specific IO definitions +#include // Interrupts +#include // Power Management and Sleep Modes + +#include "usart.h" // USART header +#include // Helper macros for baud rate calculations + +/* assign input and output streams */ +FILE usart_input = FDEV_SETUP_STREAM(NULL, (int (*)(struct __file *)) usart_getchar, _FDEV_SETUP_READ); +#if defined(USART_INTERRUPT) && (USART_INTERRUPT==1) +FILE usart_output = FDEV_SETUP_STREAM((int (*)(char, struct __file *)) usart_putchar_nonblocking, NULL, _FDEV_SETUP_WRITE); +FILE usart_io = FDEV_SETUP_STREAM((int (*)(char, struct __file *)) usart_putchar_nonblocking, (int (*)(struct __file *)) usart_getchar, _FDEV_SETUP_RW); +#else +FILE usart_output = FDEV_SETUP_STREAM((int (*)(char, struct __file *)) usart_putchar_blocking, NULL, _FDEV_SETUP_WRITE); +FILE usart_io = FDEV_SETUP_STREAM((int (*)(char, struct __file *)) usart_putchar_blocking, (int (*)(struct __file *)) usart_getchar, _FDEV_SETUP_RW); +#endif + +#if defined(USART_INTERRUPT) && (USART_INTERRUPT==1) +/* input and output ring buffer, indexes, and available memory */ +uint8_t rx_buffer[32] = {0}; +volatile uint8_t rx_i = 0; +volatile uint8_t rx_used = 0; +uint8_t tx_buffer[32] = {0}; +volatile uint8_t tx_i = 0; +volatile uint8_t tx_used = 0; +/* show the user how much received data is ready */ +volatile uint8_t usart_incoming = 0; // same as rx_used, but since the user can write this variable we don't rely on it +#endif + +/* configure USART serial port */ +void usart_init(void) +{ + UBRR0H = UBRRH_VALUE; // set baurate (calculated from BAUD) + UBRR0L = UBRRL_VALUE; // set baurate (calculated from BAUD) +#if USE_2X + UCSR0A |= (1<=sizeof(rx_buffer)) { + return; + } + cli(); + rx_buffer[(rx_i+rx_used)%sizeof(rx_buffer)] = UDR0; // put character in buffer + rx_used++; // update used buffer + usart_incoming = rx_used; // update available data + sei(); +} + +/* put character on USART stream (non-blocking using a buffer) */ +void usart_putchar_nonblocking(char c, FILE *stream) +{ + if (c == '\n') { // add carrier return before line feed. this is recommended for most UART terminals + usart_putchar_nonblocking('\r', stream); + } + if (UCSR0A&(1<=sizeof(tx_buffer)) { + set_sleep_mode(SLEEP_MODE_IDLE); + sleep_mode(); + } + cli(); + tx_buffer[(tx_i+tx_used)%sizeof(tx_buffer)] = c; // put character in buffer + tx_used++; // update used buffer + sei(); + } +} + +ISR(USART_UDRE_vect) { // USART transmit register interrupt + if (!tx_used) { // no data in the buffer to transmit + return; + } + UDR0 = tx_buffer[tx_i]; // put data in transmit register + tx_i = (tx_i+1)%sizeof(rx_buffer); // update location on buffer + tx_used--; // update used size +} +#endif + + diff --git a/lib/usart.h b/lib/usart.h new file mode 100644 index 0000000..bbc4a49 --- /dev/null +++ b/lib/usart.h @@ -0,0 +1,44 @@ +/* This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +/* Copyright (c) 2015 King Kévin */ +/* This library handles the USART configuration */ + +/* use interrupts */ +#define USART_INTERRUPT 1 +/* set tolerance to 3% to allow 115200 baudrate with 16 MHz clock, else use 9600 for default <2% */ +#define BAUD_TOL 3 +/* serial baudrate, in bits per second (with 8N1 8 bits, no parity bit, 1 stop bit settings) */ +#define BAUD 9600 + +/* input & output streams */ +FILE usart_output; +FILE usart_input; +FILE usart_io; + +/* show the user how much received data is ready */ +volatile uint8_t usart_incoming; + +/* configure serial port */ +void usart_init(void); +/* put character on USART stream (blocking) */ +void usart_putchar_blocking(char c, FILE *stream); +/* ensure all data has been transmitted */ +void usart_flush(); +/* get character from USART stream (blocking) */ +char usart_getchar(FILE *stream); +#if defined(USART_INTERRUPT) && (USART_INTERRUPT==1) +/* put character on USART stream (non-blocking using a buffer) */ +void usart_putchar_nonblocking(char c, FILE *stream); +#endif