Rotate one turned led on port 2 we use the function gpio_port_set_masked
to manipulate one single led at the time using a simple mask. Pins initialization is done using the gpio node option hog.
main.c
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
/* get the node identifier from its label, in this case will be gpio@50400 for port 2 */
#define GPIO2_NODE DT_NODELABEL( gpio2 )
int main( void )
{
/* Get the device descriptor for port 2 */
const struct device *gpio2 = DEVICE_DT_GET( GPIO2_NODE );
/* All initialization is been done by Zephyr and is indicated in the app.overlay file */
while(1)
{
for(uint8_t pin = 0; pin < 5; pin++)
{
/* Attention, on Nordic nRF54l15dk gpio0 pin 1 and 2 doesnt work just toggle pin 0, 2 and 4 */
/* Rotate a turn on led on gpio0 */
gpio_port_set_masked( gpio0, 0xFF, ( 1 << pin ) );
k_msleep(300);
}
}
return 0;
}
app.overlay
/* phandle for gpio0 port */
&gpio0 {
leds: leds{
gpio-hog;
/* We indicate 5 pins for this sub-node */
gpios = <0 GPIO_ACTIVE_HIGH>, <1 GPIO_ACTIVE_HIGH>, <2 GPIO_ACTIVE_HIGH>, <3 GPIO_ACTIVE_HIGH>, <4 GPIO_ACTIVE_HIGH>;
output-low; /* All pins configured as output with low level init */
};
};
prj.conf
# Disable optimizations
CONFIG_NO_OPTIMIZATIONS=y
# Include GPIO drivers in system config (KCONFIG)
CONFIG_GPIO=y
# Option to init pins using the DTC
CONFIG_GPIO_HOGS=y