00001 /* 00002 * Copyright (C) 2005 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 00033 /* 00034 * $Log: usart0at91.c,v $ 00035 * Revision 1.5 2008/02/15 16:59:11 haraldkipp 00036 * Spport for AT91SAM7SE512 added. 00037 * 00038 * Revision 1.4 2007/10/04 20:04:11 olereinhardt 00039 * Support for SAM7S256 added 00040 * 00041 * Revision 1.3 2006/07/05 07:55:23 haraldkipp 00042 * Daidai's support for AT91SAM7X added. 00043 * 00044 * Revision 1.2 2006/01/05 16:46:52 haraldkipp 00045 * Baudrate calculation is now based on NutGetCpuClock(). 00046 * 00047 * Revision 1.1 2005/11/20 14:40:28 haraldkipp 00048 * Added interrupt driven UART driver for AT91. 00049 * 00050 */ 00051 00052 #include <cfg/os.h> 00053 #include <cfg/clock.h> 00054 #include <cfg/arch.h> 00055 00056 #include <string.h> 00057 00058 #include <sys/atom.h> 00059 #include <sys/event.h> 00060 #include <sys/timer.h> 00061 00062 #include <dev/irqreg.h> 00063 #include <dev/usartat91.h> 00064 00065 #ifndef NUT_CPU_FREQ 00066 #ifdef NUT_PLL_CPUCLK 00067 #include <dev/cy2239x.h> 00068 #else /* !NUT_PLL_CPUCLK */ 00069 #define NUT_CPU_FREQ 73728000UL 00070 #endif /* !NUT_PLL_CPUCLK */ 00071 #endif /* !NUT_CPU_FREQ */ 00072 00073 /* 00074 * Local function prototypes. 00075 */ 00076 static u_long At91UsartGetSpeed(void); 00077 static int At91UsartSetSpeed(u_long rate); 00078 static u_char At91UsartGetDataBits(void); 00079 static int At91UsartSetDataBits(u_char bits); 00080 static u_char At91UsartGetParity(void); 00081 static int At91UsartSetParity(u_char mode); 00082 static u_char At91UsartGetStopBits(void); 00083 static int At91UsartSetStopBits(u_char bits); 00084 static u_long At91UsartGetFlowControl(void); 00085 static int At91UsartSetFlowControl(u_long flags); 00086 static u_long At91UsartGetStatus(void); 00087 static int At91UsartSetStatus(u_long flags); 00088 static u_char At91UsartGetClockMode(void); 00089 static int At91UsartSetClockMode(u_char mode); 00090 static void At91UsartTxStart(void); 00091 static void At91UsartRxStart(void); 00092 static int At91UsartInit(void); 00093 static int At91UsartDeinit(void); 00094 00099 00103 static USARTDCB dcb_usart0 = { 00104 0, /* dcb_modeflags */ 00105 0, /* dcb_statusflags */ 00106 0, /* dcb_rtimeout */ 00107 0, /* dcb_wtimeout */ 00108 {0, 0, 0, 0, 0, 0, 0, 0}, /* dcb_tx_rbf */ 00109 {0, 0, 0, 0, 0, 0, 0, 0}, /* dcb_rx_rbf */ 00110 0, /* dbc_last_eol */ 00111 At91UsartInit, /* dcb_init */ 00112 At91UsartDeinit, /* dcb_deinit */ 00113 At91UsartTxStart, /* dcb_tx_start */ 00114 At91UsartRxStart, /* dcb_rx_start */ 00115 At91UsartSetFlowControl, /* dcb_set_flow_control */ 00116 At91UsartGetFlowControl, /* dcb_get_flow_control */ 00117 At91UsartSetSpeed, /* dcb_set_speed */ 00118 At91UsartGetSpeed, /* dcb_get_speed */ 00119 At91UsartSetDataBits, /* dcb_set_data_bits */ 00120 At91UsartGetDataBits, /* dcb_get_data_bits */ 00121 At91UsartSetParity, /* dcb_set_parity */ 00122 At91UsartGetParity, /* dcb_get_parity */ 00123 At91UsartSetStopBits, /* dcb_set_stop_bits */ 00124 At91UsartGetStopBits, /* dcb_get_stop_bits */ 00125 At91UsartSetStatus, /* dcb_set_status */ 00126 At91UsartGetStatus, /* dcb_get_status */ 00127 At91UsartSetClockMode, /* dcb_set_clock_mode */ 00128 At91UsartGetClockMode, /* dcb_get_clock_mode */ 00129 }; 00130 00146 NUTDEVICE devUsartAt910 = { 00147 0, /* Pointer to next device, dev_next. */ 00148 {'u', 'a', 'r', 't', '0', 0, 0, 0, 0}, /* Unique device name, dev_name. */ 00149 IFTYP_CHAR, /* Type of device, dev_type. */ 00150 0, /* Base address, dev_base (not used). */ 00151 0, /* First interrupt number, dev_irq (not used). */ 00152 0, /* Interface control block, dev_icb (not used). */ 00153 &dcb_usart0, /* Driver control block, dev_dcb. */ 00154 UsartInit, /* Driver initialization routine, dev_init. */ 00155 UsartIOCtl, /* Driver specific control function, dev_ioctl. */ 00156 UsartRead, /* Read from device, dev_read. */ 00157 UsartWrite, /* Write to device, dev_write. */ 00158 UsartOpen, /* Open a device or file, dev_open. */ 00159 UsartClose, /* Close a device or file, dev_close. */ 00160 UsartSize /* Request file size, dev_size. */ 00161 }; 00162 00166 00167 #define USARTn_BASE USART0_BASE 00168 #define US_ID US0_ID 00169 #if defined (MCU_AT91SAM7X256) 00170 #define US_GPIO_PINS 0x00000003 00171 #elif defined (MCU_AT91SAM7S256) || defined(MCU_AT91SAM7SE512) 00172 #define US_GPIO_PINS 0x00000060 00173 #else 00174 #define US_GPIO_PINS 0x0000C000 00175 #endif 00176 #define SIG_UART sig_UART0 00177 #define dcb_usart dcb_usart0 00178 00179 #include "usartat91.c"