mirror of
https://github.com/STMicroelectronics/STM32CubeF1.git
synced 2025-05-07 19:29:11 +08:00
277 lines
8.4 KiB
C
277 lines
8.4 KiB
C
/**
|
|
******************************************************************************
|
|
* @file RTC/RTC_Alarm/Src/main.c
|
|
* @author MCD Application Team
|
|
* @brief This sample code shows how to use STM32F1xx RTC HAL API to configure
|
|
* Time and Date.
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* <h2><center>© Copyright (c) 2016 STMicroelectronics.
|
|
* All rights reserved.</center></h2>
|
|
*
|
|
* This software component is licensed by ST under BSD 3-Clause license,
|
|
* the "License"; You may not use this file except in compliance with the
|
|
* License. You may obtain a copy of the License at:
|
|
* opensource.org/licenses/BSD-3-Clause
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "main.h"
|
|
|
|
/** @addtogroup STM32F1xx_HAL_Examples
|
|
* @{
|
|
*/
|
|
|
|
/** @addtogroup RTC_Alarm
|
|
* @{
|
|
*/
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
/* Private define ------------------------------------------------------------*/
|
|
/* Private macro -------------------------------------------------------------*/
|
|
/* Private variables ---------------------------------------------------------*/
|
|
/* RTC handler declaration */
|
|
RTC_HandleTypeDef RtcHandle;
|
|
|
|
/* Buffer used for displaying Time */
|
|
uint8_t aShowTime[50] = {0};
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
void SystemClock_Config(void);
|
|
static void RTC_AlarmConfig(void);
|
|
static void RTC_TimeShow(uint8_t* showtime);
|
|
|
|
/* Private functions ---------------------------------------------------------*/
|
|
|
|
/**
|
|
* @brief Main program
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
int main(void)
|
|
{
|
|
/* STM32F103xB HAL library initialization:
|
|
- Configure the Flash prefetch
|
|
- Systick timer is configured by default as source of time base, but user
|
|
can eventually implement his proper time base source (a general purpose
|
|
timer for example or other time source), keeping in mind that Time base
|
|
duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
|
|
handled in milliseconds basis.
|
|
- Set NVIC Group Priority to 4
|
|
- Low Level Initialization
|
|
*/
|
|
HAL_Init();
|
|
|
|
/* Configure the system clock to 64 MHz */
|
|
SystemClock_Config();
|
|
|
|
/* Configure LED_GREEN */
|
|
BSP_LED_Init(LED_GREEN);
|
|
|
|
/*##-1- Configure the RTC peripheral #######################################*/
|
|
RtcHandle.Instance = RTC;
|
|
|
|
/* Configure RTC prescaler and RTC data registers */
|
|
/* RTC configured as follows:
|
|
- Asynch Prediv = Automatic calculation of prediv for 1 sec timebase
|
|
*/
|
|
RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
|
|
if (HAL_RTC_Init(&RtcHandle) != HAL_OK)
|
|
{
|
|
/* Initialization Error */
|
|
Error_Handler();
|
|
}
|
|
|
|
/*##-2- Configure Alarm ####################################################*/
|
|
/* Configure RTC Alarm */
|
|
RTC_AlarmConfig();
|
|
|
|
/* Infinite loop */
|
|
while (1)
|
|
{
|
|
/*##-3- Display the updated Time #########################################*/
|
|
RTC_TimeShow(aShowTime);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Alarm callback
|
|
* @param hrtc : RTC handle
|
|
* @retval None
|
|
*/
|
|
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
|
|
{
|
|
/* Turn LED_GREEN on: Alarm generation */
|
|
BSP_LED_On(LED_GREEN);
|
|
}
|
|
|
|
/**
|
|
* @brief System Clock Configuration
|
|
* The system Clock is configured as follow :
|
|
* System Clock source = PLL (HSI)
|
|
* SYSCLK(Hz) = 64000000
|
|
* HCLK(Hz) = 64000000
|
|
* AHB Prescaler = 1
|
|
* APB1 Prescaler = 2
|
|
* APB2 Prescaler = 1
|
|
* PLLMUL = 16
|
|
* Flash Latency(WS) = 2
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
void SystemClock_Config(void)
|
|
{
|
|
RCC_ClkInitTypeDef clkinitstruct = {0};
|
|
RCC_OscInitTypeDef oscinitstruct = {0};
|
|
|
|
/* Configure PLL ------------------------------------------------------*/
|
|
/* PLL configuration: PLLCLK = (HSI / 2) * PLLMUL = (8 / 2) * 16 = 64 MHz */
|
|
/* PREDIV1 configuration: PREDIV1CLK = PLLCLK / HSEPredivValue = 64 / 1 = 64 MHz */
|
|
/* Enable HSI and activate PLL with HSi_DIV2 as source */
|
|
oscinitstruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
|
|
oscinitstruct.HSEState = RCC_HSE_OFF;
|
|
oscinitstruct.LSEState = RCC_LSE_OFF;
|
|
oscinitstruct.HSIState = RCC_HSI_ON;
|
|
oscinitstruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
|
oscinitstruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
|
|
oscinitstruct.PLL.PLLState = RCC_PLL_ON;
|
|
oscinitstruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
|
|
oscinitstruct.PLL.PLLMUL = RCC_PLL_MUL16;
|
|
if (HAL_RCC_OscConfig(&oscinitstruct)!= HAL_OK)
|
|
{
|
|
/* Initialization Error */
|
|
while(1);
|
|
}
|
|
|
|
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
|
|
clocks dividers */
|
|
clkinitstruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
|
|
clkinitstruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
|
clkinitstruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
|
clkinitstruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
|
clkinitstruct.APB1CLKDivider = RCC_HCLK_DIV2;
|
|
if (HAL_RCC_ClockConfig(&clkinitstruct, FLASH_LATENCY_2)!= HAL_OK)
|
|
{
|
|
/* Initialization Error */
|
|
while(1);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief This function is executed in case of error occurrence.
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
void Error_Handler(void)
|
|
{
|
|
while (1)
|
|
{
|
|
/* Toggle LED2 with a period of one second */
|
|
BSP_LED_Toggle(LED2);
|
|
HAL_Delay(1000);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Configure the current time and date.
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void RTC_AlarmConfig(void)
|
|
{
|
|
RTC_DateTypeDef sdatestructure;
|
|
RTC_TimeTypeDef stimestructure;
|
|
RTC_AlarmTypeDef salarmstructure;
|
|
|
|
/*##-1- Configure the Date #################################################*/
|
|
/* Set Date: Tuesday February 18th 2014 */
|
|
sdatestructure.Year = 0x14;
|
|
sdatestructure.Month = RTC_MONTH_FEBRUARY;
|
|
sdatestructure.Date = 0x18;
|
|
sdatestructure.WeekDay = RTC_WEEKDAY_TUESDAY;
|
|
|
|
if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure,RTC_FORMAT_BCD) != HAL_OK)
|
|
{
|
|
/* Initialization Error */
|
|
Error_Handler();
|
|
}
|
|
|
|
/*##-2- Configure the Time #################################################*/
|
|
/* Set Time: 02:20:00 */
|
|
stimestructure.Hours = 0x02;
|
|
stimestructure.Minutes = 0x20;
|
|
stimestructure.Seconds = 0x00;
|
|
|
|
if(HAL_RTC_SetTime(&RtcHandle,&stimestructure,RTC_FORMAT_BCD) != HAL_OK)
|
|
{
|
|
/* Initialization Error */
|
|
Error_Handler();
|
|
}
|
|
|
|
/*##-3- Configure the RTC Alarm peripheral #################################*/
|
|
/* Set Alarm to 02:20:30
|
|
RTC Alarm Generation: Alarm on Hours, Minutes and Seconds */
|
|
salarmstructure.Alarm = RTC_ALARM_A;
|
|
salarmstructure.AlarmTime.Hours = 0x02;
|
|
salarmstructure.AlarmTime.Minutes = 0x20;
|
|
salarmstructure.AlarmTime.Seconds = 0x30;
|
|
|
|
if(HAL_RTC_SetAlarm_IT(&RtcHandle,&salarmstructure,RTC_FORMAT_BCD) != HAL_OK)
|
|
{
|
|
/* Initialization Error */
|
|
Error_Handler();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Display the current time.
|
|
* @param showtime : pointer to buffer
|
|
* @retval None
|
|
*/
|
|
static void RTC_TimeShow(uint8_t* showtime)
|
|
{
|
|
RTC_DateTypeDef sdatestructureget;
|
|
RTC_TimeTypeDef stimestructureget;
|
|
|
|
/* Get the RTC current Time */
|
|
HAL_RTC_GetTime(&RtcHandle, &stimestructureget, RTC_FORMAT_BIN);
|
|
/* Get the RTC current Date */
|
|
HAL_RTC_GetDate(&RtcHandle, &sdatestructureget, RTC_FORMAT_BIN);
|
|
/* Display time Format : hh:mm:ss */
|
|
sprintf((char*)showtime,"%02d:%02d:%02d",stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
|
|
}
|
|
|
|
#ifdef USE_FULL_ASSERT
|
|
|
|
/**
|
|
* @brief Reports the name of the source file and the source line number
|
|
* where the assert_param error has occurred.
|
|
* @param file: pointer to the source file name
|
|
* @param line: assert_param error line source number
|
|
* @retval None
|
|
*/
|
|
void assert_failed(uint8_t *file, uint32_t line)
|
|
{
|
|
/* User can add his own implementation to report the file name and line number,
|
|
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
|
|
|
/* Infinite loop */
|
|
while (1)
|
|
{
|
|
}
|
|
}
|
|
#endif
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|