Test MLDP code for Microchip RN4020 BLE
Dependencies: mbed-src mbed-rtos MODSERIAL
Diff: main.cpp
- Revision:
- 0:49a7ebfbee00
- Child:
- 1:2b5aaaddd35e
--- /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