<?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>52832 timer and PWM interrupt</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/93294/52832-timer-and-pwm-interrupt</link><description>hi, 
 I want to use timer interrupt to start PWM function, and PWM had PWM interrupt too, hoping every 20ms to start PWM function, but if I open PWM and timer interrupt together, can&amp;#39;t get normal wave, if I stop PWM interrupt, the wave is ok. I don&amp;#39;t</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 31 Oct 2022 11:50:42 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/93294/52832-timer-and-pwm-interrupt" /><item><title>RE: 52832 timer and PWM interrupt</title><link>https://devzone.nordicsemi.com/thread/393211?ContentTypeID=1</link><pubDate>Mon, 31 Oct 2022 11:50:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:24dcb4cf-683e-4316-a0bc-5ff8de63d739</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Where is&amp;nbsp;Count_Tip defined? As&amp;nbsp;it is used in&amp;nbsp;TipHandler but not defined there I assume it is intentionally global but not part of the code you copy-pasted (or if not you will get a warning). This will be incremented by 2 every time the interrupt handler runs and the event is&amp;nbsp;NRFX_PWM_EVT_END_SEQ0, so&amp;nbsp;this will quickly get a high number, and when you subsequently use it to pick an element of the&amp;nbsp;seq_values_Tip array I that will be out of bounds, so the data will be invalid (whatever is there in RAM).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: 52832 timer and PWM interrupt</title><link>https://devzone.nordicsemi.com/thread/392926?ContentTypeID=1</link><pubDate>Fri, 28 Oct 2022 01:12:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1f9c5e4c-58be-4b7d-bfa9-96e1c071f558</guid><dc:creator>K-night</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static void PWM_init(void)
{

	seq_values_Tip0[0] = seq_values_Tip[Count_Tip];
	seq_values_Tip1[0] = seq_values_Tip[Count_Tip+1];
	
	seq_values_Ring0[0] = seq_values_Ring[Count_Ring];  
	seq_values_Ring1[0] = seq_values_Ring[Count_Ring+1];  
	
	nrf_drv_pwm_config_t const configTip0 =
	{
			.output_pins =
			{
					BSP_LED_1 | NRF_DRV_PWM_PIN_INVERTED, // 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_16MHz,
			.count_mode   = NRF_PWM_MODE_UP,
			.top_value    = 0,												
			.load_mode    = NRF_PWM_LOAD_WAVE_FORM,
			.step_mode    = NRF_PWM_STEP_AUTO
	};
	nrf_drv_pwm_config_t const configRing0 =
	{
			.output_pins =
			{
					BSP_LED_0 | NRF_DRV_PWM_PIN_INVERTED, // 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_16MHz,
			.count_mode   = NRF_PWM_MODE_UP,
			.top_value    = 0,												
			.load_mode    = NRF_PWM_LOAD_WAVE_FORM,
			.step_mode    = NRF_PWM_STEP_AUTO
	};
	
	ReleasePWM();

//    APP_ERROR_CHECK(nrf_drv_pwm_init(&amp;amp;m_pwm0, &amp;amp;configTip0, NULL));
//    APP_ERROR_CHECK(nrf_drv_pwm_init(&amp;amp;m_pwm1, &amp;amp;configRing0, NULL));
//		
	APP_ERROR_CHECK(nrf_drv_pwm_init(&amp;amp;m_pwm0, &amp;amp;configTip0, TipHandler));
	APP_ERROR_CHECK(nrf_drv_pwm_init(&amp;amp;m_pwm1, &amp;amp;configRing0, RingHandler));

	
	m_used |= USED_PWM(0);
	m_used |= USED_PWM(1);
	
	nrf_pwm_sequence_t const seq_Tip0 =
	{
			.values.p_wave_form = seq_values_Tip0,
			.length          = NRF_PWM_VALUES_LENGTH(seq_values_Tip0),
			.repeats         = number_Tip[Count_Tip]-1,
			.end_delay       = 0
	};
	
	 nrf_pwm_sequence_t const seq_Tip1 =
	{
			.values.p_wave_form = seq_values_Tip1,
			.length          = NRF_PWM_VALUES_LENGTH(seq_values_Tip1),
			.repeats         = number_Tip[Count_Tip+1]-1,
			.end_delay       = 0
	};
		
	nrf_pwm_sequence_t const seq_Ring0 =
	{
			.values.p_wave_form = seq_values_Ring0,
			.length          = NRF_PWM_VALUES_LENGTH(seq_values_Ring0),
			.repeats         = number_Ring[Count_Ring]-1,
			.end_delay       = 0
	};
	
	nrf_pwm_sequence_t const seq_Ring1 =
	{
			.values.p_wave_form = seq_values_Ring1,
			.length          = NRF_PWM_VALUES_LENGTH(seq_values_Ring1),
			.repeats         = number_Ring[Count_Ring+1]-1,
			.end_delay       = 0
	};

	uint32_t start_pwm0_task_addr = nrf_drv_pwm_complex_playback(&amp;amp;m_pwm0, &amp;amp;seq_Tip0, &amp;amp;seq_Tip1, (sizeof(number_Tip)/sizeof(number_Tip[0])), NRFX_PWM_FLAG_STOP | NRF_DRV_PWM_FLAG_START_VIA_TASK|NRF_DRV_PWM_FLAG_SIGNAL_END_SEQ0 | NRF_DRV_PWM_FLAG_SIGNAL_END_SEQ1);               
	uint32_t *start_pwm0_task = (uint32_t *) start_pwm0_task_addr;     
	uint32_t start_pwm1_task_addr = nrf_drv_pwm_complex_playback(&amp;amp;m_pwm1, &amp;amp;seq_Ring0, &amp;amp;seq_Ring1, (sizeof(number_Ring)/sizeof(number_Ring[0])), NRFX_PWM_FLAG_STOP | NRF_DRV_PWM_FLAG_START_VIA_TASK|NRF_DRV_PWM_FLAG_SIGNAL_END_SEQ0 | NRF_DRV_PWM_FLAG_SIGNAL_END_SEQ1);               
   	uint32_t *start_pwm1_task = (uint32_t *) start_pwm1_task_addr;     
	*start_pwm0_task = 1;   
	*start_pwm1_task = 1;	

}

static void timer0_init(void)
{
	uint32_t time_ms = 20; //Time(in miliseconds) between consecutive compare events.
    uint32_t time_ticks;
    uint32_t err_code = NRF_SUCCESS;
	
		NRF_CLOCK-&amp;gt;TASKS_HFCLKSTART = 1; // For more accurate timing. //start high speed clock

    //Configure TIMER_LED for generating simple light effect - leds on board will invert his state one after the other.
    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    err_code = nrf_drv_timer_init(&amp;amp;TIMER_LED, &amp;amp;timer_cfg, timer_led_event_handler);
    APP_ERROR_CHECK(err_code);

    time_ticks = nrf_drv_timer_ms_to_ticks(&amp;amp;TIMER_LED, time_ms);  //time_ticks = (times*16000)/512 = 31250

    nrf_drv_timer_extended_compare(
         &amp;amp;TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);

    nrf_drv_timer_enable(&amp;amp;TIMER_LED);

}

int main(void)
{ 
	timer0_init();
    while (1)
    {
        //__WFI();
			
			 // Wait for an event.
        __WFE();

        // Clear the event register.
        __SEV();
        __WFE();
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;this is PWM, timer0 init code and main code&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: 52832 timer and PWM interrupt</title><link>https://devzone.nordicsemi.com/thread/392925?ContentTypeID=1</link><pubDate>Fri, 28 Oct 2022 01:03:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:89bbd3d8-9e54-4b20-b1a8-a90d44981015</guid><dc:creator>K-night</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;nrf_pwm_values_wave_form_t seq_values_Tip[] =
	{
		{ 0x8000+320,      0,      0,      640 }, 

		{ 0x8000,      0,      0,      16 },

		{ 0x8000+200,      0,      0,      400 },  
				
		{ 0x8000,      0,      0,      1600 },
		
	};
	
int number_Tip[]={32,61,40,114};
nrf_pwm_values_wave_form_t /*const*/ seq_values_Tip0[1];
nrf_pwm_values_wave_form_t /*const*/ seq_values_Tip1[1];


static void TipHandler(nrfx_pwm_evt_type_t event_type)
{
	
	if (event_type == NRFX_PWM_EVT_END_SEQ0)
	{	
		Count_Tip = Count_Tip+2;
		seq_values_Tip0[0] = seq_values_Tip[Count_Tip];// add this code, wave is abnormal
		NRF_PWM0-&amp;gt;SEQ[0].REFRESH = number_Tip[Count_Tip]-1;
	}
	if (event_type == NRFX_PWM_EVT_END_SEQ1)
	{	
		seq_values_Tip1[0] = seq_values_Tip[Count_Tip+1];// add this code, wave is abnormal
		NRF_PWM0-&amp;gt;SEQ[1].REFRESH = number_Tip[Count_Tip+1]-1;
	}

}

		
		
		&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;please see my code, I debug yesterday, found if I open PWM interrupt though, but if I add &amp;quot;seq_values_Tip0[0] = seq_values_Tip[Count_Tip];&amp;quot; code in the PWM interrupt, the wave is abnormal, if remove it, we can get normal wave, so I think maybe that code affected my program.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: 52832 timer and PWM interrupt</title><link>https://devzone.nordicsemi.com/thread/392840?ContentTypeID=1</link><pubDate>Thu, 27 Oct 2022 12:59:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ecef3a70-ebbe-4965-a781-0918c7e433c4</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I am not able to explain this based on the information here, and generally it should not matter if you initialize PWM from an event handler (though that makes me wonder if you re-init it or perhaps something else you did not expect is happening)?. Perhaps you can elaborate a bit, bot about how&amp;nbsp;things are failing, and perhaps more importantly add a bitt more code to provide context.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>