Sync triple-a library.
This commit is contained in:
parent
b442556dd0
commit
7733eee103
17 changed files with 333 additions and 974 deletions
|
@ -18,57 +18,73 @@
|
|||
#endif
|
||||
#include <util/setbaud.h>
|
||||
|
||||
#if defined (UBRR0H)
|
||||
|
||||
#warning UART0
|
||||
#define UBRRxH UBRR0H
|
||||
#define UBRRxL UBRR0L
|
||||
#define UCSRxA UCSR0A
|
||||
#define U2Xx U2X0
|
||||
#define UCSRxC UCSR0C
|
||||
#define UCSZx1 UCSZ01
|
||||
#define UCSZx0 UCSZ00
|
||||
#define UCSRxB UCSR0B
|
||||
#define RXENx RXEN0
|
||||
#define TXENx TXEN0
|
||||
#define UDREx UDRE0
|
||||
#define RXCx RXC0
|
||||
#define UDRx UDR0
|
||||
#define UDRIEx UDRIE0
|
||||
|
||||
#elif defined (UBRR1H)
|
||||
|
||||
#warning UART1
|
||||
#define UBRRxH UBRR1H
|
||||
#define UBRRxL UBRR1L
|
||||
#define UCSRxA UCSR1A
|
||||
#define U2Xx U2X1
|
||||
#define UCSRxC UCSR1C
|
||||
#define UCSZx1 UCSZ11
|
||||
#define UCSZx0 UCSZ10
|
||||
#define UCSRxB UCSR1B
|
||||
#define RXENx RXEN1
|
||||
#define TXENx TXEN1
|
||||
#define UDREx UDRE1
|
||||
#define RXCx RXC1
|
||||
#define UDRx UDR1
|
||||
#define UDRIEx UDRIE1
|
||||
|
||||
#else
|
||||
#error No UART?
|
||||
#endif
|
||||
|
||||
/* http://www.cs.mun.ca/~rod/Winter2007/4723/notes/serial/serial.html */
|
||||
|
||||
#if defined (__AVR_ATmega32U4__)
|
||||
void uart_init(void) {
|
||||
UBRR1H = UBRRH_VALUE;
|
||||
UBRR1L = UBRRL_VALUE;
|
||||
UBRRxH = UBRRH_VALUE;
|
||||
UBRRxL = UBRRL_VALUE;
|
||||
|
||||
#if USE_2X
|
||||
UCSR1A |= _BV(U2X1);
|
||||
UCSRxA |= _BV(U2Xx);
|
||||
#else
|
||||
UCSR1A &= ~(_BV(U2X1));
|
||||
UCSRxA &= ~(_BV(U2Xx));
|
||||
#endif
|
||||
|
||||
UCSR1C = _BV(UCSZ11) | _BV(UCSZ10); /* 8-bit data */
|
||||
UCSR1B = _BV(RXEN1) | _BV(TXEN1); /* Enable RX and TX */
|
||||
UCSRxC = _BV(UCSZx1) | _BV(UCSZx0); /* 8-bit data */
|
||||
UCSRxB = _BV(RXENx) | _BV(TXENx); /* Enable RX and TX */
|
||||
}
|
||||
#else
|
||||
void uart_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 */
|
||||
}
|
||||
#endif
|
||||
|
||||
void uart_putchar(char c, FILE *stream) {
|
||||
int uart_putchar(char c, FILE *stream) {
|
||||
if (c == '\n') {
|
||||
uart_putchar('\r', stream);
|
||||
}
|
||||
#if defined (__AVR_ATmega32U4__)
|
||||
loop_until_bit_is_set(UCSR1A, UDRE1);
|
||||
UDR1 = c;
|
||||
#else
|
||||
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||
UDR0 = c;
|
||||
#endif
|
||||
loop_until_bit_is_set(UCSRxA, UDREx);
|
||||
UDRx = c;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char uart_getchar(FILE *stream) {
|
||||
#if defined (__AVR_ATmega32U4__)
|
||||
loop_until_bit_is_set(UCSR1A, RXC1);
|
||||
return UDR1;
|
||||
#else
|
||||
loop_until_bit_is_set(UCSR0A, RXC0);
|
||||
return UDR0;
|
||||
#endif
|
||||
int uart_getchar(FILE *stream) {
|
||||
loop_until_bit_is_set(UCSRxA, RXCx);
|
||||
return UDRx;
|
||||
}
|
||||
|
|
|
@ -5,9 +5,12 @@
|
|||
* http://www.opensource.org/licenses/mit-license.php
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef UART_H
|
||||
#define UART_H
|
||||
|
||||
void uart_putchar(char c, FILE *stream);
|
||||
char uart_getchar(FILE *stream);
|
||||
int uart_putchar(char c, FILE *stream);
|
||||
int uart_getchar(FILE *stream);
|
||||
|
||||
void uart_init(void);
|
||||
|
||||
|
@ -15,3 +18,6 @@ void uart_init(void);
|
|||
|
||||
FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
|
||||
FILE uart_input = FDEV_SETUP_STREAM(NULL, uart_getchar, _FDEV_SETUP_READ);
|
||||
|
||||
#endif /* UART_H */
|
||||
|
||||
|
|
|
@ -25,6 +25,51 @@
|
|||
#define UART_TX_BUFFER_SIZE 64
|
||||
#endif
|
||||
|
||||
#if defined (UBRR0H)
|
||||
|
||||
#warning Using UART0
|
||||
#define UBRRxH UBRR0H
|
||||
#define UBRRxL UBRR0L
|
||||
#define UCSRxA UCSR0A
|
||||
#define U2Xx U2X0
|
||||
#define UCSRxC UCSR0C
|
||||
#define UCSZx1 UCSZ01
|
||||
#define UCSZx0 UCSZ00
|
||||
#define UCSRxB UCSR0B
|
||||
#define RXENx RXEN0
|
||||
#define TXENx TXEN0
|
||||
#define UDREx UDRE0
|
||||
#define RXCx RXC0
|
||||
#define UDRx UDR0
|
||||
#define UDRIEx UDRIE0
|
||||
|
||||
#define USARTx_RX_vect USART_RX_vect
|
||||
#define USARTx_UDRE_vect USART_UDRE_vect
|
||||
#elif defined (UBRR1H)
|
||||
|
||||
#warning Using UART1
|
||||
#define UBRRxH UBRR1H
|
||||
#define UBRRxL UBRR1L
|
||||
#define UCSRxA UCSR1A
|
||||
#define U2Xx U2X1
|
||||
#define UCSRxC UCSR1C
|
||||
#define UCSZx1 UCSZ11
|
||||
#define UCSZx0 UCSZ10
|
||||
#define UCSRxB UCSR1B
|
||||
#define RXENx RXEN1
|
||||
#define TXENx TXEN1
|
||||
#define UDREx UDRE1
|
||||
#define RXCx RXC1
|
||||
#define UDRx UDR1
|
||||
#define UDRIEx UDRIE1
|
||||
|
||||
#define USARTx_RX_vect USART1_RX_vect
|
||||
#define USARTx_UDRE_vect USART1_UDRE_vect
|
||||
|
||||
#else
|
||||
#error No UART?
|
||||
#endif
|
||||
|
||||
struct tx_ring {
|
||||
int buffer[UART_TX_BUFFER_SIZE];
|
||||
int start;
|
||||
|
@ -50,18 +95,16 @@ void uart_init(void) {
|
|||
rx_buffer.start = 0;
|
||||
rx_buffer.end = 0;
|
||||
|
||||
UBRR0H = UBRRH_VALUE;
|
||||
UBRR0L = UBRRL_VALUE;
|
||||
UBRRxH = UBRRH_VALUE;
|
||||
UBRRxL = UBRRL_VALUE;
|
||||
|
||||
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
|
||||
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
|
||||
UCSRxC = _BV(UCSZx1) | _BV(UCSZx0); /* 8-bit data */
|
||||
UCSRxB = _BV(RXENx) | _BV(TXENx); /* Enable RX and TX */
|
||||
|
||||
sei();
|
||||
}
|
||||
|
||||
|
||||
//int uart_putchar(char c, FILE *stream) {
|
||||
void uart_putchar(char c, FILE *stream) {
|
||||
int uart_putchar(char c, FILE *stream) {
|
||||
if (c == '\n') {
|
||||
uart_putchar('\r', stream);
|
||||
}
|
||||
|
@ -73,39 +116,39 @@ void uart_putchar(char c, FILE *stream) {
|
|||
tx_buffer.end = write_pointer;
|
||||
|
||||
/* Data available. Enable the transmit interrupt for serial port 0. */
|
||||
UCSR0B |= _BV(UDRIE0);
|
||||
UCSRxB |= _BV(UDRIEx);
|
||||
}
|
||||
|
||||
//return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char uart_getchar(FILE *stream) {
|
||||
int uart_getchar(FILE *stream) {
|
||||
int read_pointer = (rx_buffer.start + 1) % UART_RX_BUFFER_SIZE;
|
||||
|
||||
rx_buffer.start = read_pointer;
|
||||
return rx_buffer.buffer[read_pointer];
|
||||
}
|
||||
|
||||
ISR(USART_RX_vect) {
|
||||
ISR(USARTx_RX_vect) {
|
||||
int write_pointer = (rx_buffer.end + 1) % UART_RX_BUFFER_SIZE;
|
||||
|
||||
/* Add next byte to ringbuffer if it has space available. */
|
||||
if (write_pointer != rx_buffer.start){
|
||||
rx_buffer.buffer[rx_buffer.end] = UDR0;
|
||||
rx_buffer.buffer[rx_buffer.end] = UDRx;
|
||||
rx_buffer.end = write_pointer;
|
||||
}
|
||||
}
|
||||
|
||||
ISR(USART_UDRE_vect){
|
||||
ISR(USARTx_UDRE_vect){
|
||||
int read_pointer = (tx_buffer.start + 1) % UART_TX_BUFFER_SIZE;
|
||||
|
||||
/* Transmit next byte if data available in ringbuffer. */
|
||||
if (read_pointer != tx_buffer.end) {
|
||||
UDR0 = tx_buffer.buffer[read_pointer];
|
||||
UDRx = tx_buffer.buffer[read_pointer];
|
||||
tx_buffer.start = read_pointer;
|
||||
} else {
|
||||
/* Nothing to send. Disable the transmit interrupt for serial port 0. */
|
||||
UCSR0B &= ~_BV(UDRIE0);
|
||||
/* Nothing to send. Disable the transmit interrupt for serial port x. */
|
||||
UCSRxB &= ~_BV(UDRIEx);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue