STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/I2C/I2C_EEPROM/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    I2C/I2C_EEPROM/main.c 
00004   * @author  MCD Application Team
00005   * @version V1.4.0
00006   * @date    24-July-2014
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
00012   *
00013   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00014   * You may not use this file except in compliance with the License.
00015   * You may obtain a copy of the License at:
00016   *
00017   *        http://www.st.com/software_license_agreement_liberty_v2
00018   *
00019   * Unless required by applicable law or agreed to in writing, software 
00020   * distributed under the License is distributed on an "AS IS" BASIS, 
00021   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00022   * See the License for the specific language governing permissions and
00023   * limitations under the License.
00024   *
00025   ******************************************************************************
00026   */
00027 
00028 
00029 /* Includes ------------------------------------------------------------------*/
00030 #include "main.h"
00031 
00032 /** @addtogroup STM32F0xx_StdPeriph_Examples
00033   * @{
00034   */
00035 
00036 /** @addtogroup I2C_EEPROM
00037   * @{
00038   */
00039   
00040 /* Private typedef -----------------------------------------------------------*/
00041 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00042 
00043 /* Private define ------------------------------------------------------------*/
00044 /* Uncomment the following line to enable using LCD screen for messages display */
00045 #define ENABLE_LCD_MSG_DISPLAY
00046 
00047 #define sEE_WRITE_ADDRESS1        0x50
00048 #define sEE_READ_ADDRESS1         0x50
00049 #define BUFFER_SIZE1             (countof(Tx1Buffer)-1)
00050 #define BUFFER_SIZE2             (countof(Tx2Buffer)-1)
00051 #define sEE_WRITE_ADDRESS2       (sEE_WRITE_ADDRESS1 + BUFFER_SIZE1)
00052 #define sEE_READ_ADDRESS2        (sEE_READ_ADDRESS1 + BUFFER_SIZE1)
00053 
00054 /* Private macro -------------------------------------------------------------*/
00055 #define countof(a) (sizeof(a) / sizeof(*(a)))
00056 
00057 /* Private variables ---------------------------------------------------------*/
00058 uint8_t Tx1Buffer[] = "/* STM32F0xx I2C Firmware Library EEPROM driver example: \
00059                         This firmware provides a basic example of how to use the I2C firmware library and\
00060                         an associate I2C EEPROM driver to communicate with an I2C EEPROM device \
00061                         I2C peripheral is configured in Master transmitter during write operation and in\
00062                         Master receiver during read operation from I2C EEPROM.*/";
00063 
00064 uint8_t Tx2Buffer[] = "/* STM32F0xx I2C Firmware Library EEPROM driver example : \
00065                         I2C1 interfacing with M24LR64 EEPROM */";
00066 
00067 uint8_t Rx1Buffer[BUFFER_SIZE1], Rx2Buffer[BUFFER_SIZE2]; 
00068 volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
00069 volatile uint16_t NumDataRead = 0;
00070 
00071 /* Private function prototypes -----------------------------------------------*/
00072 /* Private functions ---------------------------------------------------------*/
00073 static TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
00074 
00075 /**
00076   * @brief  Main program.
00077   * @param  None
00078   * @retval None
00079   */
00080 int main(void)
00081 {
00082   /*!< At this stage the microcontroller clock setting is already configured, 
00083        this is done through SystemInit() function which is called from startup
00084        file (startup_stm32f0xx.s) before to branch to application main.
00085        To reconfigure the default setting of SystemInit() function, refer to
00086        system_stm32f0xx.c file
00087      */
00088 
00089 #ifdef ENABLE_LCD_MSG_DISPLAY
00090   /* Initialize the LCD screen for information display */
00091 #ifdef USE_STM320518_EVAL
00092   STM320518_LCD_Init();
00093 #else 
00094   STM32072B_LCD_Init();
00095 #endif /* USE_STM320518_EVAL */
00096   
00097   LCD_Clear(LCD_COLOR_BLUE);  
00098   LCD_SetBackColor(LCD_COLOR_BLUE);
00099   LCD_SetTextColor(LCD_COLOR_WHITE);
00100   LCD_DisplayStringLine(LCD_LINE_0, "STM32F0xx FW Library");
00101   LCD_DisplayStringLine(LCD_LINE_1, "   EEPROM Example   ");
00102 #endif /* ENABLE_LCD_MSG_DISPLAY */  
00103   
00104   /* Initialize the I2C EEPROM driver ----------------------------------------*/
00105   sEE_Init();  
00106 
00107   /* First write in the memory followed by a read of the written data --------*/
00108   /* Write on I2C EEPROM from sEE_WRITE_ADDRESS1 */
00109   sEE_WriteBuffer(Tx1Buffer, sEE_WRITE_ADDRESS1, BUFFER_SIZE1); 
00110 
00111   /* Wait for EEPROM standby state */
00112   sEE_WaitEepromStandbyState();  
00113   
00114   /* Set the Number of data to be read */
00115   NumDataRead = BUFFER_SIZE1;
00116   
00117   /* Read from I2C EEPROM from sEE_READ_ADDRESS1 */
00118   sEE_ReadBuffer(Rx1Buffer, sEE_READ_ADDRESS1, (uint16_t *)(&NumDataRead)); 
00119  
00120 #ifdef ENABLE_LCD_MSG_DISPLAY  
00121   LCD_DisplayStringLine(LCD_LINE_3, " Transfer 1 Ongoing ");
00122 #endif /* ENABLE_LCD_MSG_DISPLAY */ 
00123   
00124   /* Check if the data written to the memory is read correctly */
00125   TransferStatus1 = Buffercmp(Tx1Buffer, Rx1Buffer, BUFFER_SIZE1);
00126   /* TransferStatus1 = PASSED, if the transmitted and received data 
00127      to/from the EEPROM are the same */
00128   /* TransferStatus1 = FAILED, if the transmitted and received data 
00129      to/from the EEPROM are different */
00130 #ifdef ENABLE_LCD_MSG_DISPLAY  
00131   if (TransferStatus1 == PASSED)
00132   {
00133     LCD_DisplayStringLine(LCD_LINE_3, " Transfer 1 PASSED  ");
00134   }
00135   else
00136   {
00137     LCD_DisplayStringLine(LCD_LINE_3, " Transfer 1 FAILED  ");
00138   }  
00139 #endif /* ENABLE_LCD_MSG_DISPLAY */  
00140 
00141   /* Second write in the memory followed by a read of the written data -------*/
00142   /* Write on I2C EEPROM from sEE_WRITE_ADDRESS2 */
00143   sEE_WriteBuffer(Tx2Buffer, sEE_WRITE_ADDRESS2, BUFFER_SIZE2); 
00144 
00145   /* Wait for EEPROM standby state */
00146   sEE_WaitEepromStandbyState();  
00147   
00148   /* Set the Number of data to be read */
00149   NumDataRead = BUFFER_SIZE2;  
00150   
00151   /* Read from I2C EEPROM from sEE_READ_ADDRESS2 */
00152   sEE_ReadBuffer(Rx2Buffer, sEE_READ_ADDRESS2, (uint16_t *)(&NumDataRead));
00153 
00154 #ifdef ENABLE_LCD_MSG_DISPLAY   
00155   LCD_DisplayStringLine(LCD_LINE_5, " Transfer 2 Ongoing ");
00156 #endif /* ENABLE_LCD_MSG_DISPLAY */  
00157   
00158   /* Check if the data written to the memory is read correctly */
00159   TransferStatus2 = Buffercmp(Tx2Buffer, Rx2Buffer, BUFFER_SIZE2);
00160   /* TransferStatus2 = PASSED, if the transmitted and received data 
00161      to/from the EEPROM are the same */
00162   /* TransferStatus2 = FAILED, if the transmitted and received data 
00163      to/from the EEPROM are different */
00164 #ifdef ENABLE_LCD_MSG_DISPLAY   
00165   if (TransferStatus2 == PASSED)
00166   {
00167     LCD_DisplayStringLine(LCD_LINE_5, " Transfer 2 PASSED  ");
00168   }
00169   else
00170   {
00171     LCD_DisplayStringLine(LCD_LINE_5, " Transfer 2 FAILED  ");
00172   }  
00173 #endif /* ENABLE_LCD_MSG_DISPLAY */
00174   
00175   /* Free all used resources */
00176   sEE_DeInit();
00177 
00178   while (1)
00179   {
00180   }
00181 }
00182 
00183 #ifndef USE_DEFAULT_TIMEOUT_CALLBACK
00184 /**
00185   * @brief  Example of timeout situation management.
00186   * @param  None.
00187   * @retval None.
00188   */
00189 uint32_t sEE_TIMEOUT_UserCallback(void)
00190 {
00191   /* Use application may try to recover the communication by resetting I2C
00192     peripheral (calling the function I2C_SoftwareResetCmd()) then re-start
00193     the transmission/reception from a previously stored recover point.
00194     For simplicity reasons, this example only shows a basic way for errors 
00195     managements which consists of stopping all the process and requiring system
00196     reset. */
00197   
00198 #ifdef ENABLE_LCD_MSG_DISPLAY   
00199   /* Display error message on screen */
00200   LCD_Clear(LCD_COLOR_RED);  
00201   LCD_DisplayStringLine(LCD_LINE_4, "Communication ERROR!");
00202   LCD_DisplayStringLine(LCD_LINE_5, "Try again after res-");
00203   LCD_DisplayStringLine(LCD_LINE_6, "  etting the Board  ");
00204 #endif /* ENABLE_LCD_MSG_DISPLAY */
00205   
00206   /* Block communication and all processes */
00207   while (1)
00208   {   
00209   }  
00210 }
00211 #endif /* USE_DEFAULT_TIMEOUT_CALLBACK */
00212 
00213 /**
00214   * @brief  Compares two buffers.
00215   * @param  pBuffer1, pBuffer2: buffers to be compared.
00216   * @param  BufferLength: buffer's length
00217   * @retval PASSED: pBuffer1 identical to pBuffer2
00218   *         FAILED: pBuffer1 differs from pBuffer2
00219   */
00220 static TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
00221 {
00222   while(BufferLength--)
00223   {
00224     if(*pBuffer1 != *pBuffer2)
00225     {
00226       return FAILED;
00227     }
00228     
00229     pBuffer1++;
00230     pBuffer2++;
00231   }
00232 
00233   return PASSED;  
00234 }
00235 
00236 #ifdef  USE_FULL_ASSERT
00237 
00238 /**
00239   * @brief  Reports the name of the source file and the source line number
00240   *         where the assert_param error has occurred.
00241   * @param  file: pointer to the source file name
00242   * @param  line: assert_param error line source number
00243   * @retval None
00244   */
00245 void assert_failed(uint8_t* file, uint32_t line)
00246 { 
00247   /* User can add his own implementation to report the file name and line number,
00248      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00249 
00250   /* Infinite loop */
00251   while (1)
00252   {
00253   }
00254 }
00255 #endif
00256 
00257 /**
00258   * @}
00259   */
00260 
00261 /**
00262   * @}
00263   */
00264 
00265 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

 For complete documentation on STM32 Microcontrollers visit www.st.com/STM32