2525#include < Arduino.h>
2626#include < mbed.h>
2727
28+ #if defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7)
29+ #include " ble/BLE.h"
30+ #include < events/mbed_events.h>
31+ #endif
32+
2833// Parts of this file are based on: https://github.com/ARMmbed/mbed-os-cordio-hci-passthrough/pull/2
2934// With permission from the Arm Mbed team to re-license
3035
@@ -174,6 +179,17 @@ HCICordioTransportClass::~HCICordioTransportClass()
174179{
175180}
176181
182+ #if defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7)
183+ events::EventQueue eventQueue (10 * EVENTS_EVENT_SIZE);
184+ void scheduleMbedBleEvents (BLE::OnEventsToProcessCallbackContext *context) {
185+ eventQueue.call (mbed::Callback<void ()>(&context->ble , &BLE::processEvents));
186+ }
187+
188+ void completeCallback (BLE::InitializationCompleteCallbackContext *context) {
189+ eventQueue.break_dispatch ();
190+ }
191+ #endif
192+
177193int HCICordioTransportClass::begin ()
178194{
179195 _rxBuf.clear ();
@@ -183,8 +199,20 @@ int HCICordioTransportClass::begin()
183199 init_wsf (bufPoolDesc);
184200#endif
185201
202+ #if defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7)
203+ BLE &ble = BLE::Instance ();
204+ ble.onEventsToProcess (scheduleMbedBleEvents);
205+
206+ ble.init (completeCallback);
207+ eventQueue.dispatch (10000 );
208+
209+ if (!ble.hasInitialized ()){
210+ return 0 ;
211+ }
212+ #else
186213 CordioHCIHook::getDriver ().initialize ();
187214 CordioHCIHook::getDriver ().start_reset_sequence ();
215+ #endif
188216
189217 if (bleLoopThread == NULL ) {
190218 bleLoopThread = new rtos::Thread ();
@@ -205,7 +233,6 @@ void HCICordioTransportClass::end()
205233 delete bleLoopThread;
206234 bleLoopThread = NULL ;
207235 }
208-
209236 CordioHCIHook::getDriver ().terminate ();
210237
211238 _begun = false ;
0 commit comments