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