STM32F10x_StdPeriph_Examples/RTC/Calendar/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    RTC/Calendar/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 "stm32f10x.h"
00023 #include "stm32_eval.h"
00024 #include <stdio.h>
00025 
00026 /** @addtogroup STM32F10x_StdPeriph_Examples
00027   * @{
00028   */
00029 
00030 /** @addtogroup RTC_Calendar
00031   * @{
00032   */
00033 
00034 /* Private typedef -----------------------------------------------------------*/
00035 /* Private define ------------------------------------------------------------*/
00036 #define RTCClockOutput_Enable  /* RTC Clock/64 is output on tamper pin(PC.13) */
00037 
00038 /* Private macro -------------------------------------------------------------*/
00039 /* Private variables ---------------------------------------------------------*/
00040 __IO uint32_t TimeDisplay = 0;
00041 USART_InitTypeDef USART_InitStructure;
00042 
00043 /* Private function prototypes -----------------------------------------------*/
00044 void RTC_Configuration(void);
00045 void NVIC_Configuration(void);
00046 uint32_t Time_Regulate(void);
00047 void Time_Adjust(void);
00048 void Time_Show(void);
00049 void Time_Display(uint32_t TimeVar);
00050 uint8_t USART_Scanf(uint32_t value);
00051 
00052 #ifdef __GNUC__
00053 /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
00054    set to 'Yes') calls __io_putchar() */
00055 #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
00056 #else
00057 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
00058 #endif /* __GNUC__ */
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_stm32f10x_xx.s) before to branch to application main.
00072        To reconfigure the default setting of SystemInit() function, refer to
00073        system_stm32f10x.c file
00074      */     
00075 
00076   /* Initialize LED1 mounted on STM3210X-EVAL board */       
00077   STM_EVAL_LEDInit(LED1);
00078 
00079   /* USARTx configured as follow:
00080         - BaudRate = 115200 baud  
00081         - Word Length = 8 Bits
00082         - One Stop Bit
00083         - No parity
00084         - Hardware flow control disabled (RTS and CTS signals)
00085         - Receive and transmit enabled
00086   */
00087   USART_InitStructure.USART_BaudRate = 115200;
00088   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
00089   USART_InitStructure.USART_StopBits = USART_StopBits_1;
00090   USART_InitStructure.USART_Parity = USART_Parity_No;
00091   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
00092   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
00093 
00094   STM_EVAL_COMInit(COM1, &USART_InitStructure);
00095 
00096 
00097   /* NVIC configuration */
00098   NVIC_Configuration();
00099 
00100   if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
00101   {
00102     /* Backup data register value is not correct or not yet programmed (when
00103        the first time the program is executed) */
00104 
00105     printf("\r\n\n RTC not yet configured....");
00106 
00107     /* RTC Configuration */
00108     RTC_Configuration();
00109 
00110     printf("\r\n RTC configured....");
00111 
00112     /* Adjust time by values entred by the user on the hyperterminal */
00113     Time_Adjust();
00114 
00115     BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
00116   }
00117   else
00118   {
00119     /* Check if the Power On Reset flag is set */
00120     if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
00121     {
00122       printf("\r\n\n Power On Reset occurred....");
00123     }
00124     /* Check if the Pin Reset flag is set */
00125     else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
00126     {
00127       printf("\r\n\n External Reset occurred....");
00128     }
00129 
00130     printf("\r\n No need to configure RTC....");
00131     /* Wait for RTC registers synchronization */
00132     RTC_WaitForSynchro();
00133 
00134     /* Enable the RTC Second */
00135     RTC_ITConfig(RTC_IT_SEC, ENABLE);
00136     /* Wait until last write operation on RTC registers has finished */
00137     RTC_WaitForLastTask();
00138   }
00139 
00140 #ifdef RTCClockOutput_Enable
00141   /* Enable PWR and BKP clocks */
00142   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
00143 
00144   /* Allow access to BKP Domain */
00145   PWR_BackupAccessCmd(ENABLE);
00146 
00147   /* Disable the Tamper Pin */
00148   BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper
00149                                  functionality must be disabled */
00150 
00151   /* Enable RTC Clock Output on Tamper Pin */
00152   BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
00153 #endif
00154 
00155   /* Clear reset flags */
00156   RCC_ClearFlag();
00157 
00158   /* Display time in infinite loop */
00159   Time_Show();
00160 }
00161 
00162 
00163 /**
00164   * @brief  Configures the nested vectored interrupt controller.
00165   * @param  None
00166   * @retval None
00167   */
00168 void NVIC_Configuration(void)
00169 {
00170   NVIC_InitTypeDef NVIC_InitStructure;
00171 
00172   /* Configure one bit for preemption priority */
00173   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
00174 
00175   /* Enable the RTC Interrupt */
00176   NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
00177   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
00178   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
00179   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00180   NVIC_Init(&NVIC_InitStructure);
00181 }
00182 
00183 /**
00184   * @brief  Configures the RTC.
00185   * @param  None
00186   * @retval None
00187   */
00188 void RTC_Configuration(void)
00189 {
00190   /* Enable PWR and BKP clocks */
00191   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
00192 
00193   /* Allow access to BKP Domain */
00194   PWR_BackupAccessCmd(ENABLE);
00195 
00196   /* Reset Backup Domain */
00197   BKP_DeInit();
00198 
00199   /* Enable LSE */
00200   RCC_LSEConfig(RCC_LSE_ON);
00201   /* Wait till LSE is ready */
00202   while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
00203   {}
00204 
00205   /* Select LSE as RTC Clock Source */
00206   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
00207 
00208   /* Enable RTC Clock */
00209   RCC_RTCCLKCmd(ENABLE);
00210 
00211   /* Wait for RTC registers synchronization */
00212   RTC_WaitForSynchro();
00213 
00214   /* Wait until last write operation on RTC registers has finished */
00215   RTC_WaitForLastTask();
00216 
00217   /* Enable the RTC Second */
00218   RTC_ITConfig(RTC_IT_SEC, ENABLE);
00219 
00220   /* Wait until last write operation on RTC registers has finished */
00221   RTC_WaitForLastTask();
00222 
00223   /* Set RTC prescaler: set RTC period to 1sec */
00224   RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
00225 
00226   /* Wait until last write operation on RTC registers has finished */
00227   RTC_WaitForLastTask();
00228 }
00229 
00230 /**
00231   * @brief  Returns the time entered by user, using Hyperterminal.
00232   * @param  None
00233   * @retval Current time RTC counter value
00234   */
00235 uint32_t Time_Regulate(void)
00236 {
00237   uint32_t Tmp_HH = 0xFF, Tmp_MM = 0xFF, Tmp_SS = 0xFF;
00238 
00239   printf("\r\n==============Time Settings=====================================");
00240   printf("\r\n  Please Set Hours");
00241 
00242   while (Tmp_HH == 0xFF)
00243   {
00244     Tmp_HH = USART_Scanf(23);
00245   }
00246   printf(":  %d", Tmp_HH);
00247   printf("\r\n  Please Set Minutes");
00248   while (Tmp_MM == 0xFF)
00249   {
00250     Tmp_MM = USART_Scanf(59);
00251   }
00252   printf(":  %d", Tmp_MM);
00253   printf("\r\n  Please Set Seconds");
00254   while (Tmp_SS == 0xFF)
00255   {
00256     Tmp_SS = USART_Scanf(59);
00257   }
00258   printf(":  %d", Tmp_SS);
00259 
00260   /* Return the value to store in RTC counter register */
00261   return((Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS));
00262 }
00263 
00264 /**
00265   * @brief  Adjusts time.
00266   * @param  None
00267   * @retval None
00268   */
00269 void Time_Adjust(void)
00270 {
00271   /* Wait until last write operation on RTC registers has finished */
00272   RTC_WaitForLastTask();
00273   /* Change the current time */
00274   RTC_SetCounter(Time_Regulate());
00275   /* Wait until last write operation on RTC registers has finished */
00276   RTC_WaitForLastTask();
00277 }
00278 
00279 /**
00280   * @brief  Displays the current time.
00281   * @param  TimeVar: RTC counter value.
00282   * @retval None
00283   */
00284 void Time_Display(uint32_t TimeVar)
00285 {
00286   uint32_t THH = 0, TMM = 0, TSS = 0;
00287   
00288   /* Reset RTC Counter when Time is 23:59:59 */
00289   if (RTC_GetCounter() == 0x0001517F)
00290   {
00291      RTC_SetCounter(0x0);
00292      /* Wait until last write operation on RTC registers has finished */
00293      RTC_WaitForLastTask();
00294   }
00295   
00296   /* Compute  hours */
00297   THH = TimeVar / 3600;
00298   /* Compute minutes */
00299   TMM = (TimeVar % 3600) / 60;
00300   /* Compute seconds */
00301   TSS = (TimeVar % 3600) % 60;
00302 
00303   printf("Time: %0.2d:%0.2d:%0.2d\r", THH, TMM, TSS);
00304 }
00305 
00306 /**
00307   * @brief  Shows the current time (HH:MM:SS) on the Hyperterminal.
00308   * @param  None
00309   * @retval None
00310   */   
00311 void Time_Show(void)
00312 {
00313   printf("\n\r");
00314 
00315   /* Infinite loop */
00316   while (1)
00317   {
00318     /* If 1s has been elapased */
00319     if (TimeDisplay == 1)
00320     {
00321       /* Display current time */
00322       Time_Display(RTC_GetCounter());
00323       TimeDisplay = 0;
00324     }
00325   }
00326 }
00327 
00328 
00329 /**
00330   * @brief  Retargets the C library printf function to the USART.
00331   * @param  None
00332   * @retval None
00333   */
00334 PUTCHAR_PROTOTYPE
00335 {
00336   /* Place your implementation of fputc here */
00337   /* e.g. write a character to the USART */
00338   USART_SendData(EVAL_COM1, (uint8_t) ch);
00339 
00340   /* Loop until the end of transmission */
00341   while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
00342   {}
00343 
00344   return ch;
00345 }
00346 
00347 /**
00348   * @brief  Gets numeric values from the hyperterminal.
00349   * @param  None
00350   * @retval None
00351   */
00352 uint8_t USART_Scanf(uint32_t value)
00353 {
00354   uint32_t index = 0;
00355   uint32_t tmp[2] = {0, 0};
00356 
00357   while (index < 2)
00358   {
00359     /* Loop until RXNE = 1 */
00360     while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_RXNE) == RESET)
00361     {}
00362     tmp[index++] = (USART_ReceiveData(EVAL_COM1));
00363     if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39))
00364     {
00365       printf("\n\rPlease enter valid number between 0 and 9");
00366       index--;
00367     }
00368   }
00369   /* Calculate the Corresponding value */
00370   index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10);
00371   /* Checks */
00372   if (index > value)
00373   {
00374     printf("\n\rPlease enter valid number between 0 and %d", value);
00375     return 0xFF;
00376   }
00377   return index;
00378 }
00379 
00380 #ifdef  USE_FULL_ASSERT
00381 /**
00382   * @brief  Reports the name of the source file and the source line number
00383   *         where the assert_param error has occurred.
00384   * @param  file: pointer to the source file name
00385   * @param  line: assert_param error line source number
00386   * @retval None
00387   */
00388 void assert_failed(uint8_t* file, uint32_t line)
00389 {
00390   /* User can add his own implementation to report the file name and line number,
00391      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00392 
00393   /* Infinite loop */
00394   while (1)
00395   {}
00396 }
00397 #endif
00398 
00399 /**
00400   * @}
00401   */
00402 
00403 /**
00404   * @}
00405   */
00406 
00407 /******************* (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