Test MLDP code for Microchip RN4020 BLE
Dependencies: mbed-src mbed-rtos MODSERIAL
Revision 2:6dd5f9113367, committed 2019-03-13
- Comitter:
- edodm85
- Date:
- Wed Mar 13 21:24:20 2019 +0000
- Parent:
- 1:2b5aaaddd35e
- Commit message:
- Test MLDP with RN4020
Changed in this revision
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 |
diff -r 2b5aaaddd35e -r 6dd5f9113367 main.cpp --- a/main.cpp Wed Mar 13 19:13:09 2019 +0000 +++ b/main.cpp Wed Mar 13 21:24:20 2019 +0000 @@ -1,7 +1,7 @@ /* * Author: Edoardo De Marchi - * Date: 17-05-13 - * Notes: Test code for Microcip RN4020 BLE + * Date: 19-03-13 + * Notes: Test MLDP connection with Microchip RN4020 BLE */ @@ -11,21 +11,10 @@ // Send command to ble void send_cmd(char* cmd) { - ble_rn.printf("%s", cmd); + ble_rn.printf("%s\r", 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() { @@ -38,74 +27,43 @@ // reset to the factory default configuration - send_cmd("SF,1\n"); + send_cmd("SF,1"); osDelay(500); pc.printf("SF,1\n"); // sets the services supported - send_cmd("SS,C0000001\n"); // Private Service + Device Information + send_cmd("SS,00000000"); // No Service osDelay(500); - send_cmd("GS\n"); + send_cmd("GS"); // sets the supported features of current RN4020 module // Auto Advertise - send_cmd("SR,20000000\n"); + // Enable MLDP + // UART Flow Control + // Auto-enter MLDP Mode + send_cmd("SR,32000A00"); osDelay(500); - send_cmd("GR\n"); + send_cmd("GR"); // sets the device name - send_cmd("SN,RN4020\n"); - osDelay(500); - send_cmd("GN\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); + send_cmd("SN,RN4020"); 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); - + send_cmd("GN"); + // reboot the RN4020 module and to make the new settings effective - send_cmd("R,1\n"); + send_cmd("R,1"); osDelay(500); pc.printf("R,1\n"); - osDelay(2000); - - - // Show list of Services - send_cmd("LS\n"); - osDelay(500); + osDelay(2000); - start_ble = true; + start_ble = true; + cmdMldp = 1; } @@ -143,7 +101,7 @@ } if(i > 0) { - printf("%s", blueChar); + pc.printf("%s", blueChar); new_from_ble = false; } @@ -155,8 +113,6 @@ - - int main() { @@ -174,34 +130,32 @@ setup_ble(); - int i = 0; + int iBlink = 0; while(1) { if(new_from_pc) { // receive from PC uart - int i = 0; + int iCount = 0; while(pc.readable()) { - pcChar[i] = pc.getc(); - i++; + pcChar[iCount] = pc.getc(); + iCount++; } - printf("Received from PC: %s", pcChar); - // send to ble - write_characteristic(pcChar, i); + send_cmd(pcChar); memset(pcChar, 0, sizeof(pcChar)); new_from_pc = false; led3 = 0; } - osDelay(100); - i++; - if(i == 5) + osDelay(10); + iBlink++; + if(iBlink == 50) { led1 = !led1; - i=0; + iBlink=0; } } } \ No newline at end of file
diff -r 2b5aaaddd35e -r 6dd5f9113367 main.h --- a/main.h Wed Mar 13 19:13:09 2019 +0000 +++ b/main.h Wed Mar 13 21:24:20 2019 +0000 @@ -29,18 +29,13 @@ 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]; +char pcChar[100]; +char blueChar[100]; +char blueCharTemp[100];