1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys/shell/commands: pm: add set mode block mode unblock mode

This commit is contained in:
Thomas Stilwell 2019-07-09 18:38:37 -07:00 committed by Alexandre Abadie
parent aa97e7b49a
commit 325ab426d4
No known key found for this signature in database
GPG Key ID: 1C919A403CAE1405
6 changed files with 171 additions and 158 deletions

View File

@ -49,6 +49,14 @@ extern "C" {
#define PROVIDES_PM_SET_LOWEST
#endif
/**
* @brief Power Management mode blocker typedef
*/
typedef union {
uint32_t val_u32; /**< power mode blockers u32 */
uint8_t val_u8[PM_NUM_MODES]; /**< power mode blockers u8 */
} pm_blocker_t;
/**
* @brief Block a power mode
*

View File

@ -33,14 +33,6 @@
#define PM_BLOCKER_INITIAL 0x01010101 /* block all by default */
#endif
/**
* @brief Power Management mode typedef
*/
typedef union {
uint32_t val_u32;
uint8_t val_u8[PM_NUM_MODES];
} pm_blocker_t;
/**
* @brief Global variable for keeping track of blocked modes
*/

View File

@ -8,7 +8,7 @@ endif
ifneq (,$(filter mci,$(USEMODULE)))
SRC += sc_disk.c
endif
ifneq (,$(filter pm_layered,$(USEMODULE)))
ifneq (,$(filter periph_pm,$(USEMODULE)))
SRC += sc_pm.c
endif
ifneq (,$(filter ps,$(USEMODULE)))

View File

@ -13,49 +13,182 @@
* @file
* @brief Shell command to interact with the PM subsystem
*
* @author Bas Stottelaar <basstottelaar@gmail.com>
* @author Vincent Dupont <vincent@otakeys.com>
* @author Thomas Stilwell <stilwellt@openlabs.co>
*
* @}
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "periph/pm.h"
#ifdef MODULE_PM_LAYERED
#include "pm_layered.h"
/* TODO deduplicate this definition with the one in sys/pm_layered/pm.c */
typedef union {
uint32_t val_u32;
uint8_t val_u8[PM_NUM_MODES];
} pm_blocker_t;
extern volatile pm_blocker_t pm_blocker; /* sys/pm_layered/pm.c */
#endif /* MODULE_PM_LAYERED */
static void _print_usage(void) {
printf("usage: pm show: display current blockers for each power mode\n");
puts("Usage:");
#ifdef MODULE_PM_LAYERED
puts("\tpm show: display current blockers for each power mode");
puts("\tpm set <mode>: manually set power mode (lasts until WFI returns)");
puts("\tpm block <mode>: manually block power mode");
puts("\tpm unblock <mode>: manually unblock power mode");
#endif /* MODULE_PM_LAYERED */
puts("\tpm off: call pm_off()");
}
int _pm(int argc, char **argv)
#ifdef MODULE_PM_LAYERED
static int check_mode(int argc, char **argv)
{
if (argc != 2) {
_print_usage();
if (argc != 3) {
printf("Usage: %s %s <power mode>\n", argv[0], argv[1]);
return -1;
}
return 0;
}
static int parse_mode(char *argv)
{
uint8_t mode = atoi(argv);
if (mode >= PM_NUM_MODES) {
printf("Error: power mode not in range 0 - %d.\n", PM_NUM_MODES - 1);
return -1;
}
return mode;
}
static int cmd_block(char *arg)
{
int mode = parse_mode(arg);
if (mode < 0) {
return 1;
}
if (strcmp(argv[1], "show")) {
_print_usage();
return 2;
printf("Blocking power mode %d.\n", mode);
fflush(stdout);
pm_block(mode);
return 0;
}
static int cmd_set(char *arg)
{
int mode = parse_mode(arg);
if (mode < 0) {
return 1;
}
printf("CPU is entering power mode %d.\n", mode);
puts("Now waiting for a wakeup event...");
fflush(stdout);
pm_set(mode);
/* execution stops here until anything (like shell input) wakes the CPU */
printf("CPU has returned from power mode %d.\n", mode);
return 0;
}
static int cmd_unblock(char *arg)
{
int mode = parse_mode(arg);
if (mode < 0) {
return 1;
}
if (pm_blocker.val_u8[mode] == 0) {
printf("Mode %d is already unblocked.\n", mode);
return 1;
}
printf("Unblocking power mode %d.\n", mode);
fflush(stdout);
pm_unblock(mode);
return 0;
}
static int cmd_show(char *arg)
{
(void)arg;
uint8_t lowest_allowed_mode = 0;
for (unsigned i = 0; i < PM_NUM_MODES; i++) {
printf("mode %u blockers: %u \n", i, pm_blocker.val_u8[i]);
if (pm_blocker.val_u8[i] == 1) {
if (pm_blocker.val_u8[i]) {
lowest_allowed_mode = i + 1;
}
}
printf("lowest allowed mode: %u\n", lowest_allowed_mode);
printf("Lowest allowed mode: %u\n", lowest_allowed_mode);
return 0;
}
#endif /* MODULE_PM_LAYERED */
static int cmd_off(char *arg)
{
(void)arg;
pm_off();
return 0;
}
int _pm_handler(int argc, char **argv)
{
#ifdef MODULE_PM_LAYERED
if (!strcmp(argv[1], "show")) {
if (argc != 2) {
puts("usage: pm show: display current blockers for each power mode");
return 1;
}
return cmd_show(argv[1]);
}
if (!strcmp(argv[1], "block")) {
if (check_mode(argc, argv) != 0) {
return 1;
}
return cmd_block(argv[2]);
}
if (!strcmp(argv[1], "unblock")) {
if (check_mode(argc, argv) != 0) {
return 1;
}
return cmd_unblock(argv[2]);
}
if (!strcmp(argv[1], "set")) {
if (check_mode(argc, argv) != 0) {
return 1;
}
return cmd_set(argv[2]);
}
#else
(void)argc;
#endif /* MODULE_PM_LAYERED */
if (!strcmp(argv[1], "off")) {
return cmd_off(NULL);
}
_print_usage();
return 1;
}

View File

@ -34,8 +34,8 @@ extern int _id_handler(int argc, char **argv);
extern int _heap_handler(int argc, char **argv);
#endif
#ifdef MODULE_PM_LAYERED
extern int _pm(int argc, char **argv);
#ifdef MODULE_PERIPH_PM
extern int _pm_handler(int argc, char **argv);
#endif
#ifdef MODULE_PS
@ -184,8 +184,8 @@ const shell_command_t _shell_command_list[] = {
#ifdef MODULE_HEAP_CMD
{"heap", "Prints heap statistics.", _heap_handler},
#endif
#ifdef MODULE_PM_LAYERED
{ "pm", "interact with layered PM subsystem", _pm },
#ifdef MODULE_PERIPH_PM
{ "pm", "interact with layered PM subsystem", _pm_handler },
#endif
#ifdef MODULE_PS
{"ps", "Prints information about running threads.", _ps_handler},

View File

@ -32,8 +32,10 @@
#include "periph/rtc.h"
#endif
#include "pm_layered.h"
extern int _pm(int argc, char **argv);
#endif
extern int _pm_handler(int argc, char **argv);
#include "shell.h"
#ifndef BTN0_INT_FLANK
@ -42,18 +44,13 @@ extern int _pm(int argc, char **argv);
#ifdef MODULE_PM_LAYERED
/* TODO deduplicate this definition with the one in sys/pm_layered/pm.c */
typedef union {
uint32_t val_u32;
uint8_t val_u8[PM_NUM_MODES];
} pm_blocker_t;
extern volatile pm_blocker_t pm_blocker; /* sys/pm_layered/pm.c */
static int check_mode(int argc, char **argv)
#ifdef MODULE_PERIPH_RTC
static int check_mode_duration(int argc, char **argv)
{
if (argc < 2) {
printf("Usage: %s <power mode>\n", argv[0]);
if (argc != 3) {
printf("Usage: %s <power mode> <duration (s)>\n", argv[0]);
return -1;
}
@ -72,17 +69,6 @@ static int parse_mode(char *argv)
return mode;
}
#ifdef MODULE_PERIPH_RTC
static int check_mode_duration(int argc, char **argv)
{
if (argc != 3) {
printf("Usage: %s <power mode> <duration (s)>\n", argv[0]);
return -1;
}
return 0;
}
static int parse_duration(char *argv)
{
int duration = atoi(argv);
@ -101,106 +87,7 @@ static void cb_rtc(void *arg)
pm_block(level);
}
#endif /* MODULE_PERIPH_RTC */
#endif /* MODULE_PM_LAYERED */
static int cmd_off(int argc, char **argv)
{
(void) argc;
(void) argv;
puts("CPU will turn off.");
fflush(stdout);
pm_off();
return 0;
}
static int cmd_reboot(int argc, char **argv)
{
(void) argc;
(void) argv;
puts("CPU will reboot.");
fflush(stdout);
pm_reboot();
return 0;
}
#ifdef MODULE_PM_LAYERED
static int cmd_block(int argc, char **argv)
{
if (check_mode(argc, argv) != 0) {
return 1;
}
int mode = parse_mode(argv[1]);
if (mode < 0) {
return 1;
}
printf("Blocking power mode %d.\n", mode);
fflush(stdout);
pm_block(mode);
return 0;
}
static int cmd_set(int argc, char **argv)
{
if (check_mode(argc, argv) != 0) {
return 1;
}
int mode = parse_mode(argv[1]);
if (mode < 0) {
return 1;
}
printf("CPU is entering power mode %d.\n", mode);
printf("Now waiting for a wakeup event...\n");
fflush(stdout);
pm_set(mode);
/* execution stops here until anything (like shell input) wakes the CPU */
printf("CPU has returned from power mode %d.\n", mode);
return 0;
}
static int cmd_unblock(int argc, char **argv)
{
if (check_mode(argc, argv) != 0) {
return 1;
}
int mode = parse_mode(argv[1]);
if (mode < 0) {
return 1;
}
if (pm_blocker.val_u8[mode] == 0) {
printf("Mode %d is already unblocked.\n", mode);
return 1;
}
printf("Unblocking power mode %d.\n", mode);
fflush(stdout);
pm_unblock(mode);
return 0;
}
#ifdef MODULE_PERIPH_RTC
static int cmd_unblock_rtc(int argc, char **argv)
{
if (check_mode_duration(argc, argv) != 0) {
@ -248,15 +135,8 @@ static void btn_cb(void *ctx)
* @brief List of shell commands for this example.
*/
static const shell_command_t shell_commands[] = {
{ "off", "turn off", cmd_off },
{ "reboot", "reboot", cmd_reboot },
#ifdef MODULE_PM_LAYERED
{ "block", "block power mode", cmd_block },
{ "set", "set power mode", cmd_set },
{ "unblock", "unblock power mode", cmd_unblock },
#ifdef MODULE_PERIPH_RTC
{ "unblock_rtc", "temporary unblock power mode", cmd_unblock_rtc },
#endif
#if defined MODULE_PM_LAYERED && defined MODULE_PERIPH_RTC
{ "unblock_rtc", "temporarily unblock power mode", cmd_unblock_rtc },
#endif
{ NULL, NULL, NULL }
};
@ -280,7 +160,7 @@ int main(void)
* the state of PM blockers so that the user will know which power mode has
* been entered and is presumably responsible for the unresponsive shell.
*/
_pm(2, (char *[]){"pm", "show"});
_pm_handler(2, (char *[]){"pm", "show"});
#else
puts("This application allows you to test the CPU power management.\n"