Skip to content

Commit d0b686e

Browse files
authored
Merge pull request #2653 from Zero-Free/pm_dev
改进低功耗组件
2 parents abc5fff + d51e078 commit d0b686e

File tree

17 files changed

+1027
-517
lines changed

17 files changed

+1027
-517
lines changed

bsp/es32f0334/drivers/drv_pm.c

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2019-04-08 wangyq the first version
9+
* 2019-05-06 Zero-Free adapt to the new power management interface
910
*/
1011

1112
#include <rthw.h>
@@ -16,52 +17,26 @@
1617

1718
#ifdef RT_USING_PM
1819

19-
static void _drv_pm_enter(struct rt_pm *pm)
20+
static void _drv_pm_enter(struct rt_pm *pm, uint8_t mode)
2021
{
21-
rt_uint32_t mode;
22-
23-
mode = pm->current_mode;
24-
2522
switch (mode)
2623
{
27-
case PM_RUN_MODE_NORMAL:
24+
case PM_SLEEP_MODE_NONE:
2825
break;
2926

30-
case PM_SLEEP_MODE_SLEEP:
27+
case PM_SLEEP_MODE_IDLE:
3128
__WFI();
3229
break;
3330

34-
case PM_SLEEP_MODE_TIMER:
35-
pmu_stop2_enter();
36-
break;
37-
38-
case PM_SLEEP_MODE_SHUTDOWN:
39-
pmu_standby_enter(PMU_STANDBY_PORT_NONE);
40-
break;
41-
42-
default:
43-
RT_ASSERT(0);
44-
break;
45-
}
46-
}
47-
48-
static void _drv_pm_exit(struct rt_pm *pm)
49-
{
50-
rt_uint32_t mode;
51-
52-
RT_ASSERT(pm != RT_NULL);
53-
54-
mode = pm->current_mode;
55-
56-
switch (mode)
57-
{
58-
case PM_RUN_MODE_NORMAL:
31+
case PM_SLEEP_MODE_LIGHT:
5932
break;
6033

61-
case PM_SLEEP_MODE_SLEEP:
34+
case PM_SLEEP_MODE_DEEP:
35+
pmu_stop2_enter();
6236
break;
6337

64-
case PM_SLEEP_MODE_TIMER:
38+
case PM_SLEEP_MODE_STANDBY:
39+
pmu_standby_enter(PMU_STANDBY_PORT_NONE);
6540
break;
6641

6742
case PM_SLEEP_MODE_SHUTDOWN:
@@ -73,32 +48,21 @@ static void _drv_pm_exit(struct rt_pm *pm)
7348
}
7449
}
7550

76-
#if PM_RUN_MODE_COUNT > 1
77-
static void _drv_pm_frequency_change(struct rt_pm *pm, rt_uint32_t frequency)
78-
{
79-
return;
80-
}
81-
#endif
82-
8351
static int drv_hw_pm_init(void)
8452
{
8553
static const struct rt_pm_ops _ops =
8654
{
8755
_drv_pm_enter,
88-
_drv_pm_exit,
89-
90-
#if PM_RUN_MODE_COUNT > 1
91-
_drv_pm_frequency_change,
92-
#endif
56+
RT_NULL,
9357
RT_NULL,
9458
RT_NULL,
9559
RT_NULL
9660
};
9761

98-
rt_uint8_t timer_mask;
62+
rt_uint8_t timer_mask = 0;
9963

100-
/* initialize timer mask */
101-
timer_mask = 1UL << PM_SLEEP_MODE_TIMER;
64+
/* initialize timer mask(no need tickless) */
65+
// timer_mask = 1UL << PM_SLEEP_MODE_DEEP;
10266

10367
/* initialize system pm module */
10468
rt_system_pm_init(&_ops, timer_mask, RT_NULL);

bsp/es32f0654/drivers/drv_pm.c

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2019-04-01 wangyq the first version
9+
* 2019-05-06 Zero-Free adapt to the new power management interface
910
*/
1011

1112
#include <rthw.h>
@@ -16,52 +17,26 @@
1617

1718
#ifdef RT_USING_PM
1819

19-
static void _drv_pm_enter(struct rt_pm *pm)
20+
static void _drv_pm_enter(struct rt_pm *pm, uint8_t mode)
2021
{
21-
rt_uint32_t mode;
22-
23-
mode = pm->current_mode;
24-
2522
switch (mode)
2623
{
27-
case PM_RUN_MODE_NORMAL:
24+
case PM_SLEEP_MODE_NONE:
2825
break;
2926

30-
case PM_SLEEP_MODE_SLEEP:
27+
case PM_SLEEP_MODE_IDLE:
3128
__WFI();
3229
break;
3330

34-
case PM_SLEEP_MODE_TIMER:
35-
pmu_stop2_enter();
36-
break;
37-
38-
case PM_SLEEP_MODE_SHUTDOWN:
39-
pmu_standby_enter(PMU_STANDBY_PORT_NONE);
40-
break;
41-
42-
default:
43-
RT_ASSERT(0);
44-
break;
45-
}
46-
}
47-
48-
static void _drv_pm_exit(struct rt_pm *pm)
49-
{
50-
rt_uint32_t mode;
51-
52-
RT_ASSERT(pm != RT_NULL);
53-
54-
mode = pm->current_mode;
55-
56-
switch (mode)
57-
{
58-
case PM_RUN_MODE_NORMAL:
31+
case PM_SLEEP_MODE_LIGHT:
5932
break;
6033

61-
case PM_SLEEP_MODE_SLEEP:
34+
case PM_SLEEP_MODE_DEEP:
35+
pmu_stop2_enter();
6236
break;
6337

64-
case PM_SLEEP_MODE_TIMER:
38+
case PM_SLEEP_MODE_STANDBY:
39+
pmu_standby_enter(PMU_STANDBY_PORT_NONE);
6540
break;
6641

6742
case PM_SLEEP_MODE_SHUTDOWN:
@@ -73,32 +48,21 @@ static void _drv_pm_exit(struct rt_pm *pm)
7348
}
7449
}
7550

76-
#if PM_RUN_MODE_COUNT > 1
77-
static void _drv_pm_frequency_change(struct rt_pm *pm, rt_uint32_t frequency)
78-
{
79-
return;
80-
}
81-
#endif
82-
8351
static int drv_hw_pm_init(void)
8452
{
8553
static const struct rt_pm_ops _ops =
8654
{
8755
_drv_pm_enter,
88-
_drv_pm_exit,
89-
90-
#if PM_RUN_MODE_COUNT > 1
91-
_drv_pm_frequency_change,
92-
#endif
56+
RT_NULL,
9357
RT_NULL,
9458
RT_NULL,
9559
RT_NULL
9660
};
9761

98-
rt_uint8_t timer_mask;
62+
rt_uint8_t timer_mask = 0;
9963

100-
/* initialize timer mask */
101-
timer_mask = 1UL << PM_SLEEP_MODE_TIMER;
64+
/* initialize timer mask(no need tickless) */
65+
timer_mask = 1UL << PM_SLEEP_MODE_DEEP;
10266

10367
/* initialize system pm module */
10468
rt_system_pm_init(&_ops, timer_mask, RT_NULL);

bsp/stm32/libraries/HAL_Drivers/SConscript

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ if GetDepend(['RT_USING_ADC']):
3939
if GetDepend(['RT_USING_CAN']):
4040
src += ['drv_can.c']
4141

42+
if GetDepend(['RT_USING_PM', 'SOC_SERIES_STM32L4']):
43+
src += ['drv_pm.c']
44+
src += ['drv_lptim.c']
45+
4246
if GetDepend('BSP_USING_SDRAM'):
4347
src += ['drv_sdram.c']
4448

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2019-05-06 Zero-Free first version
9+
*/
10+
11+
#include <board.h>
12+
#include <drv_lptim.h>
13+
14+
static LPTIM_HandleTypeDef LptimHandle;
15+
16+
void HAL_LPTIM_MspInit(LPTIM_HandleTypeDef *hlptim)
17+
{
18+
if (hlptim->Instance == LPTIM1)
19+
{
20+
/* Peripheral clock enable */
21+
__HAL_RCC_LPTIM1_CLK_ENABLE();
22+
}
23+
}
24+
25+
void LPTIM1_IRQHandler(void)
26+
{
27+
HAL_LPTIM_IRQHandler(&LptimHandle);
28+
}
29+
30+
void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
31+
{
32+
/* enter interrupt */
33+
rt_interrupt_enter();
34+
35+
/* leave interrupt */
36+
rt_interrupt_leave();
37+
}
38+
39+
/**
40+
* This function get current count value of LPTIM
41+
*
42+
* @return the count vlaue
43+
*/
44+
rt_uint32_t stm32l4_lptim_get_current_tick(void)
45+
{
46+
return HAL_LPTIM_ReadCounter(&LptimHandle);
47+
}
48+
49+
/**
50+
* This function get the max value that LPTIM can count
51+
*
52+
* @return the max count
53+
*/
54+
rt_uint32_t stm32l4_lptim_get_tick_max(void)
55+
{
56+
return (0xFFFF);
57+
}
58+
59+
/**
60+
* This function start LPTIM with reload value
61+
*
62+
* @param reload The value that LPTIM count down from
63+
*
64+
* @return RT_EOK
65+
*/
66+
rt_err_t stm32l4_lptim_start(rt_uint32_t reload)
67+
{
68+
HAL_LPTIM_TimeOut_Start_IT(&LptimHandle, 0xFFFF, reload);
69+
70+
return (RT_EOK);
71+
}
72+
73+
/**
74+
* This function stop LPTIM
75+
*/
76+
void stm32l4_lptim_stop(void)
77+
{
78+
rt_uint32_t _ier;
79+
80+
_ier = LptimHandle.Instance->IER;
81+
LptimHandle.Instance->ICR = LptimHandle.Instance->ISR & _ier;
82+
}
83+
84+
/**
85+
* This function get the count clock of LPTIM
86+
*
87+
* @return the count clock frequency in Hz
88+
*/
89+
rt_uint32_t stm32l4_lptim_get_countfreq(void)
90+
{
91+
return 32000 / 32;
92+
}
93+
94+
/**
95+
* This function initialize the lptim
96+
*/
97+
int stm32l4_hw_lptim_init(void)
98+
{
99+
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
100+
RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct = {0};
101+
102+
/* Enable LSI clock */
103+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
104+
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
105+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
106+
HAL_RCC_OscConfig(&RCC_OscInitStruct);
107+
108+
/* Select the LSI clock as LPTIM peripheral clock */
109+
RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPTIM1;
110+
RCC_PeriphCLKInitStruct.Lptim1ClockSelection = RCC_LPTIM1CLKSOURCE_LSI;
111+
HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct);
112+
113+
LptimHandle.Instance = LPTIM1;
114+
LptimHandle.Init.Clock.Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;
115+
LptimHandle.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV32;
116+
LptimHandle.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE;
117+
LptimHandle.Init.OutputPolarity = LPTIM_OUTPUTPOLARITY_HIGH;
118+
LptimHandle.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE;
119+
LptimHandle.Init.CounterSource = LPTIM_COUNTERSOURCE_INTERNAL;
120+
if (HAL_LPTIM_Init(&LptimHandle) != HAL_OK)
121+
{
122+
return -1;
123+
}
124+
125+
NVIC_ClearPendingIRQ(LPTIM1_IRQn);
126+
NVIC_SetPriority(LPTIM1_IRQn, 0);
127+
NVIC_EnableIRQ(LPTIM1_IRQn);
128+
129+
return 0;
130+
}
131+
132+
INIT_DEVICE_EXPORT(stm32l4_hw_lptim_init);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2019-05-06 Zero-Free first version
9+
*/
10+
11+
#ifndef __DRV_PMTIMER_H__
12+
#define __DRV_PMTIMER_H__
13+
14+
#include <rtthread.h>
15+
16+
rt_uint32_t stm32l4_lptim_get_countfreq(void);
17+
rt_uint32_t stm32l4_lptim_get_tick_max(void);
18+
rt_uint32_t stm32l4_lptim_get_current_tick(void);
19+
20+
rt_err_t stm32l4_lptim_start(rt_uint32_t load);
21+
void stm32l4_lptim_stop(void);
22+
23+
#endif /* __DRV_PMTIMER_H__ */

0 commit comments

Comments
 (0)