Test MLDP code for Microchip RN4020 BLE

Dependencies:   mbed-src mbed-rtos MODSERIAL

Files at this revision

API Documentation at this revision

Comitter:
edodm85
Date:
Sat May 13 11:11:00 2017 +0000
Child:
1:2b5aaaddd35e
Commit message:
Init version

Changed in this revision

MODSERIAL.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed-src.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MODSERIAL.lib	Sat May 13 11:11:00 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AjK/code/MODSERIAL/#ae0408ebdd68
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat May 13 11:11:00 2017 +0000
@@ -0,0 +1,214 @@
+/*
+ * Author: Edoardo De Marchi
+ * Date: 17-05-13
+ * Notes: Test code for Microcip RN4020 BLE
+*/
+
+
+#include "main.h"
+
+
+// Send command to ble
+void send_cmd(char* cmd) 
+{
+    ble_rn.printf("%s", cmd); 
+}
+
+ 
+
+// write to phone via bluetooth
+void write_characteristic(char* data, int length) 
+{  
+    memset(buffer, 0, sizeof(buffer));
+    sprintf(buffer, "SUW,%s,%s\n", uuidR, data);
+    send_cmd(buffer);
+}
+ 
+ 
+ 
+// init ble
+void setup_ble() 
+{
+    // Set the WAKE_SW pin high to enter Command mode.
+    wakeSw = 1; 
+    wakeHw = 0;
+    
+    // enable Command Mode
+    cmdMldp = 0;                        // (0 --> CMD, 1 --> MLDP)
+
+
+    // reset to the factory default configuration
+    send_cmd("SF,1\n");
+    osDelay(500);                 
+    pc.printf("SF,1\n");
+
+
+    // sets the services supported
+    send_cmd("SS,C0000001\n");          // Private Service + Device Information
+    osDelay(500);
+    pc.printf("SS,C0000001\n");
+    
+    
+    // sets the supported features of current RN4020 module
+    send_cmd("SR,20002000\n");          // Auto Advertise-Server Only                                        
+    osDelay(500);
+    pc.printf("SR,20002000\n");
+   
+    
+    // sets the device name
+    send_cmd("SN,RN4020\n");           
+    osDelay(500);
+    pc.printf("SN,RN4020\n");
+    
+    
+    // Clear private service
+    send_cmd("PZ\n");                               
+    osDelay(500);
+    pc.printf("PZ\n");
+
+    
+    // Set private service UUID
+    memset(buffer, 0, sizeof(buffer));
+    sprintf(buffer, "PS,%s\n", suuid);              
+    send_cmd(buffer);
+    osDelay(500);
+    pc.printf("%s", buffer);
+    
+
+    // Set private characteristic UUID Read/Notify
+    memset(buffer, 0, sizeof(buffer));
+    sprintf(buffer, "PC,%s,12,14\n", uuidR);         // Read + Notify  10010b = 12hex and maximum data size of 20 bytes
+    send_cmd(buffer);
+    osDelay(500);
+    pc.printf("%s", buffer);
+    
+    
+    // Set private characteristic UUID Write
+    memset(buffer, 0, sizeof(buffer));
+    sprintf(buffer, "PC,%s,08,14\n", uuidW);         // Write  1000b = 08hex and maximum data size of 20 bytes
+    send_cmd(buffer);
+    osDelay(500);
+    pc.printf("%s", buffer);
+    
+    
+    // reboot the RN4020 module and to make the new settings effective
+    send_cmd("R,1\n"); 
+    osDelay(500);                 
+    pc.printf("R,1\n");
+  
+  
+    // Start advertising
+    send_cmd("A\n");
+    osDelay(500);                   
+    pc.printf("A\n");
+    
+    
+    osDelay(2000); 
+
+
+    // Show list of Services
+    send_cmd("LS\n");
+    osDelay(500);                   
+    pc.printf("LS\n");
+    
+      
+    start_ble = true;
+} 
+ 
+
+
+// This function is called when a character goes into the RX buffer.
+void rxBleCallback(MODSERIAL_IRQ_INFO *q) 
+{
+    led2 = 1;
+    new_from_ble = true;
+}
+
+// This function is called when a character goes into the RX buffer.
+void rxPcCallback(MODSERIAL_IRQ_INFO *q) 
+{
+    led3 = 1;
+    new_from_pc = true;
+} 
+
+
+
+// read from BLE
+void read_thread(void const *argument)
+{
+    while (true) 
+    {
+        if(new_from_ble)
+        {
+            memset(blueChar, 0, sizeof(blueChar)); 
+            
+            int i = 0;
+            while(ble_rn.readable())
+            {
+                blueChar[i] = ble_rn.getc();           
+                i++;        
+            }
+            if(i > 0)
+            {
+                printf("%s", blueChar);
+                new_from_ble = false;  
+            } 
+            
+            led2 = 0;
+        }  
+    } 
+} 
+
+
+
+
+
+ 
+int main() 
+{   
+
+    ble_rn.baud(115200);
+    pc.baud(115200);
+    
+    ble_rn.attach(&rxBleCallback);
+    pc.attach(&rxPcCallback);
+ 
+    osThreadCreate(osThread(read_thread), NULL);
+    
+    pc.printf("Bluetooth Start\r\n");
+    
+    // Init device
+    setup_ble();
+ 
+    
+    int i = 0;
+   
+    while(1)
+    {
+        if(new_from_pc)
+        {
+            // receive from PC uart
+            int i = 0;
+            while(pc.readable())
+            {
+                pcChar[i] = pc.getc();
+                i++;
+            }
+            
+            printf("Received from PC: %s", pcChar); 
+            
+            // send to ble
+            write_characteristic(pcChar, i);
+            memset(pcChar, 0, sizeof(pcChar));
+            new_from_pc = false;
+            led3 = 0;
+        } 
+        osDelay(100);
+        i++;
+        if(i == 5)
+        {
+            led1 = !led1;
+            i=0;    
+        }
+    } 
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Sat May 13 11:11:00 2017 +0000
@@ -0,0 +1,51 @@
+/*
+RN4020      -   mbed
+vcc         -   3.3V
+gnd         -   gnd
+rx          -   tx(p28)
+tx          -   rx(p27)
+wake_hw     -   p5
+cmd/mldp    -   p6
+wake_sw     -   p7
+*/
+
+
+
+#include "mbed.h"
+#include "MODSERIAL.h"
+#include "cmsis_os.h"
+
+
+MODSERIAL pc(USBTX, USBRX); 
+MODSERIAL ble_rn(p28, p27);
+ 
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+DigitalOut wakeHw(p5);
+DigitalOut cmdMldp(p6);
+DigitalOut wakeSw(p7);
+ 
+
+// UUID
+char suuid[] = "00035b0058e607dd021a08123a000300";     // service 
+char uuidR[] = "00035b0258e607dd021a08123a000300";     // read 
+char uuidW[] = "00035b0358e607dd021a08123a000300";     // write
+ 
+bool new_from_pc = false;
+bool new_from_ble = false;
+bool start_ble = false;
+char pcChar[20];
+char blueChar[20];
+char blueCharTemp[20];
+char buffer[200];
+
+
+
+//THREAD
+void read_thread(void const *argument);
+osThreadId tencid;
+osThreadDef(read_thread, osPriorityNormal, DEFAULT_STACK_SIZE); 
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Sat May 13 11:11:00 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#cb1d43beeb70
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-src.lib	Sat May 13 11:11:00 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-src/#4abd2456cfa5