📄 main.c
字号:
/**
******************************************************************************
* @file I2C/M24C08_EEPROM/main.c
* @author MCD Application Team
* @version V3.0.0
* @date 04/06/2009
* @brief Main program body
******************************************************************************
* @copy
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2009 STMicroelectronics</center></h2>
*/
/* Includes ------------------------------------------------------------------*/
#include "i2c_ee.h"
/** @addtogroup StdPeriph_Examples
* @{
*/
/** @addtogroup I2C_M24C08_EEPROM
* @{
*/
/* Private typedef -----------------------------------------------------------*/
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
/* Private define ------------------------------------------------------------*/
#define EEPROM_WriteAddress1 0x05
#define EEPROM_ReadAddress1 0x05
#define BufferSize1 (countof(Tx1_Buffer)-1)
#define BufferSize2 (countof(Tx2_Buffer)-1)
#define EEPROM_WriteAddress2 (EEPROM_WriteAddress1 + BufferSize1)
#define EEPROM_ReadAddress2 (EEPROM_ReadAddress1 + BufferSize1)
/* Private macro -------------------------------------------------------------*/
#define countof(a) (sizeof(a) / sizeof(*(a)))
/* Private variables ---------------------------------------------------------*/
uint8_t Tx1_Buffer[] = "/* STM32F10x I2C Firmware ";
uint8_t Tx2_Buffer[] = "Library Example */";
uint8_t Rx1_Buffer[BufferSize1], Rx2_Buffer[BufferSize2];
volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
/* Private functions ---------------------------------------------------------*/
void RCC_Configuration(void);
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
/**
* @brief Main program
* @param None
* @retval : None
*/
int main(void)
{
/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();
/* Initialize the I2C EEPROM driver ----------------------------------------*/
I2C_EE_Init();
/* First write in the memory followed by a read of the written data --------*/
/* Write on I2C EEPROM from EEPROM_WriteAddress1 */
I2C_EE_BufferWrite(Tx1_Buffer, EEPROM_WriteAddress1, BufferSize1);
/* Read from I2C EEPROM from EEPROM_ReadAddress1 */
I2C_EE_BufferRead(Rx1_Buffer, EEPROM_ReadAddress1, BufferSize1);
/* Check if the data written to the memory is read correctly */
TransferStatus1 = Buffercmp(Tx1_Buffer, Rx1_Buffer, BufferSize1);
/* TransferStatus1 = PASSED, if the transmitted and received data
to/from the EEPROM are the same */
/* TransferStatus1 = FAILED, if the transmitted and received data
to/from the EEPROM are different */
/* Wait for EEPROM standby state */
I2C_EE_WaitEepromStandbyState();
/* Second write in the memory followed by a read of the written data -------*/
/* Write on I2C EEPROM from EEPROM_WriteAddress2 */
I2C_EE_BufferWrite(Tx2_Buffer, EEPROM_WriteAddress2, BufferSize2);
/* Read from I2C EEPROM from EEPROM_ReadAddress2 */
I2C_EE_BufferRead(Rx2_Buffer, EEPROM_ReadAddress2, BufferSize2);
/* Check if the data written to the memory is read correctly */
TransferStatus2 = Buffercmp(Tx2_Buffer, Rx2_Buffer, BufferSize2);
/* TransferStatus2 = PASSED, if the transmitted and received data
to/from the EEPROM are the same */
/* TransferStatus2 = FAILED, if the transmitted and received data
to/from the EEPROM are different */
while (1)
{
}
}
/**
* @brief Configures the different system clocks.
* @param None
* @retval : None
*/
void RCC_Configuration(void)
{
/* Setup the microcontroller system. Initialize the Embedded Flash Interface,
initialize the PLL and update the SystemFrequency variable. */
SystemInit();
/* Enable peripheral clocks --------------------------------------------------*/
/* GPIOB Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* I2C1 Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
}
/**
* @brief Compares two buffers.
* @param pBuffer1, pBuffer2: buffers to be compared.
* @param BufferLength: buffer's length
* @retval : PASSED: pBuffer1 identical to pBuffer2
* FAILED: pBuffer1 differs from pBuffer2
*/
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
{
while(BufferLength--)
{
if(*pBuffer1 != *pBuffer2)
{
return FAILED;
}
pBuffer1++;
pBuffer2++;
}
return PASSED;
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval : None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -