Specific the directory for all hw and cfg module. Use double quote for cfg and hw...
[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 #warning FIXME:This module is obsolete, yuo must refactor it.
41
42 #if 0
43 #include "ser_simple_avr.h"
44
45 #include <cfg/compiler.h>
46 #include <appconfig.h>
47 #include <cfg/macros.h> /* BV() */
48 #include "hw/hw_cpu.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 = DIV_ROUND(CLOCK_FREQ / 16UL, 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 }
170
171 #endif
172