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 #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];
00049 unsigned char buf_read;
00050 unsigned char buf_write;
00051 volatile unsigned char buf_len;
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);
00104 outb(UDR, c);
00105 #elif defined __AVR_ATmega128__
00106 loop_until_bit_is_set(UCSR0A, UDRE);
00107 outb(UDR0, c);
00108 #elif (defined __AVR_ATmega163__) || (defined __AVR_ATmega323__)
00109 loop_until_bit_is_set(UCSRA, UDRE);
00110 outb(UDR, c);
00111 #endif
00112 }
00113
00114
00123 uint8_t otosGetchar(void)
00124 {
00125 uint8_t c;
00126 uint8_t istat;
00127
00128
00129 while (otosUartAvail() == 0);
00130
00131 c = rx_buf[buf_read++];
00132 if (buf_read == UART_BUF_SIZE)
00133 buf_read = 0;
00134
00135 istat = inb(SREG);
00136 cli();
00137 buf_len--;
00138 outb(SREG, istat);
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);
00158 cli();
00159 avail = buf_len;
00160 outb(SREG, istat);
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)
00181 return;
00182
00183 rx_buf[buf_write++] = c;
00184 if (buf_write == UART_BUF_SIZE)
00185 buf_write = 0;
00186 buf_len++;
00187 }
00188
00189
00190
00199 void otosUartClear(void)
00200 {
00201 #if defined __AVR_ATmega103__
00202 cbi(UCR, RXCIE);
00203 #elif defined __AVR_ATmega128__
00204 cbi(UCSR0B, RXCIE);
00205 #elif (defined __AVR_ATmega163__) || (defined __AVR_ATmega323__)
00206 cbi(UCSRB, RXCIE);
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);
00215 #elif defined __AVR_ATmega128__
00216 cbi(UCSR0B, RXCIE);
00217 #elif (defined __AVR_ATmega163__) || (defined __AVR_ATmega323__)
00218 cbi(UCSRB, RXCIE);
00219 #endif
00220 }
00221
00234 uint8_t otosPeekchar(void)
00235
00236 {
00237 if (otosUartAvail() == 0)
00238 return 0;
00239
00240 return rx_buf[buf_read];
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;
00267
00268 #ifdef CRLF_IN
00269 else
00270 otosGetchar();
00271 #endif
00272
00273 } while ((c != '\r') && (i < (len - 1)));
00274
00275 buffer[i] = 0;
00276
00277 return i;
00278 }
00279