Bluetooth UART support for the Adafruit BluefruitLE SPI, for the University of York Engineering Stage 1 project

Committer:
ajp109
Date:
Fri Mar 12 14:35:25 2021 +0000
Revision:
3:bdfd15be7b82
Parent:
0:a80552d32b80
Remove readme

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ajp109 0:a80552d32b80 1 /**************************************************************************/
ajp109 0:a80552d32b80 2 /*!
ajp109 0:a80552d32b80 3 @file Adafruit_BLE.h
ajp109 0:a80552d32b80 4 @author hathach (Adafruit Industries), ajp109 (University of York)
ajp109 0:a80552d32b80 5
ajp109 0:a80552d32b80 6 @section LICENSE
ajp109 0:a80552d32b80 7
ajp109 0:a80552d32b80 8 Software License Agreement (BSD License)
ajp109 0:a80552d32b80 9
ajp109 0:a80552d32b80 10 Copyright (c) 2014, Adafruit Industries (adafruit.com)
ajp109 0:a80552d32b80 11 All rights reserved.
ajp109 0:a80552d32b80 12
ajp109 0:a80552d32b80 13 Redistribution and use in source and binary forms, with or without
ajp109 0:a80552d32b80 14 modification, are permitted provided that the following conditions are met:
ajp109 0:a80552d32b80 15 1. Redistributions of source code must retain the above copyright
ajp109 0:a80552d32b80 16 notice, this list of conditions and the following disclaimer.
ajp109 0:a80552d32b80 17 2. Redistributions in binary form must reproduce the above copyright
ajp109 0:a80552d32b80 18 notice, this list of conditions and the following disclaimer in the
ajp109 0:a80552d32b80 19 documentation and/or other materials provided with the distribution.
ajp109 0:a80552d32b80 20 3. Neither the name of the copyright holders nor the
ajp109 0:a80552d32b80 21 names of its contributors may be used to endorse or promote products
ajp109 0:a80552d32b80 22 derived from this software without specific prior written permission.
ajp109 0:a80552d32b80 23
ajp109 0:a80552d32b80 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
ajp109 0:a80552d32b80 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
ajp109 0:a80552d32b80 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
ajp109 0:a80552d32b80 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
ajp109 0:a80552d32b80 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
ajp109 0:a80552d32b80 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
ajp109 0:a80552d32b80 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ajp109 0:a80552d32b80 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
ajp109 0:a80552d32b80 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
ajp109 0:a80552d32b80 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ajp109 0:a80552d32b80 34 */
ajp109 0:a80552d32b80 35 /**************************************************************************/
ajp109 0:a80552d32b80 36
ajp109 0:a80552d32b80 37 #ifndef _Adafruit_BLE_H_
ajp109 0:a80552d32b80 38 #define _Adafruit_BLE_H_
ajp109 0:a80552d32b80 39
ajp109 0:a80552d32b80 40 #include <stdint.h>
ajp109 0:a80552d32b80 41 #include "mbed.h"
ajp109 0:a80552d32b80 42 #include "utility/common_header.h"
ajp109 0:a80552d32b80 43 #include "utility/errors.h"
ajp109 0:a80552d32b80 44 #include "utility/TimeoutTimer.h"
ajp109 0:a80552d32b80 45 #include "Adafruit_ATParser.h"
ajp109 0:a80552d32b80 46
ajp109 0:a80552d32b80 47 #define BLE_DEFAULT_TIMEOUT 250
ajp109 0:a80552d32b80 48
ajp109 0:a80552d32b80 49 enum BLEDataType_t {
ajp109 0:a80552d32b80 50 BLE_DATATYPE_AUTO = 0,
ajp109 0:a80552d32b80 51 BLE_DATATYPE_STRING,
ajp109 0:a80552d32b80 52 BLE_DATATYPE_BYTEARRAY,
ajp109 0:a80552d32b80 53 BLE_DATATYPE_INTEGER,
ajp109 0:a80552d32b80 54 };
ajp109 0:a80552d32b80 55
ajp109 0:a80552d32b80 56
ajp109 0:a80552d32b80 57 class Adafruit_BLE : public Adafruit_ATParser
ajp109 0:a80552d32b80 58 {
ajp109 0:a80552d32b80 59 protected:
ajp109 0:a80552d32b80 60 enum {
ajp109 0:a80552d32b80 61 BLUEFRUIT_TRANSPORT_INVALID,
ajp109 0:a80552d32b80 62 BLUEFRUIT_TRANSPORT_HWUART,
ajp109 0:a80552d32b80 63 BLUEFRUIT_TRANSPORT_SWUART,
ajp109 0:a80552d32b80 64 BLUEFRUIT_TRANSPORT_HWSPI,
ajp109 0:a80552d32b80 65 BLUEFRUIT_TRANSPORT_SWSPI,
ajp109 0:a80552d32b80 66 };
ajp109 0:a80552d32b80 67
ajp109 0:a80552d32b80 68 // uint8_t _mode;
ajp109 0:a80552d32b80 69 // uint16_t _timeout;
ajp109 0:a80552d32b80 70 uint8_t _physical_transport;
ajp109 0:a80552d32b80 71 chrono::time_point<Kernel::Clock> _reset_started_timestamp;
ajp109 0:a80552d32b80 72
ajp109 0:a80552d32b80 73 public:
ajp109 0:a80552d32b80 74 // Constructor
ajp109 0:a80552d32b80 75 Adafruit_BLE(void);
ajp109 0:a80552d32b80 76
ajp109 0:a80552d32b80 77 // Functions implemented in this base class
ajp109 0:a80552d32b80 78 bool reset(bool blocking = true);
ajp109 0:a80552d32b80 79 bool factoryReset(bool blocking = true);
ajp109 0:a80552d32b80 80 bool resetCompleted(void);
ajp109 0:a80552d32b80 81
ajp109 0:a80552d32b80 82 void info(void);
ajp109 0:a80552d32b80 83 bool echo(bool enable);
ajp109 0:a80552d32b80 84
ajp109 0:a80552d32b80 85 bool isConnected(void);
ajp109 0:a80552d32b80 86 bool isVersionAtLeast(const char * versionString);
ajp109 0:a80552d32b80 87 void disconnect(void);
ajp109 0:a80552d32b80 88
ajp109 0:a80552d32b80 89 bool setAdvData(uint8_t advdata[], uint8_t size);
ajp109 0:a80552d32b80 90
ajp109 0:a80552d32b80 91 bool writeNVM(uint16_t offset, uint8_t const data[], uint16_t size);
ajp109 0:a80552d32b80 92 bool writeNVM(uint16_t offset, char const* str);
ajp109 0:a80552d32b80 93 bool writeNVM(uint16_t offset, int32_t number);
ajp109 0:a80552d32b80 94
ajp109 0:a80552d32b80 95 bool readNVM(uint16_t offset, uint8_t data[], uint16_t size);
ajp109 0:a80552d32b80 96 bool readNVM(uint16_t offset, char * str, uint16_t size);
ajp109 0:a80552d32b80 97 bool readNVM(uint16_t offset, int32_t* number);
ajp109 0:a80552d32b80 98
ajp109 0:a80552d32b80 99 // helper with bleuart
ajp109 0:a80552d32b80 100 int writeBLEUart(uint8_t const * buffer, int size);
ajp109 0:a80552d32b80 101 int writeBLEUart(char const * str)
ajp109 0:a80552d32b80 102 {
ajp109 0:a80552d32b80 103 return writeBLEUart( (uint8_t const*) str, strlen(str));
ajp109 0:a80552d32b80 104 }
ajp109 0:a80552d32b80 105
ajp109 0:a80552d32b80 106 int readBLEUart(uint8_t* buffer, int size);
ajp109 0:a80552d32b80 107
ajp109 0:a80552d32b80 108
ajp109 0:a80552d32b80 109 // No parameters
ajp109 0:a80552d32b80 110 bool sendCommandCheckOK(const char cmd[])
ajp109 0:a80552d32b80 111 {
ajp109 0:a80552d32b80 112 return this->atcommand(cmd);
ajp109 0:a80552d32b80 113 }
ajp109 0:a80552d32b80 114
ajp109 0:a80552d32b80 115 bool sendCommandWithIntReply(const char cmd[], int32_t *reply)
ajp109 0:a80552d32b80 116 {
ajp109 0:a80552d32b80 117 return this->atcommandIntReply(cmd, reply);
ajp109 0:a80552d32b80 118 }
ajp109 0:a80552d32b80 119
ajp109 0:a80552d32b80 120 // Physical transportation checking
ajp109 0:a80552d32b80 121 bool isTransportHwUart (void)
ajp109 0:a80552d32b80 122 {
ajp109 0:a80552d32b80 123 return _physical_transport == BLUEFRUIT_TRANSPORT_HWUART;
ajp109 0:a80552d32b80 124 }
ajp109 0:a80552d32b80 125 bool isTransportSwUart (void)
ajp109 0:a80552d32b80 126 {
ajp109 0:a80552d32b80 127 return _physical_transport == BLUEFRUIT_TRANSPORT_SWUART;
ajp109 0:a80552d32b80 128 }
ajp109 0:a80552d32b80 129 bool isTransportUart (void)
ajp109 0:a80552d32b80 130 {
ajp109 0:a80552d32b80 131 return isTransportHwUart() || isTransportSwUart();
ajp109 0:a80552d32b80 132 }
ajp109 0:a80552d32b80 133
ajp109 0:a80552d32b80 134 bool isTransportHwSpi (void)
ajp109 0:a80552d32b80 135 {
ajp109 0:a80552d32b80 136 return _physical_transport == BLUEFRUIT_TRANSPORT_HWSPI;
ajp109 0:a80552d32b80 137 }
ajp109 0:a80552d32b80 138 bool isTransportSwSpi (void)
ajp109 0:a80552d32b80 139 {
ajp109 0:a80552d32b80 140 return _physical_transport == BLUEFRUIT_TRANSPORT_SWSPI;
ajp109 0:a80552d32b80 141 }
ajp109 0:a80552d32b80 142 bool isTransportSpi (void)
ajp109 0:a80552d32b80 143 {
ajp109 0:a80552d32b80 144 return isTransportHwSpi() || isTransportSwSpi();
ajp109 0:a80552d32b80 145 }
ajp109 0:a80552d32b80 146
ajp109 0:a80552d32b80 147 /////////////////////
ajp109 0:a80552d32b80 148 // callback functions
ajp109 0:a80552d32b80 149 /////////////////////
ajp109 0:a80552d32b80 150 void update(uint32_t period_ms = 200);
ajp109 0:a80552d32b80 151 void handleDfuIrq(void)
ajp109 0:a80552d32b80 152 {
ajp109 0:a80552d32b80 153 this->update(0);
ajp109 0:a80552d32b80 154 }
ajp109 0:a80552d32b80 155
ajp109 0:a80552d32b80 156 void setDisconnectCallback( void (*fp) (void) );
ajp109 0:a80552d32b80 157 void setConnectCallback ( void (*fp) (void) );
ajp109 0:a80552d32b80 158
ajp109 0:a80552d32b80 159 void setBleUartRxCallback( void (*fp) (char data[], uint16_t len) );
ajp109 0:a80552d32b80 160 void setBleGattRxCallback( int32_t chars_idx, void (*fp) (int32_t, uint8_t[], uint16_t) );
ajp109 0:a80552d32b80 161
ajp109 0:a80552d32b80 162 protected:
ajp109 0:a80552d32b80 163 // helper
ajp109 0:a80552d32b80 164 void install_callback(bool enable, int8_t system_id, int8_t gatts_id);
ajp109 0:a80552d32b80 165
ajp109 0:a80552d32b80 166 void (*_disconnect_callback) (void);
ajp109 0:a80552d32b80 167 void (*_connect_callback) (void);
ajp109 0:a80552d32b80 168
ajp109 0:a80552d32b80 169 void (*_ble_uart_rx_callback) (char data[], uint16_t len);
ajp109 0:a80552d32b80 170
ajp109 0:a80552d32b80 171 void (*_ble_gatt_rx_callback) (int32_t chars_id, uint8_t data[], uint16_t len);
ajp109 0:a80552d32b80 172 };
ajp109 0:a80552d32b80 173
ajp109 0:a80552d32b80 174 //--------------------------------------------------------------------+
ajp109 0:a80552d32b80 175 // DEBUG HELPER
ajp109 0:a80552d32b80 176 //--------------------------------------------------------------------+
ajp109 0:a80552d32b80 177 #ifndef DBG_ENABLE
ajp109 0:a80552d32b80 178 #define DBG_ENABLE 0
ajp109 0:a80552d32b80 179 #endif
ajp109 0:a80552d32b80 180
ajp109 0:a80552d32b80 181 #if DBG_ENABLE
ajp109 0:a80552d32b80 182 #define DBG_LOCATION() Serial.printf("%s: %d: \r\n", __PRETTY_FUNCTION__, __LINE__)
ajp109 0:a80552d32b80 183 #define DBG_INT(x) do { Serial.print(#x " = "); Serial.println(x); } while(0)
ajp109 0:a80552d32b80 184 #define DBG_HEX(x) do { Serial.print(#x " = "); Serial.println(x, HEX); } while(0)
ajp109 0:a80552d32b80 185 #define DBG_STR(x) Serial.printf(#x " = %s\r\n", (char*)(x) )
ajp109 0:a80552d32b80 186 #define DBG_BUFFER(buf, n) \
ajp109 0:a80552d32b80 187 do {\
ajp109 0:a80552d32b80 188 uint8_t* p8 = (uint8_t*) (buf);\
ajp109 0:a80552d32b80 189 Serial.print(#buf ": ");\
ajp109 0:a80552d32b80 190 for(uint32_t i=0; i<(n); i++) Serial.printf("%02x ", p8[i]);\
ajp109 0:a80552d32b80 191 Serial.print("\r\n");\
ajp109 0:a80552d32b80 192 }while(0)
ajp109 0:a80552d32b80 193 #endif
ajp109 0:a80552d32b80 194
ajp109 0:a80552d32b80 195 #endif /* _Adafruit_BLE_H_ */