Skip to content

Commit ecd7c23

Browse files
alexanderwachtercfriedt
authored andcommitted
drivers: hwinfo: shell: Add reset cause shell command
This commit adds shell commands for the reset cause API - show: show the persistent reset cause - clear: clear the persistent reset cause - supported: list all supported reset causes Signed-off-by: Alexander Wachter <[email protected]>
1 parent 5db37b1 commit ecd7c23

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

drivers/hwinfo/hwinfo_shell.c

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,149 @@ static int cmd_get_device_id(const struct shell *sh, size_t argc, char **argv)
3838
return 0;
3939
}
4040

41+
static inline const char *cause_to_string(uint32_t cause)
42+
{
43+
switch (cause) {
44+
case RESET_PIN:
45+
return "pin";
46+
47+
case RESET_SOFTWARE:
48+
return "software";
49+
50+
case RESET_BROWNOUT:
51+
return "brownout";
52+
53+
case RESET_POR:
54+
return "power-on reset";
55+
56+
case RESET_WATCHDOG:
57+
return "watchdog";
58+
59+
case RESET_DEBUG:
60+
return "debug";
61+
62+
case RESET_SECURITY:
63+
return "security";
64+
65+
case RESET_LOW_POWER_WAKE:
66+
return "low power wake-up";
67+
68+
case RESET_CPU_LOCKUP:
69+
return "CPU lockup";
70+
71+
case RESET_PARITY:
72+
return "parity error";
73+
74+
case RESET_PLL:
75+
return "PLL error";
76+
77+
case RESET_CLOCK:
78+
return "clock";
79+
80+
default:
81+
return "unknown";
82+
}
83+
}
84+
85+
static void print_all_reset_causes(const struct shell *sh, uint32_t cause)
86+
{
87+
for (uint32_t cause_mask = 1; cause_mask; cause_mask <<= 1) {
88+
if (cause & cause_mask) {
89+
shell_print(sh, "- %s",
90+
cause_to_string(cause & cause_mask));
91+
}
92+
}
93+
}
94+
95+
static int cmd_show_reset_cause(const struct shell *sh, size_t argc,
96+
char **argv)
97+
{
98+
int res;
99+
uint32_t cause;
100+
101+
ARG_UNUSED(argc);
102+
ARG_UNUSED(argv);
103+
104+
res = hwinfo_get_reset_cause(&cause);
105+
if (res == -ENOTSUP) {
106+
shell_error(sh, "Not supported by hardware");
107+
return res;
108+
} else if (res != 0) {
109+
shell_error(sh, "Error reading the cause [%d]", res);
110+
return res;
111+
}
112+
113+
if (cause != 0) {
114+
shell_print(sh, "reset caused by:");
115+
print_all_reset_causes(sh, cause);
116+
} else {
117+
shell_print(sh, "No reset cause set");
118+
}
119+
120+
return 0;
121+
}
122+
123+
static int cmd_clear_reset_cause(const struct shell *sh, size_t argc,
124+
char **argv)
125+
{
126+
int res;
127+
128+
ARG_UNUSED(argc);
129+
ARG_UNUSED(argv);
130+
131+
res = hwinfo_clear_reset_cause();
132+
if (res == -ENOTSUP) {
133+
shell_error(sh, "Not supported by hardware");
134+
} else if (res != 0) {
135+
shell_error(sh, "Error clearing the reset causes [%d]", res);
136+
return res;
137+
}
138+
139+
return 0;
140+
}
141+
142+
static int cmd_supported_reset_cause(const struct shell *sh, size_t argc,
143+
char **argv)
144+
{
145+
uint32_t cause;
146+
int res;
147+
148+
ARG_UNUSED(argc);
149+
ARG_UNUSED(argv);
150+
151+
res = hwinfo_get_supported_reset_cause(&cause);
152+
if (res == -ENOTSUP) {
153+
shell_error(sh, "Not supported by hardware");
154+
} else if (res != 0) {
155+
shell_error(sh, "Could not get the supported reset causes [%d]", res);
156+
return res;
157+
}
158+
159+
if (cause != 0) {
160+
shell_print(sh, "supported reset causes:");
161+
print_all_reset_causes(sh, cause);
162+
} else {
163+
shell_print(sh, "No causes supporte");
164+
}
165+
166+
return 0;
167+
}
168+
169+
SHELL_STATIC_SUBCMD_SET_CREATE(sub_reset_cause,
170+
SHELL_CMD_ARG(show, NULL, "Show persistent reset causes",
171+
cmd_show_reset_cause, 1, 0),
172+
SHELL_CMD_ARG(clear, NULL, "Clear all persistent reset causes",
173+
cmd_clear_reset_cause, 1, 0),
174+
SHELL_CMD_ARG(supported, NULL,
175+
"Get a list of all supported reset causes",
176+
cmd_supported_reset_cause, 1, 0),
177+
SHELL_SUBCMD_SET_END /* Array terminated. */
178+
);
179+
41180
SHELL_STATIC_SUBCMD_SET_CREATE(sub_hwinfo,
42181
SHELL_CMD_ARG(devid, NULL, "Show device id", cmd_get_device_id, 1, 0),
182+
SHELL_CMD_ARG(reset_cause, &sub_reset_cause, "Reset cause commands",
183+
cmd_get_device_id, 1, 0),
43184
SHELL_SUBCMD_SET_END /* Array terminated. */
44185
);
45186

0 commit comments

Comments
 (0)