sn32: add watchdog driver (#41)

* sn32: add watchdog driver

reset mode only.

* fix build
This commit is contained in:
dexter93 2022-03-03 13:09:56 +02:00 committed by GitHub
parent 3821ef39ca
commit c391c7d09c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 285 additions and 182 deletions

View File

@ -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 <SN32F2xx.h>
#include <SN32F200_Def.h>
#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);
}

View File

@ -1,48 +0,0 @@
#ifndef __SN32F2XX_WDT_H
#define __SN32F2XX_WDT_H
/*_____ I N C L U D E S ____________________________________________________*/
#include <SN32F2xx.h>
/*_____ 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*/

View File

@ -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

View File

@ -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 */
/** @} */

View File

@ -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 */
/** @} */

View File

@ -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)

View File

@ -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)