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);
    
  • thanks for help.

    void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
    {   
        ret_code_t err_code;
        static sample_t m_sample;
        
        switch(p_event->type)
        {
            case NRF_DRV_TWI_RX_DONE:
               // read_data(&m_sample);
    				      read_data();
                m_xfer_done = true;
                break;
            case NRF_DRV_TWI_TX_DONE:
                if(m_set_mode_done != true)
                {
                    m_set_mode_done  = true;
                    return;
                }
                m_xfer_done = false;
                /* Read 4 bytes from the specified address. */
              //  err_code = nrf_drv_twi_rx(&m_twi_mma_7660, MMA7660_ADDR, (uint8_t*)&m_sample, sizeof(m_sample), false);
    						 err_code = nrf_drv_twi_rx(&m_twi_adxl_345,ADXL345_ADDR,(uint8_t*)&m_sample, sizeof(m_sample), false);
                APP_ERROR_CHECK(err_code);
                break;
            default:
                break;        
        }   
    }
    

    this twi_handler is confusing me much. what is its use.can you tell me? how to use it?

Reply
  • thanks for help.

    void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
    {   
        ret_code_t err_code;
        static sample_t m_sample;
        
        switch(p_event->type)
        {
            case NRF_DRV_TWI_RX_DONE:
               // read_data(&m_sample);
    				      read_data();
                m_xfer_done = true;
                break;
            case NRF_DRV_TWI_TX_DONE:
                if(m_set_mode_done != true)
                {
                    m_set_mode_done  = true;
                    return;
                }
                m_xfer_done = false;
                /* Read 4 bytes from the specified address. */
              //  err_code = nrf_drv_twi_rx(&m_twi_mma_7660, MMA7660_ADDR, (uint8_t*)&m_sample, sizeof(m_sample), false);
    						 err_code = nrf_drv_twi_rx(&m_twi_adxl_345,ADXL345_ADDR,(uint8_t*)&m_sample, sizeof(m_sample), false);
                APP_ERROR_CHECK(err_code);
                break;
            default:
                break;        
        }   
    }
    

    this twi_handler is confusing me much. what is its use.can you tell me? how to use it?

Children
No Data
Related