你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32G0 stm32g071 关于待机模式和关闭模式的进入和退出(RTC和wakeup pin) HAL库 Standby Mode and

[复制链接]
STMCU小助手 发布时间:2021-11-8 15:49
两天的调试与查阅资料终于明白了HAL库中的低功耗模式的切换和退出。主要还是自己作死选择了STM32G071,本文内容基于NUCLEO-G071RB,CUBEMX版本5.2.0,STM32Cube FW_G0 V1.2.0,MDK 5.27.1.0。

首先配置时钟、UART、RTC和中断。

20190510004029552.png


RCC选择外部晶振(shutdown 模式只能使用外部晶振)其他默认

20190510004154456.png


SYS选择调试接口,和wakeup_pin2,STM32G0目前新线还是很多资源的

20190510005141588.png


设置RTC,填写相关初始化信息、开启ALARMA时间设定为开机后30秒,注意RTC_AlarmMask=RTC_AlarmMask_None;则为精确匹配,即闹钟不仅要求时分秒匹配还要匹配日期和星期,都匹配后触发闹钟中断)

20190510005211104.png


设置串口

20190510005241164.png


打开中断入口

2019051000534771.png


20190510005358325.png


完成设置后点击生成代码创建工程

1.首先喜闻乐见的printf函数
  1. /* USER CODE BEGIN Includes */
  2. #include "stdio.h"
  3. /* USER CODE END Includes */



  4. #ifdef __GNUC__
  5.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  6.      set to 'Yes') calls __io_putchar() */
  7.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  8. #else
  9.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  10. #endif /* __GNUC__ */
  11. PUTCHAR_PROTOTYPE
  12. {
  13.   /* Place your implementation of fputc here */
  14.   /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */

  15.         HAL_UART_Transmit(&hlpuart1, (uint8_t *)&ch, 1, 0xFFFF);
  16.         
  17.   return ch;
  18. }
复制代码

2.RTC时间获取函数

  1. /* USER CODE BEGIN PTD */
  2. RTC_DateTypeDef sdatestructureget;
  3. RTC_TimeTypeDef stimestructureget;
  4. uint8_t aShowTime[16] = "hh:ms:ss";
  5. /* USER CODE END PTD */

  6. /* USER CODE BEGIN PD */
  7. static void RTC_TimeShow(uint8_t *showtime);
  8. /* USER CODE END PD */


  9. static void RTC_TimeShow(uint8_t *showtime)
  10. {


  11.   /* Get the RTC current Time */
  12.   HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
  13.   /* Get the RTC current Date */
  14.   HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
  15.   /* Display time Format : hh:mm:ss */
  16.   sprintf((char *)showtime, "%02d:%02d:%02d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
  17. }
复制代码

3.standby进入函数
  1. /* USER CODE BEGIN PD */
  2. void Sys_Enter_Standby(void);
  3. /* USER CODE END PD */

  4. void Sys_Enter_Standby(void)
  5. {
  6.   /* The Following Wakeup sequence is highly recommended prior to Standby mode entry
  7.   - Enable wakeup
  8.   - Clear wake up pin flag depending in edge detection & pin level.
  9.   - Enter the Standby mode.
  10.   */

  11.   /* Enable WakeUp Pin PWR_WAKEUP_PIN2 connected to PC.13 */
  12.   HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN2_LOW);//上升沿触发
  13.   HAL_PWREx_EnableInternalWakeUpLine();
  14.   /* Clear all related wakeup flags*/
  15.   __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WUF2);

  16.   /* Enter the Standby mode */
  17.   //HAL_PWR_EnterSTANDBYMode();
  18.         printf("enter Shutdown Mode!");
  19.         HAL_PWREx_EnterSHUTDOWNMode();

  20.   /* This code will never be reached! */
  21. }
复制代码

4.ALarmA中断回调函数 NVIC_SystemReset()用于standby触发AlarmA时间后与wake_pin触发系统自复位不同进入rtc中断后无法复位问题(我解决不了就软复位)
  1. void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
  2. {
  3.   /* Prevent unused argument(s) compilation warning */
  4.   UNUSED(hrtc);

  5.   /* NOTE : This function should not be modified, when the callback is needed,
  6.             the HAL_RTC_AlarmAEventCallback could be implemented in the user file
  7.    */

  8.         flag =1;
  9. //        NVIC_SystemReset();
  10.         
  11. }
复制代码

5.在main函数中5秒的时候进入低功耗模式
  1. /* USER CODE BEGIN 2 */
  2.         uint8_t sec=0;
  3.         if (__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET)
  4.   {
  5.     /* Clear Standby flag */
  6.     __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
  7.                 printf("wake up from standby!");
  8.     /* Check and Clear the Wakeup flag */
  9.     if (__HAL_PWR_GET_FLAG(PWR_FLAG_WUF2) != RESET)
  10.     {
  11.       __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WUF2);
  12.     }
  13.                
  14.     /* Wait that user release the User push-button */
  15.      while(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_SET){}

  16.   }


  17.   /* USER CODE END 2 */

  18.   /* Infinite loop */
  19.   /* USER CODE BEGIN WHILE */
  20.   while (1)
  21.   {
  22.     /* USER CODE END WHILE */

  23.     /* USER CODE BEGIN 3 */
  24.                 RTC_TimeShow(aShowTime);
  25.                 if(sec == 0)flag=0;

  26.                 if(sec == 5)Sys_Enter_Standby();
  27.                 if(sec!=stimestructureget.Seconds)
  28.                 {
  29.                         sec=stimestructureget.Seconds;
  30.                         printf("%s",(char *)aShowTime);
  31.                         if(flag) HAL_GPIO_TogglePin(LD4_GPIO_Port,LD4_Pin);
  32.                 }
  33.   }
  34.   /* USER CODE END 3 */
  35. }
复制代码

至此STM32G071关于低功耗模式的进入与退出已经可以实际使用,大家可以参考,实际使用稍作调整。

附加 避免上电自动配置RTC时钟
  1. /* USER CODE BEGIN Check_RTC_BKUP */
  2.         if (__HAL_RTC_GET_FLAG(&hrtc, RTC_FLAG_INITS) == 0x00u){}
  3.         else{return;}
  4.   /* USER CODE END Check_RTC_BKUP */
复制代码

以上代码请添加于MX_RTC_Init(void)中

2019051001272486.png


项目文件

20190510013203724.png



收藏 评论0 发布时间:2021-11-8 15:49

举报

0个回答

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版