SPI Master Test

Dependencies:   mbed-rtos mbed st7565LCD

Revision:
0:36cebc939c49
Child:
1:74e13cd94576
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Sep 29 05:55:59 2016 +0000
@@ -0,0 +1,53 @@
+#include "mbed.h"
+#include "rtos.h"
+
+#define SPI_SPEED   (10000000)
+
+BusOut Leds(PA_8, PB_10, PB_4, PB_5);
+BusIn Switches(PA_0, PA_1, PA_4, PB_0, PC_1, PC_0);
+
+SPI SpiM(PA_7, PA_6, PA_5); // mosi, miso, sclk
+DigitalOut SpiMCs(PB_6);
+
+uint8_t prevSendVal = 0x00;
+
+int main()
+{
+    printf("\r\n\nNucleo rtos SPI Master Test..\r\n");
+    
+    // LED Check
+    for (int i = 0; i <  5; i++) {
+        Leds.write(0x0f);
+        Thread::wait(100);
+        Leds.write(0x00);
+        Thread::wait(100);
+    }
+    
+    // Setup Switches
+    Switches.mode(PullUp);
+    /*
+    while(1) {
+        printf("%x\r\n", ~Switches.read() &0x3f);
+        Thread::wait(100);
+    }
+    */
+    
+    // Setup SPI
+    SpiMCs = 1;
+    SpiM.format(8, 0);
+    SpiM.frequency(SPI_SPEED);
+    
+    for (;;) {
+        uint8_t sendVal = ~Switches.read();
+        
+        if (prevSendVal != sendVal) {
+            SpiMCs = 0;
+            uint8_t receivedVal = SpiM.write(sendVal);
+            SpiMCs = 1;
+            
+            prevSendVal = sendVal;
+            
+            Leds.write(receivedVal & 0x0f);
+        }
+    }
+}