Update to new directory layout.
[bertos.git] / bertos / cpu / avr / drv / ser_simple_avr.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30  * All Rights Reserved.
31  * -->
32  *
33  * \brief Simple serial I/O driver
34  *
35  * \version $Id$
36  * \author Francesco Sacchi <batt@develer.com>
37  */
38
39 /*#*
40  *#* $Log$
41  *#* Revision 1.2  2006/07/19 12:56:26  bernie
42  *#* Convert to new Doxygen style.
43  *#*
44  *#* Revision 1.1  2005/04/12 01:37:50  bernie
45  *#* Import into DevLib.
46  *#*
47  *#* Revision 1.7  2005/01/23 12:24:27  bernie
48  *#* Include macros.h for BV().
49  *#*
50  *#* Revision 1.6  2004/10/20 13:40:54  batt
51  *#* Put {} instead of ; after while loop.
52  *#*
53  *#* Revision 1.5  2004/10/20 13:39:40  batt
54  *#* Reformat.
55  *#*
56  *#* Revision 1.4  2004/10/20 13:30:02  batt
57  *#* Optimization of UCSR0C writing
58  *#*
59  *#* Revision 1.3  2004/10/14 15:55:32  batt
60  *#* Add ser_purge.
61  *#*
62  *#* Revision 1.2  2004/10/14 14:46:59  batt
63  *#* Change baudrate calculation.
64  *#*
65  *#* Revision 1.1  2004/10/13 16:35:36  batt
66  *#* New (simple) serial driver.
67  *#*/
68 #include "ser_simple_avr.h"
69
70 #include <cfg/compiler.h>
71 #include <appconfig.h>
72 #include <cfg/macros.h> /* BV() */
73 #include "hw_cpu.h"
74
75 #include <avr/io.h>
76
77 /**
78  * Send a character over the serial line.
79  *
80  * \return the character sent.
81  */
82 int _ser_putchar(int c)
83 {
84         /* Disable Rx to avoid echo*/
85         UCSR0B &= ~BV(RXEN);
86         /* Enable tx*/
87         UCSR0B |= BV(TXEN);
88         /* Prepare transmission */
89         UDR0 = c;
90         /* Wait until byte sent */
91         while (!(UCSR0A & BV(TXC))) {}
92         /* Disable tx to avoid short circuit when tx and rx share the same wire. */
93         UCSR0B &= ~BV(TXEN);
94         /* Enable Rx */
95         UCSR0B |= BV(RXEN);
96         /* Delete TRANSMIT_COMPLETE_BIT flag */
97         UCSR0A |= BV(TXC);
98         return c;
99 }
100
101
102 /**
103  * Get a character from the serial line.
104  * If ther is no character in the buffer this function wait until
105  * one is received (no timeout).
106  *
107  * \return the character received.
108  */
109 int _ser_getchar(void)
110 {
111         /* Wait for data */
112         while (!(UCSR0A & BV(RXC))) {}
113         return UDR0;
114
115 }
116
117
118 /**
119  * Get a character from the receiver buffer
120  * If the buffer is empty, ser_getchar_nowait() returns
121  * immediatly EOF.
122  */
123 int _ser_getchar_nowait(void)
124 {
125         if (!(UCSR0A & BV(RXC))) return EOF;
126         else return UDR0;
127 }
128
129 void _ser_settimeouts(void)
130 {
131 }
132
133 /**
134  * Set the baudrate.
135  */
136 void _ser_setbaudrate(unsigned long rate)
137 {
138         /* Compute baud-rate period */
139         uint16_t period = DIV_ROUND(CLOCK_FREQ / 16UL, rate) - 1;
140
141         UBRR0H = (period) >> 8;
142         UBRR0L = (period);
143 }
144
145 /**
146  * Send a string.
147  */
148 int _ser_print(const char *s)
149 {
150         while(*s) _ser_putchar(*s++);
151         return 0;
152 }
153
154
155 void _ser_setparity(int parity)
156 {
157         /* Set the new parity */
158         UCSR0C |= (UCSR0C & ~(BV(UPM1) | BV(UPM0))) | (parity << UPM0); 
159 }
160
161 /**
162  * Dummy functions.
163  */
164 void _ser_purge(void)
165 {
166         while (_ser_getchar_nowait() != EOF) {}
167 }
168
169 /**
170  * Initialize serial.
171  */
172 struct Serial * _ser_open(void)
173 {
174         /*
175          * Set Rx and Tx pins as input to avoid short
176          * circuit when serial is disabled.
177          */
178         DDRE  &= ~(BV(PE0)|BV(PE1));
179         PORTE &= ~BV(PE0);
180         PORTE |=  BV(PE1);
181         /* Enable only Rx section */
182         UCSR0B = BV(RXEN);
183         return NULL;
184 }
185
186
187 /**
188  * Clean up serial port, disabling the associated hardware.
189  */
190 void _ser_close(void)
191 {
192         /* Disable Rx & Tx. */
193         UCSR0B &= ~(BV(RXEN) | BV(TXEN));
194 }