well, some numbers: Keil MDK, -Odefault, STM32F103
first, using BSRR/BRR: 6.7K ticks/1K run
Code: Select all
#define FIO_SET(port, pins) port->BSRR = (pins)
#define FIO_CLR(port, pins) port->BRR = (pins)
#define FIO_FLP(port, pins) (IO_GET(port, pins)?FIO_CLR(port, pins):FIO_SET(port, pins))
#define FIO_GET(port, pins) IO_GET(port, pins)
now, bit banding: 15.7K ticks /1K run
Code: Select all
#define PERI_BASE 0x40000000
#define PERI_BB_BASE (PERI_BASE + 0x02000000)
#define BIO2BB(addr, bit) (*(volatile uint32_t *) (PERI_BB_BASE | (((addr) - PERI_BASE) << 5) | ((bit) << 2)))
#define BIO_GET(port, bit) (BIO2BB((uint32_t) &(port->IDR), bit))
#define BIO_SET(port, bit) (BIO2BB((uint32_t) &(port->ODR), bit) = 1)
#define BIO_CLR(port, bit) (BIO2BB((uint32_t) &(port->ODR), bit) = 0)
#define BIO_FLP(port, bit) (BIO_GET(port, bit)?BIO_CLR(port, bit):BIO_SET(port, bit))
for comparison, the regular ops via ODR: 12.1K ticks /1K run
Code: Select all
#define IO_SET(port, pins) port |= (pins)
#define IO_CLR(port, pins) port &=~(pins)
#define IO_FLP(port, pins) port ^= (pins)
#define IO_GET(port, pins) ((port) & (pins))
#define GIO_SET(port, pins) IO_SET(port->ODR, (pins))
#define GIO_CLR(port, pins) IO_CLR(port->ODR, (pins))
#define GIO_FLP(port, pins) IO_FLP(port->ODR, (pins))
#define GIO_GET(port, pins) IO_GET(port->IDR, (pins))
/
So bit banding is the slowest of the three, and BSRR/BRR the fastest.