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>
40 #warning FIXME:This module is obsolete, yuo must refactor it.
43 #include "ser_simple_avr.h"
45 #include <cfg/compiler.h>
46 #include <appconfig.h>
47 #include <cfg/macros.h> /* BV() */
53 * Send a character over the serial line.
55 * \return the character sent.
57 int _ser_putchar(int c)
59 /* Disable Rx to avoid echo*/
63 /* Prepare transmission */
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. */
71 /* Delete TRANSMIT_COMPLETE_BIT flag */
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).
82 * \return the character received.
84 int _ser_getchar(void)
87 while (!(UCSR0A & BV(RXC))) {}
94 * Get a character from the receiver buffer
95 * If the buffer is empty, ser_getchar_nowait() returns
98 int _ser_getchar_nowait(void)
100 if (!(UCSR0A & BV(RXC))) return EOF;
104 void _ser_settimeouts(void)
111 void _ser_setbaudrate(unsigned long rate)
113 /* Compute baud-rate period */
114 uint16_t period = DIV_ROUND(CLOCK_FREQ / 16UL, rate) - 1;
116 UBRR0H = (period) >> 8;
123 int _ser_print(const char *s)
125 while(*s) _ser_putchar(*s++);
130 void _ser_setparity(int parity)
132 /* Set the new parity */
133 UCSR0C |= (UCSR0C & ~(BV(UPM1) | BV(UPM0))) | (parity << UPM0);
139 void _ser_purge(void)
141 while (_ser_getchar_nowait() != EOF) {}
147 struct Serial * _ser_open(void)
150 * Set Rx and Tx pins as input to avoid short
151 * circuit when serial is disabled.
153 DDRE &= ~(BV(PE0)|BV(PE1));
156 /* Enable only Rx section */
163 * Clean up serial port, disabling the associated hardware.
165 void _ser_close(void)
167 /* Disable Rx & Tx. */
168 UCSR0B &= ~(BV(RXEN) | BV(TXEN));