<?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>Produce PWM sequence in NCS v2.9.1 using nrfx driver on nRF52840</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/120943/produce-pwm-sequence-in-ncs-v2-9-1-using-nrfx-driver-on-nrf52840</link><description>Hi, 
 I want to produce a pulse with 4 seconds length from p0.25, but i am failed. 
 
 
 #include &amp;lt;zephyr/sys/printk.h&amp;gt; 
 #include &amp;lt;nrfx_pwm.h&amp;gt; 
 #include &amp;lt;hal/nrf_gpio.h&amp;gt; 
 
 
 
 static nrfx_pwm_t m_pwm = NRFX_PWM_INSTANCE ( 0 ); 
 static volatile uint8_t</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 29 Apr 2025 13:48:05 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/120943/produce-pwm-sequence-in-ncs-v2-9-1-using-nrfx-driver-on-nrf52840" /><item><title>RE: Produce PWM sequence in NCS v2.9.1 using nrfx driver on nRF52840</title><link>https://devzone.nordicsemi.com/thread/533421?ContentTypeID=1</link><pubDate>Tue, 29 Apr 2025 13:48:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:28b12702-89ad-4425-89b3-79168e4f3857</guid><dc:creator>always18</dc:creator><description>&lt;p&gt;&lt;span data-slate-fragment="JTVCJTdCJTIydHlwZSUyMiUzQSUyMnBhcmFncmFwaCUyMiUyQyUyMmNoaWxkcmVuJTIyJTNBJTVCJTdCJTIyaWQlMjIlM0ElMjJ6bERKbU40UVh6JTIyJTJDJTIycGFyYUlkeCUyMiUzQTAlMkMlMjJzcmMlMjIlM0ElMjJ0aGFua3MlMjBhJTIwbG90JTIyJTJDJTIyZHN0JTIyJTNBJTIyJUU4JUIwJUEyJUU4JUIwJUEyJTIyJTJDJTIybWV0YWRhdGElMjIlM0ElMjIlMjIlMkMlMjJtYXRjaGVzJTIyJTNBbnVsbCUyQyUyMm1ldGFEYXRhJTIyJTNBJTVCJTVEJTJDJTIydGV4dCUyMiUzQSUyMnRoYW5rcyUyMGElMjBsb3QlMjIlN0QlNUQlN0QlNUQ="&gt;thanks a lot,according to &lt;a href="https://github.com/zephyrproject-rtos/hal_nordic/tree/13ac55b5b52c905642e9c54f069109d188aa5840/nrfx/samples/src/nrfx_pwm/grouped_mode"&gt;the sample&lt;/a&gt; I solved it&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Produce PWM sequence in NCS v2.9.1 using nrfx driver on nRF52840</title><link>https://devzone.nordicsemi.com/thread/533160?ContentTypeID=1</link><pubDate>Mon, 28 Apr 2025 11:05:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e88b4166-d9b1-43c5-bb13-30bfdacb97d9</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The finished event comes after the first secuence, so you are effectively starting the new before the previous has completed, and this fails. The driver lacks a good way to solve this with this approach, so I woudl instead suggest that you alternate between sequeces as shown in &lt;a href="https://github.com/zephyrproject-rtos/hal_nordic/blob/13ac55b5b52c905642e9c54f069109d188aa5840/nrfx/samples/src/nrfx_pwm/grouped_mode"&gt;this sample&lt;/a&gt; (and update in between). This also has the benefit that interrupt latency does not matter that much, as you prepare for the switch before it actually happens.&lt;/p&gt;
&lt;p&gt;PS: there were some issues with this test code that prevented it from running that was easily catched with asserts, so I on a generic basis recommend testing with asserts (CONFIG_ASSERT=y). With that addressed, I the modified code I tested looks like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;zephyr/sys/printk.h&amp;gt;
#include &amp;lt;nrfx_pwm.h&amp;gt;
#include &amp;lt;hal/nrf_gpio.h&amp;gt;
#include &amp;lt;zephyr/kernel.h&amp;gt;

static nrfx_pwm_t m_pwm = NRFX_PWM_INSTANCE(0);
static volatile uint8_t current_seq = 0;
#define motor_pwr_ctrl      NRF_GPIO_PIN_MAP(0, 25)

// PWM sequence
nrf_pwm_values_individual_t seq_data[4] = {
    {.channel_0 = 250},   // 20% duty (250/1250)
    {.channel_0 = 625},   // 50% duty
    {.channel_0 = 875},   // 70% duty
    {.channel_0 = 1125}   // 90% duty
};

static nrf_pwm_sequence_t sequences[4] = {
    {   // sequence0
        .values.p_individual = &amp;amp;seq_data[0],
        .length = NRF_PWM_VALUES_LENGTH(seq_data[0]),
        .repeats = 0, 
        .end_delay = 0
    },
    {   // sequence1
        .values.p_individual = &amp;amp;seq_data[1],
        .length = NRF_PWM_VALUES_LENGTH(seq_data[1]),
        .repeats = 0,
        .end_delay = 0
    },
    {   // sequence2
        .values.p_individual = &amp;amp;seq_data[2],
        .length = NRF_PWM_VALUES_LENGTH(seq_data[2]),
        .repeats = 0,
        .end_delay = 0
    },
    {   // sequence3
        .values.p_individual = &amp;amp;seq_data[3],
        .length = NRF_PWM_VALUES_LENGTH(seq_data[3]),
        .repeats = 0,
        .end_delay = 0
    }
};
// PWM handler
static void pwm_handler(nrfx_pwm_evt_type_t event_type, void* pContext)
{
    static int count = 0;
    if (event_type != 0) {
        printk(&amp;quot;count: %i, evt: %i\n&amp;quot;, count, event_type);
    }
    if (event_type == NRFX_PWM_EVT_FINISHED) {
    	current_seq++;
        if (current_seq &amp;lt; 4) {
	    printk(&amp;quot;pwm_handler,current_seq=%d.\n&amp;quot;, current_seq);
            nrfx_pwm_simple_playback(&amp;amp;m_pwm, &amp;amp;sequences[current_seq], 100, NRFX_PWM_FLAG_STOP);//repeat 100 times =1 second
        } else {
            //stop and relese pwm
            nrfx_pwm_stop(&amp;amp;m_pwm, false);
            nrfx_pwm_uninit(&amp;amp;m_pwm);
            printk(&amp;quot;nrfx_pwm_uninit.\n&amp;quot;);
        }
    }
}
// PWM initial
void init_pwm(void)
{
    nrfx_pwm_config_t config = {
        .output_pins = {
            motor_pwr_ctrl, // channel 0
        },
        .irq_priority = NRFX_PWM_DEFAULT_CONFIG_IRQ_PRIORITY,
        .base_clock = NRF_PWM_CLK_125kHz,
        .count_mode = NRF_PWM_MODE_UP,
        .top_value = 1250,           // 10ms (1250/125kHz)
        .load_mode = NRF_PWM_LOAD_INDIVIDUAL,
        .step_mode = NRF_PWM_STEP_AUTO
    };

    nrfx_pwm_init(&amp;amp;m_pwm, &amp;amp;config, (nrfx_pwm_handler_t)pwm_handler, NULL);
    current_seq = 0;

    //play the sequence0 
    nrfx_pwm_simple_playback(&amp;amp;m_pwm, &amp;amp;sequences[0], 100, NRFX_PWM_FLAG_STOP); //repeat 100 times =1 second
}
void PWM_Go(void)
{
#if defined(__ZEPHYR__)
    IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_PWM0), IRQ_PRIO_LOWEST, pwm_handler, 0, 0);
#endif
    init_pwm();
}
int main(void)
{
    PWM_Go();
    while (1) {
        k_sleep(K_FOREVER); 
    }

    return 0;
}
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Produce PWM sequence in NCS v2.9.1 using nrfx driver on nRF52840</title><link>https://devzone.nordicsemi.com/thread/532819?ContentTypeID=1</link><pubDate>Thu, 24 Apr 2025 12:17:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f2f48dfe-cb6a-4853-999f-4cb825fc4f15</guid><dc:creator>always18</dc:creator><description>&lt;p&gt;No,&amp;nbsp;&amp;nbsp;From the log you can see&amp;nbsp;&lt;span&gt;nrfx_pwm_simple_playback&lt;/span&gt;&amp;nbsp;is called for four times,&lt;/p&gt;
&lt;p&gt;p0.25 output &lt;span&gt;sequences[[0] for 100 times only,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;But&amp;nbsp;it don`t output sequences[[1~3]&amp;nbsp;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1745496980684v1.png" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Produce PWM sequence in NCS v2.9.1 using nrfx driver on nRF52840</title><link>https://devzone.nordicsemi.com/thread/532809?ContentTypeID=1</link><pubDate>Thu, 24 Apr 2025 11:47:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5cd98d26-8323-4595-9687-f09ef2fb7b0a</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I do not imediately see the problem as you play the sequence 100 times, and that works. Based on that and taht you get 1 second, it seems&amp;nbsp;nrfx_pwm_simple_playback() is only called once from the init functions, but not from the pwm_handler()? Can you confirm by logging or debugging?&lt;/p&gt;
&lt;p&gt;PS: There are samples here that can be used as a reference:&amp;nbsp;&lt;a href="https://github.com/zephyrproject-rtos/hal_nordic/blob/13ac55b5b52c905642e9c54f069109d188aa5840/nrfx/samples/src/nrfx_pwm"&gt;https://github.com/zephyrproject-rtos/hal_nordic/blob/13ac55b5b52c905642e9c54f069109d188aa5840/nrfx/samples/src/nrfx_pwm&lt;/a&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Produce PWM sequence in NCS v2.9.1 using nrfx driver on nRF52840</title><link>https://devzone.nordicsemi.com/thread/532750?ContentTypeID=1</link><pubDate>Thu, 24 Apr 2025 08:37:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5c8b227f-bab0-4ad9-b3f4-2eaeed538815</guid><dc:creator>always18</dc:creator><description>&lt;p&gt;The actual pulse is 1second length with&amp;nbsp;&lt;span&gt;seq_data[0].&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>