From aae29d8e0a75eb9884e141a01d18c2a81754d6a8 Mon Sep 17 00:00:00 2001 From: Erki Date: Fri, 16 Apr 2021 20:53:30 +0300 Subject: [PATCH] Permit inverted GPIOs in the ST HAL --- Peripherals/Inc/peripherals_hal_st.hpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Peripherals/Inc/peripherals_hal_st.hpp b/Peripherals/Inc/peripherals_hal_st.hpp index 5dc07e8..f503c2f 100644 --- a/Peripherals/Inc/peripherals_hal_st.hpp +++ b/Peripherals/Inc/peripherals_hal_st.hpp @@ -74,23 +74,26 @@ struct Gpio { GPIO_TypeDef* port = nullptr; std::uint16_t pin = 0; + const bool inverted = false; Gpio() = delete; - explicit Gpio(GPIO_TypeDef* port, const std::uint16_t pin) - : port(port), pin(pin) {} + explicit Gpio(GPIO_TypeDef* port, const std::uint16_t pin, const bool inverted) + : port(port), pin(pin), inverted(inverted) {} 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); } - 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) \ - 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