pm.h
Go to the documentation of this file.
1 /********************************************************************************************************
2  * @file pm.h
3  *
4  * @brief This is the header file for TLSR8258
5  *
6  * @author Driver Group
7  * @date May 8, 2018
8  *
9  * @par Copyright (c) 2018, Telink Semiconductor (Shanghai) Co., Ltd.
10  * All rights reserved.
11  *
12  * The information contained herein is confidential property of Telink
13  * Semiconductor (Shanghai) Co., Ltd. and is available under the terms
14  * of Commercial License Agreement between Telink Semiconductor (Shanghai)
15  * Co., Ltd. and the licensee or the terms described here-in. This heading
16  * MUST NOT be removed from this file.
17  *
18  * Licensees are granted free, non-transferable use of the information in this
19  * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
20  * @par History:
21  * 1.initial release(DEC. 26 2018)
22  *
23  * @version A001
24  *
25  *******************************************************************************************************/
26 
27 #pragma once
28 
29 #include "bsp.h"
30 #include "gpio.h"
31 
32 #define PM_LONG_SUSPEND_EN 1
33 
34 #ifndef PM_TIM_RECOVER_MODE
35 #define PM_TIM_RECOVER_MODE 0
36 #endif
37 
38 
39 
40 #define PM_XTAL_DELAY_DURATION 500
41 #define EARLYWAKEUP_TIME_US_DEEP 1100
42 #define EARLYWAKEUP_TIME_US_SUSPEND 1250
43 #define EMPTYRUN_TIME_US 1500
44 
45 #define PM_DCDC_DELAY_DURATION 1000
46 
47 
48 
49 #define PM_LONG_SLEEP_WAKEUP_EN 0 //if user need to make MCU sleep for a long time that is more than 268s, this macro need to be enabled and use "pm_long_sleep_wakeup" function
50 #define SYS_CLK_48MRC_EN 0 //if user take 48M RC as system clock, this macro need to be enabled
51 
59 #define DEEP_ANA_REG0 0x3a //initial value =0x00
60 #define DEEP_ANA_REG1 0x3b //initial value =0x00
61 #define DEEP_ANA_REG2 0x3c //initial value =0x00
62 
68 #define DEEP_ANA_REG6 0x35 //initial value =0x00
69 #define DEEP_ANA_REG7 0x36 //initial value =0x00
70 #define DEEP_ANA_REG8 0x37 //initial value =0x00
71 #define DEEP_ANA_REG9 0x38 //initial value =0x00
72 #define DEEP_ANA_REG10 0x39 //initial value =0x00
73 
74 
75 #define SYS_NEED_REINIT_EXT32K BIT(0)
76 
77 
78 //ana3c system used, user can not use
79 #define SYS_DEEP_ANA_REG DEEP_ANA_REG2
80 #define WAKEUP_STATUS_TIMER_CORE ( WAKEUP_STATUS_TIMER | WAKEUP_STATUS_CORE)
81 #define WAKEUP_STATUS_TIMER_PAD ( WAKEUP_STATUS_TIMER | WAKEUP_STATUS_PAD)
82 
83 
87 typedef enum {
88  //available mode for customer
90 
92  DEEPSLEEP_MODE_RET_SRAM_LOW8K = 0x61, //for boot from sram
93  DEEPSLEEP_MODE_RET_SRAM_LOW16K = 0x43, //for boot from sram
94  DEEPSLEEP_MODE_RET_SRAM_LOW32K = 0x07, //for boot from sram
95 
96  SHUTDOWN_MODE = 0xFF,
97 
98  //not available mode
101 
102 
107 typedef enum {
108  //available wake-up source for customer
113  //not available wake-up source for customer
117 
122 enum {
127 
130 
132 };
133 
137 typedef struct{
138  unsigned char ext_cap_en; //24xtal cap
139  unsigned char pad32k_en;
140  unsigned char pm_enter_en;
141 }misc_para_t;
142 
144 
148 typedef struct{
149  unsigned char is_deepretn_back;
150  unsigned char is_pad_wakeup;
151  unsigned char wakeup_src;
152 }pm_para_t;
153 
154 extern pm_para_t pmParam;
155 
156 #if (PM_TIM_RECOVER_MODE)
157 
158 typedef struct{
159  unsigned int tick_sysClk;
160  unsigned int tick_32k;
161  unsigned char recover_flag;
162 }pm_tim_recover_t;
163 
164 extern pm_tim_recover_t pm_timRecover;
165 #endif
166 
167 
168 typedef int (*suspend_handler_t)(void);
170 
172 
173 
179 static inline int pm_is_MCU_deepRetentionWakeup(void)
180 {
181  return pmParam.is_deepretn_back;
182 }
183 
189 static inline int pm_is_deepPadWakeup(void)
190 {
191  return pmParam.is_pad_wakeup;
192 }
193 
199 static inline int pm_get_wakeup_src(void)
200 {
201  return pmParam.wakeup_src;
202 }
203 
209 void cpu_stall_wakeup_by_timer0(unsigned int tick);
210 
216 void cpu_stall_wakeup_by_timer1(unsigned int tick);
217 
223 void cpu_stall_wakeup_by_timer2(unsigned int tick);
224 
232 unsigned int cpu_stall(int WakeupSrc, unsigned int IntervalUs,unsigned int sysclktick);
233 
242 
243 
250 void start_reboot(void);
251 
258 extern unsigned int pm_get_32k_tick(void);
259 
265 void cpu_wakeup_init(void);
266 
272 unsigned int pm_tim_recover_32k_rc(unsigned int now_tick_32k);
273 
279 unsigned int pm_tim_recover_32k_xtal(unsigned int now_tick_32k);
280 
281 
282 typedef unsigned int (*pm_tim_recover_handler_t)(unsigned int);
283 
285 
286 
294 int cpu_sleep_wakeup_32k_rc(SleepMode_TypeDef sleep_mode, SleepWakeupSrc_TypeDef wakeup_src, unsigned int wakeup_tick);
295 
303 int cpu_sleep_wakeup_32k_xtal(SleepMode_TypeDef sleep_mode, SleepWakeupSrc_TypeDef wakeup_src, unsigned int wakeup_tick);
304 
305 typedef int (*cpu_pm_handler_t)(SleepMode_TypeDef sleep_mode, SleepWakeupSrc_TypeDef wakeup_src, unsigned int wakeup_tick);
306 
308 
314 static inline void blc_pm_select_internal_32k_crystal(void)
315 {
318 
319  blt_miscParam.pm_enter_en = 1; // allow enter pm, 32k rc does not need to wait for 32k clk to be stable
320 }
321 
327 static inline void blc_pm_select_external_32k_crystal(void)
328 {
331 
332  blt_miscParam.pad32k_en = 1; // set '1': 32k clk src use external 32k crystal
333 }
334 
335 /********************************** Internal APIs (not for user)***************************************************/
336 extern unsigned char tl_multi_addr;
337 extern unsigned char tl_24mrc_cal;
338 extern unsigned short tick_32k_calib;
339 extern unsigned int tick_cur;
340 extern unsigned int tick_32k_cur;
341 
342 void sleep_start(void);
343 
344 unsigned int pm_get_info0(void);
345 
346 unsigned int pm_get_info1(void);
347 
348 unsigned int cpu_get_32k_tick(void);
349 
351 
352 void check_32k_clk_stable(void);
353 
354 #if PM_LONG_SLEEP_WAKEUP_EN
355 
362 int pm_long_sleep_wakeup (SleepMode_TypeDef sleep_mode, SleepWakeupSrc_TypeDef wakeup_src, unsigned int SleepDurationUs);
363 #endif
364 
365  //end of GP7
static int pm_get_wakeup_src(void)
This function serves to get the source of wake-up.
Definition: pm.h:199
unsigned char pm_enter_en
Definition: pm.h:140
unsigned char is_pad_wakeup
Definition: pm.h:150
Definition: pm.h:96
suspend_handler_t func_before_suspend
Definition: pm.h:129
unsigned int pm_get_32k_tick(void)
This function serves to get the 32k tick.
void cpu_stall_wakeup_by_timer2(unsigned int tick)
This function serves to wake up cpu from stall mode by timer2.
static void blc_pm_select_internal_32k_crystal(void)
This function serves to determine whether wake up source is internal 32k RC.
Definition: pm.h:314
void cpu_stall_wakeup_by_timer1(unsigned int tick)
This function serves to wake up cpu from stall mode by timer1.
unsigned char is_deepretn_back
Definition: pm.h:149
Definition: pm.h:111
unsigned int tick_32k_cur
unsigned char tl_24mrc_cal
void cpu_stall_wakeup_by_timer0(unsigned int tick)
This function serves to wake up cpu from stall mode by timer0.
static void blc_pm_select_external_32k_crystal(void)
This function serves to determine whether wake up source is external 32k RC.
Definition: pm.h:327
Definition: pm.h:112
pm_tim_recover_handler_t pm_tim_recover
unsigned char wakeup_src
Definition: pm.h:151
int cpu_sleep_wakeup_32k_rc(SleepMode_TypeDef sleep_mode, SleepWakeupSrc_TypeDef wakeup_src, unsigned int wakeup_tick)
This function serves to set the working mode of MCU based on 32k crystal,e.g. suspend mode...
int(* suspend_handler_t)(void)
Definition: pm.h:168
unsigned int tick_cur
static int pm_is_deepPadWakeup(void)
This function serves to determine whether mcu is waked up by pad.
Definition: pm.h:189
SleepWakeupSrc_TypeDef
wakeup source
Definition: pm.h:107
unsigned int pm_get_info1(void)
misc_para_t blt_miscParam
#define BIT(n)
Definition: bsp.h:33
unsigned int pm_get_info0(void)
unsigned int pm_tim_recover_32k_rc(unsigned int now_tick_32k)
This function serves to recover system timer from tick of internal 32k RC.
unsigned short tick_32k_calib
unsigned int cpu_get_32k_tick(void)
void cpu_set_gpio_wakeup(GPIO_PinTypeDef pin, GPIO_LevelTypeDef pol, int en)
This function configures a GPIO pin as the wakeup pin.
Definition: pm.h:125
static int pm_is_MCU_deepRetentionWakeup(void)
This function serves to determine whether mcu is waked up from deep retention.
Definition: pm.h:179
void bls_pm_registerFuncBeforeSuspend(suspend_handler_t func)
void soft_reboot_dly13ms_use24mRC(void)
void check_32k_clk_stable(void)
unsigned int pm_tim_recover_32k_xtal(unsigned int now_tick_32k)
This function serves to recover system timer from tick of external 32k crystal.
SleepMode_TypeDef
sleep mode.
Definition: pm.h:87
void start_reboot(void)
This function serves to reboot chip.
unsigned int(* pm_tim_recover_handler_t)(unsigned int)
Definition: pm.h:282
unsigned char ext_cap_en
Definition: pm.h:138
deepsleep wakeup status
Definition: pm.h:148
unsigned char pad32k_en
Definition: pm.h:139
Definition: pm.h:128
GPIO_PinTypeDef
Define GPIO types.
Definition: gpio_8258.h:38
Definition: pm.h:91
Definition: pm.h:110
void cpu_wakeup_init(void)
This function serves to initialize MCU.
Definition: pm.h:131
Definition: pm.h:99
unsigned char tl_multi_addr
Definition: pm.h:109
Definition: pm.h:114
unsigned int cpu_stall(int WakeupSrc, unsigned int IntervalUs, unsigned int sysclktick)
This function serves to wake up cpu from stall mode by timer1 or RF TX done irq.
Definition: pm.h:89
deepsleep wakeup by external xtal
Definition: pm.h:137
int cpu_sleep_wakeup_32k_xtal(SleepMode_TypeDef sleep_mode, SleepWakeupSrc_TypeDef wakeup_src, unsigned int wakeup_tick)
This function serves to set the working mode of MCU based on 32k crystal,e.g. suspend mode...
Definition: pm.h:123
cpu_pm_handler_t cpu_sleep_wakeup
int(* cpu_pm_handler_t)(SleepMode_TypeDef sleep_mode, SleepWakeupSrc_TypeDef wakeup_src, unsigned int wakeup_tick)
Definition: pm.h:305
Definition: pm.h:126
pm_para_t pmParam
GPIO_LevelTypeDef
Definition: gpio_8258.h:125
void sleep_start(void)
Definition: pm.h:115
Definition: pm.h:124