From: batt Date: Thu, 11 Oct 2007 15:36:07 +0000 (+0000) Subject: Rename avr simple serial driver. X-Git-Tag: 1.0.0~375 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=187d609e26aacfd7073ddaa5a9c761c5e9a30765;p=bertos.git Rename avr simple serial driver. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@866 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/drv/ser_simple.c b/drv/ser_simple.c deleted file mode 100644 index 7805892a..00000000 --- a/drv/ser_simple.c +++ /dev/null @@ -1,194 +0,0 @@ -/** - * \file - * - * - * \brief Simple serial I/O driver - * - * \version $Id$ - * \author Francesco Sacchi - */ - -/*#* - *#* $Log$ - *#* Revision 1.2 2006/07/19 12:56:26 bernie - *#* Convert to new Doxygen style. - *#* - *#* Revision 1.1 2005/04/12 01:37:50 bernie - *#* Import into DevLib. - *#* - *#* Revision 1.7 2005/01/23 12:24:27 bernie - *#* Include macros.h for BV(). - *#* - *#* Revision 1.6 2004/10/20 13:40:54 batt - *#* Put {} instead of ; after while loop. - *#* - *#* Revision 1.5 2004/10/20 13:39:40 batt - *#* Reformat. - *#* - *#* Revision 1.4 2004/10/20 13:30:02 batt - *#* Optimization of UCSR0C writing - *#* - *#* Revision 1.3 2004/10/14 15:55:32 batt - *#* Add ser_purge. - *#* - *#* Revision 1.2 2004/10/14 14:46:59 batt - *#* Change baudrate calculation. - *#* - *#* Revision 1.1 2004/10/13 16:35:36 batt - *#* New (simple) serial driver. - *#*/ -#include "ser_simple.h" - -#include -#include -#include /* BV() */ -#include - -#include - -/** - * Send a character over the serial line. - * - * \return the character sent. - */ -int _ser_putchar(int c) -{ - /* Disable Rx to avoid echo*/ - UCSR0B &= ~BV(RXEN); - /* Enable tx*/ - UCSR0B |= BV(TXEN); - /* Prepare transmission */ - UDR0 = c; - /* Wait until byte sent */ - while (!(UCSR0A & BV(TXC))) {} - /* Disable tx to avoid short circuit when tx and rx share the same wire. */ - UCSR0B &= ~BV(TXEN); - /* Enable Rx */ - UCSR0B |= BV(RXEN); - /* Delete TRANSMIT_COMPLETE_BIT flag */ - UCSR0A |= BV(TXC); - return c; -} - - -/** - * Get a character from the serial line. - * If ther is no character in the buffer this function wait until - * one is received (no timeout). - * - * \return the character received. - */ -int _ser_getchar(void) -{ - /* Wait for data */ - while (!(UCSR0A & BV(RXC))) {} - return UDR0; - -} - - -/** - * Get a character from the receiver buffer - * If the buffer is empty, ser_getchar_nowait() returns - * immediatly EOF. - */ -int _ser_getchar_nowait(void) -{ - if (!(UCSR0A & BV(RXC))) return EOF; - else return UDR0; -} - -void _ser_settimeouts(void) -{ -} - -/** - * Set the baudrate. - */ -void _ser_setbaudrate(unsigned long rate) -{ - /* Compute baud-rate period */ - uint16_t period = (((CLOCK_FREQ / 16UL) + (rate / 2)) / rate) - 1; - - UBRR0H = (period) >> 8; - UBRR0L = (period); -} - -/** - * Send a string. - */ -int _ser_print(const char *s) -{ - while(*s) _ser_putchar(*s++); - return 0; -} - - -void _ser_setparity(int parity) -{ - /* Set the new parity */ - UCSR0C |= (UCSR0C & ~(BV(UPM1) | BV(UPM0))) | (parity << UPM0); -} - -/** - * Dummy functions. - */ -void _ser_purge(void) -{ - while (_ser_getchar_nowait() != EOF) {} -} - -/** - * Initialize serial. - */ -struct Serial * _ser_open(void) -{ - /* - * Set Rx and Tx pins as input to avoid short - * circuit when serial is disabled. - */ - DDRE &= ~(BV(PE0)|BV(PE1)); - PORTE &= ~BV(PE0); - PORTE |= BV(PE1); - /* Enable only Rx section */ - UCSR0B = BV(RXEN); - return NULL; -} - - -/** - * Clean up serial port, disabling the associated hardware. - */ -void _ser_close(void) -{ - /* Disable Rx & Tx. */ - UCSR0B &= ~(BV(RXEN) | BV(TXEN)); -} diff --git a/drv/ser_simple.h b/drv/ser_simple.h deleted file mode 100644 index ec4cf604..00000000 --- a/drv/ser_simple.h +++ /dev/null @@ -1,171 +0,0 @@ -/** - * \file - * - * - * \brief Simple serial I/O driver - * - * \version $Id$ - * \author Bernardo Innocenti - * \author Francesco Sacchi - */ - -/*#* - *#* $Log$ - *#* Revision 1.2 2006/07/19 12:56:26 bernie - *#* Convert to new Doxygen style. - *#* - *#* Revision 1.1 2005/04/12 01:37:50 bernie - *#* Import into DevLib. - *#* - *#* Revision 1.5 2004/10/20 13:37:49 batt - *#* Change testing of simple serial instead of ARCH_BOOT in sc driver. - *#* - *#* Revision 1.4 2004/10/15 12:22:04 batt - *#* Readd ';' in setstatus macro. - *#* - *#* Revision 1.3 2004/10/15 12:13:57 batt - *#* Correct \brief header. - *#* - *#* Revision 1.2 2004/10/15 11:54:21 batt - *#* Reformat. - *#* - *#* Revision 1.1 2004/10/13 16:35:36 batt - *#* New (simple) serial driver. - *#* - *#* - */ -#ifndef SER_SIMPLE_H -#define SER_SIMPLE_H - -/* For checking which serial driver is linked */ -#define SER_SIMPLE - -#include -#include - - -#if 0 -#if CPU_AVR - typedef uint8_t serstatus_t; - - /* Software errors */ - #define SERRF_RXFIFOOVERRUN BV(0) /**< Rx FIFO buffer overrun */ - #define SERRF_RXTIMEOUT BV(5) /**< Receive timeout */ - #define SERRF_TXTIMEOUT BV(6) /**< Transmit timeout */ - - /* Hardware errors */ - #define SERRF_RXSROVERRUN BV(3) /**< Rx shift register overrun */ - #define SERRF_FRAMEERROR BV(4) /**< Stop bit missing */ - #define SERRF_PARITYERROR BV(7) /**< Parity error */ -#else - #error unknown architecture -#endif -/*\}*/ - -/** - * \name Serial hw numbers - * - * \{ - */ -enum -{ -#if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 - SER_UART0, - SER_UART1, - SER_SPI, -#elif CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA8 - SER_UART0, - SER_SPI, -#else - #error unknown architecture -#endif - SER_CNT /**< Number of serial ports */ -}; -/*\}*/ -#endif - -/** \name Parity settings for ser_setparity() */ -/*\{*/ -#define SER_PARITY_NONE 0 -#define SER_PARITY_EVEN 2 -#define SER_PARITY_ODD 3 -/*\}*/ - - -/** Serial handle structure */ -struct Serial; - -/* Function prototypes */ -extern int _ser_putchar(int c); -extern int _ser_getchar(void); -extern int _ser_getchar_nowait(void); -/* -extern int ser_write(struct Serial *port, const void *buf, size_t len); -extern int ser_read(struct Serial *port, void *buf, size_t size); - -extern int ser_printf(struct Serial *port, const char *format, ...) FORMAT(__printf__, 2, 3); - -extern int ser_gets(struct Serial *port, char *buf, int size); -extern int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo); -*/ -extern int _ser_print(const char *s); - -extern void _ser_setbaudrate(unsigned long rate); -extern void _ser_setparity(int parity); -extern void _ser_settimeouts(void); -extern void _ser_setstatus(void); -/* -extern void ser_resync(struct Serial *port, time_t delay); -extern void ser_drain(struct Serial *port); -*/ -extern void _ser_purge(void); -extern struct Serial *_ser_open(void); -extern void _ser_close(void); - -/** - * \name Functions implemented as macros - * - * \{ - */ -#define ser_putchar(c, port) _ser_putchar(c) -#define ser_getchar(port) _ser_getchar() -#define ser_getchar_nowait(port) _ser_getchar_nowait() -#define ser_print(port, s) _ser_print(s) -#define ser_setbaudrate(port, y) _ser_setbaudrate(y) -#define ser_setparity(port, par) _ser_setparity(par) -#define ser_settimeouts(port, y, z) _ser_settimeouts() -#define ser_purge(port) _ser_purge() -#define ser_open(port) _ser_open() -#define ser_getstatus(h) 0 -#define ser_setstatus(h, x) do {(void)(x);} while(0) -/* \} */ - -#endif /* SER_SIMPLE_H */ diff --git a/drv/ser_simple_avr.c b/drv/ser_simple_avr.c new file mode 100644 index 00000000..7805892a --- /dev/null +++ b/drv/ser_simple_avr.c @@ -0,0 +1,194 @@ +/** + * \file + * + * + * \brief Simple serial I/O driver + * + * \version $Id$ + * \author Francesco Sacchi + */ + +/*#* + *#* $Log$ + *#* Revision 1.2 2006/07/19 12:56:26 bernie + *#* Convert to new Doxygen style. + *#* + *#* Revision 1.1 2005/04/12 01:37:50 bernie + *#* Import into DevLib. + *#* + *#* Revision 1.7 2005/01/23 12:24:27 bernie + *#* Include macros.h for BV(). + *#* + *#* Revision 1.6 2004/10/20 13:40:54 batt + *#* Put {} instead of ; after while loop. + *#* + *#* Revision 1.5 2004/10/20 13:39:40 batt + *#* Reformat. + *#* + *#* Revision 1.4 2004/10/20 13:30:02 batt + *#* Optimization of UCSR0C writing + *#* + *#* Revision 1.3 2004/10/14 15:55:32 batt + *#* Add ser_purge. + *#* + *#* Revision 1.2 2004/10/14 14:46:59 batt + *#* Change baudrate calculation. + *#* + *#* Revision 1.1 2004/10/13 16:35:36 batt + *#* New (simple) serial driver. + *#*/ +#include "ser_simple.h" + +#include +#include +#include /* BV() */ +#include + +#include + +/** + * Send a character over the serial line. + * + * \return the character sent. + */ +int _ser_putchar(int c) +{ + /* Disable Rx to avoid echo*/ + UCSR0B &= ~BV(RXEN); + /* Enable tx*/ + UCSR0B |= BV(TXEN); + /* Prepare transmission */ + UDR0 = c; + /* Wait until byte sent */ + while (!(UCSR0A & BV(TXC))) {} + /* Disable tx to avoid short circuit when tx and rx share the same wire. */ + UCSR0B &= ~BV(TXEN); + /* Enable Rx */ + UCSR0B |= BV(RXEN); + /* Delete TRANSMIT_COMPLETE_BIT flag */ + UCSR0A |= BV(TXC); + return c; +} + + +/** + * Get a character from the serial line. + * If ther is no character in the buffer this function wait until + * one is received (no timeout). + * + * \return the character received. + */ +int _ser_getchar(void) +{ + /* Wait for data */ + while (!(UCSR0A & BV(RXC))) {} + return UDR0; + +} + + +/** + * Get a character from the receiver buffer + * If the buffer is empty, ser_getchar_nowait() returns + * immediatly EOF. + */ +int _ser_getchar_nowait(void) +{ + if (!(UCSR0A & BV(RXC))) return EOF; + else return UDR0; +} + +void _ser_settimeouts(void) +{ +} + +/** + * Set the baudrate. + */ +void _ser_setbaudrate(unsigned long rate) +{ + /* Compute baud-rate period */ + uint16_t period = (((CLOCK_FREQ / 16UL) + (rate / 2)) / rate) - 1; + + UBRR0H = (period) >> 8; + UBRR0L = (period); +} + +/** + * Send a string. + */ +int _ser_print(const char *s) +{ + while(*s) _ser_putchar(*s++); + return 0; +} + + +void _ser_setparity(int parity) +{ + /* Set the new parity */ + UCSR0C |= (UCSR0C & ~(BV(UPM1) | BV(UPM0))) | (parity << UPM0); +} + +/** + * Dummy functions. + */ +void _ser_purge(void) +{ + while (_ser_getchar_nowait() != EOF) {} +} + +/** + * Initialize serial. + */ +struct Serial * _ser_open(void) +{ + /* + * Set Rx and Tx pins as input to avoid short + * circuit when serial is disabled. + */ + DDRE &= ~(BV(PE0)|BV(PE1)); + PORTE &= ~BV(PE0); + PORTE |= BV(PE1); + /* Enable only Rx section */ + UCSR0B = BV(RXEN); + return NULL; +} + + +/** + * Clean up serial port, disabling the associated hardware. + */ +void _ser_close(void) +{ + /* Disable Rx & Tx. */ + UCSR0B &= ~(BV(RXEN) | BV(TXEN)); +} diff --git a/drv/ser_simple_avr.h b/drv/ser_simple_avr.h new file mode 100644 index 00000000..ec4cf604 --- /dev/null +++ b/drv/ser_simple_avr.h @@ -0,0 +1,171 @@ +/** + * \file + * + * + * \brief Simple serial I/O driver + * + * \version $Id$ + * \author Bernardo Innocenti + * \author Francesco Sacchi + */ + +/*#* + *#* $Log$ + *#* Revision 1.2 2006/07/19 12:56:26 bernie + *#* Convert to new Doxygen style. + *#* + *#* Revision 1.1 2005/04/12 01:37:50 bernie + *#* Import into DevLib. + *#* + *#* Revision 1.5 2004/10/20 13:37:49 batt + *#* Change testing of simple serial instead of ARCH_BOOT in sc driver. + *#* + *#* Revision 1.4 2004/10/15 12:22:04 batt + *#* Readd ';' in setstatus macro. + *#* + *#* Revision 1.3 2004/10/15 12:13:57 batt + *#* Correct \brief header. + *#* + *#* Revision 1.2 2004/10/15 11:54:21 batt + *#* Reformat. + *#* + *#* Revision 1.1 2004/10/13 16:35:36 batt + *#* New (simple) serial driver. + *#* + *#* + */ +#ifndef SER_SIMPLE_H +#define SER_SIMPLE_H + +/* For checking which serial driver is linked */ +#define SER_SIMPLE + +#include +#include + + +#if 0 +#if CPU_AVR + typedef uint8_t serstatus_t; + + /* Software errors */ + #define SERRF_RXFIFOOVERRUN BV(0) /**< Rx FIFO buffer overrun */ + #define SERRF_RXTIMEOUT BV(5) /**< Receive timeout */ + #define SERRF_TXTIMEOUT BV(6) /**< Transmit timeout */ + + /* Hardware errors */ + #define SERRF_RXSROVERRUN BV(3) /**< Rx shift register overrun */ + #define SERRF_FRAMEERROR BV(4) /**< Stop bit missing */ + #define SERRF_PARITYERROR BV(7) /**< Parity error */ +#else + #error unknown architecture +#endif +/*\}*/ + +/** + * \name Serial hw numbers + * + * \{ + */ +enum +{ +#if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 + SER_UART0, + SER_UART1, + SER_SPI, +#elif CPU_AVR_ATMEGA103 || CPU_AVR_ATMEGA8 + SER_UART0, + SER_SPI, +#else + #error unknown architecture +#endif + SER_CNT /**< Number of serial ports */ +}; +/*\}*/ +#endif + +/** \name Parity settings for ser_setparity() */ +/*\{*/ +#define SER_PARITY_NONE 0 +#define SER_PARITY_EVEN 2 +#define SER_PARITY_ODD 3 +/*\}*/ + + +/** Serial handle structure */ +struct Serial; + +/* Function prototypes */ +extern int _ser_putchar(int c); +extern int _ser_getchar(void); +extern int _ser_getchar_nowait(void); +/* +extern int ser_write(struct Serial *port, const void *buf, size_t len); +extern int ser_read(struct Serial *port, void *buf, size_t size); + +extern int ser_printf(struct Serial *port, const char *format, ...) FORMAT(__printf__, 2, 3); + +extern int ser_gets(struct Serial *port, char *buf, int size); +extern int ser_gets_echo(struct Serial *port, char *buf, int size, bool echo); +*/ +extern int _ser_print(const char *s); + +extern void _ser_setbaudrate(unsigned long rate); +extern void _ser_setparity(int parity); +extern void _ser_settimeouts(void); +extern void _ser_setstatus(void); +/* +extern void ser_resync(struct Serial *port, time_t delay); +extern void ser_drain(struct Serial *port); +*/ +extern void _ser_purge(void); +extern struct Serial *_ser_open(void); +extern void _ser_close(void); + +/** + * \name Functions implemented as macros + * + * \{ + */ +#define ser_putchar(c, port) _ser_putchar(c) +#define ser_getchar(port) _ser_getchar() +#define ser_getchar_nowait(port) _ser_getchar_nowait() +#define ser_print(port, s) _ser_print(s) +#define ser_setbaudrate(port, y) _ser_setbaudrate(y) +#define ser_setparity(port, par) _ser_setparity(par) +#define ser_settimeouts(port, y, z) _ser_settimeouts() +#define ser_purge(port) _ser_purge() +#define ser_open(port) _ser_open() +#define ser_getstatus(h) 0 +#define ser_setstatus(h, x) do {(void)(x);} while(0) +/* \} */ + +#endif /* SER_SIMPLE_H */