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

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