-
Notifications
You must be signed in to change notification settings - Fork 18
/
nRF52840_UartBlePeripheral.ino
147 lines (125 loc) · 5.28 KB
/
nRF52840_UartBlePeripheral.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Nordic UART BLE peripheral. Copyright (c) Thomas Amberg, FHNW
// Based on https://github.com/adafruit/Adafruit_nRF52_Arduino
// /tree/master/libraries/Bluefruit52Lib/examples/Peripheral
// Copyright (c) Adafruit.com, all rights reserved.
// Licensed under the MIT license, see LICENSE or
// https://choosealicense.com/licenses/mit/
#include "Adafruit_TinyUSB.h" // Fix https://github.com/adafruit/Adafruit_nRF52_Arduino/issues/653
#include <bluefruit.h>
BLEDis deviceInfoService;
BLEBas batteryService;
// See https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v15.3.0%2Fble_sdk_app_nus_eval.html
// Nordic UART service UUID: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
// RX characteristic UUID: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E
// TX characteristic UUID: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E
// The arrays below are ordered "least significant byte first":
uint8_t const uartServiceUuid[] = { 0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x01, 0x00, 0x40, 0x6E };
uint8_t const rxCharacteristicUuid[] = { 0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x02, 0x00, 0x40, 0x6E };
uint8_t const txCharacteristicUuid[] = { 0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x03, 0x00, 0x40, 0x6E };
uint16_t mtu; // Maximum Transmission Unit
BLEService uartService = BLEService(uartServiceUuid);
BLECharacteristic rxCharacteristic = BLECharacteristic(rxCharacteristicUuid);
BLECharacteristic txCharacteristic = BLECharacteristic(txCharacteristicUuid);
void connectedCallback(uint16_t connHandle) {
char centralName[32] = { 0 };
BLEConnection *connection = Bluefruit.Connection(connHandle);
connection->getPeerName(centralName, sizeof(centralName));
Serial.print(connHandle);
Serial.print(", connected to ");
Serial.print(centralName);
Serial.println();
}
void disconnectedCallback(uint16_t connHandle, uint8_t reason) {
Serial.print(connHandle);
Serial.print(" disconnected, reason = ");
Serial.println(reason); // see https://github.com/adafruit/Adafruit_nRF52_Arduino
// /blob/master/cores/nRF5/nordic/softdevice/s140_nrf52_6.1.1_API/include/ble_hci.h
Serial.println("Advertising ...");
}
void cccdCallback(uint16_t connHandle, BLECharacteristic* characteristic, uint16_t cccdValue) {
if (characteristic->uuid == txCharacteristic.uuid) {
Serial.print("UART 'Notify', ");
if (characteristic->notifyEnabled()) {
Serial.println("enabled");
} else {
Serial.println("disabled");
}
}
}
void writeCallback(uint16_t connHandle, BLECharacteristic* characteristic, uint8_t* rxData, uint16_t len) {
if (characteristic->uuid == rxCharacteristic.uuid) {
Serial.print("rx: ");
int i = 0;
while (i < len) {
Serial.print((char) rxData[i]);
i++;
}
Serial.print("\n");
}
}
void setupUartService() {
uartService.begin(); // Must be called before calling .begin() on its characteristics
txCharacteristic.setProperties(CHR_PROPS_NOTIFY);
txCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
txCharacteristic.setMaxLen(mtu);
txCharacteristic.setCccdWriteCallback(cccdCallback); // Optionally capture CCCD updates
txCharacteristic.begin();
rxCharacteristic.setProperties(CHR_PROPS_WRITE | CHR_PROPS_WRITE_WO_RESP);
rxCharacteristic.setPermission(SECMODE_NO_ACCESS, SECMODE_OPEN);
rxCharacteristic.setMaxLen(mtu);
rxCharacteristic.setWriteCallback(writeCallback, true);
rxCharacteristic.begin();
}
void startAdvertising() {
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addService(uartService);
Bluefruit.Advertising.addName();
// See https://developer.apple.com/library/content/qa/qa1931/_index.html
const int fastModeInterval = 32; // * 0.625 ms = 20 ms
const int slowModeInterval = 244; // * 0.625 ms = 152.5 ms
const int fastModeTimeout = 30; // s
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(fastModeInterval, slowModeInterval);
Bluefruit.Advertising.setFastTimeout(fastModeTimeout);
// 0 = continue advertising after fast mode, until connected
Bluefruit.Advertising.start(0);
Serial.println("Advertising ...");
}
void setup() {
Serial.begin(115200);
while (!Serial) { delay(10); } // only if usb connected
Serial.println("Setup");
Bluefruit.begin();
Bluefruit.setName("nRF52840");
Bluefruit.Periph.setConnectCallback(connectedCallback);
Bluefruit.Periph.setDisconnectCallback(disconnectedCallback);
deviceInfoService.setManufacturer("Adafruit Industries");
deviceInfoService.setModel("Feather nRF52840 Express");
deviceInfoService.begin();
batteryService.begin();
batteryService.write(100); // %
mtu = Bluefruit.getMaxMtu(BLE_GAP_ROLE_PERIPH);
setupUartService();
startAdvertising();
}
void loop() {
if (Bluefruit.connected()) {
if (Serial.available()) {
uint8_t txData[mtu];
size_t bytesRead = Serial.readBytes(txData, mtu);
if (txCharacteristic.notify(txData, bytesRead)) {
Serial.print("tx: ");
int i = 0;
while (i < bytesRead) {
Serial.print((char) txData[i]); // echo
i++;
}
Serial.print("\n");
} else {
Serial.println("tx: Notify error.");
}
}
}
delay(200); // ms
}