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
35 * \author Francesco Sacchi <batt@develer.com>
39 #warning FIXME:This module is obsolete, yuo must refactor it.
42 #include "ser_simple_avr.h"
44 #include <cfg/compiler.h>
45 #include <appconfig.h>
46 #include <cfg/macros.h> /* BV() */
47 #include <hw/hw_cpufreq.h>
52 * Send a character over the serial line.
54 * \return the character sent.
56 int _ser_putchar(int c)
58 /* Disable Rx to avoid echo*/
62 /* Prepare transmission */
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. */
70 /* Delete TRANSMIT_COMPLETE_BIT flag */
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).
81 * \return the character received.
83 int _ser_getchar(void)
86 while (!(UCSR0A & BV(RXC))) {}
93 * Get a character from the receiver buffer
94 * If the buffer is empty, ser_getchar_nowait() returns
97 int _ser_getchar_nowait(void)
99 if (!(UCSR0A & BV(RXC))) return EOF;
103 void _ser_settimeouts(void)
110 void _ser_setbaudrate(unsigned long rate)
112 /* Compute baud-rate period */
113 uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, rate) - 1;
115 UBRR0H = (period) >> 8;
122 int _ser_print(const char *s)
124 while(*s) _ser_putchar(*s++);
129 void _ser_setparity(int parity)
131 /* Set the new parity */
132 UCSR0C |= (UCSR0C & ~(BV(UPM1) | BV(UPM0))) | (parity << UPM0);
138 void _ser_purge(void)
140 while (_ser_getchar_nowait() != EOF) {}
146 struct Serial * _ser_open(void)
149 * Set Rx and Tx pins as input to avoid short
150 * circuit when serial is disabled.
152 DDRE &= ~(BV(PE0)|BV(PE1));
155 /* Enable only Rx section */
162 * Clean up serial port, disabling the associated hardware.
164 void _ser_close(void)
166 /* Disable Rx & Tx. */
167 UCSR0B &= ~(BV(RXEN) | BV(TXEN));