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_bkp.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_bkp.h"
36 
37 /** @addtogroup N32WB452_StdPeriph_Driver
38  * @{
39  */
40 
41 /** @addtogroup BKP
42  * @brief BKP driver modules
43  * @{
44  */
45 
46 /** @addtogroup BKP_Private_TypesDefinitions
47  * @{
48  */
49 
50 /**
51  * @}
52  */
53 
54 /** @addtogroup BKP_Private_Defines
55  * @{
56  */
57 
58 /* ------------ BKP registers bit address in the alias region --------------- */
59 #define BKP_OFFSET (BKP_BASE - PERIPH_BASE)
60 
61 /* --- CTRL Register ----*/
62 
63 /* Alias word address of TP_ALEV bit */
64 #define CTRL_OFFSET     (BKP_OFFSET + 0x30)
65 #define TP_ALEV_BIT     0x01
66 #define CTRL_TP_ALEV_BB (PERIPH_BB_BASE + (CTRL_OFFSET * 32) + (TP_ALEV_BIT * 4))
67 
68 /* Alias word address of TP_EN bit */
69 #define TP_EN_BIT     0x00
70 #define CTRL_TP_EN_BB (PERIPH_BB_BASE + (CTRL_OFFSET * 32) + (TP_EN_BIT * 4))
71 
72 /* --- CTRLSTS Register ---*/
73 
74 /* Alias word address of TPINT_EN bit */
75 #define CTRLSTS_OFFSET      (BKP_OFFSET + 0x34)
76 #define TPINT_EN_BIT        0x02
77 #define CTRLSTS_TPINT_EN_BB (PERIPH_BB_BASE + (CTRLSTS_OFFSET * 32) + (TPINT_EN_BIT * 4))
78 
79 /* Alias word address of TINTF bit */
80 #define TINTF_BIT        0x09
81 #define CTRLSTS_TINTF_BB (PERIPH_BB_BASE + (CTRLSTS_OFFSET * 32) + (TINTF_BIT * 4))
82 
83 /* Alias word address of TEF bit */
84 #define TEF_BIT        0x08
85 #define CTRLSTS_TEF_BB (PERIPH_BB_BASE + (CTRLSTS_OFFSET * 32) + (TEF_BIT * 4))
86 
87 
88 /**
89  * @}
90  */
91 
92 /** @addtogroup BKP_Private_Macros
93  * @{
94  */
95 
96 /**
97  * @}
98  */
99 
100 /** @addtogroup BKP_Private_Variables
101  * @{
102  */
103 
104 /**
105  * @}
106  */
107 
108 /** @addtogroup BKP_Private_FunctionPrototypes
109  * @{
110  */
111 
112 /**
113  * @}
114  */
115 
116 /** @addtogroup BKP_Private_Functions
117  * @{
118  */
119 
120 /**
121  * @brief  Deinitializes the BKP peripheral registers to their default reset values.
122  */
BKP_DeInit(void)123 void BKP_DeInit(void)
124 {
125     RCC_EnableBackupReset(ENABLE);
126     RCC_EnableBackupReset(DISABLE);
127 }
128 
129 /**
130  * @brief  Configures the Tamper Pin active level.
131  * @param BKP_TamperPinLevel specifies the Tamper Pin active level.
132  *   This parameter can be one of the following values:
133  *     @arg BKP_TP_HIGH Tamper pin active on high level
134  *     @arg BKP_TP_LOW Tamper pin active on low level
135  */
BKP_ConfigTPLevel(uint16_t BKP_TamperPinLevel)136 void BKP_ConfigTPLevel(uint16_t BKP_TamperPinLevel)
137 {
138     /* Check the parameters */
139     assert_param(IS_BKP_TP_LEVEL(BKP_TamperPinLevel));
140     *(__IO uint32_t*)CTRL_TP_ALEV_BB = BKP_TamperPinLevel;
141 }
142 
143 /**
144  * @brief  Enables or disables the Tamper Pin activation.
145  * @param Cmd new state of the Tamper Pin activation.
146  *   This parameter can be: ENABLE or DISABLE.
147  */
BKP_TPEnable(FunctionalState Cmd)148 void BKP_TPEnable(FunctionalState Cmd)
149 {
150     /* Check the parameters */
151     assert_param(IS_FUNCTIONAL_STATE(Cmd));
152     *(__IO uint32_t*)CTRL_TP_EN_BB = (uint32_t)Cmd;
153 }
154 
155 /**
156  * @brief  Enables or disables the Tamper Pin Interrupt.
157  * @param Cmd new state of the Tamper Pin Interrupt.
158  *   This parameter can be: ENABLE or DISABLE.
159  */
BKP_TPIntEnable(FunctionalState Cmd)160 void BKP_TPIntEnable(FunctionalState Cmd)
161 {
162     /* Check the parameters */
163     assert_param(IS_FUNCTIONAL_STATE(Cmd));
164     *(__IO uint32_t*)CTRLSTS_TPINT_EN_BB = (uint32_t)Cmd;
165 }
166 
167 
168 /**
169  * @brief  Writes user data to the specified Data Backup Register.
170  * @param BKP_DAT specifies the Data Backup Register.
171  *   This parameter can be BKP_DATx where x:[1, 42]
172  * @param Data data to write
173  */
BKP_WriteBkpData(uint16_t BKP_DAT,uint16_t Data)174 void BKP_WriteBkpData(uint16_t BKP_DAT, uint16_t Data)
175 {
176     __IO uint32_t tmp = 0;
177 
178     /* Check the parameters */
179     assert_param(IS_BKP_DAT(BKP_DAT));
180 
181     tmp = (uint32_t)BKP_BASE;
182     tmp += BKP_DAT;
183 
184     *(__IO uint32_t*)tmp = Data;
185 }
186 
187 /**
188  * @brief  Reads data from the specified Data Backup Register.
189  * @param BKP_DAT specifies the Data Backup Register.
190  *   This parameter can be BKP_DATx where x:[1, 42]
191  * @return The content of the specified Data Backup Register
192  */
BKP_ReadBkpData(uint16_t BKP_DAT)193 uint16_t BKP_ReadBkpData(uint16_t BKP_DAT)
194 {
195     __IO uint32_t tmp = 0;
196 
197     /* Check the parameters */
198     assert_param(IS_BKP_DAT(BKP_DAT));
199 
200     tmp = (uint32_t)BKP_BASE;
201     tmp += BKP_DAT;
202 
203     return (*(__IO uint16_t*)tmp);
204 }
205 
206 /**
207  * @brief  Checks whether the Tamper Pin Event flag is set or not.
208  * @return The new state of the Tamper Pin Event flag (SET or RESET).
209  */
BKP_GetTEFlag(void)210 FlagStatus BKP_GetTEFlag(void)
211 {
212     return (FlagStatus)(*(__IO uint32_t*)CTRLSTS_TEF_BB);
213 }
214 
215 /**
216  * @brief  Clears Tamper Pin Event pending flag.
217  */
BKP_ClrTEFlag(void)218 void BKP_ClrTEFlag(void)
219 {
220     /* Set CTE bit to clear Tamper Pin Event flag */
221     BKP->CTRLSTS |= BKP_CTRLSTS_CLRTE;
222 }
223 
224 /**
225  * @brief  Checks whether the Tamper Pin Interrupt has occurred or not.
226  * @return The new state of the Tamper Pin Interrupt (SET or RESET).
227  */
BKP_GetTINTFlag(void)228 INTStatus BKP_GetTINTFlag(void)
229 {
230     return (INTStatus)(*(__IO uint32_t*)CTRLSTS_TINTF_BB);
231 }
232 
233 /**
234  * @brief  Clears Tamper Pin Interrupt pending bit.
235  */
BKP_ClrTINTFlag(void)236 void BKP_ClrTINTFlag(void)
237 {
238     /* Set CTI bit to clear Tamper Pin Interrupt pending bit */
239     BKP->CTRLSTS |= BKP_CTRLSTS_CLRTINT;
240 }
241 
242 /**
243  * @}
244  */
245 
246 /**
247  * @}
248  */
249 
250 /**
251  * @}
252  */
253