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

Why can not read twi data ? twi interfacing with RTC DS1307....

Why can not read twi data ? at nrf52832 chip.... but Read well in Arduino....

twi interfacing with RTC DS1307....

please give me some hint.....

  1. nrf_drv_config.h

    #define TWI0_ENABLED 1 #define TWI1_ENABLED 1

  2. define

    #define DS1307_I2C_SCL_PIN 9 // 9 #define DS1307_I2C_SDA_PIN 10 // 10

    #define DS1307_ADDR 0x68U //I2C ADDR 0x68U

    void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context) {
    switch(p_event->type) { case NRF_DRV_TWI_EVT_DONE: break; default: break; } }

    void twi_init (void) { ret_code_t err_code;

    const nrf_drv_twi_config_t    twi_ds1307_config = {
    	.scl                    = DS1307_I2C_SCL_PIN,
    	.sda                   = DS1307_I2C_SDA_PIN,
    	.frequency          = NRF_TWI_FREQ_100K,
    	.interrupt_priority = APP_IRQ_PRIORITY_HIGH
    };
    
    err_code = nrf_drv_twi_init(&m_twi_ds1307, &twi_ds1307_config, twi_handler, NULL);  		
    APP_ERROR_CHECK(err_code);
    
    nrf_drv_twi_enable(&m_twi_ds1307);
    

    }

  3. reading.....

    uint8_t r_buf[16]={0,}; err_code = nrf_drv_twi_rx(&m_twi_ds1307, DS1307_ADDR, r_buf, sizeof(r_buf)); APP_ERROR_CHECK(err_code);

    for (uint32_t i = 0; i < 16; i++) { printf("%d = 0x%02X \n",i, r_buf[i] ); }

  4. result

1 = 0x00 2 = 0x00 ... 15 = 0x00

all value zero....

Parents
  • Hi.

    Since you are using a TWI handler function, you are using the TWI driver in the so called "non-blocking" mode. That means that nrf_drv_twi_rx() returns immediately, but the TWI reading is actually not completed before later. So since you initiate r_buf[] to all zeros, and you haven't actually received any data yet when you call printf(), all you see is zeros.

    You can easily solve this problem by using the driver in blocking mode, or use a flag in your TWI handler that and wait for that flag to be set before you read out your data.

    In your post you don't show any code that tells the DS1307 what registers you want read either. I assume you have it in your application? Your code should look something like this:

    err_code = nrf_drv_twi_tx(&m_twi_ds1307, DS1307_ADDR, address_that_you_want_to_read, 1);
    APP_ERROR_CHECK(err_code);
    
    // Use blocking mode or wait in a while(!tx_flag) loop here
    
    uint8_t   r_buf[16]={0,};
    err_code = nrf_drv_twi_rx(&m_twi_ds1307, DS1307_ADDR, r_buf, sizeof(r_buf));
    APP_ERROR_CHECK(err_code);
    
    // Use blocking mode or wait in a while(!rx_flag) loop here
    
    for (uint32_t i = 0; i < 16; i++)
    {
        printf("%d = 0x%02X \n",i, r_buf[i] );  
    }
    

    Also, make sure to try this example to confirm that you have wired everything correctly and use the right address.

  • Is xfer_completed declared volatile? THis could be important.

    Can you post your entire code?

Reply Children
No Data
Related