Skip to main content
Associate III
August 26, 2023
Question

Bitwise shift left STM32F723

  • August 26, 2023
  • 8 replies
  • 5346 views

Hi there i m a student working on STM32F723 the first time, I encountered an register setting example as follows:

GPIOC->MODER |= ((2<<(6*2)) \ (2<<(7*2)));  i konw its setting the GPIO register but whats the meaning of * and 2<< at the beginning sentence? and what does bit does it set?

GPIOC-> AFR[0] |= ((8<<(6*4)) |  (8<<(7*4))); whats the meaning of 8 and 4 means? and whats bits it set?

thanks in advance.

Stanley

This topic has been closed for replies.

8 replies

Johi
Senior II
August 26, 2023

 

Hello,

This is related to the syntax of the C language.

https://www.geeksforgeeks.org/left-shift-right-shift-operators-c-cpp/ 

8 is a representation of 1000 binary. And << shifts the bits left. (6*4) = 24.

You will find the details in a good C course; knowledge of C is essential if you want to work with complex devices as STM32 microcontrollers.

StanJermAuthor
Associate III
August 26, 2023

wouldn't it be more simple to write this:

GPIOC->MODER |= (1<<6) | (1 << 7);  ??

waclawek.jan
Super User
August 27, 2023

(1 << 6) is not the same as (2 << (2 * 6)). Try it with calculator, or better, with pencil and paper.

JW

Tesla DeLorean
Guru
August 26, 2023

The *2 is the bit width of the MODE option, the 6 is the pin number or index, the 2 is the MODE for the pin.

The code will be folded and optimized by the compiler, it's done verbosely so you can see how it was arrived at without some unexplained constant.

The AFR options are 4 bits wide taking a value between 0 and 15, determining what functional connection is mapped to a pin.

The 8 is AF8, the part Data Sheet/Manual should have a list of tables showing what peripheral maps at that slot.

 

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
StanJermAuthor
Associate III
August 26, 2023

thx u so much ! 

Pavel A.
August 27, 2023

There's a lot of junk code written by ..er... budding electronic engineers. If you don't understand it and think it could be clearer/laid out better, fix it (but test your understanding first!). Don't skip your C lectures ))

Tesla DeLorean
Guru
August 27, 2023

Yes, and ORing them on does make an assumption that the current bits are zero.

Coding like this does require a full awareness of the register definitions in the Reference Manual

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
waclawek.jan
Super User
August 27, 2023

> Coding like this does require a full awareness of the register definitions in the Reference Manual

Yes, but everybody reads the RM thoroughly before them starting programming, right?

Right?

JW