usart0avr.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2007 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  * The 9-bit communication had been contributed by Brett Abbott,
00033  * Digital Telemetry Limited.
00034  *
00035  * Dave Smart contributed the synchronous mode support.
00036  */
00037 
00038 /*
00039  * $Log: usart0avr.c,v $
00040  * Revision 1.4  2007/08/29 07:43:53  haraldkipp
00041  * Documentation updated and corrected.
00042  *
00043  * Revision 1.3  2007/02/15 16:19:14  haraldkipp
00044  * Can use PORTG for half duplex control.
00045  *
00046  * Revision 1.2  2006/02/08 15:18:49  haraldkipp
00047  * ATmega2561 Support
00048  *
00049  * Revision 1.1  2005/07/26 18:02:40  haraldkipp
00050  * Moved from dev.
00051  *
00052  * Revision 1.9  2005/07/22 08:07:08  freckle
00053  * added experimental improvements to usart driver. see ChangeLog for details
00054  *
00055  * Revision 1.8  2005/02/16 19:50:28  haraldkipp
00056  * Enable tracer configuration.
00057  *
00058  * Revision 1.7  2005/02/10 07:06:18  hwmaier
00059  * Changes to incorporate support for AT90CAN128 CPU
00060  *
00061  * Revision 1.6  2005/01/24 22:34:49  freckle
00062  * Added new tracer by Phlipp Blum <blum@tik.ee.ethz.ch>
00063  *
00064  * Revision 1.5  2005/01/22 19:25:32  haraldkipp
00065  * Changed AVR port configuration names from PORTx to AVRPORTx.
00066  *
00067  * Revision 1.4  2004/10/22 18:04:35  freckle
00068  * added #ifdef check to support old-style CTS definition
00069  * (old style: setting CTS_SIGNAL, CTS_BIT, CTS_PORT, CTS_PIN and CTS_DDR)
00070  * instead of the new single CTS_IRQ definition
00071  *
00072  * Revision 1.3  2004/09/22 08:14:48  haraldkipp
00073  * Made configurable
00074  *
00075  * Revision 1.2  2004/05/24 20:17:15  drsung
00076  * Added function UsartSize to return number of chars in input buffer.
00077  *
00078  * Revision 1.1  2003/12/15 19:25:33  haraldkipp
00079  * New USART driver added
00080  *
00081  */
00082 
00083 #include <cfg/os.h>
00084 #include <cfg/arch/avr.h>
00085 
00086 #include <string.h>
00087 
00088 #include <sys/atom.h>
00089 #include <sys/event.h>
00090 #include <sys/timer.h>
00091 
00092 #include <dev/irqreg.h>
00093 #include <dev/usartavr.h>
00094 
00100 #ifdef UART0_RTS_BIT
00101 
00102 #if (UART0_RTS_AVRPORT == AVRPORTB)
00103 #define UART_RTS_PORT  PORTB
00104 #define UART_RTS_DDR   DDRB
00105 
00106 #elif (UART0_RTS_AVRPORT == AVRPORTD)
00107 #define UART_RTS_PORT  PORTD
00108 #define UART_RTS_DDR   DDRD
00109 
00110 #elif (UART0_RTS_AVRPORT == AVRPORTE)
00111 #define UART_RTS_PORT  PORTE
00112 #define UART_RTS_DDR   DDRE
00113 
00114 #elif (UART0_RTS_AVRPORT == AVRPORTF)
00115 #define UART_RTS_PORT  PORTF
00116 #define UART_RTS_DDR   DDRF
00117 #endif
00118 
00119 #define UART_RTS_BIT    UART0_RTS_BIT
00120 
00121 #endif /* UART0_RTS_BIT */
00122 
00128 #ifdef UART0_HDX_BIT
00129 
00130 #if (UART0_HDX_AVRPORT == AVRPORTB)
00131 #define UART_HDX_PORT  PORTB
00132 #define UART_HDX_DDR   DDRB
00133 
00134 #elif (UART0_HDX_AVRPORT == AVRPORTD)
00135 #define UART_HDX_PORT  PORTD
00136 #define UART_HDX_DDR   DDRD
00137 
00138 #elif (UART0_HDX_AVRPORT == AVRPORTE)
00139 #define UART_HDX_PORT  PORTE
00140 #define UART_HDX_DDR   DDRE
00141 
00142 #elif (UART0_HDX_AVRPORT == AVRPORTF)
00143 #define UART_HDX_PORT  PORTF
00144 #define UART_HDX_DDR   DDRF
00145 
00146 #elif (UART0_HDX_AVRPORT == AVRPORTG)
00147 #define UART_HDX_PORT  PORTG
00148 #define UART_HDX_DDR   DDRG
00149 
00150 #endif
00151 #define UART_HDX_BIT    UART0_HDX_BIT
00152 
00153 #endif /* UART0_HDX_BIT */
00154 
00155 
00156 /*
00157  * Local function prototypes.
00158  */
00159 static u_long AvrUsartGetSpeed(void);
00160 static int AvrUsartSetSpeed(u_long rate);
00161 static u_char AvrUsartGetDataBits(void);
00162 static int AvrUsartSetDataBits(u_char bits);
00163 static u_char AvrUsartGetParity(void);
00164 static int AvrUsartSetParity(u_char mode);
00165 static u_char AvrUsartGetStopBits(void);
00166 static int AvrUsartSetStopBits(u_char bits);
00167 static u_long AvrUsartGetFlowControl(void);
00168 static int AvrUsartSetFlowControl(u_long flags);
00169 static u_long AvrUsartGetStatus(void);
00170 static int AvrUsartSetStatus(u_long flags);
00171 static u_char AvrUsartGetClockMode(void);
00172 static int AvrUsartSetClockMode(u_char mode);
00173 static void AvrUsartTxStart(void);
00174 static void AvrUsartRxStart(void);
00175 static int AvrUsartInit(void);
00176 static int AvrUsartDeinit(void);
00177 
00182 
00186 static USARTDCB dcb_usart0 = {
00187     0,                          /* dcb_modeflags */
00188     0,                          /* dcb_statusflags */
00189     0,                          /* dcb_rtimeout */
00190     0,                          /* dcb_wtimeout */
00191     {0, 0, 0, 0, 0, 0, 0, 0},   /* dcb_tx_rbf */
00192     {0, 0, 0, 0, 0, 0, 0, 0},   /* dcb_rx_rbf */
00193     0,                          /* dbc_last_eol */
00194     AvrUsartInit,               /* dcb_init */
00195     AvrUsartDeinit,             /* dcb_deinit */
00196     AvrUsartTxStart,            /* dcb_tx_start */
00197     AvrUsartRxStart,            /* dcb_rx_start */
00198     AvrUsartSetFlowControl,     /* dcb_set_flow_control */
00199     AvrUsartGetFlowControl,     /* dcb_get_flow_control */
00200     AvrUsartSetSpeed,           /* dcb_set_speed */
00201     AvrUsartGetSpeed,           /* dcb_get_speed */
00202     AvrUsartSetDataBits,        /* dcb_set_data_bits */
00203     AvrUsartGetDataBits,        /* dcb_get_data_bits */
00204     AvrUsartSetParity,          /* dcb_set_parity */
00205     AvrUsartGetParity,          /* dcb_get_parity */
00206     AvrUsartSetStopBits,        /* dcb_set_stop_bits */
00207     AvrUsartGetStopBits,        /* dcb_get_stop_bits */
00208     AvrUsartSetStatus,          /* dcb_set_status */
00209     AvrUsartGetStatus,          /* dcb_get_status */
00210     AvrUsartSetClockMode,       /* dcb_set_clock_mode */
00211     AvrUsartGetClockMode,       /* dcb_get_clock_mode */
00212 };
00213 
00229 NUTDEVICE devUsartAvr0 = {
00230     0,                          /* Pointer to next device, dev_next. */
00231     {'u', 'a', 'r', 't', '0', 0, 0, 0, 0},    /* Unique device name, dev_name. */
00232     IFTYP_CHAR,                 /* Type of device, dev_type. */
00233     0,                          /* Base address, dev_base (not used). */
00234     0,                          /* First interrupt number, dev_irq (not used). */
00235     0,                          /* Interface control block, dev_icb (not used). */
00236     &dcb_usart0,                /* Driver control block, dev_dcb. */
00237     UsartInit,                  /* Driver initialization routine, dev_init. */
00238     UsartIOCtl,                 /* Driver specific control function, dev_ioctl. */
00239     UsartRead,                  /* Read from device, dev_read. */
00240     UsartWrite,                 /* Write to device, dev_write. */
00241     UsartWrite_P,               /* Write data from program space to device, dev_write_P. */
00242     UsartOpen,                  /* Open a device or file, dev_open. */
00243     UsartClose,                 /* Close a device or file, dev_close. */
00244     UsartSize                   /* Request file size, dev_size. */
00245 };
00255 // added extra ifdef as test below is true even if UART0_CTS_IRQ is undef
00256 #ifdef UART0_CTS_IRQ
00257 
00258 #if (UART0_CTS_IRQ == INT0)
00259 #define UART_CTS_SIGNAL sig_INTERRUPT0
00260 #define UART_CTS_BIT    0
00261 #define UART_CTS_PORT   PORTD
00262 #define UART_CTS_PIN    PIND
00263 #define UART_CTS_DDR    DDRD
00264 
00265 #elif (UART0_CTS_IRQ == INT1)
00266 #define UART_CTS_SIGNAL sig_INTERRUPT1
00267 #define UART_CTS_BIT    1
00268 #define UART_CTS_PORT   PORTD
00269 #define UART_CTS_PIN    PIND
00270 #define UART_CTS_DDR    DDRD
00271 
00272 #elif (UART0_CTS_IRQ == INT2)
00273 #define UART_CTS_SIGNAL sig_INTERRUPT2
00274 #define UART_CTS_BIT    2
00275 #define UART_CTS_PORT   PORTD
00276 #define UART_CTS_PIN    PIND
00277 #define UART_CTS_DDR    DDRD
00278 
00279 #elif (UART0_CTS_IRQ == INT3)
00280 #define UART_CTS_SIGNAL sig_INTERRUPT3
00281 #define UART_CTS_BIT    3
00282 #define UART_CTS_PORT   PORTD
00283 #define UART_CTS_PIN    PIND
00284 #define UART_CTS_DDR    DDRD
00285 
00286 #elif (UART0_CTS_IRQ == INT4)
00287 #define UART_CTS_SIGNAL sig_INTERRUPT4
00288 #define UART_CTS_BIT    4
00289 #define UART_CTS_PORT   PORTE
00290 #define UART_CTS_PIN    PINE
00291 #define UART_CTS_DDR    DDRE
00292 
00293 #elif (UART0_CTS_IRQ == INT5)
00294 #define UART_CTS_SIGNAL sig_INTERRUPT5
00295 #define UART_CTS_BIT    5
00296 #define UART_CTS_PORT   PORTE
00297 #define UART_CTS_PIN    PINE
00298 #define UART_CTS_DDR    DDRE
00299 
00300 #elif (UART0_CTS_IRQ == INT6)
00301 #define UART_CTS_SIGNAL sig_INTERRUPT6
00302 #define UART_CTS_BIT    6
00303 #define UART_CTS_PORT   PORTE
00304 #define UART_CTS_PIN    PINE
00305 #define UART_CTS_DDR    DDRE
00306 
00307 #elif (UART0_CTS_IRQ == INT7)
00308 #define UART_CTS_SIGNAL sig_INTERRUPT7
00309 #define UART_CTS_BIT    7
00310 #define UART_CTS_PORT   PORTE
00311 #define UART_CTS_PIN    PINE
00312 #define UART_CTS_DDR    DDRE
00313 
00314 #endif
00315 
00316 #else
00317 
00318 // alternate way to specify the cts line
00319 #define UART_CTS_PORT   UART0_CTS_PORT
00320 #define UART_CTS_PIN    UART0_CTS_PIN
00321 #define UART_CTS_DDR    UART0_CTS_DDR
00322 // only set CTS_BIT if used and IRQ available
00323 #ifdef UART1_CTS_BIT
00324 #define UART_CTS_SIGNAL UART0_CTS_SIGNAL
00325 #define UART_CTS_BIT    UART0_CTS_BIT
00326 #endif
00327 
00328 #endif
00329 
00330 
00334 #ifdef __AVR_ENHANCED__
00335 
00336 #define UDRn    UDR0
00337 #define UCSRnA  UCSR0A
00338 #define UCSRnB  UCSR0B
00339 #define UCSRnC  UCSR0C
00340 #define UBRRnL  UBRR0L
00341 #define UBRRnH  UBRR0H
00342 
00343 #ifdef __IMAGECRAFT__
00344 #define TXB8    TXB80
00345 #ifdef ATMega2561
00346 #define UMSEL   UMSEL00
00347 #else
00348 #define UMSEL   UMSEL0
00349 #endif
00350 #define U2X     U2X0
00351 #define UCSZ2   UCSZ02
00352 #define UCSZ1   UCSZ01
00353 #define UCSZ0   UCSZ00
00354 #define UPM0    UPM00
00355 #define UPM1    UPM01
00356 #define USBS    USBS0
00357 #define UPE     UPE0
00358 #define MPCM    MPCM0
00359 #define UCPOL   UCPOL0
00360 #endif
00361 
00362 #else
00363 
00364 #define UDRn    UDR
00365 #define UCSRnA  USR
00366 #define UCSRnB  UCR
00367 #define UBRRnL  UBRR
00368 #define UCSZ2   CHR9
00369 
00370 #endif
00371 
00372 #define sig_UART_RECV   sig_UART0_RECV
00373 #define sig_UART_DATA   sig_UART0_DATA
00374 #define sig_UART_TRANS  sig_UART0_TRANS
00375 
00376 #ifndef SIG_UART_RECV
00377 #define SIG_UART_RECV   SIG_UART0_RECV
00378 #endif
00379 #ifndef SIG_UART_DATA
00380 #define SIG_UART_DATA   SIG_UART0_DATA
00381 #endif
00382 #ifndef SIG_UART_TRANS
00383 #define SIG_UART_TRANS  SIG_UART0_TRANS
00384 #endif
00385 
00386 #define dcb_usart   dcb_usart0
00387 
00388 #ifdef NUTTRACER
00389 #define TRACE_INT_UART_CTS TRACE_INT_UART0_CTS
00390 #define TRACE_INT_UART_RXCOMPL TRACE_INT_UART0_RXCOMPL
00391 #define TRACE_INT_UART_TXEMPTY TRACE_INT_UART0_TXEMPTY
00392 #endif
00393 
00394 #ifdef UART0_READMULTIBYTE
00395 #define UART_READMULTIBYTE
00396 #endif
00397 
00398 #ifdef USE_USART0
00399 #define USE_USART
00400 #endif
00401 
00402 #ifdef UART0_NO_SW_FLOWCONTROL
00403 #define UART_NO_SW_FLOWCONTROL
00404 #endif
00405 
00408 #include "usartavr.c"

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/