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

In SDK14.2, for nrf52832, TWI can't write data with sd_app_evt_wait() in mainly loop, but when comment it, read/write all ok

Hello, boss, I used twi read/write data from others device, I find a strange problem, When in no-blocking mode, mainly loop use "sd_app_evt_wait" TWI can't write data to others device or not correctly ,But read is ok, When comment "sd_app_evt_wait", read/write is all okey, I just use "nrf_drv_twi_tx" and "nrf_drv_twi_xfer" function, and callback event function. have anyone meet this problem, expect for reply! 

  • I will try to provide an answer by upcoming monday/tuesday. Could you upload your code or a snippet of it?

    Simon

  • Those is many code in the project, Work diagram as following, 

    1. Initialize TWI and ble, 

    2. startup TWI polling every 100ms

    3. If uart Wirte command comes , sometimes write some data to TWI slave

    4. Then continue read

    TWI code:

    //TWI event callback:

    void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
    {
    ptcmdContxt ctx = (ptcmdContxt)p_context;
    switch (p_event->type)
    {
    case NRF_DRV_TWI_EVT_DONE:
    if (( p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX )||( NRF_DRV_TWI_XFER_TXRX ==p_event->xfer_desc.type ))
    {
    ....//process data and context
    }
    else if ( NRF_DRV_TWI_XFER_TX == p_event->xfer_desc.type )
    {
    ....//process data and context
    }
    break;
    default:
    break;
    }
    }

    //TWI read

    int restatrtTxRx ( nrf_drv_twi_t const * twi, uint8_t addr, uint8_t* p_tx_data, uint8_t tlen, uint8_t* p_rx_data, uint8_t rlen)
    {
    nrf_drv_twi_xfer_desc_t xfer = NRF_DRV_TWI_XFER_DESC_TXRX( addr, p_tx_data, tlen, p_rx_data, rlen );
    uint32_t flags = NRF_DRV_TWI_FLAG_RX_POSTINC|
    NRF_DRV_TWI_FLAG_TX_NO_STOP;
    ret_code_t ret = nrf_drv_twi_xfer(twi, &xfer, flags);
    return ret;
    }

    ////TWI write
    int my_write_register(uint8_t addr, //!< Target IC's SMBus address
    uint8_t command_code, //!< Command Code to be written to
    uint16_t data, //!< Data to be written
    nrf_drv_twi_t const * p_instance //!< Pointer to port instance struct
    )
    {
    ret_code_t err_code;
    uint8_t val[3];//,buf[2];

    val[0] = command_code; val[1] = data&0x00ff; val[2] = (data>>8)&0x00ff; //val[3] = 0x0;//from the low byte to high high

    err_code = nrf_drv_twi_tx( p_instance, addr, val, sizeof(val), false);

    APP_ERROR_CHECK(err_code);
    return err_code;
    }

    //TWI read

    int my_read_register(uint8_t addr, //!< Target IC's SMBus address
    uint8_t command_code, //!< Command Code to be read from
    uint16_t *data, //!< Pointer to data destination
    nrf_drv_twi_t const * p_instance //!< Pointer to port configuration struct
    )
    {
    ret_code_t err_code;
    uint8_t txdata = command_code;

    err_code = restatrtTxRx ( p_instance, addr, &txdata, sizeof(uint8_t), (uint8_t*) data, sizeof(uint16_t) );
    //err_code = nrf_drv_twi_rx( p_instance, addr, (uint8_t*)data, sizeof(data));

    APP_ERROR_CHECK(err_code);
    //NRF_LOG_INFO("\tRaw read fake data: %d\n",*data);
    return err_code;
    }

    Main code:

    int main(void)
    {
    uint32_t err_code;
    bool erase_bonds;


    err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

    log_init();

    myTwiInit();
    myUartInit();

    buttons_leds_init(&erase_bonds);
    ble_stack_init();

    //peer_manager_init(); //xjmnn for buttonless dfu init

    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();


    err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);

    for (;;)
    {
    UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());//process log information
    //power_manage();//When uncomment this, TWI write is not right
    dispatchTwiData();
    dispatchUartData();
    }
    }

     

  • Have you tried to debug your project? Try setting a break point in your twi_handler() function to check if it's called. Do you call NRF_LOG_FLUSH() in the twi_handler()? As mentioned here, it will cause problems. 

    However I was able to merge the BLE Blinky Application with the TWI Sensor Example with the BLE and TWI functionality both working. I have attached the example.

    ble_app_blinky_template_twi_sensor.zip

    Best regards, 

    Simon

  • Thks first, I have test " BLE Blinky Application and TWI Sensor Example ",It's no problem , Now in my Project , I just used TWI process SMbus data, from battery, Maybe some times is different between SMbus and TWI, ?

  • What is your frequency? As mentioned in this link, if you are using SMBus the speed should not exceed 100 kHz.

Related