Skip to content

Commit 2ccef69

Browse files
committed
Add BLE central function, debug interface and refactor peripheral
-Add debug interface on Serial1 -Update BLE stack and need update BLE's FW -Reconstruct the BLE peripheral base on V3 -Implement the BLE Central Role base on V3 -Implement some sketches for new BLE library -Add central read/write example -Add set advertising parameter interface -Add API to allow set up advertising after setup -Add interface to set the device name File description Porting from V3 system/libarc32_arduino101/common/atomic.h system/libarc32_arduino101/common/misc/byteorder.h system/libarc32_arduino101/drivers/atomic_native.c system/libarc32_arduino101/drivers/bluetooth/att.h system/libarc32_arduino101/drivers/bluetooth/bluetooth.h system/libarc32_arduino101/drivers/bluetooth/conn.h system/libarc32_arduino101/drivers/bluetooth/conn_internal.h system/libarc32_arduino101/drivers/bluetooth/gatt.h system/libarc32_arduino101/drivers/bluetooth/hci.h system/libarc32_arduino101/drivers/bluetooth/uuid.h system/libarc32_arduino101/drivers/rpc/rpc.h system/libarc32_arduino101/drivers/rpc/rpc_deserialize.c system/libarc32_arduino101/drivers/rpc/rpc_functions_to_ble_core.h system/libarc32_arduino101/drivers/rpc/rpc_functions_to_quark.h system/libarc32_arduino101/drivers/rpc/rpc_serialize.c system/libarc32_arduino101/framework/include/util/misc.h system/libarc32_arduino101/framework/src/os/panic.c system/libarc32_arduino101/framework/src/services/ble/conn.c system/libarc32_arduino101/framework/src/services/ble/conn_internal.h system/libarc32_arduino101/framework/src/services/ble/dtm_tcmd.c system/libarc32_arduino101/framework/src/services/ble/gap.c system/libarc32_arduino101/framework/src/services/ble/gatt.c system/libarc32_arduino101/framework/src/services/ble/hci_core.h system/libarc32_arduino101/framework/src/services/ble/l2cap.c system/libarc32_arduino101/framework/src/services/ble/l2cap_internal.h system/libarc32_arduino101/framework/src/services/ble/smp.h system/libarc32_arduino101/framework/src/services/ble/smp_null.c system/libarc32_arduino101/framework/src/services/ble/uuid.c system/libarc32_arduino101/framework/src/services/ble_service/ble_service.c system/libarc32_arduino101/framework/src/services/ble_service/ble_service_api.c system/libarc32_arduino101/framework/src/services/ble_service/ble_service_int.h system/libarc32_arduino101/framework/src/services/ble_service/ble_service_internal.h system/libarc32_arduino101/framework/src/services/ble_service/ble_service_utils.c system/libarc32_arduino101/framework/src/services/ble_service/gap_internal.h system/libarc32_arduino101/framework/src/services/ble_service/gatt_internal.h system/libarc32_arduino101/framework/src/services/ble_service/nble_driver.c
1 parent 10d1056 commit 2ccef69

File tree

111 files changed

+15785
-5716
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+15785
-5716
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,21 @@ them to the [support forum](https://forum.arduino.cc/index.php?board=103).
5353
> "How do I use this library?"
5454
5555
> "I can't get this example sketch to work. What am I doing wrong?"
56+
57+
# Enable debug interface on Serail1
58+
59+
* Default disable the debug interface.
60+
61+
If you want to enable debug trace on Serial1 to debug corelib, follow these instructions.
62+
63+
1. Shut down the IDE
64+
2. Go to Arduino15 directory
65+
* Windows: `C:\Users\<user>\AppData\Roaming\Arduino15`
66+
* OS X: `~/Library/Arduino15`
67+
* Linux: `~/.arduino15`
68+
3. Modify the platform.txt
69+
* Find `compiler.c.flags` and add `-DCONFIGURE_DEBUG_CORELIB_ENABLED` at the end of this line
70+
* Find `compiler.cpp.flags` and add `-DCONFIGURE_DEBUG_CORELIB_ENABLED` at the end of this line
71+
4. Initial Serial1 in your sketch
72+
* Add `Serial1.begin(115200);` in your `setup()`
73+
5. Adjust the output level at log_init function in log.c

system/libarc32_arduino101/framework/src/nordic_interface.h renamed to cores/arduino/printk.cpp

+20-8
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,25 @@
2828
* POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030

31-
#ifndef NORDIC_INTERFACE_H
32-
#define NORDIC_INTERFACE_H
33-
#include "infra/ipc_uart.h"
31+
#include <stdarg.h>
32+
#include <cstdio>
33+
#include "UARTClass.h"
3434

35-
void uart_ipc_message_cback(uint8_t cpu_id, int channel, int len, void * p_data);
36-
int send_message_ipc_uart(struct message * message);
37-
void free_message_ipc_uart(struct message * message);
38-
int nordic_interface_init(T_QUEUE queue);
35+
extern "C" void printk(const char *fmt, va_list args);
36+
extern UARTClass Serial1;
37+
#define PRINTK_BUFSIZ 256
38+
39+
void printk(const char *fmt, va_list args)
40+
{
41+
#ifdef CONFIGURE_DEBUG_CORELIB_ENABLED
42+
int len = 0;
43+
44+
char tmp[PRINTK_BUFSIZ];
45+
46+
len = vsnprintf(tmp, PRINTK_BUFSIZ, fmt, args);
47+
48+
tmp[len] = '\0';
49+
Serial1.println(tmp);
50+
#endif
51+
}
3952

40-
#endif // NORDIC_INTERFACE_H

libraries/CurieBLE/examples/BatteryMonitor/BatteryMonitor.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void setup() {
4545

4646
void loop() {
4747
// listen for BLE peripherals to connect:
48-
BLECentral central = blePeripheral.central();
48+
BLECentralHelper central = blePeripheral.central();
4949

5050
// if a central is connected to peripheral:
5151
if (central) {

libraries/CurieBLE/examples/CallbackLED/CallbackLED.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ void loop() {
4545
blePeripheral.poll();
4646
}
4747

48-
void blePeripheralConnectHandler(BLECentral& central) {
48+
void blePeripheralConnectHandler(BLEHelper& central) {
4949
// central connected event handler
5050
Serial.print("Connected event, central: ");
5151
Serial.println(central.address());
5252
}
5353

54-
void blePeripheralDisconnectHandler(BLECentral& central) {
54+
void blePeripheralDisconnectHandler(BLEHelper& central) {
5555
// central disconnected event handler
5656
Serial.print("Disconnected event, central: ");
5757
Serial.println(central.address());
5858
}
5959

60-
void switchCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
60+
void switchCharacteristicWritten(BLEHelper& central, BLECharacteristic& characteristic) {
6161
// central wrote new value to characteristic, update LED
6262
Serial.print("Characteristic event, written: ");
6363

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
Copyright (c) 2016 Intel Corporation. All rights reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#include <CurieBLE.h>
20+
21+
/*
22+
This sketch example partially implements the standard Bluetooth Low-Energy Battery service.
23+
For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
24+
*/
25+
26+
#define MAX_IMU_RECORD 1
27+
28+
struct bt_le_conn_param conn_param = {0x18, 0x28, 0, 400};
29+
typedef struct {
30+
int index;
31+
unsigned int slot[3];
32+
} imuFrameType;
33+
34+
imuFrameType imuBuf[MAX_IMU_RECORD];
35+
BLECentral bleCentral; // BLE Central Device (the board you're programming)
36+
37+
BLEService bleImuService("F7580001-153E-D4F6-F26D-43D8D98EEB13");
38+
BLECharacteristic bleImuChar("F7580003-153E-D4F6-F26D-43D8D98EEB13", // standard 128-bit characteristic UUID
39+
BLERead | BLENotify, sizeof(imuBuf)); // remote clients will be able to
40+
// get notifications if this characteristic changes
41+
42+
void ble_connected(BLEHelper &role)
43+
{
44+
BLEPeripheralHelper&peripheral = *(BLEPeripheralHelper*)(&role);
45+
Serial.println("Connected");
46+
47+
// Start discovery the profiles in peripheral device
48+
peripheral.discover();
49+
}
50+
51+
void bleImuCharacteristicWritten(BLEHelper& central, BLECharacteristic& characteristic)
52+
{
53+
// Peripheral wrote new value to characteristic by Notification/Indication
54+
const unsigned char *cvalue = characteristic.value();
55+
const imuFrameType *value = (const imuFrameType *)cvalue;
56+
Serial.print("\r\nCharacteristic event, written: ");
57+
Serial.print(value->index);
58+
Serial.print("\t");
59+
Serial.print(value->slot[0]);
60+
Serial.print("\t");
61+
Serial.print(value->slot[1]);
62+
Serial.print("\t");
63+
Serial.println(value->slot[2]);
64+
}
65+
66+
bool adv_found(uint8_t type,
67+
const uint8_t *data,
68+
uint8_t data_len,
69+
void *user_data)
70+
{
71+
bt_addr_le_t *addr = (bt_addr_le_t *)user_data;
72+
int i;
73+
74+
Serial.print("[AD]:");
75+
Serial.print(type);
76+
Serial.print(" data_len ");
77+
Serial.println(data_len);
78+
79+
switch (type)
80+
{
81+
case BT_DATA_UUID128_SOME:
82+
case BT_DATA_UUID128_ALL:
83+
{
84+
if (data_len % MAX_UUID_SIZE != 0)
85+
{
86+
Serial.println("AD malformed");
87+
return true;
88+
}
89+
struct bt_uuid * serviceuuid = bleImuService.uuid();
90+
for (i = 0; i < data_len; i += MAX_UUID_SIZE)
91+
{
92+
if (memcmp (((struct bt_uuid_128*)serviceuuid)->val, &data[i], MAX_UUID_SIZE) != 0)
93+
{
94+
continue;
95+
}
96+
97+
// Accept the advertisement
98+
if (!bleCentral.stopScan())
99+
{
100+
Serial.println("Stop LE scan failed");
101+
continue;
102+
}
103+
Serial.println("Connecting");
104+
// Connect to peripheral
105+
bleCentral.connect(addr, &conn_param);
106+
return false;
107+
}
108+
}
109+
}
110+
111+
return true;
112+
}
113+
114+
void setup() {
115+
Serial.begin(115200); // initialize serial communication
116+
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
117+
118+
bleImuChar.setEventHandler(BLEWritten, bleImuCharacteristicWritten);
119+
120+
/* Set a local name for the BLE device
121+
This name will appear in advertising packets
122+
and can be used by remote devices to identify this BLE device
123+
The name can be changed but maybe be truncated based on space
124+
left in advertisement packet */
125+
bleCentral.addAttribute(bleImuService); // Add the BLE IMU service
126+
bleCentral.addAttribute(bleImuChar); // Add the BLE IMU characteristic
127+
128+
/* Setup callback */
129+
bleCentral.setAdvertiseHandler(adv_found);
130+
bleCentral.setEventHandler(BLEConnected, ble_connected);
131+
132+
/* Now activate the BLE device. It will start continuously transmitting BLE
133+
advertising packets and will be visible to remote BLE central devices
134+
until it receives a new connection */
135+
bleCentral.begin();
136+
}
137+
138+
139+
void loop()
140+
{
141+
delay(2000);
142+
}
143+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
Copyright (c) 2016 Intel Corporation. All rights reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#include <CurieBLE.h>
20+
#include <CurieIMU.h>
21+
22+
/*
23+
This sketch example partially implements the standard Bluetooth Low-Energy Battery service.
24+
For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
25+
*/
26+
27+
#define MAX_IMU_RECORD 1
28+
29+
typedef struct {
30+
int index;
31+
unsigned int slot[3];
32+
} imuFrameType;
33+
34+
imuFrameType imuBuf[MAX_IMU_RECORD];
35+
36+
unsigned seqNum = 0;
37+
38+
BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)
39+
BLEService bleImuService("F7580001-153E-D4F6-F26D-43D8D98EEB13"); // Tx IMU data Characteristic
40+
BLECharacteristic bleImuChar("F7580003-153E-D4F6-F26D-43D8D98EEB13", // standard 128-bit characteristic UUID
41+
BLERead | BLENotify, sizeof(imuBuf)); // remote clients will be able to
42+
// get notifications if this characteristic changes
43+
void setup() {
44+
45+
Serial.begin(9600); // initialize serial communication
46+
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
47+
48+
49+
/* Set a local name for the BLE device
50+
This name will appear in advertising packets
51+
and can be used by remote devices to identify this BLE device
52+
The name can be changed but maybe be truncated based on space left in advertisement packet */
53+
blePeripheral.setLocalName("Imu");
54+
blePeripheral.setAdvertisedServiceUuid(bleImuService.uuid()); // add the service UUID
55+
blePeripheral.addAttribute(bleImuService); // Add the BLE Battery service
56+
blePeripheral.addAttribute(bleImuChar); // add the battery level characteristic
57+
58+
/* Now activate the BLE device. It will start continuously transmitting BLE
59+
advertising packets and will be visible to remote BLE central devices
60+
until it receives a new connection */
61+
blePeripheral.begin();
62+
63+
CurieIMU.begin();
64+
}
65+
66+
void recordImuData(int index) {
67+
/* Read IMU data.
68+
*/
69+
int ax, ay, az;
70+
int gx, gy, gz;
71+
72+
imuBuf[index].index = seqNum++;
73+
CurieIMU.readMotionSensor(ax, ay, az, gx, gy, gz);
74+
75+
imuBuf[index].slot[0] = (unsigned int)((ax << 16) | (ay & 0x0FFFF));
76+
imuBuf[index].slot[1] = (unsigned int)((az << 16) | (gx & 0x0FFFF));
77+
imuBuf[index].slot[2] = (unsigned int)((gy << 16) | (gz & 0x0FFFF));
78+
79+
}
80+
81+
82+
void loop() {
83+
// listen for BLE peripherals to connect:
84+
BLECentralHelper central = blePeripheral.central();
85+
86+
// if a central is connected to peripheral:
87+
if (central)
88+
{
89+
Serial.print("Connected to central: ");
90+
// print the central's MAC address:
91+
Serial.println(central.address());
92+
93+
Serial.print("IMU buffer size: ");
94+
Serial.println(sizeof(imuBuf));
95+
96+
// turn on the LED to indicate the connection:
97+
digitalWrite(13, HIGH);
98+
99+
long currentMillis, sentTime;
100+
101+
// Send IMU data as long as the central is still connected
102+
currentMillis = sentTime = millis();
103+
while (central.connected())
104+
{
105+
// Take IMU data every 100 msec
106+
if ((millis() - sentTime) >= 100)
107+
{
108+
recordImuData(0);
109+
sentTime = millis();
110+
bleImuChar.setValue((unsigned char *)&(imuBuf[0]), sizeof(imuBuf));
111+
}
112+
} // while
113+
114+
// when the central disconnects, turn off the LED:
115+
digitalWrite(13, LOW);
116+
Serial.print("Disconnected from central: ");
117+
Serial.println(central.address());
118+
}
119+
}
120+

libraries/CurieBLE/examples/LED/LED.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void setup() {
3838

3939
void loop() {
4040
// listen for BLE peripherals to connect:
41-
BLECentral central = blePeripheral.central();
41+
BLECentralHelper central = blePeripheral.central();
4242

4343
// if a central is connected to peripheral:
4444
if (central) {

0 commit comments

Comments
 (0)