Main Page   Modules   Data Structures   File List   Globals  

uart.h

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 #ifndef UART_H
00037 #define UART_H
00038 
00039 #include <avr/pgmspace.h>
00040 
00041 #include "otos_cfg.h"
00042 #include "types.h"
00043 
00044 
00045 /*
00046  * serial communication settings
00047  */
00048 
00049 
00050 /* size of the serial receive ring buffer */
00051 #define UART_BUF_SIZE   32
00052 
00053 /* #define CRLF_IN, if received lines end with CR+LF */
00054 /* #undef CRLF_IN, if received lines end with CR only */
00055 #undef CRLF_IN
00056 
00057 /* #define CRLF_OUT, if sent lines should end with CR+LF */
00058 /* #undef CRLF_OUT, if sent lines should end with CR only */
00059 #undef CRLF_OUT
00060 
00061 
00062 /*
00063  * if UART_BAUD_RATE is defined, calculate settings and try to detect
00064  * invalid or sub-optimal baudrates.
00065  * otosUartInit() is called without arguments here.
00066  */
00067 #ifdef UART_BAUD_RATE
00068 
00069     /* Baudrate calculation */
00070     #define UART_BAUD_1 (F_CPU/(UART_BAUD_RATE*16L)-1)              /* baudrate selector, integer part */
00071     #define UART_BAUD_1000 (F_CPU /(UART_BAUD_RATE*16L /1000)-1000)
00072     #if (UART_BAUD_1000 - (UART_BAUD_1 * 1000L)) >= 500             /* > 0.5? */
00073         #define UART_BAUD_SELECT    (UART_BAUD_1 + 1)               /* round up */
00074     #else
00075         #define UART_BAUD_SELECT    (UART_BAUD_1)                   /* else, don't round up */
00076     #endif
00077 
00078     /* detect invalid baudrates */
00079     #define UART_BAUD_ERROR ((UART_BAUD_1000 - UART_BAUD_SELECT * 1000L) \
00080             * 1000L / UART_BAUD_1000)                               /* baudrate error in 1/1000 */
00081 
00082     #if (UART_BAUD_ERROR > 20) || (UART_BAUD_ERROR < -20)           /* more than 2 % difference: error */
00083         #error The selected baudrate does not work with this crystal frequency (error > 2 %)
00084     #elif (UART_BAUD_ERROR > 10) || (UART_BAUD_ERROR < -10)         /* more than 1 % difference: warning */
00085         #warning The selected baudrate is not optimal (error > 1 %)
00086     #endif
00087 
00088 
00089 #if defined __AVR_ATmega103__
00090 
00091     #define otosUartInit()                                                    \
00092         outb(UCR, _BV(RXCIE) | _BV(RXEN) | _BV(TXEN));  /* enable TX and RX */  \
00093         outb(UBRR, UART_BAUD_SELECT)                    /* set baudrate */
00094 
00095 #elif defined __AVR_ATmega128__
00096 
00097     #define otosUartInit()                                                        \
00098         outb(UCSR0B, _BV(RXCIE) | _BV(RXEN) | _BV(TXEN));   /* enable TX and RX */  \
00099         outb(UBRR0L, UART_BAUD_SELECT);                     /* set baudrate low */  \
00100         outb(UBRR0H, 0)                                     /* set baudrate high */
00101 
00102 #elif (defined __AVR_ATmega163__) || (defined __AVR_ATmega323__)
00103 
00104     #define otosUartInit()                                                        \
00105         outb(UCSRB, _BV(RXCIE) | _BV(RXEN) | _BV(TXEN));    /* enable TX and RX */  \
00106         outb(UBRR, UART_BAUD_SELECT);                       /* set baudrate low */  \
00107         outb(UBRRH, 0)                                      /* set baudrate high */
00108 
00109 #endif
00110 
00111 /*
00112  * if UART_BAUD_RATE is not defined, otosUartInit() must be called with the
00113  * baudrate as argument
00114  */
00115 #else
00116 
00117 #if defined __AVR_ATmega103__
00118 
00119     /* uart init */
00120     #define otosUartInit(baud)                                                \
00121         outb(UCR, _BV(RXCIE) | _BV(RXEN) | _BV(TXEN));  /* enable TX and RX */  \
00122         outb(UBRR, (F_CPU/(baud*16L)-1))                /* set baudrate */
00123 
00124 #elif defined __AVR_ATmega128__
00125 
00126     /* uart init */
00127     #define otosUartInit(baud)                                                    \
00128         outb(UCSR0B, _BV(RXCIE) | _BV(RXEN) | _BV(TXEN));   /* enable TX and RX */  \
00129         outb(UBRR0L, (F_CPU/(baud*16L)-1));                 /* set baudrate */      \
00130         outb(UBRR0H, 0)                                     /* set baudrate high */
00131 
00132 #elif (defined __AVR_ATmega163__) || (defined __AVR_ATmega323__)
00133 
00134     /* uart init */
00135     #define otosUartInit(baud)                                                    \
00136         outb(UCSRB, _BV(RXCIE) | _BV(RXEN) | _BV(TXEN));    /* enable TX and RX */  \
00137         outb(UBRR, (F_CPU/(baud*16L)-1));                   /* set baudrate */      \
00138         outb(UBRRH, 0)                                      /* set baudrate */
00139 
00140 #endif
00141 
00142 #endif
00143 
00144 
00145 /* function prototypes */
00146 void            otosPrint(uint8_t *string);
00147 void            otosPrint_P(PGM_P string);
00148 inline void     otosPutchar(uint8_t);
00149 uint8_t         otosGetchar(void);
00150 uint8_t         otosPeekchar(void);
00151 uint8_t         otosReadline(uint8_t *buffer, uint8_t len);
00152 inline uint8_t  otosUartAvail(void);
00153 void            otosUartClear(void);
00154 
00155 
00156 #endif

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