This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nrf9160 DK thread not running

Hi 

I am using nRF9160 DK board ,below is my code, I want to create two threads in main() function for LED 1 and LED 2 to turn on led alternatively.

For some reason threads are not creating.Can you help me what wrong I have done in the code which is preventing to create thread.

#include <device.h>
#include <drivers/gpio.h>
#include <generated_dts_board_unfixed.h>
#include <kernel.h>
#include <sys/printk.h>
#include <zephyr.h>

#define LED_PORT DT_ALIAS_LED0_GPIOS_CONTROLLER
#define LED_PIN_1 DT_GPIO_LEDS_LED0_GPIOS_PIN
#define LED_PIN_2 DT_GPIO_LEDS_LED1_GPIOS_PIN
#define STACK_SIZE 1024U
#define PRIORITY 1u


void drive_led1(void *dummy1, void *dummy2, void *dummy3);
void drive_led2(void *dummy1, void *dummy2, void *dummy3);
void driveLed(struct device *devId, uint32_t pinNum);

K_THREAD_STACK_DEFINE(led1_id_stack_area, STACK_SIZE);
K_THREAD_STACK_DEFINE(led2_id_stack_area, STACK_SIZE);

K_SEM_DEFINE(drive_led, 1, 1); //Statically define and intialize a semaphore.

static struct k_thread led1_id;
static struct k_thread led2_id;

void main(void)
{


printk("Hello World!\n");

k_tid_t Led1_tid = k_thread_create(&led1_id,led1_id_stack_area,K_THREAD_STACK_SIZEOF(led1_id_stack_area),
drive_led1,NULL, NULL, NULL,PRIORITY, 0, K_NO_WAIT);

k_thread_name_set(Led1_tid, "thread_led1");

k_tid_t Led2_tid = k_thread_create(&led2_id,led2_id_stack_area,K_THREAD_STACK_SIZEOF(led2_id_stack_area),
drive_led2, NULL, NULL, NULL,PRIORITY, 0, K_NO_WAIT);

k_thread_name_set(Led2_tid, "thread_led2");



while (1)
{

}
}

//K_THREAD_DEFINE(led1_id, STACK_SIZE, drive_led1, NULL, NULL, NULL, 1, 0, K_NO_WAIT);
//K_THREAD_DEFINE(led2_id, STACK_SIZE, drive_led2, NULL, NULL, NULL, 1, 0, K_NO_WAIT);

void drive_led1(void *dummy1, void *dummy2, void *dummy3) {
struct device *dev;
static int semaTaken;
dev = device_get_binding(LED_PORT);
gpio_pin_configure(dev, LED_PIN_1, GPIO_DIR_OUT);

while (1) {
semaTaken = k_sem_take(&drive_led, K_FOREVER);
driveLed(dev, LED_PIN_1);
k_sem_give(&drive_led);
}
}

void drive_led2(void *dummy1, void *dummy2, void *dummy3) {
struct device *dev;
static int semaTaken;
dev = device_get_binding(LED_PORT);
gpio_pin_configure(dev, LED_PIN_2, GPIO_DIR_OUT);

while (1) {
semaTaken = k_sem_take(&drive_led, K_FOREVER);
driveLed(dev, LED_PIN_2);
k_sem_give(&drive_led);
}
}

void driveLed(struct device *devId, uint32_t pinNum) {
gpio_pin_write(devId, pinNum, 1);
k_sleep(1000u);
gpio_pin_write(devId, pinNum, 0);
k_sleep(1000u);
}

Parents Reply Children
  • Hi Martin,

    Anything wrong in my example code?

    Please suggest me

    regards,

    Praveen Palaparthi

  • Hi Praveen,

    Yes, there is. 

    Please use this Synchronization sample as a reference on how you can make Threads and how you can synchronize.

    I've made a version based on the said sample that achieves what you are trying to do:

    /* main.c - Hello World demo */
    
    /*
     * Copyright (c) 2012-2014 Wind River Systems, Inc.
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <zephyr.h>
    #include <sys/printk.h>
    #include <device.h>
    #include <drivers/gpio.h>
    
    /*
     * The hello world demo has two threads that utilize semaphores and sleeping
     * to take turns printing a greeting message at a controlled rate. The demo
     * shows both the static and dynamic approaches for spawning a thread; a real
     * world application would likely use the static approach for both threads.
     */
    
    
    /* Change this if you have an LED connected to a custom port */
    #ifndef DT_ALIAS_LED0_GPIOS_CONTROLLER
    #define DT_ALIAS_LED0_GPIOS_CONTROLLER 	LED0_GPIO_PORT
    #endif
    #ifndef DT_ALIAS_LED1_GPIOS_CONTROLLER
    #define DT_ALIAS_LED1_GPIOS_CONTROLLER 	LED1_GPIO_PORT
    #endif
    
    
    #define PORT0	 DT_ALIAS_LED0_GPIOS_CONTROLLER
    #define PORT1	 DT_ALIAS_LED1_GPIOS_CONTROLLER
    
    
    /* Change this if you have an LED connected to a custom pin */
    #define LED0    DT_ALIAS_LED0_GPIOS_PIN
    #define LED1    DT_ALIAS_LED1_GPIOS_PIN
    
    
    /* size of stack area used by each thread */
    #define STACKSIZE 1024
    
    /* scheduling priority used by each thread */
    #define PRIORITY 7
    
    /* delay between greetings (in ms) */
    #define SLEEPTIME 500
    
    
    void blink(const char *port, u32_t sleep_ms, u32_t led, u32_t id)
    {
    	int cnt = 0;
    	struct device *gpio_dev;
    
    	gpio_dev = device_get_binding(port);
    	__ASSERT_NO_MSG(gpio_dev != NULL);
    	gpio_pin_configure(gpio_dev, led, GPIO_DIR_OUT);
    
    	while (1) {
    		gpio_pin_write(gpio_dev, led, cnt % 2);
    		k_sleep(sleep_ms);
    		cnt++;
    	}
    }
    
    
    void blink1(void)
    {
    	blink(PORT0, 400, LED0, 0);
    }
    
    void blink2(void)
    {
    	k_sleep(400);
    	blink(PORT1, 400, LED1, 1);
    }
    
    
    /*
     * @param my_name      thread identification string
     * @param my_sem       thread's own semaphore
     * @param other_sem    other thread's semaphore
     */
    void helloLoop(const char *my_name,
    	       struct k_sem *my_sem, struct k_sem *other_sem)
    {
    	const char *tname;
    
    	while (1) {
    		/* take my semaphore */
    		k_sem_take(my_sem, K_FOREVER);
    
    		/* say "hello" */
    		tname = k_thread_name_get(k_current_get());
    		if (tname == NULL) {
    			printk("%s: Hello World from %s!\n",
    				my_name, CONFIG_BOARD);
    		} else {
    			printk("%s: Hello World from %s!\n",
    				tname, CONFIG_BOARD);
    		}
    
    		/* wait a while, then let other thread have a turn */
    		k_sleep(SLEEPTIME);
    		k_sem_give(other_sem);
    	}
    }
    
    /* define semaphores */
    
    K_SEM_DEFINE(threadA_sem, 1, 1);	/* starts off "available" */
    K_SEM_DEFINE(threadB_sem, 0, 1);	/* starts off "not available" */
    
    
    /* threadB is a dynamic thread that is spawned by threadA */
    
    void threadB(void *dummy1, void *dummy2, void *dummy3)
    {
    	ARG_UNUSED(dummy1);
    	ARG_UNUSED(dummy2);
    	ARG_UNUSED(dummy3);
    	blink1();
    	/* invoke routine to ping-pong hello messages with threadA */
    	helloLoop(__func__, &threadB_sem, &threadA_sem);
    }
    
    K_THREAD_STACK_DEFINE(threadB_stack_area, STACKSIZE);
    static struct k_thread threadB_data;
    
    /* threadA is a static thread that is spawned automatically */
    
    void threadA(void *dummy1, void *dummy2, void *dummy3)
    {
    	ARG_UNUSED(dummy1);
    	ARG_UNUSED(dummy2);
    	ARG_UNUSED(dummy3);
    
    	/* spawn threadB */
    	k_tid_t tid = k_thread_create(&threadB_data, threadB_stack_area,
    			STACKSIZE, threadB, NULL, NULL, NULL,
    			PRIORITY, 0, K_NO_WAIT);
    	blink2();
    	k_thread_name_set(tid, "thread_b");
    
    	/* invoke routine to ping-pong hello messages with threadB */
    	helloLoop(__func__, &threadA_sem, &threadB_sem);
    }
    
    K_THREAD_DEFINE(thread_a, STACKSIZE, threadA, NULL, NULL, NULL,
    		PRIORITY, 0, K_NO_WAIT);
    

Related