Europalab Devices produces a LoRaWAN transmitting client node, specialised for higher research of actuator and sensor assisted IoT networks. https://dev.europalab.com/nlnet/20200000/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

368 行
12KB

  1. /**
  2. * \file
  3. *
  4. * \brief SAM RTC Driver (Tamper)
  5. *
  6. * Copyright (c) 2015-2018 Microchip Technology Inc. and its subsidiaries.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Subject to your compliance with these terms, you may use Microchip
  13. * software and any derivatives exclusively with Microchip products.
  14. * It is your responsibility to comply with third party license terms applicable
  15. * to your use of third party software (including open source software) that
  16. * may accompany Microchip software.
  17. *
  18. * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
  19. * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
  20. * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
  21. * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
  22. * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
  23. * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
  24. * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
  25. * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
  26. * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
  27. * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
  28. * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
  29. *
  30. * \asf_license_stop
  31. *
  32. */
  33. /*
  34. * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
  35. */
  36. #ifndef RTC_TAMPER_H_INCLUDED
  37. #define RTC_TAMPER_H_INCLUDED
  38. /**
  39. *
  40. * \section asfdoc_sam0_rtc_tamper_detect RTC Tamper Detect
  41. * The RTC provides several selectable polarity external inputs (INn) that can be
  42. * used for tamper detection. When detect, tamper inputs support the four actions:
  43. * - Off
  44. * - Wake
  45. * - Capture
  46. * - Active layer protection
  47. *
  48. * \note The Active Layer Protection is a means of detecting broken traces on the
  49. * PCB provided by RTC. In this mode an RTC output signal is routed over critical
  50. * components on the board and fed back to one of the RTC inputs. The input and
  51. * output signals are compared and a tamper condition is detected when they do not match.
  52. *
  53. *
  54. * Separate debouncers are embedded for each external input. The detection time
  55. * depends on whether the debouncer operates synchronously or asynchronously,
  56. * and whether majority detection is enabled or not. For details, refer to the section
  57. * "Tamper Detection" of datasheet.
  58. * \if RTC_COUNT_CALLBACK_MODE
  59. * \addtogroup asfdoc_sam0_rtc_count_group
  60. * \else
  61. * \if RTC_CALENDAR_CALLBACK_MODE
  62. * \addtogroup asfdoc_sam0_rtc_calendar_group
  63. * \endif
  64. * \endif
  65. * @{
  66. */
  67. #if defined(FEATURE_RTC_TAMPER_DETECTION) || defined(__DOXYGEN__)
  68. /** RTC tamper ID0 detection bitmask. */
  69. #define RTC_TAMPER_DETECT_ID0 (1UL << 0)
  70. /** RTC tamper ID1 detection bitmask. */
  71. #define RTC_TAMPER_DETECT_ID1 (1UL << 1)
  72. /** RTC tamper ID2 detection bitmask. */
  73. #define RTC_TAMPER_DETECT_ID2 (1UL << 2)
  74. /** RTC tamper ID3 detection bitmask. */
  75. #define RTC_TAMPER_DETECT_ID3 (1UL << 3)
  76. /** RTC tamper ID4 detection bitmask. */
  77. #define RTC_TAMPER_DETECT_ID4 (1UL << 4)
  78. /** RTC tamper input event detection bitmask. */
  79. #define RTC_TAMPER_DETECT_EVT (1UL << 5)
  80. /**
  81. * \brief RTC tamper active layer frequency divider.
  82. *
  83. * The available prescaler factor for the RTC clock output used during active
  84. * layer protection.
  85. */
  86. enum rtc_tamper_active_layer_freq_divider {
  87. /** RTC active layer frequency is prescaled by a factor of 2 */
  88. RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_2 = RTC_MODE0_CTRLB_ACTF_DIV2,
  89. /** RTC active layer frequency is prescaled by a factor of 4 */
  90. RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_4 = RTC_MODE0_CTRLB_ACTF_DIV4,
  91. /** RTC active layer frequency is prescaled by a factor of 8 */
  92. RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_8 = RTC_MODE0_CTRLB_ACTF_DIV8,
  93. /** RTC active layer frequency is prescaled by a factor of 16 */
  94. RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_16 = RTC_MODE0_CTRLB_ACTF_DIV16,
  95. /** RTC active layer frequency is prescaled by a factor of 32 */
  96. RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_32 = RTC_MODE0_CTRLB_ACTF_DIV32,
  97. /** RTC active layer frequency is prescaled by a factor of 64 */
  98. RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_64 = RTC_MODE0_CTRLB_ACTF_DIV64,
  99. /** RTC active layer frequency is prescaled by a factor of 128 */
  100. RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_128 = RTC_MODE0_CTRLB_ACTF_DIV128,
  101. /** RTC active layer frequency is prescaled by a factor of 256 */
  102. RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_256 = RTC_MODE0_CTRLB_ACTF_DIV256,
  103. };
  104. /**
  105. * \brief RTC tamper debounce frequency divider.
  106. *
  107. * The available prescaler factor for the input debouncers.
  108. */
  109. enum rtc_tamper_debounce_freq_divider {
  110. /** RTC debounce frequency is prescaled by a factor of 2 */
  111. RTC_TAMPER_DEBOUNCE_FREQ_DIV_2 = RTC_MODE0_CTRLB_DEBF_DIV2,
  112. /** RTC debounce frequency is prescaled by a factor of 4 */
  113. RTC_TAMPER_DEBOUNCE_FREQ_DIV_4 = RTC_MODE0_CTRLB_DEBF_DIV4,
  114. /** RTC debounce frequency is prescaled by a factor of 8 */
  115. RTC_TAMPER_DEBOUNCE_FREQ_DIV_8 = RTC_MODE0_CTRLB_DEBF_DIV8,
  116. /** RTC debounce frequency is prescaled by a factor of 16 */
  117. RTC_TAMPER_DEBOUNCE_FREQ_DIV_16 = RTC_MODE0_CTRLB_DEBF_DIV16,
  118. /** RTC debounce frequency is prescaled by a factor of 32 */
  119. RTC_TAMPER_DEBOUNCE_FREQ_DIV_32 = RTC_MODE0_CTRLB_DEBF_DIV32,
  120. /** RTC debounce frequency is prescaled by a factor of 64 */
  121. RTC_TAMPER_DEBOUNCE_FREQ_DIV_64 = RTC_MODE0_CTRLB_DEBF_DIV64,
  122. /** RTC debounce frequency is prescaled by a factor of 128 */
  123. RTC_TAMPER_DEBOUNCE_FREQ_DIV_128 = RTC_MODE0_CTRLB_DEBF_DIV128,
  124. /** RTC debounce frequency is prescaled by a factor of 256 */
  125. RTC_TAMPER_DEBOUNCE_FREQ_DIV_256 = RTC_MODE0_CTRLB_DEBF_DIV256,
  126. };
  127. /**
  128. * \brief RTC tamper input action.
  129. *
  130. * The available action taken by the tamper input.
  131. */
  132. enum rtc_tamper_input_action {
  133. /** RTC tamper input action is disabled */
  134. RTC_TAMPER_INPUT_ACTION_OFF = RTC_TAMPCTRL_IN0ACT_OFF,
  135. /** RTC tamper input action is wake and set tamper flag */
  136. RTC_TAMPER_INPUT_ACTION_WAKE = RTC_TAMPCTRL_IN0ACT_WAKE,
  137. /** RTC tamper input action is capture timestamp and set tamper flag */
  138. RTC_TAMPER_INPUT_ACTION_CAPTURE = RTC_TAMPCTRL_IN0ACT_CAPTURE,
  139. /** RTC tamper input action is compare IN to OUT, when a mismatch occurs,
  140. * capture timestamp and set tamper flag */
  141. RTC_TAMPER_INPUT_ACTION_ACTL = RTC_TAMPCTRL_IN0ACT_ACTL,
  142. };
  143. /**
  144. * \brief RTC tamper input level select.
  145. *
  146. * The available edge condition for tamper INn level select.
  147. */
  148. enum rtc_tamper_level_sel {
  149. /** A falling edge condition will be detected on Tamper input */
  150. RTC_TAMPER_LEVEL_FALLING = (0),
  151. /** A rising edge condition will be detected on Tamper input */
  152. RTC_TAMPER_LEVEL_RISING = (1),
  153. };
  154. /**
  155. * \brief RTC tamper debounce sequential.
  156. *
  157. * The available sequential for tamper debounce.
  158. */
  159. enum rtc_tamper_debounce_seq {
  160. /** Tamper input detect edge with synchronous stability debounce */
  161. RTC_TAMPER_DEBOUNCE_SYNC,
  162. /** Tamper input detect edge with asynchronous stability debounce */
  163. RTC_TAMPER_DEBOUNCE_ASYNC,
  164. /** Tamper input detect edge with majority debounce */
  165. RTC_TAMPER_DEBOUNCE_MAJORITY,
  166. };
  167. /**
  168. * \brief RTC tamper input configuration structure.
  169. *
  170. * The configuration structure for tamper INn.
  171. */
  172. struct rtc_tamper_input_config {
  173. /** Debounce enable */
  174. bool debounce_enable;
  175. /** Tamper level select */
  176. enum rtc_tamper_level_sel level;
  177. /** Tamper input action */
  178. enum rtc_tamper_input_action action;
  179. };
  180. /**
  181. * \brief RTC Tamper configuration structure.
  182. *
  183. * The configuration structure for the RTC tamper. This structure should
  184. * be initialized using the \ref rtc_tamper_get_config_defaults() before any
  185. * user configurations are set.
  186. */
  187. struct rtc_tamper_config {
  188. /** Backup register reset on tamper enable */
  189. bool bkup_reset_on_tamper;
  190. /** GP register reset on tamper enable */
  191. bool gp_reset_on_tamper;
  192. /** Active layer frequency */
  193. enum rtc_tamper_active_layer_freq_divider actl_freq_div;
  194. /** Debounce frequency */
  195. enum rtc_tamper_debounce_freq_divider deb_freq_div;
  196. /** Debounce sequential */
  197. enum rtc_tamper_debounce_seq deb_seq;
  198. /** DMA on tamper enable */
  199. bool dma_tamper_enable;
  200. /** General Purpose 0/1 Enable */
  201. bool gp0_enable;
  202. /** Tamper IN configuration */
  203. struct rtc_tamper_input_config in_cfg[RTC_TAMPER_NUM];
  204. };
  205. /**
  206. * \name RTC Tamper Detection
  207. * @{
  208. */
  209. /**
  210. * \brief Gets the RTC tamper default configurations.
  211. *
  212. * Initializes the configuration structure to default values.
  213. *
  214. * The default configuration is as follows:
  215. * - Disable backup register reset on tamper
  216. * - Disable GP register reset on tamper
  217. * - Active layer clock divided by a factor of 8
  218. * - Debounce clock divided by a factor of 8
  219. * - Detect edge on INn with synchronous stability debouncing
  220. * - Disable DMA on tamper
  221. * - Enable GP register
  222. * - Disable debouce, detect on falling edge and no action on INn
  223. *
  224. * \param[out] config Configuration structure to be initialized to default values.
  225. */
  226. static inline void rtc_tamper_get_config_defaults(
  227. struct rtc_tamper_config *const config)
  228. {
  229. /* Sanity check argument */
  230. Assert(config);
  231. config->bkup_reset_on_tamper= false;
  232. config->gp_reset_on_tamper = false;
  233. config->actl_freq_div = RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_8;
  234. config->deb_freq_div = RTC_TAMPER_DEBOUNCE_FREQ_DIV_8;
  235. config->deb_seq = RTC_TAMPER_DEBOUNCE_SYNC;
  236. config->dma_tamper_enable = false;
  237. config->gp0_enable = true;
  238. for (uint8_t id = 0; id < RTC_TAMPER_NUM; id++) {
  239. config->in_cfg[id].debounce_enable = false;
  240. config->in_cfg[id].level = RTC_TAMPER_LEVEL_FALLING;
  241. config->in_cfg[id].action = RTC_TAMPER_INPUT_ACTION_OFF;
  242. }
  243. }
  244. enum status_code rtc_tamper_set_config (struct rtc_module *const module,
  245. struct rtc_tamper_config *const tamper_cfg);
  246. /**
  247. * \brief Retrieves the RTC tamper detection status.
  248. *
  249. * Retrieves the detection status of each input pin and the input event.
  250. *
  251. * \param[in] module Pointer to the RTC software instance struct
  252. *
  253. * \return Bitmask of detection flags.
  254. *
  255. * \retval RTC_TAMPER_DETECT_ID0 Tamper condition on IN0 has been detected
  256. * \retval RTC_TAMPER_DETECT_ID1 Tamper condition on IN1 has been detected
  257. * \retval RTC_TAMPER_DETECT_ID2 Tamper condition on IN2 has been detected
  258. * \retval RTC_TAMPER_DETECT_ID3 Tamper condition on IN3 has been detected
  259. * \retval RTC_TAMPER_DETECT_ID4 Tamper condition on IN4 has been detected
  260. * \retval RTC_TAMPER_DETECT_EVT Tamper input event has been detected
  261. */
  262. static inline uint32_t rtc_tamper_get_detect_flag (struct rtc_module *const module)
  263. {
  264. /* Sanity check arguments */
  265. Assert(module);
  266. Assert(module->hw);
  267. uint32_t tamper_id = module->hw->MODE0.TAMPID.reg;
  268. uint32_t detect_flags = 0;
  269. if (tamper_id & RTC_TAMPID_TAMPID0) {
  270. detect_flags |= RTC_TAMPER_DETECT_ID0;
  271. }
  272. if (tamper_id & RTC_TAMPID_TAMPID1) {
  273. detect_flags |= RTC_TAMPER_DETECT_ID1;
  274. }
  275. if (tamper_id & RTC_TAMPID_TAMPID2) {
  276. detect_flags |= RTC_TAMPER_DETECT_ID2;
  277. }
  278. if (tamper_id & RTC_TAMPID_TAMPID3) {
  279. detect_flags |= RTC_TAMPER_DETECT_ID3;
  280. }
  281. if (tamper_id & RTC_TAMPID_TAMPID4) {
  282. detect_flags |= RTC_TAMPER_DETECT_ID4;
  283. }
  284. if (tamper_id & RTC_TAMPID_TAMPEVT) {
  285. detect_flags |= RTC_TAMPER_DETECT_EVT;
  286. }
  287. return detect_flags;
  288. }
  289. /**
  290. * \brief Clears RTC tamper detection flag.
  291. *
  292. * Clears the given detection flag of the module.
  293. *
  294. * \param[in] module Pointer to the TC software instance struct
  295. * \param[in] detect_flags Bitmask of detection flags
  296. */
  297. static inline void rtc_tamper_clear_detect_flag(
  298. struct rtc_module *const module,
  299. const uint32_t detect_flags)
  300. {
  301. /* Sanity check arguments */
  302. Assert(module);
  303. Assert(module->hw);
  304. uint32_t tamper_id = 0;
  305. if (detect_flags & RTC_TAMPER_DETECT_ID0) {
  306. tamper_id |= RTC_TAMPID_TAMPID0;
  307. }
  308. if (detect_flags & RTC_TAMPER_DETECT_ID1) {
  309. tamper_id |= RTC_TAMPID_TAMPID1;
  310. }
  311. if (detect_flags & RTC_TAMPER_DETECT_ID2) {
  312. tamper_id |= RTC_TAMPID_TAMPID2;
  313. }
  314. if (detect_flags & RTC_TAMPER_DETECT_ID3) {
  315. tamper_id |= RTC_TAMPID_TAMPID3;
  316. }
  317. if (detect_flags & RTC_TAMPER_DETECT_ID4) {
  318. tamper_id |= RTC_TAMPID_TAMPID4;
  319. }
  320. if (detect_flags & RTC_TAMPER_DETECT_EVT) {
  321. tamper_id |= RTC_TAMPID_TAMPEVT;
  322. }
  323. module->hw->MODE0.TAMPID.reg = tamper_id;
  324. }
  325. /** @} */
  326. #endif
  327. /** @} */
  328. #endif /* RTC_TAMPER_H_INCLUDED */