test

Revision:
0:8491d8911089
Child:
1:397813654863
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Feb 08 18:35:32 2021 +0000
@@ -0,0 +1,87 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2019 ARM Limited
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include "mbed.h"
+#include "platform/mbed_thread.h"
+
+
+// Blinking rate in milliseconds
+#define BLINKING_RATE_MS                                                    500
+
+#define RST_PIN   P5_6
+#define MFIO_PIN  P5_4
+
+I2C i2c(P3_4, P3_5);
+
+const int addr = 0xAA;
+
+void scanI2C() {
+        printf("Searching for I2C devices...\n\r");
+ 
+        int count = 0;
+        for (int address = 0; address < 255; address +=2) { // check only for device's read addres
+            if (!i2c.write(address, NULL, 0)) { // 0 returned is ok
+                printf("I2C device found at address 0x%02X (0x%02X in 8-bit)\n\r", address >> 1, address);  // the address is without LSB, which is R/W flag. shoft it right once
+                count++;
+            }
+            thread_sleep_for(1);
+        }
+        if (count)
+            printf("%d", count);
+        else
+            printf("No");
+        printf(" device%c found\n\r\n", count == 1?'\0':'s');
+}
+
+int main()
+{   
+    i2c.frequency(400000);
+    char cmd[4];
+    
+    scanI2C();
+    
+    // Initialise the digital pin LED1 as an output
+    DigitalOut led(LED1);
+    DigitalOut rst(RST_PIN);
+    DigitalOut mfio(MFIO_PIN);
+    
+    mfio = 1;
+    rst = 1;
+    thread_sleep_for(10);
+    rst = 0;
+    thread_sleep_for(10);
+    rst = 1;
+    thread_sleep_for(1500);
+
+    while (true) {
+        cmd[0] = 0x02;
+        cmd[1] = 0x00;
+        i2c.write(addr, cmd, 2);
+        thread_sleep_for(2);
+        
+        cmd[0] = 0x00;
+        cmd[1] = 0x00;
+        i2c.read(addr, cmd, 2);
+        printf("1: %x %x\n", cmd[0], cmd[1]);
+        
+        
+        cmd[0] = 0xFF;
+        cmd[1] = 0x03;
+        i2c.write(addr, cmd, 2);
+        thread_sleep_for(2);
+        
+        cmd[0] = 0x00;
+        cmd[1] = 0x00;
+        cmd[2] = 0x00;
+        cmd[3] = 0x00;
+        i2c.read(addr, cmd, 4);
+        printf("2: %d %d %d %d\n", cmd[0], cmd[1], cmd[2], cmd[3]);
+
+        /*rst =! rst;
+        mfio =! mfio;*/
+        thread_sleep_for(BLINKING_RATE_MS);
+        led = !led;
+    }
+}
\ No newline at end of file