From a4d9238a13a34535b64a8596ff168b66b82ece42 Mon Sep 17 00:00:00 2001 From: Andrey Gusakov Date: Wed, 1 May 2024 22:59:24 +0300 Subject: [PATCH] gpio: core: guards --- firmware/hw_layer/drivers/gpio/core.cpp | 66 +++++++++++++---------- firmware/hw_layer/drivers/gpio/gpio_ext.h | 5 +- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/firmware/hw_layer/drivers/gpio/core.cpp b/firmware/hw_layer/drivers/gpio/core.cpp index 19f9a41869..efb5450250 100644 --- a/firmware/hw_layer/drivers/gpio/core.cpp +++ b/firmware/hw_layer/drivers/gpio/core.cpp @@ -40,6 +40,8 @@ struct gpiochip { static gpiochip chips[BOARD_EXT_GPIOCHIPS]; +#if EFI_PROD_CODE + /* TODO: move inside gpio chip driver? */ class external_hardware_pwm : public hardware_pwm { public: @@ -81,6 +83,8 @@ private: /* TODO: is 5 enought? */ static external_hardware_pwm extPwms[5]; +#endif + /*==========================================================================*/ /* Local functions. */ /*==========================================================================*/ @@ -100,6 +104,8 @@ static gpiochip *gpiochip_find(brain_pin_e pin) return nullptr; } +#if EFI_PROD_CODE + static external_hardware_pwm* gpiochip_getNextPwmDevice() { for (size_t i = 0; i < efi::size(extPwms); i++) { if (!extPwms[i].hasInit()) { @@ -111,6 +117,8 @@ static external_hardware_pwm* gpiochip_getNextPwmDevice() { return nullptr; } +#endif + /*==========================================================================*/ /* Exported functions. */ /*==========================================================================*/ @@ -354,33 +362,6 @@ int gpiochips_readPad(brain_pin_e pin) return chip->chip->readPad(pin - chip->base); } -/** - * @brief Try to init PWM on given pin - * @details success of call depends on chip capabilities - * returns nullptr in case there is no chip for given pin - * returns nullptr in case of pin is not PWM capable - * returns nullptr in case all extPwms are already used - * returns hardware_pwm if succes, later user can call ->setDuty to change duty - */ - -hardware_pwm* gpiochip_tryInitPwm(const char* msg, brain_pin_e pin, float frequency, float duty) -{ - gpiochip *chip = gpiochip_find(pin); - - if (!chip) { - return nullptr; - } - - /* TODO: implement reintialization of same pin with different settings reusing same external_hardware_pwm */ - if (external_hardware_pwm *device = gpiochip_getNextPwmDevice()) { - if (device->start(msg, chip, pin - chip->base, frequency, duty) >= 0) { - return device; - } - } - - return nullptr; -} - /** * @brief Get diagnostic for given gpio * @details actual output value depend on gpiochip capabilities @@ -421,6 +402,37 @@ int gpiochips_get_total_pins(void) return cnt; } +#if EFI_PROD_CODE + +/** + * @brief Try to init PWM on given pin + * @details success of call depends on chip capabilities + * returns nullptr in case there is no chip for given pin + * returns nullptr in case of pin is not PWM capable + * returns nullptr in case all extPwms are already used + * returns hardware_pwm if succes, later user can call ->setDuty to change duty + */ + +hardware_pwm* gpiochip_tryInitPwm(const char* msg, brain_pin_e pin, float frequency, float duty) +{ + gpiochip *chip = gpiochip_find(pin); + + if (!chip) { + return nullptr; + } + + /* TODO: implement reintialization of same pin with different settings reusing same external_hardware_pwm */ + if (external_hardware_pwm *device = gpiochip_getNextPwmDevice()) { + if (device->start(msg, chip, pin - chip->base, frequency, duty) >= 0) { + return device; + } + } + + return nullptr; +} + +#endif + #else /* BOARD_EXT_GPIOCHIPS > 0 */ int gpiochips_getPinOffset(brain_pin_e pin) { diff --git a/firmware/hw_layer/drivers/gpio/gpio_ext.h b/firmware/hw_layer/drivers/gpio/gpio_ext.h index df570f5882..67f5d2773f 100644 --- a/firmware/hw_layer/drivers/gpio/gpio_ext.h +++ b/firmware/hw_layer/drivers/gpio/gpio_ext.h @@ -55,8 +55,11 @@ int gpiochips_init(void); int gpiochips_setPadMode(brain_pin_e pin, iomode_t mode); int gpiochips_writePad(brain_pin_e pin, int value); int gpiochips_readPad(brain_pin_e pin); -hardware_pwm* gpiochip_tryInitPwm(const char* msg, brain_pin_e pin, float frequency, float duty); brain_pin_diag_e gpiochips_getDiag(brain_pin_e pin); +#if EFI_PROD_CODE +hardware_pwm* gpiochip_tryInitPwm(const char* msg, brain_pin_e pin, float frequency, float duty); +#endif + /* return total number of external gpios */ int gpiochips_get_total_pins(void);