Partial implementation of BlueGiga's BGAPI for use with the BLE112/3 modules over UART.

Hi there! I recently started using BLE112 modules with the mbed LPC1768 MCU, and I realized there was no implementation of BlueGiga's BGAPI available for mbed. This library implements only a few commands, but if you're looking to get started, this is a good place to look.

This was developed against BGAPI v1.3.2. I make no guarantees as to how well it will work with newer revisions of the software.

Committer:
dishbreak
Date:
Mon May 18 01:16:52 2015 +0000
Revision:
2:3ce9a31a6a7e
Parent:
1:3336b2391c80
Child:
3:8f43af513d87
Added support for Get Info command.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dishbreak 1:3336b2391c80 1 #include "BGLib.h"
dishbreak 1:3336b2391c80 2
dishbreak 1:3336b2391c80 3 BGLib::BGLib(PinName tx, PinName rx, PinName rts, PinName cts) :
dishbreak 1:3336b2391c80 4 mSerial(tx, rx) {
dishbreak 1:3336b2391c80 5 mSerial.set_flow_control(SerialBase::RTSCTS, rts, cts);
dishbreak 1:3336b2391c80 6 mSerial.baud(57600);
dishbreak 1:3336b2391c80 7 mSerial.attach(this, &BGLib::parse);
dishbreak 1:3336b2391c80 8 }
dishbreak 1:3336b2391c80 9
dishbreak 2:3ce9a31a6a7e 10 void BGLib::set_ble_rsp_system_hello(hello_callback_t pCallback) {
dishbreak 1:3336b2391c80 11 mHelloCallback = pCallback;
dishbreak 1:3336b2391c80 12 }
dishbreak 1:3336b2391c80 13
dishbreak 2:3ce9a31a6a7e 14 void BGLib::ble_cmd_system_hello() {
dishbreak 1:3336b2391c80 15 uint8_t bytes[] = {0x00, 0x00, 0x00, 0x01};
dishbreak 1:3336b2391c80 16 send_bytes(bytes, 4);
dishbreak 1:3336b2391c80 17 }
dishbreak 1:3336b2391c80 18
dishbreak 2:3ce9a31a6a7e 19 void BGLib::ble_cmd_system_get_info() {
dishbreak 2:3ce9a31a6a7e 20 uint8_t bytes[] = {0x00, 0x00, 0x00, 0x08};
dishbreak 2:3ce9a31a6a7e 21 send_bytes(bytes, 4);
dishbreak 2:3ce9a31a6a7e 22 }
dishbreak 2:3ce9a31a6a7e 23
dishbreak 2:3ce9a31a6a7e 24 void BGLib::set_ble_rsp_system_get_info(get_info_callback_t pCallback) {
dishbreak 2:3ce9a31a6a7e 25 mGetInfoCallback = pCallback;
dishbreak 2:3ce9a31a6a7e 26 }
dishbreak 2:3ce9a31a6a7e 27
dishbreak 1:3336b2391c80 28 void BGLib::parse() {
dishbreak 2:3ce9a31a6a7e 29 #ifdef DEBUG
dishbreak 2:3ce9a31a6a7e 30 printf("Got data from port!\r\n");
dishbreak 2:3ce9a31a6a7e 31 #endif
dishbreak 2:3ce9a31a6a7e 32
dishbreak 2:3ce9a31a6a7e 33 uint8_t hilen = mSerial.getc();
dishbreak 2:3ce9a31a6a7e 34 uint8_t lolen = mSerial.getc();
dishbreak 2:3ce9a31a6a7e 35 uint8_t msg_class = mSerial.getc();
dishbreak 2:3ce9a31a6a7e 36 uint8_t msg_id = mSerial.getc();
dishbreak 2:3ce9a31a6a7e 37
dishbreak 2:3ce9a31a6a7e 38 #ifdef DEBUG
dishbreak 2:3ce9a31a6a7e 39 printf("Message header: %x %x %x %x\r\n", hilen, lolen, msg_class, msg_id);
dishbreak 2:3ce9a31a6a7e 40 #endif
dishbreak 2:3ce9a31a6a7e 41
dishbreak 2:3ce9a31a6a7e 42 if (hilen == 0x00) { // response
dishbreak 2:3ce9a31a6a7e 43 if (msg_class == 0x00) { // system_class
dishbreak 2:3ce9a31a6a7e 44 if (msg_id == 0x01) { // system_hello
dishbreak 2:3ce9a31a6a7e 45 mHelloCallback();
dishbreak 2:3ce9a31a6a7e 46 } else if (msg_id == 0x08) {
dishbreak 2:3ce9a31a6a7e 47
dishbreak 2:3ce9a31a6a7e 48 #ifdef DEBUG
dishbreak 2:3ce9a31a6a7e 49 printf("Get Info Response\r\n");
dishbreak 2:3ce9a31a6a7e 50 #endif
dishbreak 2:3ce9a31a6a7e 51
dishbreak 2:3ce9a31a6a7e 52 ble_msg_system_get_info_rsp_t result;
dishbreak 2:3ce9a31a6a7e 53 result.major = mSerial.getc() | (mSerial.getc() << 8);
dishbreak 2:3ce9a31a6a7e 54 result.minor = mSerial.getc() | (mSerial.getc() << 8);
dishbreak 2:3ce9a31a6a7e 55 result.patch = mSerial.getc() | (mSerial.getc() << 8);
dishbreak 2:3ce9a31a6a7e 56 result.build = mSerial.getc() | (mSerial.getc() << 8);
dishbreak 2:3ce9a31a6a7e 57 result.ll_version = mSerial.getc() | (mSerial.getc() << 8);
dishbreak 2:3ce9a31a6a7e 58 result.protocol_version = mSerial.getc();
dishbreak 2:3ce9a31a6a7e 59 result.hw = mSerial.getc();
dishbreak 2:3ce9a31a6a7e 60 mGetInfoCallback(result);
dishbreak 2:3ce9a31a6a7e 61 }
dishbreak 2:3ce9a31a6a7e 62 }
dishbreak 2:3ce9a31a6a7e 63 }
dishbreak 2:3ce9a31a6a7e 64
dishbreak 2:3ce9a31a6a7e 65 //safety valve: if there are bytes remaining
dishbreak 1:3336b2391c80 66 }
dishbreak 1:3336b2391c80 67
dishbreak 1:3336b2391c80 68 void BGLib::send_bytes(uint8_t bytes[], int length) {
dishbreak 1:3336b2391c80 69 for (int i = 0; i < length; i++) {
dishbreak 1:3336b2391c80 70 mSerial.putc(bytes[i]);
dishbreak 1:3336b2391c80 71 }
dishbreak 1:3336b2391c80 72 }