Skip to content

Pin manipulation lock board when number is equal/above NUM_DIGITAL_PINS #2612

Closed
@VitorBoss

Description

@VitorBoss
  • OS: Win10 22H2
  • vsCode + PlatformIO
  • STM32 core version: v2.8.1
  • Board: Black F407VE

Additional context
I have a quite complex code in hands, since the core don't protect used peripherals(like USB) pins I had to code my own.

I have pins that can be reassign, and need to check is it isn't USB/SPI/UART, when I detect a conflict I set the pin to NUM_DIGITAL_PINS but this is causing the CPU to lock, and the weird thing is, this don't happen while debugging!

After entering the rabbit hole of stepping the code via STM32CubeProgrammer I found the problem is in the digitalRead, since it don't check if the pin number is valid it causes an exception when the get_GPIO_Port(STM_PORT(pn) part returns a NULL and the LL_GPIO_IsInputPinSet tries to read a NULL offset.

The code also uses EEPROM emulation using internal flash, even when the NULL pointer access don't lock the CPU it causes the flash to stop writing.

The fix is pretty simple, I have tested it right now. Just add a check on the digital manipulation on wiring_digital.c like this

void digitalWrite(uint32_t ulPin, uint32_t ulVal)
{
  PinName p = digitalPinToPinName(ulPin);

  if (p != NC) {
    digitalWriteFast(p, ulVal);
  }
}

This is how pinMode do it, and the Teensy core also do this check.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions