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

I2C in nrf51 SDK

I want to interface ADXl345 with nrf51 SDK. suppoose my device address is 0x53 and register address is 0x2D and i want to write value 8. now nordic i2c has function nrf_drv_twi_tx with attributes nrf_drv_twi_t const *const p_instance, uint8_t address, uint8_t const * p_data, uint32_t length, bool xfer_pending

now here my address is 0x53, length is 6. now can you tell me where to write register address? where to write data??

Parents
  • This should do it (includes the setup):

    //global variables
    static uint8_t data[2] = {0x2D, 8};    //contains register + data to be written to register
    #define ADXL345_ADDR        0x53U
    
    //local code
    uint32_t ret_code;
    static const nrf_drv_twi_t           p_twi_instance = NRF_DRV_TWI_INSTANCE(0);
    
    const nrf_drv_twi_config_t p_twi_config = {
       .scl                = SCL_PIN,
       .sda                = SDA_PIN,
       .frequency          = NRF_TWI_FREQ_400K,
       .interrupt_priority = APP_IRQ_PRIORITY_LOW
    };
    
    //Initiate twi driver with instance and configuration values
    ret_code = nrf_drv_twi_init(&p_twi_instance, &p_twi_config, twi_handler, NULL); 
    APP_ERROR_CHECK(ret_code); // Check for errors in return value
    
    nrf_drv_twi_enable(&p_twi_instance); // Enable the TWI instance
    
    ret_code = nrf_drv_twi_tx(&p_twi_instance, ADXL345_ADDR, data, sizeof(data), false);
    
  • The twi handler will be called when the transfer is done or an error condition is met (e.g. NACK recevied).

    void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
    {   
        switch(p_event->type)
        {
            case NRF_DRV_TWI_RX_DONE:
                //code to be run when RX done (nrf_drv_twi_rx done)
                //For example check the data that was received
                break;
            case NRF_DRV_TWI_TX_DONE:
                //code to be run when TX done (nrf_drv_twi_tx done)
                //For example start reading register if this was sent with nrf_drv_twi_tx
                //with xfer pending equals true
                break;
            default:
                break;        
        }   
    }
    
Reply
  • The twi handler will be called when the transfer is done or an error condition is met (e.g. NACK recevied).

    void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
    {   
        switch(p_event->type)
        {
            case NRF_DRV_TWI_RX_DONE:
                //code to be run when RX done (nrf_drv_twi_rx done)
                //For example check the data that was received
                break;
            case NRF_DRV_TWI_TX_DONE:
                //code to be run when TX done (nrf_drv_twi_tx done)
                //For example start reading register if this was sent with nrf_drv_twi_tx
                //with xfer pending equals true
                break;
            default:
                break;        
        }   
    }
    
Children
No Data
Related