From e7d19fd2be66b53f408f0cd5b8073c3d8c0a2649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Roussel?= Date: Wed, 12 Feb 2014 12:42:12 +0100 Subject: [PATCH 01/10] Add a reboot() function to kernel.h definitions. --- core/include/kernel.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/core/include/kernel.h b/core/include/kernel.h index 8e70f1a469..f8f0789d14 100644 --- a/core/include/kernel.h +++ b/core/include/kernel.h @@ -13,6 +13,8 @@ * @file kernel.h * @brief Kernel compile time configuration * + * A reboot() function is also provided (and used by panic() when needed). + * * @author Freie Universität Berlin, Computer Systems & Telematics * @author Kaspar Schleiser */ @@ -86,5 +88,22 @@ extern volatile int lpm_prevent_sleep; extern config_t sysconfig; +/* ------------------------------------------------------------------------- */ + +#ifdef __GNUC__ +#define NORETURN __attribute__((noreturn)) +#else +#define NORETURN /* insert other compiler attributes/pragmas/whatever here */ +#endif + +/** + * @brief Immediately reboots the system. + * + * This function is used by panic() when the DEVELHELP macro is not defined. + * + * @return WARNING: this function NEVER returns! + */ +NORETURN void reboot(void); + /** @} */ #endif /* KERNEL_H_ */ From 7073489dac9465c22c6f58e8d06c6e6b37c8b120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Roussel?= Date: Wed, 12 Feb 2014 12:55:48 +0100 Subject: [PATCH 02/10] Implemented reboot() function for MSP430 MCUs --- cpu/msp430-common/cpu.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cpu/msp430-common/cpu.c b/cpu/msp430-common/cpu.c index 504f637faf..cc8fb1e9ab 100644 --- a/cpu/msp430-common/cpu.c +++ b/cpu/msp430-common/cpu.c @@ -86,3 +86,16 @@ int inISR() { return __inISR; } + +/******************************************************************************/ + +/* System reboot */ +NORETURN void reboot(void) +{ + /* force an hardware reboot ("Power-Up Clear"), by writing + an illegal value to the watchdog control register */ + while (1) { + WDTCTL = 0x0000; + } +} + From ca6db02530b8da2c314265d97165895654bfd8ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Roussel?= Date: Wed, 12 Feb 2014 15:02:50 +0100 Subject: [PATCH 03/10] Function attributes are now defined elsewhere ("attributes.h") --- core/include/kernel.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/core/include/kernel.h b/core/include/kernel.h index f8f0789d14..eea0717243 100644 --- a/core/include/kernel.h +++ b/core/include/kernel.h @@ -23,6 +23,8 @@ #define KERNEL_H_ #include + +#include "attributes.h" #include "config.h" #include "tcb.h" #include "cpu.h" @@ -90,12 +92,6 @@ extern config_t sysconfig; /* ------------------------------------------------------------------------- */ -#ifdef __GNUC__ -#define NORETURN __attribute__((noreturn)) -#else -#define NORETURN /* insert other compiler attributes/pragmas/whatever here */ -#endif - /** * @brief Immediately reboots the system. * From c33087bdcd85536e9e0b28ba78e9fd7b7439bbd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Roussel?= Date: Fri, 14 Feb 2014 11:01:28 +0100 Subject: [PATCH 04/10] Implemented reboot() function for ARM-based MCUs --- cpu/arm_common/arm_cpu.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cpu/arm_common/arm_cpu.c b/cpu/arm_common/arm_cpu.c index 0bf51b02ca..b358a2cae5 100644 --- a/cpu/arm_common/arm_cpu.c +++ b/cpu/arm_common/arm_cpu.c @@ -19,6 +19,7 @@ #include #include "arm_cpu.h" #include "sched.h" +#include "kernel.h" #include "kernel_internal.h" #define STACK_MARKER (0x77777777) @@ -84,3 +85,9 @@ void thread_print_stack(void) printf("STACK (%u)= %X \n", i, *s); } + +NORETURN void reboot(void) +{ + arm_reset(); +} + From 7ad37edb82a671a61d8b7014a3df4416870ba13a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Roussel?= Date: Fri, 14 Feb 2014 12:13:10 +0100 Subject: [PATCH 05/10] Added while(1) to ensure GCC valids NORETURN; fixed indentation --- cpu/arm_common/arm_cpu.c | 4 +++- cpu/msp430-common/cpu.c | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/cpu/arm_common/arm_cpu.c b/cpu/arm_common/arm_cpu.c index b358a2cae5..78bbc23ca2 100644 --- a/cpu/arm_common/arm_cpu.c +++ b/cpu/arm_common/arm_cpu.c @@ -88,6 +88,8 @@ void thread_print_stack(void) NORETURN void reboot(void) { - arm_reset(); + while (1) { + arm_reset(); + } } diff --git a/cpu/msp430-common/cpu.c b/cpu/msp430-common/cpu.c index cc8fb1e9ab..f13fc8ed02 100644 --- a/cpu/msp430-common/cpu.c +++ b/cpu/msp430-common/cpu.c @@ -92,10 +92,10 @@ int inISR() /* System reboot */ NORETURN void reboot(void) { - /* force an hardware reboot ("Power-Up Clear"), by writing - an illegal value to the watchdog control register */ - while (1) { - WDTCTL = 0x0000; - } + /* force an hardware reboot ("Power-Up Clear"), by writing + an illegal value to the watchdog control register */ + while (1) { + WDTCTL = 0x0000; + } } From 561eefab07cc57afc9092d81a11030292dd8d970 Mon Sep 17 00:00:00 2001 From: Ludwig Ortmann Date: Fri, 14 Feb 2014 16:18:40 +0100 Subject: [PATCH 06/10] implement rudimentary native reboot --- cpu/native/include/native_internal.h | 1 + cpu/native/native_cpu.c | 13 +++++++++++++ cpu/native/startup.c | 3 +++ 3 files changed, 17 insertions(+) diff --git a/cpu/native/include/native_internal.h b/cpu/native/include/native_internal.h index 7b8fcab7b6..ca075ea451 100644 --- a/cpu/native/include/native_internal.h +++ b/cpu/native/include/native_internal.h @@ -54,6 +54,7 @@ extern ucontext_t end_context; extern ucontext_t *_native_cur_ctx, *_native_isr_ctx; extern const char *_progname; +extern char **_native_argv; #ifdef MODULE_UART0 #include diff --git a/cpu/native/native_cpu.c b/cpu/native/native_cpu.c index 7ac72aa5fa..b515b5054b 100644 --- a/cpu/native/native_cpu.c +++ b/cpu/native/native_cpu.c @@ -15,7 +15,9 @@ * @file * @author Ludwig Ortmann */ + #include +#include #ifdef __MACH__ #define _XOPEN_SOURCE @@ -40,6 +42,8 @@ #include #include "kernel_internal.h" +#include "kernel.h" +#include "irq.h" #include "sched.h" #include "cpu.h" @@ -59,6 +63,15 @@ char __end_stack[SIGSTKSZ]; fd_set _native_rfds; #endif +NORETURN void reboot(void) +{ + printf("\n\n\t\t!! REBOOT !!\n\n"); + if (execve(_native_argv[0], _native_argv, NULL) == -1) { + err(EXIT_FAILURE, "reboot: execve"); + } + errx(EXIT_FAILURE, "reboot: this should not habe been reached"); +} + /** * TODO: implement */ diff --git a/cpu/native/startup.c b/cpu/native/startup.c index 46d015fc01..f9a893ab3a 100644 --- a/cpu/native/startup.c +++ b/cpu/native/startup.c @@ -41,6 +41,7 @@ int (*real_printf)(const char *format, ...); int _native_null_in_pipe[2]; int _native_null_out_file; const char *_progname; +char **_native_argv; /** * initialize _native_null_in_pipe to allow for reading from stdin @@ -191,7 +192,9 @@ __attribute__((constructor)) static void startup(int argc, char **argv) *(void **)(&real_printf) = dlsym(RTLD_NEXT, "printf"); + _native_argv = argv; _progname = argv[0]; + int argp = 1; char *stderrtype = "stdio"; char *stdouttype = "stdio"; From f6d79103520cd0c77e8f233a7d39aa888f9c7855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Roussel?= Date: Fri, 14 Feb 2014 12:13:10 +0100 Subject: [PATCH 07/10] Added while(1) to ensure GCC valids NORETURN; fixed indentation --- cpu/arm_common/arm_cpu.c | 5 +++-- cpu/msp430-common/cpu.c | 11 +++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cpu/arm_common/arm_cpu.c b/cpu/arm_common/arm_cpu.c index b358a2cae5..92347d3727 100644 --- a/cpu/arm_common/arm_cpu.c +++ b/cpu/arm_common/arm_cpu.c @@ -88,6 +88,7 @@ void thread_print_stack(void) NORETURN void reboot(void) { - arm_reset(); + while (1) { + arm_reset(); + } } - diff --git a/cpu/msp430-common/cpu.c b/cpu/msp430-common/cpu.c index cc8fb1e9ab..a6b6489d6f 100644 --- a/cpu/msp430-common/cpu.c +++ b/cpu/msp430-common/cpu.c @@ -92,10 +92,9 @@ int inISR() /* System reboot */ NORETURN void reboot(void) { - /* force an hardware reboot ("Power-Up Clear"), by writing - an illegal value to the watchdog control register */ - while (1) { - WDTCTL = 0x0000; - } + /* force an hardware reboot ("Power-Up Clear"), by writing + an illegal value to the watchdog control register */ + while (1) { + WDTCTL = 0x0000; + } } - From 523129c1d67761debaca44e8456668f03c379abb Mon Sep 17 00:00:00 2001 From: Ludwig Ortmann Date: Fri, 14 Feb 2014 17:17:25 +0100 Subject: [PATCH 08/10] add system calls to the shell --- sys/shell/commands/Makefile | 2 +- sys/shell/commands/sc_sys.c | 24 ++++++++++++++++++++++++ sys/shell/commands/shell_commands.c | 4 +++- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 sys/shell/commands/sc_sys.c diff --git a/sys/shell/commands/Makefile b/sys/shell/commands/Makefile index 13beb78fef..fd8c912492 100644 --- a/sys/shell/commands/Makefile +++ b/sys/shell/commands/Makefile @@ -1,4 +1,4 @@ -SRC = shell_commands.c sc_id.c +SRC = shell_commands.c sc_id.c sc_sys.c ifneq (,$(findstring transceiver,$(USEMODULE))) SRC += sc_transceiver.c diff --git a/sys/shell/commands/sc_sys.c b/sys/shell/commands/sc_sys.c new file mode 100644 index 0000000000..54ecaec49e --- /dev/null +++ b/sys/shell/commands/sc_sys.c @@ -0,0 +1,24 @@ +/** + * Shell commands for system calls + * + * Copyright (C) 2014 Ludwig Ortmann + * + * This file is subject to the terms and conditions of the GNU Lesser General + * Public License. See the file LICENSE in the top level directory for more + * details. + * + * @ingroup shell_commands + * @{ + * @file + * @brief shell commands for system calls + * @author Ludwig Ortmann + * @} + */ + +#include "kernel.h" + +void _reboot_handler(char *unused) +{ + (void) unused; + reboot(); +} diff --git a/sys/shell/commands/shell_commands.c b/sys/shell/commands/shell_commands.c index ac4356e053..38ac0a5199 100644 --- a/sys/shell/commands/shell_commands.c +++ b/sys/shell/commands/shell_commands.c @@ -1,7 +1,7 @@ /** * Provides prototypes for available shell commands * - * Copyright (C) 2013 INRIA. + * Copyright (C) 2014 INRIA. * * This source code is licensed under the LGPLv2 license, * See the file LICENSE for more details. @@ -24,6 +24,7 @@ #include "shell_commands.h" extern void _id_handler(char *id); +extern void _reboot_handler(char *unused); extern void _heap_handler(char *unused); #ifdef MODULE_PS @@ -104,6 +105,7 @@ extern void _mersenne_get(char *str); const shell_command_t _shell_command_list[] = { {"id", "Gets or sets the node's id.", _id_handler}, + {"reboot", "Reboot the node", _reboot_handler}, #ifdef MODULE_LPC_COMMON {"heap", "Shows the heap state for the LPC2387 on the command shell.", _heap_handler}, #endif From 85a05dd79409fffde0a91e48837491e6cba3f8a6 Mon Sep 17 00:00:00 2001 From: Oleg Hahm Date: Sat, 15 Feb 2014 17:27:50 +0100 Subject: [PATCH 09/10] added reboot and reset handler for lpc1768 --- cpu/lpc1768/cpu.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/cpu/lpc1768/cpu.c b/cpu/lpc1768/cpu.c index 49b6116910..fd41453130 100644 --- a/cpu/lpc1768/cpu.c +++ b/cpu/lpc1768/cpu.c @@ -14,6 +14,7 @@ #include #include "cpu.h" +#include "kernel.h" #define ENABLE_DEBUG (0) #include "debug.h" @@ -106,3 +107,28 @@ void restore_context(void) asm("bx r0"); /* load exception return value to pc causes end of exception*/ /* {r0-r3,r12,LR,PC,xPSR} are restored automatically on exception return */ } + +#define USR_RESET (0x102) +#define SWI (0xAB) + +__attribute__((naked,noreturn)) void arm_reset(void) +{ + int value; + + asm volatile ( + "mov r0, %1" "\n\t" + "mov r1, %2" "\n\t" + "bkpt" " %a3" "\n\t" + "mov %0, r0" + : "=r" (value) /* output operands */ + : "r" USR_RESET, "r" NULL, "i" SWI /* input operands */ + : "r0", "r1", "r2", "r3", "ip", "lr", "memory", "cc" /* list of clobbered registers */ + ); +} + +NORETURN void reboot(void) +{ + while (1) { + arm_reset(); + } +} From 04671810935d18a62f5aa6e94bc3913a805100a2 Mon Sep 17 00:00:00 2001 From: Oleg Hahm Date: Sat, 15 Feb 2014 17:28:19 +0100 Subject: [PATCH 10/10] replaced tabs with spaces --- cpu/lpc1768/cpu.c | 64 +++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/cpu/lpc1768/cpu.c b/cpu/lpc1768/cpu.c index fd41453130..f5d6630195 100644 --- a/cpu/lpc1768/cpu.c +++ b/cpu/lpc1768/cpu.c @@ -21,22 +21,22 @@ int inISR(void) { - return (__get_IPSR() & 0xFF); + return (__get_IPSR() & 0xFF); } unsigned int disableIRQ(void) { - // FIXME PRIMASK is the old CPSR (FAULTMASK ??? BASEPRI ???) - //PRIMASK lesen - unsigned int uiPriMask = __get_PRIMASK(); - __disable_irq(); - return uiPriMask; + // FIXME PRIMASK is the old CPSR (FAULTMASK ??? BASEPRI ???) + //PRIMASK lesen + unsigned int uiPriMask = __get_PRIMASK(); + __disable_irq(); + return uiPriMask; } void restoreIRQ(unsigned oldPRIMASK) { - //PRIMASK lesen setzen - __set_PRIMASK(oldPRIMASK); + //PRIMASK lesen setzen + __set_PRIMASK(oldPRIMASK); } @@ -76,36 +76,36 @@ void eINT(void) void save_context(void) { - /* {r0-r3,r12,LR,PC,xPSR} are saved automatically on exception entry */ - asm("push {r4-r11}"); - /* save unsaved registers */ - asm("push {LR}"); - /* save exception return value */ + /* {r0-r3,r12,LR,PC,xPSR} are saved automatically on exception entry */ + asm("push {r4-r11}"); + /* save unsaved registers */ + asm("push {LR}"); + /* save exception return value */ - asm("ldr r1, =active_thread"); - /* load address of currend pdc */ - asm("ldr r1, [r1]"); - /* deref pdc */ - asm("str sp, [r1]"); - /* write sp to pdc->sp means current threads stack pointer */ + asm("ldr r1, =active_thread"); + /* load address of currend pdc */ + asm("ldr r1, [r1]"); + /* deref pdc */ + asm("str sp, [r1]"); + /* write sp to pdc->sp means current threads stack pointer */ } void restore_context(void) { - asm("ldr r0, =active_thread"); - /* load address of currend pdc */ - asm("ldr r0, [r0]"); - /* deref pdc */ - asm("ldr sp, [r0]"); - /* load pdc->sp to sp register */ + asm("ldr r0, =active_thread"); + /* load address of currend pdc */ + asm("ldr r0, [r0]"); + /* deref pdc */ + asm("ldr sp, [r0]"); + /* load pdc->sp to sp register */ - asm("pop {r0}"); - /* restore exception retrun value from stack */ - asm("pop {r4-r11}"); - /* load unloaded register */ -// asm("pop {r4}"); /*foo*/ - asm("bx r0"); /* load exception return value to pc causes end of exception*/ - /* {r0-r3,r12,LR,PC,xPSR} are restored automatically on exception return */ + asm("pop {r0}"); + /* restore exception retrun value from stack */ + asm("pop {r4-r11}"); + /* load unloaded register */ +// asm("pop {r4}"); /*foo*/ + asm("bx r0"); /* load exception return value to pc causes end of exception*/ + /* {r0-r3,r12,LR,PC,xPSR} are restored automatically on exception return */ } #define USR_RESET (0x102)