Enabling gpio using Zephyr for the Seeed XIAO nRF52840

Hello All,

I am trying to configure a gpio pin using Zephyr, however for some reason I am not able to configure the GPIO that would be an input for a button press.

The below is the related code:

int* init_GPIO(const char *device_name, int button_pin, int wrist_led_pin, int PWM_pulse_array_size, int pulse_logic_zero_val, gpio_callback_handler_t button_change_cb){
	const struct device *ble_device = device_get_binding(device_name);
	if (ble_device == NULL) {
		printk("Device binding is not found\n");
		return NULL; 
	}

	int err; 

	err = init_interface_button(ble_device, button_pin, button_change_cb);
	if (err < 0) {
		printk("Failed to establish interface button GPIO\n");
		return NULL; 
	}

	return; 

	// int* pwm_logic_array = init_wrist_led(ble_device, wrist_led_pin, PWM_pulse_array_size,pulse_logic_zero_val);

	// return pwm_logic_array; 
	
}

int init_interface_button(const struct device* ble_device, int button_pin, gpio_callback_handler_t button_change_cb){
	int err;
	static struct gpio_callback button_cb;
	gpio_flags_t flags = GPIO_INPUT | GPIO_PULL_UP | GPIO_ACTIVE_LOW;
	gpio_flags_t interrupts = GPIO_INT_EDGE_BOTH;

	err = gpio_pin_configure(ble_device, button_pin, GPIO_INPUT);
	if (err < 0) {
		printk("Error %d: failed to configure button gpio pin %d\n", err, button_pin);
		return err; 
	} 

	// err = gpio_pin_interrupt_configure(ble_device, button_pin, interrupts);
	// if (err != 0){
	// 	printk("Error %d: failed to configure button callback on pin %d\n", err, button_pin);
	// 	return err;
	// }

	// gpio_init_callback(&button_cb, button_change_cb,BIT(button_pin));

	return 0;
}

I have isolated the problem to be present at at when I configure the gpio pin for the button.

the below is the relevant code used to call the function:

#define ZEPHYR_DEVICE_NAME "GPIO_0"
//GPIO PINs
#define PIN_WRIST_LED 0
#define PIN_BUTTON_INTERFACE 1

#define NUM_WRIST_LED (12)
#define PWM_PULSE_ARRAY_SIZE (4*8*NUM_WRIST_LED)
#define PWM_OFF 600 //ns
#define PWM_ON 300 //ns
#define PWM_PERIOD 1250 //ns

int main(void)
{
	int err;


	//initalize the gpio
	pwm_pulse_array = init_GPIO(ZEPHYR_DEVICE_NAME, PIN_BUTTON_INTERFACE, PIN_WRIST_LED, PWM_PULSE_ARRAY_SIZE, PWM_OFF, button_interface_change_cb);
	if (pwm_pulse_array == NULL) {
		*led_operation_ptr = DISCONNECTED; 

		printk("GPIO init failed\n");
	}

The below is the device tree file code that is relating to the gpio:
xiao_ble_common.dts file

&gpiote {
	status = "okay";
};

&gpio0 {
	status = "okay";
};

&gpio1 {
	status = "okay";
};

I had modified the gpio fields to be given labels so I cam import them into the main.c code in the local xiao_ble.ts file as below:

/dts-v1/;
#include "xiao_ble_common.dtsi"

/ {
	model = "Seeed XIAO BLE";
	compatible = "seeed,xiao-ble";
};

&gpiote {
	status = "okay";
	label = "GPIOTE";
};

&gpio0 {
	status = "okay";
	label = "GPIO_0";
};

&gpio1 {
	status = "okay";
	label = "GPIO_1";
};

I have concluded that I should use the GPIO_0 label because the pins that I want to use from the pin out all start with a 0.

Any insight as to what I am doing wrong would be much appropriated. Furthermore, I am confused as to why the gpio properties in the .dts files are blank. is this suppose to be the case?

Thanks Everyone

Hi there,
Try the GPIO pin number, Ie GPIO2

HTH
GL :slight_smile: PJ
:v:

that is one issue. However, my button init code still returns an error. My led wrist init code works perfectly now.

Is there anything wrong with my configuration of the gpio as an input? That is the only thing I think it can be.

Thanks

Hm,
Print out the array of params is it Correct?

pwm_pulse_array = init_GPIO(ZEPHYR_DEVICE_NAME, PIN_BUTTON_INTERFACE, PIN_WRIST_LED, PWM_PULSE_ARRAY_SIZE, PWM_OFF, button_interface_change_cb);
and or this one?
int* init_GPIO(const char *device_name, int button_pin, int wrist_led_pin, int PWM_pulse_array_size, int pulse_logic_zero_val, gpio_callback_handler_t button_change_cb)

something isn’t just right.
GL :slight_smile: PJ

I’m looking here,
https://docs.zephyrproject.org/latest/boards/seeed/xiao_esp32c3/doc/index.html

OK I got that portion to work. The device name was completely wrong. It needed to be the value of the service you are trying to use in the device tree file. For example for the gpio pins I wanted to use it was defined as gpio0. Furthermore, the pin number to be used should be the pins values given below:

Hopefully this helps others in their projects.

Hi there,
As I pointed out in my previous post and In another the User LED wasn’t specified for a Zephyr build, its been a trend they are moving too. GPIO’s instead of physical pin numbers with so many variant makes sense I suppose. Do mark it as a Solution so others can Find it faster.
Good work , BTW
GL :slight_smile: PJ
:v: