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.5  2008/04/29 02:28:34  thiagocorrea
00041  * Add configurable DTR pin to AVR USART driver.
00042  *
00043  * Revision 1.4  2007/08/29 07:43:53  haraldkipp
00044  * Documentation updated and corrected.
00045  *
00046  * Revision 1.3  2007/02/15 16:19:14  haraldkipp
00047  * Can use PORTG for half duplex control.
00048  *
00049  * Revision 1.2  2006/02/08 15:18:49  haraldkipp
00050  * ATmega2561 Support
00051  *
00052  * Revision 1.1  2005/07/26 18:02:40  haraldkipp
00053  * Moved from dev.
00054  *
00055  * Revision 1.9  2005/07/22 08:07:08  freckle
00056  * added experimental improvements to usart driver. see ChangeLog for details
00057  *
00058  * Revision 1.8  2005/02/16 19:50:28  haraldkipp
00059  * Enable tracer configuration.
00060  *
00061  * Revision 1.7  2005/02/10 07:06:18  hwmaier
00062  * Changes to incorporate support for AT90CAN128 CPU
00063  *
00064  * Revision 1.6  2005/01/24 22:34:49  freckle
00065  * Added new tracer by Phlipp Blum <blum@tik.ee.ethz.ch>
00066  *
00067  * Revision 1.5  2005/01/22 19:25:32  haraldkipp
00068  * Changed AVR port configuration names from PORTx to AVRPORTx.
00069  *
00070  * Revision 1.4  2004/10/22 18:04:35  freckle
00071  * added #ifdef check to support old-style CTS definition
00072  * (old style: setting CTS_SIGNAL, CTS_BIT, CTS_PORT, CTS_PIN and CTS_DDR)
00073  * instead of the new single CTS_IRQ definition
00074  *
00075  * Revision 1.3  2004/09/22 08:14:48  haraldkipp
00076  * Made configurable
00077  *
00078  * Revision 1.2  2004/05/24 20:17:15  drsung
00079  * Added function UsartSize to return number of chars in input buffer.
00080  *
00081  * Revision 1.1  2003/12/15 19:25:33  haraldkipp
00082  * New USART driver added
00083  *
00084  */
00085 
00086 #include <cfg/os.h>
00087 #include <cfg/arch/avr.h>
00088 
00089 #include <string.h>
00090 
00091 #include <sys/atom.h>
00092 #include <sys/event.h>
00093 #include <sys/timer.h>
00094 
00095 #include <dev/irqreg.h>
00096 #include <dev/usartavr.h>
00097 
00103 #ifdef UART0_RTS_BIT
00104 
00105 #if (UART0_RTS_AVRPORT == AVRPORTB)
00106 #define UART_RTS_PORT  PORTB
00107 #define UART_RTS_DDR   DDRB
00108 
00109 #elif (UART0_RTS_AVRPORT == AVRPORTD)
00110 #define UART_RTS_PORT  PORTD
00111 #define UART_RTS_DDR   DDRD
00112 
00113 #elif (UART0_RTS_AVRPORT == AVRPORTE)
00114 #define UART_RTS_PORT  PORTE
00115 #define UART_RTS_DDR   DDRE
00116 
00117 #elif (UART0_RTS_AVRPORT == AVRPORTF)
00118 #define UART_RTS_PORT  PORTF
00119 #define UART_RTS_DDR   DDRF
00120 
00121 #elif (UART0_RTS_AVRPORT == AVRPORTG)
00122 #define UART_RTS_PORT  PORTG
00123 #define UART_RTS_DDR   DDRG
00124 
00125 #elif (UART0_RTS_AVRPORT == AVRPORTH)
00126 #define UART_RTS_PORT  PORTH
00127 #define UART_RTS_DDR   DDRH
00128 
00129 #endif
00130 
00131 #define UART_RTS_BIT    UART0_RTS_BIT
00132 
00133 #endif /* UART0_RTS_BIT */
00134 
00135 #ifdef UART0_DTR_BIT
00136 
00137 #if (UART0_DTR_AVRPORT == AVRPORTB)
00138 #define UART_DTR_PORT  PORTB
00139 #define UART_DTR_DDR   DDRB
00140 
00141 #elif (UART0_DTR_AVRPORT == AVRPORTD)
00142 #define UART_DTR_PORT  PORTD
00143 #define UART_DTR_DDR   DDRD
00144 
00145 #elif (UART0_DTR_AVRPORT == AVRPORTE)
00146 #define UART_DTR_PORT  PORTE
00147 #define UART_DTR_DDR   DDRE
00148 
00149 #elif (UART0_DTR_AVRPORT == AVRPORTF)
00150 #define UART_DTR_PORT  PORTF
00151 #define UART_DTR_DDR   DDRF
00152 
00153 #elif (UART0_DTR_AVRPORT == AVRPORTG)
00154 #define UART_DTR_PORT  PORTG
00155 #define UART_DTR_DDR   DDRG
00156 
00157 #elif (UART0_DTR_AVRPORT == AVRPORTH)
00158 #define UART_DTR_PORT  PORTH
00159 #define UART_DTR_DDR   DDRH
00160 
00161 #endif
00162 
00163 #define UART_DTR_BIT    UART0_DTR_BIT
00164 
00165 #endif /* UART0_DTR_BIT */
00166 
00172 #ifdef UART0_HDX_BIT
00173 
00174 #if (UART0_HDX_AVRPORT == AVRPORTB)
00175 #define UART_HDX_PORT  PORTB
00176 #define UART_HDX_DDR   DDRB
00177 
00178 #elif (UART0_HDX_AVRPORT == AVRPORTD)
00179 #define UART_HDX_PORT  PORTD
00180 #define UART_HDX_DDR   DDRD
00181 
00182 #elif (UART0_HDX_AVRPORT == AVRPORTE)
00183 #define UART_HDX_PORT  PORTE
00184 #define UART_HDX_DDR   DDRE
00185 
00186 #elif (UART0_HDX_AVRPORT == AVRPORTF)
00187 #define UART_HDX_PORT  PORTF
00188 #define UART_HDX_DDR   DDRF
00189 
00190 #elif (UART0_HDX_AVRPORT == AVRPORTG)
00191 #define UART_HDX_PORT  PORTG
00192 #define UART_HDX_DDR   DDRG
00193 
00194 #elif (UART0_HDX_AVRPORT == AVRPORTH)
00195 #define UART_HDX_PORT  PORTH
00196 #define UART_HDX_DDR   DDRH
00197 
00198 #endif
00199 #define UART_HDX_BIT    UART0_HDX_BIT
00200 
00201 #endif /* UART0_HDX_BIT */
00202 
00203 
00204 /*
00205  * Local function prototypes.
00206  */
00207 static u_long AvrUsartGetSpeed(void);
00208 static int AvrUsartSetSpeed(u_long rate);
00209 static u_char AvrUsartGetDataBits(void);
00210 static int AvrUsartSetDataBits(u_char bits);
00211 static u_char AvrUsartGetParity(void);
00212 static int AvrUsartSetParity(u_char mode);
00213 static u_char AvrUsartGetStopBits(void);
00214 static int AvrUsartSetStopBits(u_char bits);
00215 static u_long AvrUsartGetFlowControl(void);
00216 static int AvrUsartSetFlowControl(u_long flags);
00217 static u_long AvrUsartGetStatus(void);
00218 static int AvrUsartSetStatus(u_long flags);
00219 static u_char AvrUsartGetClockMode(void);
00220 static int AvrUsartSetClockMode(u_char mode);
00221 static void AvrUsartTxStart(void);
00222 static void AvrUsartRxStart(void);
00223 static int AvrUsartInit(void);
00224 static int AvrUsartDeinit(void);
00225 
00230 
00234 static USARTDCB dcb_usart0 = {
00235     0,                          /* dcb_modeflags */
00236     0,                          /* dcb_statusflags */
00237     0,                          /* dcb_rtimeout */
00238     0,                          /* dcb_wtimeout */
00239     {0, 0, 0, 0, 0, 0, 0, 0},   /* dcb_tx_rbf */
00240     {0, 0, 0, 0, 0, 0, 0, 0},   /* dcb_rx_rbf */
00241     0,                          /* dbc_last_eol */
00242     AvrUsartInit,               /* dcb_init */
00243     AvrUsartDeinit,             /* dcb_deinit */
00244     AvrUsartTxStart,            /* dcb_tx_start */
00245     AvrUsartRxStart,            /* dcb_rx_start */
00246     AvrUsartSetFlowControl,     /* dcb_set_flow_control */
00247     AvrUsartGetFlowControl,     /* dcb_get_flow_control */
00248     AvrUsartSetSpeed,           /* dcb_set_speed */
00249     AvrUsartGetSpeed,           /* dcb_get_speed */
00250     AvrUsartSetDataBits,        /* dcb_set_data_bits */
00251     AvrUsartGetDataBits,        /* dcb_get_data_bits */
00252     AvrUsartSetParity,          /* dcb_set_parity */
00253     AvrUsartGetParity,          /* dcb_get_parity */
00254     AvrUsartSetStopBits,        /* dcb_set_stop_bits */
00255     AvrUsartGetStopBits,        /* dcb_get_stop_bits */
00256     AvrUsartSetStatus,          /* dcb_set_status */
00257     AvrUsartGetStatus,          /* dcb_get_status */
00258     AvrUsartSetClockMode,       /* dcb_set_clock_mode */
00259     AvrUsartGetClockMode,       /* dcb_get_clock_mode */
00260 };
00261 
00277 NUTDEVICE devUsartAvr0 = {
00278     0,                          /* Pointer to next device, dev_next. */
00279     {'u', 'a', 'r', 't', '0', 0, 0, 0, 0},    /* Unique device name, dev_name. */
00280     IFTYP_CHAR,                 /* Type of device, dev_type. */
00281     0,                          /* Base address, dev_base (not used). */
00282     0,                          /* First interrupt number, dev_irq (not used). */
00283     0,                          /* Interface control block, dev_icb (not used). */
00284     &dcb_usart0,                /* Driver control block, dev_dcb. */
00285     UsartInit,                  /* Driver initialization routine, dev_init. */
00286     UsartIOCtl,                 /* Driver specific control function, dev_ioctl. */
00287     UsartRead,                  /* Read from device, dev_read. */
00288     UsartWrite,                 /* Write to device, dev_write. */
00289     UsartWrite_P,               /* Write data from program space to device, dev_write_P. */
00290     UsartOpen,                  /* Open a device or file, dev_open. */
00291     UsartClose,                 /* Close a device or file, dev_close. */
00292     UsartSize                   /* Request file size, dev_size. */
00293 };
00303 // added extra ifdef as test below is true even if UART0_CTS_IRQ is undef
00304 #ifdef UART0_CTS_IRQ
00305 
00306 #if (UART0_CTS_IRQ == INT0)
00307 #define UART_CTS_SIGNAL sig_INTERRUPT0
00308 #define UART_CTS_BIT    0
00309 #define UART_CTS_PORT   PORTD
00310 #define UART_CTS_PIN    PIND
00311 #define UART_CTS_DDR    DDRD
00312 
00313 #elif (UART0_CTS_IRQ == INT1)
00314 #define UART_CTS_SIGNAL sig_INTERRUPT1
00315 #define UART_CTS_BIT    1
00316 #define UART_CTS_PORT   PORTD
00317 #define UART_CTS_PIN    PIND
00318 #define UART_CTS_DDR    DDRD
00319 
00320 #elif (UART0_CTS_IRQ == INT2)
00321 #define UART_CTS_SIGNAL sig_INTERRUPT2
00322 #define UART_CTS_BIT    2
00323 #define UART_CTS_PORT   PORTD
00324 #define UART_CTS_PIN    PIND
00325 #define UART_CTS_DDR    DDRD
00326 
00327 #elif (UART0_CTS_IRQ == INT3)
00328 #define UART_CTS_SIGNAL sig_INTERRUPT3
00329 #define UART_CTS_BIT    3
00330 #define UART_CTS_PORT   PORTD
00331 #define UART_CTS_PIN    PIND
00332 #define UART_CTS_DDR    DDRD
00333 
00334 #elif (UART0_CTS_IRQ == INT4)
00335 #define UART_CTS_SIGNAL sig_INTERRUPT4
00336 #define UART_CTS_BIT    4
00337 #define UART_CTS_PORT   PORTE
00338 #define UART_CTS_PIN    PINE
00339 #define UART_CTS_DDR    DDRE
00340 
00341 #elif (UART0_CTS_IRQ == INT5)
00342 #define UART_CTS_SIGNAL sig_INTERRUPT5
00343 #define UART_CTS_BIT    5
00344 #define UART_CTS_PORT   PORTE
00345 #define UART_CTS_PIN    PINE
00346 #define UART_CTS_DDR    DDRE
00347 
00348 #elif (UART0_CTS_IRQ == INT6)
00349 #define UART_CTS_SIGNAL sig_INTERRUPT6
00350 #define UART_CTS_BIT    6
00351 #define UART_CTS_PORT   PORTE
00352 #define UART_CTS_PIN    PINE
00353 #define UART_CTS_DDR    DDRE
00354 
00355 #elif (UART0_CTS_IRQ == INT7)
00356 #define UART_CTS_SIGNAL sig_INTERRUPT7
00357 #define UART_CTS_BIT    7
00358 #define UART_CTS_PORT   PORTE
00359 #define UART_CTS_PIN    PINE
00360 #define UART_CTS_DDR    DDRE
00361 
00362 #endif
00363 
00364 #else
00365 
00366 // alternate way to specify the cts line
00367 #define UART_CTS_PORT   UART0_CTS_PORT
00368 #define UART_CTS_PIN    UART0_CTS_PIN
00369 #define UART_CTS_DDR    UART0_CTS_DDR
00370 // only set CTS_BIT if used and IRQ available
00371 #ifdef UART1_CTS_BIT
00372 #define UART_CTS_SIGNAL UART0_CTS_SIGNAL
00373 #define UART_CTS_BIT    UART0_CTS_BIT
00374 #endif
00375 
00376 #endif
00377 
00378 
00382 #ifdef __AVR_ENHANCED__
00383 
00384 #define UDRn    UDR0
00385 #define UCSRnA  UCSR0A
00386 #define UCSRnB  UCSR0B
00387 #define UCSRnC  UCSR0C
00388 #define UBRRnL  UBRR0L
00389 #define UBRRnH  UBRR0H
00390 
00391 #ifdef __IMAGECRAFT__
00392 #define TXB8    TXB80
00393 #ifdef ATMega2561
00394 #define UMSEL   UMSEL00
00395 #else
00396 #define UMSEL   UMSEL0
00397 #endif
00398 #define U2X     U2X0
00399 #define UCSZ2   UCSZ02
00400 #define UCSZ1   UCSZ01
00401 #define UCSZ0   UCSZ00
00402 #define UPM0    UPM00
00403 #define UPM1    UPM01
00404 #define USBS    USBS0
00405 #define UPE     UPE0
00406 #define MPCM    MPCM0
00407 #define UCPOL   UCPOL0
00408 #endif
00409 
00410 #else
00411 
00412 #define UDRn    UDR
00413 #define UCSRnA  USR
00414 #define UCSRnB  UCR
00415 #define UBRRnL  UBRR
00416 #define UCSZ2   CHR9
00417 
00418 #endif
00419 
00420 #define sig_UART_RECV   sig_UART0_RECV
00421 #define sig_UART_DATA   sig_UART0_DATA
00422 #define sig_UART_TRANS  sig_UART0_TRANS
00423 
00424 #ifndef SIG_UART_RECV
00425 #define SIG_UART_RECV   SIG_UART0_RECV
00426 #endif
00427 #ifndef SIG_UART_DATA
00428 #define SIG_UART_DATA   SIG_UART0_DATA
00429 #endif
00430 #ifndef SIG_UART_TRANS
00431 #define SIG_UART_TRANS  SIG_UART0_TRANS
00432 #endif
00433 
00434 #define dcb_usart   dcb_usart0
00435 
00436 #ifdef NUTTRACER
00437 #define TRACE_INT_UART_CTS TRACE_INT_UART0_CTS
00438 #define TRACE_INT_UART_RXCOMPL TRACE_INT_UART0_RXCOMPL
00439 #define TRACE_INT_UART_TXEMPTY TRACE_INT_UART0_TXEMPTY
00440 #endif
00441 
00442 #ifdef UART0_READMULTIBYTE
00443 #define UART_READMULTIBYTE
00444 #endif
00445 
00446 #ifdef USE_USART0
00447 #define USE_USART
00448 #endif
00449 
00450 #ifdef UART0_NO_SW_FLOWCONTROL
00451 #define UART_NO_SW_FLOWCONTROL
00452 #endif
00453 
00456 #include "usartavr.c"

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