ECE4333 - 2018 - Ahmed & Brandon / Mbed OS ObjectFollower

Dependencies:   TPixy-Interface

Fork of PlayBack by ECE4333 - 2018 - Ahmed & Brandon

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DE0_driver.cpp Source File

DE0_driver.cpp

00001 /******************************************************************************/
00002 // ECE4333
00003 // LAB Partner 1:   Ahmed Sobhy - ID: 3594449
00004 // LAB Partner 2:   Brandon Kingman - ID: 3470444
00005 // Project:         Autonomous Robot Design
00006 // Instructor:      Prof. Chris Diduch
00007 /******************************************************************************/
00008 // filename: DE0_driver.cpp
00009 // file content description:
00010 //      * function to initialize the hardware interface for communication
00011 //        between the FPGA and LPC1768
00012 //      * function to read required data from the DE0 (FPGA).
00013 /******************************************************************************/
00014 
00015 #include "mbed.h"
00016 #include "DE0_driver.h"
00017 
00018 #define DUMMY   0
00019 
00020 SPI DE0(p5, p6, p7); // Configure pins and name SPI port.
00021 
00022 void ResetDE0(void);
00023 void RestartSpi(void);
00024 
00025 DigitalOut IoReset(p15);    // 0-1-0 reset for all SPI peripherals in the DE0 FPGA
00026 DigitalOut SpiReset(p14);   // 0-1-0 tells the DE0 SPI interace that the next word sent is a control word
00027 
00028 int SignExtend(int16_t x)
00029 {
00030     // if number is negative
00031     if(x&0x00008000) {
00032         // reserve the sign bit into the 32-bit number
00033         x = x|0xFFFF0000;
00034     }
00035     return x;
00036 }
00037 
00038 
00039 // To reset all SPI peripheral on the FPGA:
00040 void ResetDE0(void){
00041     IoReset=0; // Reset all DE0 peripherals
00042     IoReset=1;
00043     wait_us(5);
00044     IoReset=0; 
00045     }
00046    
00047 // To reset the SPI channel in the slave and restart with a new SPI protocol.    
00048 void RestartSpi(void) {
00049     // Restart DE0 SPI channel so the next word sent is interpreted as the Control Word
00050     SpiReset=0;
00051     SpiReset=1;
00052     wait_us(5);
00053     SpiReset=0;
00054     }
00055 
00056 
00057 /*******************************************************************************
00058 * @brief    This is the initialization function for the DE0 FPGA - communication
00059             between the FPGA and MCU is done through SPI. The function 
00060             initializes the SPI channel to a bit rate of 500kbps
00061 * @param    none
00062 * @return   none
00063 *******************************************************************************/
00064 void DE0_init(void)
00065 {
00066  DE0.format(16,1); // Define SPI format: 16-bit words, mode 1 protocol.
00067  DE0.frequency(500000); // Set SPI bit rate (Hz). Default is 1 MHz
00068  ResetDE0();
00069 }
00070 
00071 /*******************************************************************************
00072 Before an SPI data transaction with the DE0 FPGA can occur, a control word must 
00073 first be written to the slave that specifies: 
00074 
00075 i) the number of word transfers,
00076 ii) the offset or starting address within the slave, and 
00077 iii) whether the transaction consists of ‘simultaneous read write’ or whether the 
00078 transactions are ‘read only’ (from the slave). 
00079 
00080 A control word need only be written once – if the same transactions are repeated.
00081 Subsequent transactions use the protocol specified by the last control word that
00082 was written. To change the transaction protocol a new control needs to be
00083 written. To write a new control word, the SpiReset input must first be set then
00084 cleared followed by a SPI write of a 16-bit word that is interpreted as the
00085 control word by the slave.
00086  
00087 The 16-bit control word format appears below.
00088 
00089 1        7bits            8bits
00090 rd | address offset | number of words 
00091 
00092 When RD = 1, no write operation occurs within the slave. Data on the MOSI is 
00093 ignored by the slave. When RD = 0, a simultaneous read + write of the slave 
00094 occurs.
00095 *******************************************************************************/
00096 
00097 
00098 /*******************************************************************************
00099 * @brief    This function reads the output from the FPGA through the SPI channel
00100 * @param    none
00101 * @return   none
00102 *******************************************************************************/
00103 void DE0_read(sensors_t * sensors)
00104 {
00105     // To place SPI module into control mode, where the next word received by the
00106     // slave is interpreted as a control word.
00107     RestartSpi();
00108     
00109     // rd = 1 (control if 1) address = 0 no of word = 2 - Specify a read only transactions of 2 words
00110     sensors->id = (uint16_t)DE0.write(0x8004);
00111     
00112     // Read the two 16 bits registers from the FPGA 
00113     sensors->dp_right = SignExtend(DE0.write(DUMMY)); // A SPI read only transaction occurs.
00114     sensors->dt_right = DE0.write(DUMMY); //
00115     
00116     sensors->dp_left = SignExtend(DE0.write(DUMMY)); // A SPI read only transaction occurs.
00117     sensors->dt_left = DE0.write(DUMMY); //
00118     
00119 }
00120