Skip to content

Commit 5e44386

Browse files
cordio: lower power polling with timeout (stm32duino#15)
* cordio: use signals to wait for new HCI data when polling * Use rtos::ThisThread::sleep_for(ms) for sleeping > 1 ms, and rtos::EventFlags * Rename WSF_MS_PER_TICK to CORDIO_TRANSPORT_WSF_MS_PER_TICK
1 parent a0c6789 commit 5e44386

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

Diff for: src/utility/HCICordioTransport.cpp

+25-8
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ extern "C" void wsf_mbed_ble_signal_event(void)
103103
}
104104
#endif //CORDIO_ZERO_COPY_HCI
105105

106+
#define CORDIO_TRANSPORT_WSF_MS_PER_TICK 10
107+
106108
static void bleLoop()
107109
{
108110
#if CORDIO_ZERO_COPY_HCI
@@ -114,7 +116,7 @@ static void bleLoop()
114116
timer.reset();
115117

116118
uint64_t last_update_ms = (last_update_us / 1000);
117-
wsfTimerTicks_t wsf_ticks = (last_update_ms / WSF_MS_PER_TICK);
119+
wsfTimerTicks_t wsf_ticks = (last_update_ms / CORDIO_TRANSPORT_WSF_MS_PER_TICK);
118120

119121
if (wsf_ticks > 0) {
120122
WsfTimerUpdate(wsf_ticks);
@@ -135,9 +137,20 @@ static void bleLoop()
135137
uint64_t time_spent = (uint64_t) timer.read_high_resolution_us();
136138

137139
/* don't bother sleeping if we're already past tick */
138-
if (sleep && (WSF_MS_PER_TICK * 1000 > time_spent)) {
140+
if (sleep && (CORDIO_TRANSPORT_WSF_MS_PER_TICK * 1000 > time_spent)) {
139141
/* sleep to maintain constant tick rate */
140-
wait_us(WSF_MS_PER_TICK * 1000 - time_spent);
142+
uint64_t wait_time_us = CORDIO_TRANSPORT_WSF_MS_PER_TICK * 1000 - time_spent;
143+
uint64_t wait_time_ms = wait_time_us / 1000;
144+
145+
wait_time_us = wait_time_us % 1000;
146+
147+
if (wait_time_ms) {
148+
rtos::ThisThread::sleep_for(wait_time_ms);
149+
}
150+
151+
if (wait_time_us) {
152+
wait_us(wait_time_us);
153+
}
141154
}
142155
}
143156
#else
@@ -147,7 +160,8 @@ static void bleLoop()
147160
#endif // CORDIO_ZERO_COPY_HCI
148161
}
149162

150-
rtos::Thread bleLoopThread;
163+
static rtos::EventFlags bleEventFlags;
164+
static rtos::Thread bleLoopThread;
151165

152166

153167
HCICordioTransportClass::HCICordioTransportClass() :
@@ -189,11 +203,12 @@ void HCICordioTransportClass::end()
189203

190204
void HCICordioTransportClass::wait(unsigned long timeout)
191205
{
192-
for (unsigned long start = millis(); (millis() - start) < timeout;) {
193-
if (available()) {
194-
break;
195-
}
206+
if (available()) {
207+
return;
196208
}
209+
210+
// wait for handleRxData to signal
211+
bleEventFlags.wait_all(0x01, timeout, true);
197212
}
198213

199214
int HCICordioTransportClass::available()
@@ -241,6 +256,8 @@ void HCICordioTransportClass::handleRxData(uint8_t* data, uint8_t len)
241256
for (int i = 0; i < len; i++) {
242257
_rxBuf.store_char(data[i]);
243258
}
259+
260+
bleEventFlags.set(0x01);
244261
}
245262

246263
HCICordioTransportClass HCICordioTransport;

0 commit comments

Comments
 (0)