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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
/*
* Copyright (C) 2022-2024, Xiaohua Semiconductor Co., Ltd.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-06-12 CDT first version
* 2024-06-14 CDT Move common function SysTick_Configuration to _pm_run
* 2026-05-27 CDT Support HC32F4A2
* 2026-06-04 CDT Support HC32F467
* 2026-06-24 CDT rt_system_pm_init parameter timer_mask change to 0 for unsupport pm tickless timer
*/
#include <board.h>
#include <drv_pm.h>
#include <drv_common.h>
#include <drv_wktm.h>
#include <drv_config.h>
#if defined(RT_USING_PM)
#if defined(BSP_USING_PM)
#define LOG_TAG "drv_pm"
#include <drv_log.h>
#define IS_PWC_UNLOCKED() ((CM_PWC->FPRC & PWC_FPRC_FPRCB1) == PWC_FPRC_FPRCB1)
typedef void (*run_switch_func_type)(void);
typedef void (*sleep_enter_func_type)(void);
static void _sleep_enter_idle(void);
static void _sleep_enter_deep(void);
static void _sleep_enter_standby(void);
static void _sleep_enter_shutdown(void);
static void _run_switch_high_to_low(void);
static void _run_switch_low_to_high(void);
static run_switch_func_type _run_switch_func[PM_RUN_MODE_MAX][PM_RUN_MODE_MAX] = {
{ RT_NULL, RT_NULL, RT_NULL, _run_switch_high_to_low },
{ RT_NULL, RT_NULL, RT_NULL, _run_switch_high_to_low },
{ RT_NULL, RT_NULL, RT_NULL, RT_NULL },
{ _run_switch_low_to_high, _run_switch_low_to_high, RT_NULL, RT_NULL },
};
static sleep_enter_func_type _sleep_enter_func[PM_SLEEP_MODE_MAX] = {
RT_NULL,
_sleep_enter_idle,
RT_NULL,
_sleep_enter_deep,
_sleep_enter_standby,
_sleep_enter_shutdown,
};
static void _uart_console_reconfig(void)
{
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
rt_device_control(rt_console_get_device(), RT_DEVICE_CTRL_CONFIG, &config);
}
static void _sleep_enter_idle(void)
{
struct pm_sleep_mode_idle_config sleep_idle_cfg = PM_SLEEP_IDLE_CFG;
PWC_SLEEP_Enter(sleep_idle_cfg.pwc_sleep_type);
}
static void _sleep_enter_deep(void)
{
struct pm_sleep_mode_deep_config sleep_deep_cfg = PM_SLEEP_DEEP_CFG;
RT_ASSERT(PM_SLEEP_CHECK(PM_SLEEP_MODE_DEEP));
(void)PWC_STOP_Config(&sleep_deep_cfg.cfg);
#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F460) || defined(HC32F467) || defined(HC32F448) || defined(HC32F4A8)
if (PWC_PWRC2_DVS == (READ_REG8(CM_PWC->PWRC2) & PWC_PWRC2_DVS))
{
CLR_REG8_BIT(CM_PWC->PWRC1, PWC_PWRC1_STPDAS);
}
else
{
SET_REG8_BIT(CM_PWC->PWRC1, PWC_PWRC1_STPDAS);
}
#endif
PWC_STOP_Enter(sleep_deep_cfg.pwc_stop_type);
}
static void _sleep_enter_standby(void)
{
struct pm_sleep_mode_standby_config sleep_standby_cfg = PM_SLEEP_STANDBY_CFG;
RT_ASSERT(PM_SLEEP_CHECK(PM_SLEEP_MODE_SHUTDOWN));
RT_ASSERT(sleep_standby_cfg.cfg.u8Mode == PWC_PD_MD1 || sleep_standby_cfg.cfg.u8Mode == PWC_PD_MD2);
(void)PWC_PD_Config(&sleep_standby_cfg.cfg);
PWC_PD_ClearWakeupStatus(PWC_PD_WKUP_FLAG_ALL);
__set_FAULTMASK(1);
PWC_PD_Enter();
}
static void _sleep_enter_shutdown(void)
{
struct pm_sleep_mode_shutdown_config sleep_shutdown_cfg = PM_SLEEP_SHUTDOWN_CFG;
RT_ASSERT(PM_SLEEP_CHECK(PM_SLEEP_MODE_SHUTDOWN));
RT_ASSERT(sleep_shutdown_cfg.cfg.u8Mode == PWC_PD_MD3 || sleep_shutdown_cfg.cfg.u8Mode == PWC_PD_MD4);
(void)PWC_PD_Config(&sleep_shutdown_cfg.cfg);
PWC_PD_ClearWakeupStatus(PWC_PD_WKUP_FLAG_ALL);
__set_FAULTMASK(1);
PWC_PD_Enter();
}
/**
* @param pm pointer to power manage structure
*/
static void _pm_sleep(struct rt_pm *pm, uint8_t mode)
{
RT_ASSERT(mode < PM_SLEEP_MODE_MAX);
if (_sleep_enter_func[mode] != NULL)
{
_sleep_enter_func[mode]();
}
}
static void _run_switch_high_to_low(void)
{
struct pm_run_mode_config st_run_mode_cfg = PM_RUN_MODE_CFG;
st_run_mode_cfg.sys_clk_cfg(PM_RUN_MODE_LOW_SPEED);
#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F460) || defined(HC32F467) || defined(HC32F448) || defined(HC32F4A8)
PWC_HighSpeedToLowSpeed();
#endif
}
static void _run_switch_low_to_high(void)
{
struct pm_run_mode_config st_run_mode_cfg = PM_RUN_MODE_CFG;
#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F460) || defined(HC32F467) || defined(HC32F448) || defined(HC32F4A8)
PWC_LowSpeedToHighSpeed();
#endif
st_run_mode_cfg.sys_clk_cfg(PM_RUN_MODE_HIGH_SPEED);
}
static void _pm_run(struct rt_pm *pm, uint8_t mode)
{
static uint8_t last_mode = PM_RUN_MODE_NORMAL_SPEED;
if (mode == last_mode)
{
return;
}
if (_run_switch_func[last_mode][mode] != RT_NULL)
{
_run_switch_func[last_mode][mode]();
SysTick_Configuration();
}
_uart_console_reconfig();
last_mode = mode;
}
/**
* This function calculate the PM tick from OS tick
*
* @param tick OS tick
*
* @return the PM tick
*/
static rt_tick_t _pm_wakeup_timer_tick_from_os_tick(rt_tick_t tick)
{
rt_uint32_t freq = hc32_wktm_get_countfreq();
return (freq * tick / RT_TICK_PER_SECOND);
}
/**
* This function start the timer of pm
*
* @param pm Pointer to power manage structure
* @param timeout How many OS Ticks that MCU can sleep
*/
static void _pm_wakeup_timer_start(struct rt_pm *pm, rt_uint32_t timeout)
{
RT_ASSERT(pm != RT_NULL);
/* Convert OS Tick to pmtimer timeout value */
timeout = _pm_wakeup_timer_tick_from_os_tick(timeout);
/* Enter __WAKEUP_TIMER_MODE */
hc32_wktm_start(timeout);
}
/**
* This function stop the timer of pm
*
* @param pm Pointer to power manage structure
*/
static void _pm_wakeup_timer_stop(struct rt_pm *pm)
{
RT_ASSERT(pm != RT_NULL);
/* Reset pmtimer status */
hc32_wktm_stop();
}
static rt_tick_t _timer_get_tick(struct rt_pm *pm)
{
RT_ASSERT(pm != RT_NULL);
/* Get timeout tick */
return hc32_wktm_get_timeout_tick();
}
/**
* This function initialize the power manager
* @note timer feature: only work as wake up timer
*/
int rt_hw_pm_init(void)
{
static const struct rt_pm_ops _ops = {
_pm_sleep,
_pm_run,
_pm_wakeup_timer_start,
_pm_wakeup_timer_stop,
_timer_get_tick,
};
/* initialize system pm module */
rt_system_pm_init(&_ops, 0U, RT_NULL);
return 0;
}
INIT_DEVICE_EXPORT(rt_hw_pm_init);
#endif
#endif /* RT_USING_PM */
|