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