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

How do I write a value V to a register R using device address A with nrf_drv_twi ?

I am using nrf51822, sdk version 11.0, sofdevice 130, i2c initialized in non-blocking mode. The read seems ok. The write is problematic for me.

Parents
  • I think Jorgen points to the best answer. For completeness below are some helper functions I have used to do write/read operations for the nrf twi interface.

    //write byte to register
    void write_byte(const nrf_drv_twi_t *twi_handle, uint8_t address, uint8_t sub_address, uint8_t *data, bool stop)
            {   
                ret_code_t err_code;
                uint8_t data_write[2];
                
                data_write[0] = sub_address;
                data_write[1] = *data;
                    
                err_code = nrf_drv_twi_tx(twi_handle, address, data_write, sizeof(data_write), stop);  
                APP_ERROR_CHECK(err_code);
            }
    
    //reading byte or bytes from register            
    void write_forread_byte(const nrf_drv_twi_t *twi_handle, uint8_t address, uint8_t sub_address)
            {   
                ret_code_t err_code;
                
                err_code = nrf_drv_twi_tx(twi_handle, address, &sub_address, sizeof(sub_address), true);  
                APP_ERROR_CHECK(err_code);
            }
        
        char read_byte(const nrf_drv_twi_t *twi_handle, uint8_t address, uint8_t sub_address)
            {   
                ret_code_t err_code;
                uint8_t data; // `data` will store the register data     
                
                write_forread_byte(twi_handle, address, sub_address);
                
                err_code = nrf_drv_twi_rx(twi_handle, address, &data, sizeof(data));
                APP_ERROR_CHECK(err_code);
             
                return data; 
            }
            
        void read_bytes(const nrf_drv_twi_t *twi_handle, uint8_t address, uint8_t sub_address, uint8_t * dest, uint8_t dest_count)
            {   
                ret_code_t err_code;  
                
                write_forread_byte(twi_handle, address, sub_address);
                
                err_code = nrf_drv_twi_rx(twi_handle, address, dest, dest_count);
                APP_ERROR_CHECK(err_code);
            }
    
  • Thanks Josh This helped me. However in the read functions i had to add a 20 millisecond delay after calling the write functions to give some time for the write function to complete. Else i am getting the error code 17 Which means NRF_ERROR_BUSY i.e the write operation isn't complete. 

  • If you get NRF_ERROR_BUSY error code, it means you are running the TWI driver in non-blocking mode. You should use flags that you set/clear in the TWI event handler to indicate when a transfer is completed. Using a "ranom" delay will not guarantee that the transfer will finish every time. You can also use the driver in blocking mode by setting the event_handler parameter to NULL when you initialize the driver using nrf_drv_twi_init.

Reply Children
No Data
Related