HPM SDK
HPMicro Software Development Kit
hpm_ewdg_drv.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023-2024 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_EWDG_DRV_H
9 #define HPM_EWDG_DRV_H
10 
11 #include "hpm_common.h"
12 #include "hpm_ewdg_regs.h"
13 #include "hpm_soc_feature.h"
14 
26 enum {
30 };
31 
38 #define EWDG_REFRESH_UNLOCK_PASSWORD_DEFAULT (0xED09U)
39 #define EWDG_UPDATE_PASSWORD_DEFAULT (0xECF9U)
40 #define EWDG_REFRESH_UNLOCK_FIXED_KEY (0x55AAU)
41 #define EWDG_REFRESH_KEY (0x5A45524FUL)
52 #define EWDG_EVENT_PARITY_ERROR (1UL << 6)
53 #define EWDG_EVENT_TIMEOUT_RESET (1UL << 5)
54 #define EWDG_EVENT_TIMEOUT_INTERRUPT (1UL << 4)
55 #define EWDG_EVENT_CFG_REG_UPDATE_UNLOCK_FAIL (1UL << 3)
56 #define EWDG_EVENT_CFG_REG_UPDATE_VIOLATION (1UL << 2)
57 #define EWDG_EVENT_REFRESH_UNLOCK_FAIL (1UL << 1)
58 #define EWDG_EVENT_REFRESH_VIOLATION (1UL << 0)
68 #define EWDG_INT_PARITY_FAIL (1UL << 2)
69 #define EWDG_INT_CTRL_REG_UNLOCK_FAIL (1UL << 4)
70 #define EWDG_INT_CTRL_REG_UPDATE_FAIL (1UL << 6)
71 #define EWDG_INT_TIMEOUT (1UL << 16)
72 #define EWDG_INT_REFRESH_UNLOCK_FAIL (1UL << 20)
73 #define EWDG_INT_REFRESH_VIOLATION (1UL << 22)
75 #define EWDG_INT_ALL (EWDG_INT_PARITY_FAIL | EWDG_INT_CTRL_REG_UNLOCK_FAIL | EWDG_INT_CTRL_REG_UPDATE_FAIL | \
76  EWDG_INT_TIMEOUT | EWDG_INT_REFRESH_UNLOCK_FAIL | EWDG_INT_REFRESH_VIOLATION)
87 #define EWDG_RST_PARITY_FAIL (1UL << 3)
88 #define EWDG_RST_CTRL_REG_UNLOCK_FAIL (1UL << 5)
89 #define EWDG_RST_CTRL_REG_UPDATE_FAIL (1UL << 7)
90 #define EWDG_RST_TIMEOUT (1UL << 17)
91 #define EWDG_RST_REFRESH_UNLOCK_FAIL (1UL << 21)
92 #define EWDG_RST_REFRESH_VIOLATION (1UL << 23)
94 #define EWDG_RST_ALL (EWDG_RST_PARITY_FAIL | EWDG_RST_CTRL_REG_UNLOCK_FAIL | EWDG_RST_CTRL_REG_UPDATE_FAIL | \
95  EWDG_RST_TIMEOUT | EWDG_RST_REFRESH_UNLOCK_FAIL | EWDG_RST_REFRESH_VIOLATION)
105 typedef enum {
117 
121 typedef enum {
125 
129 typedef enum {
141 
147 typedef enum {
159 
160 typedef enum {
166 
167 /***
168  * @brief EWDG Function Control Configurations
169  */
170 typedef struct {
178 
195  union {
196  struct {
198  uint32_t timeout_reset_us;
199  };
200  struct {
207  };
208  };
209 
216 
220 typedef struct {
234 
238 typedef struct {
242  uint32_t cnt_src_freq;
243 } ewdg_config_t;
244 
253 static inline bool ewdg_is_ctrl_reg_locked(EWDG_Type *ptr)
254 {
255  return ((ptr->CTRL0 & EWDG_CTRL0_CFG_LOCK_MASK) != 0U);
256 }
257 
265 static inline uint32_t ewdg_get_count_clk_divider(EWDG_Type *ptr)
266 {
267  return (1UL << EWDG_CTRL0_DIV_VALUE_GET(ptr->CTRL0));
268 }
269 
278 static inline bool ewdg_is_refresh_locked(EWDG_Type *ptr)
279 {
280  return ((ptr->CTRL0 & EWDG_CTRL0_REF_LOCK_MASK) != 0U);
281 }
282 
288 static inline void ewdg_unlock_ctrl_regs(EWDG_Type *ptr)
289 {
290  uint32_t ctrl_update_prot = ptr->CFG_PROT;
291  ptr->CFG_PROT = ctrl_update_prot;
292 }
293 
298 static inline void ewdg_write_refresh_reg(EWDG_Type *ptr)
299 {
301 }
302 
308 static inline uint32_t ewdg_get_timeout_reset_ticks(EWDG_Type *ptr)
309 {
310  return ptr->OT_RST_VAL;
311 }
312 
313 #if !defined(EWDG_SOC_SUPPORT_TIMEOUT_INTERRUPT) || (EWDG_SOC_SUPPORT_TIMEOUT_INTERRUPT == 1)
319 static inline uint32_t ewdg_get_timeout_interrupt_ticks(EWDG_Type *ptr)
320 {
321  return ptr->OT_INT_VAL;
322 }
323 #endif
324 
333 static inline void ewdg_clear_status_flags(EWDG_Type *ptr, uint32_t mask)
334 {
335  ptr->WDT_STATUS = mask;
336 }
337 
345 static inline uint32_t ewdg_get_status_flags(EWDG_Type *ptr)
346 {
347  return ptr->WDT_STATUS;
348 }
349 
356 {
358 }
359 
372 void ewdg_enable(EWDG_Type *ptr);
373 
374 
379 void ewdg_disable(EWDG_Type *ptr);
380 
391 hpm_stat_t ewdg_init_ctrl_func(EWDG_Type *ptr, ewdg_func_ctrl_config_t *config, uint32_t cnt_src_freq);
392 
403 
410 
421 
431 
441 
450 uint32_t ewdg_get_count_clock_freq(EWDG_Type *ptr, uint32_t src_clk_freq);
451 
460 uint64_t ewdg_convert_timeout_us_to_timeout_ticks(uint32_t src_clk_freq, uint32_t timeout_us);
461 
471 uint32_t ewdg_convert_timeout_ticks_to_timeout_us(EWDG_Type *ptr, uint32_t src_clk_freq, uint32_t timeout_ticks);
472 
478 void ewdg_enable_interrupt(EWDG_Type *ptr, uint32_t mask);
479 
485 void ewdg_disable_interrupt(EWDG_Type *ptr, uint32_t mask);
486 
492 void ewdg_enable_reset(EWDG_Type *ptr, uint32_t mask);
493 
499 void ewdg_disable_reset(EWDG_Type *ptr, uint32_t mask);
500 
507 
508 #ifdef __cplusplus
509 extern "C" {
510 #endif
511 
512 #ifdef __cplusplus
513 }
514 #endif
515 
520 #endif /* HPM_EWDG_DRV_H */
uint32_t hpm_stat_t
Definition: hpm_common.h:119
#define MAKE_STATUS(group, code)
Definition: hpm_common.h:128
@ status_group_ewdg
Definition: hpm_common.h:158
void ewdg_switch_clock_source(EWDG_Type *ptr, ewdg_cnt_clk_sel_t clk_sel)
Switch the EWDG clock source.
Definition: hpm_ewdg_drv.c:454
ewdg_refresh_unlock_method_t
EWDG Refresh Unlock Methods.
Definition: hpm_ewdg_drv.h:105
void ewdg_disable_interrupt(EWDG_Type *ptr, uint32_t mask)
Disable EWDG interrupt.
Definition: hpm_ewdg_drv.c:406
hpm_stat_t ewdg_init(EWDG_Type *ptr, ewdg_config_t *config)
Initialize the EWDG module.
Definition: hpm_ewdg_drv.c:276
uint64_t ewdg_convert_timeout_us_to_timeout_ticks(uint32_t src_clk_freq, uint32_t timeout_us)
Convert the timeout in terms of microseconds to the timeout in terms of timeout ticks.
Definition: hpm_ewdg_drv.c:365
uint32_t ewdg_get_count_clock_freq(EWDG_Type *ptr, uint32_t src_clk_freq)
Get the Divided Counter Clock Frequency for EWDG.
Definition: hpm_ewdg_drv.c:355
ewdg_window_upper_limit_t
EWDG Upper Window Limitations.
Definition: hpm_ewdg_drv.h:147
ewdg_window_low_limit_t
EWDG Lower Window Limitations.
Definition: hpm_ewdg_drv.h:129
static uint32_t ewdg_get_count_clk_divider(EWDG_Type *ptr)
Get the Divider for Counter Clock.
Definition: hpm_ewdg_drv.h:265
void ewdg_disable_reset(EWDG_Type *ptr, uint32_t mask)
Disable EWDG Reset.
Definition: hpm_ewdg_drv.c:438
static void ewdg_unlock_ctrl_regs(EWDG_Type *ptr)
Unlock Write to Control Registers.
Definition: hpm_ewdg_drv.h:288
hpm_stat_t ewdg_init_ctrl_func(EWDG_Type *ptr, ewdg_func_ctrl_config_t *config, uint32_t cnt_src_freq)
Initialize the Control function for EWDG.
Definition: hpm_ewdg_drv.c:86
hpm_stat_t ewdg_init_interrupt_reset(EWDG_Type *ptr, ewdg_interrupt_reset_config_t *config)
Initialize the Error function for EWDG.
Definition: hpm_ewdg_drv.c:204
hpm_stat_t ewdg_unlock_refresh(EWDG_Type *ptr)
Unlock the write to refresh register.
Definition: hpm_ewdg_drv.c:304
uint32_t ewdg_convert_timeout_ticks_to_timeout_us(EWDG_Type *ptr, uint32_t src_clk_freq, uint32_t timeout_ticks)
Convert the timeout in terms of timeout ticks to the timeout in terms of microseconds.
Definition: hpm_ewdg_drv.c:376
static void ewdg_clear_status_flags(EWDG_Type *ptr, uint32_t mask)
Clear Interrupt Status for EWDG.
Definition: hpm_ewdg_drv.h:333
void ewdg_enable(EWDG_Type *ptr)
Enable EWDG.
Definition: hpm_ewdg_drv.c:472
static bool ewdg_is_ctrl_reg_locked(EWDG_Type *ptr)
Check whether the Control Registers are locked.
Definition: hpm_ewdg_drv.h:253
static bool ewdg_is_refresh_locked(EWDG_Type *ptr)
Check whether the Refresh register is locked.
Definition: hpm_ewdg_drv.h:278
static uint32_t ewdg_get_timeout_reset_ticks(EWDG_Type *ptr)
Get the Timeout Reset ticks.
Definition: hpm_ewdg_drv.h:308
void ewdg_disable(EWDG_Type *ptr)
Disable EWDG.
Definition: hpm_ewdg_drv.c:480
ewdg_low_power_mode_t
Definition: hpm_ewdg_drv.h:160
ewdg_cnt_clk_sel_t
EWDG Clock source for internal counter.
Definition: hpm_ewdg_drv.h:121
static void ewdg_write_refresh_reg(EWDG_Type *ptr)
Write Refresh Magic Number to EWDG Refresh register.
Definition: hpm_ewdg_drv.h:298
static ewdg_refresh_unlock_method_t ewdg_get_refresh_unlock_method(EWDG_Type *ptr)
Get the Refresh Unlock Mechanism.
Definition: hpm_ewdg_drv.h:355
static uint32_t ewdg_get_timeout_interrupt_ticks(EWDG_Type *ptr)
Get the Timeout Interrupt ticks.
Definition: hpm_ewdg_drv.h:319
void ewdg_get_default_config(EWDG_Type *ptr, ewdg_config_t *config)
Get default configuration for EWDG.
Definition: hpm_ewdg_drv.c:60
hpm_stat_t ewdg_refresh(EWDG_Type *ptr)
Refresh EWDG.
Definition: hpm_ewdg_drv.c:346
static uint32_t ewdg_get_status_flags(EWDG_Type *ptr)
Get the Status of EWDG.
Definition: hpm_ewdg_drv.h:345
void ewdg_enable_reset(EWDG_Type *ptr, uint32_t mask)
Enable EWDG Reset.
Definition: hpm_ewdg_drv.c:422
void ewdg_enable_interrupt(EWDG_Type *ptr, uint32_t mask)
Enable EWDG interrupt.
Definition: hpm_ewdg_drv.c:390
@ ewdg_refresh_unlock_method_ring_left_shift_password_by_1_bit0_xor_password_bit0
Definition: hpm_ewdg_drv.h:113
@ ewdg_refresh_unlock_method_fixed_key
Definition: hpm_ewdg_drv.h:111
@ ewdg_refresh_unlock_method_password
Definition: hpm_ewdg_drv.h:107
@ ewdg_refresh_unlock_method_ring_left_shift_password_by_1
Definition: hpm_ewdg_drv.h:109
@ ewdg_refresh_unlock_method_max
Definition: hpm_ewdg_drv.h:115
@ ewdg_window_upper_timeout_period_4_div_16
Definition: hpm_ewdg_drv.h:152
@ ewdg_window_upper_timeout_period_7_div_16
Definition: hpm_ewdg_drv.h:155
@ ewdg_window_upper_timeout_period_1_div_16
Definition: hpm_ewdg_drv.h:149
@ ewdg_window_upper_timeout_period_2_div_16
Definition: hpm_ewdg_drv.h:150
@ ewdg_window_upper_timeout_period_6_div_16
Definition: hpm_ewdg_drv.h:154
@ ewdg_window_upper_timeout_period_8_div_16
Definition: hpm_ewdg_drv.h:148
@ ewdg_window_upper_timeout_period_3_div_16
Definition: hpm_ewdg_drv.h:151
@ ewdg_window_upper_timeout_period_max
Definition: hpm_ewdg_drv.h:157
@ ewdg_window_upper_timeout_period_5_div_16
Definition: hpm_ewdg_drv.h:153
@ ewdg_window_lower_timeout_period_8_div_16
Definition: hpm_ewdg_drv.h:131
@ ewdg_window_lower_timeout_period_14_div_16
Definition: hpm_ewdg_drv.h:137
@ ewdg_window_lower_timeout_period_max
Definition: hpm_ewdg_drv.h:139
@ ewdg_window_lower_timeout_period_10_div_16
Definition: hpm_ewdg_drv.h:133
@ ewdg_window_lower_timeout_period_12_div_16
Definition: hpm_ewdg_drv.h:135
@ status_ewdg_feature_unsupported
Definition: hpm_ewdg_drv.h:29
@ status_ewdg_div_out_of_range
Definition: hpm_ewdg_drv.h:28
@ status_ewdg_tick_out_of_range
Definition: hpm_ewdg_drv.h:27
@ ewdg_low_power_mode_halt
Definition: hpm_ewdg_drv.h:161
@ ewdg_low_power_mode_work_clock_normal
Definition: hpm_ewdg_drv.h:164
@ ewdg_low_power_mode_work_clock_div_4
Definition: hpm_ewdg_drv.h:162
@ ewdg_low_power_mode_work_clock_div_2
Definition: hpm_ewdg_drv.h:163
@ ewdg_cnt_clk_src_bus_clk
Definition: hpm_ewdg_drv.h:122
@ ewdg_cnt_clk_src_ext_osc_clk
Definition: hpm_ewdg_drv.h:123
#define EWDG_REFRESH_KEY
Definition: hpm_ewdg_drv.h:41
#define EWDG_CTRL0_REF_UNLOCK_MEC_GET(x)
Definition: hpm_ewdg_regs.h:142
#define EWDG_CTRL0_DIV_VALUE_GET(x)
Definition: hpm_ewdg_regs.h:49
#define EWDG_CTRL0_CFG_LOCK_MASK
Definition: hpm_ewdg_regs.h:82
#define EWDG_CTRL0_REF_LOCK_MASK
Definition: hpm_ewdg_regs.h:125
Definition: hpm_ewdg_regs.h:12
__RW uint32_t OT_RST_VAL
Definition: hpm_ewdg_regs.h:18
__W uint32_t WDT_REFRESH_REG
Definition: hpm_ewdg_regs.h:19
__RW uint32_t WDT_STATUS
Definition: hpm_ewdg_regs.h:20
__RW uint32_t CFG_PROT
Definition: hpm_ewdg_regs.h:21
__RW uint32_t CTRL0
Definition: hpm_ewdg_regs.h:13
__RW uint32_t OT_INT_VAL
Definition: hpm_ewdg_regs.h:17
Enhanced Watchdog Configuration Structure.
Definition: hpm_ewdg_drv.h:238
uint32_t cnt_src_freq
Definition: hpm_ewdg_drv.h:242
bool enable_watchdog
Definition: hpm_ewdg_drv.h:241
ewdg_func_ctrl_config_t ctrl_config
Definition: hpm_ewdg_drv.h:240
ewdg_interrupt_reset_config_t int_rst_config
Definition: hpm_ewdg_drv.h:239
Definition: hpm_ewdg_drv.h:170
bool enable_config_lock
Definition: hpm_ewdg_drv.h:179
bool keep_running_in_debug_mode
Definition: hpm_ewdg_drv.h:187
uint32_t timeout_reset_val
Definition: hpm_ewdg_drv.h:205
ewdg_refresh_unlock_method_t refresh_unlock_method
Definition: hpm_ewdg_drv.h:183
uint32_t timeout_interrupt_val
Definition: hpm_ewdg_drv.h:201
bool enable_window_mode
Definition: hpm_ewdg_drv.h:172
bool use_lowlevel_timeout
Definition: hpm_ewdg_drv.h:194
ewdg_cnt_clk_sel_t cnt_clk_sel
Definition: hpm_ewdg_drv.h:171
uint32_t clock_div_by_power_of_2
Definition: hpm_ewdg_drv.h:206
uint32_t timeout_interrupt_us
Definition: hpm_ewdg_drv.h:197
uint32_t timeout_reset_us
Definition: hpm_ewdg_drv.h:198
bool enable_refresh_lock
Definition: hpm_ewdg_drv.h:182
bool enable_refresh_period
Definition: hpm_ewdg_drv.h:181
uint16_t refresh_period_in_bus_cycles
Definition: hpm_ewdg_drv.h:210
ewdg_window_low_limit_t window_lower_limit
Definition: hpm_ewdg_drv.h:173
bool enable_overtime_self_clear
Definition: hpm_ewdg_drv.h:185
uint16_t ctrl_reg_update_period_bus_clk_x_128
Definition: hpm_ewdg_drv.h:214
uint16_t ctrl_reg_update_password
Definition: hpm_ewdg_drv.h:213
ewdg_low_power_mode_t low_power_mode
Definition: hpm_ewdg_drv.h:188
ewdg_window_upper_limit_t window_upper_limit
Definition: hpm_ewdg_drv.h:177
uint16_t refresh_unlock_password
Definition: hpm_ewdg_drv.h:211
EWDG Reset and Interrupt Configurations.
Definition: hpm_ewdg_drv.h:220
bool enable_ctrl_unlock_fail_interrupt
Definition: hpm_ewdg_drv.h:223
bool enable_refresh_unlock_fail_interrupt
Definition: hpm_ewdg_drv.h:229
bool enable_ctrl_update_violation_reset
Definition: hpm_ewdg_drv.h:226
bool enable_ctrl_unlock_fail_reset
Definition: hpm_ewdg_drv.h:224
bool enable_refresh_violation_interrupt
Definition: hpm_ewdg_drv.h:231
bool enable_ctrl_parity_fail_interrupt
Definition: hpm_ewdg_drv.h:221
bool enable_refresh_violation_reset
Definition: hpm_ewdg_drv.h:232
bool enable_timeout_reset
Definition: hpm_ewdg_drv.h:228
bool enable_timeout_interrupt
Definition: hpm_ewdg_drv.h:227
bool enable_refresh_unlock_fail_reset
Definition: hpm_ewdg_drv.h:230
bool enable_ctrl_update_violation_interrupt
Definition: hpm_ewdg_drv.h:225
bool enable_ctrl_parity_fail_reset
Definition: hpm_ewdg_drv.h:222