Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions device/src/logger_priority.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ int set_thread_priority_by_name(const char *thread_name, int new_priority) {
#define LOG_THREAD_PRIORITY_HIGH K_PRIO_COOP(CONFIG_NUM_COOP_PRIORITIES - 1)
#define LOG_THREAD_PRIORITY_LOW K_PRIO_PREEMPT(K_LOWEST_APPLICATION_THREAD_PRIO - 1)

bool Logger_PriorityHigh = false;

void Logger_SetPriority(bool high) {
Logger_PriorityHigh = high;
set_thread_priority_by_name("logging", high ? LOG_THREAD_PRIORITY_HIGH : LOG_THREAD_PRIORITY_LOW);
set_thread_priority_by_name("UhkShell", SHELL_THREAD_PRIORITY);
set_thread_priority_by_name("shell_rtt", SHELL_THREAD_PRIORITY);
Expand Down
3 changes: 3 additions & 0 deletions device/src/logger_priority.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

// Functions:

// True while the logging thread runs at high priority (see Logger_SetPriority).
extern bool Logger_PriorityHigh;

void Logger_SetPriority(bool high);

#endif // __MAIN_H__
Expand Down
25 changes: 25 additions & 0 deletions right/src/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <zephyr/logging/log_ctrl.h>
#include <zephyr/shell/shell.h>
#include <zephyr/shell/shell_uart.h>
#include "logger_priority.h"
#if DEVICE_IS_KEYBOARD
#include "keyboard/uart_bridge.h"
#ifdef DEVICE_HAS_OLED
Expand All @@ -47,6 +48,24 @@ char BUFFER[MAX_LOG_LENGTH]; \
BUFFER[MAX_LOG_LENGTH-1] = '\0'; \
}

#ifdef __ZEPHYR__
// With the logging thread at high priority, thread-context producers wait for the deferred
// log buffer to drain instead of overflowing it (the buffer overwrites its oldest messages).
// ISR-context producers cannot wait and may still drop. Bounded, so a stalled log thread
// can't hang callers. Must not run inside a REENTRANCY_GUARD - it drops concurrent logs.
#define LOG_BACKPRESSURE_MAX_BUFFERED 8
#define LOG_BACKPRESSURE_MAX_WAIT_MS 50

static void uartLogBackpressure(void) {
if (Logger_PriorityHigh && !k_is_in_isr()) {
for (uint8_t i = 0; i < LOG_BACKPRESSURE_MAX_WAIT_MS && log_buffered_cnt() > LOG_BACKPRESSURE_MAX_BUFFERED; i++) {
log_thread_trigger();
k_msleep(1);
}
}
}
#endif

void Uart_LogConstant(const char* buffer) {
#ifdef __ZEPHYR__
printk("%s", buffer);
Expand All @@ -57,6 +76,7 @@ void Uart_Log(const char *fmt, ...) {
#ifdef __ZEPHYR__
EXPAND_STRING(buffer);

uartLogBackpressure();
Uart_LogConstant(buffer);
#endif
}
Expand Down Expand Up @@ -165,6 +185,11 @@ void LogUSDO(const char *fmt, ...) {
}

void LogConstantTo(device_id_t deviceId, log_target_t logMask, const char* buffer) {
#ifdef __ZEPHYR__
if ((logMask & LogTarget_Uart) && DEBUG_LOG_UART && (DEVICE_IS_UHK60 || DEVICE_ID == deviceId)) {
uartLogBackpressure();
}
#endif
REENTRANCY_GUARD_BEGIN;
if (DEVICE_IS_UHK60 || DEVICE_ID == deviceId) {
#if DEVICE_HAS_OLED
Expand Down