From 9e98ed493d045c49b42eb237a0acac6772ede479 Mon Sep 17 00:00:00 2001 From: Michal Lenc Date: Tue, 6 Feb 2024 15:53:28 +0100 Subject: [PATCH] arch/samv7: add function to retrieve reset cause from HW SAMv7 reset controller stores the cause of last reset (SW reset, power up, pin reset etc.) in status register. This commit adds function that allows the board to retrieve this information. This function should be called from board support layer either during initialization or based on incoming ioctl call. Adding the sam_get_reset_cause() to sam_systemreset.c also resulted in always compiling this file by default and only putting up_systemreset() under CONFIG_SAMV7_SYSTEMRESET option. Also header file sam_systemreset.h was created as it defines reset types in comfortable manner for future processing in board layer. This is done to avoid passing boardctl dependent structure to architecture layer. Signed-off-by: Michal Lenc --- arch/arm/src/samv7/Make.defs | 5 +-- arch/arm/src/samv7/sam_systemreset.c | 51 ++++++++++++++++++++++- arch/arm/src/samv7/sam_systemreset.h | 62 ++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 arch/arm/src/samv7/sam_systemreset.h diff --git a/arch/arm/src/samv7/Make.defs b/arch/arm/src/samv7/Make.defs index 7fb0d80960071..28e4dadc2d19a 100644 --- a/arch/arm/src/samv7/Make.defs +++ b/arch/arm/src/samv7/Make.defs @@ -29,6 +29,7 @@ include armv7-m/Make.defs CHIP_CSRCS = sam_start.c sam_clockconfig.c sam_irq.c sam_allocateheap.c CHIP_CSRCS += sam_lowputc.c sam_serial.c sam_gpio.c sam_pck.c sam_uid.c +CHIP_CSRCS += sam_systemreset.c # Configuration-dependent SAMV7 files @@ -59,10 +60,6 @@ ifeq ($(CONFIG_SAMV7_RSWDT),y) CHIP_CSRCS += sam_rswdt.c endif -ifeq ($(CONFIG_SAMV7_SYSTEMRESET),y) -CHIP_CSRCS += sam_systemreset.c -endif - ifeq ($(CONFIG_SAMV7_1WIREDRIVER),y) CHIP_CSRCS += sam_1wire.c endif diff --git a/arch/arm/src/samv7/sam_systemreset.c b/arch/arm/src/samv7/sam_systemreset.c index 02d969ff09240..0ae11e233559b 100644 --- a/arch/arm/src/samv7/sam_systemreset.c +++ b/arch/arm/src/samv7/sam_systemreset.c @@ -33,13 +33,59 @@ #include "arm_internal.h" #include "hardware/sam_rstc.h" - -#ifdef CONFIG_SAMV7_SYSTEMRESET +#include "sam_systemreset.h" /**************************************************************************** * Public Functions ****************************************************************************/ +/**************************************************************************** + * Name: sam_get_reset_cause + * + * Description: + * Get cause of the last CPU reset. This is done by reading reset status + * registger. + * + * Returned Value: + * CPU reset cause in form of macros defined in sam_systemreset.h. This is + * to avoid passing boardctl dependent structure to architecture layer. + * Board level specific code should include sam_systemreset.h and set + * boardctl result according to that. -1 is returned in case of invalid + * value in status register. + * + ****************************************************************************/ + +int sam_get_reset_cause(void) +{ + int ret; + uint32_t rstsr; + + rstsr = getreg32(SAM_RSTC_SR); + switch (rstsr & RSTC_SR_RSTTYP_MASK) + { + case RSTC_SR_RSTTYP_PWRUP: + ret = SAMV7_RESET_PWRUP; + break; + case RSTC_SR_RSTTYP_BACKUP: + ret = SAMV7_RESET_BACKUP; + break; + case RSTC_SR_RSTTYP_WDOG: + ret = SAMV7_RESET_WDOG; + break; + case RSTC_SR_RSTTYP_SWRST: + ret = SAMV7_RESET_SWRST; + break; + case RSTC_SR_RSTTYP_NRST: + ret = SAMV7_RESET_NRST; + break; + default: + ret = -1; + break; + } + + return ret; +} + /**************************************************************************** * Name: up_systemreset * @@ -48,6 +94,7 @@ * ****************************************************************************/ +#ifdef CONFIG_SAMV7_SYSTEMRESET void up_systemreset(void) { uint32_t rstcr; diff --git a/arch/arm/src/samv7/sam_systemreset.h b/arch/arm/src/samv7/sam_systemreset.h new file mode 100644 index 0000000000000..d64c193e5d811 --- /dev/null +++ b/arch/arm/src/samv7/sam_systemreset.h @@ -0,0 +1,62 @@ +/**************************************************************************** + * arch/arm/src/samv7/sam_systemreset.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_ARM_SRC_SAMV7_SAM_SYSTEMRESET_H +#define __ARCH_ARM_SRC_SAMV7_SAM_SYSTEMRESET_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define SAMV7_RESET_PWRUP 1 +#define SAMV7_RESET_BACKUP 2 +#define SAMV7_RESET_WDOG 3 +#define SAMV7_RESET_SWRST 4 +#define SAMV7_RESET_NRST 5 + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: sam_get_reset_cause + * + * Description: + * Get cause of the last CPU reset. This is done by reading reset status + * registger. + * + * Returned Value: + * CPU reset cause in form of macros defined in sam_systemreset.h. This is + * to avoid passing boardctl dependent structure to architecture layer. + * Board level specific code should include sam_systemreset.h and set + * boardctl result according to that. -1 is returned in case of invalid + * value in status register. + * + ****************************************************************************/ + +int sam_get_reset_cause(void); + +#endif /* __ARCH_ARM_SRC_SAMV7_SAM_SYSTEMRESET_H */