Usb2Stream.DLL
Version 1.0.15

  Index
 
  Basics about the parallel interfaces
   

The FX2 has 2 different kinds of parallel interfaces which can me selected:

  • slave fifo interface: The FX2 is a slave of the parallel bus and has a FIFO-like interface with control and status lines. A synchronous or asynchronous interface can be selected.
  • GPIF interface: The FX2 is master of the parallel bus. The way the GPIF works is user defined but the Dll realizes also a default waveform for easy use of the GPIF.

GPIF interface

The default GPIF waveform is selected and initialized by calling the function "SetDefaultGpifParams" before starting a stream. The default waveform works synchronous to a selectable 30 or 48 MHz internal clock. A divider value defines then the transfer speed of the bus. The minimal divider value is 2, the maximum is 255. The CTL0 line of the FX2 generates a synchronous clock signal: For streams from the PC to the FX2 the data will be latched on each rising edge of the CTL0 signal. For streams from FX2 to the PC the data bus is sampled on each rising edge of the CTL0 signal. The following diagram shows the Timing diagram of the default GPIF waveform.

User defined waveforms can also be used. The waveforms can be designed using the Cypress GPIF designer which can me downloaded from the Cypress website. It is important the the GPIF waveforms make use of long transfer mode i.e. the Transaction counter TC needs to be checked and the state machine may only pass over to the idle state if the transaction counter is expired.

Slave FIFO interface

Status lines: CTL0 = /FE = low active fifo empty flag CTL1 = /FF = low active fifo full Flag Depending on the needed data transfer direction 2 IO pins need to be connected to the correct Level:
  • for PC => Device streaming: Connect FIFOADR0 and FIFOADR1 to 0
  • for Device => PC streaming: Connect FIFOADR0 to 0 and FIFOADR1 to VCC
Where FIFOADR0 is PA4, FIFOADR1 = PA5 of the FX2 controller.

The slave fifo interface can be initialized by calling the "SetSlaveFifoParams" before starting a stream. Basically 2 kind of fifo interfaces can be selected:

  • Asynchronous mode: The FIFO can be read or written using 2 asynchronous low active control signals: /WR (=RDY1) or /RD (=RDY0) depending on the transfer direction. Data gets written or read on each falling edge of theese signals.
  • synchronous mode: Data transfer takes place synchronous to the IFCLK clock signal. When /WR (RDY1) and /RD (RDY0) are low data gets transfered on each rising edge of the IFCLK signal. The IFCLK signal can be generated by the FX2 (48 or 30 MHz) or supplied externally. An external clock needs to be in the range from 5 MHz to 48 MHz and needs to be always active, also when no data is transfered.
The timing of the slave fifo interface including timing diagrams is described in the FX2LP (Cy68013A) Datasheet in the chapters: 9.7 to 9.10.

  information
   

This Dll realizes the GPIF and Slave fifo interface of the FX2 Cypress controller.

To exchange data with the user application a callback function has to be realized and set with the function SetCallBack. This Callback function will be called every time a new data packet was received or needs to be send depending on the selected streaming direction.

All Dll functions return a boolean parameter which contains true if the function was executed successfully or false if an error occured.

 

  Dll functions
    Basic functions
^     SetCallBack
 

function SetCallBack(CallBack : TUsbDataCallbackProc) : Boolean; stdcall;

Set a callback function. Calling this function before starting streaming action is mandatory. The callback function needs to have the following signature:

procedure Callbackname(pData : pointer; DatLen : DWord; PcToDevice : Boolean); stdcall;

The callback function receives 3 parameters when beeing called:

  • pData: Pointer to a data array that needs to be read or written depending on stream direction. This can either be a Byte pointer (when Bitwidth = 8) or a Word pointer (when Bitwidth = 16)
  • DatLen (indicates how many bytes need to be read or written.
    IMPORTANT: When streaming is stopped due to an error or on user-request the callback function gets called one time with the parameter DatLen set to zero).
  • PcToDevice: Depending of the stream direction that you specified when executing the StartStream function, this value will be set to true (Data transfer from computer to usb device) or false (Data transfer from usb device to computer).
^     SetDefaultGpifParams
 

function SetDefaultGpifParams(MHz48 : Boolean; Divider : Byte; BitWidth : Byte) : Boolean; stdcall;

This function specifies the Bitwidth (8 or 16) and sample frequency.
There is a selectable main frequency of 30MHz (MHz48=false) or 48 MHz (MHz48=true).
The sample frequency is derived from this main frequency followed by a divider (division factors /2 - /255).
The fastest possible setting is: SetDefaultGpifParams(true, 2, 8).
This settings selects: FSampleClock = 48MHz / 2
 

^     StartStream
  function StartStream(PcToDevice : Boolean) : Boolean; stdcall;

Start a streaming process. PcToDevice specifies the data transfer direction:
Computer => Usb device: PcToDevice=true, the Usb device will output data
Usb device => Computer: PcToDevice=false, The Usb device will receive data from an external device
 

^     StopStream
 

function StopStream : Boolean; stdcall;

Stops an active streaming process. This function might also be called when streaming is not active
 

^     SetLicense
 

function SetLicense(Filename : PChar) : Boolean; stdcall;

Activates the license. If the license is not set a nag-screen will be shown in irregular intervals.
If the license is put in the same directory as the Dll and renamed to "license.dat" this function does not need to be called because the dll checks for presence of this file during startup.  

^     isHighSpeed
 

function isHighSpeed : Boolean; stdcall;

Returns TRUE if device is present and connected to a High speed 480 Mbps USB 2.0 Port. Otherwise it returns false.

^   Status functions
^     StreamActive
  function StreamActive : Boolean; stdcall;

This function returns true if the streaming process is currently active
 

^     LostPacketCount
  function LostPacketCount : DWord; stdcall;

LostPacketCount might be called after stopping a streaming process to check if any USB data packets were lost. This function should return 0, otherwise something went wrong.
 

^     StreamDevPresent
  function StreamDevPresent : Boolean; stdcall;

With StreamDevPresent you can check if the streaming device is connected and the driver installed correctly.

 

    Advanced functions
^     SetGpifParams
  function SetGpifParams(pWaveData, pInitData : Pointer; IfConfig : Byte; BitWidth : Byte) : Boolean; stdcall;

Sets gpif data according to the contents from a GPIF.C file. Just pass pointer to the arrays from this file to this function to set the GPIF interface. Set Bitwidth to 8 or 16 to select the bitwidth.

 

^     SetGpifParamsFromFile
 

function SetGpifParamsFromFile(Filename : PChar; BitWidth : Byte) : Boolean; stdcall;
 

Set the GPIF interface parameters from a gpif.c file. Just pass the filename+path and select the Bitwidth (8 or 16) to set the gpif interface parameters.

^     SetSlaveFifoParams
 

function SetSlaveFifoParams(SyncFifo : Boolean; IfClkMode : Byte; BitWidth : Byte) : Boolean; stdcall;

The Dll also realizes the Slave fifo interface from the FX2. To enable the Slave fifo mode just call this function before starting the stream.

The parameters are:

  • SyncFifo: Set to true if you want to have an synchronous slave fifo interface or false if it should operate asynchronously.
  • IfClkMode: 0 = IFCLK will output an 48MHz clock. 1 = IFCLK will output an 30 MHz clock. 2 = IFCLK is an input and needs to have an external clock signal in the range 5-48MHz connected. Setting Bit 7 of this parameter will invert the clock signal.
  • BitWidth: 8 = 8 Bit width bus, 16 = 16 Bit width bus.

To use the slave fifo interface some external connections are needed:

  • for PC => Device streaming: Connect FIFOADR0 and FIFOADR1 to 0
  • for Device => PC streaming: Connect FIFOADR0 to 0 and FIFOADR1 to VCC

 

    IO functions
^     SetPortDir
  function SetPortDir(PortNum, PortDir : Byte) : Boolean; stdcall;

Set the direction of PortA (PortNum=0), PortB (PortNum=1) or PortD (PortNum=2). If A Bit is 1 the correspondent Port-Pin is an output, otherwise it is an input.

It is not needed to set the direction of the databus as it is done internally.

^     SetPortState
  function SetPortState(PortNum, PortState : Byte) : Boolean; stdcall;

Set the status of a complete Port (PortA=0, PortB=1, PortD=2) to "PortState".

 

^     GetPortState
  function GetPortState(PortNum: Byte; Var PortState : Byte) : Boolean; stdcall;

Read the status from the selected Port to PortState.

 

^     I2cTransfer
  function I2cTransfer(SlaveAddr : Byte; WriteLen, ReadLen : DWord; pWriteData, pReadData : pByte) : Boolean; stdcall;

Executes an I2c Read or Write transfer.

This function writes the Data specified by pWriteData and WriteLen to the i2c device with address "SlaveAddr" and reads the amount of data specified by ReadLen to pReadData after the write transfer.

 

^     SpiTransfer
  function SpiTransfer(LsbFirst : Boolean; SPIMode : Byte; Speed : Byte; PWrite, PRead : pByte; Len : Word) : Boolean; stdcall;

Executes a Spi transfer

This function writes the Data specified by pWrite to the device. As an spi transfer is always bidirectional the read data will be written to the buffer passed to pRead. pRead can also be set to nul if you are not interested in the received data.

The SPI bus function implement a SPI master and consists of 4 I/O lines:

PortA.7 = /SS (Slave select - Output)

PortA.6 = SCK (Clockline - Output)
PortA.5 = MOSI (Master Out Slave In - Output)
PortA.4 = MISO (Master In Slave Out - Output)

Data is transfered bit-wise synchronous to SCK over the data lines MOSI and MISO. /SS gets low before a data byte will be transfered and high after a byte has been transfered.

If LsbFirst is true (1), the first bit transfered is the LSB (Bit 0), if it is false (0), the MSB (Bit 7) will be transfered first.

Speed selects the bitrate of the bus:

0 = 50 kHz
1 = 25 kHz
2 = 10 kHz
3 = 5 kHz
4 = 2 kHz
5 = 1 kHz

SPIMode defines the idle state of the clockline and the edges data is transfered:

Bit 0 = 0: The Idle state of SCK is 0. The leading edge is a 0 to 1 transition, trailing edge a 1 to 0 transition
Bit 0 = 1: The Idle state of SCK is 1. The leading edge is a 1 to 0 transition, trailing edge a 0 to 1 transition

Bit 1 = 0: Data is sampled at leading edge of SCK and Setup on the trailing edge
Bit 1 = 1: Data is sampled at trailing edge of SCK and Setup on the leading edge

pWrite is an pointer to the data-block what needs to be send.

pRead is a pointer to the buffer where the read data can be stored. If you’re not interested in the received data you can set pRead to null. pRead might point to the same address location as pWrite

Len specifies the count of byte to be transfered.

 

^