This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

I want example timer + spi (SDK 14.2.0)

Hi, all.

I m trying to combine two example spi + timer using nRF52832.

But spi_event_handler function in the ex spi and timer event in the ex timer are crashed.  

If I insert source code "nrf_drv_timer_enable(&TIMER_TEST);", program is died.

I want to result this problem.

Can you help me?

source code is as below.

//const nrf_drv_timer_t TIMER_TEST = NRF_DRV_TIMER_INSTANCE(0);

/**
 * @brief SPI user event handler.
 * @param event
 */
void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
                       void *                    p_context)
{
    spi_xfer_done = true;
   
//    NRF_LOG_INFO("Transfer completed.");
//    if (m_rx_buf[0] != 0)
//    {
//        NRF_LOG_INFO(" Received:");
//        NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
//    }  
}


///**
 * @brief Handler for timer events.
 */
void timer_event_handler(nrf_timer_event_t event_type, void* p_context)
{
}

int main(void)
{
    
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();
   
    NRF_LOG_INFO("SPI example.");

    nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
    spi_config.ss_pin   = SPI_SS_PIN;
    spi_config.miso_pin = SPI_MISO_PIN;
    spi_config.mosi_pin = SPI_MOSI_PIN;
    spi_config.sck_pin  = SPI_SCK_PIN;
    APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
   
    
    
    // Timer
    uint32_t time_ms = 1000; //Time(in miliseconds) between consecutive compare events.
    uint32_t time_ticks;
    uint32_t err_code = NRF_SUCCESS;

    //Configure TIMER_BMS 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(&TIMER_TEST, &timer_cfg, timer_event_handler);
    APP_ERROR_CHECK(err_code);

    time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_TEST, time_ms);

    nrf_drv_timer_extended_compare(
         &TIMER_BMS, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);

    nrf_drv_timer_enable(&TIMER_TEST);
   
    while (1)
    {
        spi_xfer_done = false;

        __WFE();
        __WFI();
       

       // nrf_drv_spi_transfer

              
       
        NRF_LOG_FLUSH();
    }
}

Parents
  • Follow these steps:

    • Put in all the timer related code into your SPI example. Like functions, variable declarations and macros.
    • Add #include "nrf_drv_timer.h" at the top of your SPI example
    • If you're going to implement the SoftDevice later on you should use timer1 or timer2, not timer0
    • Add all the timer related configurations to sdk_config.h
      • In the sdk_config.h file of examples\peripheral\timer, copy the defines belonging to "//<e> TIMER_ENABLED - nrf_drv_timer - TIMER periperal driver - legacy layer"
    • Add <..>\modules\nrfx\drivers\include\nrfx_timer.h to the path
    • Add the source file of the nrfx driver to project explorer (\modules\nrfx\drivers\src\nrfx_timer.c)

    I've provided an example down below.

    Best regards,

    Simon

    spi.rar

Reply
  • Follow these steps:

    • Put in all the timer related code into your SPI example. Like functions, variable declarations and macros.
    • Add #include "nrf_drv_timer.h" at the top of your SPI example
    • If you're going to implement the SoftDevice later on you should use timer1 or timer2, not timer0
    • Add all the timer related configurations to sdk_config.h
      • In the sdk_config.h file of examples\peripheral\timer, copy the defines belonging to "//<e> TIMER_ENABLED - nrf_drv_timer - TIMER periperal driver - legacy layer"
    • Add <..>\modules\nrfx\drivers\include\nrfx_timer.h to the path
    • Add the source file of the nrfx driver to project explorer (\modules\nrfx\drivers\src\nrfx_timer.c)

    I've provided an example down below.

    Best regards,

    Simon

    spi.rar

Children
  • Hi, Simon.

    Thank you for answer.

    I resolved to combine two example(spi + timer).

    It is working well.

    But there are strange point.

    I have used TIMER 4 now.

    If I enabled TIMER4 single, It is not working. But when I enabled two timer, it is working well.

    or, when #define TIMER4 4, working well (Not #define TIMER4 1)

    I think it is strange.

    I m wating for your answer.

    This is timer code in sdk_config.h. (example spi base)

    --- When working.

    // <q> TIMER0_ENABLED  - Enable TIMER0 instance
     

    #ifndef TIMER0_ENABLED
    #define TIMER0_ENABLED 1     
    #endif

    // <q> TIMER1_ENABLED  - Enable TIMER1 instance
     

    #ifndef TIMER1_ENABLED
    #define TIMER1_ENABLED 0
    #endif

    // <q> TIMER2_ENABLED  - Enable TIMER2 instance
     

    #ifndef TIMER2_ENABLED
    #define TIMER2_ENABLED 0
    #endif

    // <q> TIMER3_ENABLED  - Enable TIMER3 instance
     

    #ifndef TIMER3_ENABLED
    #define TIMER3_ENABLED 0
    #endif

    // <q> TIMER4_ENABLED  - Enable TIMER4 instance
     

    #ifndef TIMER4_ENABLED
    #define TIMER4_ENABLED 1  (or #define TIMER0_ENABLED 0, #define TIMER4_ENABLED 4)
    #endif

    // </e>

    --- When not working.

    // <q> TIMER0_ENABLED  - Enable TIMER0 instance
     

    #ifndef TIMER0_ENABLED
    #define TIMER0_ENABLED 0  
    #endif

    // <q> TIMER1_ENABLED  - Enable TIMER1 instance
     

    #ifndef TIMER1_ENABLED
    #define TIMER1_ENABLED 0
    #endif

    // <q> TIMER2_ENABLED  - Enable TIMER2 instance
     

    #ifndef TIMER2_ENABLED
    #define TIMER2_ENABLED 0
    #endif

    // <q> TIMER3_ENABLED  - Enable TIMER3 instance
     

    #ifndef TIMER3_ENABLED
    #define TIMER3_ENABLED 0
    #endif

    // <q> TIMER4_ENABLED  - Enable TIMER4 instance
     

    #ifndef TIMER4_ENABLED
    #define TIMER4_ENABLED 1 
    #endif

    // </e>

    Sincerely yours

    Eric

  • Hello.

    When I insert RTT command, program is not working.

    attach code as below.

    Thank you

    Eric.

    spi + timer.zip

  • I tested your code and I was able to use RTT logging without any problems. Could you point to what specifically goes wrong?

    Best regards,

    Simon

Related