1 /*
2  * Copyright (c) 2024 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_TAMP_DRV_H
9 #define HPM_TAMP_DRV_H
10 
11 #include "hpm_common.h"
12 #include "hpm_tamp_regs.h"
13 
14 /**
15  *
16  * @brief TAMPER driver APIs
17  * @defgroup tamper_interface TAMPER driver APIs
18  * @ingroup io_interfaces
19  * @{
20  */
21 
22 typedef enum {
23     spd_1_time_per_sec = 0,
24     spd_2_times_per_sec,
25     spd_4_times_per_sec,
26     spd_8_times_per_sec,
27     spd_16_times_per_sec,
28     spd_32_times_per_sec,
29     spd_64_times_per_sec,
30     spd_128_times_per_sec,
31     spd_256_times_per_sec,
32     spd_512_times_per_sec,
33     spd_1024_times_per_sec,
34     spd_2048_times_per_sec,
35     spd_4096_times_per_sec,
36     spd_8192_times_per_sec,
37     spd_16384_times_per_sec,
38     spd_32768_times_per_sec,
39 } tamper_speed_t;
40 
41 typedef enum {
42     filter_len_1_cycle = 0,
43     filter_len_2_cycles,
44     filter_len_4_cycles,
45     filter_len_8_cycles,
46     filter_len_16_cycles,
47     filter_len_32_cycles,
48     filter_len_64_cycles,
49     filter_len_128_cycles,
50     filter_len_256_cycles,
51     filter_len_512_cycles,
52     filter_len_1024_cycles,
53     filter_len_2048_cycles,
54     filter_len_4096_cycles,
55     filter_len_8192_cycles,
56     filter_len_16384_cycles,
57     filter_len_32768_cycles,
58 } tamper_filter_len_t;
59 
60  typedef struct {
61     bool enable;
62     bool active_mode;
63     bool filter_bypass;
64     bool expect_high_level;
65     tamper_speed_t speed;
66     tamper_filter_len_t filter_len;
67     bool auto_recover;    /* used in active mode */
68     uint32_t poly;    /* used in active mode */
69     uint32_t lfsr;    /* used in active mode */
70 } tamper_ch_config_t;
71 
72 
73 #ifdef __cplusplus
74 extern "C" {
75 #endif
76 
77 /**
78  * @brief Init tamper channel config
79  *
80  * @param[in] ptr tamper base address
81  * @param[in] ch tamper channel
82  * @param[in] config tamper channel config struct pointer
83  *
84  */
85 void tamp_init_ch_config(TAMP_Type *ptr, uint8_t ch, tamper_ch_config_t *config);
86 
87 /**
88  * @brief Get tamper default channel config
89  *
90  * @param[in] ptr tamper base address
91  * @param[in] config tamper default channel config struct pointer
92  *
93  */
94 void tamp_get_default_ch_config(TAMP_Type *ptr, tamper_ch_config_t *config);
95 
96 /**
97  * @brief Set tamper channel enable or disable
98  *
99  * @param[in] ptr tamper base address
100  * @param[in] ch tamper channel
101  * @param[in] enable true - enable tamper, false - disable tamper
102  *
103  */
tamp_set_ch_enable(TAMP_Type * ptr,uint8_t ch,bool enable)104 static inline void tamp_set_ch_enable(TAMP_Type *ptr, uint8_t ch, bool enable)
105 {
106     ch >>= 1u;
107     if (enable) {
108         ptr->TAMP[ch].CONTROL |= TAMP_TAMP_CONTROL_ENABLE_MASK;
109     } else {
110         ptr->TAMP[ch].CONTROL &= ~TAMP_TAMP_CONTROL_ENABLE_MASK;
111     }
112 }
113 
114 /**
115  * @brief Set tamper channel config lock or unlock
116  *
117  * @param[in] ptr tamper base address
118  * @param[in] ch tamper channel
119  * @param[in] lock true - config lock, false - config unlock
120  *
121  */
tamp_set_ch_config_lock(TAMP_Type * ptr,uint8_t ch,bool lock)122 static inline void tamp_set_ch_config_lock(TAMP_Type *ptr, uint8_t ch, bool lock)
123 {
124     ch >>= 1u;
125     if (lock) {
126         ptr->TAMP[ch].CONTROL |= TAMP_TAMP_CONTROL_LOCK_MASK;
127     } else {
128         ptr->TAMP[ch].CONTROL &= ~TAMP_TAMP_CONTROL_LOCK_MASK;
129     }
130 }
131 
132 /**
133  * @brief Get tamper all channel flags
134  *
135  * @param[in] ptr tamper base address
136  *
137  * @return all channel flags
138  */
tamp_get_flags(TAMP_Type * ptr)139 static inline uint32_t tamp_get_flags(TAMP_Type *ptr)
140 {
141     return TAMP_TAMP_FLAG_FLAG_GET(ptr->TAMP_FLAG);
142 }
143 
144 /**
145  * @brief Clear tamper flags
146  *
147  * @param[in] ptr tamper base address
148  * @param[in] flags clear channel flags
149  *
150  */
tamp_clear_flags(TAMP_Type * ptr,uint32_t flags)151 static inline void tamp_clear_flags(TAMP_Type *ptr, uint32_t flags)
152 {
153     ptr->TAMP_FLAG = TAMP_TAMP_FLAG_FLAG_SET(flags);
154 }
155 
156 /**
157  * @brief Check tamper channel flag
158  *
159  * @param[in] ptr TAMPER base address
160  * @param[in] ch tamper channel
161  *
162  * @return true - flag is set, false - falg is unset.
163  */
tamp_check_ch_flag(TAMP_Type * ptr,uint8_t ch)164 static inline bool tamp_check_ch_flag(TAMP_Type *ptr, uint8_t ch)
165 {
166     return ((TAMP_TAMP_FLAG_FLAG_GET(ptr->TAMP_FLAG) & (1u << ch)) != 0u) ? true : false;
167 }
168 
169 /**
170  * @brief Clear tamper channel flag
171  *
172  * @param[in] ptr TAMPER base address
173  * @param[in] ch tamper channel
174  *
175  */
tamp_clear_ch_flag(TAMP_Type * ptr,uint8_t ch)176 static inline void tamp_clear_ch_flag(TAMP_Type *ptr, uint8_t ch)
177 {
178     ptr->TAMP_FLAG = TAMP_TAMP_FLAG_FLAG_SET(1u << ch);
179 }
180 
181 /**
182  * @brief Set tamper channel irq enable or disable
183  *
184  * @param[in] ptr TAMPER base address
185  * @param[in] ch tamper channel
186  * @param[in] enable true - irq enable, false - irq disable
187  *
188  */
tamp_enable_ch_irq(TAMP_Type * ptr,uint8_t ch,bool enable)189 static inline void tamp_enable_ch_irq(TAMP_Type *ptr, uint8_t ch, bool enable)
190 {
191     if (enable) {
192         ptr->IRQ_EN |= TAMP_IRQ_EN_IRQ_EN_SET(1u << ch);
193     } else {
194         ptr->IRQ_EN &= ~TAMP_IRQ_EN_IRQ_EN_SET(1u << ch);
195     }
196 }
197 
198 /**
199  * @brief Set tamper irq lock or unlock
200  *
201  * @param[in] ptr tamper base address
202  * @param[in] lock true - irq lock, false - irq unlock
203  *
204  */
tamp_set_irq_lock(TAMP_Type * ptr,bool lock)205 static inline void tamp_set_irq_lock(TAMP_Type *ptr, bool lock)
206 {
207     if (lock) {
208         ptr->IRQ_EN |= TAMP_IRQ_EN_LOCK_MASK;
209     } else {
210         ptr->IRQ_EN &= ~TAMP_IRQ_EN_LOCK_MASK;
211     }
212 }
213 
214 #ifdef __cplusplus
215 }
216 #endif
217 /**
218  * @}
219  */
220 #endif /* HPM_TAMP_DRV_H */
221 
222