From c391c7d09c546a67f1ea45df603200c5a9be8709 Mon Sep 17 00:00:00 2001 From: dexter93 Date: Thu, 3 Mar 2022 13:09:56 +0200 Subject: [PATCH] sn32: add watchdog driver (#41) * sn32: add watchdog driver reset mode only. * fix build --- os/hal/ports/SN32/LLD/SN32F2xx/WDT/WDT.c | 134 --------------- os/hal/ports/SN32/LLD/SN32F2xx/WDT/WDT.h | 48 ------ os/hal/ports/SN32/LLD/SN32F2xx/WDT/driver.mk | 9 ++ .../ports/SN32/LLD/SN32F2xx/WDT/hal_wdg_lld.c | 122 ++++++++++++++ .../ports/SN32/LLD/SN32F2xx/WDT/hal_wdg_lld.h | 152 ++++++++++++++++++ os/hal/ports/SN32/SN32F240B/platform.mk | 1 + os/hal/ports/SN32/SN32F260/platform.mk | 1 + 7 files changed, 285 insertions(+), 182 deletions(-) delete mode 100644 os/hal/ports/SN32/LLD/SN32F2xx/WDT/WDT.c delete mode 100644 os/hal/ports/SN32/LLD/SN32F2xx/WDT/WDT.h create mode 100644 os/hal/ports/SN32/LLD/SN32F2xx/WDT/driver.mk create mode 100644 os/hal/ports/SN32/LLD/SN32F2xx/WDT/hal_wdg_lld.c create mode 100644 os/hal/ports/SN32/LLD/SN32F2xx/WDT/hal_wdg_lld.h diff --git a/os/hal/ports/SN32/LLD/SN32F2xx/WDT/WDT.c b/os/hal/ports/SN32/LLD/SN32F2xx/WDT/WDT.c deleted file mode 100644 index 6f14d9ee..00000000 --- a/os/hal/ports/SN32/LLD/SN32F2xx/WDT/WDT.c +++ /dev/null @@ -1,134 +0,0 @@ -/******************** (C) COPYRIGHT 2015 SONiX ******************************* -* COMPANY: SONiX -* DATE: 2017/7 -* AUTHOR: SA1 -* IC: SN32F240B -* DESCRIPTION: WDT related functions. -*____________________________________________________________________________ -* REVISION Date User Description -* 1.0 2017/07/07 SA1 First release -* -*____________________________________________________________________________ -* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET. -* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL -* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE -* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN -* IN CONNECTION WITH THEIR PRODUCTS. -*****************************************************************************/ - -/*_____ I N C L U D E S ____________________________________________________*/ -#include -#include -#include "WDT.h" - -/*_____ D E C L A R A T I O N S ____________________________________________*/ - - -/*_____ D E F I N I T I O N S ______________________________________________*/ - - -/*_____ M A C R O S ________________________________________________________*/ - - - -/*_____ F U N C T I O N S __________________________________________________*/ -/***************************************************************************** -* Function : WDT_IRQHandler -* Description : ISR of WDT interrupt -* Input : None -* Output : None -* Return : None -* Note : None -*****************************************************************************/ -__irq void WDT_IRQHandler(void) -{ - SN_GPIO2->DATA_b.DATA0 = ~SN_GPIO2->DATA_b.DATA0; //P2.0 toggle - - __WDT_FEED_VALUE; - - __WDT_CLRINSTS; //Clear WDT interrupt flag -} - -/***************************************************************************** -* Function : WDT_Init -* Description : WDT initial -* Input : None -* Output : None -* Return : None -* Note : None -*****************************************************************************/ -void WDT_Init(void) -{ - uint32_t wRegBuf; - - SN_SYS1->APBCP1_b.WDTPRE = 2; - - #if WDT_MODE == INTERRUPT - wRegBuf = mskWDT_WDTIE_ENABLE; //WDT as interrupt mode - #endif - - #if WDT_MODE == RESET - wRegBuf = mskWDT_WDTIE_DISABLE; //WDT as reset mode - #endif - - wRegBuf = wRegBuf & (~mskWDT_WDTINT); //Clear WDT interrupt flag - - wRegBuf = wRegBuf | mskWDT_WDKEY; - - SN_WDT->CFG = wRegBuf; - - WDT_ReloadValue(13); //Set overflow time = 250ms - - WDT_NvicEnable(); //Enable WDT NVIC interrupt - - __WDT_ENABLE; //Enable WDT -} - -/***************************************************************************** -* Function : WDT_ReloadValue -* Description : set WDT reload value -* Input : time - - 0~255: overflow time set -* Output : None -* Return : None -* Note : None -*****************************************************************************/ -void WDT_ReloadValue(uint32_t time) -{ - uint32_t wRegBuf; - - wRegBuf = time | mskWDT_WDKEY; - - SN_WDT->TC = wRegBuf; - - __WDT_FEED_VALUE; -} - -/***************************************************************************** -* Function : WDT_NvicEnable -* Description : Enable WDT interrupt -* Input : None -* Output : None -* Return : None -* Note : None -*****************************************************************************/ -void WDT_NvicEnable(void) -{ - NVIC_ClearPendingIRQ(WDT_IRQn); - NVIC_EnableIRQ(WDT_IRQn); - NVIC_SetPriority(WDT_IRQn, 0); // Set interrupt priority (default) -} - -/***************************************************************************** -* Function : WDT_NvicDisable -* Description : Disable WDT interrupt -* Input : None -* Output : None -* Return : None -* Note : None -*****************************************************************************/ -void WDT_NvicDisable(void) -{ - NVIC_DisableIRQ(WDT_IRQn); -} diff --git a/os/hal/ports/SN32/LLD/SN32F2xx/WDT/WDT.h b/os/hal/ports/SN32/LLD/SN32F2xx/WDT/WDT.h deleted file mode 100644 index da1a768b..00000000 --- a/os/hal/ports/SN32/LLD/SN32F2xx/WDT/WDT.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef __SN32F2XX_WDT_H -#define __SN32F2XX_WDT_H - -/*_____ I N C L U D E S ____________________________________________________*/ -#include - -/*_____ D E F I N I T I O N S ______________________________________________*/ -#define RESET 0 //WDT as Reset mode -#define INTERRUPT 1 //WDT as Interrupt mode -#define WDT_MODE INTERRUPT - //RESET_MODE : WDT as Reset mode - -//Watchdog register key -#define mskWDT_WDKEY (0x5AFA<<16) - -//Watchdog interrupt flag -#define mskWDT_WDTINT (1<<2) - -//Watchdog interrupt enable -#define WDT_WDTIE_DISABLE 0 -#define WDT_WDTIE_ENABLE 1 -#define mskWDT_WDTIE_DISABLE (WDT_WDTIE_DISABLE<<1) -#define mskWDT_WDTIE_ENABLE (WDT_WDTIE_ENABLE<<1) - -//Watchdog enable -#define mskWDT_WDTEN_DISABLE 0 -#define mskWDT_WDTEN_ENABLE 1 - -//Watchdog Feed value -#define mskWDT_FV 0x55AA - -/*_____ M A C R O S ________________________________________________________*/ -//Watchdog Feed Value -#define __WDT_FEED_VALUE (SN_WDT->FEED = (mskWDT_WDKEY | mskWDT_FV)) - -//WDT Enable/Disable -#define __WDT_ENABLE (SN_WDT->CFG |= (mskWDT_WDKEY | mskWDT_WDTEN_ENABLE)) -#define __WDT_DISABLE (SN_WDT->CFG = (mskWDT_WDKEY | (SN_WDT->CFG & ~mskWDT_WDTEN_ENABLE))) - -//WDT INT status register clear -#define __WDT_CLRINSTS (SN_WDT->CFG = (mskWDT_WDKEY | (SN_WDT->CFG & ~mskWDT_WDTINT))) - -/*_____ D E C L A R A T I O N S ____________________________________________*/ -void WDT_Init(void); -void WDT_ReloadValue(uint32_t time); -void WDT_NvicEnable(void); -void WDT_NvicDisable(void); -#endif /*__SN32F2XX_WDT_H*/ diff --git a/os/hal/ports/SN32/LLD/SN32F2xx/WDT/driver.mk b/os/hal/ports/SN32/LLD/SN32F2xx/WDT/driver.mk new file mode 100644 index 00000000..704566f4 --- /dev/null +++ b/os/hal/ports/SN32/LLD/SN32F2xx/WDT/driver.mk @@ -0,0 +1,9 @@ +ifeq ($(USE_SMART_BUILD),yes) +ifneq ($(findstring HAL_USE_WDG TRUE,$(HALCONF)),) +PLATFORMSRC += $(CHIBIOS_CONTRIB)/os/hal/ports/SN32/LLD/SN32F2xx/WDT/hal_wdg_lld.c +endif +else +PLATFORMSRC += $(CHIBIOS_CONTRIB)/os/hal/ports/SN32/LLD/SN32F2xx/WDT/hal_wdg_lld.c +endif + +PLATFORMINC += $(CHIBIOS_CONTRIB)/os/hal/ports/SN32/LLD/SN32F2xx/WDT diff --git a/os/hal/ports/SN32/LLD/SN32F2xx/WDT/hal_wdg_lld.c b/os/hal/ports/SN32/LLD/SN32F2xx/WDT/hal_wdg_lld.c new file mode 100644 index 00000000..13156d7b --- /dev/null +++ b/os/hal/ports/SN32/LLD/SN32F2xx/WDT/hal_wdg_lld.c @@ -0,0 +1,122 @@ +/* + ChibiOS - Copyright (C) 2006..2020 Giovanni Di Sirio + + Licensed 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. +*/ + +/** + * @file WDT/hal_wdg_lld.c + * @brief WDG Driver subsystem low level driver source. + * + * @addtogroup WDG + * @{ + */ + +#include "hal.h" + +#if (HAL_USE_WDG == TRUE) || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver local definitions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver exported variables. */ +/*===========================================================================*/ + +#if SN32_WDG_USE_WDT || defined(__DOXYGEN__) +WDGDriver WDGD1; +#endif + +/*===========================================================================*/ +/* Driver local variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local functions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver interrupt handlers. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver exported functions. */ +/*===========================================================================*/ + +/** + * @brief Low level WDG driver initialization. + * + * @notapi + */ +void wdg_lld_init(void) { + +#if SN32_WDG_USE_WDT + WDGD1.state = WDG_STOP; + WDGD1.wdg = SN_WDT; +#endif +} + +/** + * @brief Configures and activates the WDG peripheral. + * + * @param[in] wdgp pointer to the @p WDGDriver object + * + * @notapi + */ +void wdg_lld_start(WDGDriver *wdgp) { + uint32_t wRegBuf; + /* Enable WDT and unlock for write.*/ + sys1EnableWDT(); + wRegBuf = mskWDT_WDTIE_DISABLE; //WDT as reset mode + wRegBuf = wRegBuf & (~mskWDT_WDTINT); //Clear WDT interrupt flag + wRegBuf = wRegBuf | mskWDT_WDKEY; + wdgp->wdg->CFG = wRegBuf; + + /* Write configuration.*/ + sys1SelectWDTPRE(wdgp->config->pr); + wdgp->wdg->TC = (wdgp->config->tc | mskWDT_WDKEY); + wdg_lld_reset(&WDGD1); + + /* Start .*/ + wdgp->wdg->CFG = (mskWDT_WDKEY | mskWDT_WDTEN_ENABLE); +} + +/** + * @brief Deactivates the WDG peripheral. + * + * @param[in] wdgp pointer to the @p WDGDriver object + * + * @notapi + */ +void wdg_lld_stop(WDGDriver *wdgp) { + if (wdgp->state == WDG_READY) { + wdgp->wdg->CFG = (mskWDT_WDKEY | (wdgp->wdg->CFG & ~mskWDT_WDTEN_ENABLE)); + sys1DisableWDT(); + } +} + +/** + * @brief Reloads WDG's counter. + * + * @param[in] wdgp pointer to the @p WDGDriver object + * + * @notapi + */ +void wdg_lld_reset(WDGDriver * wdgp) { + wdgp->wdg->FEED = (mskWDT_WDKEY | mskWDT_FV); +} + +#endif /* HAL_USE_WDG == TRUE */ + +/** @} */ diff --git a/os/hal/ports/SN32/LLD/SN32F2xx/WDT/hal_wdg_lld.h b/os/hal/ports/SN32/LLD/SN32F2xx/WDT/hal_wdg_lld.h new file mode 100644 index 00000000..203c15a6 --- /dev/null +++ b/os/hal/ports/SN32/LLD/SN32F2xx/WDT/hal_wdg_lld.h @@ -0,0 +1,152 @@ +/* + ChibiOS - Copyright (C) 2006..2020 Giovanni Di Sirio + + Licensed 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. +*/ + +/** + * @file WDT/hal_wdg_lld.h + * @brief WDG Driver subsystem low level driver header. + * + * @addtogroup WDG + * @{ + */ + +#ifndef HAL_WDG_LLD_H +#define HAL_WDG_LLD_H + +#if (HAL_USE_WDG == TRUE) || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver constants. */ +/*===========================================================================*/ + +//Watchdog register key +#define mskWDT_WDKEY (0x5AFA<<16) + +//Watchdog interrupt flag +#define mskWDT_WDTINT (1<<2) + +//Watchdog interrupt enable +#define WDT_WDTIE_DISABLE 0 +#define WDT_WDTIE_ENABLE 1 +#define mskWDT_WDTIE_DISABLE (WDT_WDTIE_DISABLE<<1) +#define mskWDT_WDTIE_ENABLE (WDT_WDTIE_ENABLE<<1) + +//Watchdog enable +#define mskWDT_WDTEN_DISABLE 0 +#define mskWDT_WDTEN_ENABLE 1 + +//Watchdog Feed value +#define mskWDT_FV 0x55AA +/** @} */ + +/*===========================================================================*/ +/* Driver pre-compile time settings. */ +/*===========================================================================*/ + +/** + * @name Configuration options + * @{ + */ +/** + * @brief WDT driver enable switch. + * @details If set to @p TRUE the support for WDT is included. + * @note The default is @p FALSE. + */ +#if !defined(SN32_WDG_USE_WDT) || defined(__DOXYGEN__) +#define SN32_WDG_USE_WDT FALSE +#endif +/** @} */ + +/*===========================================================================*/ +/* Derived constants and error checks. */ +/*===========================================================================*/ + +#if !SN32_WDG_USE_WDT +#error "WDG driver activated but no WDT peripheral assigned" +#endif + +/*===========================================================================*/ +/* Driver data structures and types. */ +/*===========================================================================*/ + +/** + * @brief Type of a structure representing an WDG driver. + */ +typedef struct WDGDriver WDGDriver; + +/** + * @brief Driver configuration structure. + * @note It could be empty on some architectures. + */ +typedef struct { + /** + * @brief Configuration of the WDT preloader. + * @details See the SN32 reference manual for details. + */ + uint32_t pr; + /** + * @brief Configuration of the WDT_TC register. + * @details See the SN32 reference manual for details. + */ + uint32_t tc; +} WDGConfig; + +/** + * @brief Structure representing an WDG driver. + */ +struct WDGDriver { + /** + * @brief Driver state. + */ + wdgstate_t state; + /** + * @brief Current configuration data. + */ + const WDGConfig *config; + /* End of the mandatory fields.*/ + /** + * @brief Pointer to the WDT registers block. + */ + SN_WDT_Type *wdg; +}; + +/*===========================================================================*/ +/* Driver macros. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +#if SN32_WDG_USE_WDT && !defined(__DOXYGEN__) +extern WDGDriver WDGD1; +#endif + +#ifdef __cplusplus +extern "C" { +#endif + void wdg_lld_init(void); + void wdg_lld_start(WDGDriver *wdgp); + void wdg_lld_stop(WDGDriver *wdgp); + void wdg_lld_reset(WDGDriver *wdgp); +#ifdef __cplusplus +} +#endif + +#endif /* HAL_USE_WDG == TRUE */ + +#endif /* HAL_WDG_LLD_H */ + +/** @} */ diff --git a/os/hal/ports/SN32/SN32F240B/platform.mk b/os/hal/ports/SN32/SN32F240B/platform.mk index 11a4de26..66b64149 100644 --- a/os/hal/ports/SN32/SN32F240B/platform.mk +++ b/os/hal/ports/SN32/SN32F240B/platform.mk @@ -25,6 +25,7 @@ include ${CHIBIOS_CONTRIB}/os/hal/ports/SN32/LLD/SN32F2xx/CT/driver.mk include ${CHIBIOS_CONTRIB}/os/hal/ports/SN32/LLD/SN32F2xx/FLASH/driver.mk include ${CHIBIOS_CONTRIB}/os/hal/ports/SN32/LLD/SN32F2xx/SysTick/driver.mk include ${CHIBIOS_CONTRIB}/os/hal/ports/SN32/LLD/SN32F2xx/SPI/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/SN32/LLD/SN32F2xx/WDT/driver.mk # Shared variables ALLCSRC += $(PLATFORMSRC_CONTRIB) diff --git a/os/hal/ports/SN32/SN32F260/platform.mk b/os/hal/ports/SN32/SN32F260/platform.mk index 93c7fc23..2082c739 100644 --- a/os/hal/ports/SN32/SN32F260/platform.mk +++ b/os/hal/ports/SN32/SN32F260/platform.mk @@ -25,6 +25,7 @@ include ${CHIBIOS_CONTRIB}/os/hal/ports/SN32/LLD/SN32F2xx/CT/driver.mk include ${CHIBIOS_CONTRIB}/os/hal/ports/SN32/LLD/SN32F2xx/FLASH/driver.mk include ${CHIBIOS_CONTRIB}/os/hal/ports/SN32/LLD/SN32F2xx/SysTick/driver.mk include ${CHIBIOS_CONTRIB}/os/hal/ports/SN32/LLD/SN32F2xx/SPI/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/SN32/LLD/SN32F2xx/WDT/driver.mk # Shared variables ALLCSRC += $(PLATFORMSRC_CONTRIB)