Toggle a pin logic state, for this example we assume we don not know if there is an led connected to pin 9 port gpio2 (but is there), for that reason we are only use the device tree definitions in file zephyr/dts/common/nordic/nrf54l_05_10_15.dtsi
, as with any other peripheral we need to get a device descriptor structure.
main.c
/* Include libraries */
#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 );
/* Configure pin 9 as output */
gpio_pin_configure( gpio2, 9, GPIO_OUTPUT);
while(1)
{
/* Toggle pin 9 from port 2 using a port function */
gpio_pin_toggle( gpio2, 9 );
/* 300ms Delay */
k_msleep( 300 );
}
return 0;
}
prj.conf
# Disable optimizations
CONFIG_NO_OPTIMIZATIONS=y
# Include GPIO driver in system config ( Kconfig )
CONFIG_GPIO=y