SPI Master used to test FPGA spi slave

Dependencies:   mbed

Committer:
leonardoaraujosantos
Date:
Fri Sep 25 15:06:28 2015 +0000
Revision:
2:24e22b31819c
Parent:
1:f05471667f60
Changing frequency

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leonardoaraujosantos 0:06683d7261a3 1 #include "mbed.h"
leonardoaraujosantos 2:24e22b31819c 2 /*
leonardoaraujosantos 2:24e22b31819c 3 SPI Master used to test the FPGA slave
leonardoaraujosantos 2:24e22b31819c 4 https://developer.mbed.org/media/uploads/robt/mbed_course_notes_-_serial_spi.pdf
leonardoaraujosantos 2:24e22b31819c 5 */
leonardoaraujosantos 2:24e22b31819c 6
leonardoaraujosantos 0:06683d7261a3 7 Serial serialIO(SERIAL_TX, SERIAL_RX);
leonardoaraujosantos 0:06683d7261a3 8 int main() {
leonardoaraujosantos 0:06683d7261a3 9
leonardoaraujosantos 0:06683d7261a3 10 // Configure the serial speed.
leonardoaraujosantos 0:06683d7261a3 11 serialIO.baud(115200);
leonardoaraujosantos 0:06683d7261a3 12 serialIO.printf("SPI Master example with mbed!\r\n");
leonardoaraujosantos 0:06683d7261a3 13
leonardoaraujosantos 0:06683d7261a3 14 // Configure the SPI to 8-bits Mode 0, 5Mhz
leonardoaraujosantos 0:06683d7261a3 15 // Create a SPI master
leonardoaraujosantos 0:06683d7261a3 16 SPI spiMaster(PA_7, PA_6, PA_5); // mosi, miso, sclk
leonardoaraujosantos 0:06683d7261a3 17 DigitalOut chipSelect(PB_6);
leonardoaraujosantos 0:06683d7261a3 18 spiMaster.format(8,0);
leonardoaraujosantos 2:24e22b31819c 19 int frequency;
leonardoaraujosantos 2:24e22b31819c 20 serialIO.printf("Choose SPI freq:\r\n");
leonardoaraujosantos 2:24e22b31819c 21 serialIO.scanf("%d",&frequency);
leonardoaraujosantos 2:24e22b31819c 22 serialIO.printf("Setting frequency: %d\r\n",frequency);
leonardoaraujosantos 2:24e22b31819c 23 spiMaster.frequency(frequency); //5000000
leonardoaraujosantos 0:06683d7261a3 24 chipSelect = 1;
leonardoaraujosantos 0:06683d7261a3 25 int mode = 0;
leonardoaraujosantos 0:06683d7261a3 26 int byteToSend = 0;
leonardoaraujosantos 0:06683d7261a3 27 int countPackages = 3;
leonardoaraujosantos 2:24e22b31819c 28 int foreverMode = 0;
leonardoaraujosantos 0:06683d7261a3 29
leonardoaraujosantos 0:06683d7261a3 30 while(1) {
leonardoaraujosantos 0:06683d7261a3 31 if ((!byteToSend) || (countPackages == 0))
leonardoaraujosantos 0:06683d7261a3 32 {
leonardoaraujosantos 0:06683d7261a3 33 serialIO.printf("Type the mode:\r\n");
leonardoaraujosantos 0:06683d7261a3 34 serialIO.scanf("%d",&mode);
leonardoaraujosantos 0:06683d7261a3 35 spiMaster.format(8,mode);
leonardoaraujosantos 0:06683d7261a3 36 serialIO.printf("Type the byte value to send, then press ENTER\r\n");
leonardoaraujosantos 0:06683d7261a3 37 serialIO.scanf("%d",&byteToSend);
leonardoaraujosantos 2:24e22b31819c 38 serialIO.printf("Number of times to send, then press ENTER (-1) is forever\r\n");
leonardoaraujosantos 2:24e22b31819c 39 serialIO.scanf("%d",&countPackages);
leonardoaraujosantos 2:24e22b31819c 40 if (countPackages < 0)
leonardoaraujosantos 2:24e22b31819c 41 {
leonardoaraujosantos 2:24e22b31819c 42 foreverMode = 1;
leonardoaraujosantos 2:24e22b31819c 43 serialIO.printf("Sending %d packages on mode %d with freq: %d (FOREVER)\r\n",countPackages,mode,frequency);
leonardoaraujosantos 2:24e22b31819c 44 }
leonardoaraujosantos 2:24e22b31819c 45 else
leonardoaraujosantos 2:24e22b31819c 46 serialIO.printf("Sending %d packages on mode %d with freq: %d\r\n",countPackages,mode,frequency);
leonardoaraujosantos 2:24e22b31819c 47
leonardoaraujosantos 0:06683d7261a3 48 }
leonardoaraujosantos 0:06683d7261a3 49 chipSelect = 0;
leonardoaraujosantos 0:06683d7261a3 50 int resp = spiMaster.write(byteToSend);
leonardoaraujosantos 2:24e22b31819c 51 chipSelect = 1;
leonardoaraujosantos 0:06683d7261a3 52 wait_us(2);
leonardoaraujosantos 2:24e22b31819c 53 if (!foreverMode)
leonardoaraujosantos 2:24e22b31819c 54 {
leonardoaraujosantos 2:24e22b31819c 55 serialIO.printf("Sending data<%d> received<%d> %d times\r\n",byteToSend, resp, countPackages);
leonardoaraujosantos 2:24e22b31819c 56 byteToSend++;
leonardoaraujosantos 2:24e22b31819c 57 }
leonardoaraujosantos 2:24e22b31819c 58 else
leonardoaraujosantos 2:24e22b31819c 59 {
leonardoaraujosantos 2:24e22b31819c 60 serialIO.printf("Sending data<%d> received<%d> %d times at freq:%d\r\n",byteToSend, resp, countPackages,frequency);
leonardoaraujosantos 2:24e22b31819c 61 }
leonardoaraujosantos 0:06683d7261a3 62 countPackages--;
leonardoaraujosantos 0:06683d7261a3 63 }
leonardoaraujosantos 0:06683d7261a3 64 }
leonardoaraujosantos 0:06683d7261a3 65