Hello,
I'm using nRF5 SDK with version 15 and nRF52832 IC. I want to sample some data with 3200Hz sampling rate. I can set the timer minimum 1ms Which means 1000Hz. How can I adjust the Timer for microSecond values.
Best Regards
Hello,
I'm using nRF5 SDK with version 15 and nRF52832 IC. I want to sample some data with 3200Hz sampling rate. I can set the timer minimum 1ms Which means 1000Hz. How can I adjust the Timer for microSecond values.
Best Regards
I assume you are referring to the app timer. The app timer uses ISRs to accomplish its task. There is no realistic way to execute microsecond ISRs on a 64MHz processor.
If you need to sample data at microsecond intervals you will need to use ppi/gpiote to execute that and likely DMA to handle the data.
Can you give me an example code about How to do it with ppi/gpiote ?
I am also using the SoftDevice.
I can't give you anything very specific since all applications are unique. Also, your requirement of sampling some data is a little vague.
However here is one link on configuring ppi/gpiote: https://devzone.nordicsemi.com/f/nordic-q-a/24283/measuring-interrupt-interval-with-app_timer/95578#95578
There is plenty of info on the ppi/gpiote in the product spec. The example above uses the registers. Nordic has a driver for it, but I rarely use drivers since most of our code has very specific requirement.
Basically you need to create a timer that runs at 3200Hz then have the timer drive your data sampling. At this point I don't know what you are sampling. If it sits on gpio then send the task endpoint to gpio to assert your external module. If instead you need to have the nRF do something, then use the event from the timer to create an ISR. You will have to be careful on the ISR. Again this is a 64MHz processor, so you really can't process many opcodes at a 3200Hz interval. Also, you won't be able to run the softdevice since the latency from the softdevice can be as much as 800usec to 1.2msec.
I wrote a code sample but the LED is toggling in 25ms which is the TIMER_LED period.
#define PPI_EXAMPLE_TIMER2_INTERVAL (100) static const nrf_drv_timer_t m_timer2 = NRF_DRV_TIMER_INSTANCE(2); static nrf_ppi_channel_t m_ppi_channel2; static void ppi_init(void) { uint32_t err_code = NRF_SUCCESS; err_code = nrf_drv_ppi_init(); APP_ERROR_CHECK(err_code); /* Configure 2nd available PPI channel to start TIMER0 counter at TIMER2 COMPARE[0] match, * which is every odd number of seconds. */ err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel2); APP_ERROR_CHECK(err_code); err_code = nrf_drv_ppi_channel_assign(m_ppi_channel2, nrf_drv_timer_event_address_get(&m_timer2, NRF_TIMER_EVENT_COMPARE0), nrf_drv_timer_task_address_get(&TIMER_LED, NRF_TIMER_TASK_START)); APP_ERROR_CHECK(err_code); // Enable both configured PPI channels err_code = nrf_drv_ppi_channel_enable(m_ppi_channel2); APP_ERROR_CHECK(err_code); } static void timer2_event_handler(nrf_timer_event_t event_type, void * p_context) { nrf_gpio_pin_toggle(19); } static void TimerInit2(void) { // Check TIMER2 configuration for details. nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG; timer_cfg.frequency = NRF_TIMER_FREQ_31250Hz; ret_code_t err_code = nrf_drv_timer_init(&m_timer2, &timer_cfg, timer2_event_handler); APP_ERROR_CHECK(err_code); nrf_drv_timer_extended_compare(&m_timer2, NRF_TIMER_CC_CHANNEL0, nrf_drv_timer_us_to_ticks(&m_timer2, PPI_EXAMPLE_TIMER2_INTERVAL), NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true); }
I do not see a reference in your code for timer_led. You should post the entire main.c
Plus it would be better for you to start with my example. Your approach requires the gpio driver and an ISR to work. Both of these require much more code execution.
I can only share some parts of my code
void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context) { switch (event_type) { case NRF_TIMER_EVENT_COMPARE0: { nrf_gpio_pin_toggle(17); } break; default: //Do nothing. break; } } static void TimerInit1(void) { nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG; err_code = nrf_drv_timer_init(&TIMER_LED, &timer_cfg, timer_led_event_handler); APP_ERROR_CHECK(err_code); time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_LED, time_ms); nrf_drv_timer_extended_compare(&TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true); nrf_drv_timer_enable(&TIMER_LED); }
I can only share some parts of my code
void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context) { switch (event_type) { case NRF_TIMER_EVENT_COMPARE0: { nrf_gpio_pin_toggle(17); } break; default: //Do nothing. break; } } static void TimerInit1(void) { nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG; err_code = nrf_drv_timer_init(&TIMER_LED, &timer_cfg, timer_led_event_handler); APP_ERROR_CHECK(err_code); time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_LED, time_ms); nrf_drv_timer_extended_compare(&TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true); nrf_drv_timer_enable(&TIMER_LED); }
Are you using the soft device?
Yes, I am using the SoftDevice.
Well no ISR approach will work for you anyway when using the SD. As I pointed out above, you cannot do this with an ISR while using the SD. Please re-read my prior response.
Since you can only do it completely in hardware ie, ppi/gpiote there will not likely be an example specific to your needs. Also, you will have to give a lot more information about the source and type of data for anyone to help.
I want to sample some data with 3200Hz sampling rate from SPI interface. There is an accelerometer connected via SPI interface.
How do I need to configure the ppi ?
Much better and specific! You should repost this using the question you just gave "I want to sample some data with 3200Hz sampling rate from SPI interface. There is an accelerometer connected via SPI interface." add to it this "I am also using the softdevice."
I have done a lot with ppi/gpiote but I haven't used SPI for anything on this device and wouldn't want to waste your time.
According to the devzone there is support for SPI with DMA and the data transfers can be triggered by timer events so I think you are in good shape to solve it. Essentially the SPI/DMA transfer will happen in hardware behind the scenes and you will just use the processor on the nRF to pick up an array from memory at a much slower pace. It can handle the data in slower chunks but not at 3200Hz.