2025-05-13 7:34 PM
Hello.. i m programming F446 using registers, and i found from F446 reference manual to configure PA1 as both input and pull-down mode. i don't understand , can anyone advise how could
1. MODER &= ~(3<<2) is to set MODER1 as 00 input?
2. PUPDR |= (1<<3); is to set as 10 pull-down mode?? isn't that mean bit 3 set to '1' ??
GPIOA->MODER &= ~(3<<2); // 7.4.1 Bits (3:2) = 0:0 --> PA1 in Input Mode
GPIOA->PUPDR |= (1<<3); // 7.4.4 Bits (3:2) = 1:0 --> PA1 is in Pull Down mode
}
2025-05-14 7:56 AM
Great !
2025-05-14 8:48 AM
For register level interactions it's probably inadvisable to a) assume current bit states, b) used repetitive RMW action on the same 'volatile' memory space, the compiler can't fold the lines.
Better to use the form, which does both things in a SINGLE LOAD-STORE interaction
GPIOA>MODER = (GPIOA>MODER ^ ~bits_to_clear) | bits_to_set;
Doing half-a-dozen load/store is NOT efficient, using subroutines and libraries, especially when defined INLINE, can result in cleaner, manageable and more efficient code.
The (1 << 3) is assuming bit 2 is zero, which it might be at reset, but it's better to be explicit about what you want, that way the next person to use your code knows what the intent was, and if you copy-n-paste to another section or reuse it later for this, or a different pin, that the expectation continue to be true.
2025-05-14 6:14 PM
Doing half-a-dozen load/store is NOT efficient, using subroutines and libraries, especially when defined INLINE, can result in cleaner, manageable and more efficient code.
sound great, could you provide an example?
2025-05-15 1:03 AM
@HMSEdinburge wrote:Doing half-a-dozen load/store is NOT efficient, using subroutines and libraries, especially when defined INLINE, can result in cleaner, manageable and more efficient code.
sound great, could you provide an example?
I don't fully agree that this is inefficient. If it is done only during initialization it won't effect program performance. Unless you want short boot times. But a few extra instructions won't be a problem.
If you cannot make any assumptions about the value in the register, then read-modify-write is the only way to make sure you only edit the bits you want. Reading once and writing once is enough as @Tesla DeLorean wrote in his code example.
Even faster would be not to read at all. Simply assign the pin configurations of all pins in that register at once, then there is no need to read the register. But then you need to do all pin configuration in one place.