<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>nrf9160 DK thread not running</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/58657/nrf9160-dk-thread-not-running</link><description>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</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 06 Mar 2020 10:36:04 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/58657/nrf9160-dk-thread-not-running" /><item><title>RE: nrf9160 DK thread not running</title><link>https://devzone.nordicsemi.com/thread/238493?ContentTypeID=1</link><pubDate>Fri, 06 Mar 2020 10:36:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ede72e9e-4681-4757-93bd-abde1d76380e</guid><dc:creator>Martin Lesund</dc:creator><description>&lt;p&gt;Hi Praveen,&lt;/p&gt;
&lt;p&gt;Yes, there is.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Please use this&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="https://github.com/NordicPlayground/fw-nrfconnect-zephyr/tree/master/samples/synchronization" rel="noopener noreferrer" target="_blank"&gt;Synchronization sample&lt;/a&gt;&amp;nbsp;as a reference on how you can make Threads and how you can synchronize.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve made a version based on the said sample that achieves what you are trying to do:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/* main.c - Hello World demo */

/*
 * Copyright (c) 2012-2014 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;sys/printk.h&amp;gt;
#include &amp;lt;device.h&amp;gt;
#include &amp;lt;drivers/gpio.h&amp;gt;

/*
 * 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&amp;#39;s own semaphore
 * @param other_sem    other thread&amp;#39;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 &amp;quot;hello&amp;quot; */
		tname = k_thread_name_get(k_current_get());
		if (tname == NULL) {
			printk(&amp;quot;%s: Hello World from %s!\n&amp;quot;,
				my_name, CONFIG_BOARD);
		} else {
			printk(&amp;quot;%s: Hello World from %s!\n&amp;quot;,
				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 &amp;quot;available&amp;quot; */
K_SEM_DEFINE(threadB_sem, 0, 1);	/* starts off &amp;quot;not available&amp;quot; */


/* 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__, &amp;amp;threadB_sem, &amp;amp;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(&amp;amp;threadB_data, threadB_stack_area,
			STACKSIZE, threadB, NULL, NULL, NULL,
			PRIORITY, 0, K_NO_WAIT);
	blink2();
	k_thread_name_set(tid, &amp;quot;thread_b&amp;quot;);

	/* invoke routine to ping-pong hello messages with threadB */
	helloLoop(__func__, &amp;amp;threadA_sem, &amp;amp;threadB_sem);
}

K_THREAD_DEFINE(thread_a, STACKSIZE, threadA, NULL, NULL, NULL,
		PRIORITY, 0, K_NO_WAIT);
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf9160 DK thread not running</title><link>https://devzone.nordicsemi.com/thread/238455?ContentTypeID=1</link><pubDate>Fri, 06 Mar 2020 09:25:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1e7855fc-c8c6-4548-ba9f-75c279814c1a</guid><dc:creator>praveenpalaparthi</dc:creator><description>&lt;p&gt;Hi Martin,&lt;/p&gt;
&lt;p&gt;Anything wrong in my example code?&lt;/p&gt;
&lt;p&gt;Please suggest me&lt;/p&gt;
&lt;p&gt;regards,&lt;/p&gt;
&lt;p&gt;Praveen Palaparthi&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf9160 DK thread not running</title><link>https://devzone.nordicsemi.com/thread/238022?ContentTypeID=1</link><pubDate>Wed, 04 Mar 2020 12:35:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:88f4c023-5fb0-4d2f-9b8c-caa1acd72c50</guid><dc:creator>Martin Lesund</dc:creator><description>&lt;p&gt;Hi Praveen,&lt;br /&gt;Please check the documentation for how to &amp;quot;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/reference/kernel/threads/index.html#spawning-a-thread" rel="noopener noreferrer" target="_blank"&gt;Spawn a Thread&lt;/a&gt;&amp;quot;.&lt;br /&gt;There you see that the thread is created &lt;span style="text-decoration:underline;"&gt;outside&lt;/span&gt; the main() function.&lt;br /&gt;&lt;br /&gt;Here is a suggestion on how you could set up your &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/reference/kernel/threads/system_threads.html#writing-a-main-function" rel="noopener noreferrer" target="_blank"&gt;main() function.&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf9160 DK thread not running</title><link>https://devzone.nordicsemi.com/thread/237983?ContentTypeID=1</link><pubDate>Wed, 04 Mar 2020 10:08:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:149dc4ba-0e86-4a91-ad96-7a6b21ce942a</guid><dc:creator>praveenpalaparthi</dc:creator><description>&lt;p&gt;Hi Martin,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thank you very much for your response.&lt;/p&gt;
&lt;p&gt;If I create thread in run time by using K_THREAD_DEFINE I am able to turn ON led&amp;#39;s alternatively.&lt;/p&gt;
&lt;p&gt;I am not able to create thread if I&amp;nbsp;create in&amp;nbsp;main() thread \ function as&amp;nbsp;done in my code example by calling k_thread_create() function.&lt;/p&gt;
&lt;p&gt;In my application, we will have one main() function in which we do initialization and create task/threads and one infinite loop as shown below.&lt;/p&gt;
&lt;p&gt;void main(void)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;hellip;&amp;hellip;&lt;/p&gt;
&lt;p&gt;&amp;hellip;..&lt;/p&gt;
&lt;p&gt;initialization of peripherals();&lt;/p&gt;
&lt;p&gt;&amp;hellip;...&lt;/p&gt;
&lt;p&gt;&amp;hellip;..&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;create task/threads1&lt;/p&gt;
&lt;p&gt;create task/threads2&lt;/p&gt;
&lt;p&gt;create task/threads3&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;while(1);&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;My question is cant we create a thread in void main (void) function? If yes how to create thread?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf9160 DK thread not running</title><link>https://devzone.nordicsemi.com/thread/237965?ContentTypeID=1</link><pubDate>Wed, 04 Mar 2020 09:29:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:42fcba2a-270b-45bb-bf10-893bbf24edec</guid><dc:creator>Martin Lesund</dc:creator><description>&lt;p&gt;Hi Praveen,&lt;/p&gt;
&lt;p&gt;There is a generic sample [&lt;a href="https://github.com/NordicPlayground/fw-nrfconnect-zephyr/tree/master/samples/basic/threads" rel="noopener noreferrer" target="_blank"&gt;Basic Thread Example&lt;/a&gt;] on how you can achieve exactly this in the zephyr repo.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If you change the sleep interval in&amp;nbsp;&lt;a href="https://github.com/NordicPlayground/fw-nrfconnect-zephyr/blob/master/samples/basic/threads/src/main.c#L74" rel="noopener noreferrer" target="_blank"&gt;Blink1 &lt;/a&gt;and &lt;a href="https://github.com/NordicPlayground/fw-nrfconnect-zephyr/blob/master/samples/basic/threads/src/main.c#L79" rel="noopener noreferrer" target="_blank"&gt;Blink2&lt;/a&gt;&amp;nbsp;to match:&lt;/p&gt;
&lt;p&gt;e.g.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void blink1(void)
{
	blink(PORT0, 400, LED0, 0);
}

void blink2(void)
{
	blink(PORT1, 400, LED1, 1);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;and change the &lt;a href="https://github.com/NordicPlayground/fw-nrfconnect-zephyr/blob/master/samples/basic/threads/src/main.c#L91" rel="noopener noreferrer" target="_blank"&gt;initialization of one of the Threads&lt;/a&gt; to have the same delay so the LEDs can alternate.&lt;/p&gt;
&lt;p&gt;e.g.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;K_THREAD_DEFINE(blink1_id, STACKSIZE, blink1, NULL, NULL, NULL,
		PRIORITY, 0, 400);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Please check out the documentation for &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/reference/kernel/threads/index.html" rel="noopener noreferrer" target="_blank"&gt;Threads&lt;/a&gt;&amp;nbsp;and &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/reference/kernel/scheduling/index.html" rel="noopener noreferrer" target="_blank"&gt;Scheduling&amp;nbsp;&lt;/a&gt;for more details and information on how to use this.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Martin L.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>