Skip to content

Commit

Permalink
imxrt-multi: implement adaptive polling rate for RTT
Browse files Browse the repository at this point in the history
JIRA: RTOS-754
  • Loading branch information
jmaksymowicz authored and Darchiv committed Oct 16, 2024
1 parent 1c925a1 commit 48a5f47
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions multi/imxrt-multi/rtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/minmax.h>
#include <sys/mman.h>
#include <sys/threads.h>
#include <sys/file.h>
Expand All @@ -33,9 +34,11 @@

#define RTT_TX_BUF_SIZE 1024
#define RTT_RX_BUF_SIZE 256
#define RTT_POLLING_RATE_MS 20
#define RTT_NO_PICKUP_TIMEOUT_MS (2 * RTT_POLLING_RATE_MS)
#define RTT_RETRIES (RTT_NO_PICKUP_TIMEOUT_MS / RTT_POLLING_RATE_MS)
#define RTT_IDLE_TIMEOUT_US 1048576
#define RTT_NO_PICKUP_TIMEOUT_US 131072
#define RTT_RATE_IDLE_US 131072
#define RTT_RATE_MAX_US 32768
#define RTT_RATE_MIN_US 2048

/* Doesn't need to be large, data will mostly be stored in RTT buffers */
#define TTY_BUF_SIZE 64
Expand Down Expand Up @@ -77,10 +80,13 @@ static inline ssize_t rtt_txAvailMode(unsigned int chn)

static void rtt_thread(void *arg)
{
unsigned retries[RTT_ACTIVE_CNT];
memset(retries, 0, sizeof(retries));
int timeout[RTT_ACTIVE_CNT];
memset(timeout, 0, sizeof(timeout));
int pollingRate = RTT_RATE_MAX_US;
int idleTimeout = 0;

for (;;) {
unsigned chnsIdle = 0;
for (int chn_idx = 0; chn_idx < RTT_ACTIVE_CNT; chn_idx++) {
rtt_t *uart = &rtt_common.uarts[chn_idx];
unsigned char data;
Expand All @@ -92,19 +98,19 @@ static void rtt_thread(void *arg)
/* Do nothing, in this case the remaining code is unnecessary */
}
else if (librtt_txCheckReaderAttached(uart->chn) != 0) {
retries[chn_idx] = RTT_RETRIES;
timeout[chn_idx] = RTT_NO_PICKUP_TIMEOUT_US;
}
else if (onTx == 0) {
if (retries[chn_idx] == 0) {
if (timeout[chn_idx] == 0) {
onTx = 1;
}
else {
retries[chn_idx]--;
timeout[chn_idx] = max(0, timeout[chn_idx] - pollingRate);
}
}


if ((onRx == 0) && ((txReady == 0) || (onTx == 0))) {
chnsIdle++;
continue;
}

Expand All @@ -116,21 +122,39 @@ static void rtt_thread(void *arg)
onRx = librtt_rxAvail(uart->chn);
}

while (onTx > 0 && txReady) {
while ((onTx > 0) && (txReady != 0)) {
data = libtty_getchar(&uart->tty_common, NULL);
ssize_t written = librtt_write(uart->chn, &data, 1, 0);
if (written <= 0) {
uart->diag_txSkipped++;
}

onTx = rtt_txAvailMode(uart->chn);
onTx = (timeout[chn_idx] == 0) ? 1 : rtt_txAvailMode(uart->chn);
txReady = libtty_txready(&uart->tty_common);
}

mutexUnlock(uart->lock);
}

usleep(RTT_POLLING_RATE_MS * 1000);
if (chnsIdle == RTT_ACTIVE_CNT) {
if (idleTimeout == 0) {
pollingRate = RTT_RATE_IDLE_US;
}
else {
idleTimeout = max(0, idleTimeout - pollingRate);
pollingRate = min(RTT_RATE_MAX_US, (pollingRate * 3) / 2);
}
}
else {
if (idleTimeout == 0) {
pollingRate = RTT_RATE_MAX_US;
}

idleTimeout = RTT_IDLE_TIMEOUT_US;
pollingRate = max(RTT_RATE_MIN_US, pollingRate / 2);
}

usleep(pollingRate);
}
}

Expand Down

0 comments on commit 48a5f47

Please sign in to comment.