Bit Manipulation Tips

With examples

XOR Bit Resetting

Backstory

GPIOA PIN 12 had to be changed from the default “ANALOG” Mode, to “ALTERNATE FUNCTION” Mode. The MCU was a STM32L073, which is a Cortex-M0+.

GPIO PINs are configured within the MODER register in pairs as can be seen from the table below.”%” in Forth means “binary value”

The existing GPIOA_MODER value was $EBEBFCFF and all other bits had to remain unchanged..

This is the binary value of $EBEBFCFF, (the value of GPIOA_MODER) and the bits are grouped in pairs for clarity. PIN 12 consists of bits 24 and 25.

GPIOPA_MODER: $EBEBFCFF
15|14|13|12|11|10|09|08|07|06|05|04|03|02|01|00
11 10 10 11 11 10 10 11 11 11 11 00 11 11 11 11

By default, all pins after power reset are configured to “ANALOG” and bit 12 was still default. To change it to AF (ALTERNATE FUNCTION) it had to be changed from “%11” to “10”. This was done as below.

GPIOx_MODER Table

_images/gpiox_moder.jpg

MODER

Mode

%00

INPUT

%01

OUTPUT

%10

AF

%11

ANALOG

Operation

Note

All commands in CAPITALS are standard Mecrisp-Stellaris Words.

----------------------------- --------------------------------------------------------------- ---------
Operation                     Binary Value                                                    Hex Value
----------------------------- --------------------------------------------------------------- ---------
Legend                        3|3|2|2|2|2|2|2|2|2|2|2|1|1|1|1|1|1|1|1|1|1|
Legend                        1|0|9|8|7|6|5|4|3|2|1|0|9|8|7|6|5|4|3|2|1|0|9|8|7|6|5|4|3|2|1|0
$EBEBFCFF                     1 1 1 0 1 0 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 $EBEBFCFF
1 24 LSHIFT                   0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 $01000000
$EBEBFCFF $01000000 XOR       1 1 1 0 1 0 1 0 1 1 1 0 1 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 $EAEBFCFF

Summary

After the operation, the new value was $EAEBFCFF and this was used as a value in a Assembly Language program.

@ Set PORTA TX, RX and RTS in alternate function mode
ldr r1, = GPIOA_MODER
ldr r0, = 0xEAEBFCFF  @ EBFFFCFF is reset value for Port A
str r0, [r1]