mirror of
https://github.com/No-Chicken/Power-Pico.git
synced 2026-04-03 13:02:36 +08:00
删除RTC,改用TIM进行非中断定时时间戳
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file rtc.h
|
||||
* @brief This file contains all the function prototypes for
|
||||
* the rtc.c file
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __RTC_H__
|
||||
#define __RTC_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
extern RTC_HandleTypeDef hrtc;
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
|
||||
/* USER CODE END Private defines */
|
||||
|
||||
void MX_RTC_Init(void);
|
||||
|
||||
/* USER CODE BEGIN Prototypes */
|
||||
void SetReferenceTime(void);
|
||||
void GetElapsedTime_HMS(uint8_t *hours, uint8_t *minutes, uint8_t *seconds);
|
||||
|
||||
/* USER CODE END Prototypes */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __RTC_H__ */
|
||||
|
||||
@@ -58,7 +58,7 @@
|
||||
/* #define HAL_IWDG_MODULE_ENABLED */
|
||||
/* #define HAL_LTDC_MODULE_ENABLED */
|
||||
/* #define HAL_RNG_MODULE_ENABLED */
|
||||
#define HAL_RTC_MODULE_ENABLED
|
||||
/* #define HAL_RTC_MODULE_ENABLED */
|
||||
/* #define HAL_SAI_MODULE_ENABLED */
|
||||
/* #define HAL_SD_MODULE_ENABLED */
|
||||
/* #define HAL_MMC_MODULE_ENABLED */
|
||||
|
||||
@@ -38,6 +38,8 @@ extern TIM_HandleTypeDef htim2;
|
||||
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
|
||||
extern TIM_HandleTypeDef htim5;
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
#define PWM_PERIOD 300
|
||||
/* USER CODE END Private defines */
|
||||
@@ -45,11 +47,15 @@ extern TIM_HandleTypeDef htim4;
|
||||
void MX_TIM1_Init(void);
|
||||
void MX_TIM2_Init(void);
|
||||
void MX_TIM4_Init(void);
|
||||
void MX_TIM5_Init(void);
|
||||
|
||||
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
|
||||
|
||||
/* USER CODE BEGIN Prototypes */
|
||||
|
||||
void ClearMicrosecondCounter(void);
|
||||
uint64_t GetMicrosecondCounter(void);
|
||||
|
||||
/* USER CODE END Prototypes */
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
#include "adc.h"
|
||||
#include "dma.h"
|
||||
#include "i2c.h"
|
||||
#include "rtc.h"
|
||||
#include "spi.h"
|
||||
#include "tim.h"
|
||||
#include "usart.h"
|
||||
@@ -75,7 +74,7 @@ int main(void)
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
//this must set same as keil setting
|
||||
SCB->VTOR = FLASH_BASE + 0x0000C000U;
|
||||
SCB->VTOR = FLASH_BASE + 0x00000000U;
|
||||
/* USER CODE END 1 */
|
||||
|
||||
/* MCU Configuration--------------------------------------------------------*/
|
||||
@@ -104,7 +103,7 @@ int main(void)
|
||||
MX_SPI2_Init();
|
||||
MX_I2C1_Init();
|
||||
MX_TIM2_Init();
|
||||
MX_RTC_Init();
|
||||
MX_TIM5_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
@@ -148,10 +147,9 @@ void SystemClock_Config(void)
|
||||
/** Initializes the RCC Oscillators according to the specified parameters
|
||||
* in the RCC_OscInitTypeDef structure.
|
||||
*/
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
|
||||
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
||||
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
||||
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
||||
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
|
||||
RCC_OscInitStruct.PLL.PLLM = 8;
|
||||
|
||||
@@ -1,160 +0,0 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file rtc.c
|
||||
* @brief This file provides code for the configuration
|
||||
* of the RTC instances.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "rtc.h"
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
RTC_HandleTypeDef hrtc;
|
||||
|
||||
/* RTC init function */
|
||||
void MX_RTC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN RTC_Init 0 */
|
||||
|
||||
/* USER CODE END RTC_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN RTC_Init 1 */
|
||||
|
||||
/* USER CODE END RTC_Init 1 */
|
||||
|
||||
/** Initialize RTC Only
|
||||
*/
|
||||
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.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
|
||||
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
|
||||
if (HAL_RTC_Init(&hrtc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN RTC_Init 2 */
|
||||
|
||||
/* USER CODE END RTC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
|
||||
{
|
||||
|
||||
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
|
||||
if(rtcHandle->Instance==RTC)
|
||||
{
|
||||
/* USER CODE BEGIN RTC_MspInit 0 */
|
||||
|
||||
/* USER CODE END RTC_MspInit 0 */
|
||||
|
||||
/** Initializes the peripherals clock
|
||||
*/
|
||||
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
|
||||
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
|
||||
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/* RTC clock enable */
|
||||
__HAL_RCC_RTC_ENABLE();
|
||||
/* USER CODE BEGIN RTC_MspInit 1 */
|
||||
|
||||
/* USER CODE END RTC_MspInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle)
|
||||
{
|
||||
|
||||
if(rtcHandle->Instance==RTC)
|
||||
{
|
||||
/* USER CODE BEGIN RTC_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END RTC_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_RTC_DISABLE();
|
||||
/* USER CODE BEGIN RTC_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END RTC_MspDeInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
// 存储参考时间的时间戳(秒数)
|
||||
static uint32_t s_ReferenceTimestamp = 0;
|
||||
|
||||
/**
|
||||
* @brief 设定参考时间:从现在开始计时
|
||||
* 调用此函数后,计时器清零,重新开始
|
||||
*/
|
||||
void SetReferenceTime(void)
|
||||
{
|
||||
RTC_TimeTypeDef sTime = {0};
|
||||
RTC_DateTypeDef sDate = {0};
|
||||
|
||||
// 获取当前RTC时间
|
||||
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
|
||||
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
|
||||
|
||||
// 转换为总秒数(简化计算,仅用于计时差)
|
||||
s_ReferenceTimestamp =
|
||||
(sDate.Year * 365 + sDate.Month * 31 + sDate.Date) * 24 * 3600 +
|
||||
sTime.Hours * 3600 +
|
||||
sTime.Minutes * 60 +
|
||||
sTime.Seconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取从参考时间到现在经过的时间(时:分:秒)
|
||||
* @param hours [out] 经过的小时数
|
||||
* @param minutes [out] 经过的分钟数
|
||||
* @param seconds [out] 经过的秒数
|
||||
*/
|
||||
void GetElapsedTime_HMS(uint8_t *hours, uint8_t *minutes, uint8_t *seconds)
|
||||
{
|
||||
RTC_TimeTypeDef sTime = {0};
|
||||
RTC_DateTypeDef sDate = {0};
|
||||
uint32_t currentTimestamp, elapsed;
|
||||
|
||||
// 获取当前时间并转为秒数
|
||||
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
|
||||
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
|
||||
|
||||
uint32_t nowSeconds =
|
||||
(sDate.Year * 365 + sDate.Month * 31 + sDate.Date) * 24 * 3600 +
|
||||
sTime.Hours * 3600 +
|
||||
sTime.Minutes * 60 +
|
||||
sTime.Seconds;
|
||||
|
||||
// 计算经过的秒数
|
||||
elapsed = nowSeconds - s_ReferenceTimestamp;
|
||||
|
||||
// 转换为 时:分:秒
|
||||
*seconds = elapsed % 60;
|
||||
*minutes = (elapsed / 60) % 60;
|
||||
*hours = (elapsed / 3600);
|
||||
}
|
||||
|
||||
/* USER CODE END 1 */
|
||||
@@ -22,11 +22,17 @@
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
// 64位总微秒计数器
|
||||
static volatile uint64_t g_total_microseconds = 0;
|
||||
// 上一次读取的TIM5计数值
|
||||
static volatile uint32_t g_last_tim_value = 0;
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
TIM_HandleTypeDef htim2;
|
||||
TIM_HandleTypeDef htim4;
|
||||
TIM_HandleTypeDef htim5;
|
||||
|
||||
/* TIM1 init function */
|
||||
void MX_TIM1_Init(void)
|
||||
@@ -172,6 +178,49 @@ void MX_TIM4_Init(void)
|
||||
/* USER CODE END TIM4_Init 2 */
|
||||
HAL_TIM_MspPostInit(&htim4);
|
||||
|
||||
}
|
||||
/* TIM5 init function */
|
||||
void MX_TIM5_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM5_Init 0 */
|
||||
|
||||
/* USER CODE END TIM5_Init 0 */
|
||||
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
TIM_OC_InitTypeDef sConfigOC = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM5_Init 1 */
|
||||
|
||||
/* USER CODE END TIM5_Init 1 */
|
||||
htim5.Instance = TIM5;
|
||||
htim5.Init.Prescaler = 95;
|
||||
htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim5.Init.Period = 4294967295;
|
||||
htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim5.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_PWM_Init(&htim5) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sConfigOC.OCMode = TIM_OCMODE_PWM1;
|
||||
sConfigOC.Pulse = 0;
|
||||
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
||||
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
|
||||
if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM5_Init 2 */
|
||||
|
||||
/* USER CODE END TIM5_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* tim_pwmHandle)
|
||||
@@ -203,6 +252,17 @@ void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* tim_pwmHandle)
|
||||
|
||||
/* USER CODE END TIM4_MspInit 1 */
|
||||
}
|
||||
else if(tim_pwmHandle->Instance==TIM5)
|
||||
{
|
||||
/* USER CODE BEGIN TIM5_MspInit 0 */
|
||||
|
||||
/* USER CODE END TIM5_MspInit 0 */
|
||||
/* TIM5 clock enable */
|
||||
__HAL_RCC_TIM5_CLK_ENABLE();
|
||||
/* USER CODE BEGIN TIM5_MspInit 1 */
|
||||
|
||||
/* USER CODE END TIM5_MspInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
|
||||
@@ -296,6 +356,17 @@ void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* tim_pwmHandle)
|
||||
|
||||
/* USER CODE END TIM4_MspDeInit 1 */
|
||||
}
|
||||
else if(tim_pwmHandle->Instance==TIM5)
|
||||
{
|
||||
/* USER CODE BEGIN TIM5_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END TIM5_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_TIM5_CLK_DISABLE();
|
||||
/* USER CODE BEGIN TIM5_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END TIM5_MspDeInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
|
||||
@@ -316,4 +387,49 @@ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/**
|
||||
* @brief 更新并获取64位的总微秒数 (核心函数)
|
||||
* @note 此函数应该被周期性调用(例如在UI的500ms定时器中)
|
||||
* 来处理32位计数器的溢出并累加到64位变量中。
|
||||
*/
|
||||
void UpdateMicrosecondCounter(void)
|
||||
{
|
||||
uint32_t current_tim_value = __HAL_TIM_GET_COUNTER(&htim5);
|
||||
uint32_t diff;
|
||||
|
||||
if (current_tim_value >= g_last_tim_value) {
|
||||
// 没有溢出
|
||||
diff = current_tim_value - g_last_tim_value;
|
||||
} else {
|
||||
// 32位计数器已经溢出
|
||||
// diff = (最大值 - 上一次的值) + 当前值 + 1
|
||||
diff = (0xFFFFFFFF - g_last_tim_value) + current_tim_value + 1;
|
||||
}
|
||||
|
||||
g_total_microseconds += diff;
|
||||
g_last_tim_value = current_tim_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取累计的64位微秒数
|
||||
* @retval 64位的总微秒数
|
||||
*/
|
||||
uint64_t GetMicrosecondCounter(void)
|
||||
{
|
||||
// 在读取前先更新一次,以获取最精确的值
|
||||
UpdateMicrosecondCounter();
|
||||
return g_total_microseconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 清除累计的微秒计数器
|
||||
*/
|
||||
void ClearMicrosecondCounter(void)
|
||||
{
|
||||
g_total_microseconds = 0;
|
||||
// 同时重置TIM5的硬件计数器和软件记录值
|
||||
__HAL_TIM_SET_COUNTER(&htim5, 0);
|
||||
g_last_tim_value = 0;
|
||||
}
|
||||
|
||||
/* USER CODE END 1 */
|
||||
|
||||
Reference in New Issue
Block a user