1 /*****************************************************************************
2  * Copyright (c) 2019, Nations Technologies Inc.
3  *
4  * All rights reserved.
5  * ****************************************************************************
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * - Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the disclaimer below.
12  *
13  * Nations' name may not be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
19  * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  * ****************************************************************************/
27 
28 /**
29  * @file n32wb452_iwdg.c
30  * @author Nations
31  * @version v1.0.0
32  *
33  * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
34  */
35 #include "n32wb452_iwdg.h"
36 
37 /** @addtogroup N32WB452_StdPeriph_Driver
38  * @{
39  */
40 
41 /** @addtogroup IWDG
42  * @brief IWDG driver modules
43  * @{
44  */
45 
46 /** @addtogroup IWDG_Private_TypesDefinitions
47  * @{
48  */
49 
50 /**
51  * @}
52  */
53 
54 /** @addtogroup IWDG_Private_Defines
55  * @{
56  */
57 
58 /* ---------------------- IWDG registers bit mask ----------------------------*/
59 
60 /* KEY register bit mask */
61 #define KEY_ReloadKey ((uint16_t)0xAAAA)
62 #define KEY_EnableKey ((uint16_t)0xCCCC)
63 
64 /**
65  * @}
66  */
67 
68 /** @addtogroup IWDG_Private_Macros
69  * @{
70  */
71 
72 /**
73  * @}
74  */
75 
76 /** @addtogroup IWDG_Private_Variables
77  * @{
78  */
79 
80 /**
81  * @}
82  */
83 
84 /** @addtogroup IWDG_Private_FunctionPrototypes
85  * @{
86  */
87 
88 /**
89  * @}
90  */
91 
92 /** @addtogroup IWDG_Private_Functions
93  * @{
94  */
95 
96 /**
97  * @brief  Enables or disables write access to IWDG_PR and IWDG_RLR registers.
98  * @param IWDG_WriteAccess new state of write access to IWDG_PR and IWDG_RLR registers.
99  *   This parameter can be one of the following values:
100  *     @arg IWDG_WRITE_ENABLE Enable write access to IWDG_PR and IWDG_RLR registers
101  *     @arg IWDG_WRITE_DISABLE Disable write access to IWDG_PR and IWDG_RLR registers
102  */
IWDG_WriteConfig(uint16_t IWDG_WriteAccess)103 void IWDG_WriteConfig(uint16_t IWDG_WriteAccess)
104 {
105     /* Check the parameters */
106     assert_param(IS_IWDG_WRITE(IWDG_WriteAccess));
107     IWDG->KEY = IWDG_WriteAccess;
108 }
109 
110 /**
111  * @brief  Sets IWDG Prescaler value.
112  * @param IWDG_Prescaler specifies the IWDG Prescaler value.
113  *   This parameter can be one of the following values:
114  *     @arg IWDG_PRESCALER_DIV4 IWDG prescaler set to 4
115  *     @arg IWDG_PRESCALER_DIV8 IWDG prescaler set to 8
116  *     @arg IWDG_PRESCALER_DIV16 IWDG prescaler set to 16
117  *     @arg IWDG_PRESCALER_DIV32 IWDG prescaler set to 32
118  *     @arg IWDG_PRESCALER_DIV64 IWDG prescaler set to 64
119  *     @arg IWDG_PRESCALER_DIV128 IWDG prescaler set to 128
120  *     @arg IWDG_PRESCALER_DIV256 IWDG prescaler set to 256
121  */
IWDG_SetPrescalerDiv(uint8_t IWDG_Prescaler)122 void IWDG_SetPrescalerDiv(uint8_t IWDG_Prescaler)
123 {
124     /* Check the parameters */
125     assert_param(IS_IWDG_PRESCALER_DIV(IWDG_Prescaler));
126     IWDG->PREDIV = IWDG_Prescaler;
127 }
128 
129 /**
130  * @brief  Sets IWDG Reload value.
131  * @param Reload specifies the IWDG Reload value.
132  *   This parameter must be a number between 0 and 0x0FFF.
133  */
IWDG_CntReload(uint16_t Reload)134 void IWDG_CntReload(uint16_t Reload)
135 {
136     /* Check the parameters */
137     assert_param(IS_IWDG_RELOAD(Reload));
138     IWDG->RELV = Reload;
139 }
140 
141 /**
142  * @brief  Reloads IWDG counter with value defined in the reload register
143  *   (write access to IWDG_PR and IWDG_RLR registers disabled).
144  */
IWDG_ReloadKey(void)145 void IWDG_ReloadKey(void)
146 {
147     IWDG->KEY = KEY_ReloadKey;
148 }
149 
150 /**
151  * @brief  Enables IWDG (write access to IWDG_PR and IWDG_RLR registers disabled).
152  */
IWDG_Enable(void)153 void IWDG_Enable(void)
154 {
155     IWDG->KEY = KEY_EnableKey;
156 }
157 
158 /**
159  * @brief  Checks whether the specified IWDG flag is set or not.
160  * @param IWDG_FLAG specifies the flag to check.
161  *   This parameter can be one of the following values:
162  *     @arg IWDG_PVU_FLAG Prescaler Value Update on going
163  *     @arg IWDG_CRVU_FLAG Reload Value Update on going
164  * @return The new state of IWDG_FLAG (SET or RESET).
165  */
IWDG_GetStatus(uint16_t IWDG_FLAG)166 FlagStatus IWDG_GetStatus(uint16_t IWDG_FLAG)
167 {
168     FlagStatus bitstatus = RESET;
169     /* Check the parameters */
170     assert_param(IS_IWDG_FLAG(IWDG_FLAG));
171     if ((IWDG->STS & IWDG_FLAG) != (uint32_t)RESET)
172     {
173         bitstatus = SET;
174     }
175     else
176     {
177         bitstatus = RESET;
178     }
179     /* Return the flag status */
180     return bitstatus;
181 }
182 
183 /**
184  * @}
185  */
186 
187 /**
188  * @}
189  */
190 
191 /**
192  * @}
193  */
194