To Interact With The General
To Interact With The General
Initializing the API From the image above, we can see that the
Some of the generic APIs in Zephyr have API- property containing all this information is
specific structs that contain the previously called gpios, and is the property name to pass
mentioned device pointer, as well as some other to GPIO_DT_SPEC_GET():
information about the device. In the GPIO API, this static const struct gpio_dt_spec led =
is the structure gpio_dt_spec. This structure has GPIO_DT_SPEC_GET(LED0_NODE, gpios);
the device pointer const struct device * port, as
well as the pin number on the device, gpio_pin_t This function will return a struct of
pin, and the device’s configuration type gpio_dt_spec with the device pointer for the
flags, gpio_dt_flags_t dt_flags. GPIO controller, &gpio0, the pin number led.pin =
13 and the flag led.dt_flags = GPIO_ACTIVE_LOW.
The port is the GPIO device controlling the pin. Pins
are usually grouped and controlled by a single GPIO Before using the device pointer contained
port. On most Nordic SoCs, there are either one or in gpio_dt_spec led, we need to check if it’s ready
two GPIO controllers, named GPIO0 or GPIO1. using gpio_is_ready_dt().
Polling means continuously reading the status of The callback function is called when an interrupt is
the pin to check if it has changed. To read the triggered.
current status of a pin, all you need to do is to call
Definition
the function gpio_pin_get_dt(),
Callback function: Also known as an interrupt For example, the following line will initialize
handler or an Interrupt Service Routine (ISR). It the pin_cb_data variable with the callback
runs asynchronously in response to a hardware or function pin_isr and the bit mask of pin dev.pin.
software interrupt. In general, ISRs have higher Note the use of the macro BIT(n), which simply gets
priority than all threads (covered in Lesson 7). It an unsigned integer with bit position n set.
preempts the execution of the current thread,
gpio_init_callback(&pin_cb_data, pin_isr,
allowing an action to take place immediately.
BIT(dev.pin));
Thread execution resumes only once all ISR work
has been completed. For example, the following line adds the callback
function that we set up in the previous steps.
The signature (prototype) of the callback function is
shown below: gpio_add_callback(button.port, &pin_cb_data);