![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Simple SPI Slave test Application, intended to be used with SSM to show problems with the SPISlave.receive() method
main.cpp@0:8a63b2e87af8, 2011-08-26 (annotated)
- Committer:
- picnic
- Date:
- Fri Aug 26 10:11:08 2011 +0000
- Revision:
- 0:8a63b2e87af8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
picnic | 0:8a63b2e87af8 | 1 | #include "mbed.h" |
picnic | 0:8a63b2e87af8 | 2 | |
picnic | 0:8a63b2e87af8 | 3 | // Simple Spi Slave |
picnic | 0:8a63b2e87af8 | 4 | // Simple SPI Slave receives bytes on the SPI and will echo back the byte received on the next request. |
picnic | 0:8a63b2e87af8 | 5 | |
picnic | 0:8a63b2e87af8 | 6 | DigitalOut myled(LED1); |
picnic | 0:8a63b2e87af8 | 7 | Serial pc(USBTX, USBRX); |
picnic | 0:8a63b2e87af8 | 8 | SPISlave spi(p11, p12, p13, p14); // mosi, miso, sclk, ssel |
picnic | 0:8a63b2e87af8 | 9 | |
picnic | 0:8a63b2e87af8 | 10 | int main() { |
picnic | 0:8a63b2e87af8 | 11 | int v = 0xff; |
picnic | 0:8a63b2e87af8 | 12 | long c = 0; |
picnic | 0:8a63b2e87af8 | 13 | int ch; |
picnic | 0:8a63b2e87af8 | 14 | int echoMode = 1; |
picnic | 0:8a63b2e87af8 | 15 | |
picnic | 0:8a63b2e87af8 | 16 | pc.printf("Simple SPI Slave Test\r\n"); |
picnic | 0:8a63b2e87af8 | 17 | pc.printf("Uses MBED's SPISlave library (1 byte in - 1 byte out)\r\n"); |
picnic | 0:8a63b2e87af8 | 18 | pc.printf("\r\n R : to add an extra reply() call\r\n" ); |
picnic | 0:8a63b2e87af8 | 19 | pc.printf(" I : Make a read() call\r\n" ); |
picnic | 0:8a63b2e87af8 | 20 | pc.printf(" S : Read receive status\r\n" ); |
picnic | 0:8a63b2e87af8 | 21 | pc.printf(" N : No reply()\r\n" ); |
picnic | 0:8a63b2e87af8 | 22 | spi.format(8,3); |
picnic | 0:8a63b2e87af8 | 23 | |
picnic | 0:8a63b2e87af8 | 24 | spi.reply( v ); |
picnic | 0:8a63b2e87af8 | 25 | |
picnic | 0:8a63b2e87af8 | 26 | while(1) { |
picnic | 0:8a63b2e87af8 | 27 | if( spi.receive() ) { |
picnic | 0:8a63b2e87af8 | 28 | // Data available - our trigger to do something |
picnic | 0:8a63b2e87af8 | 29 | v = spi.read(); // Read byte from master |
picnic | 0:8a63b2e87af8 | 30 | if ( echoMode ) { |
picnic | 0:8a63b2e87af8 | 31 | // and send last data back |
picnic | 0:8a63b2e87af8 | 32 | spi.reply( v ); |
picnic | 0:8a63b2e87af8 | 33 | } |
picnic | 0:8a63b2e87af8 | 34 | pc.printf( "%ld In %02x\r\n", c++, v ); |
picnic | 0:8a63b2e87af8 | 35 | } |
picnic | 0:8a63b2e87af8 | 36 | |
picnic | 0:8a63b2e87af8 | 37 | if ( pc.readable() ) { |
picnic | 0:8a63b2e87af8 | 38 | ch = pc.getc(); |
picnic | 0:8a63b2e87af8 | 39 | switch ( ch ) { |
picnic | 0:8a63b2e87af8 | 40 | case 'R': |
picnic | 0:8a63b2e87af8 | 41 | case 'r': |
picnic | 0:8a63b2e87af8 | 42 | spi.reply( 'B' ); |
picnic | 0:8a63b2e87af8 | 43 | pc.printf( "Bogus Replay added\r\n" ); |
picnic | 0:8a63b2e87af8 | 44 | break; |
picnic | 0:8a63b2e87af8 | 45 | case 'I': |
picnic | 0:8a63b2e87af8 | 46 | case 'i': |
picnic | 0:8a63b2e87af8 | 47 | v = spi.read(); |
picnic | 0:8a63b2e87af8 | 48 | pc.printf( "Extra read = %02x\r\n", v ); |
picnic | 0:8a63b2e87af8 | 49 | break; |
picnic | 0:8a63b2e87af8 | 50 | case 'S': |
picnic | 0:8a63b2e87af8 | 51 | case 's': |
picnic | 0:8a63b2e87af8 | 52 | v = spi.receive(); |
picnic | 0:8a63b2e87af8 | 53 | pc.printf( "Status = %02x\r\n", v ); |
picnic | 0:8a63b2e87af8 | 54 | break; |
picnic | 0:8a63b2e87af8 | 55 | case 'N': |
picnic | 0:8a63b2e87af8 | 56 | case 'n': |
picnic | 0:8a63b2e87af8 | 57 | echoMode = !echoMode; |
picnic | 0:8a63b2e87af8 | 58 | pc.printf( "Echo mode = %d\r\n", (echoMode & 1) ); |
picnic | 0:8a63b2e87af8 | 59 | break; |
picnic | 0:8a63b2e87af8 | 60 | } |
picnic | 0:8a63b2e87af8 | 61 | } |
picnic | 0:8a63b2e87af8 | 62 | } |
picnic | 0:8a63b2e87af8 | 63 | } |