Main Page   Modules   Data Structures   File List   Globals  

uart.c

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2002, 2003 Oliver Tscherwitschke
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008 1. Redistributions of source code must retain the above copyright notice,
00009    this list of conditions and the following disclaimer.
00010 2. Redistributions in binary form must reproduce the above copyright notice,
00011    this list of conditions and the following disclaimer in the documentation
00012    and/or other materials provided with the distribution.
00013 3. The name of the author may not be used to endorse or promote products
00014    derived from this software without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
00017 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00018 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
00019 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00020 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00021 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00022 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00023 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00024 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00025 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 */
00027 
00036 #include <avr/io.h>
00037 #include <avr/interrupt.h>
00038 #include <avr/signal.h>
00039 #include <avr/pgmspace.h>
00040 
00041 #include "otos_cfg.h"
00042 #include "otos_def.h"
00043 #include "types.h"
00044 #include "uart.h"
00045 
00046 
00047 
00048 unsigned char rx_buf[UART_BUF_SIZE];        /* serial receive buffer */
00049 unsigned char buf_read;                     /* read index, implicitly initialized to 0 */
00050 unsigned char buf_write;                    /* write index, implicitly initialized to 0 */
00051 volatile unsigned char buf_len;             /* number of chars in buffer, implicitly initialized to 0 */
00052 
00053 
00060 void otosPrint(uint8_t *string)
00061 {
00062     while (*string != 0)    
00063     {   
00064         otosPutchar(*(string++));
00065     }
00066 }
00067 
00068 
00075 void otosPrint_P(PGM_P string)
00076 {
00077     while (PRG_RDB(string) != 0)
00078     {
00079         otosPutchar(PRG_RDB(string++));
00080     }
00081 }
00082 
00089 void otosPutchar(uint8_t c)
00090 {   
00091     if (c == '\n')
00092     {
00093 
00094 #ifdef CRLF_OUT
00095         otosPutchar('\r');
00096 #else
00097         c = '\r';
00098 #endif
00099 
00100     }
00101     
00102 #if defined __AVR_ATmega103__
00103     loop_until_bit_is_set(USR, UDRE);       /* wait for end of last transmission */
00104     outb(UDR, c);
00105 #elif defined __AVR_ATmega128__
00106     loop_until_bit_is_set(UCSR0A, UDRE);    /* wait for end of last transmission */
00107     outb(UDR0, c);
00108 #elif (defined __AVR_ATmega163__) || (defined __AVR_ATmega323__)
00109     loop_until_bit_is_set(UCSRA, UDRE);     /* wait for end of last transmission */
00110     outb(UDR, c);
00111 #endif
00112 }
00113 
00114 
00123 uint8_t otosGetchar(void)               /* Read one char from serial port buffer */
00124 {
00125     uint8_t c;
00126     uint8_t   istat;    
00127     
00128     
00129     while (otosUartAvail() == 0);       /* Wait until character has been received  */
00130 
00131     c = rx_buf[buf_read++];             /* Read character  */
00132     if (buf_read == UART_BUF_SIZE)      /* wrap pointer  */
00133         buf_read = 0;
00134 
00135     istat = inb(SREG);      /* save cpu status register         */
00136     cli();                  /* disable interrupts               */
00137     buf_len--;
00138     outb(SREG, istat);      /* restore status register (i-bit)  */
00139 
00140     return c;
00141 }
00142 
00143 
00144 
00151 uint8_t otosUartAvail(void)
00152 {
00153     uint8_t   istat;    
00154     uint8_t   avail;
00155 
00156 
00157     istat = inb(SREG);      /* save cpu status register         */
00158     cli();
00159     avail = buf_len;
00160     outb(SREG, istat);      /* restore status register (i-bit)  */
00161     return avail;
00162 }
00163 
00164 
00170 SIGNAL(SIG_UART_RECV)
00171 {
00172     uint8_t c;
00173     
00174 #if defined __AVR_ATmega128__
00175     c = inb(UDR0);
00176 #else    
00177     c = inb(UDR);
00178 #endif
00179 
00180     if (buf_len == UART_BUF_SIZE)       /* buffer is full, ignore char */
00181         return;
00182 
00183     rx_buf[buf_write++] = c;            /* store char in ring buffer */
00184     if (buf_write == UART_BUF_SIZE)     /* wrap pointer */
00185         buf_write = 0;
00186     buf_len++;                          /* increase length */
00187 }
00188 
00189 
00190 
00199 void otosUartClear(void)
00200 {
00201 #if defined __AVR_ATmega103__
00202     cbi(UCR, RXCIE);        /* disable int */
00203 #elif defined __AVR_ATmega128__
00204     cbi(UCSR0B, RXCIE);     /* disable int */
00205 #elif (defined __AVR_ATmega163__) || (defined __AVR_ATmega323__)
00206     cbi(UCSRB, RXCIE);      /* disable int */
00207 #endif
00208     
00209     buf_read = 0;
00210     buf_write = 0;
00211     buf_len = 0;
00212 
00213 #if defined __AVR_ATmega103__
00214     sbi(UCR, RXCIE);        /* enable int */
00215 #elif defined __AVR_ATmega128__
00216     cbi(UCSR0B, RXCIE);     /* disable int */
00217 #elif (defined __AVR_ATmega163__) || (defined __AVR_ATmega323__)
00218     cbi(UCSRB, RXCIE);      /* disable int */
00219 #endif
00220 }
00221 
00234 uint8_t otosPeekchar(void)              /* Read one char from serial port buffer  */
00235                                         /* without removing it  */
00236 {   
00237     if (otosUartAvail() == 0)           /* don't wait for character */
00238         return 0;
00239 
00240     return rx_buf[buf_read];            /* Read character  */
00241 }
00242 
00257 uint8_t otosReadline(uint8_t* buffer, uint8_t len)
00258 {
00259     uint8_t i = 0;
00260     uint8_t c;
00261 
00262     do
00263     {
00264         c = otosGetchar();
00265         if (c != '\r')
00266             buffer[i++] = c;            /* store character */
00267 
00268 #ifdef CRLF_IN                          /* lines end with CR+LF */
00269         else                            /* cr detected */
00270             otosGetchar();              /* eat linefeed */
00271 #endif
00272 
00273     } while ((c != '\r') && (i < (len - 1)));
00274 
00275     buffer[i] = 0;
00276 
00277     return i;
00278 }
00279 

Generated on Sat Jan 25 18:41:43 2003 for otOS by doxygen1.3-rc2