PlayBack

Dependencies:   TPixy-Interface

Fork of ObjectFollower by ECE4333 - 2018 - Ahmed & Brandon

Drivers/DE0_driver.cpp

Committer:
asobhy
Date:
2018-02-01
Revision:
0:a355e511bc5d
Child:
1:3e9684e81312

File content as of revision 0:a355e511bc5d:

// DE0 FPGA driver

#include "mbed.h"

#define DUMMY   0

SPI DE0(p5, p6, p7); // Configure pins and name SPI port.

void ResetDE0(void);
void RestartSpi(void);

DigitalOut IoReset(p15);    // 0-1-0 reset for all SPI peripherals in the DE0 FPGA
DigitalOut SpiReset(p14);   // 0-1-0 tells the DE0 SPI interace that the next word sent is a control word


// To reset all SPI peripheral on the FPGA:
void ResetDE0(void){
    IoReset=0; // Reset all DE0 peripherals
    IoReset=1;
    wait_us(5);
    IoReset=0; 
    }
   
// To reset the SPI channel in the slave and restart with a new SPI protocol.    
void RestartSpi(void) {
    // Restart DE0 SPI channel so the next word sent is interpreted as the Control Word
    SpiReset=0;
    SpiReset=1;
    wait_us(5);
    SpiReset=0;
    }

void DE0_init(void)
{
 DE0.format(16,1); // Define SPI format: 16-bit words, mode 1 protocol.
 DE0.frequency(500000); // Set SPI bit rate (Hz). Default is 1 MHz
 ResetDE0();
}

/*

Before an SPI data transaction with the DE0 FPGA can occur, a control word must 
first be written to the slave that specifies: 

i) the number of word transfers,
ii) the offset or starting address within the slave, and 
iii) whether the transaction consists of ‘simultaneous read write’ or whether the 
transactions are ‘read only’ (from the slave). 

A control word need only be written once – if the same transactions are repeated.
Subsequent transactions use the protocol specified by the last control word that
 was written. To change the transaction protocol a new control needs to be
 written. To write a new control word, the SpiReset input must first be set then
 cleared followed by a SPI write of a 16-bit word that is interpreted as the
 control word by the slave.
 
 The 16-bit control word format appears below.

1        7bits            8bits
rd | address offset | number of words 


When RD = 1, no write operation occurs within the slave. Data on the MOSI is 
ignored by the slave. When RD = 0, a simultaneous read + write of the slave 
occurs.
 
 */


void DE0_read(uint16_t * id, uint16_t * dP, uint16_t * dT)
{
    // To place SPI module into control mode, where the next word received by the
    // slave is interpreted as a control word.
    RestartSpi();
    
    // rd = 1 (control if 1) address = 0 no of word = 2 - Specify a read only transactions of 2 words
    *id = (uint16_t)DE0.write(0x8002);
    
    // Read the two 16 bits registers from the FPGA 
    *dP = (uint16_t)DE0.write(DUMMY); // A SPI read only transaction occurs.
    *dT = (uint16_t)DE0.write(DUMMY); //
}