Make Read/Write/Toggle pin functional for testing

Make ReadPin return the last value set by WritePin for proper testing.

Add a slow-path to TogglePin that goes through a read-write cycle. This
is useful both for testing and for platforms that don't have an
efficient toggle like AVR.
pull/56/head
Yuri D'Elia 2021-07-11 20:25:52 +02:00 committed by DRracer
parent 151f030318
commit be9296281f
1 changed files with 10 additions and 0 deletions

View File

@ -57,11 +57,21 @@ __attribute__((always_inline)) inline void WritePin(const GPIO_pin portPin, Leve
} }
__attribute__((always_inline)) inline Level ReadPin(const GPIO_pin portPin) { __attribute__((always_inline)) inline Level ReadPin(const GPIO_pin portPin) {
#ifdef __AVR__
return (Level)((portPin.port->PINx & (1 << portPin.pin)) != 0); return (Level)((portPin.port->PINx & (1 << portPin.pin)) != 0);
#else
// Return the value modified by WritePin
return (Level)((portPin.port->PORTx & (1 << portPin.pin)) != 0);
#endif
} }
__attribute__((always_inline)) inline void TogglePin(const GPIO_pin portPin) { __attribute__((always_inline)) inline void TogglePin(const GPIO_pin portPin) {
#ifdef __AVR__
// Optimized path for AVR, resulting in a pin toggle
portPin.port->PINx |= (1 << portPin.pin); portPin.port->PINx |= (1 << portPin.pin);
#else
WritePin(portPin, (Level)(ReadPin(portPin) != Level::high));
#endif
} }
__attribute__((always_inline)) inline void Init(const GPIO_pin portPin, GPIO_InitTypeDef GPIO_Init) { __attribute__((always_inline)) inline void Init(const GPIO_pin portPin, GPIO_InitTypeDef GPIO_Init) {