High level Bluetooth Low Energy API and radio abstraction layer
Fork of BLE_API by
hw/nrf51822.cpp@1:fd3ec64b2345, 2013-12-04 (annotated)
- Committer:
- ktownsend
- Date:
- Wed Dec 04 07:20:24 2013 +0000
- Revision:
- 1:fd3ec64b2345
- Parent:
- 0:ace2e8d3ce79
- Child:
- 2:ffc5216bd2cc
Added LPC1768 pinout
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ktownsend | 0:ace2e8d3ce79 | 1 | #include "nrf51822.h" |
ktownsend | 0:ace2e8d3ce79 | 2 | #include "mbed.h" |
ktownsend | 0:ace2e8d3ce79 | 3 | |
ktownsend | 0:ace2e8d3ce79 | 4 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 5 | /*! |
ktownsend | 0:ace2e8d3ce79 | 6 | @brief UART callback function |
ktownsend | 0:ace2e8d3ce79 | 7 | */ |
ktownsend | 0:ace2e8d3ce79 | 8 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 9 | void nRF51822::uartCallback(void) |
ktownsend | 0:ace2e8d3ce79 | 10 | { |
ktownsend | 0:ace2e8d3ce79 | 11 | /* ToDo: Check responses and set a flag for success/error/etc. */ |
ktownsend | 0:ace2e8d3ce79 | 12 | |
ktownsend | 0:ace2e8d3ce79 | 13 | /* Read serial to clear the RX interrupt */ |
ktownsend | 0:ace2e8d3ce79 | 14 | uart.getc(); |
ktownsend | 0:ace2e8d3ce79 | 15 | } |
ktownsend | 0:ace2e8d3ce79 | 16 | |
ktownsend | 0:ace2e8d3ce79 | 17 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 18 | /*! |
ktownsend | 0:ace2e8d3ce79 | 19 | @brief Constructor |
ktownsend | 0:ace2e8d3ce79 | 20 | |
ktownsend | 0:ace2e8d3ce79 | 21 | @args fptr[in] Pointer to the callback function when any radio |
ktownsend | 0:ace2e8d3ce79 | 22 | event is raised by the radio HW. |
ktownsend | 0:ace2e8d3ce79 | 23 | */ |
ktownsend | 0:ace2e8d3ce79 | 24 | /**************************************************************************/ |
ktownsend | 1:fd3ec64b2345 | 25 | //nRF51822::nRF51822() : uart(P0_4, P0_0) /* LPC812 */ |
ktownsend | 1:fd3ec64b2345 | 26 | nRF51822::nRF51822() : uart(p9, p10) /* LPC1768 using apps board */ |
ktownsend | 0:ace2e8d3ce79 | 27 | { |
ktownsend | 0:ace2e8d3ce79 | 28 | /* Setup the nRF UART interface */ |
ktownsend | 0:ace2e8d3ce79 | 29 | uart.baud(9600); |
ktownsend | 0:ace2e8d3ce79 | 30 | |
ktownsend | 0:ace2e8d3ce79 | 31 | /* Echo data on the debug CDC port */ |
ktownsend | 0:ace2e8d3ce79 | 32 | uart.attach(this, &nRF51822::uartCallback); |
ktownsend | 0:ace2e8d3ce79 | 33 | |
ktownsend | 0:ace2e8d3ce79 | 34 | /* Add flow control for UART (required by the nRF51822) */ |
ktownsend | 1:fd3ec64b2345 | 35 | //uart.set_flow_control(Serial::RTSCTS, P0_6, P0_8); /* LPC812 */ |
ktownsend | 1:fd3ec64b2345 | 36 | uart.set_flow_control(Serial::RTSCTS, p30, p29); /* LPC1768 */ |
ktownsend | 0:ace2e8d3ce79 | 37 | |
ktownsend | 0:ace2e8d3ce79 | 38 | /* Reset the service counter */ |
ktownsend | 0:ace2e8d3ce79 | 39 | serviceCount = 0; |
ktownsend | 0:ace2e8d3ce79 | 40 | } |
ktownsend | 0:ace2e8d3ce79 | 41 | |
ktownsend | 0:ace2e8d3ce79 | 42 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 43 | /*! |
ktownsend | 0:ace2e8d3ce79 | 44 | @brief Destructor |
ktownsend | 0:ace2e8d3ce79 | 45 | */ |
ktownsend | 0:ace2e8d3ce79 | 46 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 47 | nRF51822::~nRF51822(void) |
ktownsend | 0:ace2e8d3ce79 | 48 | { |
ktownsend | 0:ace2e8d3ce79 | 49 | } |
ktownsend | 0:ace2e8d3ce79 | 50 | |
ktownsend | 0:ace2e8d3ce79 | 51 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 52 | /*! |
ktownsend | 0:ace2e8d3ce79 | 53 | |
ktownsend | 0:ace2e8d3ce79 | 54 | */ |
ktownsend | 0:ace2e8d3ce79 | 55 | /**************************************************************************/ |
ktownsend | 1:fd3ec64b2345 | 56 | //ble_error_t nRF51822::attach(void (*fptr)(void)) |
ktownsend | 1:fd3ec64b2345 | 57 | //{ |
ktownsend | 1:fd3ec64b2345 | 58 | // return BLE_ERROR_NONE; |
ktownsend | 1:fd3ec64b2345 | 59 | //} |
ktownsend | 0:ace2e8d3ce79 | 60 | |
ktownsend | 0:ace2e8d3ce79 | 61 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 62 | /*! |
ktownsend | 0:ace2e8d3ce79 | 63 | |
ktownsend | 0:ace2e8d3ce79 | 64 | */ |
ktownsend | 0:ace2e8d3ce79 | 65 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 66 | ble_error_t nRF51822::addService(BLEService & service) |
ktownsend | 0:ace2e8d3ce79 | 67 | { |
ktownsend | 0:ace2e8d3ce79 | 68 | /* ToDo: Make sure we don't overflow the array, etc. */ |
ktownsend | 0:ace2e8d3ce79 | 69 | /* ToDo: Make sure this service UUID doesn't already exist (?) */ |
ktownsend | 0:ace2e8d3ce79 | 70 | /* ToDo: Basic validation */ |
ktownsend | 0:ace2e8d3ce79 | 71 | |
ktownsend | 0:ace2e8d3ce79 | 72 | /* Add the service to the nRF51 */ |
ktownsend | 0:ace2e8d3ce79 | 73 | if (service.primaryServiceID.type == UUID::UUID_TYPE_SHORT) |
ktownsend | 0:ace2e8d3ce79 | 74 | { |
ktownsend | 0:ace2e8d3ce79 | 75 | /* 16-bit BLE UUID */ |
ktownsend | 0:ace2e8d3ce79 | 76 | uart.printf("10 01 00 04 01 02 %02X %02X\r\n", |
ktownsend | 0:ace2e8d3ce79 | 77 | service.primaryServiceID.value & 0xFF, |
ktownsend | 0:ace2e8d3ce79 | 78 | service.primaryServiceID.value >> 8); |
ktownsend | 0:ace2e8d3ce79 | 79 | } |
ktownsend | 0:ace2e8d3ce79 | 80 | else |
ktownsend | 0:ace2e8d3ce79 | 81 | { |
ktownsend | 0:ace2e8d3ce79 | 82 | /* 128-bit Custom UUID */ |
ktownsend | 0:ace2e8d3ce79 | 83 | uart.printf("10 01 00 12 01 10 %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\r\n", |
ktownsend | 0:ace2e8d3ce79 | 84 | service.primaryServiceID.base[0], |
ktownsend | 0:ace2e8d3ce79 | 85 | service.primaryServiceID.base[1], |
ktownsend | 0:ace2e8d3ce79 | 86 | service.primaryServiceID.base[2], |
ktownsend | 0:ace2e8d3ce79 | 87 | service.primaryServiceID.base[3], |
ktownsend | 0:ace2e8d3ce79 | 88 | service.primaryServiceID.base[4], |
ktownsend | 0:ace2e8d3ce79 | 89 | service.primaryServiceID.base[5], |
ktownsend | 0:ace2e8d3ce79 | 90 | service.primaryServiceID.base[6], |
ktownsend | 0:ace2e8d3ce79 | 91 | service.primaryServiceID.base[7], |
ktownsend | 0:ace2e8d3ce79 | 92 | service.primaryServiceID.base[8], |
ktownsend | 0:ace2e8d3ce79 | 93 | service.primaryServiceID.base[9], |
ktownsend | 0:ace2e8d3ce79 | 94 | service.primaryServiceID.base[10], |
ktownsend | 0:ace2e8d3ce79 | 95 | service.primaryServiceID.base[11], |
ktownsend | 0:ace2e8d3ce79 | 96 | service.primaryServiceID.base[12], |
ktownsend | 0:ace2e8d3ce79 | 97 | service.primaryServiceID.base[13], |
ktownsend | 0:ace2e8d3ce79 | 98 | service.primaryServiceID.base[14], |
ktownsend | 0:ace2e8d3ce79 | 99 | service.primaryServiceID.base[15]); |
ktownsend | 0:ace2e8d3ce79 | 100 | } |
ktownsend | 0:ace2e8d3ce79 | 101 | |
ktownsend | 0:ace2e8d3ce79 | 102 | /* ToDo: Check response */ |
ktownsend | 0:ace2e8d3ce79 | 103 | wait(0.1); |
ktownsend | 0:ace2e8d3ce79 | 104 | |
ktownsend | 0:ace2e8d3ce79 | 105 | /* Add characteristics to the service */ |
ktownsend | 0:ace2e8d3ce79 | 106 | for (uint8_t i = 0; i < service.characteristicCount; i++) |
ktownsend | 0:ace2e8d3ce79 | 107 | { |
ktownsend | 0:ace2e8d3ce79 | 108 | /* Command ID = 0x0002 */ |
ktownsend | 0:ace2e8d3ce79 | 109 | uart.printf("10 02 00 0F 01 02 %02X %02X 04 02 %02X %02X 05 02 %02X %02X 03 01 %02X\r\n", |
ktownsend | 0:ace2e8d3ce79 | 110 | service.characteristics[i].id & 0xFF, |
ktownsend | 0:ace2e8d3ce79 | 111 | service.characteristics[i].id >> 8, |
ktownsend | 0:ace2e8d3ce79 | 112 | service.characteristics[i].lenMin & 0xFF, |
ktownsend | 0:ace2e8d3ce79 | 113 | service.characteristics[i].lenMin >> 8, |
ktownsend | 0:ace2e8d3ce79 | 114 | service.characteristics[i].lenMax & 0xFF, |
ktownsend | 0:ace2e8d3ce79 | 115 | service.characteristics[i].lenMax >> 8, |
ktownsend | 0:ace2e8d3ce79 | 116 | service.characteristics[i].properties); |
ktownsend | 0:ace2e8d3ce79 | 117 | |
ktownsend | 0:ace2e8d3ce79 | 118 | /* ToDo: Check response */ |
ktownsend | 0:ace2e8d3ce79 | 119 | wait(0.1); |
ktownsend | 0:ace2e8d3ce79 | 120 | } |
ktownsend | 0:ace2e8d3ce79 | 121 | |
ktownsend | 0:ace2e8d3ce79 | 122 | /* Update the service index value */ |
ktownsend | 0:ace2e8d3ce79 | 123 | service.index = serviceCount; |
ktownsend | 0:ace2e8d3ce79 | 124 | serviceCount++; |
ktownsend | 0:ace2e8d3ce79 | 125 | |
ktownsend | 0:ace2e8d3ce79 | 126 | return BLE_ERROR_NONE; |
ktownsend | 0:ace2e8d3ce79 | 127 | } |
ktownsend | 0:ace2e8d3ce79 | 128 | |
ktownsend | 0:ace2e8d3ce79 | 129 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 130 | /*! |
ktownsend | 0:ace2e8d3ce79 | 131 | @brief Updates the value of a characteristic, based on the service |
ktownsend | 0:ace2e8d3ce79 | 132 | and characteristic index fields |
ktownsend | 0:ace2e8d3ce79 | 133 | |
ktownsend | 0:ace2e8d3ce79 | 134 | @param[in] sIndex |
ktownsend | 0:ace2e8d3ce79 | 135 | The BLEService's index value (.index) |
ktownsend | 0:ace2e8d3ce79 | 136 | @param[in] cIndex |
ktownsend | 0:ace2e8d3ce79 | 137 | The BLECharacteristic's index value (.index) |
ktownsend | 0:ace2e8d3ce79 | 138 | @param[in] buffer |
ktownsend | 0:ace2e8d3ce79 | 139 | Data to use when updating the characteristic's value |
ktownsend | 0:ace2e8d3ce79 | 140 | (raw byte array in LSB format) |
ktownsend | 0:ace2e8d3ce79 | 141 | @param[in] len |
ktownsend | 0:ace2e8d3ce79 | 142 | The number of bytes in buffer |
ktownsend | 0:ace2e8d3ce79 | 143 | */ |
ktownsend | 0:ace2e8d3ce79 | 144 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 145 | ble_error_t nRF51822::updateValue(uint8_t sIndex, uint8_t cIndex, uint8_t buffer[], uint16_t len) |
ktownsend | 0:ace2e8d3ce79 | 146 | { |
ktownsend | 0:ace2e8d3ce79 | 147 | /* Command ID = 0x0006, Payload = Service ID, Characteristic ID, Value */ |
ktownsend | 0:ace2e8d3ce79 | 148 | uart.printf("10 06 00 %02X %02X %02X", len + 2, cIndex, sIndex); |
ktownsend | 0:ace2e8d3ce79 | 149 | for (uint16_t i = 0; i<len; i++) |
ktownsend | 0:ace2e8d3ce79 | 150 | { |
ktownsend | 0:ace2e8d3ce79 | 151 | uart.printf(" %02X", buffer[i]); |
ktownsend | 0:ace2e8d3ce79 | 152 | } |
ktownsend | 0:ace2e8d3ce79 | 153 | uart.printf("\r\n"); |
ktownsend | 0:ace2e8d3ce79 | 154 | |
ktownsend | 0:ace2e8d3ce79 | 155 | /* ToDo: Check response */ |
ktownsend | 0:ace2e8d3ce79 | 156 | wait(0.1); |
ktownsend | 0:ace2e8d3ce79 | 157 | |
ktownsend | 0:ace2e8d3ce79 | 158 | return BLE_ERROR_NONE; |
ktownsend | 0:ace2e8d3ce79 | 159 | } |
ktownsend | 0:ace2e8d3ce79 | 160 | |
ktownsend | 0:ace2e8d3ce79 | 161 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 162 | /*! |
ktownsend | 0:ace2e8d3ce79 | 163 | @brief Starts the BLE HW, initialising any services that were |
ktownsend | 0:ace2e8d3ce79 | 164 | added before this function was called. |
ktownsend | 0:ace2e8d3ce79 | 165 | |
ktownsend | 0:ace2e8d3ce79 | 166 | @note All services must be added before calling this function! |
ktownsend | 0:ace2e8d3ce79 | 167 | */ |
ktownsend | 0:ace2e8d3ce79 | 168 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 169 | ble_error_t nRF51822::start(void) |
ktownsend | 0:ace2e8d3ce79 | 170 | { |
ktownsend | 0:ace2e8d3ce79 | 171 | /* Command ID = 0x0003, No payload */ |
ktownsend | 0:ace2e8d3ce79 | 172 | uart.printf("10 03 00 00\r\n"); |
ktownsend | 0:ace2e8d3ce79 | 173 | |
ktownsend | 0:ace2e8d3ce79 | 174 | /* ToDo: Check response */ |
ktownsend | 0:ace2e8d3ce79 | 175 | wait(0.5); |
ktownsend | 0:ace2e8d3ce79 | 176 | |
ktownsend | 0:ace2e8d3ce79 | 177 | return BLE_ERROR_NONE; |
ktownsend | 0:ace2e8d3ce79 | 178 | } |
ktownsend | 0:ace2e8d3ce79 | 179 | |
ktownsend | 0:ace2e8d3ce79 | 180 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 181 | /*! |
ktownsend | 0:ace2e8d3ce79 | 182 | @brief Stops the BLE HW and disconnects from any devices |
ktownsend | 0:ace2e8d3ce79 | 183 | */ |
ktownsend | 0:ace2e8d3ce79 | 184 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 185 | ble_error_t nRF51822::stop(void) |
ktownsend | 0:ace2e8d3ce79 | 186 | { |
ktownsend | 0:ace2e8d3ce79 | 187 | /* Command ID = 0x0004, No payload */ |
ktownsend | 0:ace2e8d3ce79 | 188 | uart.printf("10 04 00 00\r\n"); |
ktownsend | 0:ace2e8d3ce79 | 189 | |
ktownsend | 0:ace2e8d3ce79 | 190 | /* ToDo: Check response */ |
ktownsend | 0:ace2e8d3ce79 | 191 | wait(0.1); |
ktownsend | 0:ace2e8d3ce79 | 192 | |
ktownsend | 0:ace2e8d3ce79 | 193 | return BLE_ERROR_NONE; |
ktownsend | 0:ace2e8d3ce79 | 194 | } |
ktownsend | 0:ace2e8d3ce79 | 195 | |
ktownsend | 0:ace2e8d3ce79 | 196 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 197 | /*! |
ktownsend | 0:ace2e8d3ce79 | 198 | @brief Resets the BLE HW, removing any existing services and |
ktownsend | 0:ace2e8d3ce79 | 199 | characteristics |
ktownsend | 0:ace2e8d3ce79 | 200 | */ |
ktownsend | 0:ace2e8d3ce79 | 201 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 202 | ble_error_t nRF51822::reset(void) |
ktownsend | 0:ace2e8d3ce79 | 203 | { |
ktownsend | 0:ace2e8d3ce79 | 204 | /* Command ID = 0x0005, No payload */ |
ktownsend | 0:ace2e8d3ce79 | 205 | uart.printf("10 05 00 00\r\n"); |
ktownsend | 0:ace2e8d3ce79 | 206 | |
ktownsend | 0:ace2e8d3ce79 | 207 | /* Reset the service counter */ |
ktownsend | 0:ace2e8d3ce79 | 208 | serviceCount = 0; |
ktownsend | 0:ace2e8d3ce79 | 209 | |
ktownsend | 0:ace2e8d3ce79 | 210 | /* Wait for the radio to come back up */ |
ktownsend | 0:ace2e8d3ce79 | 211 | wait(1); |
ktownsend | 0:ace2e8d3ce79 | 212 | |
ktownsend | 0:ace2e8d3ce79 | 213 | return BLE_ERROR_NONE; |
ktownsend | 0:ace2e8d3ce79 | 214 | } |