This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

write data in EEPROM I2C without interrupt

Other Parts Discussed in Thread: TMS320F28069, CONTROLSUITE

I have been trying to write data in eeprom through I2C but data is not writing properly and i am using polling method.

here is my code

I2caRegs.I2CMDR.all = 0;


I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address
I2caRegs.I2CCNT = I2C_NUMBYTES + 1;
I2caRegs.I2CMDR.bit.IRS = 1;
I2caRegs.I2CDXR =0x00; //Send eeprom address
while ((I2caRegs.I2CSTR.bit.BB == 1 ));
I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode
I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode
I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode
I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0
I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow
while(I2caRegs.I2CSTR.bit.XRDY == 0){};

for(i = 0; i < I2C_NUMBYTES; i++){
while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
I2caRegs.I2CDXR = Data[i];
}
}

  • Hi Shubham,

    Here's a ready-made routine for you:

    //////////////////////////
    // Write data to EEPROM //
    //////////////////////////
    //I2C_SLAVE_ADDR = 0x50 address of the EEPROM
    //I am writing 'h','e','l','l','o' to the EEPROM and then read it back later
    //I2C_NUMBYTES = 5
    //[MemoryHighAddr:MemoryLowAddr] -Address within the eeprom where you want to read/write
    //Data[] has the word 'hello' and RxdData[] will store data read from the eeprom
       I2caRegs.I2CSAR = I2C_SLAVE_ADDR; 			//Set slave address
       I2caRegs.I2CCNT = I2C_NUMBYTES + 2; 			//Set count to 5 characters plus 2 address bytes
       I2caRegs.I2CDXR = MemoryHighAddr;			//Send eeprom high address            
       I2caRegs.I2CMDR.bit.TRX = 1; 				//Set to Transmit mode
       I2caRegs.I2CMDR.bit.MST = 1; 				//Set to Master mode
       I2caRegs.I2CMDR.bit.FREE = 1;				//Run in FREE mode
       I2caRegs.I2CMDR.bit.STP = 1; 				//Stop when internal counter becomes 0
       I2caRegs.I2CMDR.bit.STT = 1; 				//Send the start bit, transmission will follow
       while(I2caRegs.I2CSTR.bit.XRDY == 0){}; 		//Do nothing till data is shifted out
       I2caRegs.I2CDXR = MemoryLowAddr;  			//Send eeprom low address
       
       for(i = 0; i < I2C_NUMBYTES; i++){
       	while(I2caRegs.I2CSTR.bit.XRDY == 0){}; 	//Do nothing till data is shifted out
       	I2caRegs.I2CDXR = Data[i]; 					//Send out the message
       } 
    //////////////////////////
    // Read data from EEPROM//
    //////////////////////////
       I2caRegs.I2CSAR = I2C_SLAVE_ADDR; 			//Set slave address
       I2caRegs.I2CCNT = 2; 						//Set count to 2 address bytes
       I2caRegs.I2CDXR = MemoryHighAddr;			//Send eeprom high address            
       I2caRegs.I2CMDR.bit.TRX = 1; 				//Set to Transmit mode
       I2caRegs.I2CMDR.bit.MST = 1; 				//Set to Master mode
       I2caRegs.I2CMDR.bit.FREE = 1;				//Run in FREE mode
       I2caRegs.I2CMDR.bit.STP = 0; 				//Dont release the bus after Tx
       I2caRegs.I2CMDR.bit.STT = 1; 				//Send the start bit, transmission will follow
       
       while(I2caRegs.I2CSTR.bit.XRDY == 0){}; 		//Do nothing till data is shifted out
       I2caRegs.I2CDXR = MemoryLowAddr; 			//Send eeprom low address
       I2caRegs.I2CCNT = I2C_NUMBYTES;				//read 5 bytes from eeprom
       I2caRegs.I2CMDR.bit.TRX = 0; 				//Set to Recieve mode
       I2caRegs.I2CMDR.bit.MST = 1; 				//Set to Master mode
       I2caRegs.I2CMDR.bit.FREE = 1;				//Run in FREE mode
       I2caRegs.I2CMDR.bit.STP = 1; 				//Stop when internal counter becomes 0
       I2caRegs.I2CMDR.bit.STT = 1; //Repeated start, Reception will follow
       for(i = 0; i < I2C_NUMBYTES; i++){
       	while(I2caRegs.I2CSTR.bit.RRDY == 0){}; 	//I2CDRR not ready to read?
       	RxdData[i] = I2caRegs.I2CDRR;
       }

    Regards,

    Gautam

  • i tried this but writing query is not working properly
  • In writing query some time it write and sometime not if write but not properly
  • Shubham dubey said:
    i tried this but writing query is not working properly

    Alter it then according to your requirement (your EERPOM requirements).

  • Hi Gautum,


    I have been using eeprom M24c08 st and dsp controller tms320f28069. I tried your code and still it does not working. Can you me whole code.

    Thank for your response.

    Regards,
    Shubham
  • Those are the main i2c routines for the famous Microchip 24LCxx series. Hence I'd previously told you to alter the same wrt yours (M24c08). How will the whole code make sense when your main routines itself are not working?
    Also, do refer the F28069 based EEPROM code that can be found here:
    C:\ti\controlSUITE\device_support\f2806x\v151\F2806x_examples_ccsv5\i2c_eeprom

    Simply replace the write and read routines with the previously attached one.

    Regards,
    Gautam
  • Hi Gautum,

    I figured out where i was doing mistake in polling method. I was not providing delay after module reset.

    Here is my code and it is working

    /*************************************************************************************

    * Purpose of the Module: This Module contains the communication module for EEPROM through i2c using polling method

    * Written By: Shubham Kant Dubey
    ***************************************************************************************/


    #include "DSP28x_Project.h" void _Init_Gpio(); void _Init_i2c(); void _read_data(); void _write_data(); void delay_f(int); #define I2C_NUMBYTES 7 #define I2C_SLAVE_ADDR 0x50 int i; char Data[7]="1234567"; char RxdData[7]="skdhhhh"; void main(void) { // Step 1. Initialize System Control: InitSysCtrl(); // Step 2. Initalize GPIO: _Init_Gpio(); // Step 3. Clear all interrupts and initialize PIE vector table: // Disable CPU interrupts DINT; // Initialize PIE control registers to their default state. InitPieCtrl(); // Disable CPU interrupts and clear all CPU interrupt flags: IER = 0x0000; IFR = 0x0000; // Initialize the PIE vector table with pointers to the shell Interrupt InitPieVectTable(); _Init_i2c(); for(;;) { _write_data(); _Init_i2c(); _read_data(); _Init_i2c(); } } // Write data to EEPROM void _write_data() { /* Check for Bus Busy */ /* Disable I2C during configuration */ I2caRegs.I2CMDR.all = 0; I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address I2caRegs.I2CCNT = I2C_NUMBYTES +1; //Set count to 5 characters plus 2 address bytes I2caRegs.I2CMDR.bit.IRS = 1; delay_f(10); while(I2caRegs.I2CSTR.bit.BB==1){}; I2caRegs.I2CDXR =0x00; //Send eeprom high address I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0 I2caRegs.I2CMDR.bit.STT = 1; for(i = 0; i < I2C_NUMBYTES; i++){ while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out I2caRegs.I2CDXR = Data[i]; } delay_f(100); } // Read data from EEPROM void _read_data(){ I2caRegs.I2CMDR.all = 0; I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address I2caRegs.I2CCNT = 0x01; //Set count to 5 characters plus 2 address bytes I2caRegs.I2CMDR.bit.IRS = 1; //Send eeprom high address delay_f(10); while(I2caRegs.I2CSTR.bit.BB==1){}; I2caRegs.I2CDXR =0x00; I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode I2caRegs.I2CMDR.bit.STP = 0; //Dont release the bus after Tx I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow while(I2caRegs.I2CSTR.bit.XRDY == 0){}; I2caRegs.I2CCNT = I2C_NUMBYTES; I2caRegs.I2CSAR = I2C_SLAVE_ADDR; I2caRegs.I2CMDR.bit.TRX = 0; //Set to Recieve mode I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0 I2caRegs.I2CMDR.bit.STT = 1; //Repeated start, Reception will follow for(i = 0; i < I2C_NUMBYTES; i++){ while(I2caRegs.I2CSTR.bit.RRDY == 0){}; //I2CDRR not ready to read? RxdData[i]=I2caRegs.I2CDRR; } } void delay_f(int k) { for(i=0;i<=k;k++); } // Setup for I2C pins and peripheral clock. void _Init_Gpio(){ EALLOW; // GPIO32 - uC_CSAC_SDA GpioCtrlRegs.GPBPUD.bit.GPIO32 = 0; // Enable pull-up for GPIO32 (SDAA) GpioCtrlRegs.GPBQSEL1.bit.GPIO32 = 3; // Asynch input GPIO32 (SDAA) GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 1; // 0=GPIO, 1=I2CSDA, 2=SYNCI, 3=ADCSOCA // GPIO33 - uC_CSAC_SCL GpioCtrlRegs.GPBPUD.bit.GPIO33 = 0; // Enable pull-up for GPIO32 (SCLA) GpioCtrlRegs.GPBQSEL1.bit.GPIO33 = 3; // Asynch input GPIO32 (SLCA) GpioCtrlRegs.GPBMUX1.bit.GPIO33 = 1; // 0=GPIO, 1=I2CSCL, 2=SYNCO, 3=ADCSOCB EDIS; return; } void _Init_i2c(){ // Just like the example... I2caRegs.I2CMDR.bit.IRS = 0; // Disable I2C module for IPSC initialization (IRS = 0). // I2C_ master address. I2caRegs.I2CPSC.all = 7; // Set I2C module clock (not SCL) to 10MHz for specified operation, systemclock = 80MHz. I2caRegs.I2CCLKH = 45; // Set I2C SCL to 100kHz operation (high time multipler). I2caRegs.I2CCLKL = 45; // Set I2C SCL to 100kHz operation (low time multipler). I2caRegs.I2CIER.all = 0x0000; // No interrupts set. I2caRegs.I2CMDR.bit.IRS=1; // Enable I2C module for initialization (IRS = 1). return; }

    Regards,

    shubham