STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/I2C/I2C_TwoBoards/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    I2C/I2C_TwoBoards/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_TwoBoards
00037   * @{
00038   */
00039 
00040 /* Private typedef -----------------------------------------------------------*/
00041 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00042 /* Private define ------------------------------------------------------------*/
00043 
00044 /* Private macro -------------------------------------------------------------*/
00045 /* Private variables ---------------------------------------------------------*/
00046    
00047 /* CPAL local transfer structures */
00048 CPAL_TransferTypeDef  sRxStructure, sTxStructure;
00049 
00050 /* Buffers tables declarations */
00051 const uint8_t tStateSignal[]  = "STM32 CPAL 2xBoards Example: Signal State  ";
00052 const uint8_t tSignal1[]      = "STM32 CPAL 2xBoards Example: Signal Signal1";
00053 const uint8_t tSignal2[]      = "STM32 CPAL 2xBoards Example: Signal Signal2";
00054 
00055 uint8_t tRxBuffer[MAX_BUFF_SIZE];
00056 uint32_t BufferSize = BUFFER_SIZE;
00057 uint8_t Color = 0;
00058 __IO uint32_t ActionState = ACTION_NONE;
00059 __IO uint32_t RecieverMode = 0;
00060 __IO uint32_t TransmitMode = 0;
00061 uint32_t PeriodicValue = 15000, Divider = 1;
00062 
00063 /* Private function prototypes -----------------------------------------------*/
00064 
00065 /* Private functions ---------------------------------------------------------*/
00066 static void TIM6_Config(void);
00067 static void TIM17_Config(uint32_t Period);
00068 uint8_t Buffer_Check(uint8_t* pBuffer, uint8_t* pBuffer1, uint8_t* pBuffer2,  uint8_t* pBuffer3, uint16_t BufferLength);
00069 TestStatus Buffer_Compare(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
00070 void Switch_Color(void);
00071 void Switch_ErrorColor(void);
00072 
00073 
00074 /**
00075   * @brief  Main program.
00076   * @param  None
00077   * @retval None
00078   */
00079 int main(void)
00080 {
00081   /*!< At this stage the microcontroller clock setting is already configured, 
00082   this is done through SystemInit() function which is called from startup
00083   file (startup_stm32f0xx.s) before to branch to application main.
00084   To reconfigure the default setting of SystemInit() function, refer to
00085   system_stm32f0xx.c file
00086   */
00087   
00088   /* Initialize LEDs and LCD available on STM320518-EVAL board ****************/
00089   STM_EVAL_LEDInit(LED1);
00090   STM_EVAL_LEDInit(LED2);
00091   STM_EVAL_LEDInit(LED3);
00092   STM_EVAL_LEDInit(LED4);
00093     
00094   /* Initialize TIM6 */
00095   TIM6_Config();
00096   
00097   /* Initialize the LCD */
00098 #ifdef USE_STM320518_EVAL
00099   STM320518_LCD_Init();
00100 #else 
00101   STM32072B_LCD_Init();
00102 #endif /* USE_STM320518_EVAL */
00103 
00104   /* Display message on  LCD **************************************************/
00105   /* Clear the LCD */ 
00106   LCD_Clear(White);  
00107   /* Set the LCD Back Color */
00108   LCD_SetBackColor(Blue);
00109   /* Set the LCD Text Color */
00110   LCD_SetTextColor(Yellow);
00111   LCD_DisplayStringLine(Line0, MESSAGE1);
00112   LCD_DisplayStringLine(Line1, MESSAGE2);
00113   LCD_DisplayStringLine(Line3, MESSAGE3);
00114   
00115   /* Set the LCD Back Color */
00116   LCD_SetBackColor(White);
00117   /* Set the LCD Text Color */
00118   LCD_SetTextColor(Blue);
00119   
00120   /* Configure the Push buttons in interrupt mode *****************************/
00121   STM_EVAL_PBInit(BUTTON_SEL, Mode_EXTI);
00122   STM_EVAL_PBInit(BUTTON_TAMPER, Mode_EXTI);
00123   
00124   /* Start CPAL communication configuration ***********************************/
00125   /* Initialize local Reception structures */
00126   sRxStructure.wNumData = BufferSize;       /* Maximum Number of data to be received */
00127   sRxStructure.pbBuffer = tRxBuffer;        /* Common Rx buffer for all received data */
00128   sRxStructure.wAddr1 = 0;                  /* Not needed */
00129   sRxStructure.wAddr2 = 0;                  /* Not needed */
00130   
00131   /* Initialize local Transmission structures */
00132   sTxStructure.wNumData = BufferSize;       /* Maximum Number of data to be received */
00133   sTxStructure.pbBuffer = (uint8_t*)tStateSignal;     /* Common Rx buffer for all received data */
00134   sTxStructure.wAddr1 = OWN_ADDRESS;        /* The own board address */
00135   sTxStructure.wAddr2 = 0;                  /* Not needed */
00136   
00137   /* Set SYSCLK as I2C clock source */
00138   RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
00139   
00140   /* Configure the device structure */
00141   CPAL_I2C_StructInit(&I2C_DevStructure);      /* Set all fields to default values */
00142   I2C_DevStructure.CPAL_Mode = CPAL_MODE_SLAVE;
00143 #ifdef CPAL_I2C_DMA_PROGMODEL
00144   I2C_DevStructure.wCPAL_Options =  CPAL_OPT_NO_MEM_ADDR | CPAL_OPT_DMATX_TCIT | CPAL_OPT_DMARX_TCIT;
00145   I2C_DevStructure.CPAL_ProgModel = CPAL_PROGMODEL_DMA;
00146 #elif defined (CPAL_I2C_IT_PROGMODEL)
00147   I2C_DevStructure.wCPAL_Options =  CPAL_OPT_NO_MEM_ADDR;
00148   I2C_DevStructure.CPAL_ProgModel = CPAL_PROGMODEL_INTERRUPT;
00149 #else
00150  #error "Please select one of the programming model (in stm32f0xx_i2c_cpal_conf.h)"
00151 #endif
00152   I2C_DevStructure.pCPAL_I2C_Struct->I2C_Timing = I2C_TIMING;
00153   I2C_DevStructure.pCPAL_I2C_Struct->I2C_OwnAddress1 = OWN_ADDRESS;
00154   I2C_DevStructure.pCPAL_TransferRx = &sRxStructure;
00155   I2C_DevStructure.pCPAL_TransferTx = &sTxStructure;
00156   
00157   /* Initialize CPAL device with the selected parameters */
00158   CPAL_I2C_Init(&I2C_DevStructure);    
00159   
00160   /* Infinite loop */
00161   while (1)
00162   {
00163     /* Write operations ------------------------------------------------------*/
00164     /* Check if any action has been triggered by push buttons */
00165     if ((ActionState != ACTION_PENDING) && (ActionState != ACTION_NONE))
00166     {
00167       /* Check if the current CPAL device state allows write operation */
00168       if ((I2C_DevStructure.CPAL_State == CPAL_STATE_READY) || \
00169         (I2C_DevStructure.CPAL_State == CPAL_STATE_BUSY_RX) ||\
00170           (I2C_DevStructure.CPAL_State == CPAL_STATE_DISABLED))
00171       {        
00172         /* Initialize local Transmission structures */
00173         sTxStructure.wNumData = BufferSize;   /* Maximum Number of data to be received */
00174         sTxStructure.wAddr1 = OWN_ADDRESS;    /* The own board address */
00175         sTxStructure.wAddr2 = 0;              /* Not needed */        
00176         
00177         switch (ActionState)
00178         {
00179           
00180         case BUTTON_SEL: 
00181           sTxStructure.pbBuffer = (uint8_t*)tSignal1; 
00182           Divider = 1;
00183           break;
00184           
00185         case BUTTON_TAMPER:
00186           sTxStructure.pbBuffer = (uint8_t*)tSignal2; 
00187           Divider = 2;
00188           break;
00189           
00190         case ACTION_PERIODIC:
00191           sTxStructure.pbBuffer = (uint8_t*)tStateSignal; 
00192           break;    
00193           
00194         default:
00195           sTxStructure.pbBuffer = (uint8_t*)tSignal1; 
00196           break;            
00197         } 
00198         
00199         /* Configure the device mode to master */
00200         I2C_DevStructure.CPAL_Mode = CPAL_MODE_MASTER;
00201         /* Force the CPAL state to ready (in case a read operation has been initiated) */
00202         I2C_DevStructure.CPAL_State = CPAL_STATE_READY;
00203                 
00204         /* Prevent other actions to be performed while the current is not finished */
00205         ActionState = ACTION_PENDING;
00206         TransmitMode = STATE_ON;
00207         
00208         /* Configure a Timer to generate periodic interrupt: used to send state signal */
00209         TIM17_Config(PeriodicValue/Divider);   
00210         
00211         /* Start writing data in master mode */
00212         if (CPAL_I2C_Write(&I2C_DevStructure) == CPAL_PASS)
00213         {
00214          }
00215       }      
00216     }
00217     
00218     
00219     /* Read Operations -------------------------------------------------------*/
00220     if (((I2C_DevStructure.CPAL_State == CPAL_STATE_READY) || \
00221       (I2C_DevStructure.CPAL_State == CPAL_STATE_DISABLED)) && \
00222         (TransmitMode == STATE_OFF))
00223     {                  
00224       /* Initialize local Reception structures */
00225       sRxStructure.wNumData = BufferSize;       /* Maximum Number of data to be received */
00226       sRxStructure.pbBuffer = tRxBuffer;        /* Common Rx buffer for all received data */
00227       
00228       /* Reconfigure device for slave receiver mode */
00229       I2C_DevStructure.CPAL_Mode = CPAL_MODE_SLAVE;
00230       I2C_DevStructure.CPAL_State = CPAL_STATE_READY;
00231       
00232       /* Start waiting for data to be received in slave mode */
00233       if (CPAL_I2C_Read(&I2C_DevStructure) == CPAL_PASS)
00234       {
00235         LCD_DisplayStringLine(Line9, MEASSAGE_EMPTY); 
00236       }
00237     }   
00238   }
00239 }
00240 /**
00241   * @brief  Configures TIM6 and associated resources to generate an update interrupt each 50 ms.
00242   * @param  None
00243   * @retval None
00244   */
00245 static void TIM6_Config(void)
00246 {         
00247   /* TIM6 is used to toggle Led1 and Led4 each 50ms */  
00248   
00249   NVIC_InitTypeDef NVIC_InitStructure;
00250   TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;
00251   
00252   /* TIMER clock enable */
00253   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6 , ENABLE);
00254   
00255   /* Enable the TIMER global Interrupt */
00256   NVIC_InitStructure.NVIC_IRQChannel = TIM6_DAC_IRQn;
00257   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00258   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00259   NVIC_Init(&NVIC_InitStructure);  
00260   
00261   TIM_TimeBaseStructure.TIM_Period  = 50000;
00262   TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock/1000000)-1; 
00263   TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
00264   TIM_TimeBaseStructure.TIM_CounterMode =  TIM_CounterMode_Up;
00265   TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
00266   
00267   /* TIM IT enable */
00268   TIM_ITConfig(TIM6, TIM_IT_Update , ENABLE);
00269   
00270   /* TIM17 enable counter */
00271   TIM_Cmd(TIM6, ENABLE);
00272 }
00273 
00274 /**
00275   * @brief  Configures TIM17 and associated resources to generate an update interrupt.
00276   *         The period of Timer is depending on Period value.
00277   * @param  Period : affected to Timer Period
00278   * @retval None
00279   */
00280 static void TIM17_Config(uint32_t Period)
00281 {         
00282   /* TIM17 is used to generate periodic interrupts. At each interrupt 
00283   if Transmitter mode is selected, a status message is sent to other Board */
00284   
00285   NVIC_InitTypeDef NVIC_InitStructure;
00286   TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;
00287   
00288   /* TIMER clock enable */
00289   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17 , ENABLE);
00290   
00291   NVIC_InitStructure.NVIC_IRQChannel = TIM17_IRQn;
00292   NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
00293   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00294   NVIC_Init(&NVIC_InitStructure);  
00295   
00296   TIM_TimeBaseStructure.TIM_Period  = Period;
00297   TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock/10000)-1; 
00298   TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
00299   TIM_TimeBaseStructure.TIM_CounterMode =  TIM_CounterMode_Up;
00300   TIM_TimeBaseInit(TIM17, &TIM_TimeBaseStructure);
00301   
00302   /* TIM IT enable */
00303   TIM_ITConfig(TIM17, TIM_IT_Update , ENABLE);
00304   
00305   /* TIM17 enable counter */
00306   TIM_Cmd(TIM17, ENABLE);
00307 }
00308 /**
00309   * @brief  Compares two buffers.
00310   * @param  pBuffer1, pBuffer2: buffers to be compared.
00311   * @param  BufferLength: buffer's length
00312   * @retval PASSED: pBuffer1 identical to pBuffer2
00313   *         FAILED: pBuffer1 differs from pBuffer2
00314   */
00315 TestStatus Buffer_Compare(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
00316 {
00317   while(BufferLength--)
00318   {
00319     if(*pBuffer1 != *pBuffer2)
00320     {
00321       return FAILED;
00322     }
00323     
00324     pBuffer1++;
00325     pBuffer2++;
00326   }
00327 
00328   return PASSED;  
00329 }
00330 
00331 /**
00332   * @brief  Compares a buffer to a list of buffers.
00333   * @param  pBuffer, pBuffer1: buffers to be compared.
00334   * @param  BufferLength: buffer's length
00335   * @retval PASSED: pBuffer identical to pBuffer1
00336   *   FAILED: pBuffer differs from pBuffer1
00337   */
00338 
00339 uint8_t Buffer_Check(uint8_t* pBuffer, uint8_t* pBuffer1, uint8_t* pBuffer2,  uint8_t* pBuffer3, uint16_t BufferLength)
00340 { 
00341   if (Buffer_Compare(pBuffer, pBuffer1, BufferLength) == PASSED)
00342   {
00343     return 0;
00344   }
00345   else if (Buffer_Compare(pBuffer, pBuffer2, BufferLength) == PASSED)
00346   {
00347     return 1;
00348   }
00349   else if (Buffer_Compare(pBuffer, pBuffer3, BufferLength) == PASSED)
00350   {
00351     return 2;
00352   }  
00353   else
00354   {
00355     return 0xFF; /* Non identified buffer */
00356   }
00357 }
00358 
00359 /**
00360   * @brief  Swap the color of the line on LCD.
00361   * @param  None.
00362   * @retval None.
00363   */
00364 void Switch_Color(void)
00365 {
00366   /* Set the message color */
00367   if(Color == 0)
00368   {
00369     LCD_SetTextColor(Black);
00370     Color=1;
00371   }
00372   else
00373   {
00374     LCD_SetTextColor(Blue2);
00375     Color=0; 
00376   }   
00377 }
00378 
00379 /**
00380   * @brief  Swap the color of the line on LCD.
00381   * @param  None.
00382   * @retval None.
00383   */
00384 void Switch_ErrorColor(void)
00385 {
00386   /* Set the message color */
00387   if(Color == 0)
00388   {
00389     LCD_SetTextColor(Magenta);
00390     Color=1;
00391   }
00392   else
00393   {
00394     LCD_SetTextColor(Red);
00395     Color=0; 
00396   }   
00397 }
00398 
00399 #ifdef  USE_FULL_ASSERT
00400 
00401 /**
00402   * @brief  Reports the name of the source file and the source line number
00403   *         where the assert_param error has occurred.
00404   * @param  file: pointer to the source file name
00405   * @param  line: assert_param error line source number
00406   * @retval None
00407   */
00408 void assert_failed(uint8_t* file, uint32_t line)
00409 { 
00410   /* User can add his own implementation to report the file name and line number,
00411      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00412   
00413 
00414   /* Infinite loop */
00415   while (1)
00416   {
00417   }
00418 }
00419 #endif
00420 
00421 /**
00422   * @}
00423   */
00424 
00425 /**
00426   * @}
00427   */
00428 
00429 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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