STM32 BSRR VS ODR.
I think those reading this article know that you can write a one to bit zero of port A in two ways
or
And everyone writes that the second way is more correct, because setting the bit is then atomic. That's hard to argue with.
Now let's imagine a situation: we have an eight-bit bus over which data is transmitted, and the connection to the bus looks like this.
So it turns out that before setting each bit with BSRR, you have to check its value and act depending on what you want to get. But checking every bit takes a long time, so let's look at this situation from a different angle.
The lines belonging to port E, E7, E8, E9, E10, are numbered consecutively, while the lines belonging to port D form two consecutive pairs, PD0, PD1 and PD14, PD15.
As written above, setting the bits with BSRR is not an option: it's too slow. It can be done faster by rewriting fragments of the ODR register, at the cost of atomicity.
So first, let's extract from our variable the data that will be sent via port E (D4 - D7).
As a result of this operation we got the data to be sent; all that's left is to put it in the right position. Right now the data occupies bits 4 to 7, but it should occupy bits 7 to 10, so let's shift it three positions to the left.
As a result we get the following expression
Using the same reasoning, we write the required value to port D.
and
But that's not all: the bits must be cleared each time before setting them.
In the end, the code for sending data over the bus will look like this.
In this situation using the ODR register turned out to be justified, and the atomicity issue is solved with the PRIMASK register, which, when set to one, disables all interrupts with configurable priority.
GPIOA->ODR |= GPIO_ODR_ODR0; or
GPIOA->BSRR = GPIO_BSRR_BS0; And everyone writes that the second way is more correct, because setting the bit is then atomic. That's hard to argue with.
Now let's imagine a situation: we have an eight-bit bus over which data is transmitted, and the connection to the bus looks like this.
//D0 - PD14
//D1 - PD15
//D2 - PD0
//D3 - PD1
//D4 - PE7
//D5 - PE8
//D6 - PE9
//D7 - PE10So it turns out that before setting each bit with BSRR, you have to check its value and act depending on what you want to get. But checking every bit takes a long time, so let's look at this situation from a different angle.
The lines belonging to port E, E7, E8, E9, E10, are numbered consecutively, while the lines belonging to port D form two consecutive pairs, PD0, PD1 and PD14, PD15.
As written above, setting the bits with BSRR is not an option: it's too slow. It can be done faster by rewriting fragments of the ODR register, at the cost of atomicity.
So first, let's extract from our variable the data that will be sent via port E (D4 - D7).
cmd & 0xF0As a result of this operation we got the data to be sent; all that's left is to put it in the right position. Right now the data occupies bits 4 to 7, but it should occupy bits 7 to 10, so let's shift it three positions to the left.
(cmd & 0x00F0)<<3;As a result we get the following expression
GPIOE->ODR |= (cmd & 0xF0)<<3;Using the same reasoning, we write the required value to port D.
GPIOD->ODR |= (cmd & 0x03)<<14;and
GPIOD->ODR |= (cmd & 0x0C)>>2;But that's not all: the bits must be cleared each time before setting them.
In the end, the code for sending data over the bus will look like this.
#define __enter_critical() {uint32_t flag; flag = __get_PRIMASK();
#define __exit_critical() __set_PRIMASK(flag);}
#define _Atomic(X) __enter_critical(); {X}; __exit_critical();
static void TFT_Send_Cmd(uint8_t cmd)
{
_Atomic
(
RS_LOW
RD_HIGH
CS_LOW
GPIOE->ODR &= ~(0xF0<<3);
GPIOE->ODR |= (cmd & 0xF0)<<3;
GPIOD->ODR &= ~(0x03<<14);
GPIOD->ODR |= (cmd & 0x03)<<14;
GPIOD->ODR &= ~(0x0C>>2);
GPIOD->ODR |= (cmd & 0x0C)>>2;
WR_LOW
delay_us(5);
WR_HIGH
CS_HIGH
)
}
In this situation using the ODR register turned out to be justified, and the atomicity issue is solved with the PRIMASK register, which, when set to one, disables all interrupts with configurable priority.