| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- #include "pm.h"
- #include "rtc.h"
- #include "string.h"
- #include "app_rtc.h"
- #include "spi.h"
- #include "usart.h"
- #define PM_LOCK_COUNT 10
- #define PM_LOCK_NAME_LEN 16
- typedef struct
- {
- char name[PM_LOCK_NAME_LEN]; // 锁持有者的名称
- uint8_t is_locked; // 锁的状态:0表示未锁定,1表示已锁定
- } pm_lock_t;
- volatile uint32_t wakeup_events = 0;
- static pm_lock_t pm_locks[PM_LOCK_COUNT];
- static int wakeup_timer_sta = 0;
- static void set_rtc_wakeup_time(uint32_t period)
- {
- if (HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, (period > 0) ? (period - 1) : 0, RTC_WAKEUPCLOCK_CK_SPRE_16BITS) == HAL_OK)
- {
- wakeup_timer_sta = 1;
- }
- }
- // 初始化低功耗管理模块
- void pm_init(void)
- {
- // 初始化所有锁为未锁定状态
- memset(pm_locks, 0, sizeof(pm_locks));
- }
- // 获取一个锁
- int8_t pm_get_lock(const char *name)
- {
- // 检查是否已经存在同名的锁
- for (int i = 0; i < PM_LOCK_COUNT; i++)
- {
- if (pm_locks[i].is_locked && strcmp(pm_locks[i].name, name) == 0)
- {
- return -1; // 已经存在同名的锁,返回错误码
- }
- }
- // 寻找一个空闲的锁槽
- for (int i = 0; i < PM_LOCK_COUNT; i++)
- {
- if (!pm_locks[i].is_locked)
- {
- pm_locks[i].is_locked = 1; // 标记锁为已锁定
- strncpy(pm_locks[i].name, name, PM_LOCK_NAME_LEN - 1); // 复制锁持有者的名字
- pm_locks[i].name[PM_LOCK_NAME_LEN - 1] = '\0'; // 确保字符串以空字符结尾
- return i; // 返回锁的索引
- }
- }
- return -1; // 如果没有找到空闲的锁槽,返回错误码
- }
- // 释放一个锁
- int8_t pm_release_lock(const char *name)
- {
- for (int i = 0; i < PM_LOCK_COUNT; i++)
- {
- if (pm_locks[i].is_locked && strcmp(pm_locks[i].name, name) == 0)
- {
- pm_locks[i].is_locked = 0; // 标记锁为未锁定
- pm_locks[i].name[0] = '\0'; // 清除锁持有者的名字
- return 0; // 成功释放锁
- }
- }
- return -1; // 如果找不到指定的锁,返回错误码
- }
- // 检查是否有锁被持有
- static uint8_t pm_has_locks(void)
- {
- for (int i = 0; i < PM_LOCK_COUNT; i++)
- {
- if (pm_locks[i].is_locked)
- {
- return 1; // 存在至少一个锁被持有
- }
- }
- return 0; // 所有锁都未被持有
- }
- // 进入低功耗模式
- void pm_enter_low_power_mode(void)
- {
- if (pm_has_locks() == 0)
- {
- // 没有锁被持有,可以进入低功耗模式
- // 关闭LED
- HAL_GPIO_WritePin(LED_DBG_GPIO_Port, LED_DBG_Pin, GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED_SYS_GPIO_Port, LED_SYS_Pin, GPIO_PIN_RESET);
- HAL_SPI_DeInit(&hspi2);
- __HAL_RCC_SPI2_CLK_DISABLE();
- LL_USART_Disable(USART1);
- LL_USART_DeInit(USART1);
- // 设置RTC,30S后唤醒
- // 设置整分钟对齐
- rtc_time_t time;
- rtc_get_time(&time);
- uint8_t current_seconds = (time.tm_sec >> 4) * 10 + (time.tm_sec & 0x0f);
- uint8_t next_wakeup_second = ((current_seconds / PM_WUT_PERIOD) + 1) * PM_WUT_PERIOD;
- // 如果超过60,则归零并进位到分钟
- if (next_wakeup_second >= 60)
- {
- next_wakeup_second = 0;
- }
- if (next_wakeup_second > current_seconds)
- {
- set_rtc_wakeup_time(next_wakeup_second - current_seconds);
- }
- else
- {
- set_rtc_wakeup_time(60 - current_seconds);
- }
- // 进入低功耗
- HAL_SuspendTick();
- #ifndef __debug__
- HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
- #else
- wakeup_events = 0;
- while (wakeup_events == 0)
- ;
- HAL_GPIO_WritePin(LED_DBG_GPIO_Port, LED_DBG_Pin, GPIO_PIN_RESET);
- #endif
- HAL_ResumeTick();
-
- HAL_GPIO_WritePin(LED_SYS_GPIO_Port, LED_SYS_Pin, GPIO_PIN_SET);
- // 系统被唤醒后LED亮起20ms
- // 如需进一步降低功耗,可以屏蔽
- HAL_Delay(20);
- //MX_SPI2_Init();
- //MX_USART1_UART_Init();
- }
- else if (wakeup_timer_sta)
- {
- if (HAL_RTCEx_DeactivateWakeUpTimer(&hrtc) == HAL_OK)
- {
- wakeup_timer_sta = 0;
- }
- }
- }
- uint32_t pm_get_wakeup_events(void)
- {
- return wakeup_events;
- }
- void pm_clr_wakeup_events(uint32_t mask)
- {
- wakeup_events &= ~mask;
- }
- void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
- {
- if (GPIO_Pin == KEY_Pin)
- {
- wakeup_events |= PM_WAKUP_EVT_KEY;
- }
- }
- void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
- {
- wakeup_events |= PM_WAKUP_EVT_WUT;
- }
|