cancel
Showing results for 
Search instead for 
Did you mean: 

Nucleo Board Led I'm not able to activate, but register is showing high

uli stone
Associate II
Posted on August 16, 2017 at 13:53

Hi,

I'm not able to activate the led (green, LD2, PA5) in stepping mode. But the corrosponding register shows that it is activated.

&sharpinclude 'stm32f4xx_rcc.h'

&sharpinclude 'stm32f4xx_gpio.h'

int main()

{

    RCC->AHB1ENR |= RCC_AHB1Periph_GPIOA;

    GPIOA->MODER |= GPIO_Mode_OUT;

    GPIOA->OSPEEDR |= GPIO_Speed_25MHz;

    GPIOA->OTYPER |= GPIO_OType_PP;

    GPIOA->PUPDR |= GPIO_PuPd_NOPULL;

    while (1)

    {

        GPIOA->BSRRL = GPIO_Pin_5;

        GPIOA->BSRRH = GPIO_Pin_5;

    }

}

What is wrong?

Thank you

#stm32f4 #stm32 #register #nucleo
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on August 16, 2017 at 19:03

>>Why is it with RST not working? Do I need a Time Delay? I wanted to RST to make sure everything is 0.

Because you're holding the device in RESET by writing ONE, you must write ONE, then write ZERO into the bit field

You likely don't want to nuke the registers, for one thing it might result in the loss of debugger connectivity on GPIOA, but more specifically you might be undoing work done by SystemInit() etc in setting up external buses and pin setting that occur prior to execution of main() under the CMSIS model.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

9 REPLIES 9
Posted on August 16, 2017 at 14:37

What is wrong?

Nearly everything

You are mixing CMSIS style programming with SPL style. If you want to operate on registers instead of using a library like SPL (or HAL, or LL) then you should only use the definitions provided by stm32f405xx.h (for example) CMSIS header file.

1. RCC->AHB1ENR |= RCC_AHB1Periph_GPIOA; becomes RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;

2. GPIOA->MODER |= GPIO_Mode_OUT; becomes, for example, GPIOA->MODER |= GPIO_MODER_MODE5_0;

3. Other registers also have to be changed accordingly

Read carefully the reference manual of your MCU, and find the correct definitions in the CMSIS header file of your MCU.

Posted on August 16, 2017 at 15:01

If you don't understand register level programming consider using the library code until you do.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
uli stone
Associate II
Posted on August 16, 2017 at 16:05

Ok,

sorry for the code above, it is not mine (copied from the internet).

Mine looks like this and doesn't work as well. And I can't understand why... the green LED LD2 is working on the board.

#include 'stm32f4xx.h'

void SystemClock_Config(void);                                                       //function prototype for SystemClock configuration

void GPIO_Config(void);                                         //function frototype for GeneralPurposeInputOutput configuration

int main(void)

{

    //initialization

    SystemClock_Config();

    GPIO_Config();

    int n;

    while(1)

    {

    GPIOA->ODR |= 0x20;

    //for(n=0; n<5; n++);

    GPIOA->ODR &= ~0x20;

    //for(n=0; n<5; n++);

    }

}

//SystemClock configuration

void SystemClock_Config(void)

{

    RCC->CR |= RCC_CR_CSSON | RCC_CR_HSEBYP | RCC_CR_HSEON;                                                         //clock security system enabled, HSE Enable Bypass and HSE Enable

    //HSE Clock

    while (!(RCC->CR & RCC_CR_HSERDY));                                                            //wait till HSE oscillator is stable

    //FLASH Configuration

    FLASH->ACR |= FLASH_ACR_PRFTEN | FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_3WS;                        //FLASH Prefetch, Instruction, Data Cache enable;

                                                                                                                    //FLASH wait states 3 !change to higher value, if problems occur

    //PLL configuration and activation

    RCC->PLLCFGR |= RCC_PLLCFGR_PLLSRC_HSE | RCC_PLLCFGR_PLLM_4 |  0x00000078 | RCC_PLLCFGR_PLLP_0;

    RCC->CR |= RCC_CR_PLLON;                                                                                        //activate PLL

    while(!(RCC->CR & RCC_CR_PLLRDY));                                                                                //wait till PLL is stable

    //Sysclk activation

    RCC->CFGR |= RCC_CFGR_HPRE_DIV1;

    RCC->CFGR |= RCC_CFGR_SW_PLL;

    while (!(RCC->CFGR & RCC_CFGR_SWS_PLL));

    //PPRE1 and PPRE2 Prescaler

    RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;                                                                                //HCLK divided by 4

    RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;                                                                                //HCLK divided by 2

}

//GeneralPurposeInputOutput configuration

void GPIO_Config(void)

{

    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;                    //enable AHB1 GPIO->A and  GPIO->C clock

    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;

    RCC->AHB1RSTR |= RCC_AHB1RSTR_GPIOARST | RCC_AHB1RSTR_GPIOCRST;

    GPIOA->MODER |= GPIO_MODER_MODER5_0;

}

Whats wrong with mine?

Julien FAUCHER
Associate III
Posted on August 16, 2017 at 17:18

You should also mind that you are blinking the LED at the maximum speed of the MCU (several MHz) so you won't see anything blink.

You should use something like :

    int i;

    while(1)

    {

         GPIOA->ODR ^= (1 << 5); //Toogle the 5th pin

         for(i = 0; i < 500000; i++)

                  ;

    }

And increase/decrease the maximum value for 'i' to see it blink  

uli stone
Associate II
Posted on August 16, 2017 at 17:31

Hi Clive,

now it is working. The problem is as you mentioned:

RCC->AHB1RSTR |= RCC_AHB1RSTR_GPIOARST | RCC_AHB1RSTR_GPIOCRST;

Everything stayed as I posted it.

Why is it with RST not working? Do I need a Time Delay?

I wanted to RST to make sure everything is 0.

Yes I know that I turn on and of at max speed when it is executed in the loop. But I did it step-by-step, so it should have light up.

Thanks

Posted on August 16, 2017 at 16:46

Wouldn't do this

RCC->AHB1RSTR |= RCC_AHB1RSTR_GPIOARST | RCC_AHB1RSTR_GPIOCRST;

Use a debugger. Step through the code, review the peripheral level setting/registers. Make sure you are setting/clearing the correct bits in the registers for the specific pin being used.

Using what board?

 GPIOA->MODER = (GPIOA->MODER & ~(3 << (5 << 1))) | (1 << (5 << 1)); // Pin 5 Output

GPIOA->TYPER &= ~(1 << 5); // Pin 5 Push-Pull

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on August 16, 2017 at 18:56

And make it volatile...

The code is being stepped in a debugger, I don't think this is the problem. Operating at 0.0001 Hz

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
uli stone
Associate II
Posted on August 16, 2017 at 21:00

Thank you clive for the update!

Posted on August 16, 2017 at 19:03

>>Why is it with RST not working? Do I need a Time Delay? I wanted to RST to make sure everything is 0.

Because you're holding the device in RESET by writing ONE, you must write ONE, then write ZERO into the bit field

You likely don't want to nuke the registers, for one thing it might result in the loss of debugger connectivity on GPIOA, but more specifically you might be undoing work done by SystemInit() etc in setting up external buses and pin setting that occur prior to execution of main() under the CMSIS model.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..