4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30 * All Rights Reserved.
33 * \brief Simple serial I/O driver
36 * \author Francesco Sacchi <batt@develer.com>
41 *#* Revision 1.2 2006/07/19 12:56:26 bernie
42 *#* Convert to new Doxygen style.
44 *#* Revision 1.1 2005/04/12 01:37:50 bernie
45 *#* Import into DevLib.
47 *#* Revision 1.7 2005/01/23 12:24:27 bernie
48 *#* Include macros.h for BV().
50 *#* Revision 1.6 2004/10/20 13:40:54 batt
51 *#* Put {} instead of ; after while loop.
53 *#* Revision 1.5 2004/10/20 13:39:40 batt
56 *#* Revision 1.4 2004/10/20 13:30:02 batt
57 *#* Optimization of UCSR0C writing
59 *#* Revision 1.3 2004/10/14 15:55:32 batt
62 *#* Revision 1.2 2004/10/14 14:46:59 batt
63 *#* Change baudrate calculation.
65 *#* Revision 1.1 2004/10/13 16:35:36 batt
66 *#* New (simple) serial driver.
68 #include "ser_simple.h"
72 #include <macros.h> /* BV() */
78 * Send a character over the serial line.
80 * \return the character sent.
82 int _ser_putchar(int c)
84 /* Disable Rx to avoid echo*/
88 /* Prepare transmission */
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. */
96 /* Delete TRANSMIT_COMPLETE_BIT flag */
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).
107 * \return the character received.
109 int _ser_getchar(void)
112 while (!(UCSR0A & BV(RXC))) {}
119 * Get a character from the receiver buffer
120 * If the buffer is empty, ser_getchar_nowait() returns
123 int _ser_getchar_nowait(void)
125 if (!(UCSR0A & BV(RXC))) return EOF;
129 void _ser_settimeouts(void)
136 void _ser_setbaudrate(unsigned long rate)
138 /* Compute baud-rate period */
139 uint16_t period = (((CLOCK_FREQ / 16UL) + (rate / 2)) / rate) - 1;
141 UBRR0H = (period) >> 8;
148 int _ser_print(const char *s)
150 while(*s) _ser_putchar(*s++);
155 void _ser_setparity(int parity)
157 /* Set the new parity */
158 UCSR0C |= (UCSR0C & ~(BV(UPM1) | BV(UPM0))) | (parity << UPM0);
164 void _ser_purge(void)
166 while (_ser_getchar_nowait() != EOF) {}
172 struct Serial * _ser_open(void)
175 * Set Rx and Tx pins as input to avoid short
176 * circuit when serial is disabled.
178 DDRE &= ~(BV(PE0)|BV(PE1));
181 /* Enable only Rx section */
188 * Clean up serial port, disabling the associated hardware.
190 void _ser_close(void)
192 /* Disable Rx & Tx. */
193 UCSR0B &= ~(BV(RXEN) | BV(TXEN));