Read and write Eeprom

Example of using I2c protocol to read and write Eeprom

Eeprom is an erasable and repeatedly programmable memory, which can save the data in it even if it is powered off, and can be used for multiple cycles of programming. Next, we need to use the I2c protocol to read and write Eeprom.

Let's take a look at the Eeprom read and write news, and also at the end I will release the results of the logic analyzer after we finish reading and writing Eeprom, and then analyze why.

First, initialize the enable pin and clock according to the I2c configuration. It also includes writing a byte. code show as below. PS ( For the implementation process of the called i2c.WriteByte() function, please refer to the specific code in the I2c library function I sent before)

///Initializes all necessary hardware
void I2cEeprom::Initialize(uint8_t channel)
{	
	i2c.Initialize(channel);

	//The parameter inside the function call can be 100 400 500.
	//Corresponding to the general mode(100), fast mode(400), fast plus mode(500).
	i2c.I2cClockSpeed(100);
	ConfigPins();
}

void I2cEeprom::ConfigPins()
{
	I2c::I2cPinConfig pinConfig;
	pinConfig.sclPin = 8;
	pinConfig.sclAltNum = 4;
	pinConfig.sclCh = Gpio::_ChB;
									
	pinConfig.sdaPin = 9;
	pinConfig.sdaAltNum = 4;
	pinConfig.sdaCh = Gpio::_ChB;

	i2c.ConfigPins(pinConfig);
	wpPin.Initialize(Gpio::_ChA, 2, Gpio::_High);
}

///sends one byte
void I2cEeprom::WriteByte(const uint8_t txData, uint8_t wordAddress)
{
	WpDisable();
	i2c.WriteByte(txData, dev_write_addr);
	WpEnable();
}

                                                                     Write eeprom by page.


We can see that Eeprom's page writes are more efficient than single-section writes. Each page size is 8 bytes. code show as below. PS ( It calls the WriteBytesWithHeader function of I2c to read and write multiple bytes )

///Write a number of bytes in a EEPROM write cycle
///However,the number of bytes written at a time can not exceed the size of the EEPROM page
///The AT24C02 has 8 bytes per page
void I2cEeprom::WritePage(uint8_t *txData, uint8_t size, uint8_t wordAddress)
{
	uint8_t bytesThisCycle = 0;

	bytesThisCycle = _I2cPageSize - (wordAddress & 0X07);

	WpDisable();

	while (size > 0)
	{
		if (size < bytesThisCycle) bytesThisCycle = size;

		i2c.WriteBytesWithHeader(txData, bytesThisCycle, wordAddress, dev_write_addr);

		txData += bytesThisCycle;
		wordAddress += bytesThisCycle;
		size -= bytesThisCycle;
		bytesThisCycle = _I2cPageSize;

		System::DelayUs(10000);
	}

	WpEnable();
} Page write analysis: We calculate the start address of the Eeprom, the start address of the data, and how many bytes are left to transfer when the page is written.


random read eeprom 


We can see the random read of eeprom, it is used to write a device address and then a byte address, but there cannot be a pseudo write of data transfer and stop signal, when writing sends a start signal. Then use read to send a normal read signal. code show as below.

///read multiple bytes
uint8_t I2cEeprom::ReadBytes(uint8_t* rxData, uint8_t rxSize, uint8_t wordAddress)
{
	i2c.DisableAutoStop();
	i2c.WriteByte(wordAddress,dev_write_addr);
	while ( !i2c.IsTransferComplete() ) {}
	i2c.ReadBytes(rxData, rxSize, dev_read_addr);
	while ( !i2c.IsTransferComplete() ) {}
	i2c.ManualStop();
	i2c.EnableAutoStop();
}
Code analysis: I2c is called to write a byte but the stop signal has been disabled, and then it is called to read multiple bytes. You can leave a message if you need the source code.


The main function is as follows:

int main(void)
{
	uint8_t write[] = {1,2,3,4,5,6,7,8,9,10,11};
	uint8_t read[11] = {};

	System::Initialize();
	System::ConfigureForHsi();
	i2ceeprom.Initialize(1);

	while (1)
	{
		i2ceeprom.WritePage(write,5, 0xa8);
		System::DelayUs(10000);
		i2ceeprom.ReadBytes(read, 5, 0xa8);
		System::DelayUs(10000);
	}
}

Finally, let's look at the results of the logic analyzer.

     

We can see that the writing timing in the manual is the same. Inside 160 is the device address 10100000, 168 is the byte address.



Let's take a look at the write and read timing. We have seen the timing diagram of random read before, so we know that we need to pseudo- write a device address and a byte address first, with a start signal, and then a start signal (the green point in the figure). is the start signal, red is the stop signal.)



The reading and writing of eeprom is probably like this. If you want to refer to the source code, you can leave a message.







    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326038091&siteId=291194637