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....

  • 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.

  • Thank you for your help.

    I tried it but it did not work.


    switch(p_event->type)
    {
    	case NRF_DRV_TWI_EVT_DONE:				
    
    		if ( (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX) || (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_TX))
    		{
    			xfer_completed = true;
    			printf("\n\r twi_handler complete .\r\n");
    		}		
    
    		else
    		{
    			xfer_completed = false;
    		}
    
    		break;	
    
    	default:
    		break;
    }
    
    //	do
    //   {
    //       __WFE();
    //  }while(xfer_completed == false);
    
    err_code = nrf_drv_twi_rx(&m_twi_ds1307, DS1307_ADDR, r_buf, sizeof(r_buf));		
    //	 xfer_completed = false;
    nrf_delay_ms(10);
    

    There was no effect if enough delay was given.

    What are the elements I have not thought of yet?

  • Is xfer_completed declared volatile? THis could be important.

    Can you post your entire code?

  • I have finally read the data.

    DS1337 Get Time. twi_i2c_read_err 0, 0 ,[00]

    0 = 0x38
    1 = 0x42
    2 = 0x3A
    3 = 0x88
    4 = 0x23
    5 = 0x47
    6 = 0x17
    7 = 0x70
    8 = 0x7E
    9 = 0xF7
    10 = 0xE9
    11 = 0x23
    12 = 0xC1
    13 = 0x78
    14 = 0x67
    15 = 0x37

    The reason I could not read it was because I read sda scl using only battery power. I can read the data after connecting 3.2v power to VCC, GND in DS1307.

    Thank you for your response. I would appreciate it if you could give me a place to refer to how to convert from buffer data to date and time data.

  • We don't have examples or code for that around here, but if you google something like "dS1307 c library" i'm sure you will find some tips.

Related