1 /*
2  * Copyright 2021 MindMotion Microelectronics Co., Ltd.
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef __HAL_IWDG_H__
9 #define __HAL_IWDG_H__
10 
11 #include "hal_common.h"
12 
13 /*!
14  * @addtogroup IWDG
15  * @{
16  */
17 
18 /*!
19  * @brief IWDG driver version number.
20  */
21 #define IWDG_DRIVER_VERSION 0u /*!< IWDG_0. */
22 
23 /*!
24  * @brief IWDG key values
25  */
26 #define IWDG_KEY_ENABLE  0xCCCC /*!< IWDG Peripheral Enable . */
27 #define IWDG_KEY_RELOAD  0xAAAA /*!< IWDG Reload Counter Enable. */
28 #define IWDG_KEY_UNLOCK  0x5555 /*!< IWDG KR Write Access Enable*/
29 
30 /*!
31  * @addtogroup IWDG_STATUS
32  * @{
33  */
34 #define IWDG_STATUS_RELOAD_UPDATE     IWDG_SR_RVU_MASK           /*!< Status flag when IWDG counter reload value update, can not be cleared. */
35 #define IWDG_STATUS_PRESCALER_UPDATE  IWDG_SR_PVU_MASK           /*!< Status flag when IWDG prescaler value update, can not be cleared. */
36 #define IWDG_STATUS_ALMOST_TIMEOUT    IWDG_SR_IVU_MASK           /*!< Status flag when IWDG almost timeout. */
37 /*!
38  * @}
39  */
40 
41 /*!
42  * @addtogroup IWDG_INT
43  * @{
44  */
45 #define IWDG_INT_ALMOST_TIMEOUT  IWDG_CR_IRQSEL_MASK           /*!< IWDG Interrupt: Select IWDG interrupt method */
46 /*!
47  * @}
48  */
49 
50 /*!
51  * @brief IWDG prescaler type.
52  *
53  * Select the prescaler of time base as the IWDG counter clock.
54  */
55 typedef enum
56 {
57     IWDG_Prescaler_4   = 0u, /*!< IWDG prescaler 4. */
58     IWDG_Prescaler_8   = 1u, /*!< IWDG prescaler 8. */
59     IWDG_Prescaler_16  = 2u, /*!< IWDG prescaler 16. */
60     IWDG_Prescaler_32  = 3u, /*!< IWDG prescaler 32. */
61     IWDG_Prescaler_64  = 4u, /*!< IWDG prescaler 64. */
62     IWDG_Prescaler_128 = 5u, /*!< IWDG prescaler 128. */
63     IWDG_Prescaler_256 = 6u, /*!< IWDG prescaler 256. */
64 } IWDG_Prescaler_Type;
65 
66 /*!
67  * @brief This type of structure instance is used to keep the settings when calling the @ref IWDG_Init() to initialize the IWDG module.
68  */
69 typedef struct
70 {
71     IWDG_Prescaler_Type  Prescaler; /*!< Specify the IWDG counter time base division value. */
72     uint32_t             Relaod;    /*!< Specify the IWDG counter reload value. */
73 } IWDG_Init_Type;
74 
75 /*!
76  * @brief Initialize the IWDG module.
77  *
78  * @param IWDGx IWDG instance.
79  * @param init Pointer to the initialization structure. See to @ref IWDG_Init_Type.
80  * @return None.
81  */
82 void IWDG_Init(IWDG_Type * IWDGx, IWDG_Init_Type * init);
83 
84 /*!
85  * @brief Start the IWDG counter.
86  *
87  * Once IWDG counter is started, it cannot be stopped unless reset MCU.
88  *
89  * @param IWDGx IWDG instance.
90  * @return None.
91  */
92 void IWDG_Start(IWDG_Type * IWDGx);
93 
94 /*!
95  * @brief Get the current status flags of the IWDG module.
96  *
97  * @param IWDGx IWDG instance.
98  * @return Status flags. See to @ref IWDG_STATUS.
99  */
100 uint32_t IWDG_GetStatus(IWDG_Type * IWDGx);
101 
102 /*!
103  * @brief Clear the status flags of the IWDG module.
104  *
105  * @param IWDGx IWDG instance.
106  * @param status Status flags. See to @ref IWDG_STATUS.
107  * @return None.
108  */
109 void IWDG_ClearStatus(IWDG_Type * IWDGx, uint32_t status);
110 
111 /*!
112  * @brief Do reload IWDG counter (i.e. "feed dog").
113  *
114  * @param IWDGx IWDG instance.
115  * @return None.
116  */
117 void IWDG_DoReload(IWDG_Type * IWDGx);
118 
119 /*!
120  * @brief Enable interrupts of the IWDG module.
121  *
122  * @param IWDGx IWDG instance.
123  * @param interrupts Interrupt code masks. See to @ref IWDG_INT.
124  * @param enable 'true' to enable the indicated interrupts, 'false' has no effect.
125  * @return None.
126  */
127 void IWDG_EnableInterrupts(IWDG_Type * IWDGx, uint32_t interrupts, bool enable);
128 
129 /*!
130  *@}
131  */
132 
133 #endif /* __HAL_IWDG_H__ */
134