From de0bcbbe5ef3522eb2c0652b99d5351212765e95 Mon Sep 17 00:00:00 2001
From: Martino Facchin <m.facchin@arduino.cc>
Date: Thu, 26 Oct 2023 17:06:06 +0200
Subject: [PATCH] Fix functionality on Giga CM4 core

Requires https://github.com/arduino/ArduinoCore-mbed/pull/741
---
 src/local/BLELocalDevice.cpp        |  3 +++
 src/utility/CordioHCICustomDriver.h | 17 +++++++++++++++++
 src/utility/HCICordioTransport.cpp  | 10 ++++++----
 src/utility/HCIUartTransport.cpp    |  4 +++-
 4 files changed, 29 insertions(+), 5 deletions(-)
 create mode 100644 src/utility/CordioHCICustomDriver.h

diff --git a/src/local/BLELocalDevice.cpp b/src/local/BLELocalDevice.cpp
index 146d99b9..be603364 100644
--- a/src/local/BLELocalDevice.cpp
+++ b/src/local/BLELocalDevice.cpp
@@ -72,7 +72,10 @@ int BLELocalDevice::begin()
 #elif defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_GIGA) || defined(ARDUINO_OPTA)
   // BT_REG_ON -> HIGH
   pinMode(BT_REG_ON, OUTPUT);
+  digitalWrite(BT_REG_ON, LOW);
+  delay(500);
   digitalWrite(BT_REG_ON, HIGH);
+  delay(500);
 #elif defined(ARDUINO_PORTENTA_C33)
 #define NINA_GPIO0      (100)
 #define NINA_RESETN     (101)
diff --git a/src/utility/CordioHCICustomDriver.h b/src/utility/CordioHCICustomDriver.h
new file mode 100644
index 00000000..fc062ee8
--- /dev/null
+++ b/src/utility/CordioHCICustomDriver.h
@@ -0,0 +1,17 @@
+#if defined(CORE_CM4)
+
+#include "CyH4TransportDriver.h"
+
+ble::vendor::cypress_ble::CyH4TransportDriver& ble_cordio_get_h4_transport_driver()
+{
+    static  ble::vendor::cypress_ble::CyH4TransportDriver s_transport_driver(
+        /* TX */ CYBSP_BT_UART_TX, /* RX */ CYBSP_BT_UART_RX,
+        /* cts */ CYBSP_BT_UART_CTS, /* rts */ CYBSP_BT_UART_RTS, NC, DEF_BT_BAUD_RATE,
+        CYBSP_BT_HOST_WAKE, CYBSP_BT_DEVICE_WAKE
+    );
+    return s_transport_driver;
+}
+
+#define CUSTOM_HCI_DRIVER
+
+#endif
\ No newline at end of file
diff --git a/src/utility/HCICordioTransport.cpp b/src/utility/HCICordioTransport.cpp
index 947378cf..7848712f 100644
--- a/src/utility/HCICordioTransport.cpp
+++ b/src/utility/HCICordioTransport.cpp
@@ -17,8 +17,7 @@
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
-#if defined(ARDUINO_ARCH_MBED) && !defined(TARGET_NANO_RP2040_CONNECT)
-
+#if defined(ARDUINO_ARCH_MBED) && !defined(TARGET_NANO_RP2040_CONNECT) // && !defined(CORE_CM4)
 #include <Arduino.h>
 #include <mbed.h>
 
@@ -53,6 +52,8 @@
 #define BLE_NAMESPACE ble::vendor::cordio
 #endif
 
+#include "CordioHCICustomDriver.h"
+
 extern BLE_NAMESPACE::CordioHCIDriver& ble_cordio_get_hci_driver();
 
 namespace BLE_NAMESPACE {
@@ -181,7 +182,7 @@ HCICordioTransportClass::~HCICordioTransportClass()
 {
 }
 
-#if defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_GIGA) || defined(ARDUINO_OPTA)
+#if (defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_GIGA) || defined(ARDUINO_OPTA)) && !defined(CUSTOM_HCI_DRIVER)
 events::EventQueue eventQueue(10 * EVENTS_EVENT_SIZE);
 void scheduleMbedBleEvents(BLE::OnEventsToProcessCallbackContext *context) {
   eventQueue.call(mbed::Callback<void()>(&context->ble, &BLE::processEvents));
@@ -201,7 +202,8 @@ int HCICordioTransportClass::begin()
   init_wsf(bufPoolDesc);
 #endif
 
-#if defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_GIGA) || defined(ARDUINO_OPTA)
+#if (defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_GIGA) || defined(ARDUINO_OPTA)) && !defined(CUSTOM_HCI_DRIVER)
+
   BLE &ble = BLE::Instance();
   ble.onEventsToProcess(scheduleMbedBleEvents);
 
diff --git a/src/utility/HCIUartTransport.cpp b/src/utility/HCIUartTransport.cpp
index d3c44d8f..d6f9ec3e 100644
--- a/src/utility/HCIUartTransport.cpp
+++ b/src/utility/HCIUartTransport.cpp
@@ -17,7 +17,7 @@
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
-#if !defined(ARDUINO_ARCH_MBED) && !defined(ESP32) && !defined(ARDUINO_UNOR4_WIFI) || defined(TARGET_NANO_RP2040_CONNECT)
+#if !defined(ARDUINO_ARCH_MBED) && !defined(ESP32) && !defined(ARDUINO_UNOR4_WIFI) || defined(TARGET_NANO_RP2040_CONNECT) //|| defined(CORE_CM4)
 
 #include "HCIUartTransport.h"
 
@@ -33,6 +33,8 @@
 #define SerialHCI Serial3
 #elif defined(ARDUINO_PORTENTA_C33)
 #define SerialHCI Serial5
+#elif defined(ARDUINO_GIGA)
+arduino::UART SerialHCI(CYBSP_BT_UART_TX, CYBSP_BT_UART_RX, CYBSP_BT_UART_RTS, CYBSP_BT_UART_CTS);
 #else
 #error "Unsupported board selected!"
 #endif