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 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
29 enum {
33 };
34 
41 #define EWDG_REFRESH_UNLOCK_PASSWORD_DEFAULT (0xED09U)
42 #define EWDG_UPDATE_PASSWORD_DEFAULT (0xECF9U)
43 #define EWDG_REFRESH_UNLOCK_FIXED_KEY (0x55AAU)
44 #define EWDG_REFRESH_KEY (0x5A45524FUL)
55 #define EWDG_EVENT_PARITY_ERROR (1UL << 6)
56 #define EWDG_EVENT_TIMEOUT_RESET (1UL << 5)
57 #define EWDG_EVENT_TIMEOUT_INTERRUPT (1UL << 4)
58 #define EWDG_EVENT_CFG_REG_UPDATE_UNLOCK_FAIL (1UL << 3)
59 #define EWDG_EVENT_CFG_REG_UPDATE_VIOLATION (1UL << 2)
60 #define EWDG_EVENT_REFRESH_UNLOCK_FAIL (1UL << 1)
61 #define EWDG_EVENT_REFRESH_VIOLATION (1UL << 0)
71 #define EWDG_INT_PARITY_FAIL (1UL << 2)
72 #define EWDG_INT_CTRL_REG_UNLOCK_FAIL (1UL << 4)
73 #define EWDG_INT_CTRL_REG_UPDATE_FAIL (1UL << 6)
74 #define EWDG_INT_TIMEOUT (1UL << 16)
75 #define EWDG_INT_REFRESH_UNLOCK_FAIL (1UL << 20)
76 #define EWDG_INT_REFRESH_VIOLATION (1UL << 22)
78 #define EWDG_INT_ALL (EWDG_INT_PARITY_FAIL | EWDG_INT_CTRL_REG_UNLOCK_FAIL | EWDG_INT_CTRL_REG_UPDATE_FAIL | \
79  EWDG_INT_TIMEOUT | EWDG_INT_REFRESH_UNLOCK_FAIL | EWDG_INT_REFRESH_VIOLATION)
90 #define EWDG_RST_PARITY_FAIL (1UL << 3)
91 #define EWDG_RST_CTRL_REG_UNLOCK_FAIL (1UL << 5)
92 #define EWDG_RST_CTRL_REG_UPDATE_FAIL (1UL << 7)
93 #define EWDG_RST_TIMEOUT (1UL << 17)
94 #define EWDG_RST_REFRESH_UNLOCK_FAIL (1UL << 21)
95 #define EWDG_RST_REFRESH_VIOLATION (1UL << 23)
97 #define EWDG_RST_ALL (EWDG_RST_PARITY_FAIL | EWDG_RST_CTRL_REG_UNLOCK_FAIL | EWDG_RST_CTRL_REG_UPDATE_FAIL | \
98  EWDG_RST_TIMEOUT | EWDG_RST_REFRESH_UNLOCK_FAIL | EWDG_RST_REFRESH_VIOLATION)
108 typedef enum {
120 
124 typedef enum {
128 
132 typedef enum {
144 
150 typedef enum {
162 
163 typedef enum {
167 
168 /***
169  * @brief EWDG Function Control Configurations
170  */
171 typedef struct {
179 
196  union {
197  struct {
199  uint32_t timeout_reset_us;
200  };
201  struct {
208  };
209  };
210 
217 
221 typedef struct {
235 
239 typedef struct {
243  uint32_t cnt_src_freq;
244 } ewdg_config_t;
245 
254 static inline bool ewdg_is_ctrl_reg_locked(EWDG_Type *ptr)
255 {
256  return ((ptr->CTRL0 & EWDG_CTRL0_CFG_LOCK_MASK) != 0U);
257 }
258 
266 static inline uint32_t ewdg_get_count_clk_divider(EWDG_Type *ptr)
267 {
268  return (1UL << EWDG_CTRL0_DIV_VALUE_GET(ptr->CTRL0));
269 }
270 
279 static inline bool ewdg_is_refresh_locked(EWDG_Type *ptr)
280 {
281  return ((ptr->CTRL0 & EWDG_CTRL0_REF_LOCK_MASK) != 0U);
282 }
283 
289 static inline void ewdg_unlock_ctrl_regs(EWDG_Type *ptr)
290 {
291  uint32_t ctrl_update_prot = ptr->CFG_PROT;
292  ptr->CFG_PROT = ctrl_update_prot;
293 }
294 
299 static inline void ewdg_write_refresh_reg(EWDG_Type *ptr)
300 {
302 }
303 
309 static inline uint32_t ewdg_get_timeout_reset_ticks(EWDG_Type *ptr)
310 {
311  return ptr->OT_RST_VAL;
312 }
313 
314 #if !defined(EWDG_SOC_SUPPORT_TIMEOUT_INTERRUPT) || (EWDG_SOC_SUPPORT_TIMEOUT_INTERRUPT == 1)
320 static inline uint32_t ewdg_get_timeout_interrupt_ticks(EWDG_Type *ptr)
321 {
322  return ptr->OT_INT_VAL;
323 }
324 #endif
325 
334 static inline void ewdg_clear_status_flags(EWDG_Type *ptr, uint32_t mask)
335 {
336  ptr->WDT_STATUS = mask;
337 }
338 
346 static inline uint32_t ewdg_get_status_flags(EWDG_Type *ptr)
347 {
348  return ptr->WDT_STATUS;
349 }
350 
357 {
359 }
360 
373 void ewdg_enable(EWDG_Type *ptr);
374 
375 
380 void ewdg_disable(EWDG_Type *ptr);
381 
392 hpm_stat_t ewdg_init_ctrl_func(EWDG_Type *ptr, ewdg_func_ctrl_config_t *config, uint32_t cnt_src_freq);
393 
404 
411 
422 
432 
442 
451 uint32_t ewdg_get_count_clock_freq(EWDG_Type *ptr, uint32_t src_clk_freq);
452 
461 uint64_t ewdg_convert_timeout_us_to_timeout_ticks(uint32_t src_clk_freq, uint32_t timeout_us);
462 
472 uint32_t ewdg_convert_timeout_ticks_to_timeout_us(EWDG_Type *ptr, uint32_t src_clk_freq, uint32_t timeout_ticks);
473 
479 void ewdg_enable_interrupt(EWDG_Type *ptr, uint32_t mask);
480 
486 void ewdg_disable_interrupt(EWDG_Type *ptr, uint32_t mask);
487 
493 void ewdg_enable_reset(EWDG_Type *ptr, uint32_t mask);
494 
500 void ewdg_disable_reset(EWDG_Type *ptr, uint32_t mask);
501 
508 
509 
510 #ifdef __cplusplus
511 }
512 #endif
513 
518 #endif /* HPM_EWDG_DRV_H */
#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
uint32_t hpm_stat_t
Definition: hpm_common.h:123
#define MAKE_STATUS(group, code)
Definition: hpm_common.h:132
@ status_group_ewdg
Definition: hpm_common.h:162
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:456
ewdg_refresh_unlock_method_t
EWDG Refresh Unlock Methods.
Definition: hpm_ewdg_drv.h:108
void ewdg_disable_interrupt(EWDG_Type *ptr, uint32_t mask)
Disable EWDG interrupt.
Definition: hpm_ewdg_drv.c:408
hpm_stat_t ewdg_init(EWDG_Type *ptr, ewdg_config_t *config)
Initialize the EWDG module.
Definition: hpm_ewdg_drv.c:278
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:367
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:357
ewdg_window_upper_limit_t
EWDG Upper Window Limitations.
Definition: hpm_ewdg_drv.h:150
ewdg_window_low_limit_t
EWDG Lower Window Limitations.
Definition: hpm_ewdg_drv.h:132
static uint32_t ewdg_get_count_clk_divider(EWDG_Type *ptr)
Get the Divider for Counter Clock.
Definition: hpm_ewdg_drv.h:266
void ewdg_disable_reset(EWDG_Type *ptr, uint32_t mask)
Disable EWDG Reset.
Definition: hpm_ewdg_drv.c:440
static void ewdg_unlock_ctrl_regs(EWDG_Type *ptr)
Unlock Write to Control Registers.
Definition: hpm_ewdg_drv.h:289
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:206
hpm_stat_t ewdg_unlock_refresh(EWDG_Type *ptr)
Unlock the write to refresh register.
Definition: hpm_ewdg_drv.c:306
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:378
static void ewdg_clear_status_flags(EWDG_Type *ptr, uint32_t mask)
Clear Interrupt Status for EWDG.
Definition: hpm_ewdg_drv.h:334
void ewdg_enable(EWDG_Type *ptr)
Enable EWDG.
Definition: hpm_ewdg_drv.c:474
static bool ewdg_is_ctrl_reg_locked(EWDG_Type *ptr)
Check whether the Control Registers are locked.
Definition: hpm_ewdg_drv.h:254
static bool ewdg_is_refresh_locked(EWDG_Type *ptr)
Check whether the Refresh register is locked.
Definition: hpm_ewdg_drv.h:279
static uint32_t ewdg_get_timeout_reset_ticks(EWDG_Type *ptr)
Get the Timeout Reset ticks.
Definition: hpm_ewdg_drv.h:309
void ewdg_disable(EWDG_Type *ptr)
Disable EWDG.
Definition: hpm_ewdg_drv.c:482
ewdg_low_power_mode_t
Definition: hpm_ewdg_drv.h:163
ewdg_cnt_clk_sel_t
EWDG Clock source for internal counter.
Definition: hpm_ewdg_drv.h:124
static void ewdg_write_refresh_reg(EWDG_Type *ptr)
Write Refresh Magic Number to EWDG Refresh register.
Definition: hpm_ewdg_drv.h:299
static ewdg_refresh_unlock_method_t ewdg_get_refresh_unlock_method(EWDG_Type *ptr)
Get the Refresh Unlock Mechanism.
Definition: hpm_ewdg_drv.h:356
static uint32_t ewdg_get_timeout_interrupt_ticks(EWDG_Type *ptr)
Get the Timeout Interrupt ticks.
Definition: hpm_ewdg_drv.h:320
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:348
static uint32_t ewdg_get_status_flags(EWDG_Type *ptr)
Get the Status of EWDG.
Definition: hpm_ewdg_drv.h:346
void ewdg_enable_reset(EWDG_Type *ptr, uint32_t mask)
Enable EWDG Reset.
Definition: hpm_ewdg_drv.c:424
void ewdg_enable_interrupt(EWDG_Type *ptr, uint32_t mask)
Enable EWDG interrupt.
Definition: hpm_ewdg_drv.c:392
@ ewdg_refresh_unlock_method_ring_left_shift_password_by_1_bit0_xor_password_bit0
Definition: hpm_ewdg_drv.h:116
@ ewdg_refresh_unlock_method_fixed_key
Definition: hpm_ewdg_drv.h:114
@ ewdg_refresh_unlock_method_password
Definition: hpm_ewdg_drv.h:110
@ ewdg_refresh_unlock_method_ring_left_shift_password_by_1
Definition: hpm_ewdg_drv.h:112
@ ewdg_refresh_unlock_method_max
Definition: hpm_ewdg_drv.h:118
@ ewdg_window_upper_timeout_period_4_div_16
Definition: hpm_ewdg_drv.h:155
@ ewdg_window_upper_timeout_period_7_div_16
Definition: hpm_ewdg_drv.h:158
@ ewdg_window_upper_timeout_period_1_div_16
Definition: hpm_ewdg_drv.h:152
@ ewdg_window_upper_timeout_period_2_div_16
Definition: hpm_ewdg_drv.h:153
@ ewdg_window_upper_timeout_period_6_div_16
Definition: hpm_ewdg_drv.h:157
@ ewdg_window_upper_timeout_period_8_div_16
Definition: hpm_ewdg_drv.h:151
@ ewdg_window_upper_timeout_period_3_div_16
Definition: hpm_ewdg_drv.h:154
@ ewdg_window_upper_timeout_period_max
Definition: hpm_ewdg_drv.h:160
@ ewdg_window_upper_timeout_period_5_div_16
Definition: hpm_ewdg_drv.h:156
@ ewdg_window_lower_timeout_period_8_div_16
Definition: hpm_ewdg_drv.h:134
@ ewdg_window_lower_timeout_period_14_div_16
Definition: hpm_ewdg_drv.h:140
@ ewdg_window_lower_timeout_period_max
Definition: hpm_ewdg_drv.h:142
@ ewdg_window_lower_timeout_period_10_div_16
Definition: hpm_ewdg_drv.h:136
@ ewdg_window_lower_timeout_period_12_div_16
Definition: hpm_ewdg_drv.h:138
@ status_ewdg_feature_unsupported
Definition: hpm_ewdg_drv.h:32
@ status_ewdg_div_out_of_range
Definition: hpm_ewdg_drv.h:31
@ status_ewdg_tick_out_of_range
Definition: hpm_ewdg_drv.h:30
@ ewdg_low_power_mode_halt
Definition: hpm_ewdg_drv.h:164
@ ewdg_low_power_mode_work_clock_normal
Definition: hpm_ewdg_drv.h:165
@ ewdg_cnt_clk_src_bus_clk
Definition: hpm_ewdg_drv.h:125
@ ewdg_cnt_clk_src_ext_osc_clk
Definition: hpm_ewdg_drv.h:126
#define EWDG_REFRESH_KEY
Definition: hpm_ewdg_drv.h:44
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 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
__W uint32_t WDT_STATUS
Definition: hpm_ewdg_regs.h:20
Enhanced Watchdog Configuration Structure.
Definition: hpm_ewdg_drv.h:239
uint32_t cnt_src_freq
Definition: hpm_ewdg_drv.h:243
bool enable_watchdog
Definition: hpm_ewdg_drv.h:242
ewdg_func_ctrl_config_t ctrl_config
Definition: hpm_ewdg_drv.h:241
ewdg_interrupt_reset_config_t int_rst_config
Definition: hpm_ewdg_drv.h:240
Definition: hpm_ewdg_drv.h:171
bool enable_config_lock
Definition: hpm_ewdg_drv.h:180
bool keep_running_in_debug_mode
Definition: hpm_ewdg_drv.h:188
uint32_t timeout_reset_val
Definition: hpm_ewdg_drv.h:206
ewdg_refresh_unlock_method_t refresh_unlock_method
Definition: hpm_ewdg_drv.h:184
uint32_t timeout_interrupt_val
Definition: hpm_ewdg_drv.h:202
bool enable_window_mode
Definition: hpm_ewdg_drv.h:173
bool use_lowlevel_timeout
Definition: hpm_ewdg_drv.h:195
ewdg_cnt_clk_sel_t cnt_clk_sel
Definition: hpm_ewdg_drv.h:172
uint32_t clock_div_by_power_of_2
Definition: hpm_ewdg_drv.h:207
uint32_t timeout_interrupt_us
Definition: hpm_ewdg_drv.h:198
uint32_t timeout_reset_us
Definition: hpm_ewdg_drv.h:199
bool enable_refresh_lock
Definition: hpm_ewdg_drv.h:183
bool enable_refresh_period
Definition: hpm_ewdg_drv.h:182
uint16_t refresh_period_in_bus_cycles
Definition: hpm_ewdg_drv.h:211
ewdg_window_low_limit_t window_lower_limit
Definition: hpm_ewdg_drv.h:174
bool enable_overtime_self_clear
Definition: hpm_ewdg_drv.h:186
uint16_t ctrl_reg_update_period_bus_clk_x_128
Definition: hpm_ewdg_drv.h:215
uint16_t ctrl_reg_update_password
Definition: hpm_ewdg_drv.h:214
ewdg_low_power_mode_t low_power_mode
Definition: hpm_ewdg_drv.h:189
ewdg_window_upper_limit_t window_upper_limit
Definition: hpm_ewdg_drv.h:178
uint16_t refresh_unlock_password
Definition: hpm_ewdg_drv.h:212
EWDG Reset and Interrupt Configurations.
Definition: hpm_ewdg_drv.h:221
bool enable_ctrl_unlock_fail_interrupt
Definition: hpm_ewdg_drv.h:224
bool enable_refresh_unlock_fail_interrupt
Definition: hpm_ewdg_drv.h:230
bool enable_ctrl_update_violation_reset
Definition: hpm_ewdg_drv.h:227
bool enable_ctrl_unlock_fail_reset
Definition: hpm_ewdg_drv.h:225
bool enable_refresh_violation_interrupt
Definition: hpm_ewdg_drv.h:232
bool enable_ctrl_parity_fail_interrupt
Definition: hpm_ewdg_drv.h:222
bool enable_refresh_violation_reset
Definition: hpm_ewdg_drv.h:233
bool enable_timeout_reset
Definition: hpm_ewdg_drv.h:229
bool enable_timeout_interrupt
Definition: hpm_ewdg_drv.h:228
bool enable_refresh_unlock_fail_reset
Definition: hpm_ewdg_drv.h:231
bool enable_ctrl_update_violation_interrupt
Definition: hpm_ewdg_drv.h:226
bool enable_ctrl_parity_fail_reset
Definition: hpm_ewdg_drv.h:223