1 /**
2  * @file    smon.h
3  * @brief   Security Monitor.
4  */
5 
6 /******************************************************************************
7  *
8  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9  * Analog Devices, Inc.),
10  * Copyright (C) 2023-2024 Analog Devices, Inc.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  ******************************************************************************/
25 
26 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32520_SMON_H_
27 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32520_SMON_H_
28 
29 /* **** Includes **** */
30 #include "mxc_device.h"
31 #include "mxc_sys.h"
32 #include "smon_regs.h"
33 #include "gcr_regs.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /**
40  * @defgroup smon Security Monitor(SMON)
41  * @ingroup periphlibs
42  * @{
43  */
44 
45 /**
46  * @brief   External Sensor Number
47  *
48  */
49 typedef enum {
50     SMON_EXTSENSOR_0 = MXC_F_SMON_EXTSCN_EXTS_EN0,
51 } mxc_smon_ext_sensor_t;
52 
53 /**
54  * @brief   Enum for clock divider
55  *
56  */
57 typedef enum {
58     SMON_CLK_DIVIDE_1 = MXC_S_SMON_EXTSCN_DIVCLK_DIV1,
59     SMON_CLK_DIVIDE_2 = MXC_S_SMON_EXTSCN_DIVCLK_DIV2,
60     SMON_CLK_DIVIDE_4 = MXC_S_SMON_EXTSCN_DIVCLK_DIV4,
61     SMON_CLK_DIVIDE_8 = MXC_S_SMON_EXTSCN_DIVCLK_DIV8,
62     SMON_CLK_DIVIDE_16 = MXC_S_SMON_EXTSCN_DIVCLK_DIV16,
63     SMON_CLK_DIVIDE_32 = MXC_S_SMON_EXTSCN_DIVCLK_DIV32,
64     SMON_CLK_DIVIDE_64 = MXC_S_SMON_EXTSCN_DIVCLK_DIV64,
65 } mxc_smon_clk_divide_t;
66 
67 /**
68  * @brief   Enum for Frequency Divider
69  *
70  */
71 typedef enum {
72     SMON_FREQ_DIVIDE_4 = MXC_S_SMON_EXTSCN_EXTFRQ_FREQ2000HZ,
73     SMON_FREQ_DIVIDE_8 = MXC_S_SMON_EXTSCN_EXTFRQ_FREQ1000HZ,
74     SMON_FREQ_DIVIDE_16 = MXC_S_SMON_EXTSCN_EXTFRQ_FREQ500HZ,
75     SMON_FREQ_DIVIDE_32 = MXC_S_SMON_EXTSCN_EXTFRQ_FREQ250HZ,
76     SMON_FREQ_DIVIDE_64 = MXC_S_SMON_EXTSCN_EXTFRQ_FREQ125HZ,
77     SMON_FREQ_DIVIDE_128 = MXC_S_SMON_EXTSCN_EXTFRQ_FREQ63HZ,
78     SMON_FREQ_DIVIDE_256 = MXC_S_SMON_EXTSCN_EXTFRQ_FREQ31HZ,
79 } mxc_smon_freq_divide_t;
80 
81 /**
82  * @brief   Voltage Monitor Thresholds
83  *
84  */
85 typedef enum {
86     SMON_VTM_THRESHOLD_1_6, ///< 1.6 V
87     SMON_VTM_THRESHOLD_2_2, ///< 2.2 V
88     SMON_VTM_THRESHOLD_2_8, ///< 2.8 V
89 } mxc_smon_vtm_t;
90 
91 /**
92  * @brief   Temperature Sensor Thresholds
93  *
94  */
95 typedef enum {
96     SMON_TEMP_THRESHOLD_NEG_50, ///< -50 *C
97     SMON_TEMP_THRESHOLD_NEG_30, ///< -30 *C
98 } mxc_smon_temp_t;
99 
100 /**
101  * @brief   Digital Fault Interrupt mode
102  *
103  */
104 typedef enum {
105     SMON_DFD_INTERRUPT_NMI, ///< DRS/NMI
106     SMON_DFD_INTERRUPT_PFW, ///< PFW IRQ
107 } mxc_smon_interrupt_mode_t;
108 
109 /**
110  * @brief   Digital Fault Low Power mode
111  *
112  */
113 typedef enum {
114     SMON_DFD_LOWPOWER_ENABLE, ///< DFD enabled during LowPower mode
115     SMON_DFD_LOWPOWER_DISABLE, ///< DFD disabled during LowPower mode
116 } mxc_smon_lowpower_mode_t;
117 
118 /**
119  * @brief   Register to check if busy
120  *
121  */
122 typedef enum {
123     SMON_EXTSENSOR = MXC_F_SMON_SECST_EXTSRS,
124     SMON_INTSENSOR = MXC_F_SMON_SECST_INTSRS,
125     SMON_SECALARM = MXC_F_SMON_SECST_SECALRS,
126 } mxc_smon_busy_t;
127 
128 /**
129  * @brief   The information required to configure an external sensor
130  *
131  */
132 typedef struct {
133     mxc_smon_ext_sensor_t sensorNumber;
134     mxc_smon_clk_divide_t clockDivide;
135     mxc_smon_freq_divide_t freqDivide;
136     uint8_t errorCount;
137     uint8_t data;
138 } mxc_smon_ext_cfg_t;
139 
140 /**
141  * @brief   Initialize Security Monitor
142  *
143  */
144 void MXC_SMON_Init(void);
145 
146 /**
147  * @brief   Shutdown Security Monitor
148  *
149  */
150 void MXC_SMON_Shutdown(void);
151 
152 /**
153  * @brief   Enables desired External Sensor
154  *
155  * @param   cfg         configuration for setting up external sensor
156  * @param   delay       timeout delay
157  *
158  * @return  int         see \ref MXC_Error_Codes for a list of return codes
159  */
160 int MXC_SMON_ExtSensorEnable(mxc_smon_ext_cfg_t *cfg, uint32_t delay);
161 
162 /**
163  * @brief   Set frequency for external frequency
164  *
165  * @param   cfg         configuration for setting up external sensor
166  *
167  * @return  int         see \ref MXC_Error_Codes for a list of return codes
168  */
169 int MXC_SMON_SetSensorFrequency(mxc_smon_ext_cfg_t *cfg);
170 
171 /**
172  * @brief   Set number of acceptable errors for external sensor
173  *
174  * @param   errorCount  Error count 0 - 31
175  *
176  * @return  int         see \ref MXC_Error_Codes for a list of return codes
177  */
178 int MXC_SMON_SetErrorCount(uint8_t errorCount);
179 
180 /**
181  * @brief   Enable Temperature Sensor
182  *
183  * @param   threshold   temperatue threshold, \ref mxc_smon_temp_t
184  * @param   delay       timeout delay
185  *
186  * @return  int         see \ref MXC_Error_Codes for a list of return codes
187  */
188 int MXC_SMON_TempSensorEnable(mxc_smon_temp_t threshold, uint32_t delay);
189 
190 /**
191  * @brief   Set Temperature Threshold
192  *
193  * @param   threshold   temperature threshold, \ref mxc_smon_temp_t
194  *
195  * @return  int         see \ref MXC_Error_Codes for a list of return codes
196  */
197 int MXC_SMON_SetTempThreshold(mxc_smon_temp_t threshold);
198 
199 /**
200  * @brief   Enable Voltage Monitor
201  *
202  * @param   threshold   voltage threshold, \ref mxc_smon_vtm_t
203  * @param   delay       timeout delay
204  *
205  * @return  int         see \ref MXC_Error_Codes for a list of return codes
206  */
207 int MXC_SMON_VoltageMonitorEnable(mxc_smon_vtm_t threshold, uint32_t delay);
208 
209 /**
210  * @brief   Set Voltage Monitor Threshold
211  *
212  * @param   threshold   voltage threshold, \ref mxc_smon_vtm_t
213  *
214  * @return  int         see \ref MXC_Error_Codes for a list of return codes
215  */
216 int MXC_SMON_SetVTMThreshold(mxc_smon_vtm_t threshold);
217 
218 /**
219  * @brief   Enbale Active Die Shield Monitoring
220  *
221  * @param   delay       timeout delay
222  *
223  * @return  int         see \ref MXC_Error_Codes for a list of return codes
224  */
225 int MXC_SMON_ActiveDieShieldEnable(uint32_t delay);
226 
227 /**
228  * @brief   Enable Self Destruct Byte on External Sensor 0
229  *
230  * @param   cfg         configuration for setting up SDBE, \ref mxc_smon_ext_cfg_t
231  * @param   delay       timeout delay
232  *
233  * @return  int         see \ref MXC_Error_Codes for a list of return codes
234  */
235 int MXC_SMON_SelfDestructByteEnable(mxc_smon_ext_cfg_t *cfg, uint32_t delay);
236 
237 /**
238  * @brief   Enables PUF Trim Erase on DRS
239  *
240  */
241 void MXC_SMON_EnablePUFTrimErase(void);
242 
243 /**
244  * @brief   Disables PUF Trim Erase on DRS
245  *
246  */
247 void MXC_SMON_DisablePUFTrimErase(void);
248 
249 /**
250  * @brief   Enbale Digital Fault Detector
251  *
252  * @param   interruptMode   interrupt mode, \ref mxc_smon_interrupt_mode_t
253  * @param   lowPowerMode    low power mode, \ref mxc_smon_lowpower_mode_t
254  * @param   delay           timeout delay
255  *
256  * @return  int            see \ref MXC_Error_Codes for a list of return codes
257  */
258 int MXC_SMON_DigitalFaultDetectorEnable(mxc_smon_interrupt_mode_t interruptMode,
259                                         mxc_smon_lowpower_mode_t lowPowerMode, uint32_t delay);
260 
261 /**
262  * @brief   Get Flags set in Security Alarm Register
263  *
264  * @return  uint32_t        SECALM register
265  */
266 uint32_t MXC_SMON_GetFlags(void);
267 
268 /**
269  * @brief   Clear flags set in Security Alarm Register
270  *
271  * @param   flags       flags to clear from Security Alarm register
272  */
273 void MXC_SMON_ClearFlags(uint32_t flags);
274 
275 /**
276  * @brief   Lock the EXTSCN register to generate DRS/NMI
277  *
278  */
279 void MXC_SMON_ExtSensorLock(void);
280 
281 /**
282  * @brief   Lock the INTSCN register to generate DRS/NMI
283  *
284  */
285 void MXC_SMON_IntSensorLock(void);
286 
287 /**
288  * @brief   Checks if the registers are busy before wirting to it
289  *
290  * @param   reg         see \ref mxc_smon_busy_t for registers
291  * @param   delay       timeout delay
292  *
293  * @return  int         see \ref MXC_Error_Codes for a list of return codes
294  */
295 int MXC_SMON_isBusy(mxc_smon_busy_t reg, uint32_t delay);
296 
297 #ifdef __cplusplus
298 }
299 #endif
300 
301 /**@} end of group smon  */
302 
303 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32520_SMON_H_
304