Skip to content

[timeout, stm32wb, pic32cz] Implement an optional timeout and add it to current drivers#16

Open
AlexLanzano wants to merge 1 commit intowolfSSL:mainfrom
AlexLanzano:timeouts
Open

[timeout, stm32wb, pic32cz] Implement an optional timeout and add it to current drivers#16
AlexLanzano wants to merge 1 commit intowolfSSL:mainfrom
AlexLanzano:timeouts

Conversation

@AlexLanzano
Copy link
Member

No description provided.

@AlexLanzano AlexLanzano self-assigned this Mar 17, 2026
Copilot AI review requested due to automatic review settings March 17, 2026 16:02
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a lightweight timeout abstraction to bound polling loops, integrates it into several STM32WB and PIC32CZ drivers, and wires it into the STM32WB Nucleo board plus core tests.

Changes:

  • Add whal_Timeout + WHAL_TIMEOUT_* macros (with optional compile-out) and a new WHAL_ETIMEOUT error code.
  • Extend multiple driver config structs to accept an optional timeout pointer and apply timeouts to busy-wait/polling loops.
  • Add core unit tests for timeout behavior and configure an example tick source + timeout instance on the STM32WB55 Nucleo board.

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 18 comments.

Show a summary per file
File Description
wolfHAL/wolfHAL.h Exposes the new timeout API from the umbrella header.
wolfHAL/uart/stm32wb_uart.h Adds optional timeout pointer to STM32WB UART config.
wolfHAL/uart/pic32cz_uart.h Adds optional timeout pointer to PIC32CZ UART config.
wolfHAL/timeout.h Introduces the timeout abstraction and macros.
wolfHAL/spi/stm32wb_spi.h Adds optional timeout pointer to STM32WB SPI config.
wolfHAL/rng/stm32wb_rng.h Adds optional timeout pointer to STM32WB RNG config.
wolfHAL/flash/stm32wb_flash.h Adds optional timeout pointer to STM32WB Flash config.
wolfHAL/flash/pic32cz_flash.h Adds optional timeout pointer to PIC32CZ Flash config.
wolfHAL/error.h Adds WHAL_ETIMEOUT error code.
wolfHAL/crypto/stm32wb_aes.h Adds optional timeout pointer to STM32WB AES config.
tests/core/test_timeout.c Adds unit tests for timeout start/stop/expired and wrap behavior.
tests/core/main.c Registers/runs the new timeout test suite.
tests/core/Makefile Includes test_timeout.c in the core test build.
src/uart/stm32wb_uart.c Applies timeout guards to STM32WB UART send/recv polling.
src/uart/pic32cz_uart.c Adds timeout-bounded sync waits and TX/RX polling with timeout errors.
src/spi/stm32wb_spi.c Adds timeout-bounded polling for TXE/RXNE/BSY in SPI transfers.
src/rng/stm32wb_rng.c Adds timeout-bounded wait for RNG data-ready.
src/flash/stm32wb_flash.c Adds timeout-bounded waits for flash CFGBSY during program/erase.
src/flash/pic32cz_flash.c Adds timeout-bounded mutex/busy waits and propagates timeout errors.
src/crypto/stm32wb_aes.c Adds timeout-bounded polling for AES completion across modes.
boards/stm32wb55xx_nucleo/board.h Updates tick type to uint64_t for the new timeout tick source.
boards/stm32wb55xx_nucleo/board.c Implements Board_GetTick(), defines a default timeout instance, and injects it into device configs.
Comments suppressed due to low confidence (4)

src/flash/pic32cz_flash.c:363

  • cfg is read from flashDev before checking flashDev for NULL, which can dereference a NULL pointer. Move the cfg assignment after the if (!flashDev) validation (and consider also validating flashDev->cfg).
whal_Error whal_Pic32czFlash_Erase(whal_Flash *flashDev, size_t addr, size_t dataSz)
{
    const whal_Regmap *reg;
    whal_Pic32czFlash_Cfg *cfg = flashDev->cfg;
    whal_Error err;
    size_t pageAddr;
    size_t endAddr;

    if (!flashDev) {
        return WHAL_EINVAL;
    }

src/flash/pic32cz_flash.c:256

  • cfg is read from flashDev before validating flashDev is non-NULL, which can dereference a NULL pointer when callers pass flashDev == NULL. Move whal_Pic32czFlash_Cfg *cfg = flashDev->cfg; to after the if (!flashDev || !data) check (and similarly in the other APIs in this file).
    const whal_Regmap *reg;
    whal_Pic32czFlash_Cfg *cfg = flashDev->cfg;
    uint8_t *flashAddr = (uint8_t *)addr;
    whal_Error err;
    size_t i;

    if (!flashDev || !data) {
        return WHAL_EINVAL;
    }

src/flash/pic32cz_flash.c:287

  • cfg is read from flashDev before the if (!flashDev || !data) guard, which can dereference NULL when flashDev is NULL. Assign cfg only after validating flashDev (and flashDev->cfg if required).
                              size_t dataSz)
{
    const whal_Regmap *reg;
    whal_Pic32czFlash_Cfg *cfg = flashDev->cfg;
    const uint32_t *src;
    whal_Error err;
    size_t offset = 0;

    if (!flashDev || !data) {
        return WHAL_EINVAL;
    }

boards/stm32wb55xx_nucleo/board.c:188

  • Board_WaitMs() still uses size_t locals (startCount, currentCount) even though g_tick is now uint64_t. This truncates the tick value on 32-bit builds and makes the wrap/overflow handling (g_tickOverflow, SIZE_MAX) inconsistent with the actual counter type. Update the delay logic to use uint64_t consistently (and drop the overflow flag if 64-bit wrap is no longer a practical concern).
void Board_WaitMs(size_t ms)
{
    size_t startCount = g_tick;
    g_waiting = 1;
    while (1) {
        size_t currentCount = g_tick;
        if (g_tickOverflow) {
            if ((SIZE_MAX - startCount) + currentCount > ms) {
                break;
            }
        } else if (currentCount - startCount > ms) {
            break;
        }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +241 to +245
WHAL_TIMEOUT_START(cfg->timeout);
while (!(whal_Reg_Read(base, AES_SR_REG) & AES_SR_CCF_Msk)) {
if (WHAL_TIMEOUT_EXPIRED(cfg->timeout))
return WHAL_ETIMEOUT;
}
Comment on lines +868 to +872
WHAL_TIMEOUT_START(cfg->timeout);
while (!(whal_Reg_Read(base, AES_SR_REG) & AES_SR_CCF_Msk)) {
if (WHAL_TIMEOUT_EXPIRED(cfg->timeout))
return WHAL_ETIMEOUT;
}
Comment on lines 84 to +91
if (!rngDev || !rngData) {
return WHAL_EINVAL;
}

whal_Error err = WHAL_SUCCESS;
whal_Stm32wbRng_Cfg *cfg = (whal_Stm32wbRng_Cfg *)rngDev->cfg;
const whal_Regmap *reg = &rngDev->regmap;
size_t status;
size_t sr;
Comment on lines 205 to 209
if (args->sz == 0 || (args->sz & 0xF) != 0)
return WHAL_EINVAL;

cfg = (const whal_Stm32wbAes_Cfg *)cryptoDev->cfg;
base = cryptoDev->regmap.base;
Comment on lines 427 to 431
if (args->tagSz == 0 || args->tagSz > 16)
return WHAL_EINVAL;

cfg = (const whal_Stm32wbAes_Cfg *)cryptoDev->cfg;
base = cryptoDev->regmap.base;
Comment on lines 347 to 351
if (args->sz == 0 || (args->sz & 0xF) != 0)
return WHAL_EINVAL;

cfg = (const whal_Stm32wbAes_Cfg *)cryptoDev->cfg;
base = cryptoDev->regmap.base;
Comment on lines +382 to +386
WHAL_TIMEOUT_START(cfg->timeout);
while (!(whal_Reg_Read(base, AES_SR_REG) & AES_SR_CCF_Msk)) {
if (WHAL_TIMEOUT_EXPIRED(cfg->timeout))
return WHAL_ETIMEOUT;
}
Comment on lines +654 to +658
WHAL_TIMEOUT_START(cfg->timeout);
while (!(whal_Reg_Read(base, AES_SR_REG) & AES_SR_CCF_Msk)) {
if (WHAL_TIMEOUT_EXPIRED(cfg->timeout))
return WHAL_ETIMEOUT;
}

uint64_t Board_GetTick()
{
return g_tick;
Comment on lines 220 to +224
/* Wait for CTRLB sync */
whal_Pic32czUart_WaitSync(reg, USART_SYNCBUSY_CTRLB_Msk, USART_SYNCBUSY_CTRLB_Pos);
err = whal_Pic32czUart_WaitSync(reg, USART_SYNCBUSY_CTRLB_Msk,
USART_SYNCBUSY_CTRLB_Pos, cfg->timeout);
if (err != WHAL_SUCCESS)
return err;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants