Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

# ft245_io.c

    00001 /*! \file ft245.c \brief FTDI FT245 USB Inferface Driver Library. */
    00002 //*****************************************************************************
    00003 //
    00004 // File Name    : 'ft245.c'
    00005 // Title        : FTDI FT245 USB Inferface Driver Library
    00006 // Author       : Pascal Stang - Copyright (C) 2004
    00007 // Created      : 2004.02.10
    00008 // Revised      : 2004.02.19
    00009 // Version      : 0.1
    00010 // Target MCU   : Atmel AVR Series
    00011 // Editor Tabs  : 4
    00012 //
    00013 // NOTE: This code is currently below version 1.0, and therefore is considered
    00014 // to be lacking in some functionality or documentation, or may not be fully
    00015 // tested.  Nonetheless, you can expect most functions to work.
    00016 //
    00017 // This code is distributed under the GNU Public License
    00018 //      which can be found at http://www.gnu.org/licenses/gpl.txt
    00019 //
    00020 //*****************************************************************************
    00021
    00022 #include "lpc2000.h"
    00023 //#include <avr/io.h>
    00024 //#include <avr/signal.h>
    00025 //#include <avr/interrupt.h>
    00026
    00027 #include "global.h"
    00028 #include "timer.h"
    00029 #include "membus.h"
    00030 #include "ft245.h"
    00031
    00032 // global variables
    00033 static char ft245RxData[FT245_RX_BUFFER_SIZE];
    00034 cBuffer ft245RxBuffer;
    00035
    00036 // Functions
    00037 void ft245Init(void)
    00038 {
    00039 #ifdef FT245_PORT_INTERFACE
    00040     // initialize port pins
    00041     // configure input pins
    00042     cbi(FT245_CTRL_DDR, FT245_TXE);
    00043     cbi(FT245_CTRL_DDR, FT245_RXF);
    00044     //cbi(FT245_CTRL_DDR, FT245_SUSPEND);
    00045     // use pullups
    00046     sbi(FT245_CTRL_PORT, FT245_TXE);
    00047     sbi(FT245_CTRL_PORT, FT245_RXF);
    00048     //sbi(FT245_CTRL_PORT, FT245_SUSPEND);
    00049
    00050     // configure output pins
    00051     sbi(FT245_CTRL_DDR, FT245_RESET);
    00052     sbi(FT245_CTRL_DDR, FT245_WR);
    00053     sbi(FT245_CTRL_DDR, FT245_RD);
    00054     // preset the outputs
    00055     sbi(FT245_CTRL_PORT, FT245_RESET);
    00056     cbi(FT245_CTRL_PORT, FT245_WR);
    00057     sbi(FT245_CTRL_PORT, FT245_RD);
    00058
    00059     // configure data bus
    00060     outb(FT245_DATA_DDR, 0x00);
    00061     outb(FT245_DATA_PORT, 0xFF);
    00062 #endif
    00063 #ifdef FT245_MEMBUS_INTERFACE
    00064     // initialize the memory bus
    00065     membusInit();
    00066     // configure status pins
    00067     IODIR &= FT245_TXE;
    00068     IODIR &= FT245_RXF;
    00069 #endif
    00070     // initialize the RX buffer
    00071     bufferInit(&ft245RxBuffer, ft245RxData, FT245_RX_BUFFER_SIZE);
    00072
    00073     // reset the FT245
    00074     //ft245Reset();
    00075
    00076 }
    00077
    00078 void ft245Reset(void)
    00079 {
    00080 #ifdef FT245_PORT_INTERFACE
    00081     // activate reset line
    00082     cbi(FT245_CTRL_PORT, FT245_RESET);
    00083     // hold for 1ms
    00084     timerPause(1);
    00085     // release reset
    00086     sbi(FT245_CTRL_PORT, FT245_RESET);
    00087 #endif
    00088 #ifdef FT245_MEMBUS_INTERFACE
    00089
    00090 #endif
    00091 }
    00092
    00093 u08 ft245ReadByte(void)
    00094 {
    00095 #ifdef FT245_PORT_INTERFACE
    00096     u08 data;
    00097     // data bus set for input
    00098     outb(FT245_DATA_DDR, 0x00);
    00099     // assert RD line
    00100     cbi(FT245_CTRL_PORT, FT245_RD);
    00101     // wait
    00102     asm volatile ("nop");
    00103     asm volatile ("nop");
    00104     asm volatile ("nop");
    00105     asm volatile ("nop");
    00106     // read data
    00107     data = inb(FT245_DATA_PORTIN);
    00108     // release RD line
    00109     sbi(FT245_CTRL_PORT, FT245_RD);
    00110     // return data
    00111     return data;
    00112 #endif
    00113 #ifdef FT245_MEMBUS_INTERFACE
    00114     return membusRead(FT245_BASE_ADDRESS);
    00115 #endif
    00116 }
    00117
    00118 void ft245WriteByte(u08 data)
    00119 {
    00120 #ifdef FT245_PORT_INTERFACE
    00121     // wait for transmit fifo to be ready
    00122     while(!ft245CheckTxFifo());
    00123     // assert WR line
    00124     sbi(FT245_CTRL_PORT, FT245_WR);
    00125     // data bus set for output
    00126     outb(FT245_DATA_DDR, 0xFF);
    00127     // write data
    00128     outb(FT245_DATA_PORT, data);
    00129     // wait
    00130     asm volatile ("nop");
    00131     asm volatile ("nop");
    00132     asm volatile ("nop");
    00133     asm volatile ("nop");
    00134     // release WR line
    00135     cbi(FT245_CTRL_PORT, FT245_WR);
    00136     // return data bus to input + pullups
    00137     outb(FT245_DATA_DDR, 0x00);
    00138     outb(FT245_DATA_PORT, 0xFF);
    00139 #endif
    00140 #ifdef FT245_MEMBUS_INTERFACE
    00141     return membusWrite(FT245_BASE_ADDRESS, data);
    00142 #endif
    00143 }
    00144
    00145 u08 ft245CheckRxFifo(void)
    00146 {
    00147     // check state of RXF line
    00148     // RXF = L  => Data in fifo ready to be read
    00149     // RXF = H  => No data, or not ready
    00150 #ifdef FT245_PORT_INTERFACE
    00151     if( inb(FT245_CTRL_PORTIN) & (1<<FT245_RXF) )
    00152         return FALSE;   // no data
    00153     else
    00154         return TRUE;    // data is available
    00155 #endif
    00156 #ifdef FT245_MEMBUS_INTERFACE
    00157     if(IOPIN & FT245_RXF)
    00158         return FALSE;   // no data
    00159     else
    00160         return TRUE;    // data is available
    00161 #endif
    00162 }
    00163
    00164 u08 ft245CheckTxFifo(void)
    00165 {
    00166     // check state of TXE line
    00167     // TXE = L  => Tx fifo is ready to be written
    00168     // TXE = H  => Tx fifo full or not ready
    00169 #ifdef FT245_PORT_INTERFACE
    00170     if( inb(FT245_CTRL_PORTIN) & (1<<FT245_TXE) )
    00171         return FALSE;   // full or not ready
    00172     else
    00173         return TRUE;    // tx fifo space is available
    00174 #endif
    00175 #ifdef FT245_MEMBUS_INTERFACE
    00176     if(IOPIN & FT245_TXE)
    00177         return FALSE;   // full or not ready
    00178     else
    00179         return TRUE;    // tx fifo space is available
    00180 #endif
    00181 }
    00182
    00183 /*
    00184 u08 ft245IsSuspended(void)
    00185 {
    00186     // check state of SUSPEND line
    00187     // SUSPEND = L  => Data in fifo ready to be read
    00188     // SUSPEND = H  => No data, or not ready
    00189     if( inb(FT245_CTRL_PORTIN) & (1<<FT245_RXF) )
    00190         return FALSE;   // no data
    00191     else
    00192         return TRUE;    // data is available
    00193 }
    00194 */
    00195
    00196 void ft245SendByte(u08 data)
    00197 {
    00198     // wait for FT245 to become ready
    00199     while(!ft245CheckTxFifo());
    00200     // send byte
    00201     ft245WriteByte(data);
    00202 }
    00203
    00204 cBuffer* ft245GetRxBuffer(void)
    00205 {
    00206     // since there is an internal buffer in the FT245 chip
    00207     // we don't read in bytes immediately on reception.
    00208     // instead, the FT245 buffer is read in when the
    00209     // receive buffer is requested
    00210
    00211     // read in receive buffer
    00212     while(ft245CheckRxFifo())
    00213     {
    00214         bufferAddToEnd(&ft245RxBuffer, ft245ReadByte());
    00215     }
    00216
    00217     return &ft245RxBuffer;
    00218 }