Import into DevLib.
[bertos.git] / drv / ser_simple.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * All Rights Reserved.
6  * -->
7  *
8  * \brief Simple serial I/O driver
9  *
10  * \version $Id$
11  * \author Francesco Sacchi <batt@develer.com>
12  */
13
14 /*#*
15  *#* $Log$
16  *#* Revision 1.1  2005/04/12 01:37:50  bernie
17  *#* Import into DevLib.
18  *#*
19  *#* Revision 1.7  2005/01/23 12:24:27  bernie
20  *#* Include macros.h for BV().
21  *#*
22  *#* Revision 1.6  2004/10/20 13:40:54  batt
23  *#* Put {} instead of ; after while loop.
24  *#*
25  *#* Revision 1.5  2004/10/20 13:39:40  batt
26  *#* Reformat.
27  *#*
28  *#* Revision 1.4  2004/10/20 13:30:02  batt
29  *#* Optimization of UCSR0C writing
30  *#*
31  *#* Revision 1.3  2004/10/14 15:55:32  batt
32  *#* Add ser_purge.
33  *#*
34  *#* Revision 1.2  2004/10/14 14:46:59  batt
35  *#* Change baudrate calculation.
36  *#*
37  *#* Revision 1.1  2004/10/13 16:35:36  batt
38  *#* New (simple) serial driver.
39  *#*/
40 #include "ser_simple.h"
41
42 #include <compiler.h>
43 #include <config.h>
44 #include <macros.h> /* BV() */
45 #include <hw.h>
46
47 #include <avr/io.h>
48
49 /*!
50  * Send a character over the serial line.
51  *
52  * \return the character sent.
53  */
54 int _ser_putchar(int c)
55 {
56         /* Disable Rx to avoid echo*/
57         UCSR0B &= ~BV(RXEN);
58         /* Enable tx*/
59         UCSR0B |= BV(TXEN);
60         /* Prepare transmission */
61         UDR0 = c;
62         /* Wait until byte sent */
63         while (!(UCSR0A & BV(TXC))) {}
64         /* Disable tx to avoid short circuit when tx and rx share the same wire. */
65         UCSR0B &= ~BV(TXEN);
66         /* Enable Rx */
67         UCSR0B |= BV(RXEN);
68         /* Delete TRANSMIT_COMPLETE_BIT flag */
69         UCSR0A |= BV(TXC);
70         return c;
71 }
72
73
74 /*!
75  * Get a character from the serial line.
76  * If ther is no character in the buffer this function wait until
77  * one is received (no timeout).
78  *
79  * \return the character received.
80  */
81 int _ser_getchar(void)
82 {
83         /* Wait for data */
84         while (!(UCSR0A & BV(RXC))) {}
85         return UDR0;
86
87 }
88
89
90 /*!
91  * Get a character from the receiver buffer
92  * If the buffer is empty, ser_getchar_nowait() returns
93  * immediatly EOF.
94  */
95 int _ser_getchar_nowait(void)
96 {
97         if (!(UCSR0A & BV(RXC))) return EOF;
98         else return UDR0;
99 }
100
101 void _ser_settimeouts(void)
102 {
103 }
104
105 /*!
106  * Set the baudrate.
107  */
108 void _ser_setbaudrate(unsigned long rate)
109 {
110         /* Compute baud-rate period */
111         uint16_t period = (((CLOCK_FREQ / 16UL) + (rate / 2)) / rate) - 1;
112
113         UBRR0H = (period) >> 8;
114         UBRR0L = (period);
115 }
116
117 /*!
118  * Send a string.
119  */
120 int _ser_print(const char *s)
121 {
122         while(*s) _ser_putchar(*s++);
123         return 0;
124 }
125
126
127 void _ser_setparity(int parity)
128 {
129         /* Set the new parity */
130         UCSR0C |= (UCSR0C & ~(BV(UPM1) | BV(UPM0))) | (parity << UPM0); 
131 }
132
133 /*!
134  * Dummy functions.
135  */
136 void _ser_purge(void)
137 {
138         while (_ser_getchar_nowait() != EOF) {}
139 }
140
141 /*!
142  * Initialize serial.
143  */
144 struct Serial * _ser_open(void)
145 {
146         /*
147          * Set Rx and Tx pins as input to avoid short
148          * circuit when serial is disabled.
149          */
150         DDRE  &= ~(BV(PE0)|BV(PE1));
151         PORTE &= ~BV(PE0);
152         PORTE |=  BV(PE1);
153         /* Enable only Rx section */
154         UCSR0B = BV(RXEN);
155         return NULL;
156 }
157
158
159 /*!
160  * Clean up serial port, disabling the associated hardware.
161  */
162 void _ser_close(void)
163 {
164         /* Disable Rx & Tx. */
165         UCSR0B &= ~(BV(RXEN) | BV(TXEN));
166 }