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