SPI Master used to test FPGA spi slave
Dependencies: mbed
main.cpp@0:06683d7261a3, 2015-09-24 (annotated)
- Committer:
- leonardoaraujosantos
- Date:
- Thu Sep 24 07:03:02 2015 +0000
- Revision:
- 0:06683d7261a3
- Child:
- 1:f05471667f60
Working on Master
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leonardoaraujosantos | 0:06683d7261a3 | 1 | #include "mbed.h" |
leonardoaraujosantos | 0:06683d7261a3 | 2 | // https://developer.mbed.org/media/uploads/robt/mbed_course_notes_-_serial_spi.pdf |
leonardoaraujosantos | 0:06683d7261a3 | 3 | Serial serialIO(SERIAL_TX, SERIAL_RX); |
leonardoaraujosantos | 0:06683d7261a3 | 4 | int main() { |
leonardoaraujosantos | 0:06683d7261a3 | 5 | |
leonardoaraujosantos | 0:06683d7261a3 | 6 | // Configure the serial speed. |
leonardoaraujosantos | 0:06683d7261a3 | 7 | serialIO.baud(115200); |
leonardoaraujosantos | 0:06683d7261a3 | 8 | serialIO.printf("SPI Master example with mbed!\r\n"); |
leonardoaraujosantos | 0:06683d7261a3 | 9 | |
leonardoaraujosantos | 0:06683d7261a3 | 10 | // Configure the SPI to 8-bits Mode 0, 5Mhz |
leonardoaraujosantos | 0:06683d7261a3 | 11 | // Create a SPI master |
leonardoaraujosantos | 0:06683d7261a3 | 12 | SPI spiMaster(PA_7, PA_6, PA_5); // mosi, miso, sclk |
leonardoaraujosantos | 0:06683d7261a3 | 13 | DigitalOut chipSelect(PB_6); |
leonardoaraujosantos | 0:06683d7261a3 | 14 | spiMaster.format(8,0); |
leonardoaraujosantos | 0:06683d7261a3 | 15 | spiMaster.frequency(5000000); |
leonardoaraujosantos | 0:06683d7261a3 | 16 | chipSelect = 1; |
leonardoaraujosantos | 0:06683d7261a3 | 17 | int mode = 0; |
leonardoaraujosantos | 0:06683d7261a3 | 18 | int byteToSend = 0; |
leonardoaraujosantos | 0:06683d7261a3 | 19 | int countPackages = 3; |
leonardoaraujosantos | 0:06683d7261a3 | 20 | |
leonardoaraujosantos | 0:06683d7261a3 | 21 | while(1) { |
leonardoaraujosantos | 0:06683d7261a3 | 22 | if ((!byteToSend) || (countPackages == 0)) |
leonardoaraujosantos | 0:06683d7261a3 | 23 | { |
leonardoaraujosantos | 0:06683d7261a3 | 24 | serialIO.printf("Type the mode:\r\n"); |
leonardoaraujosantos | 0:06683d7261a3 | 25 | serialIO.scanf("%d",&mode); |
leonardoaraujosantos | 0:06683d7261a3 | 26 | spiMaster.format(8,mode); |
leonardoaraujosantos | 0:06683d7261a3 | 27 | serialIO.printf("Type the byte value to send, then press ENTER\r\n"); |
leonardoaraujosantos | 0:06683d7261a3 | 28 | serialIO.scanf("%d",&byteToSend); |
leonardoaraujosantos | 0:06683d7261a3 | 29 | serialIO.printf("Number of times to send, then press ENTER\r\n"); |
leonardoaraujosantos | 0:06683d7261a3 | 30 | serialIO.scanf("%d",&countPackages); |
leonardoaraujosantos | 0:06683d7261a3 | 31 | serialIO.printf("Sending %d packages on mode %d\r\n",countPackages,mode); |
leonardoaraujosantos | 0:06683d7261a3 | 32 | } |
leonardoaraujosantos | 0:06683d7261a3 | 33 | chipSelect = 0; |
leonardoaraujosantos | 0:06683d7261a3 | 34 | int resp = spiMaster.write(byteToSend); |
leonardoaraujosantos | 0:06683d7261a3 | 35 | chipSelect = 1; |
leonardoaraujosantos | 0:06683d7261a3 | 36 | serialIO.printf("Sending data<%d> received<%d> %d times\r\n",byteToSend, resp, countPackages); |
leonardoaraujosantos | 0:06683d7261a3 | 37 | wait_us(2); |
leonardoaraujosantos | 0:06683d7261a3 | 38 | byteToSend++; |
leonardoaraujosantos | 0:06683d7261a3 | 39 | countPackages--; |
leonardoaraujosantos | 0:06683d7261a3 | 40 | } |
leonardoaraujosantos | 0:06683d7261a3 | 41 | } |
leonardoaraujosantos | 0:06683d7261a3 | 42 |