Hello,
I am currently trying to get a slightly modified version of the blink example from zephyr to work on the nrf5340.
As you can see below, I am starting two threads, one for the input button and one for the LED, and as soon as the LED thread is started, the LED should start blinking. Nevertheless, it doesn't and if I take a look in the serial output, I see the "ON" message but never the "OFF" message. Therefore it seems, that the thread never wakes up from the k_msleep() function.
Does anyone have a clue for me why this is happening?
Thanks in advance!
PS: I am using a nrf5340 DK
#include <zephyr.h>
#include <device.h>
#include <drivers/gpio.h>
#include <sys/printk.h>
#include <sys/__assert.h>
#include <string.h>
/* size of stack area used by each thread */
#define STACKSIZE 1024
/* scheduling priority used by each thread */
#define PRIORITY 7
#define LED_NODE DT_ALIAS(led0)
#define BUTTON_NODE DT_ALIAS(sw0)
struct led {
struct gpio_dt_spec spec;
const char *gpio_pin_name;
};
struct button {
struct gpio_dt_spec spec;
const char *gpio_pin_name;
};
static const struct led led = {
.spec = GPIO_DT_SPEC_GET_OR(LED_NODE, gpios, {0}),
.gpio_pin_name = DT_PROP_OR(LED_NODE, label, ""),
};
static const struct button button = {
.spec = GPIO_DT_SPEC_GET_OR(BUTTON_NODE, gpios, {0}),
.gpio_pin_name = DT_PROP_OR(BUTTON_NODE, label, ""),
};
void blink(const struct led *led, uint32_t sleep_ms, uint32_t id)
{
gpio_pin_configure_dt(&led->spec, GPIO_OUTPUT);
while (1) {
gpio_pin_set_dt(&led->spec, 1);
printk("ON\n");
k_msleep(sleep_ms);
gpio_pin_set_dt(&led->spec, 0);
printk("OFF\n");
k_msleep(sleep_ms);
}
}
void blinkThread(void)
{
blink(&led, 100, 0);
}
void inputThread(void)
{
gpio_pin_configure_dt(&button.spec, GPIO_INPUT);
while (1) {
if (gpio_pin_get(button.spec.port, button.spec.pin)) {
printk("Button pressed\n");
}
}
}
K_THREAD_DEFINE(blinkThread_id, STACKSIZE, blinkThread, NULL, NULL, NULL,
PRIORITY, 0, 0);
K_THREAD_DEFINE(inputThread_id, STACKSIZE, inputThread, NULL, NULL, NULL,
PRIORITY, 0, 0);