STM8S/A Standard Peripherals Firmware Library: main.c Source File

STM8S/A

STM8S_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  V2.2.0
00006   * @date     30-September-2014
00007   * @brief   This file contains the main function for I2C EEPROM Read Write example.
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 /* Includes ------------------------------------------------------------------*/
00029 #include "stm8s.h"
00030 #include "stm8s_eval_i2c_ee.h"
00031 #include "stm8s_eval_lcd.h"
00032 
00033 /**
00034   * @addtogroup I2C_EEPROM
00035   * @{
00036   */
00037 
00038 /* Private typedef -----------------------------------------------------------*/
00039 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00040 
00041 /* Private define ------------------------------------------------------------*/
00042 /* Uncomment the following line to enable using LCD screen for messages display */
00043 #define ENABLE_LCD_MSG_DISPLAY
00044 
00045 #define sEE_WRITE_ADDRESS1        0x50
00046 #define sEE_READ_ADDRESS1         0x50
00047 #define BUFFER_SIZE1             (countof(Tx1_Buffer)-1)
00048 #define BUFFER_SIZE2             (countof(Tx2_Buffer)-1)
00049 #define sEE_WRITE_ADDRESS2       (sEE_WRITE_ADDRESS1 + BUFFER_SIZE1)
00050 #define sEE_READ_ADDRESS2        (sEE_READ_ADDRESS1 + BUFFER_SIZE1)
00051 
00052 /* Private macro -------------------------------------------------------------*/
00053 #define countof(a) (sizeof(a) / sizeof(*(a)))
00054 
00055 /* Private variables ---------------------------------------------------------*/
00056 uint8_t Tx1_Buffer[] = "/* STM8S I2C Firmware Library EEPROM driver example: \
00057                         buffer 1 transfer into address sEE_WRITE_ADDRESS1 */ \
00058                         Example Description \
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 (here the\
00061                         example is interfacing with M24C64 EEPROM)\
00062                           \
00063                         I2C peripheral is configured in Master transmitter during write operation and in\
00064                         Master receiver during read operation from I2C EEPROM. \
00065                           \
00066                         I2C peripheral speed is set to 200kHz and can be configured by \
00067                         modifying the relative define in stm8s_eval_i2c_ee.h file.\
00068                          \
00069                         For M24C64 devices all the memory is accessible through the two-bytes \
00070                         addressing mode and need to define block addresses. In this case, only the physical \
00071                         address has to be defined (according to the address pins (E0,E1 and E2) connection).\
00072                         This address is defined in stm8s_eval_i2c_ee.h (default is 0xA0: E0, E1 and E2 tied to ground).\
00073                         The EEPROM addresses where the program start the write and the read operations \
00074                         is defined in the main.c file. \
00075                          \
00076                         First, the content of Tx1_Buffer is written to the EEPROM_WriteAddress1 and the\
00077                         written data are read. The written and the read buffers data are then compared.\
00078                         Following the read operation, the program waits that the EEPROM reverts to its \
00079                         Standby state. A second write operation is, then, performed and this time, Tx2_Buffer\
00080                         is written to EEPROM_WriteAddress2, which represents the address just after the last \
00081                         written one in the first write. After completion of the second write operation, the \
00082                         written data are read. The contents of the written and the read buffers are compared.\
00083                          ";
00084 uint8_t Tx2_Buffer[] = "/* STM8S I2C Firmware Library EEPROM driver example: \
00085                         buffer 2 transfer into address sEE_WRITE_ADDRESS2 */";
00086 uint8_t Rx1_Buffer[BUFFER_SIZE1], Rx2_Buffer[BUFFER_SIZE2];
00087 volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
00088 volatile uint16_t NumDataRead = 0;
00089 
00090 /* Private functions ---------------------------------------------------------*/
00091 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
00092 /**
00093   * @brief  Main program
00094   * @param  None
00095   * @retval None
00096   */
00097 void main(void)
00098 {
00099   /* Initialization of the clock */
00100   /* Clock divider to HSI/1 */
00101   CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
00102 
00103 #ifdef ENABLE_LCD_MSG_DISPLAY
00104   /* Initialize the LCD screen for information display */
00105   STM8S_EVAL_LCD_Init();
00106   LCD_Clear();  
00107 
00108   LCD_SetCursorPos(LCD_LINE1, 0);
00109   LCD_Print("STM8S FW Library");
00110   LCD_SetCursorPos(LCD_LINE2, 0);
00111   LCD_Print("   I2C EEPROM   ");
00112 #endif /* ENABLE_LCD_MSG_DISPLAY */  
00113   
00114   /* Initialize the I2C EEPROM driver ----------------------------------------*/
00115   sEE_Init();  
00116 
00117   /* First write in the memory followed by a read of the written data --------*/
00118   /* Write on I2C EEPROM from sEE_WRITE_ADDRESS1 */
00119   sEE_WriteBuffer(Tx1_Buffer, sEE_WRITE_ADDRESS1, BUFFER_SIZE1); 
00120 
00121   /* Wait for EEPROM standby state */
00122   sEE_WaitEepromStandbyState();  
00123   
00124   /* Set the Number of data to be read */
00125   NumDataRead = BUFFER_SIZE1;
00126   
00127   /* Read from I2C EEPROM from sEE_READ_ADDRESS1 */
00128   sEE_ReadBuffer(Rx1_Buffer, sEE_READ_ADDRESS1, (uint16_t *)(&NumDataRead)); 
00129 
00130  
00131 #ifdef ENABLE_LCD_MSG_DISPLAY  
00132   LCD_SetCursorPos(LCD_LINE1,0);
00133   LCD_Print(" EEPROM Transfer1");
00134   LCD_SetCursorPos(LCD_LINE2, 0);
00135   LCD_Print("     Ongoing     ");
00136 #endif /* ENABLE_LCD_MSG_DISPLAY */ 
00137   
00138   /* Wait till transfer is complete */
00139   while (NumDataRead > 0)
00140   {}  
00141   
00142   /* Check if the data written to the memory is read correctly */
00143   TransferStatus1 = Buffercmp(Tx1_Buffer, Rx1_Buffer, BUFFER_SIZE1);
00144   /* TransferStatus1 = PASSED, if the transmitted and received data 
00145      to/from the EEPROM are the same */
00146   /* TransferStatus1 = FAILED, if the transmitted and received data 
00147      to/from the EEPROM are different */
00148 #ifdef ENABLE_LCD_MSG_DISPLAY  
00149   if (TransferStatus1 == PASSED)
00150   {
00151     LCD_SetCursorPos(LCD_LINE1,0);
00152     LCD_Print(" EEPROM Transfer1");
00153     LCD_SetCursorPos(LCD_LINE2, 0);
00154     LCD_Print("     PASSED      ");
00155   }
00156   else
00157   {
00158     LCD_SetCursorPos(LCD_LINE1,0);
00159     LCD_Print(" EEPROM Transfer1");
00160     LCD_SetCursorPos(LCD_LINE2, 0);
00161     LCD_Print("     FAILED      ");
00162   }  
00163 #endif /* ENABLE_LCD_MSG_DISPLAY */  
00164 
00165   /* Second write in the memory followed by a read of the written data -------*/
00166   /* Write on I2C EEPROM from sEE_WRITE_ADDRESS2 */
00167   sEE_WriteBuffer(Tx2_Buffer, sEE_WRITE_ADDRESS2, BUFFER_SIZE2); 
00168 
00169   /* Wait for EEPROM standby state */
00170   sEE_WaitEepromStandbyState();  
00171   
00172   /* Set the Number of data to be read */
00173   NumDataRead = BUFFER_SIZE2;  
00174   
00175   /* Read from I2C EEPROM from sEE_READ_ADDRESS2 */
00176   sEE_ReadBuffer(Rx2_Buffer, sEE_READ_ADDRESS2, (uint16_t *)(&NumDataRead));
00177 
00178 
00179 #ifdef ENABLE_LCD_MSG_DISPLAY   
00180   LCD_SetCursorPos(LCD_LINE1,0);
00181   LCD_Print(" EEPROM Transfer2");
00182   LCD_SetCursorPos(LCD_LINE2, 0);
00183   LCD_Print("     Ongoing     ");
00184 #endif /* ENABLE_LCD_MSG_DISPLAY */  
00185   
00186   /* Wait till transfer is complete */
00187   while (NumDataRead > 0)
00188   {}
00189   
00190   /* Check if the data written to the memory is read correctly */
00191   TransferStatus2 = Buffercmp(Tx2_Buffer, Rx2_Buffer, BUFFER_SIZE2);
00192   /* TransferStatus2 = PASSED, if the transmitted and received data 
00193      to/from the EEPROM are the same */
00194   /* TransferStatus2 = FAILED, if the transmitted and received data 
00195      to/from the EEPROM are different */
00196 #ifdef ENABLE_LCD_MSG_DISPLAY   
00197   if (TransferStatus1 == PASSED)
00198   {
00199     LCD_SetCursorPos(LCD_LINE1,0);
00200     LCD_Print(" EEPROM Transfer2");
00201     LCD_SetCursorPos(LCD_LINE2, 0);
00202     LCD_Print("     PASSED      ");
00203   }
00204   else
00205   {
00206     LCD_SetCursorPos(LCD_LINE1,0);
00207     LCD_Print(" EEPROM Transfer2");
00208     LCD_SetCursorPos(LCD_LINE2, 0);
00209     LCD_Print("     FAILED      ");
00210   }  
00211 #endif /* ENABLE_LCD_MSG_DISPLAY */
00212   
00213   /* Free all used resources */
00214   sEE_DeInit();
00215 
00216   while (1)
00217   {
00218   }
00219 }
00220 
00221 #ifndef USE_DEFAULT_TIMEOUT_CALLBACK
00222 /**
00223   * @brief  Example of timeout situation management.
00224   * @param  None.
00225   * @retval None.
00226   */
00227 uint32_t sEE_TIMEOUT_UserCallback(void)
00228 {
00229   /* User application may try to recover the communication by resetting I2C
00230     peripheral (calling the function I2C_SoftwareResetCmd()) then re-start
00231     the transmission/reception from a previously stored recover point.
00232     For simplicity reasons, this example only shows a basic way for errors 
00233     managements which consists of stopping all the process and requiring system
00234     reset. */
00235   
00236 #ifdef ENABLE_LCD_MSG_DISPLAY   
00237   /* Display error message on screen */
00238     LCD_SetCursorPos(LCD_LINE1,0);
00239     LCD_Print("    Com ERROR    ");
00240     LCD_SetCursorPos(LCD_LINE2, 0);
00241     LCD_Print("   Reset Board   ");
00242 #endif /* ENABLE_LCD_MSG_DISPLAY */
00243   
00244   /* Block communication and all processes */
00245   while (1)
00246   {   
00247   }  
00248 }
00249 #endif /* USE_DEFAULT_TIMEOUT_CALLBACK */
00250 
00251 /**
00252   * @brief  Compares two buffers.
00253   * @param  pBuffer1, pBuffer2: buffers to be compared.
00254   * @param  BufferLength: buffer's length
00255   * @retval PASSED: pBuffer1 identical to pBuffer2
00256   *         FAILED: pBuffer1 differs from pBuffer2
00257   */
00258 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
00259 {
00260   while(BufferLength--)
00261   {
00262     if(*pBuffer1 != *pBuffer2)
00263     {
00264       return FAILED;
00265     }
00266     
00267     pBuffer1++;
00268     pBuffer2++;
00269   }
00270 
00271   return PASSED;  
00272 }
00273 
00274 #ifdef USE_FULL_ASSERT
00275 /**
00276   * @brief  Reports the name of the source file and the source line number
00277   *   where the assert_param error has occurred.
00278   * @param file: pointer to the source file name
00279   * @param line: assert_param error line source number
00280   * @retval None
00281   */
00282 void assert_failed(uint8_t* file, uint32_t line)
00283 { 
00284   /* User can add his own implementation to report the file name and line number,
00285      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00286 
00287   /* Infinite loop */
00288   while (1)
00289   {
00290   }
00291 }
00292 #endif
00293 
00294 /**
00295   * @}
00296   */
00297 
00298 
00299 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM8 Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM8 8-bit Microcontrollers platform visit www.st.com