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

twi_handler doent work after wake up

Hi everybody,

I'm developing an product with :

NRF52840, V0.9.0, SDK15

In the DK I connected LIS3DH (accelerometer by TWI)

all works.I configuration of HZ and temperature.

even I can read the sensor. (the twi_handler works fine)

but I included a code of NFC wake up (power on, sleep)

and when the NFC wake up the MCU, I what to read again the accelerometer.

in this moment the TWI RX or TX doesnt call the twi_handler. and the program stuck in    while (m_xfer_done == false);

I have tested disable Twi before to WFE, and enable it when wake up,...  but it doesnt work..

Could you give me some ideas about what happen?

thanks 

Ricardo

void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
  switch (p_event->type)
    {
        case NRF_DRV_TWI_EVT_DONE:       
            if (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX)
            {
                data_handler(m_sample);
            }
            m_xfer_done = true;
             break;
        default:
            break;
    }
}

static void read_sensor_Vibrometer()          //                                      *********************** LECTURA DE SENSOR VIBRACION *********************
{
    ret_code_t err_code;
    m_xfer_done = false; 

    //  ****** send a request accelerometer values ****
    
    uint8_t reg[1] = {LIS3DH_GET_ACC};
    err_code = nrf_drv_twi_tx(&m_twi, LIS3DH_WRITE, reg, sizeof(reg), false);
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);
    m_xfer_done = false; 
    
  
     //  ****** read the values ****
    
    APP_ERROR_CHECK(err_code);
    err_code = nrf_drv_twi_rx(&m_twi, LIS3DH_READ, recepcion, 6);    //  ret_code_t err_code;
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);
    m_xfer_done = false; 

}

  • Are you calling read_sensor_Vibrometer from the NFC interrupt handler? If so, which interrupt priority have you given to the TWI and NFC peripheral when you initialized them? It could sound like there is an issue with interrupt priorities. You could try to lower the NFC IRQ priority.

  • Hi bjorn

    Thanks for your answer

    No I dont Calling read_sensor_Vibrometer from NFC handler..

    the main is the follow.

    int main(void)
    {   twi_init();  
        LIS3DH_Turn_on();
        LIS3DH_Temperature();
        ciclomedicion();
        while (true)
        {
          Prepare_NFC_Wake_up(); // here if i enable NFC dindt affect in the ciclomedicion()
          ciclomedicion();
        
          if (m_system_off_mode_on)
            {
             NRF_LOG_INFO(" system go to SLEEP MODE \r\n .");
             NRF_LOG_FLUSH();
             nrf_delay_ms(1000);
             NRF_LOG_INFO("sleep on \r\n .");
             NRF_LOG_FLUSH();
         
             m_system_off_mode_on = false;
             Prepare_NFC_Wake_up(); // here is the original place to activate NFC
                      
             while (true) 
             
             { __WFE();  } 
        }    
    }         

    And the Function Of NFC are the following

    static void Prepare_NFC_Wake_up(void)
    {             
              //Enable interrupt for FIELDDETECT Event
              NRF_NFCT->INTENSET = NFCT_INTENSET_FIELDDETECTED_Enabled << NFCT_INTENSET_FIELDDETECTED_Pos;
            
              // Setting Grouping priority 
              //NVIC_SetPriorityGrouping(1);
              
              //Register interrupt for NFCT module
              NVIC_EnableIRQ(NFCT_IRQn);  //  NFCT_IRQn
          
              //Start sensing 
              NRF_NFCT->TASKS_SENSE = 1;
                   
    }
     
    static void Prepare_NFC_Turn_off(void)
    {             
              //Enable interrupt for FIELDDETECT Event
              //NRF_NFCT->INTENSET = NFCT_INTENSET_FIELDDETECTED_Enabled << NFCT_INTENSET_FIELDDETECTED_Pos;
        
              //Register interrupt for NFCT module
              NVIC_DisableIRQ(NFCT_IRQn);  //  NFCT_IRQn
          
              //Start sensing 
              NRF_NFCT->TASKS_SENSE = 0;
    }
    
    // This IRQ handler will trigger when NFC field is detected
    void NFCT_IRQHandler(void)
    {
      if (NRF_NFCT->EVENTS_FIELDDETECTED == 1)
      {
        Prepare_NFC_Turn_off();
        NRF_NFCT->EVENTS_FIELDDETECTED = 0;
    
        NRF_LOG_INFO(" ***************  TURN ON BY NFC  ***************\r\n");
        NRF_LOG_FLUSH();
        
        ciclomedicion();
        
        Prepare_NFC_Wake_up();
      }
    }

    the function ciclomedicion() is a routine take measure accelerometer and temperature. Both works well even when NFC is turn ON (prepare_NFC_wake_up)

    as I can realized the problem is the __WFE it routine doing something with the handler.(handler of TWI or handler of NRF_LOG_FLUSH)

    when I turn on with NFC, the routine NFCT_IRQHandler  is start.

    I added the following code to check the start again of the program..

    NRF_LOG_INFO(" *************** TURN ON BY NFC ***************\r\n");
    NRF_LOG_FLUSH();

    but the program is stuck here in the while cycle

    static void serial_tx(void const * p_context, char const * p_buffer, size_t len)
    {
        uint8_t len8 = (uint8_t)(len & 0x000000FF);
        m_xfer_done = false;
        ret_code_t err_code = nrf_drv_uart_tx(&m_uart, (uint8_t *)p_buffer, len8);
        APP_ERROR_CHECK(err_code);
        /* wait for completion since buffer is reused*/
        while (m_async_mode && (m_xfer_done == false))  **** HERE THE PROGRAM STUCK ****
        {
    
        }
    
    }

    if I erased the 

    NRF_LOG_INFO(" *************** TURN ON BY NFC ***************\r\n");
    NRF_LOG_FLUSH();

    the program Stuck in the  while cycle of the read_sensor_Vibrometer...  (read_sensor_Vibrometer is inside of ciclomedicion()

     

    I hope you have all the information...

    thanks in advance..

    Ricardo

  • HI.

    I add the Twi_ini

    void twi_init (void)
    {
        ret_code_t err_code;
    
        const nrf_drv_twi_config_t twi_lm75b_config = {
           .scl                = ARDUINO_SCL_PIN,
           .sda                = ARDUINO_SDA_PIN,
           .frequency          = NRF_DRV_TWI_FREQ_100K,   
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
           .clear_bus_init     = false
        };
    
        err_code = nrf_drv_twi_init(&m_twi, &twi_lm75b_config, twi_handler, NULL); // 
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_twi_enable(&m_twi);
    
        
    }

    thanks

  • Ok, but you are calling ciclomedicion() in the interrupt handler of the NFC peripheral, so if the NFC IRQ handler has the same priority as the TWI handler, which sets the m_xfer_done flag, then you will never enter the  TWI handler. 

    So you can try to set the priority of the NFC IRQ handler lower than the TWI IRQ handler by calling

    NVIC_SetPriority(5, 3);

    in Prepare_NFC_Wake_up.

     

  • HI bjorn

    Thanks for your advice, you are right,  the problem is the priority..

    I added :

    1.- NVIC_SetPriority(NFCT_IRQn, 5); but i have to changed the priotity of the UART

    2.- #define UART_DEFAULT_CONFIG_IRQ_PRIORITY 6  // Change from 7 to 6

    with both the program doesnt stuck in 

    NRF_LOG_FLUSH();

    or getting the values from accelerometer (twi)..

    and it Works the NFC with rtc event for wakeup...

    Thanks

Related