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!

  • I am using 2.8v vcc for AT24C32 EEPROM it is enough for read and write operation

Reply Children
No Data
Related