AT32 PWC — 待机 + RTC 唤醒

AT32 AT32F403A_407_Firmware_Library V2.2.2 PWC 待机 + RTC 唤醒 示例教学

PWC — 待机 + RTC 唤醒

固件库: AT32F403A_407_Firmware_Library V2.2.2
芯片: AT32
源文件: AT32/AT32F403A_407_Firmware_Library_V2.2.2/project/at_start_f403a/examples/pwc/standby_rtc/src/main.c


功能简介

本示例演示待机(Standby)最低功耗模式。几乎所有电路断电,只保留 RTC 和唤醒引脚。唤醒后相当于复位重启。

完整代码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#include "at32f403a_407_board.h"
#include "at32f403a_407_clock.h"

void rtc_config(void)
{
  /* enable the battery-powered domain write operations */
  pwc_battery_powered_domain_access(TRUE);

  /* reset battery-powered domain register */
  bpr_reset();

  /* enable the lick */
  crm_clock_source_enable(CRM_CLOCK_SOURCE_LICK, TRUE);

  /* wait lick is ready */
  while(crm_flag_get(CRM_LICK_STABLE_FLAG) == RESET);

  /* select the rtc clock source */
  crm_rtc_clock_select(CRM_RTC_CLOCK_LICK);

  /* enable rtc clock */
  crm_rtc_clock_enable(TRUE);

  /* wait for rtc registers update */
  rtc_wait_update_finish();

  /* set rtc divider: set rtc period to 1sec */
  rtc_divider_set(32767);

  /* wait for the register write to complete */
  rtc_wait_config_finish();
}

void rtc_alarm_config(uint8_t alarm_time)
{
  /* clear second flag */
  rtc_flag_clear(RTC_TS_FLAG);

  /* wait for the second flag to be set */
  while(rtc_flag_get(RTC_TS_FLAG) == RESET);

  /* config the wakeup time */
  rtc_alarm_set(rtc_counter_get() + alarm_time);

  /* wait for the register write to complete */
  rtc_wait_config_finish();
}

int main(void)
{
  __IO uint32_t index = 0;

  /* congfig the system clock */
  system_clock_config();

  /* init at start board */
  at32_board_init();

  /* config priority group */
  nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);

  /* turn on the led light */
  at32_led_off(LED2);
  at32_led_off(LED3);
  at32_led_off(LED4);

  /* enable pwc and bpr clock */
  crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
  crm_periph_clock_enable(CRM_BPR_PERIPH_CLOCK, TRUE);

  if(pwc_flag_get(PWC_STANDBY_FLAG) != RESET)
  {
    /* wakeup from standby */
    pwc_flag_clear(PWC_STANDBY_FLAG);
    at32_led_on(LED2);
  }

  if(pwc_flag_get(PWC_WAKEUP_FLAG) != RESET)
  {
    /* wakeup event occurs */
    pwc_flag_clear(PWC_WAKEUP_FLAG);
    at32_led_on(LED3);
  }

  /* config rtc */
  rtc_config();

  at32_led_on(LED4);

  delay_ms(1000);

  /* set the wakeup time to 10 seconds */
  rtc_alarm_config(10);

  /* enter standby mode */
  pwc_standby_mode_enter();

  while(1)
  {
  }
}

代码讲解

系统时钟初始化:配置 MCU 的主频和时钟树。不同芯片的时钟配置不同,一般由工具生成。

板级初始化:初始化开发板上的 LED、按键等基础外设。

中断优先级配置:NVIC 设置中断的抢占优先级和子优先级。数字越小优先级越高。

延时:使用系统延时函数控制 LED 翻转间隔。

世界是你们
使用 Hugo 构建
主题 StackJimmy 设计