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_MAX32570_SMON_H_
27 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32570_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     SMON_EXTSENSOR_1 = MXC_F_SMON_EXTSCN_EXTS_EN1,
52     SMON_EXTSENSOR_2 = MXC_F_SMON_EXTSCN_EXTS_EN2,
53     SMON_EXTSENSOR_3 = MXC_F_SMON_EXTSCN_EXTS_EN3,
54     SMON_EXTSENSOR_4 = MXC_F_SMON_EXTSCN_EXTS_EN4,
55     SMON_EXTSENSOR_5 = MXC_F_SMON_EXTSCN_EXTS_EN5,
56 } mxc_smon_ext_sensor_t;
57 
58 /**
59  * @brief   Enum for Clock Divider
60  *
61  */
62 typedef enum {
63     SMON_CLK_DIVIDE_1 = MXC_S_SMON_EXTSCN_DIVCLK_DIV1,
64     SMON_CLK_DIVIDE_2 = MXC_S_SMON_EXTSCN_DIVCLK_DIV2,
65     SMON_CLK_DIVIDE_4 = MXC_S_SMON_EXTSCN_DIVCLK_DIV4,
66     SMON_CLK_DIVIDE_8 = MXC_S_SMON_EXTSCN_DIVCLK_DIV8,
67     SMON_CLK_DIVIDE_16 = MXC_S_SMON_EXTSCN_DIVCLK_DIV16,
68     SMON_CLK_DIVIDE_32 = MXC_S_SMON_EXTSCN_DIVCLK_DIV32,
69     SMON_CLK_DIVIDE_64 = MXC_S_SMON_EXTSCN_DIVCLK_DIV64,
70 } mxc_smon_clk_divide_t;
71 
72 /**
73  * @brief   Enum for Frequency Divider
74  *
75  */
76 typedef enum {
77     SMON_FREQ_DIVIDE_4 = MXC_S_SMON_EXTSCN_EXTFRQ_FREQ2000HZ,
78     SMON_FREQ_DIVIDE_8 = MXC_S_SMON_EXTSCN_EXTFRQ_FREQ1000HZ,
79     SMON_FREQ_DIVIDE_16 = MXC_S_SMON_EXTSCN_EXTFRQ_FREQ500HZ,
80     SMON_FREQ_DIVIDE_32 = MXC_S_SMON_EXTSCN_EXTFRQ_FREQ250HZ,
81     SMON_FREQ_DIVIDE_64 = MXC_S_SMON_EXTSCN_EXTFRQ_FREQ125HZ,
82     SMON_FREQ_DIVIDE_128 = MXC_S_SMON_EXTSCN_EXTFRQ_FREQ63HZ,
83     SMON_FREQ_DIVIDE_256 = MXC_S_SMON_EXTSCN_EXTFRQ_FREQ31HZ,
84 } mxc_smon_freq_divide_t;
85 
86 /**
87  * @brief   Voltage Monitor Thresholds
88  *
89  */
90 typedef enum {
91     SMON_VTM_THRESHOLD_1_6, ///< 1.6 V
92     SMON_VTM_THRESHOLD_2_2, ///< 2.2 V
93     SMON_VTM_THRESHOLD_2_8, ///< 2.8 V
94 } mxc_smon_vtm_t;
95 
96 /**
97  * @brief   Temperature Sensor Thresholds
98  *
99  */
100 typedef enum {
101     SMON_TEMP_THRESHOLD_NEG_50, ///< -50 *C
102     SMON_TEMP_THRESHOLD_NEG_30, ///< -30 *C
103 } mxc_smon_temp_t;
104 
105 /**
106  * @brief   Digital Fault Interrupt mode
107  *
108  */
109 typedef enum {
110     SMON_DFD_INTERRUPT_NMI, ///< DRS/NMI
111     SMON_DFD_INTERRUPT_PFW, ///< PFW IRQ
112 } mxc_smon_interrupt_mode_t;
113 
114 /**
115  * @brief   Digital Fault Low Power mode
116  *
117  */
118 typedef enum {
119     SMON_DFD_LOWPOWER_ENABLE, ///< DFD enabled during LowPower mode
120     SMON_DFD_LOWPOWER_DISABLE, ///< DFD disabled during LowPower mode
121 } mxc_smon_lowpower_mode_t;
122 
123 /**
124  * @brief   Register to check if busy
125  *
126  */
127 typedef enum {
128     SMON_EXTSENSOR = MXC_F_SMON_SECST_EXTSRS,
129     SMON_INTSENSOR = MXC_F_SMON_SECST_INTSRS,
130     SMON_SECALARM = MXC_F_SMON_SECST_SECALRS,
131 } mxc_smon_busy_t;
132 
133 /**
134  * @brief   The information required to configure an external sensor
135  *
136  */
137 typedef struct {
138     mxc_smon_ext_sensor_t sensorNumber;
139     mxc_smon_clk_divide_t clockDivide;
140     mxc_smon_freq_divide_t freqDivide;
141     uint8_t errorCount;
142     uint8_t data;
143 } mxc_smon_ext_cfg_t;
144 
145 /**
146  * @brief   Initialize Security Monitor
147  *
148  */
149 void MXC_SMON_Init(void);
150 
151 /**
152  * @brief   Shutdown Security Monitor
153  *
154  */
155 void MXC_SMON_Shutdown(void);
156 
157 /**
158  * @brief   Enables desired External Sensor
159  *
160  * @param   cfg         configuration for setting up external sensor
161  * @param   delay       timeout delay
162  *
163  * @return  int         see \ref MXC_Error_Codes for a list of return codes
164  */
165 int MXC_SMON_ExtSensorEnable(mxc_smon_ext_cfg_t *cfg, uint32_t delay);
166 
167 /**
168  * @brief   Set frequency for external frequency
169  *
170  * @param   cfg         configuration for setting up external sensor
171  *
172  * @return  int         see \ref MXC_Error_Codes for a list of return codes
173  */
174 int MXC_SMON_SetSensorFrequency(mxc_smon_ext_cfg_t *cfg);
175 
176 /**
177  * @brief   Set number of acceptable errors for external sensor
178  *
179  * @param   errorCount  Error count 0 - 31
180  *
181  * @return  int         see \ref MXC_Error_Codes for a list of return codes
182  */
183 int MXC_SMON_SetErrorCount(uint8_t errorCount);
184 
185 /**
186  * @brief   Enable Temperature Sensor
187  *
188  * @param   threshold   temperature threshold, \ref mxc_smon_temp_t
189  * @param   delay       timeout delay
190  *
191  * @return  int         see \ref MXC_Error_Codes for a list of return codes
192  */
193 int MXC_SMON_TempSensorEnable(mxc_smon_temp_t threshold, uint32_t delay);
194 
195 /**
196  * @brief   Set Temperature Threshold
197  *
198  * @param   threshold   temperature threshold, \ref mxc_smon_temp_t
199  *
200  * @return  int         see \ref MXC_Error_Codes for a list of return codes
201  */
202 int MXC_SMON_SetTempThreshold(mxc_smon_temp_t threshold);
203 
204 /**
205  * @brief   Enable Voltage Monitor
206  *
207  * @param   threshold   voltage threshold, \ref mxc_smon_vtm_t
208  * @param   delay       timeout delay
209  *
210  * @return  int         see \ref MXC_Error_Codes for a list of return codes
211  */
212 int MXC_SMON_VoltageMonitorEnable(mxc_smon_vtm_t threshold, uint32_t delay);
213 
214 /**
215  * @brief   Set Voltage Monitor Threshold
216  *
217  * @param   threshold   voltage threshold, \ref mxc_smon_vtm_t
218  *
219  * @return  int         see \ref MXC_Error_Codes for a list of return codes
220  */
221 int MXC_SMON_SetVTMThreshold(mxc_smon_vtm_t threshold);
222 
223 /**
224  * @brief   Enbale Active Die Shield Monitoring
225  *
226  * @param   delay       timeout delay
227  *
228  * @return  int         see \ref MXC_Error_Codes for a list of return codes
229  */
230 int MXC_SMON_ActiveDieShieldEnable(uint32_t delay);
231 
232 /**
233  * @brief   Enable Self Destruct Byte on External Sensor 0
234  *
235  * @param   cfg         configuration for setting up SDBE
236  * @param   delay       timeout delay
237  *
238  * @return  int         see \ref MXC_Error_Codes for a list of return codes
239  */
240 int MXC_SMON_SelfDestructByteEnable(mxc_smon_ext_cfg_t *cfg, uint32_t delay);
241 
242 /**
243  * @brief   Enables PUF Trim Erase on DRS
244  *
245  */
246 void MXC_SMON_EnablePUFTrimErase(void);
247 
248 /**
249  * @brief   Disables PUF Trim Erase on DRS
250  *
251  */
252 void MXC_SMON_DisablePUFTrimErase(void);
253 
254 /**
255  * @brief   Enbale Digital Fault Detector
256  *
257  * @param   interruptMode   interrupt mode, \ref mxc_smon_interrupt_mode_t
258  * @param   lowPowerMode    low power mode, \ref mxc_smon_lowpower_mode_t
259  * @param   delay          timeout delay
260  *
261  * @return  int            see \ref MXC_Error_Codes for a list of return codes
262  */
263 int MXC_SMON_DigitalFaultDetectorEnable(mxc_smon_interrupt_mode_t interruptMode,
264                                         mxc_smon_lowpower_mode_t lowPowerMode, uint32_t delay);
265 
266 /**
267  * @brief   Get Flags set in Security Alarm Register
268  *
269  * @return  uint32_t        SECALM register
270  */
271 uint32_t MXC_SMON_GetFlags(void);
272 
273 /**
274  * @brief   Clear flags set in Security Alarm Register
275  *
276  * @param   flags           flags to clear from Security Alarm register
277  */
278 void MXC_SMON_ClearFlags(uint32_t flags);
279 
280 /**
281  * @brief   Lock the EXTSCN register to generate DRS/NMI
282  *
283  */
284 void MXC_SMON_ExtSensorLock(void);
285 
286 /**
287  * @brief   Lock the INTSCN register to generate DRS/NMI
288  *
289  */
290 void MXC_SMON_IntSensorLock(void);
291 
292 /**
293  * @brief   Checks if the registers are busy before wirting to it
294  *
295  * @param   reg         see \ref mxc_smon_busy_t for registers
296  * @param   delay       timeout delay
297  *
298  * @return  int         see \ref MXC_Error_Codes for a list of return codes
299  */
300 int MXC_SMON_isBusy(mxc_smon_busy_t reg, uint32_t delay);
301 
302 #ifdef __cplusplus
303 }
304 #endif
305 
306 /**@} end of group smon  */
307 
308 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32570_SMON_H_
309