1 /*
2  * Copyright (C) 2018-2020 Alibaba Group Holding Limited
3  */
4 
5 #ifndef __GENIE_LPM_H__
6 #define __GENIE_LPM_H__
7 
8 #define INTERRUPT_DELAY_TIME (50) //For debounce
9 
10 #define DEFAULT_BOOTUP_DELAY_SLEEP_TIME (10000) //Unit:ms
11 
12 #define PWR_STANDBY_BOOT_FLAG (0x1688)
13 #define PWR_BOOT_REASON (0x4000f034)
14 
15 typedef enum _genie_lpm_status_e
16 {
17     STATUS_WAKEUP,
18     STATUS_SLEEP
19 } genie_lpm_status_e;
20 
21 typedef enum _genie_lpm_wakeup_reason_e
22 {
23     WAKEUP_BY_IO,
24     WAKEUP_BY_TIMER,
25     WAKEUP_IS_WAKEUP
26 } genie_lpm_wakeup_reason_e;
27 
28 typedef void (*genie_lpm_cb_t)(genie_lpm_wakeup_reason_e reason, genie_lpm_status_e status, void* arg);
29 
30 typedef struct _genie_lpm_wakeup_io_config_s {
31    uint8_t port;
32    uint8_t io_pol;
33 }genie_lpm_wakeup_io_config_t;
34 
35 
36 #define GENIE_WAKEUP_PIN(_port, _pol)  \
37 {                      \
38 	.port   = (_port), \
39 	.io_pol = (_pol),   \
40 }
41 
42 typedef struct _genie_lpm_wakeup_io_s {
43    uint8_t io_list_size;
44    genie_lpm_wakeup_io_config_t* io_config;
45 } genie_lpm_wakeup_io_t;
46 
47 typedef struct _genie_lpm_io_status_s {
48    uint8_t port;
49    uint8_t trigger_flag;
50    uint8_t status;
51 }_genie_lpm_io_status_t;
52 
53 typedef struct _genie_lpm_io_status_list_s {
54    _genie_lpm_io_status_t *io_status;
55    uint8_t size;
56 }_genie_lpm_io_status_list_t;
57 
58 
59 typedef struct _genie_lpm_conf_s
60 {
61     uint8_t lpm_wakeup_io;       //0:then not wakeup by GPIO
62     genie_lpm_wakeup_io_t  lpm_wakeup_io_config;   //wakeup IO list config
63     uint8_t is_auto_enable;      //1:auto enter sleep mode when bootup
64     uint32_t delay_sleep_time;   //if auto enter sleep,delay some time then enter sleep mode
65     uint16_t sleep_ms;           //sleep time
66     uint16_t wakeup_ms;          //wakeup time
67     genie_lpm_cb_t genie_lpm_cb; //User callback
68 } genie_lpm_conf_t;
69 
70 typedef struct _genie_lpm_ctx_s
71 {
72     genie_lpm_conf_t p_config;
73     uint8_t status;
74     uint8_t is_mesh_init;
75     uint8_t has_disabled;
76     aos_timer_t wakeup_timer;
77     aos_timer_t io_wakeup_timer;
78     aos_mutex_t mutex;
79     long long last_interrupt_time;
80 } genie_lpm_ctx_t;
81 
82 
83 void genie_lpm_init(genie_lpm_conf_t *lpm_conf);
84 int genie_lpm_start(void);
85 void genie_lpm_io_wakeup_handler(void *arg);
86 int genie_lpm_enable(bool force);
87 int genie_lpm_disable(void);
88 int genie_lpm_deep_sleep(void);
89 bool genie_lpm_get_wakeup_io_status(void);
90 
91 #endif
92