Skip to content

Commit 965c58a

Browse files
committed
suppress tx_error and automatic pairing mode when usb is connected
1 parent 4d68aef commit 965c58a

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/connection/esb.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ static uint8_t paired_addr[8] = {0};
5454
static bool esb_initialized = false;
5555
static bool esb_paired = false;
5656

57+
#define TX_ERROR_THRESHOLD 100
58+
5759
LOG_MODULE_REGISTER(esb_event, LOG_LEVEL_INF);
5860

5961
static void esb_thread(void);
@@ -64,18 +66,15 @@ void event_handler(struct esb_evt const *event)
6466
switch (event->evt_id)
6567
{
6668
case ESB_EVENT_TX_SUCCESS:
67-
if (tx_errors >= 100)
68-
set_status(SYS_STATUS_CONNECTION_ERROR, false);
6969
tx_errors = 0;
7070
if (esb_paired)
7171
clocks_stop();
7272
break;
7373
case ESB_EVENT_TX_FAILED:
74-
if (++tx_errors == 100) // consecutive failure to transmit
75-
{
74+
if (tx_errors < UINT32_MAX)
75+
tx_errors++;
76+
if (tx_errors == TX_ERROR_THRESHOLD) // consecutive failure to transmit
7677
last_tx_success = k_uptime_get();
77-
set_status(SYS_STATUS_CONNECTION_ERROR, true);
78-
}
7978
LOG_DBG("TX FAILED");
8079
if (esb_paired)
8180
clocks_stop();
@@ -443,26 +442,34 @@ bool esb_ready(void)
443442

444443
static void esb_thread(void)
445444
{
445+
int64_t start_time = k_uptime_get();
446+
446447
// Read paired address from retained
447448
memcpy(paired_addr, retained->paired_addr, sizeof(paired_addr));
448449

449450
while (1)
450451
{
451-
if (!esb_paired)
452+
if (!esb_paired && get_status(SYS_STATUS_USB_CONNECTED) == false && k_uptime_get() - 750 > start_time) // only automatically enter pairing while not potentially communicating by usb
452453
{
453454
esb_pair();
454455
esb_initialize(true);
455456
}
456-
if (tx_errors >= 100)
457+
if (tx_errors >= TX_ERROR_THRESHOLD)
457458
{
459+
if (get_status(SYS_STATUS_CONNECTION_ERROR) == false && get_status(SYS_STATUS_USB_CONNECTED) == false) // only raise error while not potentially communicating by usb
460+
set_status(SYS_STATUS_CONNECTION_ERROR, true);
458461
#if USER_SHUTDOWN_ENABLED
459-
if (k_uptime_get() - last_tx_success > CONFIG_CONNECTION_TIMEOUT_DELAY) // shutdown if receiver is not detected
462+
if (k_uptime_get() - last_tx_success > CONFIG_CONNECTION_TIMEOUT_DELAY) // shutdown if receiver is not detected // TODO: is shutdown necessary if usb is connected at the time?
460463
{
461464
LOG_WRN("No response from receiver in %dm", CONFIG_CONNECTION_TIMEOUT_DELAY / 60000);
462465
sys_request_system_off();
463466
}
464467
#endif
465468
}
469+
else if (tx_errors == 0 && get_status(SYS_STATUS_CONNECTION_ERROR) == true)
470+
{
471+
set_status(SYS_STATUS_CONNECTION_ERROR, false);
472+
}
466473
k_msleep(100);
467474
}
468475
}

0 commit comments

Comments
 (0)