SPI Master used to test FPGA spi slave

Dependencies:   mbed

main.cpp

Committer:
leonardoaraujosantos
Date:
2015-09-24
Revision:
0:06683d7261a3
Child:
1:f05471667f60

File content as of revision 0:06683d7261a3:

#include "mbed.h"
// https://developer.mbed.org/media/uploads/robt/mbed_course_notes_-_serial_spi.pdf
Serial serialIO(SERIAL_TX, SERIAL_RX);
int main() {
    
    // Configure the serial speed.
    serialIO.baud(115200);
    serialIO.printf("SPI Master example with mbed!\r\n");
    
    // Configure the SPI to 8-bits Mode 0, 5Mhz
    // Create a SPI master
    SPI spiMaster(PA_7, PA_6, PA_5); // mosi, miso, sclk
    DigitalOut chipSelect(PB_6);
    spiMaster.format(8,0);    
    spiMaster.frequency(5000000);
    chipSelect = 1;    
    int mode = 0;
    int byteToSend = 0;
    int countPackages = 3;
                
    while(1) {        
        if ((!byteToSend) || (countPackages == 0))
        {
            serialIO.printf("Type the mode:\r\n");
            serialIO.scanf("%d",&mode);
            spiMaster.format(8,mode);            
            serialIO.printf("Type the byte value to send, then press ENTER\r\n");
            serialIO.scanf("%d",&byteToSend);        
            serialIO.printf("Number of times to send, then press ENTER\r\n");
            serialIO.scanf("%d",&countPackages);                    
            serialIO.printf("Sending %d packages on mode %d\r\n",countPackages,mode);
        }
        chipSelect = 0;        
        int resp = spiMaster.write(byteToSend);                
        chipSelect = 1;
        serialIO.printf("Sending data<%d> received<%d> %d times\r\n",byteToSend, resp, countPackages);
        wait_us(2);
        byteToSend++;
        countPackages--;        
    }
}