pm.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "pm.h"
  2. #include "rtc.h"
  3. #include "string.h"
  4. #include "app_rtc.h"
  5. #include "spi.h"
  6. #include "usart.h"
  7. #define PM_LOCK_COUNT 10
  8. #define PM_LOCK_NAME_LEN 16
  9. typedef struct
  10. {
  11. char name[PM_LOCK_NAME_LEN]; // 锁持有者的名称
  12. uint8_t is_locked; // 锁的状态:0表示未锁定,1表示已锁定
  13. } pm_lock_t;
  14. volatile uint32_t wakeup_events = 0;
  15. static pm_lock_t pm_locks[PM_LOCK_COUNT];
  16. static int wakeup_timer_sta = 0;
  17. static void set_rtc_wakeup_time(uint32_t period)
  18. {
  19. if (HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, (period > 0) ? (period - 1) : 0, RTC_WAKEUPCLOCK_CK_SPRE_16BITS) == HAL_OK)
  20. {
  21. wakeup_timer_sta = 1;
  22. }
  23. }
  24. // 初始化低功耗管理模块
  25. void pm_init(void)
  26. {
  27. // 初始化所有锁为未锁定状态
  28. memset(pm_locks, 0, sizeof(pm_locks));
  29. }
  30. // 获取一个锁
  31. int8_t pm_get_lock(const char *name)
  32. {
  33. // 检查是否已经存在同名的锁
  34. for (int i = 0; i < PM_LOCK_COUNT; i++)
  35. {
  36. if (pm_locks[i].is_locked && strcmp(pm_locks[i].name, name) == 0)
  37. {
  38. return -1; // 已经存在同名的锁,返回错误码
  39. }
  40. }
  41. // 寻找一个空闲的锁槽
  42. for (int i = 0; i < PM_LOCK_COUNT; i++)
  43. {
  44. if (!pm_locks[i].is_locked)
  45. {
  46. pm_locks[i].is_locked = 1; // 标记锁为已锁定
  47. strncpy(pm_locks[i].name, name, PM_LOCK_NAME_LEN - 1); // 复制锁持有者的名字
  48. pm_locks[i].name[PM_LOCK_NAME_LEN - 1] = '\0'; // 确保字符串以空字符结尾
  49. return i; // 返回锁的索引
  50. }
  51. }
  52. return -1; // 如果没有找到空闲的锁槽,返回错误码
  53. }
  54. // 释放一个锁
  55. int8_t pm_release_lock(const char *name)
  56. {
  57. for (int i = 0; i < PM_LOCK_COUNT; i++)
  58. {
  59. if (pm_locks[i].is_locked && strcmp(pm_locks[i].name, name) == 0)
  60. {
  61. pm_locks[i].is_locked = 0; // 标记锁为未锁定
  62. pm_locks[i].name[0] = '\0'; // 清除锁持有者的名字
  63. return 0; // 成功释放锁
  64. }
  65. }
  66. return -1; // 如果找不到指定的锁,返回错误码
  67. }
  68. // 检查是否有锁被持有
  69. static uint8_t pm_has_locks(void)
  70. {
  71. for (int i = 0; i < PM_LOCK_COUNT; i++)
  72. {
  73. if (pm_locks[i].is_locked)
  74. {
  75. return 1; // 存在至少一个锁被持有
  76. }
  77. }
  78. return 0; // 所有锁都未被持有
  79. }
  80. // 进入低功耗模式
  81. void pm_enter_low_power_mode(void)
  82. {
  83. if (pm_has_locks() == 0)
  84. {
  85. // 没有锁被持有,可以进入低功耗模式
  86. // 关闭LED
  87. HAL_GPIO_WritePin(LED_DBG_GPIO_Port, LED_DBG_Pin, GPIO_PIN_SET);
  88. HAL_GPIO_WritePin(LED_SYS_GPIO_Port, LED_SYS_Pin, GPIO_PIN_RESET);
  89. HAL_SPI_DeInit(&hspi2);
  90. __HAL_RCC_SPI2_CLK_DISABLE();
  91. LL_USART_Disable(USART1);
  92. LL_USART_DeInit(USART1);
  93. // 设置RTC,30S后唤醒
  94. // 设置整分钟对齐
  95. rtc_time_t time;
  96. rtc_get_time(&time);
  97. uint8_t current_seconds = (time.tm_sec >> 4) * 10 + (time.tm_sec & 0x0f);
  98. uint8_t next_wakeup_second = ((current_seconds / PM_WUT_PERIOD) + 1) * PM_WUT_PERIOD;
  99. // 如果超过60,则归零并进位到分钟
  100. if (next_wakeup_second >= 60)
  101. {
  102. next_wakeup_second = 0;
  103. }
  104. if (next_wakeup_second > current_seconds)
  105. {
  106. set_rtc_wakeup_time(next_wakeup_second - current_seconds);
  107. }
  108. else
  109. {
  110. set_rtc_wakeup_time(60 - current_seconds);
  111. }
  112. // 进入低功耗
  113. HAL_SuspendTick();
  114. #ifndef __debug__
  115. HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
  116. #else
  117. wakeup_events = 0;
  118. while (wakeup_events == 0)
  119. ;
  120. HAL_GPIO_WritePin(LED_DBG_GPIO_Port, LED_DBG_Pin, GPIO_PIN_RESET);
  121. #endif
  122. HAL_ResumeTick();
  123. HAL_GPIO_WritePin(LED_SYS_GPIO_Port, LED_SYS_Pin, GPIO_PIN_SET);
  124. // 系统被唤醒后LED亮起20ms
  125. // 如需进一步降低功耗,可以屏蔽
  126. HAL_Delay(20);
  127. //MX_SPI2_Init();
  128. //MX_USART1_UART_Init();
  129. }
  130. else if (wakeup_timer_sta)
  131. {
  132. if (HAL_RTCEx_DeactivateWakeUpTimer(&hrtc) == HAL_OK)
  133. {
  134. wakeup_timer_sta = 0;
  135. }
  136. }
  137. }
  138. uint32_t pm_get_wakeup_events(void)
  139. {
  140. return wakeup_events;
  141. }
  142. void pm_clr_wakeup_events(uint32_t mask)
  143. {
  144. wakeup_events &= ~mask;
  145. }
  146. void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
  147. {
  148. if (GPIO_Pin == KEY_Pin)
  149. {
  150. wakeup_events |= PM_WAKUP_EVT_KEY;
  151. }
  152. }
  153. void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
  154. {
  155. wakeup_events |= PM_WAKUP_EVT_WUT;
  156. }