Permit inverted GPIOs in the ST HAL
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Erki 2021-04-16 20:53:30 +03:00
parent df35b93d22
commit aae29d8e0a

View File

@ -74,23 +74,26 @@ struct Gpio
{ {
GPIO_TypeDef* port = nullptr; GPIO_TypeDef* port = nullptr;
std::uint16_t pin = 0; std::uint16_t pin = 0;
const bool inverted = false;
Gpio() = delete; Gpio() = delete;
explicit Gpio(GPIO_TypeDef* port, const std::uint16_t pin) explicit Gpio(GPIO_TypeDef* port, const std::uint16_t pin, const bool inverted)
: port(port), pin(pin) {} : port(port), pin(pin), inverted(inverted) {}
void Set(const bool& state) void Set(const bool& state)
{ {
HAL_GPIO_WritePin(port, pin, GPIO_PinState(state)); HAL_GPIO_WritePin(port, pin, GPIO_PinState(inverted ? !state : state));
} }
void Toggle() { HAL_GPIO_TogglePin(port, pin); } void Toggle() { HAL_GPIO_TogglePin(port, pin); }
bool Read() const { return HAL_GPIO_ReadPin(port, pin); } bool Read() const { return inverted ? !bool(HAL_GPIO_ReadPin(port, pin)) : bool(HAL_GPIO_ReadPin(port, pin)); }
}; };
#define CREATE_GPIO(name) \ #define CREATE_GPIO(name) \
Peripherals::Hal::St::Gpio { name##_GPIO_Port, name##_Pin } Peripherals::Hal::St::Gpio { name##_GPIO_Port, name##_Pin, false }
#define CREATE_INV_GPIO(name) \
Peripherals::Hal::St::Gpio { name##_GPIO_Port, name##_Pin, true }
#endif// HAL_GPIO_MODULE_ENABLED #endif// HAL_GPIO_MODULE_ENABLED