From 3be223197dd2031c3e146086490bbc63afb78232 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Wed, 28 Aug 2019 17:06:58 -0400 Subject: [PATCH 1/3] cordio: use signals to wait for new HCI data when polling --- src/utility/HCICordioTransport.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/utility/HCICordioTransport.cpp b/src/utility/HCICordioTransport.cpp index 125a6d20..e8eeddde 100644 --- a/src/utility/HCICordioTransport.cpp +++ b/src/utility/HCICordioTransport.cpp @@ -147,7 +147,8 @@ static void bleLoop() #endif // CORDIO_ZERO_COPY_HCI } -rtos::Thread bleLoopThread; +static osThreadId mainThreadId; +static rtos::Thread bleLoopThread; HCICordioTransportClass::HCICordioTransportClass() @@ -162,6 +163,8 @@ int HCICordioTransportClass::begin() { _rxBuf.clear(); + mainThreadId = osThreadGetId(); + #if CORDIO_ZERO_COPY_HCI ble::vendor::cordio::buf_pool_desc_t bufPoolDesc = CordioHCIHook::getDriver().get_buffer_pool_description(); init_wsf(bufPoolDesc); @@ -184,11 +187,12 @@ void HCICordioTransportClass::end() void HCICordioTransportClass::wait(unsigned long timeout) { - for (unsigned long start = millis(); (millis() - start) < timeout;) { - if (available()) { - break; - } + if (available()) { + return; } + + // wait for handleRxData to signal + rtos::ThisThread::flags_wait_all_for(0x01, timeout, true); } int HCICordioTransportClass::available() @@ -232,6 +236,8 @@ void HCICordioTransportClass::handleRxData(uint8_t* data, uint8_t len) for (int i = 0; i < len; i++) { _rxBuf.store_char(data[i]); } + + osSignalSet(mainThreadId, 0x1); } HCICordioTransportClass HCICordioTransport; From 9b35ebd33c3e73c05969e7461e46bac864a13b2a Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Tue, 10 Sep 2019 15:22:26 -0400 Subject: [PATCH 2/3] Use rtos::ThisThread::sleep_for(ms) for sleeping > 1 ms, and rtos::EventFlags --- src/utility/HCICordioTransport.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/utility/HCICordioTransport.cpp b/src/utility/HCICordioTransport.cpp index e8eeddde..774e1c14 100644 --- a/src/utility/HCICordioTransport.cpp +++ b/src/utility/HCICordioTransport.cpp @@ -103,6 +103,9 @@ extern "C" void wsf_mbed_ble_signal_event(void) } #endif //CORDIO_ZERO_COPY_HCI +#undef WSF_MS_PER_TICK +#define WSF_MS_PER_TICK 10 + static void bleLoop() { #if CORDIO_ZERO_COPY_HCI @@ -137,7 +140,18 @@ static void bleLoop() /* don't bother sleeping if we're already past tick */ if (sleep && (WSF_MS_PER_TICK * 1000 > time_spent)) { /* sleep to maintain constant tick rate */ - wait_us(WSF_MS_PER_TICK * 1000 - time_spent); + uint64_t wait_time_us = WSF_MS_PER_TICK * 1000 - time_spent; + uint64_t wait_time_ms = wait_time_us / 1000; + + wait_time_us = wait_time_us % 1000; + + if (wait_time_ms) { + rtos::ThisThread::sleep_for(wait_time_ms); + } + + if (wait_time_us) { + wait_us(wait_time_us); + } } } #else @@ -147,7 +161,7 @@ static void bleLoop() #endif // CORDIO_ZERO_COPY_HCI } -static osThreadId mainThreadId; +static rtos::EventFlags bleEventFlags; static rtos::Thread bleLoopThread; @@ -163,8 +177,6 @@ int HCICordioTransportClass::begin() { _rxBuf.clear(); - mainThreadId = osThreadGetId(); - #if CORDIO_ZERO_COPY_HCI ble::vendor::cordio::buf_pool_desc_t bufPoolDesc = CordioHCIHook::getDriver().get_buffer_pool_description(); init_wsf(bufPoolDesc); @@ -192,7 +204,7 @@ void HCICordioTransportClass::wait(unsigned long timeout) } // wait for handleRxData to signal - rtos::ThisThread::flags_wait_all_for(0x01, timeout, true); + bleEventFlags.wait_all(0x01, timeout, true); } int HCICordioTransportClass::available() @@ -237,7 +249,7 @@ void HCICordioTransportClass::handleRxData(uint8_t* data, uint8_t len) _rxBuf.store_char(data[i]); } - osSignalSet(mainThreadId, 0x1); + bleEventFlags.set(0x01); } HCICordioTransportClass HCICordioTransport; From 8f4bea8f625f69ad329a81c24799c16487d96385 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Wed, 11 Sep 2019 09:20:43 -0400 Subject: [PATCH 3/3] Rename WSF_MS_PER_TICK to CORDIO_TRANSPORT_WSF_MS_PER_TICK --- src/utility/HCICordioTransport.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/utility/HCICordioTransport.cpp b/src/utility/HCICordioTransport.cpp index 774e1c14..663554e6 100644 --- a/src/utility/HCICordioTransport.cpp +++ b/src/utility/HCICordioTransport.cpp @@ -103,8 +103,7 @@ extern "C" void wsf_mbed_ble_signal_event(void) } #endif //CORDIO_ZERO_COPY_HCI -#undef WSF_MS_PER_TICK -#define WSF_MS_PER_TICK 10 +#define CORDIO_TRANSPORT_WSF_MS_PER_TICK 10 static void bleLoop() { @@ -117,7 +116,7 @@ static void bleLoop() timer.reset(); uint64_t last_update_ms = (last_update_us / 1000); - wsfTimerTicks_t wsf_ticks = (last_update_ms / WSF_MS_PER_TICK); + wsfTimerTicks_t wsf_ticks = (last_update_ms / CORDIO_TRANSPORT_WSF_MS_PER_TICK); if (wsf_ticks > 0) { WsfTimerUpdate(wsf_ticks); @@ -138,9 +137,9 @@ static void bleLoop() uint64_t time_spent = (uint64_t) timer.read_high_resolution_us(); /* don't bother sleeping if we're already past tick */ - if (sleep && (WSF_MS_PER_TICK * 1000 > time_spent)) { + if (sleep && (CORDIO_TRANSPORT_WSF_MS_PER_TICK * 1000 > time_spent)) { /* sleep to maintain constant tick rate */ - uint64_t wait_time_us = WSF_MS_PER_TICK * 1000 - time_spent; + uint64_t wait_time_us = CORDIO_TRANSPORT_WSF_MS_PER_TICK * 1000 - time_spent; uint64_t wait_time_ms = wait_time_us / 1000; wait_time_us = wait_time_us % 1000;