I am using STM32F103, CUBE IDE with HAL library. I am trying to write the outport as giving a 4 bit value to the entire port (16 bit) by using a shift bit operator. Can I directly apply that data to the port as follows. GPIO_PORTA |= 0x0ff; Like wise Or how to write it?
If you want the whole port (all 16 pins) get the value, then you simply write it to the ODR register:
GPIOB->ODR = 0xFFFF; //writing 1/0 in a bit position, sets/resets that bit. ALL BITS ARE AFFECTED no matter the value your write in.
If you want to set some pins, but not altering the other pins, with one instruction:
GPIOB->BSRR = 0x00F; //writing 1 in a bit position, sets that bit leaves the others as they are
If you want to reset some pins, but not altering the other pins, with one instruction:
GPIOB->BRR = 0x00F; //writing 1 in a bit position, clears that bit and leaves the others as they are
If you want a "portion" of a GPIO port to get an specific value, this method is 2 instructions instead of "read-change-write":
GPIOB->BRR = 0x00F; //clear the first 4 bits GPIOB->BSRR = 0x00A; //set the desired bits in that 4-bit field
You can directly write to whole 16bit "port" (GPIO). There are few ways, "bare metal", for example
GPIOA->ODR = x;
using LL API:
But if you need to modify only few bits (for example lower 8) from all 16. Then you have two posibilities.
a) Do read-modify-write on whole port, like:
tmp = LL_GPIO_ReadOutputPort(GPIOA); // read tmp = tmp & 0x00FF; // clear (modify) tmp |= x; // write (modify) LL_GPIO_WriteOutputPort(GPIOA,x); // write
In this case you have to consider to force "atomicity" if needed (search what that mean if you do not know).
b) Use Bit Set/Reset register. For example in STM32G0 you have 32bit GPIOx->BSSR register. High part is used to reseting selected bits in ODR ("port" output"), Lower part serve to setting selected bits. Writing into this register can atomicaly change any selected bits to any combination without affecting others. But written value have to be "prepared" in advance (you cannot write directly your "x" value). This is atomic process and you have not solve atomicity problems (if there are).
Take a look at LL_GPIO_WriteOutputPort in page 814 of UM1850