STM32F10x_StdPeriph_Examples/I2C/EEPROM/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    I2C/EEPROM/main.c 
00004   * @author  MCD Application Team
00005   * @version V3.4.0
00006   * @date    10/15/2010
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @copy
00010   *
00011   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017   *
00018   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00019   */ 
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32_eval_i2c_ee.h"
00023 
00024 
00025 #ifdef USE_STM3210E_EVAL
00026  #include "stm3210e_eval_lcd.h"
00027 #elif defined(USE_STM3210B_EVAL)
00028  #include "stm3210b_eval_lcd.h"
00029 #elif defined(USE_STM3210C_EVAL)
00030  #include "stm3210c_eval_lcd.h"
00031 #elif defined(USE_STM32100B_EVAL)
00032  #include "stm32100b_eval_lcd.h"
00033 #elif defined(USE_STM32100E_EVAL)
00034  #include "stm32100e_eval_lcd.h"
00035 #endif /* USE_STM3210E_EVAL */  
00036 
00037 /** @addtogroup STM32F10x_StdPeriph_Examples
00038   * @{
00039   */
00040 
00041 /** @addtogroup I2C_EEPROM
00042   * @{
00043   */ 
00044 
00045 /* Private typedef -----------------------------------------------------------*/
00046 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00047 
00048 /* Private define ------------------------------------------------------------*/
00049 /* Uncomment the following line to enable using LCD screen for messages display */
00050 #define ENABLE_LCD_MSG_DISPLAY
00051 
00052 #define sEE_WRITE_ADDRESS1        0x50
00053 #define sEE_READ_ADDRESS1         0x50
00054 #define BUFFER_SIZE1             (countof(Tx1_Buffer)-1)
00055 #define BUFFER_SIZE2             (countof(Tx2_Buffer)-1)
00056 #define sEE_WRITE_ADDRESS2       (sEE_WRITE_ADDRESS1 + BUFFER_SIZE1)
00057 #define sEE_READ_ADDRESS2        (sEE_READ_ADDRESS1 + BUFFER_SIZE1)
00058 
00059 /* Private macro -------------------------------------------------------------*/
00060 #define countof(a) (sizeof(a) / sizeof(*(a)))
00061 
00062 /* Private variables ---------------------------------------------------------*/
00063 uint8_t Tx1_Buffer[] = "/* STM32F10xx I2C Firmware Library EEPROM driver example: \
00064                         buffer 1 transfer into address sEE_WRITE_ADDRESS1 */ \
00065                         Example Description \
00066                         This firmware provides a basic example of how to use the I2C firmware library and\
00067                         an associate I2C EEPROM driver to communicate with an I2C EEPROM device (here the\
00068                         example is interfacing with M24C64 EEPROM)\
00069                           \
00070                         I2C peripheral is configured in Master transmitter during write operation and in\
00071                         Master receiver during read operation from I2C EEPROM. \
00072                           \
00073                         The peripheral used is I2C1 but can be configured by modifying the defines values\
00074                         in stm32xxxx_eval.h file. The speed is set to 200kHz and can be configured by \
00075                         modifying the relative define in stm32_eval_i2c_ee.h file.\
00076                          \
00077                         For M24C64 devices all the memory is accessible through the two-bytes \
00078                         addressing mode and need to define block addresses. In this case, only the physical \
00079                         address has to be defined (according to the address pins (E0,E1 and E2) connection).\
00080                         This address is defined in i2c_ee.h (default is 0xA0: E0, E1 and E2 tied to ground).\
00081                         The EEPROM addresses where the program start the write and the read operations \
00082                         is defined in the main.c file. \
00083                          \
00084                         First, the content of Tx1_Buffer is written to the EEPROM_WriteAddress1 and the\
00085                         written data are read. The written and the read buffers data are then compared.\
00086                         Following the read operation, the program waits that the EEPROM reverts to its \
00087                         Standby state. A second write operation is, then, performed and this time, Tx2_Buffer\
00088                         is written to EEPROM_WriteAddress2, which represents the address just after the last \
00089                         written one in the first write. After completion of the second write operation, the \
00090                         written data are read. The contents of the written and the read buffers are compared.\
00091                          \
00092                         All transfers are managed in DMA mode (except when 1-byte read/write operation is\
00093                         required). Once sEE_ReadBuffer() or sEE_WriteBuffer() function is called, the \
00094                         use application may perform other tasks in parallel while Read/Write operation is\
00095                         managed by DMA.\
00096                           \
00097                         This example provides the possibility to use the STM32XXXX-EVAL LCD screen for\
00098                         messages display (transfer status: Ongoing, PASSED, FAILED).\
00099                         To enable this option uncomment the define ENABLE_LCD_MSG_DISPLAY in the main.c\
00100                         file.                                                                              ";
00101 uint8_t Tx2_Buffer[] = "/* STM32F10xx I2C Firmware Library EEPROM driver example: \
00102                         buffer 2 transfer into address sEE_WRITE_ADDRESS2 */";
00103 uint8_t Rx1_Buffer[BUFFER_SIZE1], Rx2_Buffer[BUFFER_SIZE2];
00104 volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
00105 volatile uint16_t NumDataRead = 0;
00106 
00107 /* Private functions ---------------------------------------------------------*/
00108 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
00109 
00110 /**
00111   * @brief   Main program
00112   * @param  None
00113   * @retval None
00114   */
00115 int main(void)
00116 {
00117   /*!< At this stage the microcontroller clock setting is already configured, 
00118        this is done through SystemInit() function which is called from startup
00119        file (startup_stm32f10x_xx.s) before to branch to application main.
00120        To reconfigure the default setting of SystemInit() function, refer to
00121        system_stm32f10x.c file
00122      */     
00123 
00124 #ifdef ENABLE_LCD_MSG_DISPLAY
00125   /* Initialize the LCD screen for information display */
00126  #ifdef USE_STM3210E_EVAL
00127   STM3210E_LCD_Init();
00128  #elif defined(USE_STM3210B_EVAL)
00129   STM3210B_LCD_Init();
00130  #elif defined(USE_STM3210C_EVAL)
00131   STM3210C_LCD_Init();
00132  #elif defined(USE_STM32100B_EVAL)
00133   STM32100B_LCD_Init();
00134  #elif defined(USE_STM32100E_EVAL)
00135   STM32100E_LCD_Init();
00136  #endif /* USE_STM3210E_EVAL */  
00137   
00138   /* Display application information */
00139   LCD_Clear(LCD_COLOR_BLUE);  
00140   LCD_SetBackColor(LCD_COLOR_BLUE);
00141   LCD_SetTextColor(LCD_COLOR_WHITE);
00142   LCD_DisplayStringLine(LCD_LINE_0, "SMT32F1xx FW Library");
00143   LCD_DisplayStringLine(LCD_LINE_1, "   EEPROM Example   ");
00144 #endif /* ENABLE_LCD_MSG_DISPLAY */    
00145   
00146   /* Initialize the I2C EEPROM driver ----------------------------------------*/
00147   sEE_Init();  
00148 
00149   /* First write in the memory followed by a read of the written data --------*/
00150   /* Write on I2C EEPROM from sEE_WRITE_ADDRESS1 */
00151   sEE_WriteBuffer(Tx1_Buffer, sEE_WRITE_ADDRESS1, BUFFER_SIZE1);
00152 
00153   /* Set the Number of data to be read */
00154   NumDataRead = BUFFER_SIZE1;
00155   
00156   /* Read from I2C EEPROM from sEE_READ_ADDRESS1 */
00157   sEE_ReadBuffer(Rx1_Buffer, sEE_READ_ADDRESS1, (uint16_t *)(&NumDataRead)); 
00158 
00159 #ifdef ENABLE_LCD_MSG_DISPLAY  
00160   LCD_DisplayStringLine(LCD_LINE_3, " Transfer 1 Ongoing ");
00161 #endif /* ENABLE_LCD_MSG_DISPLAY */   
00162 
00163   /* Wait till DMA transfer is compelete (Tranfer complete interrupt handler 
00164     resets the variable holding the number of data to be read) */
00165   while (NumDataRead > 0)
00166   {    
00167     /* Starting from this point, if the requested number of data is higher than 1, 
00168        then only the DMA is managing the data transfer. Meanwhile, CPU is free to 
00169        perform other tasks:
00170        
00171       // Add your code here: 
00172       //...
00173       //...
00174   
00175        For simplicity reasons, this example is just waiting till the end of the 
00176        transfer. */    
00177   }
00178   
00179   /* Check if the data written to the memory is read correctly */
00180   TransferStatus1 = Buffercmp(Tx1_Buffer, Rx1_Buffer, BUFFER_SIZE1);
00181   /* TransferStatus1 = PASSED, if the transmitted and received data 
00182      to/from the EEPROM are the same */
00183   /* TransferStatus1 = FAILED, if the transmitted and received data 
00184      to/from the EEPROM are different */
00185 #ifdef ENABLE_LCD_MSG_DISPLAY  
00186   if (TransferStatus1 == PASSED)
00187   {
00188     LCD_DisplayStringLine(LCD_LINE_3, " Transfer 1 PASSED  ");
00189   }
00190   else
00191   {
00192     LCD_DisplayStringLine(LCD_LINE_3, " Transfer 1 FAILED  ");
00193   }  
00194 #endif /* ENABLE_LCD_MSG_DISPLAY */
00195 
00196 /*----------------------------------
00197   
00198                                     ------------------------------------------*/
00199   
00200   /* Second write in the memory followed by a read of the written data -------*/
00201   /* Write on I2C EEPROM from sEE_WRITE_ADDRESS2 */
00202   sEE_WriteBuffer(Tx2_Buffer, sEE_WRITE_ADDRESS2, BUFFER_SIZE2);
00203 
00204   /* Set the Number of data to be read */
00205   NumDataRead = BUFFER_SIZE2;  
00206   
00207   /* Read from I2C EEPROM from sEE_READ_ADDRESS2 */
00208   sEE_ReadBuffer(Rx2_Buffer, sEE_READ_ADDRESS2, (uint16_t *)(&NumDataRead));
00209 
00210 #ifdef ENABLE_LCD_MSG_DISPLAY   
00211   LCD_DisplayStringLine(LCD_LINE_5, " Transfer 2 Ongoing ");
00212 #endif /* ENABLE_LCD_MSG_DISPLAY */  
00213   
00214   /* Wait till DMA transfer is compelete (Tranfer complete interrupt handler 
00215     resets the variable holding the number of data to be read) */
00216   while (NumDataRead > 0)
00217   {
00218     /* Starting from this point, if the requested number of data is higher than 1, 
00219        then only the DMA is managing the data transfer. Meanwhile, CPU is free to 
00220        perform other tasks:
00221        
00222       // Add your code here: 
00223       //...
00224       //...
00225   
00226        For simplicity reasons, this example is just waiting till the end of the 
00227        transfer. */    
00228   }
00229   
00230   /* Check if the data written to the memory is read correctly */
00231   TransferStatus2 = Buffercmp(Tx2_Buffer, Rx2_Buffer, BUFFER_SIZE2);
00232   /* TransferStatus2 = PASSED, if the transmitted and received data 
00233      to/from the EEPROM are the same */
00234   /* TransferStatus2 = FAILED, if the transmitted and received data 
00235      to/from the EEPROM are different */
00236 #ifdef ENABLE_LCD_MSG_DISPLAY   
00237   if (TransferStatus1 == PASSED)
00238   {
00239     LCD_DisplayStringLine(LCD_LINE_5, " Transfer 2 PASSED  ");
00240   }
00241   else
00242   {
00243     LCD_DisplayStringLine(LCD_LINE_5, " Transfer 2 FAILED  ");
00244   }  
00245 #endif /* ENABLE_LCD_MSG_DISPLAY */
00246   
00247   /* Free all used resources */
00248   sEE_DeInit();
00249 
00250 #ifdef ENABLE_LCD_MSG_DISPLAY
00251   /* Display end of example information */
00252   LCD_DisplayStringLine(LCD_LINE_7, "---End Of Example---");
00253 #endif /* ENABLE_LCD_MSG_DISPLAY */  
00254 
00255   while (1)
00256   {
00257   }
00258 }
00259 
00260 #ifndef USE_DEFAULT_TIMEOUT_CALLBACK
00261 /**
00262   * @brief  Example of timeout situation management.
00263   * @param  None.
00264   * @retval None.
00265   */
00266 uint32_t sEE_TIMEOUT_UserCallback(void)
00267 {
00268   /* Use application may try to recover the communication by resetting I2C
00269     peripheral (calling the function I2C_SoftwareResetCmd()) then re-start
00270     the transmission/reception from a previously stored recover point.
00271     For simplicity reasons, this example only shows a basic way for errors 
00272     managements which consists of stopping all the process and requiring system
00273     reset. */
00274   
00275 #ifdef ENABLE_LCD_MSG_DISPLAY   
00276   /* Display error message on screen */
00277   LCD_Clear(LCD_COLOR_RED);  
00278   LCD_DisplayStringLine(LCD_LINE_4, "Communication ERROR!");
00279   LCD_DisplayStringLine(LCD_LINE_5, "Try again after res-");
00280   LCD_DisplayStringLine(LCD_LINE_6, "  etting the Board  ");
00281 #endif /* ENABLE_LCD_MSG_DISPLAY */
00282   
00283   /* Block communication and all processes */
00284   while (1)
00285   {   
00286   }  
00287 }
00288 #endif /* USE_DEFAULT_TIMEOUT_CALLBACK */
00289 
00290 /**
00291   * @brief  Compares two buffers.
00292   * @param  pBuffer1, pBuffer2: buffers to be compared.
00293   * @param  BufferLength: buffer's length
00294   * @retval PASSED: pBuffer1 identical to pBuffer2
00295   *         FAILED: pBuffer1 differs from pBuffer2
00296   */
00297 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
00298 {
00299   while(BufferLength--)
00300   {
00301     if(*pBuffer1 != *pBuffer2)
00302     {
00303       return FAILED;
00304     }
00305     
00306     pBuffer1++;
00307     pBuffer2++;
00308   }
00309 
00310   return PASSED;  
00311 }
00312 
00313 #ifdef  USE_FULL_ASSERT
00314 
00315 /**
00316   * @brief  Reports the name of the source file and the source line number
00317   *         where the assert_param error has occurred.
00318   * @param  file: pointer to the source file name
00319   * @param  line: assert_param error line source number
00320   * @retval None
00321   */
00322 void assert_failed(uint8_t* file, uint32_t line)
00323 { 
00324   /* User can add his own implementation to report the file name and line number,
00325      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00326 
00327   /* Infinite loop */
00328   while (1)
00329   {
00330   }
00331 }
00332 #endif
00333 
00334 /**
00335   * @}
00336   */ 
00337 
00338 /**
00339   * @}
00340   */ 
00341 
00342 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32