|

楼主 |
发表于 2021-5-1 18:04:23
|
显示全部楼层
以STM32L4为例,实现原理
main:
- /**
- ******************************************************************************
- * @file AN4759/Projects/STM32L476RG_Nucleo/RTC_SmoothCalib/Src/main.c
- * @author MCD Application Team
- * @version V1.0.0
- * @date 25-July-2016
- * @brief This sample code demonstrates the STM32L476 features shown in AN4759
- * "Using the hardware real-time clock (RTC) in low-power modes with
- * STM32 microcontrollers"
- ******************************************************************************
- * @attention
- *
- * COPYRIGHT(c) 2016 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32l4xx_hal.h"
- #include "rtc.h"
- #include "tim.h"
- #include "gpio.h"
- /* Private variables ---------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- __IO ITStatus CalibrationStatus = RESET;
- __IO ITStatus Calibration1stITDone = RESET;
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- void Error_Handler(void);
- /* Private function prototypes -----------------------------------------------*/
- static void calibrate(void);
- int main(void)
- {
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* Configure the system clock */
- SystemClock_Config();
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_TIM3_Init();
- MX_RTC_Init();
- MX_TIM2_Init();
- if (HAL_TIM_Base_Start_IT(&hTim3) != HAL_OK)
- {
- /* Starting Error */
- Error_Handler();
- }
- /* Start channel 1 in Output compare mode */
- if (HAL_TIM_OC_Start_IT(&hTim3, TIM_CHANNEL_1) != HAL_OK)
- {
- /* Starting Error */
- Error_Handler();
- }
- /*##-1- Start TIM2 generation in interrupt mode ####################*/
- if (HAL_TIM_Base_Start_IT(&hTim2) != HAL_OK)
- {
- /* Starting Error */
- Error_Handler();
- }
- /* Start channel 1 in Input capture mode */
- if (HAL_TIM_IC_Start_IT(&hTim2, TIM_CHANNEL_1) != HAL_OK)
- {
- /* Starting Error */
- Error_Handler();
- }
- /* Start channel 2 in Output compare mode */
- if (HAL_TIM_OC_Start_IT(&hTim2, TIM_CHANNEL_2) != HAL_OK)
- {
- /* Starting Error */
- Error_Handler();
- }
- /*##-2- Start TIM2 generation in interrupt mode ####################*/
- /* Infinite loop */
- while (1)
- {
- if (CalibrationStatus == SET)
- {
- calibrate();
- CalibrationStatus = RESET;
- }
- }
- }
- /** System Clock Configuration
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_PeriphCLKInitTypeDef PeriphClkInit;
- /* In the case of the LSERDY flag being already set,
- * the LSE initialization through HAL_RCC_OscConfig can be skipped.
- * This will avoid deteriorating the timing of the application
- * when it takes time to initialize LSE while it is not needed.
- */
- if(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
- {
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE;
- RCC_OscInitStruct.LSEState = RCC_LSE_ON;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = 16;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLM = 1;
- RCC_OscInitStruct.PLL.PLLN = 10;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
- RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
- RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- }
- else
- {
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = 16;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLM = 1;
- RCC_OscInitStruct.PLL.PLLN = 10;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
- RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
- RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- }
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
- | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
- {
- Error_Handler();
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
- PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- Error_Handler();
- }
- HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_LSE, RCC_MCODIV_1);
- HAL_RCCEx_EnableLSCO(RCC_LSCOSOURCE_LSE);
- __HAL_RCC_PWR_CLK_ENABLE();
- if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
- {
- Error_Handler();
- }
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /**
- * @brief
- 1. gets the number of RTCCLK to be masked or added during a 32-second clycle
- */
- static void calibrate(void)
- {
- const int32_t cycles_expected = 0x100000; /* expected number of RTCCLK cycles during 32-seconds */
- int32_t smooth_calib_plus_pulses = RTC_SMOOTHCALIB_PLUSPULSES_RESET;
- int32_t smooth_calib_minus_pulses_value = 0x0;
- if (Calibration1stITDone == RESET)
- {
- Calibration1stITDone = SET;
- }
- else
- {
- /* update RTC_Calib */
- if ( (uint32_t) (TIM2->CCR1 >= cycles_expected))
- {
- smooth_calib_plus_pulses = RTC_SMOOTHCALIB_PLUSPULSES_RESET;
- smooth_calib_minus_pulses_value = (uint32_t) (TIM2->CCR1 - cycles_expected );
- }
- else
- {
- smooth_calib_plus_pulses = RTC_SMOOTHCALIB_PLUSPULSES_SET;
- smooth_calib_minus_pulses_value = (uint32_t) ((RTC_SMOOTH_CALIB_MINUS_MASK) - (cycles_expected - TIM2->CCR1 ));
- }
- if (smooth_calib_minus_pulses_value > RTC_SMOOTH_CALIB_MINUS_MASK)
- {
- smooth_calib_minus_pulses_value = (uint32_t) (RTC_SMOOTH_CALIB_MINUS_MASK);
- }
- HAL_RTCEx_SetSmoothCalib(&hrtc, RTC_SMOOTHCALIB_PERIOD_32SEC, smooth_calib_plus_pulses, smooth_calib_minus_pulses_value);
- }
- }
- /**
- * @brief This function is executed in case of error occurrence.
- * @param None
- * @retval None
- */
- void Error_Handler(void)
- {
- /* User can add his own implementation to report the HAL error return state */
- while (1)
- {}
- }
- #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)
- {
- }
- #endif
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
tim:
- /**
- ******************************************************************************
- * @file AN4759/Projects/STM32L476RG_Nucleo/RTC_SmoothCalib/Src/TIM.c
- * @author MCD Application Team
- * @version V1.0.0
- * @date 25-July-2016
- * @brief This file provides code for the configuration
- * of the TIM instances.
- ******************************************************************************
- * @attention
- *
- * COPYRIGHT(c) 2016 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "tim.h"
- #include "gpio.h"
- TIM_HandleTypeDef hTim2;
- TIM_HandleTypeDef hTim3;
- /* TIM2 init function */
- void MX_TIM2_Init(void)
- {
- TIM_ClockConfigTypeDef s_clock_source_config;
- TIM_SlaveConfigTypeDef s_slave_config;
- TIM_MasterConfigTypeDef s_master_config;
- TIM_IC_InitTypeDef s_config_ic;
- TIM_OC_InitTypeDef s_config_oc;
- hTim2.Instance = TIM2;
- hTim2.Init.Prescaler = 0;
- hTim2.Init.CounterMode = TIM_COUNTERMODE_UP;
- hTim2.Init.Period = 0xFFFFFFFF;
- hTim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- if (HAL_TIM_Base_Init(&hTim2) != HAL_OK)
- {
- Error_Handler();
- }
- s_clock_source_config.ClockSource = TIM_CLOCKSOURCE_ETRMODE2;
- s_clock_source_config.ClockPolarity = TIM_CLOCKPOLARITY_NONINVERTED;
- s_clock_source_config.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
- s_clock_source_config.ClockFilter = 0;
- if (HAL_TIM_ConfigClockSource(&hTim2, &s_clock_source_config) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_IC_Init(&hTim2) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_OC_Init(&hTim2) != HAL_OK)
- {
- Error_Handler();
- }
- s_slave_config.SlaveMode = TIM_SLAVEMODE_RESET;
- s_slave_config.InputTrigger = TIM_TS_ITR2;
- if (HAL_TIM_SlaveConfigSynchronization(&hTim2, &s_slave_config) != HAL_OK)
- {
- Error_Handler();
- }
- s_master_config.MasterOutputTrigger = TIM_TRGO_RESET;
- s_master_config.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&hTim2, &s_master_config) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIMEx_RemapConfig(&hTim2, TIM_TIM2_ETR_LSE) != HAL_OK)
- {
- Error_Handler();
- }
- s_config_ic.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
- s_config_ic.ICSelection = TIM_ICSELECTION_TRC;
- s_config_ic.ICPrescaler = TIM_ICPSC_DIV1;
- s_config_ic.ICFilter = 0;
- if (HAL_TIM_IC_ConfigChannel(&hTim2, &s_config_ic, TIM_CHANNEL_1) != HAL_OK)
- {
- Error_Handler();
- }
- s_config_oc.OCMode = TIM_OCMODE_TOGGLE;
- s_config_oc.Pulse = 0;
- s_config_oc.OCPolarity = TIM_OCPOLARITY_HIGH;
- s_config_oc.OCFastMode = TIM_OCFAST_DISABLE;
- if (HAL_TIM_OC_ConfigChannel(&hTim2, &s_config_oc, TIM_CHANNEL_2) != HAL_OK)
- {
- Error_Handler();
- }
- HAL_TIM_MspPostInit(&hTim2);
- }
- /* TIM3 init function */
- void MX_TIM3_Init(void)
- {
- TIM_ClockConfigTypeDef s_clock_source_config;
- TIM_MasterConfigTypeDef s_master_config;
- TIM_OC_InitTypeDef s_config_oc;
- hTim3.Instance = TIM3;
- hTim3.Init.Prescaler = 0;
- hTim3.Init.CounterMode = TIM_COUNTERMODE_UP;
- hTim3.Init.Period = 15;
- hTim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- if (HAL_TIM_Base_Init(&hTim3) != HAL_OK)
- {
- Error_Handler();
- }
- s_clock_source_config.ClockSource = TIM_CLOCKSOURCE_ETRMODE2;
- s_clock_source_config.ClockPolarity = TIM_CLOCKPOLARITY_NONINVERTED;
- s_clock_source_config.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
- s_clock_source_config.ClockFilter = 0;
- if (HAL_TIM_ConfigClockSource(&hTim3, &s_clock_source_config) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_OC_Init(&hTim3) != HAL_OK)
- {
- Error_Handler();
- }
- s_master_config.MasterOutputTrigger = TIM_TRGO_RESET;
- s_master_config.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&hTim3, &s_master_config) != HAL_OK)
- {
- Error_Handler();
- }
- s_config_oc.OCMode = TIM_OCMODE_TOGGLE;
- s_config_oc.Pulse = 0;
- s_config_oc.OCPolarity = TIM_OCPOLARITY_HIGH;
- s_config_oc.OCFastMode = TIM_OCFAST_DISABLE;
- if (HAL_TIM_OC_ConfigChannel(&hTim3, &s_config_oc, TIM_CHANNEL_1) != HAL_OK)
- {
- Error_Handler();
- }
- HAL_TIM_MspPostInit(&hTim3);
- }
- void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_Handle)
- {
- GPIO_InitTypeDef gpio_init_struct;
- if (tim_Handle->Instance == TIM2)
- {
- /* Peripheral clock enable */
- __HAL_RCC_TIM2_CLK_ENABLE();
- /* Peripheral interrupt init */
- HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(TIM2_IRQn);
- }
- else if (tim_Handle->Instance == TIM3)
- {
- /* Peripheral clock enable */
- __HAL_RCC_TIM3_CLK_ENABLE();
- /**TIM3 GPIO Configuration
- PD2 ------> TIM3_ETR
- */
- gpio_init_struct.Pin = GPIO_PIN_2;
- gpio_init_struct.Mode = GPIO_MODE_AF_PP;
- gpio_init_struct.Pull = GPIO_NOPULL;
- gpio_init_struct.Speed = GPIO_SPEED_FREQ_LOW;
- gpio_init_struct.Alternate = GPIO_AF2_TIM3;
- HAL_GPIO_Init(GPIOD, &gpio_init_struct);
- /* Peripheral interrupt init */
- HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(TIM3_IRQn);
- }
- else
- {
- Error_Handler();
- }
- }
- void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
- {
- GPIO_InitTypeDef gpio_init_struct;
- if (timHandle->Instance == TIM2)
- {
- /**TIM2 GPIO Configuration
- PA1 ------> TIM2_CH2
- */
- gpio_init_struct.Pin = GPIO_PIN_1;
- gpio_init_struct.Mode = GPIO_MODE_AF_PP;
- gpio_init_struct.Pull = GPIO_NOPULL;
- gpio_init_struct.Speed = GPIO_SPEED_FREQ_LOW;
- gpio_init_struct.Alternate = GPIO_AF1_TIM2;
- HAL_GPIO_Init(GPIOA, &gpio_init_struct);
- /* use LSE as clock */
- TIM2->OR1 = TIM_TIM2_ETR_LSE;
- }
- else if (timHandle->Instance == TIM3)
- {
- /**TIM3 GPIO Configuration
- PA6 ------> TIM3_CH1
- */
- gpio_init_struct.Pin = GPIO_PIN_6;
- gpio_init_struct.Mode = GPIO_MODE_AF_PP;
- gpio_init_struct.Pull = GPIO_NOPULL;
- gpio_init_struct.Speed = GPIO_SPEED_FREQ_LOW;
- gpio_init_struct.Alternate = GPIO_AF2_TIM3;
- HAL_GPIO_Init(GPIOA, &gpio_init_struct);
- /* use LSE as clock */
- TIM3->CR2 |= TIM_TRGO_OC1REF;
- }
- else
- {
- Error_Handler();
- }
- }
- void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_Handle)
- {
- if (tim_Handle->Instance == TIM2)
- {
- /* Peripheral clock disable */
- __HAL_RCC_TIM2_CLK_DISABLE();
- /* Peripheral interrupt Deinit*/
- HAL_NVIC_DisableIRQ(TIM2_IRQn);
- }
- else if (tim_Handle->Instance == TIM3)
- {
- /* Peripheral clock disable */
- __HAL_RCC_TIM3_CLK_DISABLE();
- /**TIM3 GPIO Configuration
- PA6 ------> TIM3_CH1
- PD2 ------> TIM3_ETR
- */
- HAL_GPIO_DeInit(GPIOA, GPIO_PIN_6);
- HAL_GPIO_DeInit(GPIOD, GPIO_PIN_2);
- /* Peripheral interrupt Deinit*/
- HAL_NVIC_DisableIRQ(TIM3_IRQn);
- }
- else
- {
- Error_Handler();
- }
- }
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
rtc:
- /**
- ******************************************************************************
- * @file AN4759/Projects/STM32L476RG_Nucleo/RTC_SmoothCalib/Src/RTC.c
- * @author MCD Application Team
- * @version V1.0.0
- * @date 25-July-2016
- * @brief This file provides code for the configuration
- * of the RTC instances.
- ******************************************************************************
- * @attention
- *
- * COPYRIGHT(c) 2016 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "rtc.h"
- #include "gpio.h"
- RTC_HandleTypeDef hrtc;
- /* RTC init function */
- void MX_RTC_Init(void)
- {
- RTC_TimeTypeDef s_time;
- RTC_DateTypeDef s_date;
- /**Initialize RTC and set the Time and Date
- */
- hrtc.Instance = RTC;
- hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
- hrtc.Init.AsynchPrediv = 127;
- hrtc.Init.SynchPrediv = 255;
- hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
- hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_POS1;
- hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
- hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
- if (HAL_RTC_Init(&hrtc) != HAL_OK)
- {
- Error_Handler();
- }
- /* If the calendar is already initialized, INITS bit is set : no need to initialize it again */
- if ((RTC->ISR & RTC_ISR_INITS) != RTC_ISR_INITS)
- {
- s_time.Hours = 0x1;
- s_time.Minutes = 0x1;
- s_time.Seconds = 0x1;
- s_time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
- s_time.StoreOperation = RTC_STOREOPERATION_RESET;
- if (HAL_RTC_SetTime(&hrtc, &s_time, RTC_FORMAT_BCD) != HAL_OK)
- {
- Error_Handler();
- }
- s_date.WeekDay = RTC_WEEKDAY_MONDAY;
- s_date.Month = RTC_MONTH_JANUARY;
- s_date.Date = 0x1;
- s_date.Year = 0x16;
- if (HAL_RTC_SetDate(&hrtc, &s_date, RTC_FORMAT_BCD) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**Enable Calibration
- */
- if (HAL_RTCEx_SetCalibrationOutPut(&hrtc, RTC_CALIBOUTPUT_1HZ) != HAL_OK)
- {
- Error_Handler();
- }
- }
- void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
- {
- GPIO_InitTypeDef gpio_init_struct;
- if (hrtc->Instance == RTC)
- {
- /* Peripheral clock enable */
- __HAL_RCC_RTC_ENABLE();
- /**RTC GPIO Configuration
- PB2 ------> RTC_OUT_CALIB
- */
- gpio_init_struct.Pin = GPIO_PIN_2;
- gpio_init_struct.Mode = GPIO_MODE_AF_PP;
- gpio_init_struct.Pull = GPIO_NOPULL;
- gpio_init_struct.Speed = GPIO_SPEED_FREQ_LOW;
- gpio_init_struct.Alternate = GPIO_AF0_RTC_50Hz;
- HAL_GPIO_Init(GPIOB, &gpio_init_struct);
- }
- }
- void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
- {
- if (hrtc->Instance == RTC)
- {
- /* Peripheral clock disable */
- __HAL_RCC_RTC_DISABLE();
- /**RTC GPIO Configuration
- PB2 ------> RTC_OUT_CALIB
- */
- HAL_GPIO_DeInit(GPIOB, GPIO_PIN_2);
- }
- }
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码 |
|