[nRF5340 TWI ] Access two sensors separetly on same TWI

Hello,

I am using the gpio_pca95cc.c driver on the zephyr for a nRF5340. I am a beginner at using zephyr OS

I am able to use one GPIO expander but when I add the second one, I can’t figure a way to access a specific device

In the myboard.dts file I configure the hardware with the following

&i2c0 {
    compatible = "nordic,nrf-twim";
    status = "okay";
    pinctrl-0 = <&i2c0_default>;
    pinctrl-1 = <&i2c0_sleep>;
    pinctrl-names = "default", "sleep";
    PCAL9535_0: pca95xx@20 {
        compatible = "nxp,pca95xx";
        #gpio-cells = <2>;
        gpio-controller;
        reg = <0x20 >;
        ngpios = < 16 >;
        interrupt-gpios = <&gpio0 9 (GPIO_ACTIVE_LOW)>;
        has-interrupt-mask-reg;
    };
    PCAL9535_1: pca95xx@21 {
        compatible = "nxp,pca95xx";
        #gpio-cells = <2>;
        gpio-controller;
        reg = <0x21 >;
        ngpios = < 16 >;
        interrupt-gpios = <&gpio0 9 (GPIO_ACTIVE_LOW)>;
        has-interrupt-mask-reg;
    };
};

&PCAL9535_0 {
	status = "okay";
};

&PCAL9535_1 {
	status = "okay";
};

In the prj.conf:

# Enable I2C
CONFIG_GPIO=y
CONFIG_I2C=y
CONFIG_GPIO_PCA95XX=y
CONFIG_GPIO_PCA95XX_INTERRUPT=y

And in the main.c file to use the driver when I have only one I do this:

void main(void)
{
    int err = 0;
    const struct device *const gpio_pca0 = DEVICE_DT_GET_ANY(nxp_pca95xx);
    if (!gpio_pca0) {
        LOG_INF("Error initialization PCA");
        return;
    }
    gpio_pin_configure(gpio_pca0, 12, GPIO_OUTPUT);
    gpio_pin_set(gpio_pca0, 12, 1);
}

My problem is that I cannot choose which device is chosen by the function DEVICE_DT_GET_ANY(nxp_pca95xx);

Ideally I would like to create two struct device one that points at each IO expander so that I can use them separately:

 

    const struct device *const gpio_pca0 = DEVICE_DT_GET_ANY(nxp_pca95xx);
    const struct device *const gpio_pca1 = DEVICE_DT_GET_ANY(nxp_pca95xx);

I have tried with the function DEVICE_DT_GET to select a custom one but I could not find my way around the definitionsas I always have an error :

#define PCA1_NODE DT_ALIAS(PCAL9535_1)
#define PCA1_NAME DEVICE_DT_NAME_GET(PCA1_NODE)

void main(void)
{
    int err = 0;
    const struct device *const gpio_pca1 = DEVICE_DT_GET(PCA1_NAME);
…
}

Could you help me with this problem?

Thank you by advance

Regards

Augustin

  • Hi Augustin,

    You can use something similar as below:

    void main(void)
    {
    	int err = 0;
    
    	const struct device *const gpio_pcaA = DEVICE_DT_GET(DT_NODELABEL(pcaalpha));
    	const struct device *const gpio_pcaB = DEVICE_DT_GET(DT_NODELABEL(pcabravo));
    
    	if (!gpio_pcaA) {
    		printk("\nError initialization Alpha PCA"); return;
    	}
    	
    	if (!gpio_pcaB) {
    		printk("\nError initialization Bravo PCA"); return;
    	}
    
    	printk("%s PCA initialized on ! %s\n", gpio_pcaA->name, CONFIG_BOARD);
    	printk("%s PCA initialized on ! %s\n", gpio_pcaB->name, CONFIG_BOARD);
    
    }

    Please note that I have changed the sub-node names in the device overlay as (also some minor modifications):

    &i2c0 {
        compatible = "nordic,nrf-twim";
        status = "okay";
        pinctrl-0 = <&i2c0_default>;
        pinctrl-1 = <&i2c0_sleep>;
        pinctrl-names = "default", "sleep";
        pcaAlpha: pcaAlpha@20 {
            compatible = "nxp,pca95xx";
            #gpio-cells = <2>;
            gpio-controller;
            reg = <0x20 >;
            ngpios = < 16 >;
            interrupt-gpios = <&gpio0 9 (GPIO_ACTIVE_LOW)>;
            label = "pcaAlpha";
        };
        pcaBravo: pcaBravo@21 {
            compatible = "nxp,pca95xx";
            #gpio-cells = <2>;
            gpio-controller;
            reg = <0x21 >;
            ngpios = < 16 >;
            interrupt-gpios = <&gpio0 9 (GPIO_ACTIVE_LOW)>;        
            label = "pcaBravo";
        };
    };

    Tested it on 52840

    Hope it helps,

    Naeem

Related