gpio: core: guards

This commit is contained in:
Andrey Gusakov 2024-05-01 22:59:24 +03:00
parent b8edd90a22
commit 1cc685c682
2 changed files with 43 additions and 28 deletions

View File

@ -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) {

View File

@ -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);