1 /**
2   ******************************************************************************
3   * @file    tae32f53xx_ll_iwdg.c
4   * @author  MCD Application Team
5   * @brief   IWDG LL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of the Independent Watchdog (IWDG) peripheral:
8   *           + Initialization and De-Initialization functions
9   *           + Refresh function
10   *           + Interrupt and Callback functions
11   *
12   ******************************************************************************
13   * @attention
14   *
15   * <h2><center>&copy; Copyright (c) 2020 Tai-Action.
16   * All rights reserved.</center></h2>
17   *
18   * This software is licensed by Tai-Action under BSD 3-Clause license,
19   * the "License"; You may not use this file except in compliance with the
20   * License. You may obtain a copy of the License at:
21   *                        opensource.org/licenses/BSD-3-Clause
22   *
23   ******************************************************************************
24   */
25 
26 /* Includes ------------------------------------------------------------------*/
27 #include "tae32f53xx_ll.h"
28 
29 
30 #define DBG_TAG             "IWDG LL"
31 #define DBG_LVL             DBG_ERROR
32 #include "dbg/tae32f53xx_dbg.h"
33 
34 
35 /** @addtogroup TAE32F53xx_LL_Driver
36   * @{
37   */
38 
39 /** @defgroup IWDG_LL IWDG LL
40   * @brief    IWDG LL module driver.
41   * @{
42   */
43 
44 #ifdef LL_IWDG_MODULE_ENABLED
45 
46 /* Private typedef -----------------------------------------------------------*/
47 /* Private define ------------------------------------------------------------*/
48 /** @defgroup IWDG_LL_Private_Defines IWDG LL Private Defines
49   * @brief    IWDG LL Private Defines
50   * @{
51   */
52 
53 /**
54   * @brief Max delay time for IWDG status register update
55   */
56 #define IWDG_TIMEOUT_VALUE                          100U
57 /**
58   * @}
59   */
60 
61 
62 /* Private macro -------------------------------------------------------------*/
63 /* Private variables ---------------------------------------------------------*/
64 /* Private function prototypes -----------------------------------------------*/
65 /* Exported functions --------------------------------------------------------*/
66 /** @defgroup IWDG_LL_Exported_Functions IWDG LL Exported Functions
67   * @brief    IWDG LL Exported Functions
68   * @{
69   */
70 
71 /** @defgroup IWDG_LL_Exported_Functions_Group1 Initialization and De-Initialization functions
72   * @brief    Initialization and De-Initialization functions
73 @verbatim
74   ===============================================================================
75             ##### Initialization and de-initialization functions #####
76   ===============================================================================
77   [..]  This section provides a set of functions allowing to initialize and
78         deinitialize the IWDG peripheral
79 
80 @endverbatim
81   * @{
82   */
83 
84 /**
85   * @brief  Initializes the IWDG peripheral according to the specified parameters in the Init.
86   * @param  Instance IWDG peripheral instance
87   * @param  Init pointer to a IWDG_InitTypeDef structure that contains the configuration information
88   *         for the specified IWDG peripheral.
89   * @return status of the initialization
90   */
LL_IWDG_Init(IWDG_TypeDef * Instance,IWDG_InitTypeDef * Init)91 LL_StatusETypeDef LL_IWDG_Init(IWDG_TypeDef *Instance, IWDG_InitTypeDef *Init)
92 {
93     uint32_t tickstart = 0;
94 
95     /* Check the IWDG initiation struct allocation */
96     if (Init == NULL) {
97         return LL_ERROR;
98     }
99 
100     /* Check the parameters */
101     assert_param(IS_IWDG_ALL_INSTANCE(Instance));
102     assert_param(IS_IWDG_RELOAD_Val(Init->Reload_val));
103 
104     /* Handle Something */
105     LL_IWDG_MspInit(Instance);
106 
107     /* Start IWDG to work */
108     __LL_IWDG_START(Instance);
109 
110     /* Enable write access to IWDG_PSCR, IWDG_RLR and IWDG_CR registers */
111     __LL_IWDG_ENABLE_WRITE_ACCESS(Instance);
112 
113     /* PSCUPD must be 0 before writting prescaler settings to IWDG_PSCR register */
114     tickstart = LL_GetTick();
115 
116     while (__LL_IWDG_GET_FLAG(Instance, IWDG_FLAG_PSCUPD) != RESET) {
117         if ((LL_GetTick() - tickstart) > IWDG_TIMEOUT_VALUE) {
118             return LL_TIMEOUT;
119         }
120     }
121 
122     /* Write to IWDG registers the Prescaler to work with */
123     WRITE_REG(Instance->PSCR, Init->Prescaler);
124 
125     /* RLVUPD must be 0 before writting reload values to IWDG_RLR register */
126     tickstart = LL_GetTick();
127 
128     while (__LL_IWDG_GET_FLAG(Instance, IWDG_FLAG_RLVUPD) != RESET) {
129         if ((LL_GetTick() - tickstart) > IWDG_TIMEOUT_VALUE) {
130             return LL_TIMEOUT;
131         }
132     }
133 
134     /* Write to IWDG registers the Reload values to work with */
135     WRITE_REG(Instance->RLR, Init->Reload_val);
136 
137     /* Configure the IWDG bahavior after timeout */
138     MODIFY_REG(Instance->CR, IWDG_CR_MODE, Init->Mode);
139 
140     /* Enable IWDG interrupt when using interrupt mode */
141     if (Init->Mode == IWDG_MODE_INTERRUPT) {
142         __LL_IWDG_ENABLE_IT(Instance, IWDG_IT_TOIE);
143     }
144 
145     /* Disable write access to IWDG_PSCR, IWDG_RLR and IWDG_CR registers */
146     __LL_IWDG_DISABLE_WRITE_ACCESS(Instance);
147 
148     /* Reload IWDG counter with value defined in the reload register */
149     __LL_IWDG_RELOAD_COUNTER(Instance);
150 
151     /* Return function status */
152     return LL_OK;
153 }
154 
155 /**
156   * @brief  De-initializes the IWDG peripheral.
157   * @param  Instance IWDG peripheral
158   * @return status of the de-initialization
159   */
LL_IWDG_DeInit(IWDG_TypeDef * Instance)160 LL_StatusETypeDef LL_IWDG_DeInit(IWDG_TypeDef *Instance)
161 {
162     /* Check the parameters */
163     assert_param(IS_IWDG_ALL_INSTANCE(Instance));
164 
165     /* Enable write access to IWDG_PSCR, IWDG_RLR and IWDG_CR registers */
166     __LL_IWDG_ENABLE_WRITE_ACCESS(Instance);
167 
168     /* Disable the IWDG peripheral. */
169     __LL_IWDG_STOP(Instance);
170 
171     /* Disable write access to IWDG_PSCR, IWDG_RLR and IWDG_CR registers */
172     __LL_IWDG_DISABLE_WRITE_ACCESS(Instance);
173 
174     /* Handle Something */
175     LL_IWDG_MspDeInit(Instance);
176 
177     /* Return function status */
178     return LL_OK;
179 }
180 
181 /**
182   * @brief  Initializes the IWDG MSP.
183   * @param  Instance IWDG peripheral
184   * @return None
185   */
LL_IWDG_MspInit(IWDG_TypeDef * Instance)186 __WEAK void LL_IWDG_MspInit(IWDG_TypeDef *Instance)
187 {
188     /* Prevent unused argument(s) compilation warning */
189     LL_UNUSED(Instance);
190 
191     /* NOTE: This function should not be modified, when the callback is needed,
192              the LL_IWDG_MspInit could be implemented in the user file
193      */
194 }
195 
196 /**
197   * @brief  DeInitializes the IWDG MSP
198   * @param  Instance IWDG peripheral
199   * @return None
200   */
LL_IWDG_MspDeInit(IWDG_TypeDef * Instance)201 __WEAK void LL_IWDG_MspDeInit(IWDG_TypeDef *Instance)
202 {
203     /* Prevent unused argument(s) compilation warning */
204     LL_UNUSED(Instance);
205 
206     /* NOTE: This function should not be modified, when the callback is needed,
207              the LL_IWDG_MspDeInit could be implemented in the user file
208      */
209 }
210 
211 /**
212   * @}
213   */
214 
215 
216 /** @defgroup IWDG_LL_Exported_Functions_Group2 IWDG Input and Output operation functions
217   * @brief    IWDG Input and Output operation functions
218 @verbatim
219   ===============================================================================
220                   ##### Input and Output operation functions #####
221   ===============================================================================
222   [..]
223     This section provides functions allowing to:
224     (+) Refresh the IWDG.
225 
226 @endverbatim
227   * @{
228   */
229 
230 /**
231   * @brief  Refresh the IWDG.
232   * @param  Instance: IWDG peripheral
233   * @return LL_Status
234   */
LL_IWDG_Refresh(IWDG_TypeDef * Instance)235 LL_StatusETypeDef LL_IWDG_Refresh(IWDG_TypeDef *Instance)
236 {
237     uint32_t tickstart = 0;
238     /* Check the parameters */
239     assert_param(IS_IWDG_ALL_INSTANCE(Instance));
240 
241     /* Notice that RLVUPD and PSCUPD must be 0 before refreshing IWDG counter */
242     while ((__LL_IWDG_GET_FLAG(Instance, IWDG_FLAG_RLVUPD) != RESET) || (__LL_IWDG_GET_FLAG(Instance, IWDG_FLAG_PSCUPD) != RESET)) {
243         if ((LL_GetTick() - tickstart) > IWDG_TIMEOUT_VALUE) {
244             return LL_TIMEOUT;
245         }
246     }
247 
248     /* Reload IWDG counter with value defined in the IWDG_RLR register */
249     __LL_IWDG_RELOAD_COUNTER(Instance);
250 
251     /* Return function status */
252     return LL_OK;
253 }
254 
255 /**
256   * @}
257   */
258 
259 
260 /** @defgroup IWDG_LL_Exported_Functions_Interrupt IWDG Initerrupt management
261   * @brief    IWDG Initerrupt management
262 @verbatim
263   ===============================================================================
264                           ##### Initerrupt management #####
265   ===============================================================================
266   [..]
267       This section provides IWDG interrupt handler and callback functions.
268 
269 @endverbatim
270   * @{
271   */
272 
273 /**
274   * @brief  This function handles IWDG interrupts requests.
275   * @param  Instance: IWDG peripheral
276   * @return None
277   */
LL_IWDG_IRQHandler(IWDG_TypeDef * Instance)278 void LL_IWDG_IRQHandler(IWDG_TypeDef *Instance)
279 {
280     if ((__LL_IWDG_IT_CHECK_SOURCE(Instance, IWDG_IT_TOIE) != RESET) && (__LL_IWDG_GET_FLAG(Instance, IWDG_FLAG_TOIF) != RESET)) {
281         /* Clear the TOIF interrupt flag */
282         __LL_IWDG_CLEAR_FLAG(Instance, IWDG_FLAG_TOIF);
283 
284         /* IWDG timeout callback */
285         LL_IWDG_TimeOutCallBack(Instance);
286     }
287 }
288 
289 /**
290   * @brief  Timeout callback
291   * @param  Instance IWDG peripheral
292   * @return None
293   */
LL_IWDG_TimeOutCallBack(IWDG_TypeDef * Instance)294 __WEAK void LL_IWDG_TimeOutCallBack(IWDG_TypeDef *Instance)
295 {
296     /* Prevent unused argument(s) compilation warning */
297     LL_UNUSED(Instance);
298 
299     /* NOTE: This function should not be modified, when the callback is needed,
300              the LL_IWDG_TimeOutCallBack could be implemented in the user file
301      */
302 }
303 
304 /**
305   * @}
306   */
307 
308 /**
309   * @}
310   */
311 
312 
313 /* Private functions ---------------------------------------------------------*/
314 
315 
316 #endif /* LL_IWDG_MODULE_ENABLED */
317 
318 
319 /**
320   * @}
321   */
322 
323 /**
324   * @}
325   */
326 
327 
328 /************************* (C) COPYRIGHT Tai-Action *****END OF FILE***********/
329 
330