Skip to content

Commit

Permalink
Merge pull request #22 from bcmi-labs/speedUpBLE
Browse files Browse the repository at this point in the history
Speed up dfu via BLE
  • Loading branch information
facchinm authored Jun 17, 2021
2 parents e91fbdf + 962c613 commit 1073687
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 148 deletions.
15 changes: 11 additions & 4 deletions Arduino_BHY2/src/Arduino_BHY2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include "mbed.h"
#include "Nicla_System.h"

mbed::DigitalIn eslovInt(p19, PullUp);

Arduino_BHY2::Arduino_BHY2() :
_debug(NULL),
_pingTime(0),
Expand Down Expand Up @@ -39,10 +37,18 @@ void Arduino_BHY2::pingI2C() {
void Arduino_BHY2::checkEslovInt() {
if (millis() - _startTime < _timeout) {
//Timeout didn't expire yet
if (!eslovInt) {
if (!digitalRead(p19)) {
//Wait for MKR to clear Eslov Int pin
while(!digitalRead(p19)) {}
if (_debug) _debug->println("MKR released Eslov Int pin");

//Change mode for Eslov Int pin
//Eslov Int Pin will be used to synchronize Dfu via Eslov
pinMode(p19, OUTPUT);

eslovHandler.begin();
//Eslov has been activated
_eslovActive = true;
eslovHandler.begin();
}
} else {
//Timeout expired
Expand All @@ -57,6 +63,7 @@ void Arduino_BHY2::setLDOTimeout(int time) {

bool Arduino_BHY2::begin()
{
pinMode(p19, INPUT);
nicla::begin();
_startTime = millis();
nicla::enable3V3LDO();
Expand Down
16 changes: 1 addition & 15 deletions Arduino_BHY2/src/BLEHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

// DFU channels
BLEService dfuService("34c2e3b8-34aa-11eb-adc1-0242ac120002");
auto dfuAckUuid = "34c2e3be-34aa-11eb-adc1-0242ac120002";
auto dfuInternalUuid = "34c2e3b9-34aa-11eb-adc1-0242ac120002";
auto dfuExternalUuid = "34c2e3ba-34aa-11eb-adc1-0242ac120002";
BLECharacteristic dfuAckCharacteristic(dfuAckUuid, (BLERead | BLENotify), 1);
BLECharacteristic dfuInternalCharacteristic(dfuInternalUuid, BLEWrite, sizeof(DFUPacket), true);
BLECharacteristic dfuExternalCharacteristic(dfuExternalUuid, BLEWrite, sizeof(DFUPacket), true);

Expand All @@ -28,16 +26,6 @@ BLEHandler::~BLEHandler()
{
}

void BLEHandler::writeDFUAcknowledgment()
{
uint8_t ack = dfuManager.acknowledgment();
dfuAckCharacteristic.writeValue(ack);

if (_lastDfuPack && ack == 0x0F) {
dfuManager.closeDfu();
}
}

// DFU channel
void BLEHandler::processDFUPacket(DFUType dfuType, BLECharacteristic characteristic)
{
Expand All @@ -52,9 +40,8 @@ void BLEHandler::processDFUPacket(DFUType dfuType, BLECharacteristic characteris
if (data[0]) {
//Last packet
_lastDfuPack = true;
dfuManager.closeDfu();
}

writeDFUAcknowledgment();
}

void BLEHandler::receivedInternalDFU(BLEDevice central, BLECharacteristic characteristic)
Expand Down Expand Up @@ -97,7 +84,6 @@ bool BLEHandler::begin()
BLE.setAdvertisedService(dfuService);
dfuService.addCharacteristic(dfuInternalCharacteristic);
dfuService.addCharacteristic(dfuExternalCharacteristic);
dfuService.addCharacteristic(dfuAckCharacteristic);
BLE.addService(dfuService);
dfuInternalCharacteristic.setEventHandler(BLEWritten, receivedInternalDFU);
dfuExternalCharacteristic.setEventHandler(BLEWritten, receivedExternalDFU);
Expand Down
1 change: 0 additions & 1 deletion Arduino_BHY2/src/BLEHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class BLEHandler {

bool _lastDfuPack;

void writeDFUAcknowledgment();
void processDFUPacket(DFUType dfuType, BLECharacteristic characteristic);

static void receivedInternalDFU(BLEDevice central, BLECharacteristic characteristic);
Expand Down
27 changes: 16 additions & 11 deletions Arduino_BHY2/src/DFUManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,23 @@ bool DFUManager::begin()

void DFUManager::processPacket(DFUSource source, DFUType dfuType, const uint8_t* data)
{
DFUPacket* packet = (DFUPacket*)data;
_transferPending = true;
_dfuSource = source;

if (source == bleDFU && eslovHandler.eslovActive) {
eslovHandler.eslovActive = false;
eslovHandler.end();
} else if (source == eslovDFU && bleHandler.bleActive) {
bleHandler.bleActive = false;
bleHandler.end();
DFUPacket* packet = (DFUPacket*)data;

if (source == bleDFU) {
//If Eslov is still active, turn it off
if (eslovHandler.eslovActive) {
eslovHandler.eslovActive = false;
eslovHandler.end();
}
} else {
//If BLE is still active, turn it off
if (bleHandler.bleActive) {
bleHandler.bleActive = false;
bleHandler.end();
}
}

if (_debug) {
Expand Down Expand Up @@ -90,10 +97,8 @@ void DFUManager::processPacket(DFUSource source, DFUType dfuType, const uint8_t*
_debug->print("Last packet received. Remaining: ");
_debug->println(packet->remaining);
}
if (_acknowledgment == DFUAck) {
fclose(_target);
_target = NULL;
}
fclose(_target);
_target = NULL;
}
}

Expand Down
3 changes: 2 additions & 1 deletion Arduino_BHY2/src/DFUManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct __attribute__((packed)) DFUPacket {
uint16_t index: 15;
uint16_t remaining: 15;
};
uint8_t data[192];
uint8_t data[232];
};

class DFUManager {
Expand All @@ -45,6 +45,7 @@ class DFUManager {

uint8_t acknowledgment();


private:
static SPIFBlockDevice _bd;
static mbed::LittleFileSystem _fs;
Expand Down
49 changes: 29 additions & 20 deletions Arduino_BHY2/src/EslovHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ EslovHandler::~EslovHandler()

bool EslovHandler::begin()
{
eslovBusy();
Wire.begin(ESLOV_DEFAULT_ADDRESS);
eslovActive = true;
Wire.onReceive(EslovHandler::onReceive);
Wire.onRequest(EslovHandler::onRequest);
eslovAvailable();
return true;
}

Expand All @@ -35,6 +37,18 @@ void EslovHandler::onRequest()
eslovHandler.requestEvent();
}

void EslovHandler::eslovBusy()
{
//Set Eslov INT pin
digitalWrite(p19, LOW);
}

void EslovHandler::eslovAvailable()
{
//Release Eslov INT pin
digitalWrite(p19, HIGH);
}

void EslovHandler::requestEvent()
{
if (_debug) {
Expand All @@ -56,16 +70,6 @@ void EslovHandler::requestEvent()
_debug->println(data.size);
}

} else if (_state == ESLOV_DFU_ACK_STATE) {
uint8_t ack = dfuManager.acknowledgment();
if (_lastDfuPack && ack == 0x0F) {
dfuManager.closeDfu();
}
if (_debug) {
_debug->print("Ack: ");
_debug->println(ack);
}
Wire.write(ack);
} else if (_state == ESLOV_SENSOR_ACK_STATE) {
uint8_t ack = sensortec.acknowledgment();
if (_debug) {
Expand All @@ -90,35 +94,40 @@ void EslovHandler::receiveEvent(int length)

while(Wire.available())
{
eslovBusy();
_rxBuffer[_rxIndex++] = Wire.read();

// Check if packet is complete depending on its opcode
if (_rxBuffer[0] == ESLOV_DFU_EXTERNAL_OPCODE) {
if (_rxIndex == sizeof(DFUPacket) + 1) {
dfuManager.processPacket(eslovDFU, DFU_EXTERNAL, &_rxBuffer[1]);

dump();

_rxIndex = 0;

//Last packet
if (_rxBuffer[1]) {
_lastDfuPack = true;
dfuManager.closeDfu();
}

_state = ESLOV_DFU_ACK_STATE;

dump();
_rxIndex = 0;
eslovAvailable();
}

} else if (_rxBuffer[0] == ESLOV_DFU_INTERNAL_OPCODE) {
if (_rxIndex == sizeof(DFUPacket) + 1) {
dfuManager.processPacket(eslovDFU, DFU_INTERNAL, &_rxBuffer[1]);

dump();

_rxIndex = 0;

//Last packet
if (_rxBuffer[1]) {
_lastDfuPack = true;
dfuManager.closeDfu();
}

_state = ESLOV_DFU_ACK_STATE;

dump();
_rxIndex = 0;
eslovAvailable();
}

} else if (_rxBuffer[0] == ESLOV_SENSOR_CONFIG_OPCODE) {
Expand Down
3 changes: 3 additions & 0 deletions Arduino_BHY2/src/EslovHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class EslovHandler {
void receiveEvent(int length);
void requestEvent();

void eslovBusy();
void eslovAvailable();

int _rxIndex;
uint8_t _rxBuffer[ESLOV_MAX_LENGTH];

Expand Down
2 changes: 1 addition & 1 deletion Arduino_BHY2_HOST/src/DFUTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct __attribute__((packed)) DFUPacket {
uint16_t index: 15;
uint16_t remaining: 15;
};
uint8_t data[192];
uint8_t data[232];
};

#endif
Loading

0 comments on commit 1073687

Please sign in to comment.