2023-12-15 06:32 AM - last edited on 2023-12-20 04:46 AM by Amel NASRI
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?
Solved! Go to Solution.
2023-12-16 09:16 AM
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:
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
2023-12-15 07:45 AM - edited 2023-12-15 07:47 AM
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.
2023-12-15 06:12 PM - edited 2023-12-15 06:15 PM
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.
2023-12-16 09:16 AM
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:
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
2023-12-16 04:18 PM
thanks JW, could you kindly advise the datasheet number RM0.... couldnt see clearly, thx...
2023-12-16 04:21 PM
i m reading RM0390 and i guess its not the right one....
2023-12-16 04:31 PM
Click the image to view the full size. RM0008.
GPIO registers for all STM32F4 chips are the same.
2023-12-16 11:36 PM
Which STM32 do you use?
Only STM32F1xx have GPIOx_CRL registers.
JW
2023-12-17 10:39 AM
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...
2023-12-18 05:59 AM
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...