<?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>Integrate PWM into COAP simple server</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/39919/integrate-pwm-into-coap-simple-server</link><description>Hello, I need to use PWM to control GPIO and integrate it to work on simple COAP server app using nrf52840 DK. 
 I used &amp;quot;pwm_simple&amp;quot; example from SDK (not Thread/Zigbie SDK) and when it was working already as I wanted I tried to integrate that code in</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 07 Nov 2018 15:06:58 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/39919/integrate-pwm-into-coap-simple-server" /><item><title>RE: Integrate PWM into COAP simple server</title><link>https://devzone.nordicsemi.com/thread/156353?ContentTypeID=1</link><pubDate>Wed, 07 Nov 2018 15:06:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8bec085a-bff5-4412-af8d-69ff20c33efe</guid><dc:creator>Bj&amp;#248;rn Kvaale</dc:creator><description>&lt;p&gt;Great! Good to hear that you figured it out!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Integrate PWM into COAP simple server</title><link>https://devzone.nordicsemi.com/thread/156200?ContentTypeID=1</link><pubDate>Tue, 06 Nov 2018 15:26:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4e1a5db3-f6e0-4ef2-94e8-3846bbb00f60</guid><dc:creator>Gregory1992</dc:creator><description>&lt;p&gt;Thanks for your answer. I managed to build it after manually commenting out all the ifdefs in nrf_drv_pwm.c file. It seems that due to some macros in config file, big part of C file was ignored at parsing and therefore function definitions weren&amp;#39;t there.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Integrate PWM into COAP simple server</title><link>https://devzone.nordicsemi.com/thread/156190?ContentTypeID=1</link><pubDate>Tue, 06 Nov 2018 14:27:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2f09fad7-a1d7-4cf1-adce-218a42e2d5b3</guid><dc:creator>Bj&amp;#248;rn Kvaale</dc:creator><description>&lt;p&gt;Sorry for the delayed response. Have you made sure that the header files are included in the path? It seems some header files are missing. I would compare the header files in your example with the PWM driver example in the Thread SDK &amp;amp; see what the difference is.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Integrate PWM into COAP simple server</title><link>https://devzone.nordicsemi.com/thread/155654?ContentTypeID=1</link><pubDate>Thu, 01 Nov 2018 21:43:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8c8c93ad-fb8d-497d-88d9-3b669e7e9140</guid><dc:creator>Gregory1992</dc:creator><description>&lt;p&gt;Thanks for that.&amp;nbsp;I finally commented out this #ifdef macros to force NRFX_PWM0_ENABLED because despite NRFX_PWM0_ENABLED set to 1 in config it would not&amp;nbsp;define &amp;nbsp;NRFX_PWM0_ENABLED, I don&amp;#39;t know why.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;But it seems that it&amp;#39;s not enough as I still get bunch of other errors, like &amp;quot;nrf_drv_pwm.h&amp;quot; is not included at all (undefined references to functions). I modified Makefile to include paths used by PWM driver demo (I made diff on those makefiles). I&amp;#39;m using functions from simple PWM example from this forum and it works flawlessly in directory of PWM driver example of Thread SDK. I tried different things for may hours but basically I always get undefined reference to functions during linking.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;Compiling file: main.c
Linking target: _build/nrf52840_xxaa.out
_build/nrf52840_xxaa/main.c.o: In function `pwm_init&amp;#39;:
C:\Nordic\nRF5_SDK_for_Thread_and_Zigbee_2.0.0_29775ac\examples\thread\simple_coap_server\pca10056\blank\armgcc/../../../main.c:247: undefined reference to `nrfx_pwm_init&amp;#39;
_build/nrf52840_xxaa/main.c.o: In function `pwm_update_duty_cycle&amp;#39;:
C:\Nordic\nRF5_SDK_for_Thread_and_Zigbee_2.0.0_29775ac\examples\thread\simple_coap_server\pca10056\blank\armgcc/../../../main.c:225: undefined reference to `nrfx_pwm_simple_playback&amp;#39;
C:\Nordic\nRF5_SDK_for_Thread_and_Zigbee_2.0.0_29775ac\examples\thread\simple_coap_server\pca10056\blank\armgcc/../../../main.c:225: undefined reference to `nrfx_pwm_simple_playback&amp;#39;
C:\Nordic\nRF5_SDK_for_Thread_and_Zigbee_2.0.0_29775ac\examples\thread\simple_coap_server\pca10056\blank\armgcc/../../../main.c:225: undefined reference to `nrfx_pwm_simple_playback&amp;#39;
collect2.exe: error: ld returned 1 exit status
make: *** [_build/nrf52840_xxaa.out] Error 1
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;This is my app code (main.c from simple COAP server with added PWM functions).&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;
#include &amp;lt;stdint.h&amp;gt;

#include &amp;quot;app_scheduler.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;bsp_thread.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;

#include &amp;quot;thread_coap_utils.h&amp;quot;
#include &amp;quot;thread_utils.h&amp;quot;

#include &amp;lt;openthread/thread.h&amp;gt;

#include &amp;quot;nrf_drv_pwm.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;bsp.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;

#define GPIO_LED_OUT ( NRF_GPIO_PIN_MAP(0,30) )
#define SCHED_QUEUE_SIZE      32                              /**&amp;lt; Maximum number of events in the scheduler queue. */
#define SCHED_EVENT_DATA_SIZE APP_TIMER_SCHED_EVENT_DATA_SIZE /**&amp;lt; Maximum app_scheduler event size. */

/***************************************************************************************************
 * @section Buttons
 **************************************************************************************************/

static void bsp_event_handler(bsp_event_t event)
{
    switch(event)
    {
        case BSP_EVENT_KEY_3:
            thread_coap_utils_provisioning_enable(true);
            break;

        default:
            return;
    }
}

/***************************************************************************************************
 * @section Callbacks
 **************************************************************************************************/

static void thread_state_changed_callback(uint32_t flags, void * p_context)
{
    if (flags &amp;amp; OT_CHANGED_THREAD_ROLE)
    {
        switch (otThreadGetDeviceRole(p_context))
        {
            case OT_DEVICE_ROLE_CHILD:
            case OT_DEVICE_ROLE_ROUTER:
            case OT_DEVICE_ROLE_LEADER:
                break;

            case OT_DEVICE_ROLE_DISABLED:
            case OT_DEVICE_ROLE_DETACHED:
            default:
                thread_coap_utils_provisioning_enable(false);
                break;
        }
    }
    NRF_LOG_INFO(&amp;quot;State changed! Flags: 0x%08x Current role: %d\r\n&amp;quot;,
                 flags,
                 otThreadGetDeviceRole(p_context));

}

/***************************************************************************************************
 * @section Initialization
 **************************************************************************************************/

/**@brief Function for initializing the Application Timer Module.
 */
static void timer_init(void)
{
    uint32_t error_code = app_timer_init();
    APP_ERROR_CHECK(error_code);
}


/**@brief Function for initializing the nrf log module.
 */
static void log_init(void)
{
    ret_code_t err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    NRF_LOG_DEFAULT_BACKENDS_INIT();
}


/**@brief Function for initializing the Thread Board Support Package.
 */
static void thread_bsp_init(void)
{
    uint32_t error_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_event_handler);
    APP_ERROR_CHECK(error_code);

    error_code = bsp_thread_init(thread_ot_instance_get());
    APP_ERROR_CHECK(error_code);
}


/**@brief Function for initializing the Thread Stack.
 */
static void thread_instance_init(void)
{
    thread_configuration_t thread_configuration =
    {
        .role                  = RX_ON_WHEN_IDLE,
        .autocommissioning     = true,
        .poll_period           = 2500,
        .default_child_timeout = 10,
    };

    thread_init(&amp;amp;thread_configuration);
    thread_cli_init();
    thread_state_changed_callback_set(thread_state_changed_callback);
}


/**@brief Function for initializing the Constrained Application Protocol Module.
 */
static void thread_coap_init(void)
{
    thread_coap_configuration_t thread_coap_configuration =
    {
        .coap_server_enabled               = true,
        .coap_client_enabled               = false,
        .coap_cloud_enabled                = false,
        .configurable_led_blinking_enabled = false,
    };

    thread_coap_utils_init(&amp;amp;thread_coap_configuration);
}


/**@brief Function for initializing scheduler module.
 */
static void scheduler_init(void)
{
    APP_SCHED_INIT(SCHED_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE);
}

///////////////////////////////
///////////////////////////////
///////////////////////////////

static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);

// Declare variables holding PWM sequence values. In this example only one channel is used
nrf_pwm_values_individual_t seq_values[] = {{ 0, 0, 0, 0}};
nrf_pwm_sequence_t const seq =
{
    .values.p_individual = seq_values,
    .length          = NRF_PWM_VALUES_LENGTH(seq_values),
    .repeats         = 0,
    .end_delay       = 0
};

// Set duty cycle between 0 and 100%
void pwm_update_duty_cycle(uint8_t duty_cycle)
{
    // Check if value is outside of range. If so, set to 100%
    if(duty_cycle &amp;gt;= 100)
    {
        seq_values-&amp;gt;channel_0 = 100;
    }
    else
    {
        seq_values-&amp;gt;channel_0 = duty_cycle;
    }

    nrf_drv_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;seq, 1, NRF_DRV_PWM_FLAG_LOOP);
}

static void pwm_init(void)
{
    nrf_drv_pwm_config_t const config0 =
    {
        .output_pins =
        {
            GPIO_LED_OUT,						  // channel 0
            NRF_DRV_PWM_PIN_NOT_USED,             // channel 1
            NRF_DRV_PWM_PIN_NOT_USED,             // channel 2
            NRF_DRV_PWM_PIN_NOT_USED,             // channel 3
        },
        .irq_priority = APP_IRQ_PRIORITY_LOWEST,
        .base_clock   = NRF_PWM_CLK_1MHz,
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = 100,
        .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
        .step_mode    = NRF_PWM_STEP_AUTO
    };
    // Init PWM without error handler
    APP_ERROR_CHECK(nrf_drv_pwm_init(&amp;amp;m_pwm0, &amp;amp;config0, NULL));
}

static void ledOutOff(void)
{
	pwm_update_duty_cycle(100); // values has to be inverted
}

static void ledOutDim(void)
{
	pwm_update_duty_cycle(95);
}

static void ledOutOn(void)
{
	pwm_update_duty_cycle(0);
}

int main(int argc, char * argv[])
{
    log_init();
    scheduler_init();
    timer_init();

    // Initialize Thread CoAP utils.
    thread_coap_utils_led_timer_init();
    thread_coap_utils_provisioning_timer_init();

    // Initialize the Thread stack.
    thread_instance_init();
    thread_coap_init();
    thread_bsp_init();

    pwm_init();

    for (;;)
    {
		int sleep_time  = 1000;

		ledOutOff();
		nrf_delay_ms(sleep_time);
		ledOutDim();
		nrf_delay_ms(sleep_time);
		ledOutOn();
		nrf_delay_ms(sleep_time);
    }

//    while (true)
//    {
//        thread_process();
//        app_sched_execute();
//
//        if (NRF_LOG_PROCESS() == false)
//        {
//            thread_sleep();
//        }
//    }
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Integrate PWM into COAP simple server</title><link>https://devzone.nordicsemi.com/thread/154863?ContentTypeID=1</link><pubDate>Mon, 29 Oct 2018 12:46:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:047bbcaf-22a7-4ac6-b1c2-09c66721b1e1</guid><dc:creator>Bj&amp;#248;rn Kvaale</dc:creator><description>&lt;p&gt;I have had a similar problem when I integrated the pwm driver example from the nRF5 SDK into the Bluetooth Mesh light switch example. The important error is this one: &amp;quot;NRFX_PWM0_INST_IDX&amp;#39; undeclared here&amp;quot;. If you search for&amp;nbsp;&lt;span&gt;NRFX_PWM0_INST_IDX, you will most likely find a line similar to this:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#if NRFX_CHECK(NRFX_PWM0_ENABLED)
    NRFX_PWM0_INST_IDX,
#endif&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I am guessing these lines will be grayed out because the NRFX_PWM0_ENABLED has not been defined. In the mesh sdk, this is done via the apply_old_config.h header file, but it might be done in sdk_config.h in the thread sdk.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I would check both of these header files &amp;amp; make sure that&amp;nbsp;NRFX_PWM0_ENABLED is defined &amp;amp; enabled.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Kind Regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Bjørn&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>