Skip to main content
Graduate II
October 16, 2024
Solved

IO port always readuing 0v in release mode.

  • October 16, 2024
  • 3 replies
  • 1599 views

Hi

  I am using a STM32L433 and have a digital input pin, which is checked on bootup and enables the USB if 3.3v is in the pin.  When working in debug mode, it always works, but when I switch to release mode , using stm32CubeIDE , it always reads 0v, and my optimization is None. Below is my code .

 

 

RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN; //Enable Port B clock

GPIOB->MODER &= ~GPIO_MODER_MODE12; //PB12 as input, check for usb interaction
inSetupMode=(GPIOB->IDR >> 12 )& 1; //=1 when using USB


 

 

Can anyone let me know what the issue is?

 

Many thanks

 

Scott

    This topic has been closed for replies.
    Best answer by Tesla DeLorean

    After the write

    See how HAL does the clock enables, reads back to insure writes complete first, as forces IN-ORDER-COMPLETION for the write buffers and pipeline, and adds perhaps 4 bus cycles to the transaction.

    #define __HAL_RCC_GPIOB_CLK_ENABLE() do { \
     __IO uint32_t tmpreg; \
     SET_BIT(RCC->IOPENR, RCC_IOPENR_GPIOBEN); \
     /* Delay after an RCC peripheral clock enabling */ \
     tmpreg = READ_BIT(RCC->IOPENR, RCC_IOPENR_GPIOBEN); \
     UNUSED(tmpreg); \
     } while(0U)

     

    3 replies

    Graduate II
    October 16, 2024

    Hard to know.

    Enable the clocks early. There is a hazard enabling the clock and then immediately writing the peripheral registers.

    Read back the RCC->AHB2ENR  /  _DSB()

    SSmit.13Author
    Graduate II
    October 16, 2024

    Hi ,

    what is _DSB?

     

    What do I check ? Is it if the B clock is enabled?

     

    And if its not what do I do if its not enabled?

     

    Sorry for all the questions.

     

    Scott

     

    Graduate
    October 16, 2024

    Not a check, just small delay. Also, I would suggest adding some small delay between MODER setting and reading the IDR.

    SSmit.13Author
    Graduate II
    October 16, 2024

    Many thanks for the reply. I will add a delay.

     

    Thanks

    Scott

     

    SSmit.13Author
    Graduate II
    October 16, 2024

    I added the line:

    __DSB();

    after the clocks being enabled, but never made a difference.

     

    But when I added a 2ms delay after GPIOB->MODE and this worked.

    Where do I add the DSB command, and is it just the DSB() on its own?

    CHeers

    Scott

     

    Graduate II
    October 16, 2024

    After the write

    See how HAL does the clock enables, reads back to insure writes complete first, as forces IN-ORDER-COMPLETION for the write buffers and pipeline, and adds perhaps 4 bus cycles to the transaction.

    #define __HAL_RCC_GPIOB_CLK_ENABLE() do { \
     __IO uint32_t tmpreg; \
     SET_BIT(RCC->IOPENR, RCC_IOPENR_GPIOBEN); \
     /* Delay after an RCC peripheral clock enabling */ \
     tmpreg = READ_BIT(RCC->IOPENR, RCC_IOPENR_GPIOBEN); \
     UNUSED(tmpreg); \
     } while(0U)