I have a BLE sensor that periodically generates 50 bytes of data, as fast as every 0.5 seconds and as slow as every 5 seconds.
I am trying to use a Nano Matter as an I2C bridge to an Uno R4 Minima controller.
But the Nano Matter Wire library does not support the transmit buffer in Follower mode and I am having trouble reliably sending the 50 bytes.
I assume I have an I2C bus timing problem, but everything I try results in inaccurate reception.
Here is the code for the Nano Matter using the Silicon Labs BLE stack and an I2C onRequest handler that simply writes 50 bytes (which doesn't work)...
#include <Wire.h>
uint8_t ProbeStatusData[50] = { 0 };
uint32_t last_blink = 0;
uint8_t blink_state = 0;
void TxHandler(void) {
for (int tx = 0; tx < 50; tx++) {
Wire.write(ProbeStatusData[tx]);
}
}
void setup() {
Wire.begin(0x42);
Wire.onRequest(TxHandler);
pinMode(LED_BUILTIN, OUTPUT);
last_blink = millis();
}
void loop() {
if ((millis() - last_blink) > 750) {
digitalWrite(LED_BUILTIN, (++blink_state & 1) ? HIGH : LOW);
last_blink = millis();
}
}
// ********************************
// Silicon Labs Bluetooth Stack
// ********************************
enum conn_state_t {
ST_BOOT,
ST_SCAN,
ST_CONNECT,
ST_SERVICE_DISCOVER,
ST_CHAR_DISCOVER,
ST_REQUEST_NOTIFICATION,
ST_RECEIVE_DATA
};
const uint8_t target_mac[6] = { 0x60, 0xa6, 0x5a, 0x05, 0x71, 0xc2 }; // c2:71:05:5a:a6:60 in reverse order
const uint8_t target_uuid[] = { 0x7A, 0x40, 0xC1, 0x51, 0xAE, 0x97, 0x44, 0x3D,
0x92, 0x37, 0xAB, 0xCA, 0x00, 0x01, 0x00, 0x00 }; // For 00000100-CAAB-3792-3D44-97AE51C1407A in little-endian
const uint8_t target_char_uuid[] = { 0x7A, 0x40, 0xC1, 0x51, 0xAE, 0x97, 0x44, 0x3D,
0x92, 0x37, 0xAB, 0xCA, 0x01, 0x01, 0x00, 0x00 }; // For 00000101-CAAB-3792-3D44-97AE51C1407A in little-endian
uint32_t CPT_service_handle = __UINT32_MAX__;
uint16_t CPT_char_handle = __UINT16_MAX__;
uint8_t data_length = 50;
conn_state_t connection_state = ST_BOOT;
// Bluetooth Stack Event Handler
void sl_bt_on_event(sl_bt_msg_t *evt) {
sl_status_t sc;
switch (SL_BT_MSG_ID(evt->header)) {
// This event is received when BLE has successfully booted
case sl_bt_evt_system_boot_id:
// Start scanning for BLE devices
sc = sl_bt_scanner_set_parameters(sl_bt_scanner_scan_mode_active, 16, 16);
app_assert_status(sc);
sc = sl_bt_scanner_start(sl_bt_scanner_scan_phy_1m,
sl_bt_scanner_discover_generic);
app_assert_status(sc);
connection_state = ST_SCAN;
break;
// This event is received when a BLE device is scanned
case sl_bt_evt_scanner_legacy_advertisement_report_id:
// Check if the BLE device is our CPT MAC address
if (memcmp(evt->data.evt_scanner_legacy_advertisement_report.address.addr, target_mac, 6) == 0) {
// Stop scanning
sc = sl_bt_scanner_stop();
app_assert_status(sc);
// Connect to the device
sc = sl_bt_connection_open(evt->data.evt_scanner_legacy_advertisement_report.address,
evt->data.evt_scanner_legacy_advertisement_report.address_type,
sl_bt_gap_phy_1m,
NULL);
app_assert_status(sc);
connection_state = ST_CONNECT;
}
break;
// This event is received when a BLE connection has been opened
case sl_bt_evt_connection_opened_id:
// Discover the target Probe Status service
sc = sl_bt_gatt_discover_primary_services_by_uuid(evt->data.evt_connection_opened.connection,
sizeof(target_uuid),
target_uuid);
app_assert_status(sc);
connection_state = ST_SERVICE_DISCOVER;
break;
// This event is received when a BLE connection has been closed
case sl_bt_evt_connection_closed_id:
// Restart scanning
sc = sl_bt_scanner_start(sl_bt_scanner_scan_phy_1m,
sl_bt_scanner_discover_generic);
app_assert_status(sc);
connection_state = ST_SCAN;
break;
// This event is generated when the target service is discovered
case sl_bt_evt_gatt_service_id:
// Store the handle of the discovered service
CPT_service_handle = evt->data.evt_gatt_service.service;
break;
// This event is generated when the target characteristic is discovered
case sl_bt_evt_gatt_characteristic_id:
// Store the handle of the discovered characteristic
CPT_char_handle = evt->data.evt_gatt_characteristic.characteristic;
break;
// This event is received when the GATT procedure completes
case sl_bt_evt_gatt_procedure_completed_id:
if (connection_state == ST_SERVICE_DISCOVER) {
// Discover target characteristic on the CPT
sc = sl_bt_gatt_discover_characteristics_by_uuid(evt->data.evt_gatt_procedure_completed.connection,
CPT_service_handle,
sizeof(target_char_uuid),
target_char_uuid);
app_assert_status(sc);
connection_state = ST_CHAR_DISCOVER;
break;
}
if (connection_state == ST_CHAR_DISCOVER) {
// Enable update notifications
sc = sl_bt_gatt_set_characteristic_notification(evt->data.evt_gatt_procedure_completed.connection,
CPT_char_handle,
sl_bt_gatt_notification);
app_assert_status(sc);
connection_state = ST_REQUEST_NOTIFICATION;
break;
}
if (connection_state == ST_REQUEST_NOTIFICATION) {
// Subscribed to notifications
connection_state = ST_RECEIVE_DATA;
}
break;
// This event is received when the characteristic value ia updated
case sl_bt_evt_gatt_characteristic_value_id:
// Check if this is a notification
if (evt->data.evt_gatt_characteristic_value.att_opcode == sl_bt_gatt_handle_value_notification) {
// Check if this is from our target characteristic
if (evt->data.evt_gatt_characteristic_value.characteristic == CPT_char_handle) {
// Read the Probe Status data
data_length = evt->data.evt_gatt_characteristic_value.value.len;
memcpy(ProbeStatusData,
evt->data.evt_gatt_characteristic_value.value.data,
data_length);
}
}
break;
// Default event handler
default:
break;
}
}
#ifndef BLE_STACK_SILABS
#error "This code rquires the Silicon Labs BLE stack. Please select 'BLE (Silabs)' in 'Tools > Protocol stack'."
#endif
I know the Nano Matter is properly being notified and reading the probe data.
And the I2C connection between Nano Matter Connector Carrier is using pullup resistors and a voltage translation / isolator between it and the Uno R4 Minima.
Everything works except the Nano Matter in Follower mode because it cannot use the transmit buffer like this...
void TxHandler(void) {
Wire.write(ProbeStatusData, 50);
}
One the Uno R4 Minima I have tried a few different ways to read the data.
This is the basic code to request sensor data every second...
#include <Wire.h>
uint32_t last_millis = 0;
uint8_t RxByte[50] = {};
void setup() {
Wire.begin();
Serial.begin(9600);
Serial.println("here we go...");
last_millis = millis();
}
void loop() {
if (millis() - last_millis > 1000) {
Wire.requestFrom(0x42, 50);
for (int i = 0; i < 50; i++) {
RxByte[i] = Wire.read();
}
Serial.print(millis());
Serial.print(" - ");
for (int x = 0; x < 50; x++) {
Serial.print(RxByte[x]);
}
Serial.println();
last_millis = millis();
}
}
The received data is not accurate.
The same occurs if I try to do 50 individual 1 byte reads...
for (int i = 0; i < 50; i++) {
Wire.requestFrom(0x42, 1);
RxByte[i] = Wire.read();
}
Does anyone know how to arbitrate and successfully transmit 50 bytes from an I2C Follower using an On Request event handler on a controller which does not support the transmit buffer?
Between other Arduino boards and even the Nano Matter in Leader mode, using...
// Peripheral Follower
Wire.write(ProbeStatusData, 50);
// Controller Leader
Wire.requestFrom(0x42, 50);
for (int i = 0; i < 50; i++) {
RxByte[i] = Wire.read();
}
...works perfectly fine.
Thank you for any advice!