Advertisement
pongfactory

I2C EEPROM Exam

Jan 27th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.31 KB | None | 0 0
  1. /**
  2.   *****************************************************************************
  3.   * @title   I2C_EEPROM.c
  4.   * @author  CooCox
  5.   * @date    12 Nov 2013
  6.   * @brief   This example provides a basic example of how to use the I2C soft
  7.   *          ware library and an associate I2C EEPROM driver to communicate w
  8.   *          ith an I2C EEPROM device.
  9.   *******************************************************************************
  10.   */
  11. ////// The above comment is automatically generated by CoIDE ///////////////////
  12.  
  13. /**
  14.   ******************************************************************************
  15.   * @file    I2C/I2C_EEPROM/main.c
  16.   * @author  MCD Application Team
  17.   * @version V1.0.0
  18.   * @date    30-October-2010
  19.   * @brief   Main program body
  20.   ******************************************************************************
  21.   * @attention
  22.   *
  23.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  24.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  25.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  26.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  27.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  28.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  29.   *
  30.   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  31.   ******************************************************************************
  32.   */
  33.  
  34. /* Includes ------------------------------------------------------------------*/
  35. #include <stdio.h>
  36. #include "stm32f10x_usart.h"
  37. #include "stm32f10x_gpio.h"
  38. #include "stm32f10x_i2c.h"
  39. #include "stm32f10x_rcc.h"
  40.  
  41. /***************************************************************************//**
  42.  * Global variables, private define, macro and typedef
  43.  ******************************************************************************/
  44. GPIO_InitTypeDef  GPIO_InitStructure;
  45. USART_InitTypeDef USART_InitStructure;
  46. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
  47.  
  48. #define EEPROM_WriteAddress1    0x50
  49. #define EEPROM_ReadAddress1     0x50
  50. #define BufferSize1             (countof(Tx1_Buffer)-1)
  51. #define BufferSize2             (countof(Tx2_Buffer)-1)
  52. #define EEPROM_WriteAddress2    (EEPROM_WriteAddress1 + BufferSize1)
  53. #define EEPROM_ReadAddress2     (EEPROM_ReadAddress1 + BufferSize1)
  54.  
  55. #define I2C_EE             I2C1
  56. #define I2C_EE_CLK         RCC_APB1Periph_I2C1
  57. #define I2C_EE_GPIO        GPIOB
  58. #define I2C_EE_GPIO_CLK    RCC_APB2Periph_GPIOB
  59. #define I2C_EE_SCL         GPIO_Pin_8
  60. #define I2C_EE_SDA         GPIO_Pin_9
  61.  
  62. #define I2C_Speed              200000
  63. #define I2C_SLAVE_ADDRESS7     0xA0
  64. #define I2C_FLASH_PAGESIZE     32
  65. #define EEPROM_HW_ADDRESS      0xA0   /* E0 = E1 = E2 = 0 */
  66.  
  67. /* Private macro -------------------------------------------------------------*/
  68. #define countof(a) (sizeof(a) / sizeof(*(a)))
  69.  
  70. /* Private variables ---------------------------------------------------------*/
  71. uint16_t EEPROM_ADDRESS;
  72. uint8_t Tx1_Buffer[] = "/* STM32F10x I2C Firmware ";
  73. uint8_t Rx1_Buffer[BufferSize1];
  74. volatile TestStatus TransferStatus1 = FAILED;
  75.  
  76. /***************************************************************************//**
  77.  * Declare function prototypes
  78.  ******************************************************************************/
  79. void RCC_Configuration(void);
  80. void GPIO_Configuration(void);
  81. void USART_Configuration(void);
  82. void I2C_Configuration(void);
  83. void I2C_EE_Init(void);
  84. void I2C_EE_ByteWrite(uint8_t* pBuffer, uint16_t WriteAddr);
  85. void I2C_EE_PageWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t NumByteToWrite);
  86. void I2C_EE_BufferWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite);
  87. void I2C_EE_BufferRead(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead);
  88. void I2C_EE_WaitEepromStandbyState(void);
  89. TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
  90.  
  91. /***************************************************************************//**
  92.  * @brief  First, the content of Tx1_Buffer is written to the EEPROM_WriteAddress1
  93.  *         and the written data are read. The written and the read buffers data are
  94.  *         then compared. Following the read operation, the program wait that the
  95.  *         EEPROM reverts to its Standby state. A second write operation is, then,
  96.  *         performed and this time, Tx2_Buffer is written to EEPROM_WriteAddress2,
  97.  *         which represents the address just after the last written one in the first
  98.  *         write. After completion of the second write operation, the written data
  99.  *         are read. The contents of the written and the read buffers are compared.
  100.  ******************************************************************************/
  101. void I2C_EEPROM(void)
  102. {
  103.     /* System clocks configuration */
  104.     RCC_Configuration();
  105.     /* USART configuration */
  106.     USART_Configuration();
  107.     /* Initialize the I2C EEPROM driver */
  108.     I2C_EE_Init();
  109.  
  110.     /* First write in the memory followed by a read of the written data */
  111.     /* Write on I2C EEPROM from EEPROM_WriteAddress1 */
  112.     I2C_EE_BufferWrite(Tx1_Buffer, EEPROM_WriteAddress1, BufferSize1);
  113.  
  114.     /* Read from I2C EEPROM from EEPROM_ReadAddress1 */
  115.     I2C_EE_BufferRead(Rx1_Buffer, EEPROM_ReadAddress1, BufferSize1);
  116.  
  117.     /* Check if the data written to the memory is read correctly */
  118.     TransferStatus1 = Buffercmp(Tx1_Buffer, Rx1_Buffer, BufferSize1);  
  119. }
  120.  
  121. /***************************************************************************//**
  122.  * @brief  Configures the different system clocks.
  123.  ******************************************************************************/
  124. void RCC_Configuration(void)
  125. {
  126.     /* Setup the microcontroller system. Initialize the Embedded Flash Interface,
  127.        initialize the PLL and update the SystemFrequency variable. */
  128.     SystemInit();
  129. }
  130.  
  131. void GPIO_Configuration(void)
  132. {
  133.     GPIO_InitTypeDef  GPIO_InitStructure;
  134.  
  135.     GPIO_PinRemapConfig(GPIO_Remap_I2C1,ENABLE);
  136.  
  137.     /* Configure I2C_EE pins: SCL and SDA */
  138.     GPIO_InitStructure.GPIO_Pin =  I2C_EE_SCL | I2C_EE_SDA;
  139.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  140.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  141.     GPIO_Init(I2C_EE_GPIO, &GPIO_InitStructure);
  142. }
  143.  
  144. /***************************************************************************//**
  145.  * @brief  Configure USART2
  146.  ******************************************************************************/
  147. void USART_Configuration()
  148. {
  149.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
  150.     GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
  151.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  152.  
  153.     /* Configure USART Tx as alternate function push-pull */
  154.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  155.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  156.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  157.     GPIO_Init(GPIOD, &GPIO_InitStructure);
  158.  
  159.     /* Configure USART Rx as input floating */
  160.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  161.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  162.     GPIO_Init(GPIOD, &GPIO_InitStructure);
  163.  
  164.      /* USART1 configured as follow:
  165.            - BaudRate = 115200 baud
  166.            - Word Length = 8 Bits
  167.            - One Stop Bit
  168.            - No parity
  169.            - Hardware flow control disabled (RTS and CTS signals)
  170.            - Receive and transmit enabled
  171.            - USART Clock disabled
  172.            - USART CPOL: Clock is active low
  173.            - USART CPHA: Data is captured on the middle
  174.            - USART LastBit: The clock pulse of the last data bit is not output to
  175.                             the SCLK pin
  176.      */
  177.      USART_InitStructure.USART_BaudRate            = 115200;
  178.      USART_InitStructure.USART_WordLength          = USART_WordLength_8b;
  179.      USART_InitStructure.USART_StopBits            = USART_StopBits_1;
  180.      USART_InitStructure.USART_Parity              = USART_Parity_No ;
  181.      USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  182.      USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;
  183.      USART_Init(USART2, &USART_InitStructure);
  184.      USART_Cmd(USART2, ENABLE);
  185. }
  186.  
  187. /***************************************************************************//**
  188.  *  @brief  I2C Configuration
  189.  ******************************************************************************/
  190. void I2C_Configuration(void)
  191. {
  192.     I2C_InitTypeDef  I2C_InitStructure;
  193.  
  194.     /* I2C configuration */
  195.     I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  196.     I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  197.     I2C_InitStructure.I2C_OwnAddress1 = I2C_SLAVE_ADDRESS7;
  198.     I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  199.     I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  200.     I2C_InitStructure.I2C_ClockSpeed = I2C_Speed;
  201.  
  202.     /* I2C Peripheral Enable */
  203.     I2C_Cmd(I2C_EE, ENABLE);
  204.     /* Apply I2C configuration after enabling it */
  205.     I2C_Init(I2C_EE, &I2C_InitStructure);
  206. }
  207.  
  208. /***************************************************************************//**
  209.  * @brief  Initializes peripherals used by the I2C EEPROM driver.
  210.  ******************************************************************************/
  211. void I2C_EE_Init()
  212. {
  213.     /* I2C Periph clock enable */
  214.     RCC_APB1PeriphClockCmd(I2C_EE_CLK, ENABLE);
  215.  
  216.     /* GPIO Periph clock enable */
  217.     RCC_APB2PeriphClockCmd(I2C_EE_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
  218.  
  219.     /* GPIO configuration */
  220.     GPIO_Configuration();
  221.  
  222.     /* I2C configuration */
  223.     I2C_Configuration();
  224.  
  225.     /* Select the EEPROM address according to the state of E0, E1, E2 pins */
  226.     EEPROM_ADDRESS = EEPROM_HW_ADDRESS;
  227. }
  228.  
  229. /***************************************************************************//**
  230.  * @brief      Writes one byte to the I2C EEPROM.
  231.  * @param[in]  pBuffer   : pointer to the buffer  containing the data to be
  232.  *                         written to the EEPROM.
  233.  * @param[in]  WriteAddr : EEPROM's internal address to write to.
  234.  * @return     None
  235.  ******************************************************************************/
  236. void I2C_EE_ByteWrite(uint8_t* pBuffer, uint16_t WriteAddr)
  237. {
  238.     /* Send STRAT condition */
  239.     I2C_GenerateSTART(I2C_EE, ENABLE);
  240.  
  241.     /* Test on EV5 and clear it */
  242.     while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_MODE_SELECT));
  243.  
  244.     /* Send EEPROM address for write */
  245.     I2C_Send7bitAddress(I2C_EE, EEPROM_ADDRESS, I2C_Direction_Transmitter);
  246.  
  247.     /* Test on EV6 and clear it */
  248.     while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
  249.  
  250.     /* Send the EEPROM's internal address to write to : MSB of the address first */
  251.     I2C_SendData(I2C_EE, (uint8_t)((WriteAddr & 0xFF00) >> 8));
  252.  
  253.     /* Test on EV8 and clear it */
  254.     while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  255.  
  256.     /* Send the EEPROM's internal address to write to : LSB of the address */
  257.     I2C_SendData(I2C_EE, (uint8_t)(WriteAddr & 0x00FF));
  258.  
  259.     /* Test on EV8 and clear it */
  260.     while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  261.  
  262.     /* Send the byte to be written */
  263.     I2C_SendData(I2C_EE, *pBuffer);
  264.  
  265.     /* Test on EV8 and clear it */
  266.     while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  267.  
  268.     /* Send STOP condition */
  269.     I2C_GenerateSTOP(I2C_EE, ENABLE);
  270. }
  271.  
  272. /***************************************************************************//**
  273.  * @brief      Reads a block of data from the EEPROM.
  274.  * @param[in]  pBuffer : pointer to the buffer that receives the data read
  275.  *                       from the EEPROM.
  276.  * @param[in]  ReadAddr : EEPROM's internal address to read from.
  277.  * @param[in]  NumByteToRead : number of bytes to read from the EEPROM.
  278.  * @return     None
  279.  ******************************************************************************/
  280. void I2C_EE_BufferRead(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead)
  281. {
  282.     /* While the bus is busy */
  283.     while(I2C_GetFlagStatus(I2C_EE, I2C_FLAG_BUSY));
  284.  
  285.     /* Send START condition */
  286.     I2C_GenerateSTART(I2C_EE, ENABLE);
  287.  
  288.     /* Test on EV5 and clear it */
  289.     while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_MODE_SELECT));
  290.  
  291.     /* Send EEPROM address for write */
  292.     I2C_Send7bitAddress(I2C_EE, EEPROM_ADDRESS, I2C_Direction_Transmitter);
  293.  
  294.     /* Test on EV6 and clear it */
  295.     while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
  296.  
  297.     /* Send the EEPROM's internal address to read from: MSB of the address first */
  298.     I2C_SendData(I2C_EE, (uint8_t)((ReadAddr & 0xFF00) >> 8));
  299.  
  300.     /* Test on EV8 and clear it */
  301.     while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  302.  
  303.     /* Send the EEPROM's internal address to read from: LSB of the address */
  304.     I2C_SendData(I2C_EE, (uint8_t)(ReadAddr & 0x00FF));
  305.  
  306.     /* Test on EV8 and clear it */
  307.     while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  308.  
  309.     /* Send STRAT condition a second time */
  310.     I2C_GenerateSTART(I2C_EE, ENABLE);
  311.  
  312.     /* Test on EV5 and clear it */
  313.     while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_MODE_SELECT));
  314.  
  315.     /* Send EEPROM address for read */
  316.     I2C_Send7bitAddress(I2C_EE, EEPROM_ADDRESS, I2C_Direction_Receiver);
  317.  
  318.     /* Test on EV6 and clear it */
  319.     while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
  320.  
  321.     /* While there is data to be read */
  322.     while(NumByteToRead)
  323.     {
  324.         if(NumByteToRead == 1)
  325.         {
  326.             /* Disable Acknowledgement */
  327.             I2C_AcknowledgeConfig(I2C_EE, DISABLE);
  328.  
  329.             /* Send STOP Condition */
  330.             I2C_GenerateSTOP(I2C_EE, ENABLE);
  331.         }
  332.  
  333.     /* Test on EV7 and clear it */
  334.         if(I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_RECEIVED))
  335.         {
  336.             /* Read a byte from the EEPROM */
  337.             *pBuffer = I2C_ReceiveData(I2C_EE);
  338.  
  339.             /* Point to the next location where the byte read will be saved */
  340.             pBuffer++;
  341.  
  342.             /* Decrement the read bytes counter */
  343.             NumByteToRead--;
  344.         }
  345.     }
  346.  
  347.     /* Enable Acknowledgement to be ready for another reception */
  348.     I2C_AcknowledgeConfig(I2C_EE, ENABLE);
  349. }
  350.  
  351. /***************************************************************************//**
  352.  * @brief      Writes buffer of data to the I2C EEPROM.
  353.  * @param[in]  pBuffer : pointer to the buffer  containing the data to be
  354.  *                       written to the EEPROM.
  355.  * @param[in]  WriteAddr : EEPROM's internal address to write to.
  356.  * @param[in]  NumByteToWrite : number of bytes to write to the EEPROM.
  357.  * @return     None
  358.  ******************************************************************************/
  359. void I2C_EE_BufferWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
  360. {
  361.     uint8_t NumOfPage = 0, NumOfSingle = 0, count = 0;
  362.     uint16_t Addr = 0;
  363.  
  364.     Addr = WriteAddr % I2C_FLASH_PAGESIZE;
  365.     count = I2C_FLASH_PAGESIZE - Addr;
  366.     NumOfPage =  NumByteToWrite / I2C_FLASH_PAGESIZE;
  367.     NumOfSingle = NumByteToWrite % I2C_FLASH_PAGESIZE;
  368.  
  369.     /* If WriteAddr is I2C_FLASH_PAGESIZE aligned  */
  370.     if(Addr == 0)
  371.     {
  372.         /* If NumByteToWrite < I2C_FLASH_PAGESIZE */
  373.         if(NumOfPage == 0)
  374.         {
  375.             I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
  376.             I2C_EE_WaitEepromStandbyState();
  377.         }
  378.         /* If NumByteToWrite > I2C_FLASH_PAGESIZE */
  379.         else
  380.         {
  381.             while(NumOfPage--)
  382.             {
  383.                 I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_FLASH_PAGESIZE);
  384.                 I2C_EE_WaitEepromStandbyState();
  385.                 WriteAddr +=  I2C_FLASH_PAGESIZE;
  386.                 pBuffer += I2C_FLASH_PAGESIZE;
  387.             }
  388.  
  389.             if(NumOfSingle!=0)
  390.             {
  391.                 I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
  392.                 I2C_EE_WaitEepromStandbyState();
  393.             }
  394.         }
  395.     }
  396.     /* If WriteAddr is not I2C_FLASH_PAGESIZE aligned  */
  397.     else
  398.     {
  399.         /* If NumByteToWrite < I2C_FLASH_PAGESIZE */
  400.         if(NumOfPage== 0)
  401.         {
  402.             /* If the number of data to be written is more than the remaining space
  403.               in the current page: */
  404.             if (NumByteToWrite > count)
  405.             {
  406.                 /* Write the data conained in same page */
  407.                 I2C_EE_PageWrite(pBuffer, WriteAddr, count);
  408.                 I2C_EE_WaitEepromStandbyState();
  409.  
  410.                 /* Write the remaining data in the following page */
  411.                 I2C_EE_PageWrite((uint8_t*)(pBuffer + count), (WriteAddr + count), (NumByteToWrite - count));
  412.                 I2C_EE_WaitEepromStandbyState();
  413.             }
  414.             else
  415.             {
  416.                 I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
  417.                 I2C_EE_WaitEepromStandbyState();
  418.             }
  419.         }
  420.         /* If NumByteToWrite > I2C_FLASH_PAGESIZE */
  421.         else
  422.         {
  423.             NumByteToWrite -= count;
  424.             NumOfPage =  NumByteToWrite / I2C_FLASH_PAGESIZE;
  425.             NumOfSingle = NumByteToWrite % I2C_FLASH_PAGESIZE;
  426.  
  427.             if(count != 0)
  428.             {
  429.                 I2C_EE_PageWrite(pBuffer, WriteAddr, count);
  430.                 I2C_EE_WaitEepromStandbyState();
  431.                 WriteAddr += count;
  432.                 pBuffer += count;
  433.             }
  434.  
  435.             while(NumOfPage--)
  436.             {
  437.                 I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_FLASH_PAGESIZE);
  438.                 I2C_EE_WaitEepromStandbyState();
  439.                 WriteAddr +=  I2C_FLASH_PAGESIZE;
  440.                 pBuffer += I2C_FLASH_PAGESIZE;
  441.             }
  442.             if(NumOfSingle != 0)
  443.             {
  444.                 I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
  445.                 I2C_EE_WaitEepromStandbyState();
  446.             }
  447.         }
  448.     }
  449. }
  450.  
  451. /***************************************************************************//**
  452.  * @brief      Writes more than one byte to the EEPROM with a single WRITE cycle.
  453.  *             Note: The number of byte can't exceed the EEPROM page size.
  454.  * @param[in]  pBuffer : pointer to the buffer containing the data to be
  455.  *                       written to the EEPROM.
  456.  * @param[in]  WriteAddr : EEPROM's internal address to write to.
  457.  * @param[in]  NumByteToWrite : number of bytes to write to the EEPROM.
  458.  * @return     None
  459.  ******************************************************************************/
  460. void I2C_EE_PageWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t NumByteToWrite)
  461. {
  462.     /* While the bus is busy */
  463.     while(I2C_GetFlagStatus(I2C_EE, I2C_FLAG_BUSY));
  464.  
  465.     /* Send START condition */
  466.     I2C_GenerateSTART(I2C_EE, ENABLE);
  467.  
  468.     /* Test on EV5 and clear it */
  469.     while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_MODE_SELECT));
  470.  
  471.     /* Send EEPROM address for write */
  472.     I2C_Send7bitAddress(I2C_EE, EEPROM_ADDRESS, I2C_Direction_Transmitter);
  473.  
  474.     /* Test on EV6 and clear it */
  475.     while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
  476.  
  477.     /* Send the EEPROM's internal address to write to : MSB of the address first */
  478.     I2C_SendData(I2C_EE, (uint8_t)((WriteAddr & 0xFF00) >> 8));
  479.  
  480.     /* Test on EV8 and clear it */
  481.     while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  482.  
  483.     /* Send the EEPROM's internal address to write to : LSB of the address */
  484.     I2C_SendData(I2C_EE, (uint8_t)(WriteAddr & 0x00FF));
  485.  
  486.     /* Test on EV8 and clear it */
  487.     while(! I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  488.  
  489.     /* While there is data to be written */
  490.     while(NumByteToWrite--)
  491.     {
  492.         /* Send the current byte */
  493.         I2C_SendData(I2C_EE, *pBuffer);
  494.  
  495.         /* Point to the next byte to be written */
  496.         pBuffer++;
  497.  
  498.         /* Test on EV8 and clear it */
  499.         while (!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  500.     }
  501.  
  502.     /* Send STOP condition */
  503.     I2C_GenerateSTOP(I2C_EE, ENABLE);
  504. }
  505.  
  506. /***************************************************************************//**
  507.  * @brief  Wait for EEPROM Standby state
  508.  ******************************************************************************/
  509. void I2C_EE_WaitEepromStandbyState(void)
  510. {
  511.     __IO uint16_t SR1_Tmp = 0;
  512.  
  513.     do
  514.     {
  515.         /* Send START condition */
  516.         I2C_GenerateSTART(I2C_EE, ENABLE);
  517.  
  518.         /* Read I2C_EE SR1 register to clear pending flags */
  519.         SR1_Tmp = I2C_ReadRegister(I2C_EE, I2C_Register_SR1);
  520.  
  521.         /* Send EEPROM address for write */
  522.         I2C_Send7bitAddress(I2C_EE, EEPROM_ADDRESS, I2C_Direction_Transmitter);
  523.     }while(!(I2C_ReadRegister(I2C_EE, I2C_Register_SR1) & 0x0002));
  524.  
  525.     /* Clear AF flag */
  526.     I2C_ClearFlag(I2C_EE, I2C_FLAG_AF);
  527.  
  528.     /* STOP condition */
  529.     I2C_GenerateSTOP(I2C_EE, ENABLE);
  530. }
  531.  
  532. /**************************************************************************//**
  533.  * @brief    : Compare two buffers.
  534.  * @param[in]  pBuffer  : buffer to be compared
  535.  * @param[in]  PBuffer1 : buffer to be compared
  536.  * @param[in]  BufferLength : buffer's length
  537.  * @return   :
  538.  *      @li PASSED : pBuffer identical to pBuffer1
  539.  *      @li FAILED : pBuffer differs from pBuffer1
  540.  ******************************************************************************/
  541. TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
  542. {
  543.     while(BufferLength--)
  544.     {
  545.         if(*pBuffer1 != *pBuffer2)
  546.         {
  547.             return FAILED;
  548.         }
  549.         pBuffer1++;
  550.         pBuffer2++;
  551.     }
  552.     return PASSED;
  553. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement