Devchannel Team / Mbed 2 deprecated SPIExample_Master

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 /*
00003     SPI Master used to test the FPGA slave
00004     https://developer.mbed.org/media/uploads/robt/mbed_course_notes_-_serial_spi.pdf
00005 */
00006 
00007 Serial serialIO(SERIAL_TX, SERIAL_RX);
00008 int main() {
00009     
00010     // Configure the serial speed.
00011     serialIO.baud(115200);
00012     serialIO.printf("SPI Master example with mbed!\r\n");
00013     
00014     // Configure the SPI to 8-bits Mode 0, 5Mhz
00015     // Create a SPI master
00016     SPI spiMaster(PA_7, PA_6, PA_5); // mosi, miso, sclk
00017     DigitalOut chipSelect(PB_6);
00018     spiMaster.format(8,0);    
00019     int frequency;
00020     serialIO.printf("Choose SPI freq:\r\n");
00021     serialIO.scanf("%d",&frequency);
00022     serialIO.printf("Setting frequency: %d\r\n",frequency);
00023     spiMaster.frequency(frequency); //5000000
00024     chipSelect = 1;    
00025     int mode = 0;
00026     int byteToSend = 0;
00027     int countPackages = 3;
00028     int foreverMode = 0;
00029                 
00030     while(1) {        
00031         if ((!byteToSend) || (countPackages == 0))
00032         {
00033             serialIO.printf("Type the mode:\r\n");
00034             serialIO.scanf("%d",&mode);
00035             spiMaster.format(8,mode);            
00036             serialIO.printf("Type the byte value to send, then press ENTER\r\n");
00037             serialIO.scanf("%d",&byteToSend);        
00038             serialIO.printf("Number of times to send, then press ENTER (-1) is forever\r\n");
00039             serialIO.scanf("%d",&countPackages);    
00040             if (countPackages < 0)     
00041             {    
00042                 foreverMode = 1;
00043                 serialIO.printf("Sending %d packages on mode %d with freq: %d (FOREVER)\r\n",countPackages,mode,frequency);
00044             }
00045             else                
00046                 serialIO.printf("Sending %d packages on mode %d with freq: %d\r\n",countPackages,mode,frequency);
00047                                 
00048         }
00049         chipSelect = 0;        
00050         int resp = spiMaster.write(byteToSend);                
00051         chipSelect = 1;        
00052         wait_us(2);
00053         if (!foreverMode)
00054         {
00055             serialIO.printf("Sending data<%d> received<%d> %d times\r\n",byteToSend, resp, countPackages);
00056             byteToSend++;
00057         } 
00058         else
00059         {
00060             serialIO.printf("Sending data<%d> received<%d> %d times at freq:%d\r\n",byteToSend, resp, countPackages,frequency);
00061         }
00062         countPackages--;        
00063     }
00064 }
00065