Software uart log: better locking mechanism

This commit is contained in:
DrZlo13 2022-02-25 11:55:40 +10:00
parent d845ae3d72
commit b137997f29
1 changed files with 5 additions and 4 deletions

View File

@ -4,19 +4,20 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#define LOG_BUFFER_SIZE (128) extern void esp_log_impl_lock(void);
extern void esp_log_impl_unlock(void);
#define LOG_BUFFER_SIZE (128)
static SoftUart* log_uart = NULL; static SoftUart* log_uart = NULL;
static char log_buffer[LOG_BUFFER_SIZE]; static char log_buffer[LOG_BUFFER_SIZE];
static int soft_uart_log_vprintf(const char* str, va_list l) { static int soft_uart_log_vprintf(const char* str, va_list l) {
portMUX_TYPE myMutex = portMUX_INITIALIZER_UNLOCKED; esp_log_impl_lock();
portENTER_CRITICAL(&myMutex);
int len = vsnprintf(log_buffer, LOG_BUFFER_SIZE, str, l); int len = vsnprintf(log_buffer, LOG_BUFFER_SIZE, str, l);
soft_uart_transmit(log_uart, (uint8_t*)log_buffer, strlen(log_buffer)); soft_uart_transmit(log_uart, (uint8_t*)log_buffer, strlen(log_buffer));
portEXIT_CRITICAL(&myMutex); esp_log_impl_unlock();
return len; return len;
} }