From 7e06b6c50448bc7c082e9235bb90263970468ad3 Mon Sep 17 00:00:00 2001 From: jonas Date: Thu, 9 Dec 2021 18:11:55 +0800 Subject: [fix]修复AT组件在CLI模式下,指令输入过长导致数组越界的问题 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/net/at/src/at_cli.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'components') diff --git a/components/net/at/src/at_cli.c b/components/net/at/src/at_cli.c index c589bb68a0..d51358d863 100644 --- a/components/net/at/src/at_cli.c +++ b/components/net/at/src/at_cli.c @@ -264,6 +264,10 @@ static void client_cli_parser(at_client_t client) } else { + if(cur_line_len >= FINSH_CMD_SIZE) + { + continue; + } rt_kprintf("%c", ch); cur_line[cur_line_len++] = ch; } -- cgit v1.3.1 From 7ff976dab392a82f4307b58b36c6551612fa7e5f Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Tue, 7 Dec 2021 02:18:58 -0500 Subject: [posix] [pipe] add IPC for POSIX and add pipe for it --- components/drivers/Kconfig | 6 +----- components/drivers/include/ipc/pipe.h | 6 ------ components/drivers/src/pipe.c | 26 +++++++++++++------------- components/libc/posix/Kconfig | 2 ++ components/libc/posix/ipc/Kconfig | 17 +++++++++++++++++ 5 files changed, 33 insertions(+), 24 deletions(-) create mode 100644 components/libc/posix/ipc/Kconfig (limited to 'components') diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 159a6cbda3..8d340da3f5 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -5,10 +5,6 @@ config RT_USING_DEVICE_IPC default y if RT_USING_DEVICE_IPC - config RT_PIPE_BUFSZ - int "Set pipe buffer size" - default 512 - config RT_USING_SYSTEM_WORKQUEUE bool "Using system default workqueue" default n @@ -18,7 +14,7 @@ if RT_USING_DEVICE_IPC int "The stack size for system workqueue thread" default 2048 - config RT_SYSTEM_WORKQUEUE_PRIORITY + config RT_SYSTEM_WORKQUEUE_PRIORITY int "The priority level of system workqueue thread" default 23 endif diff --git a/components/drivers/include/ipc/pipe.h b/components/drivers/include/ipc/pipe.h index a45e8ae002..7d271c3a04 100644 --- a/components/drivers/include/ipc/pipe.h +++ b/components/drivers/include/ipc/pipe.h @@ -15,12 +15,6 @@ #include #include -#ifndef RT_PIPE_BUFSZ -#define PIPE_BUFSZ 512 -#else -#define PIPE_BUFSZ RT_PIPE_BUFSZ -#endif - struct rt_pipe_device { struct rt_device parent; diff --git a/components/drivers/src/pipe.c b/components/drivers/src/pipe.c index 89b544bc4e..8f9a62e05b 100644 --- a/components/drivers/src/pipe.c +++ b/components/drivers/src/pipe.c @@ -13,7 +13,7 @@ #include #include -#ifdef RT_USING_POSIX_DEVIO +#if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE) #include #include #include @@ -320,9 +320,9 @@ static const struct dfs_file_ops pipe_fops = RT_NULL, pipe_fops_poll, }; -#endif /* RT_USING_POSIX_DEVIO */ +#endif /* defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE) */ -rt_err_t rt_pipe_open(rt_device_t device, rt_uint16_t oflag) +static rt_err_t rt_pipe_open(rt_device_t device, rt_uint16_t oflag) { rt_pipe_t *pipe = (rt_pipe_t *)device; rt_err_t ret = RT_EOK; @@ -350,7 +350,7 @@ __exit: return ret; } -rt_err_t rt_pipe_close(rt_device_t device) +static rt_err_t rt_pipe_close(rt_device_t device) { rt_pipe_t *pipe = (rt_pipe_t *)device; @@ -368,7 +368,7 @@ rt_err_t rt_pipe_close(rt_device_t device) return RT_EOK; } -rt_size_t rt_pipe_read(rt_device_t device, rt_off_t pos, void *buffer, rt_size_t count) +static rt_size_t rt_pipe_read(rt_device_t device, rt_off_t pos, void *buffer, rt_size_t count) { uint8_t *pbuf; rt_size_t read_bytes = 0; @@ -396,7 +396,7 @@ rt_size_t rt_pipe_read(rt_device_t device, rt_off_t pos, void *buffer, rt_size_t return read_bytes; } -rt_size_t rt_pipe_write(rt_device_t device, rt_off_t pos, const void *buffer, rt_size_t count) +static rt_size_t rt_pipe_write(rt_device_t device, rt_off_t pos, const void *buffer, rt_size_t count) { uint8_t *pbuf; rt_size_t write_bytes = 0; @@ -424,7 +424,7 @@ rt_size_t rt_pipe_write(rt_device_t device, rt_off_t pos, const void *buffer, rt return write_bytes; } -rt_err_t rt_pipe_control(rt_device_t dev, int cmd, void *args) +static rt_err_t rt_pipe_control(rt_device_t dev, int cmd, void *args) { return RT_EOK; } @@ -479,9 +479,9 @@ rt_pipe_t *rt_pipe_create(const char *name, int bufsz) rt_free(pipe); return RT_NULL; } -#ifdef RT_USING_POSIX_DEVIO +#if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE) dev->fops = (void*)&pipe_fops; -#endif /* RT_USING_POSIX_DEVIO */ +#endif return pipe; } @@ -529,7 +529,7 @@ int rt_pipe_delete(const char *name) return result; } -#ifdef RT_USING_POSIX_DEVIO +#if defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE) int pipe(int fildes[2]) { rt_pipe_t *pipe; @@ -539,7 +539,7 @@ int pipe(int fildes[2]) rt_snprintf(dname, sizeof(dname), "pipe%d", pipeno++); - pipe = rt_pipe_create(dname, PIPE_BUFSZ); + pipe = rt_pipe_create(dname, RT_USING_POSIX_PIPE_SIZE); if (pipe == RT_NULL) { return -1; @@ -567,7 +567,7 @@ int mkfifo(const char *path, mode_t mode) { rt_pipe_t *pipe; - pipe = rt_pipe_create(path, PIPE_BUFSZ); + pipe = rt_pipe_create(path, RT_USING_POSIX_PIPE_SIZE); if (pipe == RT_NULL) { return -1; @@ -575,4 +575,4 @@ int mkfifo(const char *path, mode_t mode) return 0; } -#endif /* RT_USING_POSIX_DEVIO */ +#endif /* defined(RT_USING_POSIX_DEVIO) && defined(RT_USING_POSIX_PIPE) */ diff --git a/components/libc/posix/Kconfig b/components/libc/posix/Kconfig index db83269dd0..52b5fdc948 100644 --- a/components/libc/posix/Kconfig +++ b/components/libc/posix/Kconfig @@ -55,4 +55,6 @@ if RT_USING_PTHREADS default 8 endif +source "$RTT_DIR/components/libc/posix/ipc/Kconfig" + endmenu diff --git a/components/libc/posix/ipc/Kconfig b/components/libc/posix/ipc/Kconfig new file mode 100644 index 0000000000..f5ec6ad040 --- /dev/null +++ b/components/libc/posix/ipc/Kconfig @@ -0,0 +1,17 @@ +menu "Interprocess Communication (IPC)" + +config RT_USING_POSIX_PIPE + bool "Enable pipe and FIFO" + select RT_USING_POSIX_FS + select RT_USING_POSIX_DEVIO + select RT_USING_POSIX_POLL + default n + +config RT_USING_POSIX_PIPE_SIZE + int "Set pipe buffer size" + depends on RT_USING_POSIX_PIPE + default 512 + +comment "Socket is in the 'Network' category" + +endmenu -- cgit v1.3.1 From ce8085befa161f9940157df6b41190aaf531a5cc Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Tue, 7 Dec 2021 15:42:06 -0500 Subject: [posix][ipc] add ipc header files --- components/libc/posix/ipc/sys/ipc.h | 15 +++++++++++++++ components/libc/posix/ipc/sys/msg.h | 15 +++++++++++++++ components/libc/posix/ipc/sys/sem.h | 15 +++++++++++++++ components/libc/posix/ipc/sys/shm.h | 15 +++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 components/libc/posix/ipc/sys/ipc.h create mode 100644 components/libc/posix/ipc/sys/msg.h create mode 100644 components/libc/posix/ipc/sys/sem.h create mode 100644 components/libc/posix/ipc/sys/shm.h (limited to 'components') diff --git a/components/libc/posix/ipc/sys/ipc.h b/components/libc/posix/ipc/sys/ipc.h new file mode 100644 index 0000000000..03b5458d51 --- /dev/null +++ b/components/libc/posix/ipc/sys/ipc.h @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-12-07 Meco Man First version + */ +#ifndef __SYS_IPC_H__ +#define __SYS_IPC_H__ + + + +#endif diff --git a/components/libc/posix/ipc/sys/msg.h b/components/libc/posix/ipc/sys/msg.h new file mode 100644 index 0000000000..c030c9e8cb --- /dev/null +++ b/components/libc/posix/ipc/sys/msg.h @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-12-07 Meco Man First version + */ +#ifndef __SYS_MSG_H__ +#define __SYS_MSG_H__ + + + +#endif diff --git a/components/libc/posix/ipc/sys/sem.h b/components/libc/posix/ipc/sys/sem.h new file mode 100644 index 0000000000..b849bd687d --- /dev/null +++ b/components/libc/posix/ipc/sys/sem.h @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-12-07 Meco Man First version + */ +#ifndef __SYS_SEM_H__ +#define __SYS_SEM_H__ + + + +#endif diff --git a/components/libc/posix/ipc/sys/shm.h b/components/libc/posix/ipc/sys/shm.h new file mode 100644 index 0000000000..762d68891a --- /dev/null +++ b/components/libc/posix/ipc/sys/shm.h @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-12-07 Meco Man First version + */ +#ifndef __SYS_SHM_H__ +#define __SYS_SHM_H__ + + + +#endif -- cgit v1.3.1 From 15435b68507e0a5b6b90341d7c53945487aaf0de Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Tue, 7 Dec 2021 15:47:00 -0500 Subject: [posix][mmap] create the mman folder --- components/libc/compilers/common/sys/ioctl.h | 4 +- components/libc/compilers/common/sys/mman.h | 60 ------------------------- components/libc/posix/Kconfig | 2 +- components/libc/posix/io/SConscript | 4 -- components/libc/posix/io/mman/SConscript | 11 +++++ components/libc/posix/io/mman/mman.c | 67 ++++++++++++++++++++++++++++ components/libc/posix/io/mman/sys/mman.h | 60 +++++++++++++++++++++++++ components/libc/posix/io/mmap.c | 67 ---------------------------- components/libc/posix/io/poll/poll.c | 2 +- 9 files changed, 142 insertions(+), 135 deletions(-) delete mode 100644 components/libc/compilers/common/sys/mman.h create mode 100644 components/libc/posix/io/mman/SConscript create mode 100644 components/libc/posix/io/mman/mman.c create mode 100644 components/libc/posix/io/mman/sys/mman.h delete mode 100644 components/libc/posix/io/mmap.c (limited to 'components') diff --git a/components/libc/compilers/common/sys/ioctl.h b/components/libc/compilers/common/sys/ioctl.h index 29ed3a8b84..a1642199b1 100644 --- a/components/libc/compilers/common/sys/ioctl.h +++ b/components/libc/compilers/common/sys/ioctl.h @@ -7,8 +7,8 @@ * Date Author Notes * 2020-09-01 Meco Man First Version */ -#ifndef _SYS_IOCTL_H -#define _SYS_IOCTL_H +#ifndef __SYS_IOCTL_H__ +#define __SYS_IOCTL_H__ #include #include diff --git a/components/libc/compilers/common/sys/mman.h b/components/libc/compilers/common/sys/mman.h deleted file mode 100644 index 2da2ddf716..0000000000 --- a/components/libc/compilers/common/sys/mman.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/11/30 Bernard The first version. - */ - -#ifndef _SYS_MMAN_H -#define _SYS_MMAN_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#define MAP_FAILED ((void *) -1) - -#define MAP_SHARED 0x01 -#define MAP_PRIVATE 0x02 -#define MAP_TYPE 0x0f -#define MAP_FIXED 0x10 -#define MAP_ANON 0x20 -#define MAP_ANONYMOUS MAP_ANON -#define MAP_NORESERVE 0x4000 -#define MAP_GROWSDOWN 0x0100 -#define MAP_DENYWRITE 0x0800 -#define MAP_EXECUTABLE 0x1000 -#define MAP_LOCKED 0x2000 -#define MAP_POPULATE 0x8000 -#define MAP_NONBLOCK 0x10000 -#define MAP_STACK 0x20000 -#define MAP_HUGETLB 0x40000 -#define MAP_FILE 0 - -#define PROT_NONE 0 -#define PROT_READ 1 -#define PROT_WRITE 2 -#define PROT_EXEC 4 -#define PROT_GROWSDOWN 0x01000000 -#define PROT_GROWSUP 0x02000000 - -#define MS_ASYNC 1 -#define MS_INVALIDATE 2 -#define MS_SYNC 4 - -#define MCL_CURRENT 1 -#define MCL_FUTURE 2 -#define MCL_ONFAULT 4 - -void *mmap (void *start, size_t len, int prot, int flags, int fd, off_t off); -int munmap (void *start, size_t len); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/components/libc/posix/Kconfig b/components/libc/posix/Kconfig index 52b5fdc948..4534da7118 100644 --- a/components/libc/posix/Kconfig +++ b/components/libc/posix/Kconfig @@ -30,7 +30,7 @@ if RT_USING_POSIX_FS bool "Enable Asynchronous I/O " default n - config RT_USING_POSIX_MMAP + config RT_USING_POSIX_MMAN bool "Enable Memory-Mapped I/O " default n endif diff --git a/components/libc/posix/io/SConscript b/components/libc/posix/io/SConscript index 2a7cd76ded..32654674e8 100644 --- a/components/libc/posix/io/SConscript +++ b/components/libc/posix/io/SConscript @@ -18,10 +18,6 @@ if GetDepend('RT_USING_POSIX_SELECT'): src += ['select.c'] flag = True -if GetDepend('RT_USING_POSIX_MMAP'): - src += ['mmap.c'] - flag = True - if flag == True: group = DefineGroup('POSIX', src, depend = [], CPPPATH = CPPPATH) diff --git a/components/libc/posix/io/mman/SConscript b/components/libc/posix/io/mman/SConscript new file mode 100644 index 0000000000..7dd096c998 --- /dev/null +++ b/components/libc/posix/io/mman/SConscript @@ -0,0 +1,11 @@ +# RT-Thread building script for component + +from building import * + +cwd = GetCurrentDir() +src = ['mman.c'] +CPPPATH = [cwd] + +group = DefineGroup('POSIX', src, depend = ['RT_USING_POSIX_MMAN'], CPPPATH = CPPPATH) + +Return('group') diff --git a/components/libc/posix/io/mman/mman.c b/components/libc/posix/io/mman/mman.c new file mode 100644 index 0000000000..827f2cbb14 --- /dev/null +++ b/components/libc/posix/io/mman/mman.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2017/11/30 Bernard The first version. + */ + +#include +#include + +#include +#include + +#include "sys/mman.h" + +void *mmap(void *addr, size_t length, int prot, int flags, + int fd, off_t offset) +{ + uint8_t *mem; + + if (addr) + { + mem = addr; + } + else mem = (uint8_t *)malloc(length); + + if (mem) + { + off_t cur; + size_t read_bytes; + + cur = lseek(fd, 0, SEEK_SET); + + lseek(fd, offset, SEEK_SET); + read_bytes = read(fd, mem, length); + if (read_bytes != length) + { + if (addr == RT_NULL) + { + /* read failed */ + free(mem); + mem = RT_NULL; + } + } + lseek(fd, cur, SEEK_SET); + + return mem; + } + + errno = ENOMEM; + + return MAP_FAILED; +} + +int munmap(void *addr, size_t length) +{ + if (addr) + { + free(addr); + return 0; + } + + return -1; +} diff --git a/components/libc/posix/io/mman/sys/mman.h b/components/libc/posix/io/mman/sys/mman.h new file mode 100644 index 0000000000..a730f33b6b --- /dev/null +++ b/components/libc/posix/io/mman/sys/mman.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2017/11/30 Bernard The first version. + */ + +#ifndef __SYS_MMAN_H__ +#define __SYS_MMAN_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#define MAP_FAILED ((void *) -1) + +#define MAP_SHARED 0x01 +#define MAP_PRIVATE 0x02 +#define MAP_TYPE 0x0f +#define MAP_FIXED 0x10 +#define MAP_ANON 0x20 +#define MAP_ANONYMOUS MAP_ANON +#define MAP_NORESERVE 0x4000 +#define MAP_GROWSDOWN 0x0100 +#define MAP_DENYWRITE 0x0800 +#define MAP_EXECUTABLE 0x1000 +#define MAP_LOCKED 0x2000 +#define MAP_POPULATE 0x8000 +#define MAP_NONBLOCK 0x10000 +#define MAP_STACK 0x20000 +#define MAP_HUGETLB 0x40000 +#define MAP_FILE 0 + +#define PROT_NONE 0 +#define PROT_READ 1 +#define PROT_WRITE 2 +#define PROT_EXEC 4 +#define PROT_GROWSDOWN 0x01000000 +#define PROT_GROWSUP 0x02000000 + +#define MS_ASYNC 1 +#define MS_INVALIDATE 2 +#define MS_SYNC 4 + +#define MCL_CURRENT 1 +#define MCL_FUTURE 2 +#define MCL_ONFAULT 4 + +void *mmap (void *start, size_t len, int prot, int flags, int fd, off_t off); +int munmap (void *start, size_t len); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/components/libc/posix/io/mmap.c b/components/libc/posix/io/mmap.c deleted file mode 100644 index e68b82e640..0000000000 --- a/components/libc/posix/io/mmap.c +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/11/30 Bernard The first version. - */ - -#include -#include - -#include -#include - -#include - -void *mmap(void *addr, size_t length, int prot, int flags, - int fd, off_t offset) -{ - uint8_t *mem; - - if (addr) - { - mem = addr; - } - else mem = (uint8_t *)malloc(length); - - if (mem) - { - off_t cur; - size_t read_bytes; - - cur = lseek(fd, 0, SEEK_SET); - - lseek(fd, offset, SEEK_SET); - read_bytes = read(fd, mem, length); - if (read_bytes != length) - { - if (addr == RT_NULL) - { - /* read failed */ - free(mem); - mem = RT_NULL; - } - } - lseek(fd, cur, SEEK_SET); - - return mem; - } - - errno = ENOMEM; - - return MAP_FAILED; -} - -int munmap(void *addr, size_t length) -{ - if (addr) - { - free(addr); - return 0; - } - - return -1; -} diff --git a/components/libc/posix/io/poll/poll.c b/components/libc/posix/io/poll/poll.c index 0c773b306c..a1500ba109 100644 --- a/components/libc/posix/io/poll/poll.c +++ b/components/libc/posix/io/poll/poll.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include "poll.h" struct rt_poll_node; -- cgit v1.3.1 From 153ab5c2382b90dab1b2f2cbbb99e82f235208f0 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Wed, 8 Dec 2021 09:13:57 -0500 Subject: update poll sconscript --- components/libc/posix/io/poll/SConscript | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'components') diff --git a/components/libc/posix/io/poll/SConscript b/components/libc/posix/io/poll/SConscript index 47f1722ed3..7d82ade035 100644 --- a/components/libc/posix/io/poll/SConscript +++ b/components/libc/posix/io/poll/SConscript @@ -3,9 +3,12 @@ from building import * cwd = GetCurrentDir() -src = ['poll.c'] +src = [] CPPPATH = [cwd] -group = DefineGroup('POSIX', src, depend = ['RT_USING_POSIX_POLL'], CPPPATH = CPPPATH) +if GetDepend('RT_USING_POSIX_POLL'): + src += ['poll.c'] + +group = DefineGroup('POSIX', src, depend = [], CPPPATH = CPPPATH) Return('group') -- cgit v1.3.1 From 7455e2487bd420c06ba7198228b0dcca297f83cc Mon Sep 17 00:00:00 2001 From: 张世争 Date: Sun, 5 Dec 2021 09:07:23 +0800 Subject: [components] PM框架更新 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/stm32/stm32l475-atk-pandora/board/pm_cfg.h | 25 ++ components/drivers/Kconfig | 36 +++ components/drivers/include/drivers/lptimer.h | 38 +++ components/drivers/include/drivers/pm.h | 38 ++- components/drivers/pm/SConscript | 1 + components/drivers/pm/lptimer.c | 176 ++++++++++ components/drivers/pm/pm.c | 427 +++++++++++++++++++++++-- 7 files changed, 700 insertions(+), 41 deletions(-) create mode 100644 bsp/stm32/stm32l475-atk-pandora/board/pm_cfg.h create mode 100644 components/drivers/include/drivers/lptimer.h create mode 100644 components/drivers/pm/lptimer.c (limited to 'components') diff --git a/bsp/stm32/stm32l475-atk-pandora/board/pm_cfg.h b/bsp/stm32/stm32l475-atk-pandora/board/pm_cfg.h new file mode 100644 index 0000000000..bb4c3cf22d --- /dev/null +++ b/bsp/stm32/stm32l475-atk-pandora/board/pm_cfg.h @@ -0,0 +1,25 @@ +#ifndef __PM_CFG_H__ +#define __PM_CFG_H__ + +enum pm_module_id +{ + PM_NONE_ID = 0, + PM_POWER_ID, + PM_BOARD_ID, + PM_LCD_ID, + PM_KEY_ID, + PM_TP_ID, + PM_OTA_ID, + PM_SPI_ID, + PM_I2C_ID, + PM_ADC_ID, + PM_RTC_ID, + PM_GPIO_ID, + PM_UART_ID, + PM_SENSOR_ID, + PM_ALARM_ID, + PM_BLE_ID, + PM_MODULE_MAX_ID, /* enum must! */ +}; + +#endif diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 8d340da3f5..d197bd246e 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -145,6 +145,42 @@ config RT_USING_PM bool "Using Power Management device drivers" default n + if RT_USING_PM + config PM_TICKLESS_THRESHOLD_TIME + int "PM tickless threashold time" + default 2 + + config PM_USING_CUSTOM_CONFIG + bool "PM using custom pm config" + default n + + config PM_ENABLE_DEBUG + bool "PM Enable Debug" + default n + + config PM_ENABLE_SUSPEND_SLEEP_MODE + bool "PM Device suspend change sleep mode" + default n + + config PM_ENABLE_THRESHOLD_SLEEP_MODE + bool "PM using threshold time change sleep mode" + default n + + if PM_ENABLE_THRESHOLD_SLEEP_MODE + config PM_LIGHT_THRESHOLD_TIME + int "PM light mode threashold time" + default 5 + + config PM_DEEP_THRESHOLD_TIME + int "PM deep mode threashold time" + default 20 + + config PM_STANDBY_THRESHOLD_TIME + int "PM standby mode threashold time" + default 100 + endif + endif + config RT_USING_RTC bool "Using RTC device drivers" default n diff --git a/components/drivers/include/drivers/lptimer.h b/components/drivers/include/drivers/lptimer.h new file mode 100644 index 0000000000..e59ce61d22 --- /dev/null +++ b/components/drivers/include/drivers/lptimer.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-10-11 zhangsz the first version + */ + +#ifndef __LPTIMER_H__ +#define __LPTIMER_H__ + +#include + +struct rt_lptimer +{ + struct rt_timer timer; + rt_list_t list; +}; +typedef struct rt_lptimer *rt_lptimer_t; + +void rt_lptimer_init(rt_lptimer_t timer, + const char *name, + void (*timeout)(void *parameter), + void *parameter, + rt_tick_t time, + rt_uint8_t flag); + +rt_err_t rt_lptimer_detach(rt_lptimer_t timer); +rt_err_t rt_lptimer_start(rt_lptimer_t timer); +rt_err_t rt_lptimer_stop(rt_lptimer_t timer); + +rt_err_t rt_lptimer_control(rt_lptimer_t timer, int cmd, void *arg); + +rt_tick_t rt_lptimer_next_timeout_tick(void); + +#endif diff --git a/components/drivers/include/drivers/pm.h b/components/drivers/include/drivers/pm.h index d38c0a4e2a..2deee83692 100644 --- a/components/drivers/include/drivers/pm.h +++ b/components/drivers/include/drivers/pm.h @@ -17,8 +17,7 @@ #include #include - -#ifndef PM_HAS_CUSTOM_CONFIG +#include /* All modes used for rt_pm_request() and rt_pm_release() */ enum @@ -48,10 +47,6 @@ enum RT_PM_FREQUENCY_PENDING = 0x01, }; -#define RT_PM_DEFAULT_SLEEP_MODE PM_SLEEP_MODE_NONE -#define RT_PM_DEFAULT_DEEPSLEEP_MODE PM_SLEEP_MODE_DEEP -#define RT_PM_DEFAULT_RUN_MODE PM_RUN_MODE_NORMAL_SPEED - /* The name of all modes used in the msh command "pm_dump" */ #define PM_SLEEP_MODE_NAMES \ { \ @@ -71,6 +66,7 @@ enum "Low Mode", \ } +#ifndef PM_USING_CUSTOM_CONFIG /** * Modules used for * pm_module_request(PM_BOARD_ID, PM_SLEEP_MODE_IDLE) @@ -98,11 +94,23 @@ enum pm_module_id { PM_MODULE_MAX_ID, /* enum must! */ }; -#else /* PM_HAS_CUSTOM_CONFIG */ +#else #include -#endif /* PM_HAS_CUSTOM_CONFIG */ +#endif /* PM_USING_CUSTOM_CONFIG */ + +#ifndef RT_PM_DEFAULT_SLEEP_MODE +#define RT_PM_DEFAULT_SLEEP_MODE PM_SLEEP_MODE_NONE +#endif + +#ifndef RT_PM_DEFAULT_DEEPSLEEP_MODE +#define RT_PM_DEFAULT_DEEPSLEEP_MODE PM_SLEEP_MODE_DEEP +#endif + +#ifndef RT_PM_DEFAULT_RUN_MODE +#define RT_PM_DEFAULT_RUN_MODE PM_RUN_MODE_NORMAL_SPEED +#endif /** * device control flag to request or release power @@ -160,6 +168,9 @@ struct rt_pm /* modules request status*/ struct rt_pm_module module_status[PM_MODULE_MAX_ID]; + /* sleep request table */ + rt_uint32_t sleep_status[PM_SLEEP_MODE_MAX - 1][(PM_MODULE_MAX_ID + 31) / 32]; + /* the list of device, which has PM feature */ rt_uint8_t device_pm_number; struct rt_device_pm *device_pm; @@ -203,5 +214,16 @@ void rt_pm_module_release_all(uint8_t module_id, rt_uint8_t sleep_mode); void rt_pm_module_delay_sleep(rt_uint8_t module_id, rt_tick_t timeout); rt_uint32_t rt_pm_module_get_status(void); rt_uint8_t rt_pm_get_sleep_mode(void); +struct rt_pm *rt_pm_get_handle(void); + +/* sleep : request or release */ +void rt_pm_sleep_request(rt_uint16_t module_id, rt_uint8_t mode); +void rt_pm_sleep_release(rt_uint16_t module_id, rt_uint8_t mode); +void rt_pm_sleep_none_request(rt_uint16_t module_id); +void rt_pm_sleep_none_release(rt_uint16_t module_id); +void rt_pm_sleep_idle_request(rt_uint16_t module_id); +void rt_pm_sleep_idle_release(rt_uint16_t module_id); +void rt_pm_sleep_light_request(rt_uint16_t module_id); +void rt_pm_sleep_light_release(rt_uint16_t module_id); #endif /* __PM_H__ */ diff --git a/components/drivers/pm/SConscript b/components/drivers/pm/SConscript index a94fc11f57..fd23e8293c 100644 --- a/components/drivers/pm/SConscript +++ b/components/drivers/pm/SConscript @@ -7,6 +7,7 @@ group = [] if GetDepend(['RT_USING_PM']): src = src + ['pm.c'] + src = src + ['lptimer.c'] if len(src): group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH) diff --git a/components/drivers/pm/lptimer.c b/components/drivers/pm/lptimer.c new file mode 100644 index 0000000000..144270cf7f --- /dev/null +++ b/components/drivers/pm/lptimer.c @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-10-11 zhangsz the first version + */ + +#include +#include +#include + +static rt_list_t rt_soft_lptimer_list = RT_LIST_OBJECT_INIT(rt_soft_lptimer_list); + +/* lptimer init */ +void rt_lptimer_init(rt_lptimer_t timer, + const char *name, + void (*timeout)(void *parameter), + void *parameter, + rt_tick_t time, + rt_uint8_t flag) +{ + rt_timer_init(&timer->timer, name, timeout, parameter, time, flag); + rt_list_init(&timer->list); +} + +/* lptimer detach */ +rt_err_t rt_lptimer_detach(rt_lptimer_t timer) +{ + rt_base_t level; + rt_err_t ret = RT_EOK; + RT_ASSERT(timer != RT_NULL); + + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + ret = rt_timer_detach(&timer->timer); + rt_list_remove(&timer->list); + + /* enable interrupt */ + rt_hw_interrupt_enable(level); + return ret; +} + +/* lptimer start */ +rt_err_t rt_lptimer_start(rt_lptimer_t timer) +{ + rt_base_t level; + + RT_ASSERT(timer != RT_NULL); + + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + rt_list_remove(&timer->list); /* remove first */ + if (rt_timer_start(&timer->timer) == RT_EOK) + { + /* insert to lptimer list */ + rt_list_insert_after(&rt_soft_lptimer_list, &(timer->list)); + } + else + { + /* enable interrupt */ + rt_hw_interrupt_enable(level); + return -RT_ERROR; + } + + /* enable interrupt */ + rt_hw_interrupt_enable(level); + return RT_EOK; +} + +/* lptimer stop */ +rt_err_t rt_lptimer_stop(rt_lptimer_t timer) +{ + rt_base_t level; + RT_ASSERT(timer != RT_NULL); + + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + rt_list_remove(&timer->list); + if (rt_timer_stop(&timer->timer) == RT_EOK) + { + /* enable interrupt */ + rt_hw_interrupt_enable(level); + return RT_EOK; + } + else + { + /* enable interrupt */ + rt_hw_interrupt_enable(level); + return -RT_ERROR; + } +} + +rt_err_t rt_lptimer_control(rt_lptimer_t timer, int cmd, void *arg) +{ + RT_ASSERT(timer != RT_NULL); + + return rt_timer_control(&timer->timer, cmd, arg); +} + +/* get the next soft lptimer timeout */ +rt_tick_t rt_lptimer_next_timeout_tick(void) +{ + struct rt_lptimer *timer; + rt_base_t level; + rt_tick_t timeout_tick = RT_TICK_MAX; + struct rt_list_node *node = RT_NULL; + rt_tick_t temp_tick = 0; + rt_tick_t min_tick = RT_TICK_MAX; + rt_tick_t cur_tick = rt_tick_get(); + + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + if (!rt_list_isempty(&rt_soft_lptimer_list)) + { + /* find the first active timer's timeout */ + rt_list_for_each(node, &rt_soft_lptimer_list) + { + timer = rt_list_entry(node, struct rt_lptimer, list); + if (timer->timer.parent.flag & RT_TIMER_FLAG_ACTIVATED) + { + temp_tick = timer->timer.timeout_tick - cur_tick; + + /* find the least timeout_tick */ + if (min_tick > temp_tick) + { + min_tick = temp_tick; + timeout_tick = timer->timer.timeout_tick; + } + } + } + } + + /* enable interrupt */ + rt_hw_interrupt_enable(level); + + return timeout_tick; +} + +void lptimer_dump(void) +{ + struct rt_lptimer *timer; + rt_base_t level; + struct rt_list_node *node = RT_NULL; + + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + rt_kprintf("| lptimer | periodic | timeout | flag |\n"); + rt_kprintf("+---------------+------------+------------+-------------+\n"); + + if (!rt_list_isempty(&rt_soft_lptimer_list)) + { + rt_list_for_each(node, &rt_soft_lptimer_list) + { + timer = rt_list_entry(node, struct rt_lptimer, list); + rt_kprintf("| %-13s | 0x%08x | 0x%08x |", + timer->timer.parent.name, timer->timer.init_tick, + timer->timer.timeout_tick); + if (timer->timer.parent.flag & RT_TIMER_FLAG_ACTIVATED) + rt_kprintf(" activated |\n"); + else + rt_kprintf(" deactivated |\n"); + } + } + + /* enable interrupt */ + rt_hw_interrupt_enable(level); + rt_kprintf("+---------------+------------+------------+-------------+\n"); +} + +MSH_CMD_EXPORT(lptimer_dump, soft lptimer dump); diff --git a/components/drivers/pm/pm.c b/components/drivers/pm/pm.c index cf488a9ce1..b3ebbeb078 100644 --- a/components/drivers/pm/pm.c +++ b/components/drivers/pm/pm.c @@ -15,9 +15,44 @@ #include #include #include +#include #ifdef RT_USING_PM +/* tickless threshold time */ +#ifndef PM_TICKLESS_THRESHOLD_TIME +#define PM_TICKLESS_THRESHOLD_TIME 2 +#endif + +/* tickless threshold : sleep mode */ +#ifndef PM_TICKLESS_THRESHOLD_MODE +#define PM_TICKLESS_THRESHOLD_MODE PM_SLEEP_MODE_IDLE +#endif + +/* busy : sleep mode */ +#ifndef PM_BUSY_SLEEP_MODE +#define PM_BUSY_SLEEP_MODE PM_SLEEP_MODE_IDLE +#endif + +/* suspend : suspend sleep mode */ +#ifndef PM_SUSPEND_SLEEP_MODE +#define PM_SUSPEND_SLEEP_MODE PM_SLEEP_MODE_IDLE +#endif + +#ifdef PM_ENABLE_THRESHOLD_SLEEP_MODE +#ifndef PM_LIGHT_THRESHOLD_TIME +#define PM_LIGHT_THRESHOLD_TIME 5 +#endif + +#ifndef PM_DEEP_THRESHOLD_TIME +#define PM_DEEP_THRESHOLD_TIME 20 +#endif + +#ifndef PM_STANDBY_THRESHOLD_TIME +#define PM_STANDBY_THRESHOLD_TIME 100 +#endif +#endif + static struct rt_pm _pm; /* default mode : system power on */ @@ -29,8 +64,6 @@ static rt_uint8_t _pm_default_deepsleep = RT_PM_DEFAULT_DEEPSLEEP_MODE; static struct rt_pm_notify _pm_notify; static rt_uint8_t _pm_init_flag = 0; -#define RT_PM_TICKLESS_THRESH (2) - RT_WEAK rt_uint32_t rt_pm_enter_critical(rt_uint8_t sleep_mode) { return rt_hw_interrupt_disable(); @@ -41,6 +74,48 @@ RT_WEAK void rt_pm_exit_critical(rt_uint32_t ctx, rt_uint8_t sleep_mode) rt_hw_interrupt_enable(ctx); } +/* lptimer start */ +static void pm_lptimer_start(struct rt_pm *pm, uint32_t timeout) +{ + if (_pm.ops == RT_NULL) + return; + + if (_pm.ops->timer_start != RT_NULL) + _pm.ops->timer_start(pm, timeout); +} + +/* lptimer stop */ +static void pm_lptimer_stop(struct rt_pm *pm) +{ + if (_pm.ops == RT_NULL) + return; + + if (_pm.ops->timer_stop != RT_NULL) + _pm.ops->timer_stop(pm); +} + +/* lptimer get timeout tick */ +static rt_tick_t pm_lptimer_get_timeout(struct rt_pm *pm) +{ + if (_pm.ops == RT_NULL) + return RT_TICK_MAX; + + if (_pm.ops->timer_get_tick != RT_NULL) + return _pm.ops->timer_get_tick(pm); + + return RT_TICK_MAX; +} + +/* enter sleep mode */ +static void pm_sleep(struct rt_pm *pm, uint8_t sleep_mode) +{ + if (_pm.ops == RT_NULL) + return; + + if (_pm.ops->sleep != RT_NULL) + _pm.ops->sleep(pm, sleep_mode); +} + /** * This function will suspend all registered devices */ @@ -111,6 +186,30 @@ static void _pm_frequency_scaling(struct rt_pm *pm) } } +/** + * judge sleep mode from sleep request + * + * @param none + * + * @return sleep mode + */ +static rt_uint8_t _judge_sleep_mode(void) +{ + rt_uint16_t index; + rt_uint16_t len; + + for (index = 0; index < PM_SLEEP_MODE_MAX -1; index++) + { + for (len = 0; len < ((PM_MODULE_MAX_ID + 31) / 32); len++) + { + if (_pm.sleep_status[index][len] != 0x00) + return index; + } + } + + return PM_SLEEP_MODE_MAX; /* default sleep mode */ +} + /** * This function selects the sleep mode according to the rt_pm_request/rt_pm_release count. */ @@ -120,6 +219,7 @@ static rt_uint8_t _pm_select_sleep_mode(struct rt_pm *pm) rt_uint8_t mode; mode = _pm_default_deepsleep; + rt_uint8_t request_mode = _judge_sleep_mode(); for (index = PM_SLEEP_MODE_NONE; index < PM_SLEEP_MODE_MAX; index ++) { if (pm->modes[index]) @@ -128,7 +228,10 @@ static rt_uint8_t _pm_select_sleep_mode(struct rt_pm *pm) break; } } - pm->sleep_mode = mode; + + /* select the high power mode */ + if (request_mode < mode) + mode = request_mode; return mode; } @@ -185,6 +288,72 @@ static rt_bool_t _pm_device_check_idle(void) return RT_TRUE; } +RT_WEAK rt_tick_t pm_timer_next_timeout_tick(rt_uint8_t mode) +{ + switch (mode) + { + case PM_SLEEP_MODE_LIGHT: + return rt_timer_next_timeout_tick(); + case PM_SLEEP_MODE_DEEP: + case PM_SLEEP_MODE_STANDBY: + return rt_lptimer_next_timeout_tick(); + } + + return RT_TICK_MAX; +} + +/** + * This function will judge sleep mode from threshold timeout. + * + * @param cur_mode the current pm sleep mode + * @param timeout_tick the threshold timeout + * + * @return none + */ +RT_WEAK rt_uint8_t pm_get_sleep_threshold_mode(rt_uint8_t cur_mode, rt_tick_t timeout_tick) +{ + rt_uint8_t sleep_mode = cur_mode; + + if (_pm_init_flag == 0) + return sleep_mode; + + if (cur_mode >= PM_SLEEP_MODE_MAX) + return sleep_mode; + +#ifdef PM_ENABLE_THRESHOLD_SLEEP_MODE + switch (cur_mode) + { + case PM_SLEEP_MODE_NONE: + case PM_SLEEP_MODE_IDLE: + break; + case PM_SLEEP_MODE_LIGHT: + if (timeout_tick < PM_LIGHT_THRESHOLD_TIME) + sleep_mode = PM_SLEEP_MODE_IDLE; + break; + case PM_SLEEP_MODE_DEEP: + if (timeout_tick < PM_LIGHT_THRESHOLD_TIME) + sleep_mode = PM_SLEEP_MODE_IDLE; + else if (timeout_tick < PM_DEEP_THRESHOLD_TIME) + sleep_mode = PM_SLEEP_MODE_LIGHT; + break; + case PM_SLEEP_MODE_STANDBY: + if (timeout_tick < PM_LIGHT_THRESHOLD_TIME) + sleep_mode = PM_SLEEP_MODE_IDLE; + else if (timeout_tick < PM_DEEP_THRESHOLD_TIME) + sleep_mode = PM_SLEEP_MODE_LIGHT; + else if (timeout_tick < PM_STANDBY_THRESHOLD_TIME) + sleep_mode = PM_SLEEP_MODE_DEEP; + } +#else + if (timeout_tick < PM_TICKLESS_THRESHOLD_TIME) + { + cur_mode = PM_SLEEP_MODE_IDLE; + } +#endif + + return cur_mode; +} + /** * This function changes the power sleep mode base on the result of selection */ @@ -192,16 +361,21 @@ static void _pm_change_sleep_mode(struct rt_pm *pm) { rt_tick_t timeout_tick, delta_tick; rt_base_t level; - int ret = RT_EOK; + uint8_t sleep_mode = PM_SLEEP_MODE_DEEP; level = rt_pm_enter_critical(pm->sleep_mode); - /* module busy request */ + /* judge sleep mode from module request */ + pm->sleep_mode = _pm_select_sleep_mode(pm); + + /* module busy request check */ if (_pm_device_check_idle() == RT_FALSE) { - pm->ops->sleep(pm, PM_SLEEP_MODE_NONE); - rt_pm_exit_critical(level, pm->sleep_mode); - return; + sleep_mode = PM_BUSY_SLEEP_MODE; + if (sleep_mode < pm->sleep_mode) + { + pm->sleep_mode = sleep_mode; /* judge the highest sleep mode */ + } } if (_pm.sleep_mode == PM_SLEEP_MODE_NONE) @@ -216,50 +390,54 @@ static void _pm_change_sleep_mode(struct rt_pm *pm) _pm_notify.notify(RT_PM_ENTER_SLEEP, pm->sleep_mode, _pm_notify.data); /* Suspend all peripheral device */ - ret = _pm_device_suspend(pm->sleep_mode); +#ifdef PM_ENABLE_SUSPEND_SLEEP_MODE + int ret = _pm_device_suspend(pm->sleep_mode); if (ret != RT_EOK) { _pm_device_resume(pm->sleep_mode); if (_pm_notify.notify) _pm_notify.notify(RT_PM_EXIT_SLEEP, pm->sleep_mode, _pm_notify.data); + if (pm->sleep_mode > PM_SUSPEND_SLEEP_MODE) + { + pm->sleep_mode = PM_SUSPEND_SLEEP_MODE; + } + pm->ops->sleep(pm, pm->sleep_mode); /* suspend failed */ rt_pm_exit_critical(level, pm->sleep_mode); - return; } - +#else + _pm_device_suspend(pm->sleep_mode); +#endif /* Tickless*/ if (pm->timer_mask & (0x01 << pm->sleep_mode)) { - timeout_tick = rt_timer_next_timeout_tick(); - if (timeout_tick == RT_TICK_MAX) - { - if (pm->ops->timer_start) - { - pm->ops->timer_start(pm, RT_TICK_MAX); - } - } - else + timeout_tick = pm_timer_next_timeout_tick(pm->sleep_mode); + timeout_tick = timeout_tick - rt_tick_get(); + + /* Judge sleep_mode from threshold time */ + pm->sleep_mode = pm_get_sleep_threshold_mode(pm->sleep_mode, timeout_tick); + + if (pm->timer_mask & (0x01 << pm->sleep_mode)) { - timeout_tick = timeout_tick - rt_tick_get(); - if (timeout_tick < RT_PM_TICKLESS_THRESH) + if (timeout_tick == RT_TICK_MAX) { - pm->sleep_mode = PM_SLEEP_MODE_IDLE; + pm_lptimer_start(pm, RT_TICK_MAX); } else { - pm->ops->timer_start(pm, timeout_tick); + pm_lptimer_start(pm, timeout_tick); } } } /* enter lower power state */ - pm->ops->sleep(pm, pm->sleep_mode); + pm_sleep(pm, pm->sleep_mode); /* wake up from lower power state*/ if (pm->timer_mask & (0x01 << pm->sleep_mode)) { - delta_tick = pm->ops->timer_get_tick(pm); - pm->ops->timer_stop(pm); + delta_tick = pm_lptimer_get_timeout(pm); + pm_lptimer_stop(pm); if (delta_tick) { rt_tick_set(rt_tick_get() + delta_tick); @@ -320,7 +498,6 @@ void rt_pm_request(rt_uint8_t mode) pm = &_pm; if (pm->modes[mode] < 255) pm->modes[mode] ++; - _pm_select_sleep_mode(pm); rt_hw_interrupt_enable(level); } @@ -346,7 +523,6 @@ void rt_pm_release(rt_uint8_t mode) pm = &_pm; if (pm->modes[mode] > 0) pm->modes[mode] --; - _pm_select_sleep_mode(pm); rt_hw_interrupt_enable(level); } @@ -371,7 +547,6 @@ void rt_pm_release_all(rt_uint8_t mode) level = rt_hw_interrupt_disable(); pm = &_pm; pm->modes[mode] = 0; - _pm_select_sleep_mode(pm); rt_hw_interrupt_enable(level); } @@ -401,7 +576,6 @@ void rt_pm_module_request(uint8_t module_id, rt_uint8_t mode) pm->module_status[module_id].req_status = 0x01; if (pm->modes[mode] < 255) pm->modes[mode] ++; - _pm_select_sleep_mode(pm); rt_hw_interrupt_enable(level); } @@ -433,7 +607,6 @@ void rt_pm_module_release(uint8_t module_id, rt_uint8_t mode) pm->modes[mode] --; if (pm->modes[mode] == 0) pm->module_status[module_id].req_status = 0x00; - _pm_select_sleep_mode(pm); rt_hw_interrupt_enable(level); } @@ -460,10 +633,135 @@ void rt_pm_module_release_all(uint8_t module_id, rt_uint8_t mode) pm = &_pm; pm->modes[mode] = 0; pm->module_status[module_id].req_status = 0x00; - _pm_select_sleep_mode(pm); rt_hw_interrupt_enable(level); } +/** + * This function will let current module work with specified sleep mode. + * + * @param module_id the pm module id + * @param mode the pm sleep mode + * + * @return none + */ +void rt_pm_sleep_request(rt_uint16_t module_id, rt_uint8_t mode) +{ + rt_uint32_t level; + + if (module_id >= PM_MODULE_MAX_ID) + { + return; + } + + if (mode >= (PM_SLEEP_MODE_MAX - 1)) + { + return; + } + + level = rt_hw_interrupt_disable(); + _pm.sleep_status[mode][module_id / 32] |= 1 << (module_id % 32); + rt_hw_interrupt_enable(level); +} + +/** + * This function will let current module work with PM_SLEEP_MODE_NONE mode. + * + * @param module_id the pm module id + * + * @return NULL + */ +void rt_pm_sleep_none_request(rt_uint16_t module_id) +{ + rt_pm_sleep_request(module_id, PM_SLEEP_MODE_NONE); +} + +/** + * This function will let current module work with PM_SLEEP_MODE_IDLE mode. + * + * @param module_id the pm module id + * + * @return NULL + */ +void rt_pm_sleep_idle_request(rt_uint16_t module_id) +{ + rt_pm_sleep_request(module_id, PM_SLEEP_MODE_IDLE); +} + +/** + * This function will let current module work with PM_SLEEP_MODE_LIGHT mode. + * + * @param module_id the pm module id + * + * @return NULL + */ +void rt_pm_sleep_light_request(rt_uint16_t module_id) +{ + rt_pm_sleep_request(module_id, PM_SLEEP_MODE_LIGHT); +} + +/** + * When current module don't work, release requested sleep mode. + * + * @param module_id the pm module id + * @param mode the pm sleep mode + * + * @return NULL + */ +void rt_pm_sleep_release(rt_uint16_t module_id, rt_uint8_t mode) +{ + rt_uint32_t level; + + if (module_id >= PM_MODULE_MAX_ID) + { + return; + } + + if (mode >= (PM_SLEEP_MODE_MAX - 1)) + { + return; + } + + level = rt_hw_interrupt_disable(); + _pm.sleep_status[mode][module_id / 32] &= ~(1 << (module_id % 32)); + rt_hw_interrupt_enable(level); +} + +/** + * The specified module release the requested PM_SLEEP_MODE_NONE mode + * + * @param module_id the pm module id + * + * @return none + */ +void rt_pm_sleep_none_release(rt_uint16_t module_id) +{ + rt_pm_sleep_release(module_id, PM_SLEEP_MODE_NONE); +} + +/** + * The specified module release the requested PM_SLEEP_MODE_IDLE mode + * + * @param module_id the pm module id + * + * @return none + */ +void rt_pm_sleep_idle_release(rt_uint16_t module_id) +{ + rt_pm_sleep_release(module_id, PM_SLEEP_MODE_IDLE); +} + +/** + * The specified module release the requested PM_SLEEP_MODE_LIGHT mode + * + * @param module_id the pm module id + * + * @return none + */ +void rt_pm_sleep_light_release(rt_uint16_t module_id) +{ + rt_pm_sleep_release(module_id, PM_SLEEP_MODE_LIGHT); +} + /** * Register a device with PM feature * @@ -853,6 +1151,69 @@ rt_uint8_t rt_pm_get_sleep_mode(void) return pm->sleep_mode; } +/* get pm entity pointer */ +struct rt_pm *rt_pm_get_handle(void) +{ + return &_pm; +} + +#ifdef PM_ENABLE_DEBUG +/** + * print current module sleep request list + * + * @param none + * + * @return none + */ +void pm_sleep_dump(void) +{ + uint8_t index; + uint16_t len; + + rt_kprintf("+-------------+--------------+\n"); + rt_kprintf("| Sleep Mode | Request List |\n"); + rt_kprintf("+-------------+--------------+\n"); + for (index = 0; index < PM_SLEEP_MODE_MAX -1; index++) + { + for (len = 0; len < ((PM_MODULE_MAX_ID + 31) / 32); len++) + { + rt_kprintf("| Mode[%d] : %d | 0x%08x |\n", index, len, + _pm.sleep_status[index][len]); + } + } + rt_kprintf("+-------------+--------------+\n"); +} +MSH_CMD_EXPORT(pm_sleep_dump, dump pm request list); + +static void pm_sleep_request(int argc, char **argv) +{ + int module = 0; + int mode = 0; + + if (argc >= 3) + { + module = atoi(argv[1]); + mode = atoi(argv[2]); + rt_pm_sleep_request(module, mode); + } +} +MSH_CMD_EXPORT(pm_sleep_request, pm_sleep_request module sleep_mode); + +static void pm_sleep_release(int argc, char **argv) +{ + int module = 0; + int mode = 0; + + if (argc >= 3) + { + module = atoi(argv[1]); + mode = atoi(argv[2]); + rt_pm_sleep_release(module, mode); + } +} +MSH_CMD_EXPORT(pm_sleep_release, pm_sleep_release module sleep_mode); +#endif + static void rt_pm_dump_status(void) { rt_uint32_t index; -- cgit v1.3.1