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

Twi problem

Hi forum,

I am working on nrf52832 custom board, using sdk 15.0.0.

I referred example/peripherals/twi_sesnor example as reference. I am trying to interface LIS2dw12 with nrf52832.

While debugging, I found that the board gets stuck in the "while (m_xfer_done == false)" loop inside the function that writes to the board to read data from the board.

please help me slove this error.

  • //
    // TWI events handler.
    //
    void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
    {
        switch (p_event->type)
        {
            // TWI event completed
            // If data was being read, jump to the data handler
            // Otherwise, assume it was a transmit and just indicate transfer is done
            case NRF_DRV_TWI_EVT_DONE:
                if (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX) // Reading data
                {
                  if (reg[0] == HTS221_WHO_AM_I) // Reading from HTS221 WHO_AM_I register
                  id_handler(m_sample);
                 if (reg[0] == HTS221_CTRL_REG1) // Reading CTRL_REG1
                    reg1_handler(m_sample);
                  if (reg[0] == 0xB0) // Reading the calibration data
                   calibration_handler(calibration_regs);
                  if (reg[0] == 0xAA) // Reading the temperature registers
                    temperature_handler(raw_temp);
                 if (reg[0] == HTS221_STATUS_REG) // Reading the status register
                    status_handler(m_sample);
                if (reg[0] == 0xA8)  // Reading the humidity registers
                   humidity_handler(raw_hum);
                }
               m_xfer_done = true;
            // m_xfer_done = false;
                break;
            default:
                break;
        }
    }
    
    //
    // TWI initialization.
    //
    void twi_init (void)
    {
        ret_code_t err_code;
    
        const nrf_drv_twi_config_t twi_hts221_config = {
           .scl                =SCL_PIN,
           .sda                =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_hts221_config, twi_handler, NULL);
        APP_ERROR_CHECK(err_code);
        printf("init: %d\n",err_code);
    
        nrf_drv_twi_enable(&m_twi);
    
        //
        // read calibration registers
        //
        reg[0]=0xB0; // 1st Register address of the calibration registers
        // Note bit 7 is set for register address incrementing. 0x30 | 0x80
        err_code = nrf_drv_twi_tx(&m_twi, HTS221_ADDR, reg, sizeof(reg),true);
        APP_ERROR_CHECK(err_code);
        // Wait for transmit to complete
        while (m_xfer_done == false);
        // Reset TWI event complete flag
        m_xfer_done = false;
        // Read 16 bytes of raw calibration data from 0x30-3F
        // Data will be returned via twi event handler
        err_code = nrf_drv_twi_rx(&m_twi, HTS221_ADDR, calibration_regs, sizeof(calibration_regs));
        APP_ERROR_CHECK(err_code);
    
    }

    if anymore required let me know. few days ago I was tested this code it works fine.it gives us temperature and humidity exact value match with surrounding temp.

  • Hi forum,

    I tried to change interrupt priority to low it works once. after redumped code its not working.

    please help me to slove this problem.

  • Hi,

    Is the flag declared as static volatile? Can you set a breakpoint at 27 and upload a screenshot from debug view that shows that the handler is called and the variable is set to true. Also, what other modules/peripherals do you use? Is the Softdevice used?

    regards

    Jared 

Related