Skip to content

Commit

Permalink
feat(DEV-1287): implement more generic log-hooking
Browse files Browse the repository at this point in the history
  • Loading branch information
hennejg committed Sep 19, 2024
1 parent ecfc6a5 commit cfc3c0e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 45 deletions.
8 changes: 1 addition & 7 deletions cores/esp32/esp32-hal-log.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ extern "C"
const char * pathToFileName(const char * path);
int log_printf(const char *fmt, ...);
void log_print_buf(const uint8_t *b, size_t len);

extern void log_CrashLog(bool panic, const char *format, ...);
extern void (*log_hook)(const char*message, size_t length);

#define ARDUHAL_SHORT_LOG_FORMAT(letter, format) ARDUHAL_LOG_COLOR_ ## letter format ARDUHAL_LOG_RESET_COLOR "\r\n"
#define ARDUHAL_LOG_FORMAT(letter, format) ARDUHAL_LOG_COLOR_ ## letter "[%6u][" #letter "][%s:%u] %s(): " format ARDUHAL_LOG_RESET_COLOR "\r\n", (unsigned long) (esp_timer_get_time() / 1000ULL), pathToFileName(__FILE__), __LINE__, __FUNCTION__
Expand Down Expand Up @@ -119,7 +118,6 @@ extern void log_CrashLog(bool panic, const char *format, ...);
#define log_d(format, ...) \
do { \
log_printf(ARDUHAL_LOG_FORMAT(D, format), ##__VA_ARGS__); \
log_CrashLog(false, ARDUHAL_LOG_FORMAT(D, format), ##__VA_ARGS__); \
} while (0)
#define isr_log_d(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(D, format), ##__VA_ARGS__)
#define log_buf_d(b,l) do{ARDUHAL_LOG_COLOR_PRINT(D);log_print_buf(b,l);ARDUHAL_LOG_COLOR_PRINT_END;}while(0)
Expand All @@ -139,7 +137,6 @@ extern void log_CrashLog(bool panic, const char *format, ...);
#define log_i(format, ...) \
do { \
log_printf(ARDUHAL_LOG_FORMAT(I, format), ##__VA_ARGS__); \
log_CrashLog(false, ARDUHAL_LOG_FORMAT(I, format), ##__VA_ARGS__); \
} while (0)
#define isr_log_i(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(I, format), ##__VA_ARGS__)
#define log_buf_i(b,l) do{ARDUHAL_LOG_COLOR_PRINT(I);log_print_buf(b,l);ARDUHAL_LOG_COLOR_PRINT_END;}while(0)
Expand All @@ -159,7 +156,6 @@ extern void log_CrashLog(bool panic, const char *format, ...);
#define log_w(format, ...) \
do { \
log_printf(ARDUHAL_LOG_FORMAT(W, format), ##__VA_ARGS__); \
log_CrashLog(false, ARDUHAL_LOG_FORMAT(W, format), ##__VA_ARGS__); \
} while (0)
#define isr_log_w(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(W, format), ##__VA_ARGS__)
#define log_buf_w(b,l) do{ARDUHAL_LOG_COLOR_PRINT(W);log_print_buf(b,l);ARDUHAL_LOG_COLOR_PRINT_END;}while(0)
Expand All @@ -179,7 +175,6 @@ extern void log_CrashLog(bool panic, const char *format, ...);
#define log_e(format, ...) \
do { \
log_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__); \
log_CrashLog(false, ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__); \
} while (0)
#define isr_log_e(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__)
#define log_buf_e(b,l) do{ARDUHAL_LOG_COLOR_PRINT(E);log_print_buf(b,l);ARDUHAL_LOG_COLOR_PRINT_END;}while(0)
Expand All @@ -199,7 +194,6 @@ extern void log_CrashLog(bool panic, const char *format, ...);
#define log_n(format, ...) \
do { \
log_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__); \
log_CrashLog(false, ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__); \
} while (0)
#define isr_log_n(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__)
#define log_buf_n(b,l) do{ARDUHAL_LOG_COLOR_PRINT(E);log_print_buf(b,l);ARDUHAL_LOG_COLOR_PRINT_END;}while(0)
Expand Down
80 changes: 42 additions & 38 deletions cores/esp32/esp32-hal-uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static bool _uartDetachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
// get UART information
uart_t* uart = &_uart_bus_array[uart_num];
bool retCode = true;
//log_v("detaching UART%d pins: prev,pin RX(%d,%d) TX(%d,%d) CTS(%d,%d) RTS(%d,%d)", uart_num,
//log_v("detaching UART%d pins: prev,pin RX(%d,%d) TX(%d,%d) CTS(%d,%d) RTS(%d,%d)", uart_num,
// uart->_rxPin, rxPin, uart->_txPin, txPin, uart->_ctsPin, ctsPin, uart->_rtsPin, rtsPin); vTaskDelay(10);

// detaches pins and sets Peripheral Manager and UART information
Expand Down Expand Up @@ -179,7 +179,7 @@ static bool _uartDetachBus_RTS(void *busptr)
return _uartDetachPins(bus->num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, bus->_rtsPin);
}

// Attach function for UART
// Attach function for UART
// connects the IO Pad, set Paripheral Manager and internal UART structure data
static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin)
{
Expand All @@ -189,7 +189,7 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
}
// get UART information
uart_t* uart = &_uart_bus_array[uart_num];
//log_v("attaching UART%d pins: prev,new RX(%d,%d) TX(%d,%d) CTS(%d,%d) RTS(%d,%d)", uart_num,
//log_v("attaching UART%d pins: prev,new RX(%d,%d) TX(%d,%d) CTS(%d,%d) RTS(%d,%d)", uart_num,
// uart->_rxPin, rxPin, uart->_txPin, txPin, uart->_ctsPin, ctsPin, uart->_rtsPin, rtsPin); vTaskDelay(10);


Expand Down Expand Up @@ -285,7 +285,7 @@ void uartGetEventQueue(uart_t* uart, QueueHandle_t *q)
return;
}

bool uartIsDriverInstalled(uart_t* uart)
bool uartIsDriverInstalled(uart_t* uart)
{
if(uart == NULL) {
return false;
Expand All @@ -311,7 +311,7 @@ bool uartSetPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, in
bool retCode = true;
UART_MUTEX_LOCK();

//log_v("setting UART%d pins: prev->new RX(%d->%d) TX(%d->%d) CTS(%d->%d) RTS(%d->%d)", uart_num,
//log_v("setting UART%d pins: prev->new RX(%d->%d) TX(%d->%d) CTS(%d->%d) RTS(%d->%d)", uart_num,
// uart->_rxPin, rxPin, uart->_txPin, txPin, uart->_ctsPin, ctsPin, uart->_rtsPin, rtsPin); vTaskDelay(10);

// First step: detachs all previous UART pins
Expand Down Expand Up @@ -345,15 +345,15 @@ bool uartSetPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, in
if (rtsPinChanged) {
retCode &= _uartAttachPins(uart->num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, rtsPin);
}
UART_MUTEX_UNLOCK();
UART_MUTEX_UNLOCK();

if (!retCode) {
log_e("UART%d set pins failed.", uart_num);
}
return retCode;
}

//
//
bool uartSetHwFlowCtrlMode(uart_t *uart, uart_hw_flowcontrol_t mode, uint8_t threshold) {
if(uart == NULL) {
return false;
Expand All @@ -362,7 +362,7 @@ bool uartSetHwFlowCtrlMode(uart_t *uart, uart_hw_flowcontrol_t mode, uint8_t thr
// IDF will check (mode > HW_FLOWCTRL_CTS_RTS || threshold >= SOC_UART_FIFO_LEN)
UART_MUTEX_LOCK();
bool retCode = (ESP_OK == uart_set_hw_flow_ctrl(uart->num, mode, threshold));
UART_MUTEX_UNLOCK();
UART_MUTEX_UNLOCK();
return retCode;
}

Expand Down Expand Up @@ -422,7 +422,7 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
} else {
log_v("UART%d changed baudrate to %d", uart_nr, baudrate);
uart->_baudrate = baudrate;
}
}
}
uart_word_length_t data_bits = (config & 0xc) >> 2;
uart_parity_t parity = config & 0x3;
Expand All @@ -441,15 +441,15 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
retCode = false;
} else {
log_v("UART%d changed parity to %s", uart_nr, parity == 0 ? "NONE" : parity == 2 ? "EVEN" : "ODD");
}
}
}
if (retCode && (uart->_config & 0xc30) >> 4 != stop_bits) {
if (ESP_OK != uart_set_stop_bits(uart_nr, stop_bits)) {
log_e("UART%d changing stop bits failed.", uart_nr);
retCode = false;
} else {
log_v("UART%d changed stop bits to %d", uart_nr, stop_bits == 3 ? 2 : 1);
}
}
}
if (retCode) uart->_config = config;
if (retCode && rxPin > 0 && uart->_rxPin != rxPin) {
Expand All @@ -468,7 +468,7 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
log_e("UART%d changing TX pin failed.", uart_nr);
} else {
log_v("UART%d changed TX pin to %d", uart_nr, txPin);
}
}
}
UART_MUTEX_UNLOCK();
if (retCode) {
Expand Down Expand Up @@ -496,12 +496,12 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx

if (retCode) retCode &= ESP_OK == uart_param_config(uart_nr, &uart_config);

// Is it right or the idea is to swap rx and tx pins?
// Is it right or the idea is to swap rx and tx pins?
if (retCode && inverted) {
// invert signal for both Rx and Tx
retCode &= ESP_OK == uart_set_line_inverse(uart_nr, UART_SIGNAL_TXD_INV | UART_SIGNAL_RXD_INV);
retCode &= ESP_OK == uart_set_line_inverse(uart_nr, UART_SIGNAL_TXD_INV | UART_SIGNAL_RXD_INV);
}

if (retCode) {
uart->_baudrate = baudrate;
uart->_config = config;
Expand Down Expand Up @@ -583,7 +583,7 @@ void uartEnd(uint8_t uart_num)
}
// get UART information
uart_t* uart = &_uart_bus_array[uart_num];

UART_MUTEX_LOCK();
_uartDetachPins(uart_num, uart->_rxPin, uart->_txPin, uart->_ctsPin, uart->_rtsPin);
if(uart_is_driver_installed(uart_num)) {
Expand All @@ -598,13 +598,13 @@ void uartSetRxInvert(uart_t* uart, bool invert)
if (uart == NULL)
return;
#if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2
// POTENTIAL ISSUE :: original code only set/reset rxd_inv bit
// POTENTIAL ISSUE :: original code only set/reset rxd_inv bit
// IDF or LL set/reset the whole inv_mask!
// if (invert)
// ESP_ERROR_CHECK(uart_set_line_inverse(uart->num, UART_SIGNAL_RXD_INV));
// else
// ESP_ERROR_CHECK(uart_set_line_inverse(uart->num, UART_SIGNAL_INV_DISABLE));

#else
// this implementation is better over IDF API because it only affects RXD
// this is supported in ESP32, ESP32-S2 and ESP32-C3
Expand All @@ -613,7 +613,7 @@ void uartSetRxInvert(uart_t* uart, bool invert)
hw->conf0.rxd_inv = 1;
else
hw->conf0.rxd_inv = 0;
#endif
#endif
}


Expand All @@ -639,10 +639,10 @@ uint32_t uartAvailableForWrite(uart_t* uart)
return 0;
}
UART_MUTEX_LOCK();
uint32_t available = uart_ll_get_txfifo_len(UART_LL_GET_HW(uart->num));
uint32_t available = uart_ll_get_txfifo_len(UART_LL_GET_HW(uart->num));
size_t txRingBufferAvailable = 0;
if (ESP_OK == uart_get_tx_buffer_free_size(uart->num, &txRingBufferAvailable)) {
available += txRingBufferAvailable;
available += txRingBufferAvailable;
}
UART_MUTEX_UNLOCK();
return available;
Expand Down Expand Up @@ -671,7 +671,7 @@ size_t uartReadBytes(uart_t* uart, uint8_t *buffer, size_t size, uint32_t timeou
bytes_read += len;
}


UART_MUTEX_UNLOCK();
return bytes_read;
}
Expand Down Expand Up @@ -757,7 +757,7 @@ void uartFlushTxOnly(uart_t* uart, bool txOnly)
if(uart == NULL) {
return;
}

UART_MUTEX_LOCK();
while(!uart_ll_is_tx_idle(UART_LL_GET_HW(uart->num)));

Expand Down Expand Up @@ -850,7 +850,7 @@ bool uartSetMode(uart_t *uart, uart_mode_t mode)
{
return false;
}

UART_MUTEX_LOCK();
bool retCode = (ESP_OK == uart_set_mode(uart->num, mode));
UART_MUTEX_UNLOCK();
Expand All @@ -872,6 +872,8 @@ int uartGetDebug()
return s_uart_debug_nr;
}

void (*log_hook)(const char*message, size_t length) = 0;

int log_printfv(const char *format, va_list arg)
{
static char loc_buf[200];
Expand Down Expand Up @@ -900,6 +902,8 @@ int log_printfv(const char *format, va_list arg)
ets_printf("%s", temp);
#else
int wlen = vsnprintf(temp, len+1, format, arg);
if(log_hook)
log_hook(temp, wlen);
for (int i = 0; i < wlen; i++) {
ets_write_char_uart(temp[i]);
}
Expand Down Expand Up @@ -962,7 +966,7 @@ void log_print_buf(const uint8_t *b, size_t len){
}

/*
* if enough pulses are detected return the minimum high pulse duration + minimum low pulse duration divided by two.
* if enough pulses are detected return the minimum high pulse duration + minimum low pulse duration divided by two.
* This equals one bit period. If flag is true the function return inmediately, otherwise it waits for enough pulses.
*/
unsigned long uartBaudrateDetect(uart_t *uart, bool flg)
Expand Down Expand Up @@ -993,25 +997,25 @@ unsigned long uartBaudrateDetect(uart_t *uart, bool flg)


/*
* To start detection of baud rate with the uart the auto_baud.en bit needs to be cleared and set. The bit period is
* detected calling uartBadrateDetect(). The raw baudrate is computed using the UART_CLK_FREQ. The raw baudrate is
* To start detection of baud rate with the uart the auto_baud.en bit needs to be cleared and set. The bit period is
* detected calling uartBadrateDetect(). The raw baudrate is computed using the UART_CLK_FREQ. The raw baudrate is
* rounded to the closed real baudrate.
*
*
* ESP32-C3 reports wrong baud rate detection as shown below:
*
*
* This will help in a future recall for the C3.
* Baud Sent: Baud Read:
* 300 --> 19536
* 2400 --> 19536
* 4800 --> 19536
* 9600 --> 28818
* 4800 --> 19536
* 9600 --> 28818
* 19200 --> 57678
* 38400 --> 115440
* 57600 --> 173535
* 115200 --> 347826
* 230400 --> 701754
*
*
*
*
*/
void uartStartDetectBaudrate(uart_t *uart) {
if(uart == NULL) {
Expand All @@ -1025,10 +1029,10 @@ void uartStartDetectBaudrate(uart_t *uart) {
hw->auto_baud.en = 0;
hw->auto_baud.en = 1;
#else

// ESP32-C3 requires further testing
// Baud rate detection returns wrong values
// Baud rate detection returns wrong values

log_e("baud rate detection for this SoC is not supported.");
return;

Expand All @@ -1039,7 +1043,7 @@ void uartStartDetectBaudrate(uart_t *uart) {
//hw->conf0.autobaud_en = 1;
#endif
}

unsigned long uartDetectBaudrate(uart_t *uart)
{
if(uart == NULL) {
Expand Down Expand Up @@ -1067,7 +1071,7 @@ unsigned long uartDetectBaudrate(uart_t *uart)
uartStateDetectingBaudrate = false; // Initialize for the next round

unsigned long baudrate = getApbFrequency() / divisor;

//log_i("APB_FREQ = %d\nraw baudrate detected = %d", getApbFrequency(), baudrate);

static const unsigned long default_rates[] = {300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400};
Expand Down

0 comments on commit cfc3c0e

Please sign in to comment.