4 * Copyright 2004, 2005 Develer S.r.l. (http://www.develer.com/)
8 * \brief Simple serial I/O driver
11 * \author Francesco Sacchi <batt@develer.com>
16 *#* Revision 1.1 2005/04/12 01:37:50 bernie
17 *#* Import into DevLib.
19 *#* Revision 1.7 2005/01/23 12:24:27 bernie
20 *#* Include macros.h for BV().
22 *#* Revision 1.6 2004/10/20 13:40:54 batt
23 *#* Put {} instead of ; after while loop.
25 *#* Revision 1.5 2004/10/20 13:39:40 batt
28 *#* Revision 1.4 2004/10/20 13:30:02 batt
29 *#* Optimization of UCSR0C writing
31 *#* Revision 1.3 2004/10/14 15:55:32 batt
34 *#* Revision 1.2 2004/10/14 14:46:59 batt
35 *#* Change baudrate calculation.
37 *#* Revision 1.1 2004/10/13 16:35:36 batt
38 *#* New (simple) serial driver.
40 #include "ser_simple.h"
44 #include <macros.h> /* BV() */
50 * Send a character over the serial line.
52 * \return the character sent.
54 int _ser_putchar(int c)
56 /* Disable Rx to avoid echo*/
60 /* Prepare transmission */
62 /* Wait until byte sent */
63 while (!(UCSR0A & BV(TXC))) {}
64 /* Disable tx to avoid short circuit when tx and rx share the same wire. */
68 /* Delete TRANSMIT_COMPLETE_BIT flag */
75 * Get a character from the serial line.
76 * If ther is no character in the buffer this function wait until
77 * one is received (no timeout).
79 * \return the character received.
81 int _ser_getchar(void)
84 while (!(UCSR0A & BV(RXC))) {}
91 * Get a character from the receiver buffer
92 * If the buffer is empty, ser_getchar_nowait() returns
95 int _ser_getchar_nowait(void)
97 if (!(UCSR0A & BV(RXC))) return EOF;
101 void _ser_settimeouts(void)
108 void _ser_setbaudrate(unsigned long rate)
110 /* Compute baud-rate period */
111 uint16_t period = (((CLOCK_FREQ / 16UL) + (rate / 2)) / rate) - 1;
113 UBRR0H = (period) >> 8;
120 int _ser_print(const char *s)
122 while(*s) _ser_putchar(*s++);
127 void _ser_setparity(int parity)
129 /* Set the new parity */
130 UCSR0C |= (UCSR0C & ~(BV(UPM1) | BV(UPM0))) | (parity << UPM0);
136 void _ser_purge(void)
138 while (_ser_getchar_nowait() != EOF) {}
144 struct Serial * _ser_open(void)
147 * Set Rx and Tx pins as input to avoid short
148 * circuit when serial is disabled.
150 DDRE &= ~(BV(PE0)|BV(PE1));
153 /* Enable only Rx section */
160 * Clean up serial port, disabling the associated hardware.
162 void _ser_close(void)
164 /* Disable Rx & Tx. */
165 UCSR0B &= ~(BV(RXEN) | BV(TXEN));