Zephyr GPIO write works but readback fails

My device tree overlay GPIO section looks like this:


 /delete-node/ &led2; // GPIO 19 repurposed to be vout enable

 /{
	lm5176_control {
		compatible = "gpio-leds";
		enable1: enable_1 {
			gpios = <&gpio0 19 GPIO_ACTIVE_HIGH>;
			label = "VOUT Enable Low";
		};
	};
	aliases {
		cmvoutenable = &enable1;
		/delete-property/ led1; // This was GPIO 19
	};
 };

Note that I repurposing a GPIO that was used for an LED in the board's device tree file. Also note that node numbers start from 1 and property numbers start from 0, so the delete-node and delete-property are both deleting the items for GPIO 19.

My runtime code looks like this:

#define VOUT_ENABLE DT_ALIAS(cmvoutenable)
static const struct gpio_dt_spec vout_enable = GPIO_DT_SPEC_GET(VOUT_ENABLE, gpios);

#define PGOOD DT_ALIAS(cmpgood)
static const struct gpio_dt_spec pgood = GPIO_DT_SPEC_GET(PGOOD, gpios);

message_error_t lm5176_init( void ) {

    message_error_t ret = MESSAGE_OK;

    printk("Initializing LM5176 boost/buck\n");
    if (!device_is_ready(vout_enable.port)) {
        printk("VOUT GPIO enable not ready\n");
        return GPIO_ERROR;
    } else {
        if( gpio_pin_configure_dt(&vout_enable, GPIO_OUTPUT_ACTIVE) < 0 ) {
            printk("VOUT GPIO enable cannot be activated\n");
            return GPIO_ERROR;
        }
    }
    if (!device_is_ready(pgood.port)) {
        printk("PGOOD GPIO not ready\n");
        return GPIO_ERROR;
    } else {
        if( gpio_pin_configure_dt(&pgood, GPIO_INPUT ) < 0 ) {
            printk("PGOOD GPIO cannot set as input\n");
            return GPIO_ERROR;
        }
    }
    ret = cm_write_vout_mv( 5000 );
    if(  ret != MESSAGE_OK ) {
        printk("Error initializing lm5176\n");
        return ret;
    }
    ret = lm5176_vout_set_enable(false);
    if(  ret != MESSAGE_OK ) {
        printk("Error setting Vout\n");
        return ret;
    }
    return MESSAGE_OK;
}


message_error_t lm5176_vout_set_enable( bool on )
{
    bool pin;
    if ( on ) {
        printk("lm5176_vout_set_enable - setting Vout enable pin low (enabled)\n");
        pin = 0;
    }
    else {
        printk("lm5176_vout_set_enable - setting Vout enable pin high (disabled)\n");
        pin = 1;
    }
    // GPIO logic is reversed from pin function
    if( gpio_pin_set_dt(&vout_enable, pin ) < 0 ) {
        printk("Unable to set Vout enable\n");
        return GPIO_ERROR;
    }
    return MESSAGE_OK;
}

bool lm5176_vout_get_enable( void ) {
    // GPIO logic is reversed from pin function
    if( gpio_pin_get_dt(&vout_enable) ) {
        printk("lm5176_vout_get_enable - Vout pin high (disabled)\n");
        return false;
    } else {
        printk("lm5176_vout_get_enable - Vout pin low (enabled)\n");
        return true;
    }
}

The gpio_pin_set_dt call is working perfectly - I can see the GPIO pin go high or low as expected.

But the gpio_pin_get_dt call always returns false, regardless of the state of the GPIO output.

I suspect this problem is because I'm configuring the GPIO as an output and then trying to read its state. In other words, I'd like to access the output register of the GPIO instead of creating a different variable to hold the last state I set. Is this possible?

Related