PlayBack

Dependencies:   TPixy-Interface

Fork of ObjectFollower by ECE4333 - 2018 - Ahmed & Brandon

Committer:
asobhy
Date:
Thu Feb 15 01:15:26 2018 +0000
Revision:
7:73fd05fe556a
Parent:
1:3e9684e81312
Child:
8:a0890fa79084
Added comments to several files and organized the code a bit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
asobhy 0:a355e511bc5d 1 // DE0 FPGA driver
asobhy 0:a355e511bc5d 2
asobhy 0:a355e511bc5d 3 #include "mbed.h"
asobhy 0:a355e511bc5d 4
asobhy 0:a355e511bc5d 5 #define DUMMY 0
asobhy 0:a355e511bc5d 6
asobhy 0:a355e511bc5d 7 SPI DE0(p5, p6, p7); // Configure pins and name SPI port.
asobhy 0:a355e511bc5d 8
asobhy 0:a355e511bc5d 9 void ResetDE0(void);
asobhy 0:a355e511bc5d 10 void RestartSpi(void);
asobhy 0:a355e511bc5d 11
asobhy 0:a355e511bc5d 12 DigitalOut IoReset(p15); // 0-1-0 reset for all SPI peripherals in the DE0 FPGA
asobhy 0:a355e511bc5d 13 DigitalOut SpiReset(p14); // 0-1-0 tells the DE0 SPI interace that the next word sent is a control word
asobhy 0:a355e511bc5d 14
asobhy 0:a355e511bc5d 15
asobhy 1:3e9684e81312 16 int SignExtend(int16_t x)
asobhy 1:3e9684e81312 17 {
asobhy 1:3e9684e81312 18 // if number is negative
asobhy 1:3e9684e81312 19 if(x&0x00008000) {
asobhy 1:3e9684e81312 20 // reserve the sign bit into the 32-bit number
asobhy 1:3e9684e81312 21 x = x|0xFFFF0000;
asobhy 1:3e9684e81312 22 }
asobhy 1:3e9684e81312 23 return x;
asobhy 1:3e9684e81312 24 }
asobhy 1:3e9684e81312 25
asobhy 1:3e9684e81312 26
asobhy 0:a355e511bc5d 27 // To reset all SPI peripheral on the FPGA:
asobhy 0:a355e511bc5d 28 void ResetDE0(void){
asobhy 0:a355e511bc5d 29 IoReset=0; // Reset all DE0 peripherals
asobhy 0:a355e511bc5d 30 IoReset=1;
asobhy 0:a355e511bc5d 31 wait_us(5);
asobhy 0:a355e511bc5d 32 IoReset=0;
asobhy 0:a355e511bc5d 33 }
asobhy 0:a355e511bc5d 34
asobhy 0:a355e511bc5d 35 // To reset the SPI channel in the slave and restart with a new SPI protocol.
asobhy 0:a355e511bc5d 36 void RestartSpi(void) {
asobhy 0:a355e511bc5d 37 // Restart DE0 SPI channel so the next word sent is interpreted as the Control Word
asobhy 0:a355e511bc5d 38 SpiReset=0;
asobhy 0:a355e511bc5d 39 SpiReset=1;
asobhy 0:a355e511bc5d 40 wait_us(5);
asobhy 0:a355e511bc5d 41 SpiReset=0;
asobhy 0:a355e511bc5d 42 }
asobhy 0:a355e511bc5d 43
asobhy 7:73fd05fe556a 44
asobhy 7:73fd05fe556a 45 /*******************************************************************************
asobhy 7:73fd05fe556a 46 * @brief This is the initialization function for the DE0 FPGA - communication
asobhy 7:73fd05fe556a 47 between the FPGA and MCU is done through SPI. The function
asobhy 7:73fd05fe556a 48 initializes the SPI channel to a bit rate of 500kbps
asobhy 7:73fd05fe556a 49 * @param none
asobhy 7:73fd05fe556a 50 * @return none
asobhy 7:73fd05fe556a 51 *******************************************************************************/
asobhy 0:a355e511bc5d 52 void DE0_init(void)
asobhy 0:a355e511bc5d 53 {
asobhy 0:a355e511bc5d 54 DE0.format(16,1); // Define SPI format: 16-bit words, mode 1 protocol.
asobhy 0:a355e511bc5d 55 DE0.frequency(500000); // Set SPI bit rate (Hz). Default is 1 MHz
asobhy 0:a355e511bc5d 56 ResetDE0();
asobhy 0:a355e511bc5d 57 }
asobhy 0:a355e511bc5d 58
asobhy 7:73fd05fe556a 59 /*******************************************************************************
asobhy 0:a355e511bc5d 60 Before an SPI data transaction with the DE0 FPGA can occur, a control word must
asobhy 0:a355e511bc5d 61 first be written to the slave that specifies:
asobhy 0:a355e511bc5d 62
asobhy 0:a355e511bc5d 63 i) the number of word transfers,
asobhy 0:a355e511bc5d 64 ii) the offset or starting address within the slave, and
asobhy 0:a355e511bc5d 65 iii) whether the transaction consists of ‘simultaneous read write’ or whether the
asobhy 0:a355e511bc5d 66 transactions are ‘read only’ (from the slave).
asobhy 0:a355e511bc5d 67
asobhy 0:a355e511bc5d 68 A control word need only be written once – if the same transactions are repeated.
asobhy 0:a355e511bc5d 69 Subsequent transactions use the protocol specified by the last control word that
asobhy 7:73fd05fe556a 70 was written. To change the transaction protocol a new control needs to be
asobhy 7:73fd05fe556a 71 written. To write a new control word, the SpiReset input must first be set then
asobhy 7:73fd05fe556a 72 cleared followed by a SPI write of a 16-bit word that is interpreted as the
asobhy 7:73fd05fe556a 73 control word by the slave.
asobhy 0:a355e511bc5d 74
asobhy 7:73fd05fe556a 75 The 16-bit control word format appears below.
asobhy 0:a355e511bc5d 76
asobhy 0:a355e511bc5d 77 1 7bits 8bits
asobhy 0:a355e511bc5d 78 rd | address offset | number of words
asobhy 0:a355e511bc5d 79
asobhy 0:a355e511bc5d 80 When RD = 1, no write operation occurs within the slave. Data on the MOSI is
asobhy 0:a355e511bc5d 81 ignored by the slave. When RD = 0, a simultaneous read + write of the slave
asobhy 0:a355e511bc5d 82 occurs.
asobhy 7:73fd05fe556a 83 *******************************************************************************/
asobhy 0:a355e511bc5d 84
asobhy 0:a355e511bc5d 85
asobhy 7:73fd05fe556a 86 /*******************************************************************************
asobhy 7:73fd05fe556a 87 * @brief This function reads the output from the FPGA through the SPI channel
asobhy 7:73fd05fe556a 88 * @param none
asobhy 7:73fd05fe556a 89 * @return none
asobhy 7:73fd05fe556a 90 *******************************************************************************/
asobhy 1:3e9684e81312 91 void DE0_read(uint16_t * id, int * dP, uint16_t * dT)
asobhy 0:a355e511bc5d 92 {
asobhy 0:a355e511bc5d 93 // To place SPI module into control mode, where the next word received by the
asobhy 0:a355e511bc5d 94 // slave is interpreted as a control word.
asobhy 0:a355e511bc5d 95 RestartSpi();
asobhy 0:a355e511bc5d 96
asobhy 0:a355e511bc5d 97 // rd = 1 (control if 1) address = 0 no of word = 2 - Specify a read only transactions of 2 words
asobhy 0:a355e511bc5d 98 *id = (uint16_t)DE0.write(0x8002);
asobhy 0:a355e511bc5d 99
asobhy 0:a355e511bc5d 100 // Read the two 16 bits registers from the FPGA
asobhy 1:3e9684e81312 101 *dP = SignExtend(DE0.write(DUMMY)); // A SPI read only transaction occurs.
asobhy 1:3e9684e81312 102 *dT = DE0.write(DUMMY); //
asobhy 0:a355e511bc5d 103 }
asobhy 0:a355e511bc5d 104