cancel
Showing results for 
Search instead for 
Did you mean: 

Could someone write how to do GPIO without HAL?

LMI2
Lead

I saw somewhere a very basic examples how to do basic GPIO. But now I can't find it. Most of my bookmarks for this forum do not work anyway, all though they not are so old.

But could some one write examples of how to write or read one pin or several at once. And toggling of course.

Edit: It probably doesn't matter, but I'm currently using STM32F746 and H750

8 REPLIES 8

GPIOD->ODR is the output register, bit 0 PD0, bit 15 PD15

GPIOD->BSRR is the set/reset register, single write equivalent of RMW on ODR

GPIOD->BSRR = (1 << 4) | (1 << 😎 | (1 << (0 + 16)); // Set PD4, PD8, Clear PD0

One of the other guys had a safe toggle macro

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
LMI2
Lead

Ah, That was quick, thank you.

Is the reset always like this, 1 << (0 + 16)?

Well there's the Reference Manual if you want to understand the mechanics.

The GPIO bank has 16 pins, for the set/reset register the low order 16-bits SET the pin HIGH, the high order 16-bits CLEAR the pin LOW

You can write the whole 32-bit in a single write, so it is faster/safer than doing the equivalent RMW on the ODR

Toggle ALL PINS would look something like

uint32_t tmp = GPIOD->ODR;

GPIOD->BSRR = (tmp << 16) | (~tmp & 0xFFFF);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
LMI2
Lead

I have to check the manual. It may be even been the manual I saw these. Google didn't find anything more about this.

So, upper bits set and lower bits reset, what happens if I set and reset a pin. A short?

Nikita91
Lead II

If both bits are set the reference manual say (search for GPIOx_BSRR):

If both BSx and BRx are set, BSx has priority.

gregstm
Senior III

The BSRR is great. The reference manual has all this info. Open it up and search for the term "BSRR" and read carefully everywhere it occurs.

LMI2
Lead

I searched from a couple of Reference manuals and they didn't have any code examples. In my opinion there is no good place on the net where input, out and toggling are given well and all in one place

.

You are looking for it at the wrong place. Bitwise operations, i.e. how to set a certain bit, are explained in C textbooks.