2015-04-09 18:32:04 +02:00
|
|
|
/* UART Example for Teensy USB Development Board
|
|
|
|
* http://www.pjrc.com/teensy/
|
|
|
|
* Copyright (c) 2009 PJRC.COM, LLC
|
2019-08-30 20:19:03 +02:00
|
|
|
*
|
2015-04-09 18:32:04 +02:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2019-08-30 20:19:03 +02:00
|
|
|
*
|
2015-04-09 18:32:04 +02:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2019-08-30 20:19:03 +02:00
|
|
|
*
|
2015-04-09 18:32:04 +02:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Version 1.0: Initial Release
|
|
|
|
// Version 1.1: Add support for Teensy 2.0, minor optimizations
|
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
|
|
|
|
#include "uart.h"
|
|
|
|
|
2021-01-30 04:53:56 +01:00
|
|
|
#if defined(__AVR_AT90USB162__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
|
2020-02-21 12:04:05 +01:00
|
|
|
# define UDRn UDR1
|
2020-03-06 13:55:15 +01:00
|
|
|
# define UBRRnL UBRR1L
|
2020-02-21 12:04:05 +01:00
|
|
|
# define UCSRnA UCSR1A
|
|
|
|
# define UCSRnB UCSR1B
|
|
|
|
# define UCSRnC UCSR1C
|
|
|
|
# define U2Xn U2X1
|
|
|
|
# define RXENn RXEN1
|
|
|
|
# define TXENn TXEN1
|
|
|
|
# define RXCIEn RXCIE1
|
|
|
|
# define UCSZn1 UCSZ11
|
|
|
|
# define UCSZn0 UCSZ10
|
|
|
|
# define UDRIEn UDRIE1
|
2020-03-06 13:55:15 +01:00
|
|
|
# define USARTn_UDRE_vect USART1_UDRE_vect
|
|
|
|
# define USARTn_RX_vect USART1_RX_vect
|
|
|
|
#elif defined(__AVR_ATmega32A__)
|
|
|
|
# define UDRn UDR
|
|
|
|
# define UBRRnL UBRRL
|
|
|
|
# define UCSRnA UCSRA
|
|
|
|
# define UCSRnB UCSRB
|
|
|
|
# define UCSRnC UCSRC
|
|
|
|
# define U2Xn U2X
|
|
|
|
# define RXENn RXEN
|
|
|
|
# define TXENn TXEN
|
|
|
|
# define RXCIEn RXCIE
|
|
|
|
# define UCSZn1 UCSZ1
|
|
|
|
# define UCSZn0 UCSZ0
|
|
|
|
# define UDRIEn UDRIE
|
|
|
|
# define USARTn_UDRE_vect USART_UDRE_vect
|
|
|
|
# define USARTn_RX_vect USART_RX_vect
|
2021-01-27 07:42:49 +01:00
|
|
|
#elif defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__)
|
|
|
|
# define UDRn UDR0
|
|
|
|
# define UBRRnL UBRR0L
|
|
|
|
# define UCSRnA UCSR0A
|
|
|
|
# define UCSRnB UCSR0B
|
|
|
|
# define UCSRnC UCSR0C
|
|
|
|
# define U2Xn U2X0
|
|
|
|
# define RXENn RXEN0
|
|
|
|
# define TXENn TXEN0
|
|
|
|
# define RXCIEn RXCIE0
|
|
|
|
# define UCSZn1 UCSZ01
|
|
|
|
# define UCSZn0 UCSZ00
|
|
|
|
# define UDRIEn UDRIE0
|
|
|
|
# define USARTn_UDRE_vect USART_UDRE_vect
|
|
|
|
# define USARTn_RX_vect USART_RX_vect
|
2020-02-21 11:32:30 +01:00
|
|
|
#endif
|
|
|
|
|
2015-04-09 18:32:04 +02:00
|
|
|
// These buffers may be any size from 2 to 256 bytes.
|
|
|
|
#define RX_BUFFER_SIZE 64
|
2020-02-21 11:32:30 +01:00
|
|
|
#define TX_BUFFER_SIZE 256
|
2015-04-09 18:32:04 +02:00
|
|
|
|
|
|
|
static volatile uint8_t tx_buffer[TX_BUFFER_SIZE];
|
|
|
|
static volatile uint8_t tx_buffer_head;
|
|
|
|
static volatile uint8_t tx_buffer_tail;
|
|
|
|
static volatile uint8_t rx_buffer[RX_BUFFER_SIZE];
|
|
|
|
static volatile uint8_t rx_buffer_head;
|
|
|
|
static volatile uint8_t rx_buffer_tail;
|
|
|
|
|
|
|
|
// Initialize the UART
|
2019-08-30 20:19:03 +02:00
|
|
|
void uart_init(uint32_t baud) {
|
|
|
|
cli();
|
2020-03-06 13:55:15 +01:00
|
|
|
UBRRnL = (F_CPU / 4 / baud - 1) / 2;
|
2020-02-21 11:32:30 +01:00
|
|
|
UCSRnA = (1 << U2Xn);
|
|
|
|
UCSRnB = (1 << RXENn) | (1 << TXENn) | (1 << RXCIEn);
|
|
|
|
UCSRnC = (1 << UCSZn1) | (1 << UCSZn0);
|
2019-08-30 20:19:03 +02:00
|
|
|
tx_buffer_head = tx_buffer_tail = 0;
|
|
|
|
rx_buffer_head = rx_buffer_tail = 0;
|
|
|
|
sei();
|
2015-04-09 18:32:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transmit a byte
|
2019-08-30 20:19:03 +02:00
|
|
|
void uart_putchar(uint8_t c) {
|
|
|
|
uint8_t i;
|
|
|
|
|
|
|
|
i = tx_buffer_head + 1;
|
|
|
|
if (i >= TX_BUFFER_SIZE) i = 0;
|
2020-02-21 12:04:05 +01:00
|
|
|
// return immediately to avoid deadlock when interrupt is disabled(called from ISR)
|
|
|
|
if (tx_buffer_tail == i && (SREG & (1 << SREG_I)) == 0) return;
|
2019-08-30 20:19:03 +02:00
|
|
|
while (tx_buffer_tail == i)
|
|
|
|
; // wait until space in buffer
|
|
|
|
// cli();
|
|
|
|
tx_buffer[i] = c;
|
|
|
|
tx_buffer_head = i;
|
2020-03-06 14:28:11 +01:00
|
|
|
UCSRnB = (1 << RXENn) | (1 << TXENn) | (1 << RXCIEn) | (1 << UDRIEn);
|
2019-08-30 20:19:03 +02:00
|
|
|
// sei();
|
2015-04-09 18:32:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Receive a byte
|
2019-08-30 20:19:03 +02:00
|
|
|
uint8_t uart_getchar(void) {
|
|
|
|
uint8_t c, i;
|
|
|
|
|
|
|
|
while (rx_buffer_head == rx_buffer_tail)
|
|
|
|
; // wait for character
|
|
|
|
i = rx_buffer_tail + 1;
|
|
|
|
if (i >= RX_BUFFER_SIZE) i = 0;
|
|
|
|
c = rx_buffer[i];
|
|
|
|
rx_buffer_tail = i;
|
|
|
|
return c;
|
2015-04-09 18:32:04 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 07:42:49 +01:00
|
|
|
// Return whether the number of bytes waiting in the receive buffer is nonzero.
|
2015-04-09 18:32:04 +02:00
|
|
|
// Call this before uart_getchar() to check if it will need
|
|
|
|
// to wait for a byte to arrive.
|
2021-01-27 07:42:49 +01:00
|
|
|
bool uart_available(void) {
|
2019-08-30 20:19:03 +02:00
|
|
|
uint8_t head, tail;
|
|
|
|
|
|
|
|
head = rx_buffer_head;
|
|
|
|
tail = rx_buffer_tail;
|
2021-01-27 07:42:49 +01:00
|
|
|
if (head >= tail) return (head - tail) > 0;
|
|
|
|
return (RX_BUFFER_SIZE + head - tail) > 0;
|
2015-04-09 18:32:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transmit Interrupt
|
2020-03-06 13:55:15 +01:00
|
|
|
ISR(USARTn_UDRE_vect) {
|
2019-08-30 20:19:03 +02:00
|
|
|
uint8_t i;
|
|
|
|
|
|
|
|
if (tx_buffer_head == tx_buffer_tail) {
|
|
|
|
// buffer is empty, disable transmit interrupt
|
2020-02-21 11:32:30 +01:00
|
|
|
UCSRnB = (1 << RXENn) | (1 << TXENn) | (1 << RXCIEn);
|
2019-08-30 20:19:03 +02:00
|
|
|
} else {
|
|
|
|
i = tx_buffer_tail + 1;
|
|
|
|
if (i >= TX_BUFFER_SIZE) i = 0;
|
2020-03-06 13:55:15 +01:00
|
|
|
UDRn = tx_buffer[i];
|
2019-08-30 20:19:03 +02:00
|
|
|
tx_buffer_tail = i;
|
|
|
|
}
|
2015-04-09 18:32:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Receive Interrupt
|
2020-03-06 13:55:15 +01:00
|
|
|
ISR(USARTn_RX_vect) {
|
2019-08-30 20:19:03 +02:00
|
|
|
uint8_t c, i;
|
|
|
|
|
2020-02-21 11:32:30 +01:00
|
|
|
c = UDRn;
|
2019-08-30 20:19:03 +02:00
|
|
|
i = rx_buffer_head + 1;
|
|
|
|
if (i >= RX_BUFFER_SIZE) i = 0;
|
|
|
|
if (i != rx_buffer_tail) {
|
|
|
|
rx_buffer[i] = c;
|
|
|
|
rx_buffer_head = i;
|
|
|
|
}
|
2015-04-09 18:32:04 +02:00
|
|
|
}
|