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);
            }
    
Reply Children
No Data
Related