STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/I2C/I2C_WakeUpFromStop/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    I2C/I2C_WakeUpFromStop/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_WakeUpFromStop
00037   * @{
00038   */
00039 
00040 /* Private typedef -----------------------------------------------------------*/
00041 /* Private define ------------------------------------------------------------*/
00042 /* Private macro -------------------------------------------------------------*/
00043 /* Private variables ---------------------------------------------------------*/
00044 const uint8_t BufferTX[]= "STM32 CPAL FIRMWARE DRIVER FOR I2Cx DEVICE: WAKEUP FROM STOP MODE EXAMPLE";
00045 uint8_t BufferRX[BUFFSIZE];
00046 CPAL_TransferTypeDef  sRxStructure, sTxStructure;
00047 uint32_t BufferSize = BUFFSIZE;
00048 #ifdef I2C_SLAVE  
00049  GPIO_InitTypeDef GPIO_InitStructure;
00050 #endif /* I2C_SLAVE */
00051 __IO TestStatus TransferStatus = PASSED;
00052 
00053 /* Private function prototypes -----------------------------------------------*/
00054 /* Private functions ---------------------------------------------------------*/
00055 static void RCC_Config(void);
00056 static __IO uint32_t TimingDelay;
00057 #ifdef I2C_SLAVE  
00058  static TestStatus Compare_bBuffer(uint8_t* pBuffer, uint8_t* pBuffer1, uint32_t BufferLength);
00059  static void Reset_bBuffer(uint8_t *pBuffer, uint32_t BufferLenght);
00060  static void Restore_Configuration(void);
00061 #endif /* I2C_SLAVE */
00062 
00063 
00064 /**
00065   * @brief  Main program.
00066   * @param  None
00067   * @retval None
00068   */
00069 int main(void)
00070 {
00071   /*!< At this stage the microcontroller clock setting is already configured, 
00072        this is done through SystemInit() function which is called from startup
00073        file (startup_stm32f0xx.s) before to branch to application main.
00074        To reconfigure the default setting of SystemInit() function, refer to
00075        system_stm32f0xx.c file
00076   */
00077     
00078   /* Configure Clocks */
00079   RCC_Config();
00080   
00081   /* Initialize LEDsand LCD available on EVAL board ***************************/
00082   STM_EVAL_LEDInit(LED1);
00083   STM_EVAL_LEDInit(LED2);
00084   STM_EVAL_LEDInit(LED3);
00085   STM_EVAL_LEDInit(LED4);
00086   
00087   /* Initialize the LCD */
00088 #ifdef USE_STM320518_EVAL
00089   STM320518_LCD_Init();
00090 #else 
00091   STM32072B_LCD_Init();
00092 #endif /* USE_STM320518_EVAL */
00093 
00094   /* Display message on  LCD ***********************************************/
00095   /* Clear the LCD */ 
00096   LCD_Clear(White);  
00097   /* Set the LCD Back Color */
00098   LCD_SetBackColor(Blue);
00099   /* Set the LCD Text Color */
00100   LCD_SetTextColor(Yellow);
00101   LCD_DisplayStringLine(Line0, MESSAGE1);
00102   LCD_DisplayStringLine(Line1, MESSAGE2);  
00103   /* Set the LCD Back Color */
00104   LCD_SetBackColor(White);
00105   /* Set the LCD Text Color */
00106   LCD_SetTextColor(Blue);
00107   
00108   /* Configure the Push buttons in Polling mode */
00109   STM_EVAL_PBInit(BUTTON_SEL, Mode_GPIO);
00110   
00111   /* if STM32 device is set as Master */
00112 #ifdef I2C_MASTER     
00113 
00114   /* Configure and enable the systick timer to generate an interrupt each 1 ms */
00115   SysTick_Config((SystemCoreClock / 1000));
00116    
00117   /* Deinitialize I2Cx Device */ 
00118   CPAL_I2C_DeInit(&MASTERSTRUCTURE); 
00119   
00120   /* Initialize CPAL I2C structure parameters values */
00121   CPAL_I2C_StructInit(&MASTERSTRUCTURE);
00122   
00123  #ifdef CPAL_I2C_DMA_PROGMODEL
00124    MASTERSTRUCTURE.wCPAL_Options =  CPAL_OPT_NO_MEM_ADDR | CPAL_OPT_DMATX_TCIT;
00125    MASTERSTRUCTURE.CPAL_ProgModel = CPAL_PROGMODEL_DMA;
00126  #elif defined (CPAL_I2C_IT_PROGMODEL)
00127    MASTERSTRUCTURE.wCPAL_Options =  CPAL_OPT_NO_MEM_ADDR;
00128    MASTERSTRUCTURE.CPAL_ProgModel = CPAL_PROGMODEL_INTERRUPT;
00129  #else
00130   #error "Please select one of the programming model (in main.h)"
00131  #endif
00132   
00133   /* Set I2C Speed */
00134   MASTERSTRUCTURE.pCPAL_I2C_Struct->I2C_Timing = MASTER_I2C_TIMING;
00135   
00136   /* Select Master Mode */
00137   MASTERSTRUCTURE.CPAL_Mode = CPAL_MODE_MASTER; 
00138   
00139   /* Initialize I2Cx Device*/
00140   CPAL_I2C_Init(&MASTERSTRUCTURE); 
00141   
00142   /* Infinite loop */
00143   while(1)
00144   {   
00145     /* Initialize Transfer parameters */
00146     MASTERSTRUCTURE.pCPAL_TransferTx = &sTxStructure;    
00147     sTxStructure.wNumData = BufferSize;
00148     sTxStructure.pbBuffer = (uint8_t*)BufferTX;
00149     sTxStructure.wAddr1 = OWNADDRESS;
00150     
00151     /* Update LCD Display */
00152     LCD_SetBackColor(White);
00153     LCD_SetTextColor(Blue);    
00154     LCD_DisplayStringLine(Line8, MEASSAGE_EMPTY);
00155     LCD_DisplayStringLine(Line5, MESSAGE4);
00156     LCD_DisplayStringLine(Line6, MESSAGE5);
00157     
00158     /* wait until Key button is pushed */
00159     while(! STM_EVAL_PBGetState(BUTTON_SEL));
00160     
00161     /* Update LCD Display */
00162     LCD_DisplayStringLine(Line5, MEASSAGE_EMPTY);
00163     LCD_DisplayStringLine(Line6, MEASSAGE_EMPTY);
00164     
00165     /* Write operation */
00166     CPAL_I2C_Write(&MASTERSTRUCTURE);
00167     
00168     /* Wait until communication finishes */
00169     while ((MASTERSTRUCTURE.CPAL_State != CPAL_STATE_READY) && (MASTERSTRUCTURE.CPAL_State != CPAL_STATE_ERROR));
00170     
00171     if (TransferStatus == PASSED)
00172     {
00173       /* Update LCD Display */
00174       LCD_SetBackColor(Red);
00175       LCD_SetTextColor(White);    
00176       LCD_DisplayStringLine(Line8, MESSAGE6);      
00177     }
00178     else
00179     {
00180       TransferStatus = PASSED;
00181     }
00182     
00183     Delay(1000);
00184   }
00185 #endif /* I2C_MASTER */
00186   
00187   /* if STM32 device is set as Slave */  
00188 #ifdef I2C_SLAVE    
00189   
00190   /* GPIOA Periph clock enable */
00191   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
00192   
00193   /* Output System Clock on MCO pin (PA.08) */
00194   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
00195   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00196   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00197   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00198   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00199   GPIO_Init(GPIOA, &GPIO_InitStructure);
00200   
00201 #ifdef USE_STM320518_EVAL
00202   RCC_MCOConfig(RCC_MCOSource_SYSCLK);
00203 #else 
00204   RCC_MCOConfig(RCC_MCOSource_SYSCLK, RCC_MCOPrescaler_1);
00205 #endif /* USE_STM320518_EVAL */
00206   
00207   /* Deinitialize I2Cx Device */ 
00208   CPAL_I2C_DeInit(&SLAVESTRUCTURE); 
00209   
00210   /* Initialize CPAL I2C structure parameters values */
00211   CPAL_I2C_StructInit(&SLAVESTRUCTURE);
00212   
00213 #ifdef CPAL_I2C_DMA_PROGMODEL
00214   SLAVESTRUCTURE.wCPAL_Options = CPAL_OPT_I2C_NACK_ADD | CPAL_OPT_I2C_WAKEUP_STOP | CPAL_OPT_DMARX_TCIT;
00215   SLAVESTRUCTURE.CPAL_ProgModel = CPAL_PROGMODEL_DMA;
00216 #elif defined (CPAL_I2C_IT_PROGMODEL)
00217   SLAVESTRUCTURE.wCPAL_Options =  CPAL_OPT_I2C_NACK_ADD | CPAL_OPT_I2C_WAKEUP_STOP;
00218   SLAVESTRUCTURE.CPAL_ProgModel = CPAL_PROGMODEL_INTERRUPT;
00219 #else
00220  #error "Please select one of the programming model (in main.h)"
00221 #endif
00222   
00223   /* Configure Own address 1 */
00224   SLAVESTRUCTURE.pCPAL_I2C_Struct->I2C_OwnAddress1 = OWNADDRESS;
00225   
00226   /* Set I2C Speed */
00227   SLAVESTRUCTURE.pCPAL_I2C_Struct->I2C_Timing = SLAVE_I2C_TIMING;
00228   
00229   /* Select Slave Mode */ 
00230   SLAVESTRUCTURE.CPAL_Mode = CPAL_MODE_SLAVE; 
00231   
00232   /* Initialize I2Cx Device*/
00233   CPAL_I2C_Init(&SLAVESTRUCTURE);    
00234   
00235   /* Infinite loop */
00236   while(1)
00237   {     
00238     /* Reset BufferRX value */
00239     Reset_bBuffer(BufferRX, (uint16_t)BufferSize);
00240     
00241     /* Initialize Transfer parameters */
00242     SLAVESTRUCTURE.pCPAL_TransferRx = &sRxStructure;    
00243     sRxStructure.wNumData = BufferSize;         
00244     sRxStructure.pbBuffer = (uint8_t*)BufferRX;
00245     
00246     /* Update LCD Display */
00247     LCD_SetBackColor(White);
00248     LCD_SetTextColor(Blue);
00249     LCD_DisplayStringLine(Line8, MEASSAGE_EMPTY);
00250     LCD_DisplayStringLine(Line9, MEASSAGE_EMPTY);
00251     LCD_DisplayStringLine(Line5, MESSAGE7);
00252     
00253     Delay(1000);
00254     
00255     /* Update LCD Display */
00256     LCD_DisplayStringLine(Line5, MEASSAGE_EMPTY);
00257     LCD_DisplayStringLine(Line6, MESSAGE8);
00258     
00259     /* Read operation */
00260     CPAL_I2C_Read(&SLAVESTRUCTURE);  
00261     
00262     /* Enter Stop Mode and wait for interrupt to wake up */
00263     PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
00264     
00265     /* Wait until communication finishes */
00266     while ((SLAVESTRUCTURE.CPAL_State != CPAL_STATE_READY) && (SLAVESTRUCTURE.CPAL_State != CPAL_STATE_ERROR));
00267     
00268     /* Configure SystemClock*/
00269     Restore_Configuration();
00270     
00271     /* Configure and enable the systick timer to generate an interrupt each 1 ms */
00272     SysTick_Config((SystemCoreClock / 1000));
00273   
00274     /* Update LCD Display */
00275     LCD_DisplayStringLine(Line6, MEASSAGE_EMPTY);    
00276     LCD_SetBackColor(Red);
00277     LCD_SetTextColor(White);     
00278     LCD_DisplayStringLine(Line8, MESSAGE9);
00279     
00280     /* If are received correctly */
00281     if (Compare_bBuffer((uint8_t*)BufferTX, BufferRX, BufferSize) == PASSED )
00282     {          
00283       /* Update LCD Display */
00284       LCD_DisplayStringLine(Line9, MESSAGE6);      
00285     }
00286     else
00287     {          
00288       /* Update LCD Display */
00289       LCD_DisplayStringLine(Line9, MESSAGE10);
00290     }
00291     
00292     Delay(1500);
00293   }  
00294 #endif /* I2C_SLAVE */
00295 }
00296 
00297 
00298 /**
00299   * @brief  Configure the I2C Clock source and Power clock
00300   * @param  None
00301   * @retval None
00302   */
00303 static void RCC_Config(void)
00304 {
00305   /* Configure the I2C1 Clock Source */
00306   RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);
00307   
00308   /* Enable PWR APB clock */
00309   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); 
00310 }
00311 
00312 #ifdef I2C_SLAVE  
00313 /**
00314   * @brief  Restore peripheral config before entering STOP mode.
00315   * @param  None
00316   * @retval None
00317   */
00318 static void Restore_Configuration(void)
00319 {
00320   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
00321   
00322   /* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/    
00323   /* Enable HSE */    
00324   RCC_HSEConfig(RCC_HSE_ON);
00325  
00326   /* Wait till HSE is ready and if Time out is reached exit */
00327   HSEStatus = RCC_WaitForHSEStartUp();
00328 
00329   if (HSEStatus == (uint32_t)0x01)
00330   {
00331     /* Enable Prefetch Buffer */
00332     FLASH_SetLatency(FLASH_Latency_1);
00333     
00334     /* HCLK = SYSCLK */
00335     RCC_HCLKConfig(RCC_SYSCLK_Div1); 
00336     
00337     /* PCLK = HCLK */
00338     RCC_PCLKConfig(RCC_HCLK_Div1);
00339         
00340     /*  PLL configuration:  = HSE *  6 = 48 MHz */
00341     RCC_PREDIV1Config(RCC_PREDIV1_Div1);
00342     RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_CFGR_PLLMULL6);
00343     
00344     /* Enable PLL */
00345     RCC_PLLCmd(ENABLE);
00346     
00347     /* PLL as system clock source */
00348     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
00349   } 
00350 }
00351 
00352 /**
00353   * @brief  Compare two 8-bit buffers and returns the comparison result
00354   * @param  pBuffer: pointer to the source buffer to be compared to.
00355   * @param  pBuffer1: pointer to the second source buffer to be compared to the first.
00356   * @param  BufferLength: size of buffer to compare.
00357   * @retval PASSED: Comparison is OK (the two Buffers are identical)
00358   *         FAILED: Comparison is NOK (Buffers are different)
00359   */
00360 static TestStatus Compare_bBuffer(uint8_t* pBuffer, uint8_t* pBuffer1, uint32_t BufferLength)
00361 {
00362   while (BufferLength--)
00363   {
00364     if (*pBuffer != *pBuffer1)
00365     {
00366       return FAILED;
00367     }
00368 
00369     pBuffer++;
00370     pBuffer1++;
00371   }
00372 
00373   return PASSED;
00374 }
00375 
00376 /**
00377   * @brief  Fill Buffer with zero value
00378   * @param  pBuffer: pointer on the Buffer to fill.
00379   * @param  BufferLength: size of buffer to Fill.
00380   * @retval None
00381   */
00382 
00383 static void Reset_bBuffer(uint8_t *pBuffer, uint32_t BufferLenght)
00384 {
00385   uint32_t indextmp = 0;
00386 
00387   /* Put in global buffer same values */
00388   for ( indextmp = 0; indextmp < BufferLenght; indextmp++ )
00389   {
00390     pBuffer[indextmp] = 0;
00391   }
00392 }
00393 #endif /* I2C_SLAVE */
00394 
00395 /**
00396   * @brief  Inserts a delay time.
00397   * @param  nTime: specifies the delay time length, in 10 ms.
00398   * @retval None
00399   */
00400 void Delay(__IO uint32_t nTime)
00401 {
00402   TimingDelay = nTime;
00403 
00404   while(TimingDelay != 0);
00405 }
00406 
00407 /**
00408   * @brief  Decrements the TimingDelay variable.
00409   * @param  None
00410   * @retval None
00411   */
00412 void TimingDelay_Decrement(void)
00413 {
00414   if (TimingDelay != 0x00)
00415   { 
00416     TimingDelay--;
00417   }
00418 }
00419 
00420 #ifdef  USE_FULL_ASSERT
00421 
00422 /**
00423   * @brief  Reports the name of the source file and the source line number
00424   *         where the assert_param error has occurred.
00425   * @param  file: pointer to the source file name
00426   * @param  line: assert_param error line source number
00427   * @retval None
00428   */
00429 void assert_failed(uint8_t* file, uint32_t line)
00430 { 
00431   /* User can add his own implementation to report the file name and line number,
00432      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00433 
00434   /* Infinite loop */
00435   while (1)
00436   {
00437   }
00438 }
00439 #endif
00440 
00441 /**
00442   * @}
00443   */
00444 
00445 /**
00446   * @}
00447   */
00448 
00449 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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