- Main Page
- Related Pages
- Files

- File List
- Globals

# uart.c

Go to the documentation of this file.

    00001
    00012 #define _UART_C_ 1
    00013
    00014 #include <avr/io.h>
    00015 #include <avr/interrupt.h>
    00016 #include "uart.h"
    00017
    00018 uint8_t rx_buffer[UART_RX_BUFFER_SIZE];
    00019 uint8_t tx_buffer[UART_TX_BUFFER_SIZE];
    00020
    00021 volatile uint8_t rx_head=0;
    00022 volatile uint8_t rx_tail=0;
    00023 volatile uint8_t tx_head=0;
    00024 volatile uint8_t tx_tail=0;
    00025
    00026
    00027 void uart_init(void) {
    00028
    00029                 UART_DDR |= TXPIN_bm;
    00030                 UART_DDR &= ~RXPIN_bm;
    00031
    00032                 UBRRH = (unsigned char)(UBRR_val>>8);
    00033                 UBRRL = (unsigned char)(UBRR_val);
    00034
    00035                 //UCSRA |= (1<<U2X);
    00036                 UCSRB = (1<<RXEN) | (1<<TXEN);
    00037                 UCSRC = (1<<URSEL) | (1<<UCSZ1) | (1<<UCSZ0);
    00038
    00039                 #if UART_CHRSIZE_9BIT==1
    00040                 UCSRB |= (1<<UCSZ2); //9-Bit Character Size
    00041                 #endif
    00042
    00043                 uart_rxc_int_en();
    00044
    00045 }
    00046
    00047
    00048 void uart_sendbyte(uint8_t data) {
    00049         while(tx_head == tx_tail-1) {}
    00050         tx_buffer[tx_head++] = data;
    00051         tx_head &= (UART_TX_BUFFER_SIZE-1);
    00052         uart_udre_int_en();
    00053 }
    00054
    00055
    00056 uint8_t uart_senddata(uint8_t data){
    00057         if(!(UCSRA & (1<<UDRE))) { return 0; }
    00058         UCSRB &= ~(1<<TXB8);
    00059         UART_DATA = data;
    00060         return 1;
    00061 }
    00062
    00063
    00064 uint8_t uart_sendid(uint8_t data){
    00065         if(!(UCSRA & (1<<UDRE))) { return 0; }
    00066         UCSRB |= (1<<TXB8);
    00067         UART_DATA = data;
    00068         return 1;
    00069 }
    00070
    00071
    00072 uint8_t uart_getbyte(uint8_t *data) {
    00073         if(rx_tail==rx_head) {
    00074                 return 0;
    00075         } else {
    00076                 *data=rx_buffer[rx_tail++];
    00077                 rx_tail &= (UART_RX_BUFFER_SIZE-1);
    00078                 return 1;
    00079         }
    00080 }
    00081
    00082 #if UART_MPCM_ENABLE == 1
    00083 uint8_t uart_senddata(uint8_t data){
    00084         if(!(UCSRA & (1<<UDRE))) { return 0; }
    00085         UCSRB &= ~(1<<TXB8);
    00086         UART_DATA = data;
    00087         return 1;
    00088 }
    00089
    00090
    00091 uint8_t uart_sendid(uint8_t data){
    00092         if(!(UCSRA & (1<<UDRE))) { return 0; }
    00093         UCSRB |= (1<<TXB8);
    00094         UART_DATA = data;
    00095         return 1;
    00096 }
    00097 #endif
    00098
    00099 #if UART_PRINTF_COMPATIBILITY == 1
    00100
    00101 int uart_putchar(char c, FILE *sream) {
    00102         while(tx_head == tx_tail-1) {}
    00103         tx_buffer[tx_head++] = c;
    00104         tx_head &= (UART_TX_BUFFER_SIZE-1);
    00105         uart_udre_int_en();
    00106         return 0;
    00107 }
    00108
    00109
    00110 int uart_getchar(FILE *stream) {
    00111         int data;
    00112         if(rx_tail==rx_head) {
    00113                 return _FDEV_EOF;
    00114         } else {
    00115                 data= (int)rx_buffer[rx_tail++];
    00116                 rx_tail &= (UART_RX_BUFFER_SIZE-1);
    00117                 return data;
    00118         }
    00119
    00120 }
    00121
    00122 #endif
    00123
    00130 ISR(RXC_vect) {
    00131
    00132         if(rx_head != ((rx_tail-1) & (UART_RX_BUFFER_SIZE-1)) ) {
    00133                 rx_buffer[rx_head++] = UART_DATA;
    00134                 rx_head &= (UART_RX_BUFFER_SIZE-1);
    00135         }
    00136 }
    00137
    00145 ISR(DRE_vect) {
    00146         if(tx_tail==tx_head) {
    00147                 uart_udre_int_dis();
    00148         } else {
    00149                 UART_DATA = tx_buffer[tx_tail++];
    00150                 tx_tail &= (UART_TX_BUFFER_SIZE-1);
    00151         }
    00152 }