Nut/OS  4.10.3
API Reference
usart1avr.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.9  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.8  2008/08/11 06:59:17  haraldkipp
00045  * BSD types replaced by stdint types (feature request #1282721).
00046  *
00047  * Revision 1.7  2008/04/29 02:28:34  thiagocorrea
00048  * Add configurable DTR pin to AVR USART driver.
00049  *
00050  * Revision 1.6  2007/08/29 07:43:53  haraldkipp
00051  * Documentation updated and corrected.
00052  *
00053  * Revision 1.5  2007/02/15 16:19:14  haraldkipp
00054  * Can use PORTG for half duplex control.
00055  *
00056  * Revision 1.4  2006/05/30 18:34:46  beutel
00057  * added #include <cfg/os.h> in compliance with usart0avr.c
00058  *
00059  * Revision 1.3  2006/02/08 15:20:06  haraldkipp
00060  * ATmega2561 Support
00061  *
00062  * Revision 1.2  2005/10/07 22:05:00  hwmaier
00063  * Using __AVR_ENHANCED__ macro instead of __AVR_ATmega128__ to support also AT90CAN128 MCU
00064  *
00065  * Revision 1.1  2005/07/26 18:02:40  haraldkipp
00066  * Moved from dev.
00067  *
00068  * Revision 1.8  2005/07/22 08:07:08  freckle
00069  * added experimental improvements to usart driver. see ChangeLog for details
00070  *
00071  * Revision 1.7  2005/01/24 22:34:49  freckle
00072  * Added new tracer by Phlipp Blum <blum@tik.ee.ethz.ch>
00073  *
00074  * Revision 1.6  2005/01/22 19:25:48  haraldkipp
00075  * Changed AVR port configuration names from PORTx to AVRPORTx.
00076  *
00077  * Revision 1.5  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.4  2004/10/03 18:43:44  haraldkipp
00083  * Some drivers may require the base address set to 1
00084  *
00085  * Revision 1.3  2004/09/22 08:14:48  haraldkipp
00086  * Made configurable
00087  *
00088  * Revision 1.2  2004/05/24 20:17:15  drsung
00089  * Added function UsartSize to return number of chars in input buffer.
00090  *
00091  * Revision 1.1  2003/12/15 19:25:33  haraldkipp
00092  * New USART driver added
00093  *
00094  */
00095 
00096 #include <cfg/os.h>
00097 #include <cfg/arch/avr.h>
00098 
00099 #include <string.h>
00100 
00101 #include <sys/atom.h>
00102 #include <sys/event.h>
00103 #include <sys/timer.h>
00104 
00105 #include <dev/irqreg.h>
00106 #include <dev/usartavr.h>
00107 
00113 #ifdef UART1_RTS_BIT
00114 
00115 #if (UART1_RTS_AVRPORT == AVRPORTB)
00116 #define UART_RTS_PORT   PORTB
00117 #define UART_RTS_DDR    DDRB
00118 
00119 #elif (UART1_RTS_AVRPORT == AVRPORTD)
00120 #define UART_RTS_PORT   PORTD
00121 #define UART_RTS_DDR    DDRD
00122 
00123 #elif (UART1_RTS_AVRPORT == AVRPORTE)
00124 #define UART_RTS_PORT   PORTE
00125 #define UART_RTS_DDR    DDRE
00126 
00127 #elif (UART1_RTS_AVRPORT == AVRPORTF)
00128 #define UART_RTS_PORT   PORTF
00129 #define UART_RTS_DDR    DDRF
00130 
00131 #elif (UART1_RTS_AVRPORT == AVRPORTG)
00132 #define UART_RTS_PORT   PORTG
00133 #define UART_RTS_DDR    DDRG
00134 
00135 #elif (UART1_RTS_AVRPORT == AVRPORTH)
00136 #define UART_RTS_PORT   PORTH
00137 #define UART_RTS_DDR    DDRH
00138 
00139 #endif
00140 #define UART_RTS_BIT    UART1_RTS_BIT
00141 
00142 #endif /* UART1_RTS_BIT */
00143 
00144 #ifdef UART1_DTR_BIT
00145 
00146 #if (UART1_DTR_AVRPORT == AVRPORTB)
00147 #define UART_DTR_PORT  PORTB
00148 #define UART_DTR_DDR   DDRB
00149 
00150 #elif (UART1_DTR_AVRPORT == AVRPORTD)
00151 #define UART_DTR_PORT  PORTD
00152 #define UART_DTR_DDR   DDRD
00153 
00154 #elif (UART1_DTR_AVRPORT == AVRPORTE)
00155 #define UART_DTR_PORT  PORTE
00156 #define UART_DTR_DDR   DDRE
00157 
00158 #elif (UART1_DTR_AVRPORT == AVRPORTF)
00159 #define UART_DTR_PORT  PORTF
00160 #define UART_DTR_DDR   DDRF
00161 
00162 #elif (UART1_DTR_AVRPORT == AVRPORTG)
00163 #define UART_DTR_PORT  PORTG
00164 #define UART_DTR_DDR   DDRG
00165 
00166 #elif (UART1_DTR_AVRPORT == AVRPORTH)
00167 #define UART_DTR_PORT  PORTH
00168 #define UART_DTR_DDR   DDRH
00169 
00170 #endif
00171 
00172 #define UART_DTR_BIT    UART1_DTR_BIT
00173 
00174 #endif /* UART1_DTR_BIT */
00175 
00181 #ifdef UART1_HDX_BIT
00182 
00183 #if (UART1_HDX_AVRPORT == AVRPORTB)
00184 #define UART_HDX_PORT   PORTB
00185 #define UART_HDX_DDR    DDRB
00186 
00187 #elif (UART1_HDX_AVRPORT == AVRPORTD)
00188 #define UART_HDX_PORT   PORTD
00189 #define UART_HDX_DDR    DDRD
00190 
00191 #elif (UART1_HDX_AVRPORT == AVRPORTE)
00192 #define UART_HDX_PORT   PORTE
00193 #define UART_HDX_DDR    DDRE
00194 
00195 #elif (UART1_HDX_AVRPORT == AVRPORTF)
00196 #define UART_HDX_PORT   PORTF
00197 #define UART_HDX_DDR    DDRF
00198 
00199 #elif (UART1_HDX_AVRPORT == AVRPORTG)
00200 #define UART_HDX_PORT  PORTG
00201 #define UART_HDX_DDR   DDRG
00202 
00203 #elif (UART1_HDX_AVRPORT == AVRPORTH)
00204 #define UART_HDX_PORT  PORTH
00205 #define UART_HDX_DDR   DDRH
00206 
00207 #endif /* UART1_HDX_AVRPORT */
00208 #define UART_HDX_BIT    UART1_HDX_BIT
00209 
00210 #endif /* UART1_HDX_BIT */
00211 
00212 
00213 #ifdef __AVR_ENHANCED__
00214 
00215 /*
00216  * Local function prototypes.
00217  */
00218 static uint32_t AvrUsartGetSpeed(void);
00219 static int AvrUsartSetSpeed(uint32_t rate);
00220 static uint8_t AvrUsartGetDataBits(void);
00221 static int AvrUsartSetDataBits(uint8_t bits);
00222 static uint8_t AvrUsartGetParity(void);
00223 static int AvrUsartSetParity(uint8_t mode);
00224 static uint8_t AvrUsartGetStopBits(void);
00225 static int AvrUsartSetStopBits(uint8_t bits);
00226 static uint32_t AvrUsartGetFlowControl(void);
00227 static int AvrUsartSetFlowControl(uint32_t flags);
00228 static uint32_t AvrUsartGetStatus(void);
00229 static int AvrUsartSetStatus(uint32_t flags);
00230 static uint8_t AvrUsartGetClockMode(void);
00231 static int AvrUsartSetClockMode(uint8_t mode);
00232 static void AvrUsartTxStart(void);
00233 static void AvrUsartRxStart(void);
00234 static int AvrUsartInit(void);
00235 static int AvrUsartDeinit(void);
00236 
00241 
00245 static USARTDCB dcb_usart1 = {
00246     0,                          /* dcb_modeflags */
00247     0,                          /* dcb_statusflags */
00248     0,                          /* dcb_rtimeout */
00249     0,                          /* dcb_wtimeout */
00250     {0, 0, 0, 0, 0, 0, 0, 0},   /* dcb_tx_rbf */
00251     {0, 0, 0, 0, 0, 0, 0, 0},   /* dcb_rx_rbf */
00252     0,                          /* dbc_last_eol */
00253     AvrUsartInit,               /* dcb_init */
00254     AvrUsartDeinit,             /* dcb_deinit */
00255     AvrUsartTxStart,            /* dcb_tx_start */
00256     AvrUsartRxStart,            /* dcb_rx_start */
00257     AvrUsartSetFlowControl,     /* dcb_set_flow_control */
00258     AvrUsartGetFlowControl,     /* dcb_get_flow_control */
00259     AvrUsartSetSpeed,           /* dcb_set_speed */
00260     AvrUsartGetSpeed,           /* dcb_get_speed */
00261     AvrUsartSetDataBits,        /* dcb_set_data_bits */
00262     AvrUsartGetDataBits,        /* dcb_get_data_bits */
00263     AvrUsartSetParity,          /* dcb_set_parity */
00264     AvrUsartGetParity,          /* dcb_get_parity */
00265     AvrUsartSetStopBits,        /* dcb_set_stop_bits */
00266     AvrUsartGetStopBits,        /* dcb_get_stop_bits */
00267     AvrUsartSetStatus,          /* dcb_set_status */
00268     AvrUsartGetStatus,          /* dcb_get_status */
00269     AvrUsartSetClockMode,       /* dcb_set_clock_mode */
00270     AvrUsartGetClockMode,       /* dcb_get_clock_mode */
00271 };
00272 
00288 NUTDEVICE devUsartAvr1 = {
00289     0,                          /* Pointer to next device, dev_next. */
00290     {'u', 'a', 'r', 't', '1', 0, 0, 0, 0},    /* Unique device name, dev_name. */
00291     IFTYP_CHAR,                 /* Type of device, dev_type. */
00292     1,                          /* Base address, dev_base. */
00293     0,                          /* First interrupt number, dev_irq (not used). */
00294     0,                          /* Interface control block, dev_icb (not used). */
00295     &dcb_usart1,                /* Driver control block, dev_dcb. */
00296     UsartInit,                  /* Driver initialization routine, dev_init. */
00297     UsartIOCtl,                 /* Driver specific control function, dev_ioctl. */
00298     UsartRead,                  /* Read from device, dev_read. */
00299     UsartWrite,                 /* Write to device, dev_write. */
00300     UsartWrite_P,               /* Write data from program space to device, dev_write_P. */
00301     UsartOpen,                  /* Open a device or file, dev_open. */
00302     UsartClose,                 /* Close a device or file, dev_close. */
00303     UsartSize                   /* Request file size, dev_size. */
00304 };
00313 // added extra ifdef as test below is true even if UART1_CTS_IRQ is undef
00314 #ifdef UART1_CTS_IRQ
00315 
00316 #if (UART1_CTS_IRQ == INT0)
00317 #define UART_CTS_SIGNAL sig_INTERRUPT0
00318 #define UART_CTS_BIT    0
00319 #define UART_CTS_PORT   PORTD
00320 #define UART_CTS_PIN    PIND
00321 #define UART_CTS_DDR    DDRD
00322 
00323 #elif (UART1_CTS_IRQ == INT1)
00324 #define UART_CTS_SIGNAL sig_INTERRUPT1
00325 #define UART_CTS_BIT    1
00326 #define UART_CTS_PORT   PORTD
00327 #define UART_CTS_PIN    PIND
00328 #define UART_CTS_DDR    DDRD
00329 
00330 #elif (UART1_CTS_IRQ == INT2)
00331 #define UART_CTS_SIGNAL sig_INTERRUPT2
00332 #define UART_CTS_BIT    2
00333 #define UART_CTS_PORT   PORTD
00334 #define UART_CTS_PIN    PIND
00335 #define UART_CTS_DDR    DDRD
00336 
00337 #elif (UART1_CTS_IRQ == INT3)
00338 #define UART_CTS_SIGNAL sig_INTERRUPT3
00339 #define UART_CTS_BIT    3
00340 #define UART_CTS_PORT   PORTD
00341 #define UART_CTS_PIN    PIND
00342 #define UART_CTS_DDR    DDRD
00343 
00344 #elif (UART1_CTS_IRQ == INT4)
00345 #define UART_CTS_SIGNAL sig_INTERRUPT4
00346 #define UART_CTS_BIT    4
00347 #define UART_CTS_PORT   PORTE
00348 #define UART_CTS_PIN    PINE
00349 #define UART_CTS_DDR    DDRE
00350 
00351 #elif (UART1_CTS_IRQ == INT5)
00352 #define UART_CTS_SIGNAL sig_INTERRUPT5
00353 #define UART_CTS_BIT    5
00354 #define UART_CTS_PORT   PORTE
00355 #define UART_CTS_PIN    PINE
00356 #define UART_CTS_DDR    DDRE
00357 
00358 #elif (UART1_CTS_IRQ == INT6)
00359 #define UART_CTS_SIGNAL sig_INTERRUPT6
00360 #define UART_CTS_BIT    6
00361 #define UART_CTS_PORT   PORTE
00362 #define UART_CTS_PIN    PINE
00363 #define UART_CTS_DDR    DDRE
00364 
00365 #elif (UART1_CTS_IRQ == INT7)
00366 #define UART_CTS_SIGNAL sig_INTERRUPT7
00367 #define UART_CTS_BIT    7
00368 #define UART_CTS_PORT   PORTE
00369 #define UART_CTS_PIN    PINE
00370 #define UART_CTS_DDR    DDRE
00371 
00372 #endif
00373 
00374 #else
00375 
00376 // alternate way to specify the cts line
00377 #define UART_CTS_PORT   UART1_CTS_PORT
00378 #define UART_CTS_PIN    UART1_CTS_PIN
00379 #define UART_CTS_DDR    UART1_CTS_DDR
00380 // only set CTS_BIT if used and IRQ available
00381 #ifdef UART1_CTS_BIT
00382 #define UART_CTS_SIGNAL UART1_CTS_SIGNAL
00383 #define UART_CTS_BIT    UART1_CTS_BIT
00384 #endif
00385 
00386 #endif
00387 
00390 
00391 #define UDRn    UDR1
00392 #define UCSRnA  UCSR1A
00393 #define UCSRnB  UCSR1B
00394 #define UCSRnC  UCSR1C
00395 #define UBRRnL  UBRR1L
00396 #define UBRRnH  UBRR1H
00397 
00398 #ifdef __IMAGECRAFT__
00399 #define TXB8    TXB81
00400 #if defined(ATMega2560) || defined(ATMega2561)
00401 #define UMSEL   UMSEL01
00402 #else
00403 #define UMSEL   UMSEL1
00404 #endif
00405 #define U2X     U2X1
00406 #define UCSZ2   UCSZ12
00407 #define UCSZ1   UCSZ11
00408 #define UCSZ0   UCSZ10
00409 #define UPM0    UPM10
00410 #define UPM1    UPM11
00411 #define USBS    USBS1
00412 #define UPE     UPE1
00413 #define MPCM    MPCM1
00414 #define UCPOL   UCPOL1
00415 #endif
00416 
00417 #define sig_UART_RECV   sig_UART1_RECV
00418 #define sig_UART_DATA   sig_UART1_DATA
00419 #define sig_UART_TRANS  sig_UART1_TRANS
00420 
00421 #define SIG_UART_RECV   SIG_UART1_RECV
00422 #define SIG_UART_DATA   SIG_UART1_DATA
00423 #define SIG_UART_TRANS  SIG_UART1_TRANS
00424 
00425 #define dcb_usart   dcb_usart1
00426 
00427 #ifdef NUTTRACER
00428 #define TRACE_INT_UART_CTS TRACE_INT_UART1_CTS
00429 #define TRACE_INT_UART_RXCOMPL TRACE_INT_UART1_RXCOMPL
00430 #define TRACE_INT_UART_TXEMPTY TRACE_INT_UART1_TXEMPTY
00431 #endif
00432 
00433 #ifdef UART1_READMULTIBYTE
00434 #define UART_READMULTIBYTE
00435 #endif
00436 
00437 #ifdef USE_USART1
00438 #define USE_USART
00439 #endif
00440 
00441 #ifdef UART1_NO_SW_FLOWCONTROL
00442 #define UART_NO_SW_FLOWCONTROL
00443 #endif
00444 
00445 #include "usartavr.c"
00446 
00447 #endif