1 /* Includes ------------------------------------------------------------------*/
2 #include "air32f10x_wwdg.h"
3 #include "air32f10x_rcc.h"
4 
5 /** @addtogroup STM32F10x_StdPeriph_Driver
6   * @{
7   */
8 
9 /** @defgroup WWDG
10   * @brief WWDG driver modules
11   * @{
12   */
13 
14 /** @defgroup WWDG_Private_TypesDefinitions
15   * @{
16   */
17 
18 /**
19   * @}
20   */
21 
22 /** @defgroup WWDG_Private_Defines
23   * @{
24   */
25 
26 /* ----------- WWDG registers bit address in the alias region ----------- */
27 #define WWDG_OFFSET       (WWDG_BASE - PERIPH_BASE)
28 
29 /* Alias word address of EWI bit */
30 #define CFR_OFFSET        (WWDG_OFFSET + 0x04)
31 #define EWI_BitNumber     0x09
32 #define CFR_EWI_BB        (PERIPH_BB_BASE + (CFR_OFFSET * 32) + (EWI_BitNumber * 4))
33 
34 /* --------------------- WWDG registers bit mask ------------------------ */
35 
36 /* CR register bit mask */
37 #define CR_WDGA_Set       ((uint32_t)0x00000080)
38 
39 /* CFR register bit mask */
40 #define CFR_WDGTB_Mask    ((uint32_t)0xFFFFFE7F)
41 #define CFR_W_Mask        ((uint32_t)0xFFFFFF80)
42 #define BIT_Mask          ((uint8_t)0x7F)
43 
44 /**
45   * @}
46   */
47 
48 /** @defgroup WWDG_Private_Macros
49   * @{
50   */
51 
52 /**
53   * @}
54   */
55 
56 /** @defgroup WWDG_Private_Variables
57   * @{
58   */
59 
60 /**
61   * @}
62   */
63 
64 /** @defgroup WWDG_Private_FunctionPrototypes
65   * @{
66   */
67 
68 /**
69   * @}
70   */
71 
72 /** @defgroup WWDG_Private_Functions
73   * @{
74   */
75 
76 /**
77   * @brief  Deinitializes the WWDG peripheral registers to their default reset values.
78   * @param  None
79   * @retval None
80   */
WWDG_DeInit(void)81 void WWDG_DeInit(void)
82 {
83   RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG, ENABLE);
84   RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG, DISABLE);
85 }
86 
87 /**
88   * @brief  Sets the WWDG Prescaler.
89   * @param  WWDG_Prescaler: specifies the WWDG Prescaler.
90   *   This parameter can be one of the following values:
91   *     @arg WWDG_Prescaler_1: WWDG counter clock = (PCLK1/4096)/1
92   *     @arg WWDG_Prescaler_2: WWDG counter clock = (PCLK1/4096)/2
93   *     @arg WWDG_Prescaler_4: WWDG counter clock = (PCLK1/4096)/4
94   *     @arg WWDG_Prescaler_8: WWDG counter clock = (PCLK1/4096)/8
95   * @retval None
96   */
WWDG_SetPrescaler(uint32_t WWDG_Prescaler)97 void WWDG_SetPrescaler(uint32_t WWDG_Prescaler)
98 {
99   uint32_t tmpreg = 0;
100   /* Check the parameters */
101   assert_param(IS_WWDG_PRESCALER(WWDG_Prescaler));
102   /* Clear WDGTB[1:0] bits */
103   tmpreg = WWDG->CFR & CFR_WDGTB_Mask;
104   /* Set WDGTB[1:0] bits according to WWDG_Prescaler value */
105   tmpreg |= WWDG_Prescaler;
106   /* Store the new value */
107   WWDG->CFR = tmpreg;
108 }
109 
110 /**
111   * @brief  Sets the WWDG window value.
112   * @param  WindowValue: specifies the window value to be compared to the downcounter.
113   *   This parameter value must be lower than 0x80.
114   * @retval None
115   */
WWDG_SetWindowValue(uint8_t WindowValue)116 void WWDG_SetWindowValue(uint8_t WindowValue)
117 {
118   __IO uint32_t tmpreg = 0;
119 
120   /* Check the parameters */
121   assert_param(IS_WWDG_WINDOW_VALUE(WindowValue));
122   /* Clear W[6:0] bits */
123 
124   tmpreg = WWDG->CFR & CFR_W_Mask;
125 
126   /* Set W[6:0] bits according to WindowValue value */
127   tmpreg |= WindowValue & (uint32_t) BIT_Mask;
128 
129   /* Store the new value */
130   WWDG->CFR = tmpreg;
131 }
132 
133 /**
134   * @brief  Enables the WWDG Early Wakeup interrupt(EWI).
135   * @param  None
136   * @retval None
137   */
WWDG_EnableIT(void)138 void WWDG_EnableIT(void)
139 {
140   *(__IO uint32_t *) CFR_EWI_BB = (uint32_t)ENABLE;
141 }
142 
143 /**
144   * @brief  Sets the WWDG counter value.
145   * @param  Counter: specifies the watchdog counter value.
146   *   This parameter must be a number between 0x40 and 0x7F.
147   * @retval None
148   */
WWDG_SetCounter(uint8_t Counter)149 void WWDG_SetCounter(uint8_t Counter)
150 {
151   /* Check the parameters */
152   assert_param(IS_WWDG_COUNTER(Counter));
153   /* Write to T[6:0] bits to configure the counter value, no need to do
154      a read-modify-write; writing a 0 to WDGA bit does nothing */
155   WWDG->CR = Counter & BIT_Mask;
156 }
157 
158 /**
159   * @brief  Enables WWDG and load the counter value.
160   * @param  Counter: specifies the watchdog counter value.
161   *   This parameter must be a number between 0x40 and 0x7F.
162   * @retval None
163   */
WWDG_Enable(uint8_t Counter)164 void WWDG_Enable(uint8_t Counter)
165 {
166   /* Check the parameters */
167   assert_param(IS_WWDG_COUNTER(Counter));
168   WWDG->CR = CR_WDGA_Set | Counter;
169 }
170 
171 /**
172   * @brief  Checks whether the Early Wakeup interrupt flag is set or not.
173   * @param  None
174   * @retval The new state of the Early Wakeup interrupt flag (SET or RESET)
175   */
WWDG_GetFlagStatus(void)176 FlagStatus WWDG_GetFlagStatus(void)
177 {
178   return (FlagStatus)(WWDG->SR);
179 }
180 
181 /**
182   * @brief  Clears Early Wakeup interrupt flag.
183   * @param  None
184   * @retval None
185   */
WWDG_ClearFlag(void)186 void WWDG_ClearFlag(void)
187 {
188   WWDG->SR = (uint32_t)RESET;
189 }
190 
191 /**
192   * @}
193   */
194 
195 /**
196   * @}
197   */
198 
199 /**
200   * @}
201   */
202