Synergia AVR 0.1

lib/usart/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 bool Usart::gotLine(){
00059         BufferItem *p = buf._head;
00060         while(p){
00061                 if(p->character == '\n') return true;
00062                 p = p->next;
00063         }
00064         return false;
00065 }
00066 
00067 char * Usart::readline(){
00068         char c;
00069         char * buf = (char*) malloc(sizeof(char)*1024);
00070         int i = 0;
00071         
00072         while(i < 1024){
00073                 while(empty());
00074                 c = read();
00075                 if(c == '\n') break;
00076                 buf[i] = c;
00077                 i++;
00078         }
00079         
00080         buf[i] = '\0';
00081         return buf;
00082 }
00083 
00084 Usart & Usart::operator<<(const unsigned char byte){
00085         sendByte(byte);
00086         return *this;
00087 }
00088 
00089 Usart & Usart::operator<<(const char byte){
00090         sendByte(byte);
00091         return *this;
00092 }
00093 
00094 Usart & Usart::operator<<(char* string){
00095         while (*string != '\0') sendByte(*string++);
00096         return *this;
00097 }
00098 
00099 Usart & Usart::operator<<(const long number){
00100         char str[10];
00101         itoa(number, str, 10);
00102         *this << str;
00103         return *this;
00104 }
00105 
00106 Usart & Usart::operator<<(const int number){
00107         *this << (long)number;
00108         return *this;
00109 }
00110 
00111 Usart & Usart::operator>>(unsigned char &c){
00112         c = read();
00113         return *this;
00114 }
00115 
00116 Usart & Usart::operator>>(int &c){
00117         unsigned char first = read();
00118         int n = 0;
00119         int sign = 1;
00120         
00121         if(first == '-') sign = -1;
00122         else if(first >= '0' && first <= '9') n = __usart__char2int(first);
00123         
00124         for(;;){
00125                 while(buf.empty());
00126                 unsigned char c = buf.read();
00127                 if(c == '\n') break;
00128                 n *= 10;
00129                 n += __usart__char2int(c);
00130         }
00131         
00132         c = n*sign;
00133         return *this;
00134 }
00135 
00136 
00137 // USARTn specific methods
00138 
00139 #if defined(__AVR_ATmega128__)
00140 
00141 // USART0
00142 Usart0::Usart0(){
00143         int ubrr = (int)(F_CPU/USART_BAUD/16) - 1;
00144         
00145         UBRR0H = (unsigned char) (ubrr >> 8);
00146         UBRR0L = (unsigned char) ubrr;
00147         UCSR0B = (1 << RXCIE0) | (1 << RXEN0) | (1 << TXEN0);
00148         UCSR0C = (1 << USBS0) | (1 << UCSZ01) | (3 << UCSZ00);
00149 }
00150 
00151 void Usart0::sendByte(unsigned char byte){
00152         while (!(UCSR0A & (1<<UDRE0)));
00153         UDR0 = byte;
00154 }
00155 
00156 
00157 // USART1
00158 Usart1::Usart1(){
00159         int ubrr = (int)(F_CPU/USART_BAUD/16) - 1;
00160         
00161         UBRR1H = (unsigned char) (ubrr >> 8);
00162         UBRR1L = (unsigned char) ubrr;
00163         UCSR1B = (1 << RXCIE1) | (1 << RXEN1) | (1 << TXEN1);
00164         UCSR1C = (1 << USBS0) | (1 << UCSZ11) | (3 << UCSZ10);
00165 }
00166 
00167 void Usart1::sendByte(unsigned char byte){
00168         while (!(UCSR1A & (1<<UDRE1)));
00169         UDR1 = byte;
00170 }
00171 
00172 #elif defined(__AVR_ATmega32__)
00173 
00174 // USART32
00175 Usart32::Usart32(){
00176         int ubrr = (int)(F_CPU/USART_BAUD/16) - 1;
00177         
00178         UBRRH = (unsigned char) (ubrr >> 8);
00179         UBRRL = (unsigned char) ubrr;
00180         UCSRB = (1 << RXCIE) | (1 << RXEN) | (1 << TXEN);
00181         // UCSRC = (1 << USBS) | (1 << UCSZ1) | (3 << UCSZ0);
00182         UCSRC = (1 << URSEL) | (1 << USBS) | (1 << UCSZ1) | (3 << UCSZ0);
00183 }
00184 
00185 void Usart32::sendByte(unsigned char byte){
00186         while (!(UCSRA & (1<<UDRE)));
00187         UDR = byte;
00188 }
00189 
00190 #endif
 All Classes Files Functions Variables Defines