00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00047
00048
00049
00050
00051 #define UART_BUF_SIZE 32
00052
00053
00054
00055 #undef CRLF_IN
00056
00057
00058
00059 #undef CRLF_OUT
00060
00061
00062
00063
00064
00065
00066
00067 #ifdef UART_BAUD_RATE
00068
00069
00070 #define UART_BAUD_1 (F_CPU/(UART_BAUD_RATE*16L)-1)
00071 #define UART_BAUD_1000 (F_CPU /(UART_BAUD_RATE*16L /1000)-1000)
00072 #if (UART_BAUD_1000 - (UART_BAUD_1 * 1000L)) >= 500
00073 #define UART_BAUD_SELECT (UART_BAUD_1 + 1)
00074 #else
00075 #define UART_BAUD_SELECT (UART_BAUD_1)
00076 #endif
00077
00078
00079 #define UART_BAUD_ERROR ((UART_BAUD_1000 - UART_BAUD_SELECT * 1000L) \
00080 * 1000L / UART_BAUD_1000)
00081
00082 #if (UART_BAUD_ERROR > 20) || (UART_BAUD_ERROR < -20)
00083 #error The selected baudrate does not work with this crystal frequency (error > 2 %)
00084 #elif (UART_BAUD_ERROR > 10) || (UART_BAUD_ERROR < -10)
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)); \
00093 outb(UBRR, UART_BAUD_SELECT)
00094
00095 #elif defined __AVR_ATmega128__
00096
00097 #define otosUartInit() \
00098 outb(UCSR0B, _BV(RXCIE) | _BV(RXEN) | _BV(TXEN)); \
00099 outb(UBRR0L, UART_BAUD_SELECT); \
00100 outb(UBRR0H, 0)
00101
00102 #elif (defined __AVR_ATmega163__) || (defined __AVR_ATmega323__)
00103
00104 #define otosUartInit() \
00105 outb(UCSRB, _BV(RXCIE) | _BV(RXEN) | _BV(TXEN)); \
00106 outb(UBRR, UART_BAUD_SELECT); \
00107 outb(UBRRH, 0)
00108
00109 #endif
00110
00111
00112
00113
00114
00115 #else
00116
00117 #if defined __AVR_ATmega103__
00118
00119
00120 #define otosUartInit(baud) \
00121 outb(UCR, _BV(RXCIE) | _BV(RXEN) | _BV(TXEN)); \
00122 outb(UBRR, (F_CPU/(baud*16L)-1))
00123
00124 #elif defined __AVR_ATmega128__
00125
00126
00127 #define otosUartInit(baud) \
00128 outb(UCSR0B, _BV(RXCIE) | _BV(RXEN) | _BV(TXEN)); \
00129 outb(UBRR0L, (F_CPU/(baud*16L)-1)); \
00130 outb(UBRR0H, 0)
00131
00132 #elif (defined __AVR_ATmega163__) || (defined __AVR_ATmega323__)
00133
00134
00135 #define otosUartInit(baud) \
00136 outb(UCSRB, _BV(RXCIE) | _BV(RXEN) | _BV(TXEN)); \
00137 outb(UBRR, (F_CPU/(baud*16L)-1)); \
00138 outb(UBRRH, 0)
00139
00140 #endif
00141
00142 #endif
00143
00144
00145
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