hpm_tamp_drv.h 4.8 KB

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