Refactor to use new protocol module and sipo.
[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  * \author Francesco Sacchi <batt@develer.com>
36  */
37
38
39 #warning FIXME:This module is obsolete, yuo must refactor it.
40
41 #if 0
42 #include "ser_simple_avr.h"
43
44 #include <cfg/compiler.h>
45 #include <appconfig.h>
46 #include <cfg/macros.h> /* BV() */
47 #include <hw/hw_cpufreq.h>
48
49 #include <avr/io.h>
50
51 /**
52  * Send a character over the serial line.
53  *
54  * \return the character sent.
55  */
56 int _ser_putchar(int c)
57 {
58         /* Disable Rx to avoid echo*/
59         UCSR0B &= ~BV(RXEN);
60         /* Enable tx*/
61         UCSR0B |= BV(TXEN);
62         /* Prepare transmission */
63         UDR0 = c;
64         /* Wait until byte sent */
65         while (!(UCSR0A & BV(TXC))) {}
66         /* Disable tx to avoid short circuit when tx and rx share the same wire. */
67         UCSR0B &= ~BV(TXEN);
68         /* Enable Rx */
69         UCSR0B |= BV(RXEN);
70         /* Delete TRANSMIT_COMPLETE_BIT flag */
71         UCSR0A |= BV(TXC);
72         return c;
73 }
74
75
76 /**
77  * Get a character from the serial line.
78  * If ther is no character in the buffer this function wait until
79  * one is received (no timeout).
80  *
81  * \return the character received.
82  */
83 int _ser_getchar(void)
84 {
85         /* Wait for data */
86         while (!(UCSR0A & BV(RXC))) {}
87         return UDR0;
88
89 }
90
91
92 /**
93  * Get a character from the receiver buffer
94  * If the buffer is empty, ser_getchar_nowait() returns
95  * immediatly EOF.
96  */
97 int _ser_getchar_nowait(void)
98 {
99         if (!(UCSR0A & BV(RXC))) return EOF;
100         else return UDR0;
101 }
102
103 void _ser_settimeouts(void)
104 {
105 }
106
107 /**
108  * Set the baudrate.
109  */
110 void _ser_setbaudrate(unsigned long rate)
111 {
112         /* Compute baud-rate period */
113         uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
114
115         UBRR0H = (period) >> 8;
116         UBRR0L = (period);
117 }
118
119 /**
120  * Send a string.
121  */
122 int _ser_print(const char *s)
123 {
124         while(*s) _ser_putchar(*s++);
125         return 0;
126 }
127
128
129 void _ser_setparity(int parity)
130 {
131         /* Set the new parity */
132         UCSR0C |= (UCSR0C & ~(BV(UPM1) | BV(UPM0))) | (parity << UPM0); 
133 }
134
135 /**
136  * Dummy functions.
137  */
138 void _ser_purge(void)
139 {
140         while (_ser_getchar_nowait() != EOF) {}
141 }
142
143 /**
144  * Initialize serial.
145  */
146 struct Serial * _ser_open(void)
147 {
148         /*
149          * Set Rx and Tx pins as input to avoid short
150          * circuit when serial is disabled.
151          */
152         DDRE  &= ~(BV(PE0)|BV(PE1));
153         PORTE &= ~BV(PE0);
154         PORTE |=  BV(PE1);
155         /* Enable only Rx section */
156         UCSR0B = BV(RXEN);
157         return NULL;
158 }
159
160
161 /**
162  * Clean up serial port, disabling the associated hardware.
163  */
164 void _ser_close(void)
165 {
166         /* Disable Rx & Tx. */
167         UCSR0B &= ~(BV(RXEN) | BV(TXEN));
168 }
169
170 #endif
171