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

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

  • 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;        
        }   
    }
    
  • can we remove it if i dont want to use it? with initializing NULL in nrf_drv_twi_init

  • #include <stdio.h> #include "boards.h" #include "app_util_platform.h" #include "app_uart.h" #include "app_error.h" #include "nrf_drv_twi.h"

    #define ARDUINO_I2C_SCL_PIN 7
    #define ARDUINO_I2C_SDA_PIN 30
    
    #define UART_TX_BUF_SIZE 256
    #define UART_RX_BUF_SIZE 1
    #define ADXL345_ADDR        0x53U
    #define ADXL345_REG_MODE    0x2DU
    #define ACTIVE_MODE 8u
    #define NUMBER_OF_SAMPLES 20
    
    static volatile bool m_set_mode_done;
    static const nrf_drv_twi_t m_twi_adxl_345 = NRF_DRV_TWI_INSTANCE(0);
    
    //uart
    static void uart_events_handler(app_uart_evt_t * p_event)
    {
        switch (p_event->evt_type)
        {
            case APP_UART_COMMUNICATION_ERROR:
                APP_ERROR_HANDLER(p_event->data.error_communication);
                break;
    
            case APP_UART_FIFO_ERROR:
                APP_ERROR_HANDLER(p_event->data.error_code);
                break;
    
            default:
                break;
        }
    }
    static void uart_config(void)
    {
        uint32_t                     err_code;
        const app_uart_comm_params_t comm_params =
        {
            RX_PIN_NUMBER,
            TX_PIN_NUMBER,
            RTS_PIN_NUMBER,
            CTS_PIN_NUMBER,
            APP_UART_FLOW_CONTROL_DISABLED,
            false,
            UART_BAUDRATE_BAUDRATE_Baud38400
        };
    
        APP_UART_FIFO_INIT(&comm_params,
                           UART_RX_BUF_SIZE,
                           UART_TX_BUF_SIZE,
                           uart_events_handler,
                           APP_IRQ_PRIORITY_LOW,
                           err_code);
    
        APP_ERROR_CHECK(err_code);
    }
    
    
    void ADXL345_set_mode(void)
    {ret_code_t err_code;
    
    	 uint8_t reg[2] = {ADXL345_REG_MODE, ACTIVE_MODE};
      err_code = nrf_drv_twi_tx(&m_twi_adxl_345,ADXL345_ADDR , reg, sizeof(reg), false);
    	while(m_set_mode_done == false);
    }
    uint16_t read_data(nrf_drv_twi_t const * const  m_twi_adxl_345,uint8_t i2cAddress, uint8_t reg) 
    {
     
    uint32_t           err_code;
    		uint8_t data[2] = {0, 0};
    		uint8_t value;
    
              err_code = nrf_drv_twi_tx(m_twi_adxl_345,ADXL345_ADDR , data, sizeof(data), false);
              APP_ERROR_CHECK(err_code);
     err_code =  nrf_drv_twi_rx(m_twi_adxl_345,ADXL345_ADDR,data,sizeof(data),false);
                       
      value = (data[0] << 8) | data[1];
        return value;
    }
    
    
    void write_data(nrf_drv_twi_t const * const  m_twi_adxl_345,uint8_t i2cAddress, uint8_t reg) {
        uint8_t tx_data[3];
    uint32_t           err_code;
        tx_data[0] = (uint8_t)reg;
    
     
        err_code = nrf_drv_twi_tx(m_twi_adxl_345,ADXL345_ADDR , tx_data, sizeof(tx_data), false);
        APP_ERROR_CHECK(err_code);
    }
    
    
    void twi_init (void)
    {
        ret_code_t err_code;
    		const nrf_drv_twi_config_t twi_adxl_345_config = {
           .scl                = ARDUINO_SCL_PIN,
           .sda                = ARDUINO_SDA_PIN,
           .frequency          = NRF_TWI_FREQ_100K,
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH  // interrupt priority
    };
    	err_code = nrf_drv_twi_init(&m_twi_adxl_345, &twi_adxl_345_config, NULL, NULL);
        APP_ERROR_CHECK(err_code);
    nrf_drv_twi_enable(&m_twi_adxl_345);
    }
    int main(void)
    {
        uart_config();
    	uint8_t a;
       
        printf("\n\rTWI sensor example\r\n");
        twi_init();
        
        ADXL345_set_mode();
        write_data(&m_twi_adxl_345,ADXL345_ADDR,0x32);
    	 a = read_data(&m_twi_adxl_345,ADXL345_ADDR,0x32);
    	printf("%d",a);
    }
    

    hey it is my finle code. but still not getting data

Related