cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F411 - common short questions ...

timbo2023
Associate III

Good Evening Ladies and Gentlemen,

 

A short introducing purpose of mine:

 

int main(void)
{
RCC->AHB1ENR |= 0xFC;             // ---> Here I would like to open the Port clock
RCC->APB1ENR |= 0xFC;             // ---> Here I would like to open the USART2 clock - in this context not relevant

GPIOA->MODER |= 0x55555555;       // ---> Here I define Port A as output
GPIOC->MODER |= 0x55555555;       // ---> Here I define Port C as output

GPIOA->ODR |= (1<<2);             // ---> Here I would like to set PA2 "high"

GPIOC->ODR |= (1<<13);            // ---> Here I would like to set the onboard LED (PC13) "high"

while(1) {
GPIOA->ODR |= (1<<0);             // Here I would like to "toggle" PA0 and PA6  
GPIOA->ODR |= (1<<6);
delayMs(50);                      // It only is a not relevant "delay" for this purpose

GPIOA->ODR &= ~(1<<0);
GPIOA->ODR &= ~(1<<6);
delayMs(50);
}
}

 

 

But something went wrong - it does not work - Do you have any suggestions especially regarding a simple "toggling"?

It should be a pre work for a stepper motor controlling.

 

Best regards

~TIMBO~

35 REPLIES 35
Uwe Bonnes
Principal III

Do not use magic defines. GPIOA and USART2 clock enables are defined different!

 

#define RCC_AHB1ENR_GPIOAEN_Msk (0x1U << RCC_AHB1ENR_GPIOAEN_Pos) /*!< 0x00000001 */

#define RCC_APB1ENR_USART2EN_Msk (0x1U << RCC_APB1ENR_USART2EN_Pos) /*!< 0x00020000 */

timbo2023
Associate III

Firstly thank you for your input and your comment regarding a clock-definition at the code begin.

This I will consider.

In spite of it I am a little bit unsure using your possibilities for enable the clocks in a right (code-) way.

Is there a shorter form for code writing of enabling ports A, C und farer USART2-clock?

How I work with defines of clock addresses at the code begin?

 

Could you give there a more detailled example ...

TDK
Guru

Debug and step through, verify GPIO registers are getting updated as specified. The IDR value will reflect the current value being output. If ODR and IDR are changing, the pin is toggling, and the problem is elsewhere.

+1 about not using magic values. Use the CMSIS defines.

RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;

is a lot more readable than

> RCC->AHB1ENR |= 0xFC;

If you feel a post has answered your question, please click "Accept as Solution".

But:

Is the approach in your eyes the same?

In look to my wished writing

Another way:

RCC->AHB1ENR |= (1<<0)|(1<<2);  // enable A and C port
RCC->APB1ENR |= (1<<17); // enable USART2

 

Is that quiet ok, too?

And in addition - it would be supportive when you could comment the "toggling" shortly...

Yes, that will work as well. But if there's a typo it's going to be hard to spot and debug. Whereas if there's a typo in RCC_AHB1ENR_GPIOAEN then it won't compile and you will know immediately.

> And in addition - it would be supportive when you could comment the "toggling" shortly...

Not sure what you mean here. Do you know how to debug, step through and examine registers? The SFRs window in STM32CubeIDE will show register values.

If you feel a post has answered your question, please click "Accept as Solution".
gbm
Lead III

The next problem reported by the author of this thread will be "MCU not found while trying to download the code".

Do NOT set PA13 & PA14 as GPIO outputs! They are used for SWD debug connection to ST-Link.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
timbo2023
Associate III

That's a very interesting information, gbm - i will check the reference manual regarding it.

Ho can I fix that in HEX-writing?

timbo2023
Associate III

Ich changed the mode register to

 

 

GPIOA->MODER |= (1<<0)|(1<<4)|(1<<8);

 

Could you check it?