rtc_tamper.h 12 KB

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