<?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>sample ADC synced with PWM</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/34631/sample-adc-synced-with-pwm</link><description>Hi All, 
 I am newbie with nordic, and I need some help. 
 
 I am using nRF52832 with softdevice. 
 I need to generate a 128 kHz clock signal (PWM pulses with 128kHz frequency, duty cycle 50%,) and to sample Analog input at the same frequency (128 kHz</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 24 May 2018 14:24:10 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/34631/sample-adc-synced-with-pwm" /><item><title>RE: sample ADC synced with PWM</title><link>https://devzone.nordicsemi.com/thread/133285?ContentTypeID=1</link><pubDate>Thu, 24 May 2018 14:24:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d6f13414-8500-4c2e-9018-4185e6401012</guid><dc:creator>AmirZ</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;After reading some documentation and examples, I am trying to connect SAADC with PWM using PPI, but I failed to do that.&lt;/p&gt;
&lt;p&gt;I manage to convert the PWM but faild to trigger the ADC sampling.&lt;/p&gt;
&lt;p&gt;Attached my code to do this,&lt;/p&gt;
&lt;p&gt;please give me some advises or suggestion to correct it.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;void pwm_init(void)&lt;br /&gt;{ &lt;br /&gt; &lt;br /&gt;uint32_t out_pins[] = {18, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED};&lt;br /&gt; static uint16_t pwm_seq[2] = {62, 62};&lt;br /&gt; nrf_pwm_sequence_t const seq = &lt;br /&gt; {&lt;br /&gt; .values.p_common = pwm_seq,&lt;br /&gt; .length = sizeof(pwm_seq)/sizeof(uint16_t),&lt;br /&gt; .repeats = 0,&lt;br /&gt; .end_delay = 0&lt;br /&gt; };&lt;/p&gt;
&lt;p&gt;//nrf_gpio_cfg_output(5);&lt;br /&gt; nrf_pwm_pins_set(NRF_PWM0, out_pins);&lt;br /&gt; nrf_pwm_enable(NRF_PWM0);&lt;br /&gt; nrf_pwm_configure(NRF_PWM0, NRF_PWM_CLK_16MHz, NRF_PWM_MODE_UP, 124);&lt;br /&gt; nrf_pwm_loop_set(NRF_PWM0, 0);&lt;br /&gt; nrf_pwm_decoder_set(NRF_PWM0, NRF_PWM_LOAD_COMMON, NRF_PWM_STEP_AUTO);&lt;/p&gt;
&lt;p&gt;nrf_pwm_sequence_set(NRF_PWM0, 0, &amp;amp;seq);&lt;/p&gt;
&lt;p&gt;NRF_PWM0-&amp;gt;TASKS_SEQSTART[0] = 1;&lt;br /&gt; &lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;void saadc_init(void)&lt;br /&gt;{&lt;br /&gt; ret_code_t err_code;&lt;br /&gt; nrf_saadc_channel_config_t channel_config =&lt;br /&gt; NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(3);&lt;/p&gt;
&lt;p&gt;err_code = nrf_drv_saadc_init(NULL, saadc_callback);&lt;br /&gt; APP_ERROR_CHECK(err_code);&lt;/p&gt;
&lt;p&gt;err_code = nrf_drv_saadc_channel_init(0, &amp;amp;channel_config);&lt;br /&gt; APP_ERROR_CHECK(err_code);&lt;/p&gt;
&lt;p&gt;err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER);&lt;br /&gt; APP_ERROR_CHECK(err_code);&lt;/p&gt;
&lt;p&gt;/*err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAMPLES_IN_BUFFER);&lt;br /&gt; APP_ERROR_CHECK(err_code);*/&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;void saadc_sampling_event_init(void)&lt;br /&gt;{&lt;br /&gt; ret_code_t err_code;&lt;/p&gt;
&lt;p&gt;err_code = nrf_drv_ppi_init();&lt;br /&gt; APP_ERROR_CHECK(err_code);&lt;br /&gt; &lt;br /&gt; &lt;br /&gt; uint32_t pwm_pulse_event_addr = nrf_pwm_task_address_get(NRF_PWM0, NRF_PWM_EVENT_STOPPED );&lt;br /&gt; uint32_t saadc_sample_task_addr = nrf_drv_saadc_sample_task_get();&lt;/p&gt;
&lt;p&gt;#if 1&lt;br /&gt; /* setup ppi channel so that timer compare event is triggering sample task in SAADC */&lt;br /&gt; err_code = nrf_drv_ppi_channel_alloc(&amp;amp;m_ppi_channel);&lt;br /&gt; APP_ERROR_CHECK(err_code);&lt;/p&gt;
&lt;p&gt;err_code = nrf_drv_ppi_channel_assign(m_ppi_channel,&lt;br /&gt; pwm_pulse_event_addr,&lt;br /&gt; saadc_sample_task_addr);&lt;br /&gt; APP_ERROR_CHECK(err_code);&lt;br /&gt; #endif&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;void saadc_sampling_event_enable(void)&lt;br /&gt;{&lt;br /&gt; ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel);&lt;/p&gt;
&lt;p&gt;APP_ERROR_CHECK(err_code);&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;void saadc_callback(nrf_drv_saadc_evt_t const * p_event)&lt;br /&gt;{&lt;br /&gt; if (p_event-&amp;gt;type == NRF_DRV_SAADC_EVT_DONE)&lt;br /&gt; {&lt;br /&gt; ret_code_t err_code;&lt;/p&gt;
&lt;p&gt;err_code = nrf_drv_saadc_buffer_convert(p_event-&amp;gt;data.done.p_buffer, SAMPLES_IN_BUFFER);&lt;br /&gt; APP_ERROR_CHECK(err_code);&lt;/p&gt;
&lt;p&gt;int i;&lt;br /&gt; printf(&amp;quot;ADC event number: %d\r\n&amp;quot;, (int)m_adc_evt_counter);&lt;/p&gt;
&lt;p&gt;for (i = 0; i &amp;lt; SAMPLES_IN_BUFFER; i++)&lt;br /&gt; {&lt;br /&gt; printf(&amp;quot;%d &amp;quot;, p_event-&amp;gt;data.done.p_buffer[i]);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; printf(&amp;quot;\n\r&amp;quot;);&lt;br /&gt; }&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;void main()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;pwm_init();&lt;br /&gt; saadc_init();&lt;br /&gt; saadc_sampling_event_init();&lt;br /&gt; &lt;br /&gt; saadc_sampling_event_enable();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sample ADC synced with PWM</title><link>https://devzone.nordicsemi.com/thread/132802?ContentTypeID=1</link><pubDate>Tue, 22 May 2018 14:18:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7ab0a00d-900e-4e44-a61b-3f1a57303b73</guid><dc:creator>Rune Holmgren</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;It&amp;#39;s possible to do this. It&amp;#39;s a quite specialized use case,&amp;nbsp;so I suspect you will have to interact with hardware registers directly in accordance to the product specifications instead of using the drivers. It&amp;#39;s not particularly hard to do,&amp;nbsp;but requires spending time reading the specifications for the hardware well to understand the implementation. If you set up the PWM peripheral to generate the pulse as well as a PPI event on each pulse you can trigger a measurement by the SAADC&amp;nbsp;at a frequency up to 200kHz. I recommend reading the PPI documentation linked below, but to summize&amp;nbsp;PPI is Nordics system for allowing two hardware peripherals to send events from one device which trigger a task in the other device without involving the CPU. The SAADC&amp;nbsp;running in continuous&amp;nbsp;mode should be used with this PPI channel as the trigger, and data will accumulate for the CPU to handle whenever it is available.&lt;/p&gt;
&lt;p&gt;Be aware that this sample rate is quite high, so you will not be able to continuously send all the data over BLE at the rate you are collecting it.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.nrf52832.ps.v1.1%2Fppi.html&amp;amp;cp=2_1_0_21&amp;amp;anchor=concept_sxf_21l_1s"&gt;PPI documentation&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.nrf52832.ps.v1.1%2Fsaadc.html&amp;amp;cp=2_1_0_36&amp;amp;anchor=saadc_frontpage"&gt;SAADC documentation&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.nrf52832.ps.v1.1%2Fpwm.html&amp;amp;cp=2_1_0_46&amp;amp;anchor=concept_pzc_1pw_nr"&gt;PWM documentation&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Note that using PPI, a timer, and the GPIO might be a suitable alternative to using the PWM peripheral. I recommended the PWM peripheral for its accuracy and flexibility, but it might be overkill to use it.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Rune Holmgren&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>