ser_simple_avr.c

Go to the documentation of this file.
00001 
00039 /*#*
00040  *#* $Log$
00041  *#* Revision 1.2  2006/07/19 12:56:26  bernie
00042  *#* Convert to new Doxygen style.
00043  *#*
00044  *#* Revision 1.1  2005/04/12 01:37:50  bernie
00045  *#* Import into DevLib.
00046  *#*
00047  *#* Revision 1.7  2005/01/23 12:24:27  bernie
00048  *#* Include macros.h for BV().
00049  *#*
00050  *#* Revision 1.6  2004/10/20 13:40:54  batt
00051  *#* Put {} instead of ; after while loop.
00052  *#*
00053  *#* Revision 1.5  2004/10/20 13:39:40  batt
00054  *#* Reformat.
00055  *#*
00056  *#* Revision 1.4  2004/10/20 13:30:02  batt
00057  *#* Optimization of UCSR0C writing
00058  *#*
00059  *#* Revision 1.3  2004/10/14 15:55:32  batt
00060  *#* Add ser_purge.
00061  *#*
00062  *#* Revision 1.2  2004/10/14 14:46:59  batt
00063  *#* Change baudrate calculation.
00064  *#*
00065  *#* Revision 1.1  2004/10/13 16:35:36  batt
00066  *#* New (simple) serial driver.
00067  *#*/
00068 #include "ser_simple_avr.h"
00069 
00070 #include <cfg/compiler.h>
00071 #include <appconfig.h>
00072 #include <cfg/macros.h> /* BV() */
00073 #include "hw_cpu.h"
00074 
00075 #include <avr/io.h>
00076 
00082 int _ser_putchar(int c)
00083 {
00084     /* Disable Rx to avoid echo*/
00085     UCSR0B &= ~BV(RXEN);
00086     /* Enable tx*/
00087     UCSR0B |= BV(TXEN);
00088     /* Prepare transmission */
00089     UDR0 = c;
00090     /* Wait until byte sent */
00091     while (!(UCSR0A & BV(TXC))) {}
00092     /* Disable tx to avoid short circuit when tx and rx share the same wire. */
00093     UCSR0B &= ~BV(TXEN);
00094     /* Enable Rx */
00095     UCSR0B |= BV(RXEN);
00096     /* Delete TRANSMIT_COMPLETE_BIT flag */
00097     UCSR0A |= BV(TXC);
00098     return c;
00099 }
00100 
00101 
00109 int _ser_getchar(void)
00110 {
00111     /* Wait for data */
00112     while (!(UCSR0A & BV(RXC))) {}
00113     return UDR0;
00114 
00115 }
00116 
00117 
00123 int _ser_getchar_nowait(void)
00124 {
00125     if (!(UCSR0A & BV(RXC))) return EOF;
00126     else return UDR0;
00127 }
00128 
00129 void _ser_settimeouts(void)
00130 {
00131 }
00132 
00136 void _ser_setbaudrate(unsigned long rate)
00137 {
00138     /* Compute baud-rate period */
00139     uint16_t period = DIV_ROUND(CLOCK_FREQ / 16UL, rate) - 1;
00140 
00141     UBRR0H = (period) >> 8;
00142     UBRR0L = (period);
00143 }
00144 
00148 int _ser_print(const char *s)
00149 {
00150     while(*s) _ser_putchar(*s++);
00151     return 0;
00152 }
00153 
00154 
00155 void _ser_setparity(int parity)
00156 {
00157     /* Set the new parity */
00158     UCSR0C |= (UCSR0C & ~(BV(UPM1) | BV(UPM0))) | (parity << UPM0); 
00159 }
00160 
00164 void _ser_purge(void)
00165 {
00166     while (_ser_getchar_nowait() != EOF) {}
00167 }
00168 
00172 struct Serial * _ser_open(void)
00173 {
00174     /*
00175      * Set Rx and Tx pins as input to avoid short
00176      * circuit when serial is disabled.
00177      */
00178     DDRE  &= ~(BV(PE0)|BV(PE1));
00179     PORTE &= ~BV(PE0);
00180     PORTE |=  BV(PE1);
00181     /* Enable only Rx section */
00182     UCSR0B = BV(RXEN);
00183     return NULL;
00184 }
00185 
00186 
00190 void _ser_close(void)
00191 {
00192     /* Disable Rx & Tx. */
00193     UCSR0B &= ~(BV(RXEN) | BV(TXEN));
00194 }