<?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>Thread with callback</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/71868/thread-with-callback</link><description>Hello, 
 I have just started in the world of Nordic products and quite recently in the embedded world. I&amp;#39;m trying to learn and understand the use of zephyr in basic projects. 
 In this one I use two threads, one that makes the led blink and the other</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 05 Mar 2021 08:51:00 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/71868/thread-with-callback" /><item><title>RE: Thread with callback</title><link>https://devzone.nordicsemi.com/thread/297975?ContentTypeID=1</link><pubDate>Fri, 05 Mar 2021 08:51:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a51505c1-de33-4b88-b922-6f62af5025f9</guid><dc:creator>ActiTAG</dc:creator><description>&lt;p&gt;Hello Mr. Richard,&lt;/p&gt;
&lt;p&gt;I use an external programmer.&amp;nbsp;The&amp;nbsp;&lt;span&gt;nRF Connect SDK&amp;nbsp;version is 1.4.2 and the Thingy version 1.4.0.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanks a lot ! I remove&amp;nbsp;CONFIG_DK_LIBRARY_INVERT_BUTTONS=n and it&amp;#39;s working ! Thank you !&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Thread with callback</title><link>https://devzone.nordicsemi.com/thread/297870?ContentTypeID=1</link><pubDate>Thu, 04 Mar 2021 15:34:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:70f694ab-e911-4935-9d35-f49cb80c352f</guid><dc:creator>Carl Richard</dc:creator><description>&lt;p&gt;Hi again!&lt;br /&gt;&lt;br /&gt;I did some testing on my Thingy:91 and&amp;nbsp;your project worked fine, though I had to remove&amp;nbsp;CONFIG_DK_LIBRARY_INVERT_BUTTONS=n and add an extra configuration for the SPM. Can you try the attached hex file and see if that works for you?&lt;br /&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/thingy91_5F00_threads.hex"&gt;devzone.nordicsemi.com/.../thingy91_5F00_threads.hex&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In addition I&amp;#39;m wondering:&lt;br /&gt;- are you programming the Thingy using MCUBoot or an external programmer?&amp;nbsp;&lt;br /&gt;- What version of the nRF Connect SDK are you using?&lt;br /&gt;- Which version of the Thingy:91 do you have?&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve also attached the project I&amp;#39;m using, for your reference.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/thingy91_5F00_threads.zip"&gt;devzone.nordicsemi.com/.../thingy91_5F00_threads.zip&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Carl Richard&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Thread with callback</title><link>https://devzone.nordicsemi.com/thread/297808?ContentTypeID=1</link><pubDate>Thu, 04 Mar 2021 13:11:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4211e378-3520-47fb-a54c-a8dfe189e708</guid><dc:creator>ActiTAG</dc:creator><description>&lt;p&gt;Hello, It&amp;#39;s change nothing to build in non-secure. I don&amp;#39;t find something to help me on this problem. &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f61e.svg" title="Disappointed"&gt;&amp;#x1f61e;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Thread with callback</title><link>https://devzone.nordicsemi.com/thread/296936?ContentTypeID=1</link><pubDate>Mon, 01 Mar 2021 15:31:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:267b599f-c73b-4872-9422-31fcaa51a22d</guid><dc:creator>Carl Richard</dc:creator><description>&lt;p&gt;Hi again!&lt;br /&gt;&lt;br /&gt;I was not aware that you were working with the Thingy:91. That may require some other configurations and approaches. However, I see that you are building for the thingy91_nrf9160 board. The nRF9160 is designed to have applications running on the non-secure partition, so could you try to build for thingy91_nrf9160ns instead and see if you application runs properly then?&lt;br /&gt;&lt;br /&gt;I will get back to you on further considerations regarding the Thingy:91.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Carl Richard&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Thread with callback</title><link>https://devzone.nordicsemi.com/thread/296761?ContentTypeID=1</link><pubDate>Mon, 01 Mar 2021 08:45:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4da55bde-3073-47b7-8432-3ce176f3776c</guid><dc:creator>ActiTAG</dc:creator><description>&lt;p&gt;Yes with pleasure! Thank you&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/SENSORS.rar"&gt;devzone.nordicsemi.com/.../SENSORS.rar&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Thread with callback</title><link>https://devzone.nordicsemi.com/thread/296756?ContentTypeID=1</link><pubDate>Mon, 01 Mar 2021 08:09:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6ee7a4c6-2f81-416c-93bb-48ec552454c5</guid><dc:creator>Carl Richard</dc:creator><description>&lt;p&gt;Understood. What is your prj.conf? Could you send archive and share your project directory here so that I can do some testing?&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Carl Richard&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Thread with callback</title><link>https://devzone.nordicsemi.com/thread/296750?ContentTypeID=1</link><pubDate>Mon, 01 Mar 2021 07:57:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b5d39c01-7585-4bb0-b06d-2863717a3db3</guid><dc:creator>ActiTAG</dc:creator><description>&lt;p&gt;Hello, I don&amp;#39;t see any mistake. The lib uses LOG but I can&amp;#39;t find any trace of it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Thread with callback</title><link>https://devzone.nordicsemi.com/thread/296567?ContentTypeID=1</link><pubDate>Fri, 26 Feb 2021 13:38:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:834b63fc-5bfc-446a-b540-a42ac5b34c57</guid><dc:creator>Carl Richard</dc:creator><description>&lt;p&gt;Good to hear! Does the logging give you any errors? I would also recommend trying to debug this using Segger Embedded Studio and see what the code actually does.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Carl Richard&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Thread with callback</title><link>https://devzone.nordicsemi.com/thread/296537?ContentTypeID=1</link><pubDate>Fri, 26 Feb 2021 12:45:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bdf98be7-5cba-491a-b524-e57ede70e8d6</guid><dc:creator>ActiTAG</dc:creator><description>&lt;p&gt;Oh ok thanks it&amp;#39;s clearer! despite everything nothing happens when I press the button :x&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Thread with callback</title><link>https://devzone.nordicsemi.com/thread/296513?ContentTypeID=1</link><pubDate>Fri, 26 Feb 2021 11:51:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2727d137-d0d6-4a8f-bd21-8e9eb97217cb</guid><dc:creator>Carl Richard</dc:creator><description>&lt;p&gt;Hi again.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;When using the DK buttons and LEDs library you must use the correct callback definition. The handles all the GPIO interaction so you don&amp;#39;t need to address that directly.&amp;nbsp; See the attached code, which is a modified version of what you posted earlier.&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;/*
 * Copyright (c) 2017 Linaro Limited
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;device.h&amp;gt;
#include &amp;lt;devicetree.h&amp;gt;
#include &amp;lt;drivers/gpio.h&amp;gt;
#include &amp;lt;sys/util.h&amp;gt;
#include &amp;lt;sys/printk.h&amp;gt;
#include &amp;lt;inttypes.h&amp;gt;
#include &amp;lt;drivers/pwm.h&amp;gt;
#include &amp;lt;sys/__assert.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;dk_buttons_and_leds.h&amp;gt;

#include &amp;quot;buzzer.h&amp;quot;
/* size of stack area used by each thread */
#define STACKSIZE 1024

/* scheduling priority used by each thread */
#define PRIORITY 7

#define SLEEP_MS 500
/*alias*/
#define LED0_NODE DT_ALIAS(led2)
#define SW0_NODE DT_ALIAS(sw0)

/*FIFO*/
struct printk_data_t {
	void *fifo_reserved; /* 1st word reserved for use by fifo */
	uint32_t conp;
};

K_FIFO_DEFINE(printk_fifo);

void print(uint32_t id){
    struct printk_data_t tx_data = { .conp = id };
    size_t size = sizeof(struct printk_data_t);
    char *mem_ptr = k_malloc(size);
    __ASSERT_NO_MSG(mem_ptr != 0);

    memcpy(mem_ptr, &amp;amp;tx_data, size);

    k_fifo_put(&amp;amp;printk_fifo, mem_ptr);

    k_msleep(SLEEP_MS);
    
}


/**@brief Callback for button events from the DK buttons and LEDs library. */
static void button_handler(uint32_t button_states, uint32_t has_changed) {
        print(1);
        ui_buzzer_set_frequency(100, 20);
        k_msleep(SLEEP_MS);
        ui_buzzer_set_frequency(0, 0);
        
	if (has_changed &amp;amp; button_states &amp;amp; DK_BTN1_MSK) {
		printk(&amp;quot;DEV_DBG: button 1 pressed\n&amp;quot;);
	}

	else if (has_changed &amp;amp; button_states &amp;amp; DK_BTN2_MSK) {
		printk(&amp;quot;DEV_DBG: button 2 pressed\n&amp;quot;);
	}        
}

void blink()
{
	int cnt = 0;
	int ret;
        
    /*led*/
	int err;

	err = dk_leds_init();
	if (err) {
		printk(&amp;quot;Could not initialize leds, err code: %d\n&amp;quot;, err);
	}

	err = dk_set_leds_state(1, DK_ALL_LEDS_MSK);
	if (err) {
		printk(&amp;quot;Could not set leds state, err code: %d\n&amp;quot;, err);
	}

	err = dk_buttons_init(button_handler);
	if (err) {
		printk(&amp;quot;Could not initialize leds, err code: %d\n&amp;quot;, err);
	}

    /*buzzer*/
    err = ui_buzzer_init();
	if (err) {
		printk(&amp;quot;Could not enable buzzer, err code: %d\n&amp;quot;, err);
		return err;
	}

	while (1) {
		err = dk_set_leds_state(cnt % 2, DK_ALL_LEDS_MSK);
                if (err) {
                        printk(&amp;quot;Could not set leds state, err code: %d\n&amp;quot;, err);
                }
                if (cnt % 2) {
                         print(0);
                }
                k_msleep(SLEEP_MS);
                cnt++;              
	}
}


void uart_out(void)
{
	while (1) {
		struct printk_data_t *rx_data = k_fifo_get(&amp;amp;printk_fifo,
							   K_FOREVER);
		printk(&amp;quot;[Info] conponent %d turn on \n&amp;quot;,
		       rx_data-&amp;gt;conp);
		k_free(rx_data);
	}
}



K_THREAD_DEFINE(blink_id, STACKSIZE, blink, NULL, NULL, NULL,
		PRIORITY, 0, 0);
K_THREAD_DEFINE(uart_out_id, STACKSIZE, uart_out, NULL, NULL, NULL,
		PRIORITY, 0, 0);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Notice two important changes in the code.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;1. The button handler callback is changed to the following:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;/**@brief Callback for button events from the DK buttons and LEDs library. */
static void button_handler(uint32_t button_states, uint32_t has_changed) {
        print(1);
        ui_buzzer_set_frequency(100, 20);
        k_msleep(SLEEP_MS);
        ui_buzzer_set_frequency(0, 0);
        
	if (has_changed &amp;amp; button_states &amp;amp; DK_BTN1_MSK) {
		printk(&amp;quot;DEV_DBG: button 1 pressed\n&amp;quot;);
	}

	else if (has_changed &amp;amp; button_states &amp;amp; DK_BTN2_MSK) {
		printk(&amp;quot;DEV_DBG: button 2 pressed\n&amp;quot;);
	}        
}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;2. The buttons are initialized directly with the callback function as a parameter:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;err = dk_buttons_init(button_handler);
if (err) {
    printk(&amp;quot;Could not initialize leds, err code: %d\n&amp;quot;, err);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I hope this is more clear to you now.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Carl Richard&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Thread with callback</title><link>https://devzone.nordicsemi.com/thread/295993?ContentTypeID=1</link><pubDate>Wed, 24 Feb 2021 11:06:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:62499a21-13e0-46ed-83bf-fed8e7907671</guid><dc:creator>ActiTAG</dc:creator><description>&lt;p&gt;Hello Mr Richard,&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve tried it by myself but I must admit that I have trouble understanding the structure to pass for the button to work. Do you have any advice to help me please?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/0243.main.c"&gt;devzone.nordicsemi.com/.../0243.main.c&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Thread with callback</title><link>https://devzone.nordicsemi.com/thread/295642?ContentTypeID=1</link><pubDate>Mon, 22 Feb 2021 14:44:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0e4d5938-f582-47e7-bc7c-8954312c13e5</guid><dc:creator>Carl Richard</dc:creator><description>&lt;p&gt;Great. I will try to come up with an example for you tomorrow. In the meantime I believe you will find the relevant information in the source code of the library I spoke of earlier. Here the buttons are tied to a callback called&amp;nbsp;&lt;a href="https://github.com/nrfconnect/sdk-nrf/blob/73c461e3aba1820cdf6950fe99a94bafb329d403/lib/dk_buttons_and_leds/dk_buttons_and_leds.c#L214"&gt;button_pressed&lt;/a&gt;, when that callback is triggered it does two things:&lt;br /&gt;1. Calls &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/reference/kernel/smp/smp.html#spinlocks"&gt;k_spin_lock()&lt;/a&gt;&amp;nbsp;to prevent further interrupts&lt;br /&gt;2. Disables&amp;nbsp;further callbacks calling the &lt;a href="https://github.com/nrfconnect/sdk-nrf/blob/73c461e3aba1820cdf6950fe99a94bafb329d403/lib/dk_buttons_and_leds/dk_buttons_and_leds.c#L78"&gt;callback_ctrl&lt;/a&gt; function&lt;/p&gt;
&lt;p&gt;After this it handles the actual button press before unlocking the spinlock and enables the callbacks again. If you do this for your single button it should work properly. I suggest that you define the button device globally so it can be accessed from the callback and callback_ctrl.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Carl Richard&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Thread with callback</title><link>https://devzone.nordicsemi.com/thread/295619?ContentTypeID=1</link><pubDate>Mon, 22 Feb 2021 14:10:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f1893788-923a-4b2f-aabe-28901eddbace</guid><dc:creator>ActiTAG</dc:creator><description>&lt;p&gt;&lt;span&gt;Thank you for your response. I followed your first advice to put the button in the same thread as the led. But for your 2. I don&amp;#39;t see how to do it, would you have some explanation and an example please ?&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Thread with callback</title><link>https://devzone.nordicsemi.com/thread/295550?ContentTypeID=1</link><pubDate>Mon, 22 Feb 2021 11:32:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:64024f4e-742f-4923-8ee7-4f5680db65a0</guid><dc:creator>Carl Richard</dc:creator><description>&lt;p&gt;Hi!&lt;br /&gt;&lt;br /&gt;Thanks for reaching out. I suggest that you look into how buttons are handled in the &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/include/dk_buttons_and_leds.html#dk-button-and-leds"&gt;DK Buttons and LEDs library&lt;/a&gt;. Note especially that:&lt;br /&gt;1. Buttons don&amp;#39;t need a thread per say, as they work through interrupts and callbacks.&lt;br /&gt;2. When a button is pressed the interrupt should be disabled while the callback is handled. Otherwise you will get an assert like this:&amp;nbsp;&lt;strong&gt;ASSERTION FAIL [!arch_is_in_isr()] @ WEST_TOPDIR/zephyr/kernel/sched.c:1306&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;As for the LED thread not working the eternal while loop at the end of your button thread holds the CPU hostage from running the other threads. The button and callback is initialized anyway, so you don&amp;#39;t need to have the &amp;quot;while&amp;quot; there.&lt;br /&gt;&lt;br /&gt;Hope this helps you on your way!&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Carl Richard&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>