Synergia AVR 0.1

receiver/firmware/usart.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2010 Tymon Tobolski
00003  
00004 Permission is hereby granted, free of charge, to any person obtaining
00005 a copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be
00013 included in all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00016 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00018 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00019 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00020 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00021 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022 */
00023 
00024 #include <avr/io.h>
00025 #include <stdlib.h>
00026 #include "usart.h"
00027 
00035 #define __usart__char2int(c) (c - 48)
00036 
00044 #define __usart__int2char(i) (i + 48)
00045 
00046 void Usart::push(char c){
00047         buf.push(c);
00048 }
00049 
00050 unsigned char Usart::read(){
00051         return buf.read();
00052 }
00053 
00054 bool Usart::empty(){
00055         return buf.empty();
00056 }
00057 
00058 char * Usart::readline(){
00059         char c;
00060         char * buf = (char*) malloc(sizeof(char)*1024);
00061         int i = 0;
00062 
00063         while(i < 1024){
00064                 while(empty());
00065                 c = read();
00066                 if(c == '\n') break;
00067                 buf[i] = c;
00068                 i++;
00069         }
00070         
00071         buf[i] = '\0';
00072         return buf;
00073 }
00074 
00075 Usart & Usart::operator<<(const unsigned char byte){
00076         sendByte(byte);
00077         return *this;
00078 }
00079 
00080 Usart & Usart::operator<<(const char byte){
00081         sendByte(byte);
00082         return *this;
00083 }
00084 
00085 Usart & Usart::operator<<(char* string){
00086         while (*string != '\0') sendByte(*string++);
00087         return *this;
00088 }
00089 
00090 Usart & Usart::operator<<(const long number){
00091         char str[10];
00092         itoa(number, str, 10);
00093         *this << str;
00094         return *this;
00095 }
00096 
00097 Usart & Usart::operator<<(const int number){
00098         *this << (long)number;
00099         return *this;
00100 }
00101 
00102 Usart & Usart::operator>>(unsigned char &c){
00103         c = read();
00104         return *this;
00105 }
00106 
00107 Usart & Usart::operator>>(int &c){
00108         unsigned char first = read();
00109         int n = 0;
00110         int sign = 1;
00111 
00112         if(first == '-') sign = -1;
00113         else if(first >= '0' && first <= '9') n = __usart__char2int(first);
00114 
00115         for(;;){
00116                 while(buf.empty());
00117                 unsigned char c = buf.read();
00118                 if(c == '\n') break;
00119                 n *= 10;
00120                 n += __usart__char2int(c);
00121         }
00122 
00123         c = n*sign;
00124         return *this;
00125 }
00126 
00127 
00128 // USARTn specific methods
00129 
00130 #if defined(__AVR_ATmega128__)
00131 
00132 // USART0
00133 Usart0::Usart0(){
00134         int ubrr = (int)(F_CPU/USART_BAUD/16) - 1;
00135         
00136         UBRR0H = (unsigned char) (ubrr >> 8);
00137         UBRR0L = (unsigned char) ubrr;
00138         UCSR0B = (1 << RXCIE0) | (1 << RXEN0) | (1 << TXEN0);
00139         UCSR0C = (1 << USBS0) | (1 << UCSZ01) | (3 << UCSZ00);
00140 }
00141 
00142 void Usart0::sendByte(unsigned char byte){
00143         while (!(UCSR0A & (1<<UDRE0)));
00144         UDR0 = byte;
00145 }
00146 
00147 
00148 // USART1
00149 Usart1::Usart1(){
00150         int ubrr = (int)(F_CPU/USART_BAUD/16) - 1;
00151         
00152         UBRR1H = (unsigned char) (ubrr >> 8);
00153         UBRR1L = (unsigned char) ubrr;
00154         UCSR1B = (1 << RXCIE1) | (1 << RXEN1) | (1 << TXEN1);
00155         UCSR1C = (1 << USBS0) | (1 << UCSZ11) | (3 << UCSZ10);
00156 }
00157 
00158 void Usart1::sendByte(unsigned char byte){
00159         while (!(UCSR1A & (1<<UDRE1)));
00160         UDR1 = byte;
00161 }
00162 
00163 #elif defined(__AVR_ATmega32__)
00164 
00165 // USART32
00166 Usart32::Usart32(){
00167         int ubrr = (int)(F_CPU/USART_BAUD/16) - 1;
00168         
00169         UBRRH = (unsigned char) (ubrr >> 8);
00170         UBRRL = (unsigned char) ubrr;
00171         UCSRB = (1 << RXCIE) | (1 << RXEN) | (1 << TXEN);
00172         // UCSRC = (1 << USBS) | (1 << UCSZ1) | (3 << UCSZ0);
00173         UCSRC = (1 << URSEL) | (1 << USBS) | (1 << UCSZ1) | (3 << UCSZ0);
00174 }
00175 
00176 void Usart32::sendByte(unsigned char byte){
00177         while (!(UCSRA & (1<<UDRE)));
00178         UDR = byte;
00179 }
00180 
00181 #endif
 All Classes Files Functions Variables Defines