硬汉嵌入式论坛

 找回密码
 立即注册
查看: 2798|回复: 6
收起左侧

[STM32H7] x-cube-rtc软件包,含各系列STM32 RTC校准方法

[复制链接]

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
115518
QQ
发表于 2021-5-1 17:55:54 | 显示全部楼层 |阅读模式
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
115518
QQ
 楼主| 发表于 2021-5-1 18:04:23 | 显示全部楼层
以STM32L4为例,实现原理
QQ截图20210501180356.png


main:
  1. /**
  2.   ******************************************************************************
  3.   * @file    AN4759/Projects/STM32L476RG_Nucleo/RTC_SmoothCalib/Src/main.c
  4.   * @author  MCD Application Team
  5.   * @version V1.0.0
  6.   * @date    25-July-2016
  7.   * @brief   This sample code demonstrates the STM32L476 features shown in AN4759
  8.   *             "Using the hardware real-time clock (RTC) in low-power modes with
  9.   *              STM32 microcontrollers"
  10.   ******************************************************************************
  11.   * @attention
  12.   *
  13.   * COPYRIGHT(c) 2016 STMicroelectronics
  14.   *
  15.   * Redistribution and use in source and binary forms, with or without modification,
  16.   * are permitted provided that the following conditions are met:
  17.   *   1. Redistributions of source code must retain the above copyright notice,
  18.   *      this list of conditions and the following disclaimer.
  19.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  20.   *      this list of conditions and the following disclaimer in the documentation
  21.   *      and/or other materials provided with the distribution.
  22.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  23.   *      may be used to endorse or promote products derived from this software
  24.   *      without specific prior written permission.
  25.   *
  26.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.   *
  37.   ******************************************************************************
  38.   */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "stm32l4xx_hal.h"
  41. #include "rtc.h"
  42. #include "tim.h"
  43. #include "gpio.h"

  44. /* Private variables ---------------------------------------------------------*/

  45. /* Private variables ---------------------------------------------------------*/
  46. __IO ITStatus CalibrationStatus = RESET;
  47. __IO ITStatus Calibration1stITDone = RESET;

  48. /* Private function prototypes -----------------------------------------------*/
  49. void SystemClock_Config(void);
  50. void Error_Handler(void);

  51. /* Private function prototypes -----------------------------------------------*/
  52. static void calibrate(void);

  53. int main(void)
  54. {
  55.   /* MCU Configuration----------------------------------------------------------*/

  56.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  57.   HAL_Init();

  58.   /* Configure the system clock */
  59.   SystemClock_Config();

  60.   /* Initialize all configured peripherals */
  61.   MX_GPIO_Init();
  62.   MX_TIM3_Init();
  63.   MX_RTC_Init();
  64.   MX_TIM2_Init();

  65.   if (HAL_TIM_Base_Start_IT(&hTim3) != HAL_OK)
  66.   {
  67.     /* Starting Error */
  68.     Error_Handler();
  69.   }
  70.   /* Start channel 1 in Output compare mode */
  71.   if (HAL_TIM_OC_Start_IT(&hTim3, TIM_CHANNEL_1) != HAL_OK)
  72.   {
  73.     /* Starting Error */
  74.     Error_Handler();
  75.   }

  76.   /*##-1- Start TIM2 generation in interrupt mode ####################*/
  77.   if (HAL_TIM_Base_Start_IT(&hTim2) != HAL_OK)
  78.   {
  79.     /* Starting Error */
  80.     Error_Handler();
  81.   }

  82.   /* Start channel 1 in Input capture mode */
  83.   if (HAL_TIM_IC_Start_IT(&hTim2, TIM_CHANNEL_1) != HAL_OK)
  84.   {
  85.     /* Starting Error */
  86.     Error_Handler();
  87.   }

  88.   /* Start channel 2 in Output compare mode */
  89.   if (HAL_TIM_OC_Start_IT(&hTim2, TIM_CHANNEL_2) != HAL_OK)
  90.   {
  91.     /* Starting Error */
  92.     Error_Handler();
  93.   }
  94.   /*##-2- Start TIM2 generation in interrupt mode ####################*/

  95.   /* Infinite loop */
  96.   while (1)
  97.   {
  98.     if (CalibrationStatus == SET)
  99.     {
  100.       calibrate();
  101.       CalibrationStatus  = RESET;
  102.     }
  103.   }
  104. }

  105. /** System Clock Configuration
  106. */
  107. void SystemClock_Config(void)
  108. {

  109.   RCC_OscInitTypeDef RCC_OscInitStruct;
  110.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  111.   RCC_PeriphCLKInitTypeDef PeriphClkInit;

  112.   /* In the case of the LSERDY flag being already set,
  113.   *    the LSE initialization through HAL_RCC_OscConfig can be skipped.
  114.   *    This will avoid deteriorating the timing of the application
  115.   *    when it takes time to initialize LSE while it is not needed.
  116.   */
  117.   if(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  118.   {
  119.     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE;
  120.     RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  121.     RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  122.     RCC_OscInitStruct.HSICalibrationValue = 16;
  123.     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  124.     RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  125.     RCC_OscInitStruct.PLL.PLLM = 1;
  126.     RCC_OscInitStruct.PLL.PLLN = 10;
  127.     RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  128.     RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  129.     RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  130.     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  131.     {
  132.       Error_Handler();
  133.     }
  134.   }
  135.   else
  136.   {
  137.     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  138.     RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  139.     RCC_OscInitStruct.HSICalibrationValue = 16;
  140.     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  141.     RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  142.     RCC_OscInitStruct.PLL.PLLM = 1;
  143.     RCC_OscInitStruct.PLL.PLLN = 10;
  144.     RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  145.     RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  146.     RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  147.     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  148.     {
  149.       Error_Handler();
  150.     }   
  151.   }

  152.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  153.                                 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  154.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  155.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  156.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  157.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  158.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  159.   {
  160.     Error_Handler();
  161.   }

  162.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  163.   PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  164.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  165.   {
  166.     Error_Handler();
  167.   }

  168.   HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_LSE, RCC_MCODIV_1);

  169.   HAL_RCCEx_EnableLSCO(RCC_LSCOSOURCE_LSE);

  170.   __HAL_RCC_PWR_CLK_ENABLE();

  171.   if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  172.   {
  173.     Error_Handler();
  174.   }

  175.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);

  176.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  177.   /* SysTick_IRQn interrupt configuration */
  178.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  179. }

  180. /**
  181.    * @brief
  182.   1. gets the number of RTCCLK to be masked or added during a 32-second clycle
  183.    */
  184. static void calibrate(void)
  185. {
  186.   const int32_t cycles_expected = 0x100000; /* expected number of RTCCLK cycles during 32-seconds */
  187.   int32_t smooth_calib_plus_pulses = RTC_SMOOTHCALIB_PLUSPULSES_RESET;
  188.   int32_t smooth_calib_minus_pulses_value = 0x0;

  189.   if (Calibration1stITDone == RESET)
  190.   {
  191.     Calibration1stITDone = SET;
  192.   }
  193.   else
  194.   {
  195.     /* update RTC_Calib */
  196.     if ( (uint32_t) (TIM2->CCR1 >= cycles_expected))
  197.     {
  198.       smooth_calib_plus_pulses = RTC_SMOOTHCALIB_PLUSPULSES_RESET;
  199.       smooth_calib_minus_pulses_value = (uint32_t) (TIM2->CCR1 - cycles_expected );
  200.     }
  201.     else
  202.     {
  203.       smooth_calib_plus_pulses = RTC_SMOOTHCALIB_PLUSPULSES_SET;
  204.       smooth_calib_minus_pulses_value = (uint32_t) ((RTC_SMOOTH_CALIB_MINUS_MASK) - (cycles_expected - TIM2->CCR1 ));
  205.     }
  206.     if (smooth_calib_minus_pulses_value > RTC_SMOOTH_CALIB_MINUS_MASK)
  207.     {
  208.       smooth_calib_minus_pulses_value = (uint32_t) (RTC_SMOOTH_CALIB_MINUS_MASK);
  209.     }

  210.     HAL_RTCEx_SetSmoothCalib(&hrtc, RTC_SMOOTHCALIB_PERIOD_32SEC, smooth_calib_plus_pulses, smooth_calib_minus_pulses_value);
  211.   }
  212. }

  213. /**
  214.   * @brief  This function is executed in case of error occurrence.
  215.   * @param  None
  216.   * @retval None
  217.   */
  218. void Error_Handler(void)
  219. {
  220.   /* User can add his own implementation to report the HAL error return state */
  221.   while (1)
  222.   {}
  223. }

  224. #ifdef USE_FULL_ASSERT

  225. /**
  226.    * @brief Reports the name of the source file and the source line number
  227.    * where the assert_param error has occurred.
  228.    * @param file: pointer to the source file name
  229.    * @param line: assert_param error line source number
  230.    * @retval None
  231.    */
  232. void assert_failed(uint8_t* file, uint32_t line)
  233. {
  234. }

  235. #endif

  236. /**
  237.   * @}
  238.   */

  239. /**
  240.   * @}
  241. */

  242. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码



tim:
  1. /**
  2.   ******************************************************************************
  3.   * @file    AN4759/Projects/STM32L476RG_Nucleo/RTC_SmoothCalib/Src/TIM.c
  4.   * @author  MCD Application Team
  5.   * @version V1.0.0
  6.   * @date    25-July-2016
  7.   * @brief   This file provides code for the configuration
  8.   *          of the TIM instances.
  9.   ******************************************************************************
  10.   * @attention
  11.   *
  12.   * COPYRIGHT(c) 2016 STMicroelectronics
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */

  38. /* Includes ------------------------------------------------------------------*/
  39. #include "tim.h"

  40. #include "gpio.h"

  41. TIM_HandleTypeDef hTim2;
  42. TIM_HandleTypeDef hTim3;

  43. /* TIM2 init function */
  44. void MX_TIM2_Init(void)
  45. {
  46.   TIM_ClockConfigTypeDef s_clock_source_config;
  47.   TIM_SlaveConfigTypeDef s_slave_config;
  48.   TIM_MasterConfigTypeDef s_master_config;
  49.   TIM_IC_InitTypeDef s_config_ic;
  50.   TIM_OC_InitTypeDef s_config_oc;

  51.   hTim2.Instance = TIM2;
  52.   hTim2.Init.Prescaler = 0;
  53.   hTim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  54.   hTim2.Init.Period = 0xFFFFFFFF;
  55.   hTim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  56.   if (HAL_TIM_Base_Init(&hTim2) != HAL_OK)
  57.   {
  58.     Error_Handler();
  59.   }

  60.   s_clock_source_config.ClockSource = TIM_CLOCKSOURCE_ETRMODE2;
  61.   s_clock_source_config.ClockPolarity = TIM_CLOCKPOLARITY_NONINVERTED;
  62.   s_clock_source_config.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
  63.   s_clock_source_config.ClockFilter = 0;
  64.   if (HAL_TIM_ConfigClockSource(&hTim2, &s_clock_source_config) != HAL_OK)
  65.   {
  66.     Error_Handler();
  67.   }

  68.   if (HAL_TIM_IC_Init(&hTim2) != HAL_OK)
  69.   {
  70.     Error_Handler();
  71.   }

  72.   if (HAL_TIM_OC_Init(&hTim2) != HAL_OK)
  73.   {
  74.     Error_Handler();
  75.   }

  76.   s_slave_config.SlaveMode = TIM_SLAVEMODE_RESET;
  77.   s_slave_config.InputTrigger = TIM_TS_ITR2;
  78.   if (HAL_TIM_SlaveConfigSynchronization(&hTim2, &s_slave_config) != HAL_OK)
  79.   {
  80.     Error_Handler();
  81.   }

  82.   s_master_config.MasterOutputTrigger = TIM_TRGO_RESET;
  83.   s_master_config.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  84.   if (HAL_TIMEx_MasterConfigSynchronization(&hTim2, &s_master_config) != HAL_OK)
  85.   {
  86.     Error_Handler();
  87.   }

  88.   if (HAL_TIMEx_RemapConfig(&hTim2, TIM_TIM2_ETR_LSE) != HAL_OK)
  89.   {
  90.     Error_Handler();
  91.   }

  92.   s_config_ic.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
  93.   s_config_ic.ICSelection = TIM_ICSELECTION_TRC;
  94.   s_config_ic.ICPrescaler = TIM_ICPSC_DIV1;
  95.   s_config_ic.ICFilter = 0;
  96.   if (HAL_TIM_IC_ConfigChannel(&hTim2, &s_config_ic, TIM_CHANNEL_1) != HAL_OK)
  97.   {
  98.     Error_Handler();
  99.   }

  100.   s_config_oc.OCMode = TIM_OCMODE_TOGGLE;
  101.   s_config_oc.Pulse = 0;
  102.   s_config_oc.OCPolarity = TIM_OCPOLARITY_HIGH;
  103.   s_config_oc.OCFastMode = TIM_OCFAST_DISABLE;
  104.   if (HAL_TIM_OC_ConfigChannel(&hTim2, &s_config_oc, TIM_CHANNEL_2) != HAL_OK)
  105.   {
  106.     Error_Handler();
  107.   }

  108.   HAL_TIM_MspPostInit(&hTim2);

  109. }
  110. /* TIM3 init function */
  111. void MX_TIM3_Init(void)
  112. {
  113.   TIM_ClockConfigTypeDef s_clock_source_config;
  114.   TIM_MasterConfigTypeDef s_master_config;
  115.   TIM_OC_InitTypeDef s_config_oc;

  116.   hTim3.Instance = TIM3;
  117.   hTim3.Init.Prescaler = 0;
  118.   hTim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  119.   hTim3.Init.Period = 15;
  120.   hTim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  121.   if (HAL_TIM_Base_Init(&hTim3) != HAL_OK)
  122.   {
  123.     Error_Handler();
  124.   }

  125.   s_clock_source_config.ClockSource = TIM_CLOCKSOURCE_ETRMODE2;
  126.   s_clock_source_config.ClockPolarity = TIM_CLOCKPOLARITY_NONINVERTED;
  127.   s_clock_source_config.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
  128.   s_clock_source_config.ClockFilter = 0;
  129.   if (HAL_TIM_ConfigClockSource(&hTim3, &s_clock_source_config) != HAL_OK)
  130.   {
  131.     Error_Handler();
  132.   }

  133.   if (HAL_TIM_OC_Init(&hTim3) != HAL_OK)
  134.   {
  135.     Error_Handler();
  136.   }

  137.   s_master_config.MasterOutputTrigger = TIM_TRGO_RESET;
  138.   s_master_config.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  139.   if (HAL_TIMEx_MasterConfigSynchronization(&hTim3, &s_master_config) != HAL_OK)
  140.   {
  141.     Error_Handler();
  142.   }

  143.   s_config_oc.OCMode = TIM_OCMODE_TOGGLE;
  144.   s_config_oc.Pulse = 0;
  145.   s_config_oc.OCPolarity = TIM_OCPOLARITY_HIGH;
  146.   s_config_oc.OCFastMode = TIM_OCFAST_DISABLE;
  147.   if (HAL_TIM_OC_ConfigChannel(&hTim3, &s_config_oc, TIM_CHANNEL_1) != HAL_OK)
  148.   {
  149.     Error_Handler();
  150.   }

  151.   HAL_TIM_MspPostInit(&hTim3);

  152. }

  153. void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_Handle)
  154. {

  155.   GPIO_InitTypeDef gpio_init_struct;
  156.   if (tim_Handle->Instance == TIM2)
  157.   {
  158.     /* Peripheral clock enable */
  159.     __HAL_RCC_TIM2_CLK_ENABLE();

  160.     /* Peripheral interrupt init */
  161.     HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
  162.     HAL_NVIC_EnableIRQ(TIM2_IRQn);
  163.   }
  164.   else if (tim_Handle->Instance == TIM3)
  165.   {
  166.     /* Peripheral clock enable */
  167.     __HAL_RCC_TIM3_CLK_ENABLE();

  168.     /**TIM3 GPIO Configuration
  169.     PD2     ------> TIM3_ETR
  170.     */
  171.     gpio_init_struct.Pin = GPIO_PIN_2;
  172.     gpio_init_struct.Mode = GPIO_MODE_AF_PP;
  173.     gpio_init_struct.Pull = GPIO_NOPULL;
  174.     gpio_init_struct.Speed = GPIO_SPEED_FREQ_LOW;
  175.     gpio_init_struct.Alternate = GPIO_AF2_TIM3;
  176.     HAL_GPIO_Init(GPIOD, &gpio_init_struct);

  177.     /* Peripheral interrupt init */
  178.     HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0);
  179.     HAL_NVIC_EnableIRQ(TIM3_IRQn);
  180.   }
  181.   else
  182.   {
  183.     Error_Handler();
  184.   }
  185. }
  186. void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
  187. {

  188.   GPIO_InitTypeDef gpio_init_struct;
  189.   if (timHandle->Instance == TIM2)
  190.   {
  191.     /**TIM2 GPIO Configuration
  192.     PA1     ------> TIM2_CH2
  193.     */
  194.     gpio_init_struct.Pin = GPIO_PIN_1;
  195.     gpio_init_struct.Mode = GPIO_MODE_AF_PP;
  196.     gpio_init_struct.Pull = GPIO_NOPULL;
  197.     gpio_init_struct.Speed = GPIO_SPEED_FREQ_LOW;
  198.     gpio_init_struct.Alternate = GPIO_AF1_TIM2;
  199.     HAL_GPIO_Init(GPIOA, &gpio_init_struct);

  200.     /* use LSE as clock */
  201.     TIM2->OR1 = TIM_TIM2_ETR_LSE;

  202.   }
  203.   else if (timHandle->Instance == TIM3)
  204.   {

  205.     /**TIM3 GPIO Configuration
  206.     PA6     ------> TIM3_CH1
  207.     */
  208.     gpio_init_struct.Pin = GPIO_PIN_6;
  209.     gpio_init_struct.Mode = GPIO_MODE_AF_PP;
  210.     gpio_init_struct.Pull = GPIO_NOPULL;
  211.     gpio_init_struct.Speed = GPIO_SPEED_FREQ_LOW;
  212.     gpio_init_struct.Alternate = GPIO_AF2_TIM3;
  213.     HAL_GPIO_Init(GPIOA, &gpio_init_struct);

  214.     /* use LSE as clock */
  215.     TIM3->CR2 |= TIM_TRGO_OC1REF;
  216.   }
  217.   else
  218.   {
  219.     Error_Handler();
  220.   }

  221. }

  222. void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_Handle)
  223. {

  224.   if (tim_Handle->Instance == TIM2)
  225.   {
  226.     /* Peripheral clock disable */
  227.     __HAL_RCC_TIM2_CLK_DISABLE();

  228.     /* Peripheral interrupt Deinit*/
  229.     HAL_NVIC_DisableIRQ(TIM2_IRQn);

  230.   }
  231.   else if (tim_Handle->Instance == TIM3)
  232.   {
  233.     /* Peripheral clock disable */
  234.     __HAL_RCC_TIM3_CLK_DISABLE();

  235.     /**TIM3 GPIO Configuration
  236.     PA6     ------> TIM3_CH1
  237.     PD2     ------> TIM3_ETR
  238.     */
  239.     HAL_GPIO_DeInit(GPIOA, GPIO_PIN_6);

  240.     HAL_GPIO_DeInit(GPIOD, GPIO_PIN_2);

  241.     /* Peripheral interrupt Deinit*/
  242.     HAL_NVIC_DisableIRQ(TIM3_IRQn);
  243.   }
  244.   else
  245.   {
  246.     Error_Handler();
  247.   }
  248. }

  249. /**
  250.   * @}
  251.   */

  252. /**
  253.   * @}
  254.   */

  255. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码


rtc:

  1. /**
  2.   ******************************************************************************
  3.   * @file    AN4759/Projects/STM32L476RG_Nucleo/RTC_SmoothCalib/Src/RTC.c
  4.   * @author  MCD Application Team
  5.   * @version V1.0.0
  6.   * @date    25-July-2016
  7.   * @brief   This file provides code for the configuration
  8.   *          of the RTC instances.
  9.   ******************************************************************************
  10.   * @attention
  11.   *
  12.   * COPYRIGHT(c) 2016 STMicroelectronics
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */

  38. /* Includes ------------------------------------------------------------------*/
  39. #include "rtc.h"

  40. #include "gpio.h"

  41. RTC_HandleTypeDef hrtc;

  42. /* RTC init function */
  43. void MX_RTC_Init(void)
  44. {
  45.   RTC_TimeTypeDef s_time;
  46.   RTC_DateTypeDef s_date;

  47.   /**Initialize RTC and set the Time and Date
  48.   */
  49.   hrtc.Instance = RTC;
  50.   hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  51.   hrtc.Init.AsynchPrediv = 127;
  52.   hrtc.Init.SynchPrediv = 255;
  53.   hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  54.   hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_POS1;
  55.   hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  56.   hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  57.   if (HAL_RTC_Init(&hrtc) != HAL_OK)
  58.   {
  59.     Error_Handler();
  60.   }

  61.   /* If the calendar is already initialized, INITS bit is set : no need to initialize it again */
  62.   if ((RTC->ISR & RTC_ISR_INITS) != RTC_ISR_INITS)
  63.   {
  64.     s_time.Hours = 0x1;
  65.     s_time.Minutes = 0x1;
  66.     s_time.Seconds = 0x1;
  67.     s_time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  68.     s_time.StoreOperation = RTC_STOREOPERATION_RESET;
  69.     if (HAL_RTC_SetTime(&hrtc, &s_time, RTC_FORMAT_BCD) != HAL_OK)
  70.     {
  71.       Error_Handler();
  72.     }

  73.     s_date.WeekDay = RTC_WEEKDAY_MONDAY;
  74.     s_date.Month = RTC_MONTH_JANUARY;
  75.     s_date.Date = 0x1;
  76.     s_date.Year = 0x16;

  77.     if (HAL_RTC_SetDate(&hrtc, &s_date, RTC_FORMAT_BCD) != HAL_OK)
  78.     {
  79.       Error_Handler();
  80.     }
  81.   }

  82.   /**Enable Calibration
  83.   */
  84.   if (HAL_RTCEx_SetCalibrationOutPut(&hrtc, RTC_CALIBOUTPUT_1HZ) != HAL_OK)
  85.   {
  86.     Error_Handler();
  87.   }

  88. }

  89. void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
  90. {

  91.   GPIO_InitTypeDef gpio_init_struct;
  92.   if (hrtc->Instance == RTC)
  93.   {
  94.     /* Peripheral clock enable */
  95.     __HAL_RCC_RTC_ENABLE();

  96.     /**RTC GPIO Configuration
  97.     PB2     ------> RTC_OUT_CALIB
  98.     */
  99.     gpio_init_struct.Pin = GPIO_PIN_2;
  100.     gpio_init_struct.Mode = GPIO_MODE_AF_PP;
  101.     gpio_init_struct.Pull = GPIO_NOPULL;
  102.     gpio_init_struct.Speed = GPIO_SPEED_FREQ_LOW;
  103.     gpio_init_struct.Alternate = GPIO_AF0_RTC_50Hz;
  104.     HAL_GPIO_Init(GPIOB, &gpio_init_struct);
  105.   }
  106. }

  107. void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
  108. {

  109.   if (hrtc->Instance == RTC)
  110.   {
  111.     /* Peripheral clock disable */
  112.     __HAL_RCC_RTC_DISABLE();

  113.     /**RTC GPIO Configuration
  114.     PB2     ------> RTC_OUT_CALIB
  115.     */
  116.     HAL_GPIO_DeInit(GPIOB, GPIO_PIN_2);

  117.   }
  118. }

  119. /**
  120.   * @}
  121.   */

  122. /**
  123.   * @}
  124.   */

  125. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
回复

使用道具 举报

0

主题

7

回帖

7

积分

新手上路

积分
7
发表于 2024-5-28 15:24:02 | 显示全部楼层
请教一下大佬,这个校准要耗时32秒才行吗?
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
115518
QQ
 楼主| 发表于 2024-5-28 16:04:53 | 显示全部楼层
cuca.z 发表于 2024-5-28 15:24
请教一下大佬,这个校准要耗时32秒才行吗?

ST官方的这个方案我们没有实测,你用的那个方案测试的。
回复

使用道具 举报

0

主题

7

回帖

7

积分

新手上路

积分
7
发表于 2024-5-28 16:26:01 | 显示全部楼层
最近在评估校准方案,还没实施过,特来请教一下。你们用什么方案校准?
我们用STM32L443这款
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
115518
QQ
 楼主| 发表于 2024-5-29 09:01:00 | 显示全部楼层
cuca.z 发表于 2024-5-28 16:26
最近在评估校准方案,还没实施过,特来请教一下。你们用什么方案校准?
我们用STM32L443这款

我们那个可以联网,所以可以做网络SNTP教程。

如果不能联网,可以考虑看看自带温补晶振的RX8025
回复

使用道具 举报

0

主题

7

回帖

7

积分

新手上路

积分
7
发表于 2024-5-31 15:19:40 | 显示全部楼层
eric2013 发表于 2024-5-29 09:01
我们那个可以联网,所以可以做网络SNTP教程。

如果不能联网,可以考虑看看自带温补晶振的RX8025

现在来看,ST官方给的校准需要一个外部输入的高精度1Hz波形。
大佬有没有推荐的信号源设备
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-4-29 14:33 , Processed in 0.709090 second(s), 27 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表