cancel
Showing results for 
Search instead for 
Did you mean: 

Configure AF GPIO register using 11U STM32F446RE

StanCosgrove
Associate II

Hi, i came across a program to config alternation function for SPI GPIO port A. 

GPIOA->CRL |= (11U<<20);       // PA5 SCK AF output push pull

GPIOA->CRL |= (11U<<28);       // PA7 MOSI AF output push pull

What does the 11U means? i know its something like setting the 20th bit to 11? is 11 the binary value for 3? how does the value 3 relates to PA5 SCK and 28 relates to PA7 MOSI ?  Anyone can advise?

1 ACCEPTED SOLUTION

Accepted Solutions

As others explained, 11U is decimal 11. Without the U suffix, it would be treated as signed, and shifting it so that the result is higher than 2^31-1 (the highest non-negative int32_t) is undefined by standard.

11 decimal is 1011 binary, so if GPIOA->CRL |= (11U<<20); sets the following bits:

waclawekjan_1-1702746880448.png

other bits remain unaffected. MODE5 will be 0b11, so Output Mode, 50MHz; if the register was zeroed previously, CNF5 will be 0b10 i.e. Alternate Push Pull; if it was at its reset state, then CNF5 will be 0b11 i.e. Alternate Open-drain.

JW

View solution in original post

12 REPLIES 12
TDK
Guru

11U is an unsigned integer literal, as opposed to an integer literal.

It doesn't do much here, but in some cases where you use all 32 bits you have to make it unsigned. For instance, you can't write 0x80000000 as an int32_t, but you can as a uint32_t.

 

You may also see things like "1234ULL" which would mean it's a (long long unsigned) literal, which can be required if a normal unsigned integer isn't large enough to fit the value.

 

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

What does the 11U means? i know its something like setting the 20th bit to 11? is 11 the binary value for 3?


No, that is a decimal 11. Learn the C programming language:

https://en.cppreference.com/w/c/language/integer_constant

 

how does the value 3 relates to PA5 SCK and 28 relates to PA7 MOSI ?  Anyone can advise?


Reference manual documents all of the registers, bits and their values.

As others explained, 11U is decimal 11. Without the U suffix, it would be treated as signed, and shifting it so that the result is higher than 2^31-1 (the highest non-negative int32_t) is undefined by standard.

11 decimal is 1011 binary, so if GPIOA->CRL |= (11U<<20); sets the following bits:

waclawekjan_1-1702746880448.png

other bits remain unaffected. MODE5 will be 0b11, so Output Mode, 50MHz; if the register was zeroed previously, CNF5 will be 0b10 i.e. Alternate Push Pull; if it was at its reset state, then CNF5 will be 0b11 i.e. Alternate Open-drain.

JW

StanCosgrove
Associate II

thanks JW, could you kindly advise the datasheet number RM0.... couldnt see clearly, thx...

i  m reading RM0390 and i guess its not the right one....

Click the image to view the full size. RM0008.

GPIO registers for all STM32F4 chips are the same.

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

Which STM32 do you use?

Only STM32F1xx have GPIOx_CRL registers.

JW

The title says F4 (STM32F446RE), the tag/label is for H7 and the code is from F1 series. How are you capable of navigating the internet to find anything? Oh, wait...

thanks JW, could you kindly advise the datasheet number RM0.... couldnt see clearly, thx...


Hi JW,  thanks for the detailed explanation, i can rationalize with you till 50MHz.... but i dont understand the following : 

"if the register was zeroed previously, CNF5 will be 0b10 i.e. Alternate Push Pull; if it was at its reset state, then CNF5 will be 0b11 i.e. Alternate Open-drain."

why is there 2 possibilities when the instruction 11 decimal --> 1011b, shouldnt CNF5 be only 0b10 ? where does the 0b11 at reset state come from? Pls advise...