STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/RTC/RTC_Tamper/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    RTC/RTC_Tamper/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 /* Includes ------------------------------------------------------------------*/
00029 #include "main.h"
00030 
00031 /** @addtogroup STM32F0xx_StdPeriph_Examples
00032   * @{
00033   */
00034 
00035 /** @addtogroup RTC_Tamper
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 #define RTC_BKP_DR_NUMBER              5  /* RTC Backup Data Register Number */
00042 
00043 /* Uncomment the corresponding line to select the RTC Clock source */
00044 //#define RTC_CLOCK_SOURCE_LSE   /* LSE used as RTC source clock */
00045  #define RTC_CLOCK_SOURCE_LSI */ /* LSI used as RTC source clock. The RTC Clock
00046                                       may varies due to LSI frequency dispersion. */
00047 
00048 /* Private macro -------------------------------------------------------------*/
00049 /* Private variables ---------------------------------------------------------*/
00050 uint32_t RTC_BKP_DR[RTC_BKP_DR_NUMBER] =
00051   {
00052     RTC_BKP_DR0, RTC_BKP_DR1, RTC_BKP_DR2, RTC_BKP_DR3, RTC_BKP_DR4
00053   };
00054 
00055 /* Private function prototypes -----------------------------------------------*/
00056 static void RTC_Config(void);
00057 static void WriteToRTC_BKP_DR(uint32_t FirstRTCBackupData);
00058 static uint32_t CheckRTC_BKP_DR(uint32_t FirstRTCBackupData);
00059 
00060 /* Private functions ---------------------------------------------------------*/
00061 
00062 /**
00063   * @brief  Main program.
00064   * @param  None
00065   * @retval None
00066   */
00067 int main(void)
00068 {
00069   /*!< At this stage the microcontroller clock setting is already configured, 
00070        this is done through SystemInit() function which is called from startup
00071        file (startup_stm32f0xx.s) before to branch to application main.
00072        To reconfigure the default setting of SystemInit() function, refer to
00073        system_stm32f0xx.c file
00074      */ 
00075 
00076   /* Initialize Leds mounted on STM320518-EVAL board */
00077   STM_EVAL_LEDInit(LED1);
00078   STM_EVAL_LEDInit(LED2);
00079   STM_EVAL_LEDInit(LED3);
00080   STM_EVAL_LEDInit(LED4);
00081   
00082   /* RTC configuration */
00083   RTC_Config();
00084 
00085   /* Write To RTC Backup Data registers */
00086   WriteToRTC_BKP_DR(0xA53C);
00087 
00088   /* Check if the written data are correct */
00089   if(CheckRTC_BKP_DR(0xA53C) == 0x00)
00090   {
00091     /* Turn on LED1 */
00092     STM_EVAL_LEDOn(LED1);
00093   }
00094   else
00095   {
00096     /* Turn on LED3 */
00097     STM_EVAL_LEDOn(LED3);
00098   }
00099 
00100   /* Infinite loop */
00101   while (1)
00102   {
00103   }
00104 }
00105 
00106 /**
00107   * @brief  Configure the RTC peripheral by selecting the clock source.
00108   * @param  None
00109   * @retval None
00110   */
00111 static void RTC_Config(void)
00112 {
00113   NVIC_InitTypeDef NVIC_InitStructure;
00114   EXTI_InitTypeDef  EXTI_InitStructure;
00115 
00116   /* Enable the PWR clock */
00117   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
00118 
00119   /* Allow access to RTC */
00120   PWR_BackupAccessCmd(ENABLE);
00121       
00122 #if defined (RTC_CLOCK_SOURCE_LSI)  /* LSI used as RTC source clock*/
00123 /* The RTC Clock may varies due to LSI frequency dispersion. */
00124   /* Enable the LSI OSC */ 
00125   RCC_LSICmd(ENABLE);
00126 
00127   /* Wait till LSI is ready */  
00128   while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
00129   {
00130   }
00131 
00132   /* Select the RTC Clock Source */
00133   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
00134   
00135 #elif defined (RTC_CLOCK_SOURCE_LSE) /* LSE used as RTC source clock */
00136   /* Enable the LSE OSC */
00137   RCC_LSEConfig(RCC_LSE_ON);
00138 
00139   /* Wait till LSE is ready */  
00140   while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
00141   {
00142   }
00143 
00144   /* Select the RTC Clock Source */
00145   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
00146   
00147 #else
00148   #error Please select the RTC Clock source inside the main.c file
00149 #endif /* RTC_CLOCK_SOURCE_LSI */
00150   
00151   /* Enable The external line19 interrupt */
00152   EXTI_ClearITPendingBit(EXTI_Line19);
00153   EXTI_InitStructure.EXTI_Line = EXTI_Line19;
00154   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
00155   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
00156   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
00157   EXTI_Init(&EXTI_InitStructure);
00158 
00159   /* Enable RTC IRQChannel */
00160   NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
00161   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00162   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00163   NVIC_Init(&NVIC_InitStructure);
00164 
00165   /* Disable the Tamper 1 detection */
00166   RTC_TamperCmd(RTC_Tamper_1, DISABLE);
00167 
00168   /* Clear Tamper 1 pin Event(TAMP1F) pending flag */
00169   RTC_ClearFlag(RTC_FLAG_TAMP1F);
00170 
00171   /* Configure the Tamper 1 Trigger */
00172   RTC_TamperTriggerConfig(RTC_Tamper_1,  RTC_TamperTrigger_RisingEdge);
00173 
00174   /* Enable the Tamper interrupt */
00175   RTC_ITConfig(RTC_IT_TAMP, ENABLE);
00176 
00177   /* Clear Tamper 1 pin interrupt pending bit */
00178   RTC_ClearITPendingBit(RTC_IT_TAMP1);
00179 
00180   /* Enable the Tamper 1 detection */
00181   RTC_TamperCmd(RTC_Tamper_1, ENABLE);
00182 }
00183 
00184 /**
00185   * @brief  Writes data RTC Backup DRx registers.
00186   * @param  FirstRTCBackupData: data to be written to RTC Backup data registers.
00187   * @retval None
00188   */
00189 static void WriteToRTC_BKP_DR(uint32_t FirstRTCBackupData)
00190 {
00191   uint32_t index = 0;
00192 
00193   for (index = 0; index < RTC_BKP_DR_NUMBER; index++)
00194   {
00195     /* write To bkp data register */
00196     RTC_WriteBackupRegister(RTC_BKP_DR[index], FirstRTCBackupData + (index * 0x5A));
00197   }
00198 }
00199 
00200 /**
00201   * @brief  Checks if the RTC Backup DRx registers values are correct or not.
00202   * @param  FirstRTCBackupData: data to be compared with RTC Backup data registers.
00203   * @retval - 0: All RTC Backup DRx registers values are correct
00204   *         - Value different from 0: Number of the first Backup register
00205   *           which value is not correct
00206   */
00207 static uint32_t CheckRTC_BKP_DR(uint32_t FirstRTCBackupData)
00208 {
00209   uint32_t index = 0;
00210 
00211   for (index = 0; index < RTC_BKP_DR_NUMBER; index++)
00212   {
00213     /* Read from data register */
00214     if (RTC_ReadBackupRegister(RTC_BKP_DR[index]) != (FirstRTCBackupData + (index * 0x5A)))
00215     {
00216       return (index + 1);
00217     }
00218   }
00219     return 0;
00220 }
00221 
00222 /**
00223   * @brief  Checks if the RTC Backup DRx registers are reset or not.
00224   * @param  None
00225   * @retval - 0: All RTC Backup DRx registers are reset
00226   *         - Value different from 0: Number of the first Backup register
00227   *           not reset
00228   */
00229 uint32_t IsBackupRegReset(void)
00230 {
00231   uint32_t index = 0;
00232 
00233   for (index = 0; index < RTC_BKP_DR_NUMBER; index++)
00234   {
00235      /* Read from bkp Data Register */
00236     if (RTC_ReadBackupRegister(RTC_BKP_DR[index]) != 0x0)
00237     {
00238       return (index + 1);
00239     }
00240   }
00241   return 0;
00242 }
00243 
00244 #ifdef  USE_FULL_ASSERT
00245 
00246 /**
00247   * @brief  Reports the name of the source file and the source line number
00248   *         where the assert_param error has occurred.
00249   * @param  file: pointer to the source file name
00250   * @param  line: assert_param error line source number
00251   * @retval None
00252   */
00253 void assert_failed(uint8_t* file, uint32_t line)
00254 { 
00255   /* User can add his own implementation to report the file name and line number,
00256      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00257 
00258   /* Infinite loop */
00259   while (1)
00260   {
00261   }
00262 }
00263 #endif
00264 
00265 /**
00266   * @}
00267   */
00268 
00269 /**
00270   * @}
00271   */
00272 
00273 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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