Skip to content

cordio: lower power polling with timeout #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/utility/HCICordioTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively I think we can rename it in the file, as it doesn't seem to be compiled in mbed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming it here should work just fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@facchinm great, just pushed. This is ready for review now.

Later on we could be a bit smarter and change the poll interval dynamically based on the connection, advertising or scan intervals. However, let's see how feedback from customers goes.


static void bleLoop()
{
#if CORDIO_ZERO_COPY_HCI
Expand Down Expand Up @@ -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
Expand All @@ -147,7 +161,8 @@ static void bleLoop()
#endif // CORDIO_ZERO_COPY_HCI
}

rtos::Thread bleLoopThread;
static rtos::EventFlags bleEventFlags;
static rtos::Thread bleLoopThread;


HCICordioTransportClass::HCICordioTransportClass()
Expand Down Expand Up @@ -184,11 +199,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
bleEventFlags.wait_all(0x01, timeout, true);
}

int HCICordioTransportClass::available()
Expand Down Expand Up @@ -232,6 +248,8 @@ void HCICordioTransportClass::handleRxData(uint8_t* data, uint8_t len)
for (int i = 0; i < len; i++) {
_rxBuf.store_char(data[i]);
}

bleEventFlags.set(0x01);
}

HCICordioTransportClass HCICordioTransport;
Expand Down