Skip to content

Core debug hardening #1826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 26, 2022
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
1 change: 0 additions & 1 deletion cores/arduino/stm32/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ void uart_deinit(serial_t *obj);
#if defined(HAL_PWR_MODULE_ENABLED) && (defined(UART_IT_WUF) || defined(LPUART1_BASE))
void uart_config_lowpower(serial_t *obj);
#endif
size_t uart_write(serial_t *obj, uint8_t data, uint16_t size);
int uart_getc(serial_t *obj, unsigned char *c);
void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t *));
void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *), size_t size);
Expand Down
79 changes: 39 additions & 40 deletions libraries/SrcWrapper/src/stm32/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,14 @@ typedef enum {
} uart_index_t;

static UART_HandleTypeDef *uart_handlers[UART_NUM] = {NULL};

static serial_t serial_debug = { .uart = NP, .index = UART_NUM };
static serial_t serial_debug = {
.uart = NP,
.pin_tx = NC,
.pin_rx = NC,
.pin_rts = NC,
.pin_cts = NC,
.index = UART_NUM
};

/* Aim of the function is to get serial_s pointer using huart pointer */
/* Highly inspired from magical linux kernel's "container_of" */
Expand Down Expand Up @@ -115,22 +121,30 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par

/* Pin Tx must not be NP */
if (uart_tx == NP) {
core_debug("ERROR: [U(S)ART] Tx pin has no peripheral!\n");
if (obj != &serial_debug) {
core_debug("ERROR: [U(S)ART] Tx pin has no peripheral!\n");
}
return;
}
/* Pin Rx must not be NP if not half-duplex */
if ((obj->pin_rx != NC) && (uart_rx == NP)) {
core_debug("ERROR: [U(S)ART] Rx pin has no peripheral!\n");
if (obj != &serial_debug) {
core_debug("ERROR: [U(S)ART] Rx pin has no peripheral!\n");
}
return;
}
/* Pin RTS must not be NP if flow control is enabled */
if ((obj->pin_rts != NC) && (uart_rts == NP)) {
core_debug("ERROR: [U(S)ART] RTS pin has no peripheral!\n");
if (obj != &serial_debug) {
core_debug("ERROR: [U(S)ART] RTS pin has no peripheral!\n");
}
return;
}
/* Pin CTS must not be NP if flow control is enabled */
if ((obj->pin_cts != NC) && (uart_cts == NP)) {
core_debug("ERROR: [U(S)ART] CTS pin has no peripheral!\n");
if (obj != &serial_debug) {
core_debug("ERROR: [U(S)ART] CTS pin has no peripheral!\n");
}
return;
}

Expand All @@ -144,7 +158,9 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
obj->uart = pinmap_merge_peripheral(obj->uart, uart_cts);

if (obj->uart == NP) {
core_debug("ERROR: [U(S)ART] Rx/Tx/RTS/CTS pins peripherals mismatch!\n");
if (obj != &serial_debug) {
core_debug("ERROR: [U(S)ART] Rx/Tx/RTS/CTS pins peripherals mismatch!\n");
}
return;
}

Expand Down Expand Up @@ -660,22 +676,6 @@ void uart_config_lowpower(serial_t *obj)
}
#endif

/**
* @brief write the data on the uart
* @param obj : pointer to serial_t structure
* @param data : byte to write
* @param size : number of data to write
* @retval The number of bytes written
*/
size_t uart_write(serial_t *obj, uint8_t data, uint16_t size)
{
if (HAL_UART_Transmit(uart_handlers[obj->index], &data, size, TX_TIMEOUT) == HAL_OK) {
return size;
} else {
return 0;
}
}

/**
* @brief Function called to initialize the debug uart interface
* @note Call only if debug U(S)ART peripheral is not already initialized
Expand All @@ -686,13 +686,12 @@ size_t uart_write(serial_t *obj, uint8_t data, uint16_t size)
void uart_debug_init(void)
{
if (DEBUG_UART != NP) {
serial_debug.pin_rx = pinmap_pin(DEBUG_UART, PinMap_UART_RX);
#if defined(DEBUG_PINNAME_TX)
serial_debug.pin_tx = DEBUG_PINNAME_TX;
#else
serial_debug.pin_tx = pinmap_pin(DEBUG_UART, PinMap_UART_TX);
#endif

/* serial_debug.pin_rx set by default to NC to configure in half duplex mode */
uart_init(&serial_debug, DEBUG_UART_BAUDRATE, UART_WORDLENGTH_8B, UART_PARITY_NONE, UART_STOPBITS_1);
}
}
Expand All @@ -706,11 +705,13 @@ void uart_debug_init(void)
size_t uart_debug_write(uint8_t *data, uint32_t size)
{
uint32_t tickstart = HAL_GetTick();
serial_t *obj = NULL;

if (DEBUG_UART == NP) {
return 0;
}
if (serial_debug.index >= UART_NUM) {
if (DEBUG_UART == NP) {
return 0;
}

/* Search if DEBUG_UART already initialized */
for (serial_debug.index = 0; serial_debug.index < UART_NUM; serial_debug.index++) {
if (uart_handlers[serial_debug.index] != NULL) {
Expand All @@ -726,24 +727,22 @@ size_t uart_debug_write(uint8_t *data, uint32_t size)
if (serial_debug.index >= UART_NUM) {
return 0;
}
} else {
serial_t *obj = get_serial_obj(uart_handlers[serial_debug.index]);
if (obj) {
serial_debug.irq = obj->irq;
}
}
}
obj = get_serial_obj(uart_handlers[serial_debug.index]);
if (!obj) {
return 0;
}

HAL_NVIC_DisableIRQ(serial_debug.irq);

while (HAL_UART_Transmit(uart_handlers[serial_debug.index], data, size, TX_TIMEOUT) != HAL_OK) {
if ((HAL_GetTick() - tickstart) >= TX_TIMEOUT) {
size = 0;
break;
while (serial_tx_active(obj)) {
if ((HAL_GetTick() - tickstart) >= TX_TIMEOUT) {
return 0;
}
}

HAL_NVIC_EnableIRQ(serial_debug.irq);
if (HAL_UART_Transmit(&(obj->handle), data, size, TX_TIMEOUT) != HAL_OK) {
size = 0;
}

return size;
}
Expand Down
2 changes: 0 additions & 2 deletions libraries/SrcWrapper/src/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
#undef errno
extern int errno;

extern size_t uart_debug_write(uint8_t *data, uint32_t size);

// Helper macro to mark unused parameters and prevent compiler warnings.
// Appends _UNUSED to the variable name to prevent accidentally using them.
#ifdef UNUSED
Expand Down