STM32 Hardware I2C EEPROM Command Analysis

Publisher:LogicLeaperLatest update time:2016-04-08 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
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;
   //EEPROM is set to standby mode
    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();
           }
       }
   }
}
 
 
void I2C_EE_ByteWrite(u8* pBuffer, u8 WriteAddr)
{
   //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);
}
 
void I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)
{
   //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);
}
 
void I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)
{
   //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);
 
}
 
 
void I2C_EE_WaitEepromStandbyState(void)
{
    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

Previous article:Configuration of J-Flash ARM
Next article:STM32 timer input capture and output comparison

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号