/* * * Copyright © 2004, University of Washington, * Department of Computer Science and Engineering. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * 3. Neither name of the University of Washington, Department of Computer * Science and Engineering nor the names of its contributors may be used to * endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * */ #include "FTDI466API.h" using namespace std; int main(void) { FTDI466API usbDevice; char buffer[256]; unsigned char rxBuffer[256]; unsigned char txBuffer[256]; DWORD numBytesToSend; DWORD bytesSent; DWORD numBytesToRead; DWORD bytesReceived; // setup USB device for MPSSE mode bool setup = usbDevice.open(); if(!setup) return 0; cout << "INITIALIZING SPI" << endl; // setup for SPI communication txBuffer[0] = 0x80; // setup PORT txBuffer[1] = 0x08; // make CS high txBuffer[2] = 0x0B; // outputs: SK, DO, CS, inputs: DI, GPIOL1-L4 txBuffer[3] = 0x86; // set clk divisor to Tx at 200kHz txBuffer[4] = 0x1D; // speed low byte txBuffer[5] = 0x00; // speed high byte txBuffer[6] = 0x85; // disconnect TDI/DO output from TDO/DI input for loopback testing numBytesToSend = 7; // send the instructions ot the USB device bytesSent = usbDevice.write(txBuffer, numBytesToSend); if(bytesSent != numBytesToSend) cerr << "Not all the bytes were sent when initializing MPSSE" << endl; // see if there were any error codes when setting up SPI numBytesToRead = usbDevice.getReceiveQueueSize(); if(numBytesToRead > 0) { bytesReceived = usbDevice.read(rxBuffer, numBytesToRead); if(bytesReceived != numBytesToRead) cerr << "Problem when trying to retrieve the error bytes" << endl; for(unsigned int i = 0; i < bytesReceived; i++) cout << "Error Byte: " << rxBuffer[i] << endl; } // loop to demonstrate the SPI protocol for(int loop = 0; loop < 10; loop++) { Sleep(1000); txBuffer[0] = 0x80; // setup PORT txBuffer[1] = 0x00; // make CS low txBuffer[2] = 0x0B; // outputs: SK, DO, CS, inputs: DI, GPIOL1-L4 txBuffer[3] = 0x35; // clock out on negative edge, in on negative edge, MSB txBuffer[4] = 0x04; // low byte of length : note a length of zero is 1 byte, 1 is 2 bytes txBuffer[5] = 0x00; // high byte of length txBuffer[6] = 0x71; // payload txBuffer[7] = 0x72; txBuffer[8] = 0x73; txBuffer[9] = 0x74; txBuffer[10] = 0x70 + (char) loop; txBuffer[11] = 0x80; // setup PORT txBuffer[12] = 0x08; // make CS high txBuffer[13] = 0x0B; // outputs: SK, DO, CS, inputs: DI, GPIOL1-L4 numBytesToSend = 14; // send bytes bytesSent = usbDevice.write(txBuffer, numBytesToSend); if(bytesSent != numBytesToSend) cerr << "Not all the bytes were sent when initializing MPSSE" << endl; Sleep(5); // make sure the usb device has enough time to execute command - 5 ms latency timeout is set // get number of bytes in the received queue numBytesToRead = usbDevice.getReceiveQueueSize(); cout << "Received " << numBytesToRead << " Bytes" << endl; if(numBytesToRead > 0) { // get the received bytes bytesReceived = usbDevice.read(rxBuffer, numBytesToRead); if(bytesReceived != numBytesToRead) cerr << "Problem when trying to retrieve the bytes from the receive queue" << endl; else { // print out the bytes received over SPI in hex for(unsigned int i=0; i < bytesReceived; i++) cout << itoa(rxBuffer[i],buffer,16) << " "; cout << endl; } } } // finished with the USB device usbDevice.close(); return 0; }