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

I2C interface with AT24C32

Dear Nordic Team,

Thanks for supporting.

I want example code for I2C interface with AT24C32

Parents
  • Hi Murugan,

    had to use an CAT24c128 on a nrf51822 custom board using i2c (TWI) two weeks ago. Got it working quite easy. Since CAT24c128 and at2432 pretty much run on the same code, i will give you some snippets that will get you started quickly.

    Enable and configure TWI

    First of all make sure that TWI is enabled in "nrf_drv_config.h". You also need to do pin configuration there so that the defines match your hardware.(Hence you have to include n/f_drv_twi.h / c and add the path to those files to your header search path) (you didn't mention if you are using a custom board or a dev kit) For me, I use TWI1:

    #define TWI0_ENABLED 0
    #if (TWI0_ENABLED == 1)
    #define TWI0_USE_EASY_DMA 0
    
    #define TWI0_CONFIG_FREQUENCY    NRF_TWI_FREQ_100K
    #define TWI0_CONFIG_SCL          8
    #define TWI0_CONFIG_SDA          9
    #define TWI0_CONFIG_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW
    
    #define TWI0_INSTANCE_INDEX      0
    #endif
    
    #define TWI1_ENABLED 1
    
    #if (TWI1_ENABLED == 1)
    #define TWI1_USE_EASY_DMA 0
    
    #define TWI1_CONFIG_FREQUENCY    NRF_TWI_FREQ_100K
    #define TWI1_CONFIG_SCL          8
    #define TWI1_CONFIG_SDA          9
    #define TWI1_CONFIG_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW
    
    #define TWI1_INSTANCE_INDEX      (TWI0_ENABLED)
    #endif
    
    #define TWI_COUNT                (TWI0_ENABLED + TWI1_ENABLED)
    

    Initialize TWI Master

    To communicate with the eprom over i2c, you need to initialize twi master first. You can do so by calling twi_master_init(void) i.e. from your main() just before you reach while(1) loop. The implementation of the init function could look like:

    ret_code_t twi_master_init(void){
        ret_code_t ret;
        const nrf_drv_twi_config_t config =
        {
           .scl                = TWI1_CONFIG_SCL,
           .sda                = TWI1_CONFIG_SDA,
           .frequency          = NRF_TWI_FREQ_100K,
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH
        };
        do
        {
            ret = nrf_drv_twi_init(&m_twi_master, &config, NULL, NULL);
            if(NRF_SUCCESS != ret)
            {
                break;
            }
            nrf_drv_twi_enable(&m_twi_master);
        }while(0);
    
    		
        return ret;
    }
    

    As soon as TWI is initialized, you can build functions that use the TWI to send and receive data to and from your EEPROM. You just need to make sure that all TWI communication is addressing the correct i2c slave. I defined the cat24c128s address as

    //	EEPROM 24C128 Slave Address	 (0xA0 <--> 0xA1 handled by nordic with read / write function)
    #define I2C_24C128_SLAVE_ADDR        (0xA0 >> 1)
    

    Read a byte from EEPROM Lets go and read 1 Byte from a memory address of the eeprom:

    void eep_readByte(uint16_t eep_address, unsigned char* val){
    	
    	unsigned char eep_by_address[2];
    
    	eep_by_address[1]	= eep_address;
    	eep_by_address[0] = (unsigned char)(eep_address << 8);
    	// setting the start address
    	nrf_drv_twi_tx(&m_twi_master, I2C_24C128_SLAVE_ADDR, eep_by_address, 2, true);
    	// read a byte
        nrf_drv_twi_rx(&m_twi_master, I2C_24C128_SLAVE_ADDR, val,1);
    	
    	nrf_delay_ms(5);
    }
    

    Reading a byte follows a write statement because you need to tell the cat24 where to start from.

    So writing a byte is even easier:

    Writing a Byte

    void eep_WriteByte(uint16_t eep_address, unsigned char val){
    	
    	unsigned char eep_by_address[3];
        eep_by_address[2] = val;
    	eep_by_address[1] = eep_address;
    	eep_by_address[0] = (unsigned char)(eep_address << 8);
    	
    	nrf_drv_twi_tx(&m_twi_master, I2C_24C128_SLAVE_ADDR, eep_by_address, 3, false);
    	nrf_delay_ms(5);
    }
    

    Hope that helps a little bit!

  • My code :

    /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
     *
     * The information contained herein is property of Nordic Semiconductor ASA.
     * Terms and conditions of usage are described in detail in NORDIC
     * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
     *
     * Licensees are granted free, non-transferable use of the information. NO
     * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
     * the file.
     *
     */
    
    /** @file
     * @defgroup tw_sensor_example main.c
     * @{
     * @ingroup nrf_twi_example
     * @brief TWI Sensor Example main file.
     *
     * This file contains the source code for a sample application using TWI.
     *
     */
    
    #include <stdio.h>
    #include "boards.h"
    #include "app_util_platform.h"
    #include "app_uart.h"
    #include "app_error.h"
    #include "nrf_drv_twi.h"
    #include "nrf_delay.h"
    
    /*Pins to connect shield. */
    #define ARDUINO_I2C_SCL_PIN 5
    #define ARDUINO_I2C_SDA_PIN 6
    
    
    
    #define I2C_24C128_SLAVE_ADDR        (0xA0 >> 1)
    
    
    
    
    
    /* Define version of GCC. */
    #define GCC_VERSION (__GNUC__ * 10000 \
                         + __GNUC_MINOR__ * 100 \
                         + __GNUC_PATCHLEVEL__)
    
    /**
     * @brief Structure for holding sum of samples from accelerometer.
     */
    
    
    #ifdef __GNUC_PATCHLEVEL__
    #if GCC_VERSION < 50505
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wmissing-braces"           // Hack to GCC 4.9.3 bug. Can be deleted after switch on using GCC 5.0.0
    #endif
    #endif
    /* Buffer for samples. */
    
    #ifdef __GNUC_PATCHLEVEL__
    #if GCC_VERSION < 50505
    #pragma GCC diagnostic pop
    #endif
    #endif
    
    /* TWI instance. */
    
    static const nrf_drv_twi_t m_twi_mma_EEP = NRF_DRV_TWI_INSTANCE(0);
    
    void eep_WriteByte(uint16_t eep_address, unsigned char val);
    unsigned char eep_readByte(uint16_t eep_address);
    ret_code_t err_codecpy;
    
    
    
    
    
    /**
     * @brief Function for averaging samples from accelerometer.
     */
    
    /**
     * @brief TWI events handler.
     */
    
    /**
     * @brief UART initialization.
     */
    void  twi_init (void)
    {
        ret_code_t err_code;
         //ret_code_t ret;
        const nrf_drv_twi_config_t twi_mma_EEP_config = {
           .scl                = ARDUINO_SCL_PIN,
           .sda                = ARDUINO_SDA_PIN,
           .frequency          = NRF_TWI_FREQ_100K,
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH
        };
      
        err_code = nrf_drv_twi_init(&m_twi_mma_EEP, &twi_mma_EEP_config, NULL, NULL); //twi_handler
          APP_ERROR_CHECK(err_code);
    	
    				 nrf_drv_twi_enable(&m_twi_mma_EEP);
    	
    		
       err_codecpy=err_code;
    		nrf_delay_ms(5);
    }
    
    unsigned char eep_readByte(uint16_t eep_address){
    
        unsigned char eep_by_address[2];
    unsigned char reg=0;
        eep_by_address[1]   = eep_address;
        eep_by_address[0] = (unsigned char)(eep_address << 8);
        // setting the start address
        nrf_drv_twi_tx(&m_twi_mma_EEP, I2C_24C128_SLAVE_ADDR, eep_by_address, 2, true);
        // read a byte
        nrf_drv_twi_rx(&m_twi_mma_EEP, I2C_24C128_SLAVE_ADDR, &reg,1);
    
        nrf_delay_ms(5);
    	return reg;
    }
    void eep_WriteByte(uint16_t eep_address, unsigned char val){
    
        unsigned char eep_by_address[3];
        eep_by_address[2] = val;
        eep_by_address[1] = eep_address;
        eep_by_address[0] = (unsigned char)(eep_address << 8);
    
        nrf_drv_twi_tx(&m_twi_mma_EEP, I2C_24C128_SLAVE_ADDR, eep_by_address, 3, false);
        nrf_delay_ms(5);
    }
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
        
        twi_init();
    
    	unsigned char  reg1 = 0;
    
        while(true)
        {
            nrf_delay_ms(1000);
    			    eep_WriteByte(0x0000,'A');
    	 nrf_delay_ms(5);
    	eep_WriteByte(0x0001,'B');
    	 nrf_delay_ms(5);
    	   reg1=eep_readByte(0x0000);
    	if(reg1!=0){
    		nrf_delay_ms(5);
    	}
    	else if(reg1==0){
    	 nrf_delay_ms(5);
    	}
    
        }
    }
    
    /** @} */
    
Reply
  • My code :

    /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
     *
     * The information contained herein is property of Nordic Semiconductor ASA.
     * Terms and conditions of usage are described in detail in NORDIC
     * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
     *
     * Licensees are granted free, non-transferable use of the information. NO
     * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
     * the file.
     *
     */
    
    /** @file
     * @defgroup tw_sensor_example main.c
     * @{
     * @ingroup nrf_twi_example
     * @brief TWI Sensor Example main file.
     *
     * This file contains the source code for a sample application using TWI.
     *
     */
    
    #include <stdio.h>
    #include "boards.h"
    #include "app_util_platform.h"
    #include "app_uart.h"
    #include "app_error.h"
    #include "nrf_drv_twi.h"
    #include "nrf_delay.h"
    
    /*Pins to connect shield. */
    #define ARDUINO_I2C_SCL_PIN 5
    #define ARDUINO_I2C_SDA_PIN 6
    
    
    
    #define I2C_24C128_SLAVE_ADDR        (0xA0 >> 1)
    
    
    
    
    
    /* Define version of GCC. */
    #define GCC_VERSION (__GNUC__ * 10000 \
                         + __GNUC_MINOR__ * 100 \
                         + __GNUC_PATCHLEVEL__)
    
    /**
     * @brief Structure for holding sum of samples from accelerometer.
     */
    
    
    #ifdef __GNUC_PATCHLEVEL__
    #if GCC_VERSION < 50505
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wmissing-braces"           // Hack to GCC 4.9.3 bug. Can be deleted after switch on using GCC 5.0.0
    #endif
    #endif
    /* Buffer for samples. */
    
    #ifdef __GNUC_PATCHLEVEL__
    #if GCC_VERSION < 50505
    #pragma GCC diagnostic pop
    #endif
    #endif
    
    /* TWI instance. */
    
    static const nrf_drv_twi_t m_twi_mma_EEP = NRF_DRV_TWI_INSTANCE(0);
    
    void eep_WriteByte(uint16_t eep_address, unsigned char val);
    unsigned char eep_readByte(uint16_t eep_address);
    ret_code_t err_codecpy;
    
    
    
    
    
    /**
     * @brief Function for averaging samples from accelerometer.
     */
    
    /**
     * @brief TWI events handler.
     */
    
    /**
     * @brief UART initialization.
     */
    void  twi_init (void)
    {
        ret_code_t err_code;
         //ret_code_t ret;
        const nrf_drv_twi_config_t twi_mma_EEP_config = {
           .scl                = ARDUINO_SCL_PIN,
           .sda                = ARDUINO_SDA_PIN,
           .frequency          = NRF_TWI_FREQ_100K,
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH
        };
      
        err_code = nrf_drv_twi_init(&m_twi_mma_EEP, &twi_mma_EEP_config, NULL, NULL); //twi_handler
          APP_ERROR_CHECK(err_code);
    	
    				 nrf_drv_twi_enable(&m_twi_mma_EEP);
    	
    		
       err_codecpy=err_code;
    		nrf_delay_ms(5);
    }
    
    unsigned char eep_readByte(uint16_t eep_address){
    
        unsigned char eep_by_address[2];
    unsigned char reg=0;
        eep_by_address[1]   = eep_address;
        eep_by_address[0] = (unsigned char)(eep_address << 8);
        // setting the start address
        nrf_drv_twi_tx(&m_twi_mma_EEP, I2C_24C128_SLAVE_ADDR, eep_by_address, 2, true);
        // read a byte
        nrf_drv_twi_rx(&m_twi_mma_EEP, I2C_24C128_SLAVE_ADDR, &reg,1);
    
        nrf_delay_ms(5);
    	return reg;
    }
    void eep_WriteByte(uint16_t eep_address, unsigned char val){
    
        unsigned char eep_by_address[3];
        eep_by_address[2] = val;
        eep_by_address[1] = eep_address;
        eep_by_address[0] = (unsigned char)(eep_address << 8);
    
        nrf_drv_twi_tx(&m_twi_mma_EEP, I2C_24C128_SLAVE_ADDR, eep_by_address, 3, false);
        nrf_delay_ms(5);
    }
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
        
        twi_init();
    
    	unsigned char  reg1 = 0;
    
        while(true)
        {
            nrf_delay_ms(1000);
    			    eep_WriteByte(0x0000,'A');
    	 nrf_delay_ms(5);
    	eep_WriteByte(0x0001,'B');
    	 nrf_delay_ms(5);
    	   reg1=eep_readByte(0x0000);
    	if(reg1!=0){
    		nrf_delay_ms(5);
    	}
    	else if(reg1==0){
    	 nrf_delay_ms(5);
    	}
    
        }
    }
    
    /** @} */
    
Children
No Data
Related