<?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>Faster nrf53 PWM frequency update through Zephyr?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/78461/faster-nrf53-pwm-frequency-update-through-zephyr</link><description>Hello! I am a beginner at nrf programming and have just started using the NCS and Zephyr on an nrf5340 evaluation board. I understand that Zephyr seems to be more or less a prerequisite right now when programming the nrf53 but I may have a problem. I</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sat, 14 Aug 2021 13:03:55 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/78461/faster-nrf53-pwm-frequency-update-through-zephyr" /><item><title>RE: Faster nrf53 PWM frequency update through Zephyr?</title><link>https://devzone.nordicsemi.com/thread/324929?ContentTypeID=1</link><pubDate>Sat, 14 Aug 2021 13:03:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a624da37-bf7d-4e0a-8e32-9ca35947b93c</guid><dc:creator>JonasForssell</dc:creator><description>&lt;p&gt;I am going to close this ticket now since the original question was answered. The issue with being able to change frequency remains and I will open a new ticket around this.&lt;br /&gt;&lt;br /&gt;Many thanks to J&amp;ouml;rg and Torbj&amp;ouml;rn for your kind help!&lt;/p&gt;
&lt;p&gt;/Jonas&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Faster nrf53 PWM frequency update through Zephyr?</title><link>https://devzone.nordicsemi.com/thread/324861?ContentTypeID=1</link><pubDate>Fri, 13 Aug 2021 12:54:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d63b47ba-e79c-446b-8c06-6331a43f9e81</guid><dc:creator>JonasForssell</dc:creator><description>&lt;p&gt;Encouraged by my earlier success, I tried to make the driver run in wave_form mode in order to have the possibility of updating the top_value dynamically and thereby changing the frequency.&lt;br /&gt;&lt;br /&gt;Starting off with a simple single setup, I do not however get the expected output.&lt;/p&gt;
&lt;p&gt;The fourth value (supposed to be countertop) is ignored for some reson and the default value seems to be around 1000 which results in a 1kHz signal. With a 50 on the duty cycle position I get expected 5% duty. I have confirmed that I can change the duty cycle as expected but the countertop value is ignored.&lt;br /&gt;&lt;br /&gt;Any idea what I have got wrong?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/**
 * Simple program to drive LED0 on the nrf5340 evaluation board using the nrfx driver for lower level access
 * Jonas Forssell
 */

#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;nrfx_pwm.h&amp;gt;
#include &amp;lt;zephyr.h&amp;gt;

// Use Zephyr macro to get access to the LED0 on the board
#define OUTPUT_PIN DT_GPIO_PIN(DT_ALIAS(led0), gpios)

// Use nrfx to create a PWM instance which we will connect to the LED0 later
static nrfx_pwm_t m_pwm0 = NRFX_PWM_INSTANCE(0);

// Declare variables holding PWM sequence values. In this example only one channel is used 
// We start off with a counter to 50 and a top value of 100 giving a 50% duty cyle.
nrf_pwm_values_wave_form_t seq_values[] = {50, 0, 0, 100};

nrf_pwm_sequence_t const seq =
{
    .values.p_wave_form = seq_values,
    .length          = NRF_PWM_VALUES_LENGTH(seq_values),
    .repeats         = 0,
    .end_delay       = 0
};


nrfx_pwm_config_t const config0 =
    {
        .output_pins =
        {
            OUTPUT_PIN,                        // channel 0 now connected to LED0
            NRFX_PWM_PIN_NOT_USED,             // channel 1
            NRFX_PWM_PIN_NOT_USED,             // channel 2
            NRFX_PWM_PIN_NOT_USED,             // channel 3
        },
        .irq_priority = NRFX_PWM_DEFAULT_CONFIG_IRQ_PRIORITY,
        .base_clock   = NRF_PWM_CLK_1MHz,       // Here we select which prescaler to use
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = 100,                    // This is ignored when in wave form mode
        .load_mode    = NRF_PWM_LOAD_WAVE_FORM,
        .step_mode    = NRF_PWM_STEP_AUTO
    };

int main(void) {

// Init PWM without error handler
nrfx_pwm_init(&amp;amp;m_pwm0, &amp;amp;config0, NULL, NULL);

// Set and start
nrfx_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;seq, 1, NRFX_PWM_FLAG_LOOP);

while(1) 
    k_msleep(3000);

}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Faster nrf53 PWM frequency update through Zephyr?</title><link>https://devzone.nordicsemi.com/thread/324775?ContentTypeID=1</link><pubDate>Fri, 13 Aug 2021 08:12:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b4b195f1-6981-4de3-80a6-03d4c47aeadd</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Jonas&lt;/p&gt;
&lt;p&gt;Good to hear you got it working &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Older examples written for the nRF5 SDK will require some porting, since the older nrf_drv drivers are all replaced by nrfx drivers in Zephyr.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Faster nrf53 PWM frequency update through Zephyr?</title><link>https://devzone.nordicsemi.com/thread/324682?ContentTypeID=1</link><pubDate>Thu, 12 Aug 2021 13:34:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ab00375b-1417-404f-abf4-e2f7eececa30</guid><dc:creator>JonasForssell</dc:creator><description>&lt;p&gt;Based on your feedback Torbj&amp;ouml;rn and J&amp;ouml;rg, I have messed about for a while trying to create a working PWM example where I use the nrfx_pwm drivers instead.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I was quite amazed to get this to work. I based it on a simple-pwm example I found for the nrf_pwm driver but needed to tweak it a bit. Mostly by replacing nrf references with corresponding nrfx equivalents.&lt;/p&gt;
&lt;p&gt;I needed to use Zephyr to get a reference to the LED0 from the devicetree but then it compiled and worked.&lt;/p&gt;
&lt;p&gt;Thanks for your help gentlemen!&lt;/p&gt;
&lt;p&gt;/Jonas&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/**
 * Simple program to drive LED0 on the nrf5340 evaluation board using the nrfx driver for lower level access
 * Jonas Forssell
 */

#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;nrfx_pwm.h&amp;gt;
#include &amp;lt;zephyr.h&amp;gt;


// Use Zephyr macro to get access to the LED0 on the board
#define OUTPUT_PIN DT_GPIO_PIN(DT_ALIAS(led0), gpios)

// Use nrfx to create a PWM instance which we will connect to the LED0 later
static nrfx_pwm_t m_pwm0 = NRFX_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;
    }
    
    nrfx_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;seq, 1, NRFX_PWM_FLAG_LOOP);
}

static void pwm_init(void)
{
    nrfx_pwm_config_t const config0 =
    {
        .output_pins =
        {
            OUTPUT_PIN,                        // channel 0 now connected to LED0
            NRFX_PWM_PIN_NOT_USED,             // channel 1
            NRFX_PWM_PIN_NOT_USED,             // channel 2
            NRFX_PWM_PIN_NOT_USED,             // channel 3
        },
        .irq_priority = NRFX_PWM_DEFAULT_CONFIG_IRQ_PRIORITY,
        .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
    nrfx_pwm_init(&amp;amp;m_pwm0, &amp;amp;config0, NULL, NULL);
    
}


int main(void)
{

    // Start clock for accurate frequencies
    NRF_CLOCK-&amp;gt;TASKS_HFCLKSTART = 1; 
    // Wait for clock to start
    while(NRF_CLOCK-&amp;gt;EVENTS_HFCLKSTARTED == 0) 
        ;
    
    pwm_init();

    for (;;)
    {
        for(int i = 0; i &amp;lt;= 100; i++)
        {
            k_msleep(10);
            pwm_update_duty_cycle(i);
        }
    }
}


/** @} */&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Faster nrf53 PWM frequency update through Zephyr?</title><link>https://devzone.nordicsemi.com/thread/324614?ContentTypeID=1</link><pubDate>Thu, 12 Aug 2021 09:02:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:88953675-08ba-4466-b29c-c8c96bac051e</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Jonas&lt;/p&gt;
&lt;p&gt;I wouldn&amp;#39;t recommend programming the hardware directly, but it is possible to bypass the Zephyr PWM driver and use the nrfx_pwm driver instead. Then you are much closer to the hardware, but you don&amp;#39;t need to spend a lot of time understanding how the hardware works.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;As Jörg is hinting at it is possible to write directly to the RAM buffer assigned to the PWM, rather than starting the PWM explicitly each time you want to change it.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;When using the nrfx_pwm driver you have to provide the buffer to the driver, which means you have access to it in the application and can update it without involving the driver.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The idea of connecting the ADC to the same buffer is interesting, but this is not something we have tested. Also, the drawback of this method is that you are not able to do any scaling, inversion or anything else that you might want to do to the data before you output it on the PWM.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Faster nrf53 PWM frequency update through Zephyr?</title><link>https://devzone.nordicsemi.com/thread/324612?ContentTypeID=1</link><pubDate>Thu, 12 Aug 2021 09:01:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a36252d9-d4e0-412d-8d04-5584cd580676</guid><dc:creator>Turbo J</dc:creator><description>&lt;p&gt;What I meant by &amp;quot;programming the hardware directly&amp;quot; was reading the PS document where all those SAADC and PWM registers are documented, and then translating that information directly into C code.&lt;/p&gt;
&lt;p&gt;And yes, that ignores Zephyr and nrfx (almost) completely - both won&amp;#39;t allow you enough control over the hardware IMHO.&lt;/p&gt;
&lt;p&gt;Otherwise I don&amp;#39;t see how to implement you application.&lt;/p&gt;
&lt;p&gt;This is actually quite a common theme in &amp;micro;C programming whenever you code something &lt;em&gt;interesting&lt;/em&gt; that the original developers (of the APIs and Zephyr) did not anticipate before.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Faster nrf53 PWM frequency update through Zephyr?</title><link>https://devzone.nordicsemi.com/thread/324607?ContentTypeID=1</link><pubDate>Thu, 12 Aug 2021 08:31:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bbc1767a-a622-4224-8890-db128c12442e</guid><dc:creator>JonasForssell</dc:creator><description>&lt;p&gt;I&amp;#39;ve been studying a bit more to try to comprehend the limitations. Here are my conclusions. They might be wrong:&lt;/p&gt;
&lt;p&gt;1. I have not yet found any info on how Zephyr actually implements PWM but the frequency update limitation mensioned above is still valid.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;2. nrf53 development are based around Zephyr and the possibility of using the nrf5 SDK is not available.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;3. However, nrfx API can be activated (still have to learn how) and seems to support nrf53. In this case, I suspect some Zephyr bits should be deactivated to avoid conflict.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;4. To get high performance PWM (&amp;gt;200 kHz), I need to activate and use the PWM peripheral in the nrf53 (using nrfx commands)&lt;/p&gt;
&lt;p&gt;Examples and documentation I have found so far:&lt;br /&gt;&lt;a href="https://docs.zephyrproject.org/latest/samples/boards/nrf/nrfx/README.html"&gt;https://docs.zephyrproject.org/latest/samples/boards/nrf/nrfx/README.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/69264/outputting-a-pwm-signal-and-it-s-inverse-thru-gpio-pins"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/69264/outputting-a-pwm-signal-and-it-s-inverse-thru-gpio-pins&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;So far, I have not found an example where the nrfx PWM is implemented and Zephyr is deactivated.&lt;/p&gt;
&lt;p&gt;I would greatly appreciate any help and guidance on this topic.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;/Jonas&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Faster nrf53 PWM frequency update through Zephyr?</title><link>https://devzone.nordicsemi.com/thread/324382?ContentTypeID=1</link><pubDate>Wed, 11 Aug 2021 07:43:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e8ee672e-0323-4248-8e55-10d698e7a51d</guid><dc:creator>JonasForssell</dc:creator><description>&lt;p&gt;Thanks for quick feedback Turbo J!!&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve tried experimenting with different pulse values down to ns and although it idoes affect the PWM signal, it does not change the update timing unfortunately.&lt;br /&gt;&lt;br /&gt;Your tip regarding the PWM and SAADC seems really interesting. Any tips on where I can read further on this? Perhaps examples?&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
&lt;p&gt;/Jonas&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Faster nrf53 PWM frequency update through Zephyr?</title><link>https://devzone.nordicsemi.com/thread/324368?ContentTypeID=1</link><pubDate>Wed, 11 Aug 2021 07:08:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1e6d754d-0256-4e5b-950a-8ae2a699b960</guid><dc:creator>Turbo J</dc:creator><description>&lt;p&gt;Your PWM period time of 100000 is in the ms range, and you cannot set a new value &lt;em&gt;during&lt;/em&gt; that period. See PS for details.&lt;/p&gt;
&lt;p&gt;What you could do - IMHO not in zephyr, but by programming the hardware directly - is using the same buffer for the SAADC result and the PWM input. This way the PWM can pick up a newly sampled ADC value for each period.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>