SPI Master used to test FPGA spi slave

Dependencies:   mbed

Committer:
gaelito
Date:
Thu Sep 24 07:07:28 2015 +0000
Revision:
1:f05471667f60
Parent:
0:06683d7261a3
Child:
2:24e22b31819c
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leonardoaraujosantos 0:06683d7261a3 1 #include "mbed.h"
gaelito 1:f05471667f60 2 /**/
leonardoaraujosantos 0:06683d7261a3 3 // https://developer.mbed.org/media/uploads/robt/mbed_course_notes_-_serial_spi.pdf
leonardoaraujosantos 0:06683d7261a3 4 Serial serialIO(SERIAL_TX, SERIAL_RX);
leonardoaraujosantos 0:06683d7261a3 5 int main() {
leonardoaraujosantos 0:06683d7261a3 6
leonardoaraujosantos 0:06683d7261a3 7 // Configure the serial speed.
leonardoaraujosantos 0:06683d7261a3 8 serialIO.baud(115200);
leonardoaraujosantos 0:06683d7261a3 9 serialIO.printf("SPI Master example with mbed!\r\n");
leonardoaraujosantos 0:06683d7261a3 10
leonardoaraujosantos 0:06683d7261a3 11 // Configure the SPI to 8-bits Mode 0, 5Mhz
leonardoaraujosantos 0:06683d7261a3 12 // Create a SPI master
leonardoaraujosantos 0:06683d7261a3 13 SPI spiMaster(PA_7, PA_6, PA_5); // mosi, miso, sclk
leonardoaraujosantos 0:06683d7261a3 14 DigitalOut chipSelect(PB_6);
leonardoaraujosantos 0:06683d7261a3 15 spiMaster.format(8,0);
leonardoaraujosantos 0:06683d7261a3 16 spiMaster.frequency(5000000);
leonardoaraujosantos 0:06683d7261a3 17 chipSelect = 1;
leonardoaraujosantos 0:06683d7261a3 18 int mode = 0;
leonardoaraujosantos 0:06683d7261a3 19 int byteToSend = 0;
leonardoaraujosantos 0:06683d7261a3 20 int countPackages = 3;
leonardoaraujosantos 0:06683d7261a3 21
leonardoaraujosantos 0:06683d7261a3 22 while(1) {
leonardoaraujosantos 0:06683d7261a3 23 if ((!byteToSend) || (countPackages == 0))
leonardoaraujosantos 0:06683d7261a3 24 {
leonardoaraujosantos 0:06683d7261a3 25 serialIO.printf("Type the mode:\r\n");
leonardoaraujosantos 0:06683d7261a3 26 serialIO.scanf("%d",&mode);
leonardoaraujosantos 0:06683d7261a3 27 spiMaster.format(8,mode);
leonardoaraujosantos 0:06683d7261a3 28 serialIO.printf("Type the byte value to send, then press ENTER\r\n");
leonardoaraujosantos 0:06683d7261a3 29 serialIO.scanf("%d",&byteToSend);
leonardoaraujosantos 0:06683d7261a3 30 serialIO.printf("Number of times to send, then press ENTER\r\n");
leonardoaraujosantos 0:06683d7261a3 31 serialIO.scanf("%d",&countPackages);
leonardoaraujosantos 0:06683d7261a3 32 serialIO.printf("Sending %d packages on mode %d\r\n",countPackages,mode);
leonardoaraujosantos 0:06683d7261a3 33 }
leonardoaraujosantos 0:06683d7261a3 34 chipSelect = 0;
leonardoaraujosantos 0:06683d7261a3 35 int resp = spiMaster.write(byteToSend);
leonardoaraujosantos 0:06683d7261a3 36 chipSelect = 1;
leonardoaraujosantos 0:06683d7261a3 37 serialIO.printf("Sending data<%d> received<%d> %d times\r\n",byteToSend, resp, countPackages);
leonardoaraujosantos 0:06683d7261a3 38 wait_us(2);
leonardoaraujosantos 0:06683d7261a3 39 byteToSend++;
leonardoaraujosantos 0:06683d7261a3 40 countPackages--;
leonardoaraujosantos 0:06683d7261a3 41 }
leonardoaraujosantos 0:06683d7261a3 42 }
leonardoaraujosantos 0:06683d7261a3 43