CH592_Makefile_Template/sdk/StdPeriphDriver/inc/CH59x_sys.h

206 lines
5.0 KiB
C
Raw Normal View History

2023-12-19 16:10:07 +01:00
/********************************** (C) COPYRIGHT *******************************
* File Name : CH59x_SYS.h
* Author : WCH
* Version : V1.2
* Date : 2021/11/17
* Description
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#ifndef __CH59x_SYS_H__
#define __CH59x_SYS_H__
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief rtc interrupt event define
*/
typedef enum
{
RST_STATUS_SW = 0, // 软件复位
RST_STATUS_RPOR, // 上电复位
RST_STATUS_WTR, // 看门狗超时复位
RST_STATUS_MR, // 外部手动复位
RST_STATUS_LRM0, // 唤醒复位-软复位引起
RST_STATUS_GPWSM, // 下电模式唤醒复位
RST_STATUS_LRM1, // 唤醒复位-看门狗引起
RST_STATUS_LRM2, // 唤醒复位-手动复位引起
} SYS_ResetStaTypeDef;
/**
* @brief rtc interrupt event define
*/
typedef enum
{
INFO_ROM_READ = 0, // FlashROM 代码和数据区 是否可读
INFO_RESET_EN = 2, // RST#外部手动复位输入功能是否开启
INFO_BOOT_EN, // 系统引导程序 BootLoader 是否开启
INFO_DEBUG_EN, // 系统仿真调试接口是否开启
INFO_LOADER, // 当前系统是否处于Bootloader 区
STA_SAFEACC_ACT, // 当前系统是否处于安全访问状态否则RWA属性区域不可访问
} SYS_InfoStaTypeDef;
/**
* @brief ID类
*/
#define SYS_GetChipID() R8_CHIP_ID
/**
* @brief 访ID
*/
#define SYS_GetAccessID() R8_SAFE_ACCESS_ID
/**
* @brief
*
* @param sc - refer to SYS_CLKTypeDef
*/
void SetSysClock(SYS_CLKTypeDef sc);
/**
* @brief
*
* @return Hz
*/
uint32_t GetSysClock(void);
/**
* @brief
*
* @param i - refer to SYS_InfoStaTypeDef
*
* @return
*/
uint8_t SYS_GetInfoSta(SYS_InfoStaTypeDef i);
/**
* @brief
*
* @return refer to SYS_ResetStaTypeDef
*/
#define SYS_GetLastResetSta() (R8_RESET_STATUS & RB_RESET_FLAG)
/**
* @brief
*/
void SYS_ResetExecute(void);
/**
* @brief
*
* @param i - refer to SYS_InfoStaTypeDef
*/
#define SYS_ResetKeepBuf(d) (R8_GLOB_RESET_KEEP = d)
/**
* @brief
*
* @param pirqv -
*/
void SYS_DisableAllIrq(uint32_t *pirqv);
/**
* @brief
*
* @param irq_status -
*/
void SYS_RecoverIrq(uint32_t irq_status);
/**
* @brief (SYSTICK)
*
* @return
*/
uint32_t SYS_GetSysTickCnt(void);
/**
* @brief
*
* @param c -
*/
#define WWDG_SetCounter(c) (R8_WDOG_COUNT = c)
/**
* @brief 使
*
* @param s -
*/
void WWDG_ITCfg(FunctionalState s);
/**
* @brief
*
* @param s -
*/
void WWDG_ResetCfg(FunctionalState s);
/**
* @brief
*
* @return
*/
#define WWDG_GetFlowFlag() (R8_RST_WDOG_CTRL & RB_WDOG_INT_FLAG)
/**
* @brief
*/
void WWDG_ClearFlag(void);
/**
* @brief uS
*
* @param t -
*/
void mDelayuS(uint16_t t);
/**
* @brief mS
*
* @param t -
*/
void mDelaymS(uint16_t t);
extern volatile uint32_t IRQ_STA;
/**
* @brief Enter safe access mode.
*
* @NOTE: After enter safe access mode, about 16 system frequency cycles
* are in safe mode, and one or more secure registers can be rewritten
* within the valid period. The safe mode will be automatically
* terminated after the above validity period is exceeded.
*/
__attribute__((always_inline)) static inline void sys_safe_access_enable(void)
{
if(read_csr(0x800)&0x08)
{
IRQ_STA = read_csr(0x800);
write_csr(0x800, (IRQ_STA&(~0x08)));
}
SAFEOPERATE;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
SAFEOPERATE;
}
__attribute__((always_inline)) static inline void sys_safe_access_disable(void)
{
R8_SAFE_ACCESS_SIG = 0;
write_csr(0x800, read_csr(0x800)|(IRQ_STA&0x08));
IRQ_STA = 0;
SAFEOPERATE;
}
#ifdef __cplusplus
}
#endif
#endif // __CH59x_SYS_H__