void I2C_EE_BufferWrite(u8* pBuffer, u8 WriteAddr, u16 NumByteToWrite)
u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0;
//Write the address to see which number of each page
Addr = WriteAddr % I2C_PageSize;
//The number of entries to be written at the beginning of the page
count = I2C_PageSize - Addr;
//Number of pages to write
NumOfPage = NumByteToWrite / I2C_PageSize;
//Less than one page
NumOfSingle = NumByteToWrite % I2C_PageSize;
I2C_EE_WaitEepromStandbyState();
//Write address is the beginning of the page
if (Addr == 0)
{
//The data is smaller than one page
if (NumOfPage == 0)
{
//Write less than one page of data
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
//EEPROM is set to standby mode
I2C_EE_WaitEepromStandbyState();
}
//Data is greater than or equal to one page
else
{
while (NumOfPage--) //Number of pages to be written
{
//Write a page of data
I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize);
//EEPROM is set to standby mode
I2C_EE_WaitEepromStandbyState();
WriteAddr += I2C_PageSize;
pBuffer += I2C_PageSize;
}
//The remaining data is less than one page
if (NumOfSingle!=0)
{
//Write less than one page of data
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
//EEPROM is set to standby mode
I2C_EE_WaitEepromStandbyState();
}
}
}
//The write address is not the beginning of the page
else
{
//The data is smaller than one page
if (NumOfPage== 0)
{
//Write less than one page of data
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
//EEPROM is set to standby mode
I2C_EE_WaitEepromStandbyState();
}
//Data is greater than or equal to one page
else
{
NumByteToWrite -= count;
//Recalculate the number of pages to be written
NumOfPage = NumByteToWrite / I2C_PageSize;
//Recalculate the number of pages that are less than one page
NumOfSingle = NumByteToWrite % I2C_PageSize;
if (count != 0)
{
//Write the starting space to a full page
I2C_EE_PageWrite(pBuffer, WriteAddr, count);
//EEPROM is set to standby mode
I2C_EE_WaitEepromStandbyState();
WriteAddr += count;
pBuffer += count;
}
//Number of pages to write
while (NumOfPage--)
{
//Write a page of data
I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize);
//EEPROM is set to standby mode
I2C_EE_WaitEepromStandbyState();
WriteAddr += I2C_PageSize;
pBuffer += I2C_PageSize;
}
//The remaining data is less than one page
if (NumOfSingle != 0)
{
//Write less than one page of data
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
//EEPROM is set to standby mode
I2C_EE_WaitEepromStandbyState();
}
}
}
//Generate I2C2 transmission START condition
I2C_GenerateSTART(I2C2, ENABLE);
//Determine whether the host mode is successful by generating START (detecting EV5)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
//Send device address (write)
I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);
// Check if the host transmission mode is successful (check EV6)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//Send address through peripheral I2C2
I2C_SendData(I2C2, WriteAddr);
//Check whether the bytes sent by the host are successful (check EV8)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//Send data through peripheral I2C2
I2C_SendData(I2C2, *pBuffer);
// Check if the bytes sent by the host are successful (check EV8)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//Generate I2C2 transmission STOP condition
I2C_GenerateSTOP(I2C2, ENABLE);
//Generate I2C2 transmission START condition
I2C_GenerateSTART(I2C2, ENABLE);
//Determine whether the host mode is successful by generating START (detecting EV5)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
//Send device address (write)
I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);
// Check if the host transmission mode is successful (check EV6)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//Send address through peripheral I2C2
I2C_SendData(I2C2, WriteAddr);
// Check if the bytes sent by the host are successful (check EV8)
while (! I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//Write data
while (NumByteToWrite--)
{
//Send data through peripheral I2C2
I2C_SendData(I2C2, *pBuffer);
// pointer++
pBuffer++;
// Check if the bytes sent by the host are successful (check EV8)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
//Generate I2C2 transmission STOP condition
I2C_GenerateSTOP(I2C2, ENABLE);
//EEPROM is set to standby mode
I2C_EE_WaitEepromStandbyState();
//Generate I2C2 transmission START condition
I2C_GenerateSTART(I2C2, ENABLE);
//Determine whether the host mode is successful by generating START (detecting EV5)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
// Disable ACK before reading data in a single data transfer situation
if (NumByteToRead==1)
{
I2C_AcknowledgeConfig(I2C2, DISABLE); //Disable the I2C2 response function
}
//Transmit the address word to the specified slave I2C device and select the sending direction
I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);
// Check if the host transmission mode is successful (check EV6)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
// Enable I2C peripherals
I2C_Cmd(I2C2, ENABLE);
//Send address through peripheral I2C2
I2C_SendData(I2C2, ReadAddr);
// Check if the bytes sent by the host are successful (check EV8)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//Generate I2C2 transmission START condition
I2C_GenerateSTART(I2C2, ENABLE);
//Determine whether the host mode is successful by generating START (detecting EV5)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
//Send the address word to the specified slave I2C device and select the receiving direction
I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Receiver);
// Check if the host receiving mode is successful (check EV6)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
//Read data
while (NumByteToRead)
{
// Check whether the host received the byte successfully (check EV8)
if (I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
if (NumByteToRead == 2)
{
// Enable or disable the specified I2C response function
I2C_AcknowledgeConfig(I2C2, DISABLE);
}
if (NumByteToRead == 1)
{
//Generate I2C2 transmission STOP condition
I2C_GenerateSTOP(I2C2, ENABLE);
}
//Return the most recently received data via I2C2
*pBuffer = I2C_ReceiveData(I2C2);
//Point to the next address
pBuffer++;
NumByteToRead--;
}
}
//Enable I2C2 response function
I2C_AcknowledgeConfig(I2C2, ENABLE);
vu16 SR1_Tmp = 0;
do
{
//Generate I2C2 transmission START condition
I2C_GenerateSTART(I2C2, ENABLE);
//Read the specified I2C register I2C_SR1 and return its value
SR1_Tmp = I2C_ReadRegister(I2C2, I2C_Register_SR1);
//Transmit the address word to the specified slave I2C device and select the sending direction
I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);
} while (!(I2C_ReadRegister(I2C2, I2C_Register_SR1) & 0x0002));//Address sending completed
// Clear the I2Cx response error flag
I2C_ClearFlag(I2C2, I2C_FLAG_AF);
Keywords:STM32
Reference address:STM32 Hardware I2C EEPROM Command Analysis
{
}
void I2C_EE_ByteWrite(u8* pBuffer, u8 WriteAddr)
{
}
void I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)
{
}
void I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)
{
}
void I2C_EE_WaitEepromStandbyState(void)
{
}
Previous article:Configuration of J-Flash ARM
Next article:STM32 timer input capture and output comparison
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
Guess you like
- I created a project with stm32cubemx and then used the module generated by keil RTE, but the code generated by RTE is repeated. How can I disable the duplication?
- 【ESP32-C3-DevKitM-1】LED PWM for ESP32-C3
- CS8626 pin diagram Filter-free, 50W mono Class D power amplifier HT8696 circuit diagram
- 【NUCLEO-L552ZE Review】+wifi module
- Forward - I've learned a lot. Have you ever seen the cross-section of a BGA packaged chip?
- The most complete circuit testing process
- Driver recommendations for three-phase current-source PWM rectifiers
- Clock switching glitch free
- Difference between ARM7 and ARM9
- What do you think of the MicroPython trademark incident?