reset_cause.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /**
  2. * \file
  3. *
  4. * \brief CPU reset cause functions
  5. *
  6. * Copyright (c) 2010-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 COMMON_DRIVERS_CPU_RESET_CAUSE_H
  47. #define COMMON_DRIVERS_CPU_RESET_CAUSE_H
  48. #include <parts.h>
  49. #include <stdbool.h>
  50. #if XMEGA
  51. # include "xmega_reset_cause.h"
  52. #elif UC3
  53. # include "avr32_reset_cause.h"
  54. #elif SAM4L
  55. # include "sam4l_reset_cause.h"
  56. #else
  57. # error Unsupported chip type
  58. #endif
  59. /**
  60. * \defgroup reset_cause_group CPU reset cause
  61. *
  62. * See \ref reset_cause_quickstart
  63. *
  64. * This is a generic interface for getting and clearing the chip reset causes.
  65. *
  66. * \section dependencies Dependencies
  67. *
  68. * The reset cause interface does not depend on any other modules, as it only
  69. * accesses a few registers in the device core.
  70. *
  71. * On the other hand, the software reset call might depend on \ref sysclk_group
  72. * to enable the clock to the debug system, for devices doing software reset
  73. * through the on-chip debug system. This applies only to the 32-bit AVR
  74. * devices.
  75. *
  76. * \section Quick start guide
  77. * See \ref reset_cause_quickstart
  78. *
  79. * @{
  80. */
  81. /*
  82. * Sanity check of reset causes, define undefined reset causes to 0. Hence they
  83. * will always return false when queried.
  84. */
  85. #ifndef CHIP_RESET_CAUSE_BOD_CPU
  86. /**
  87. * \brief Brown-out detected on CPU power domain reset cause not available on
  88. * this chip.
  89. */
  90. # define CHIP_RESET_CAUSE_BOD_CPU 0
  91. #endif
  92. #ifndef CHIP_RESET_CAUSE_BOD_IO
  93. /**
  94. * \brief Brown-out detected on I/O power domain reset cause not available on
  95. * this chip.
  96. */
  97. # define CHIP_RESET_CAUSE_BOD_IO 0
  98. #endif
  99. #ifndef CHIP_RESET_CAUSE_CPU_ERROR
  100. //! CPU error reset cause not available on this chip.
  101. # define CHIP_RESET_CAUSE_CPU_ERROR 0
  102. #endif
  103. #ifndef CHIP_RESET_CAUSE_EXTRST
  104. //! External reset cause not available on this chip.
  105. # define CHIP_RESET_CAUSE_EXTRST 0
  106. #endif
  107. #ifndef CHIP_RESET_CAUSE_JTAG
  108. //! JTAG reset cause not available on this chip.
  109. # define CHIP_RESET_CAUSE_JTAG 0
  110. #endif
  111. #ifndef CHIP_RESET_CAUSE_OCD
  112. //! On-chip debug system reset cause not available on this chip.
  113. # define CHIP_RESET_CAUSE_OCD 0
  114. #endif
  115. #ifndef CHIP_RESET_CAUSE_POR
  116. //! Power-on-reset reset cause not available on this chip.
  117. # define CHIP_RESET_CAUSE_POR 0
  118. #endif
  119. #ifndef CHIP_RESET_CAUSE_POR_IO
  120. //! Power-on-reset on I/O power domain reset cause not available on this chip.
  121. # define CHIP_RESET_CAUSE_POR_IO 0
  122. #endif
  123. #ifndef CHIP_RESET_CAUSE_SLEEP
  124. //! Wake from Shutdown sleep mode reset cause not available on this chip.
  125. # define CHIP_RESET_CAUSE_SLEEP 0
  126. #endif
  127. #ifndef CHIP_RESET_CAUSE_SOFT
  128. //! Software reset reset cause not available on this chip.
  129. # define CHIP_RESET_CAUSE_SOFT 0
  130. #endif
  131. #ifndef CHIP_RESET_CAUSE_SPIKE
  132. //! Spike detected reset cause not available on this chip.
  133. # define CHIP_RESET_CAUSE_SPIKE 0
  134. #endif
  135. #ifndef CHIP_RESET_CAUSE_WDT
  136. //! Watchdog timeout reset cause not available on this chip.
  137. # define CHIP_RESET_CAUSE_WDT 0
  138. #endif
  139. /**
  140. * \brief List of reset causes in bit-mask format
  141. */
  142. enum reset_cause {
  143. /** \brief Brown-out detected on CPU power domain reset cause */
  144. RESET_CAUSE_BOD_CPU = CHIP_RESET_CAUSE_BOD_CPU,
  145. /** \brief Brown-out detected on I/O power domain reset cause */
  146. RESET_CAUSE_BOD_IO = CHIP_RESET_CAUSE_BOD_IO,
  147. /** \brief CPU error reset cause */
  148. RESET_CAUSE_CPU_ERROR = CHIP_RESET_CAUSE_CPU_ERROR,
  149. /** \brief External reset cause */
  150. RESET_CAUSE_EXTRST = CHIP_RESET_CAUSE_EXTRST,
  151. /** \brief JTAG reset cause */
  152. RESET_CAUSE_JTAG = CHIP_RESET_CAUSE_JTAG,
  153. /** \brief On-chip debug system reset cause */
  154. RESET_CAUSE_OCD = CHIP_RESET_CAUSE_OCD,
  155. /** \brief Power-on-reset reset cause */
  156. RESET_CAUSE_POR = CHIP_RESET_CAUSE_POR,
  157. /** \brief Power-on-reset reset cause */
  158. RESET_CAUSE_POR_IO = CHIP_RESET_CAUSE_POR_IO,
  159. /** \brief Wake from Shutdown sleep mode reset cause */
  160. RESET_CAUSE_SLEEP = CHIP_RESET_CAUSE_SLEEP,
  161. /** \brief Software reset reset cause */
  162. RESET_CAUSE_SOFT = CHIP_RESET_CAUSE_SOFT,
  163. /** \brief Spike detected reset cause */
  164. RESET_CAUSE_SPIKE = CHIP_RESET_CAUSE_SPIKE,
  165. /** \brief Watchdog timeout reset cause */
  166. RESET_CAUSE_WDT = CHIP_RESET_CAUSE_WDT,
  167. };
  168. //! \name Management
  169. //@{
  170. /**
  171. * \fn void reset_do_soft_reset(void)
  172. * \brief Perform a software reset of the device
  173. *
  174. * \note This function will never return.
  175. * \note This function does not disable interrupts, this is up to the caller to
  176. * handle.
  177. */
  178. /**
  179. * \fn reset_cause_t reset_cause_get_causes(void)
  180. * \brief Get all reset causes
  181. *
  182. * This function will return a value containing the currently triggered reset
  183. * cause(s).
  184. *
  185. * \return Bit-mask with each active reset cause set to 1.
  186. */
  187. /**
  188. * \fn reset_cause_clear_causes(reset_cause_t causes)
  189. * \brief Clear a bit-mask of reset causes
  190. *
  191. * This function will clear the provided reset causes in the reset cause
  192. * register.
  193. *
  194. * \param causes bit-mask of reset causes to clear
  195. */
  196. //@}
  197. //! \name Specific reset cause helper functions
  198. //@{
  199. /**
  200. * \brief Check if chip reset was caused by a CPU power brown-out detection
  201. *
  202. * \return True if reset was caused by a CPU power brown-out detection
  203. */
  204. static inline bool reset_cause_is_cpu_brown_out_detected(void)
  205. {
  206. return (reset_cause_get_causes() & RESET_CAUSE_BOD_CPU);
  207. }
  208. /**
  209. * \brief Check if chip reset was caused by an I/O power brown-out detection
  210. *
  211. * \return True if reset was caused by an I/O power brown-out detection
  212. */
  213. static inline bool reset_cause_is_io_brown_out_detected(void)
  214. {
  215. return (reset_cause_get_causes() & RESET_CAUSE_BOD_IO);
  216. }
  217. /**
  218. * \brief Check if chip reset was caused by a brown-out detection on any
  219. * power domain.
  220. *
  221. * \return True if reset was caused by a power brown-out detection
  222. */
  223. static inline bool reset_cause_is_brown_out_detected(void)
  224. {
  225. return (reset_cause_is_cpu_brown_out_detected() ||
  226. reset_cause_is_io_brown_out_detected());
  227. }
  228. /**
  229. * \brief Check if chip reset was caused by a CPU error, illegal access
  230. *
  231. * \return True if reset was caused by a CPU error, illegal access
  232. */
  233. static inline bool reset_cause_is_cpu_error(void)
  234. {
  235. return (reset_cause_get_causes() & RESET_CAUSE_CPU_ERROR);
  236. }
  237. /**
  238. * \brief Check if chip reset was caused by an external reset
  239. *
  240. * \return True if reset was caused by an external reset
  241. */
  242. static inline bool reset_cause_is_external_reset(void)
  243. {
  244. return (reset_cause_get_causes() & RESET_CAUSE_EXTRST);
  245. }
  246. /**
  247. * \brief Check if chip reset was caused by a JTAG reset
  248. *
  249. * \return True if reset was caused by a JTAG reset
  250. */
  251. static inline bool reset_cause_is_jtag(void)
  252. {
  253. return (reset_cause_get_causes() & RESET_CAUSE_JTAG);
  254. }
  255. /**
  256. * \brief Check if chip reset was caused by the on-chip debug system
  257. *
  258. * \return True if reset was caused by the on-chip debug system
  259. */
  260. static inline bool reset_cause_is_ocd(void)
  261. {
  262. return (reset_cause_get_causes() & RESET_CAUSE_OCD);
  263. }
  264. /**
  265. * \brief Check if chip reset was caused by a power-on-reset
  266. *
  267. * \return True if reset was caused by a power-on-reset
  268. */
  269. static inline bool reset_cause_is_power_on_reset(void)
  270. {
  271. return (reset_cause_get_causes() & RESET_CAUSE_POR);
  272. }
  273. /**
  274. * \brief Check if chip reset was caused by an I/O power-on-reset
  275. *
  276. * \return True if reset was caused by a power-on-reset
  277. */
  278. static inline bool reset_cause_is_io_power_on_reset(void)
  279. {
  280. return (reset_cause_get_causes() & RESET_CAUSE_POR_IO);
  281. }
  282. /**
  283. * \brief Check if chip reset was caused by a wake up from shutdown sleep mode
  284. *
  285. * \return True if reset was caused by a wake up from shutdown sleep mode
  286. */
  287. static inline bool reset_cause_is_wake_from_shutdown_sleep(void)
  288. {
  289. return (reset_cause_get_causes() & RESET_CAUSE_SLEEP);
  290. }
  291. /**
  292. * \brief Check if chip reset was caused by a software reset
  293. *
  294. * \return True if reset was caused by a software reset
  295. */
  296. static inline bool reset_cause_is_software_reset(void)
  297. {
  298. return (reset_cause_get_causes() & RESET_CAUSE_SOFT);
  299. }
  300. /**
  301. * \brief Check if chip reset was caused by a power spike detection
  302. *
  303. * \return True if reset was caused by a spike detection
  304. */
  305. static inline bool reset_cause_is_spike_detected(void)
  306. {
  307. return (reset_cause_get_causes() & RESET_CAUSE_SPIKE);
  308. }
  309. /**
  310. * \brief Check if chip reset was caused by a watchdog timeout
  311. *
  312. * \return True if reset was caused by a watchdog timeout
  313. */
  314. static inline bool reset_cause_is_watchdog(void)
  315. {
  316. return (reset_cause_get_causes() & RESET_CAUSE_WDT);
  317. }
  318. //@}
  319. //! @}
  320. /**
  321. * \page reset_cause_quickstart Quick start guide for reset cause service
  322. *
  323. * This is the quick start guide for the \ref reset_cause_group
  324. * "Reset Cause service", with step-by-step instructions on how to configure
  325. * and use the driver in a selection of use cases.
  326. *
  327. * The use cases contain several code fragments. The code fragments in the
  328. * steps for setup can be copied into a custom initialization function, while
  329. * the steps for usage can be copied into, e.g., the main application function.
  330. *
  331. * \section reset_cause_basic_use_case Basic use case
  332. * In this basic use case, the reset cause service is used for checking if the
  333. * last reset was a watchdog reset.
  334. *
  335. * \section reset_cause_basic_use_case_setup Setup steps
  336. *
  337. * \subsection reset_cause_basic_use_case_setup_code Example code
  338. * Add to application C-file:
  339. * \code
  340. if (reset_cause_is_watchdog()) {
  341. // Do action due to last reset being a watchdog reset
  342. reset_cause_clear_causes(RESET_CAUSE_WDT);
  343. }
  344. \endcode
  345. *
  346. * \subsection reset_cause_basic_use_case_setup_flow Workflow
  347. * -# Check for watchdog reset flag:
  348. * - \code if (reset_cause_is_watchdog()) { \endcode
  349. * - \attention Please consult the specific device datasheet on which reset
  350. * causes that are supported.
  351. * -# Insert your own code taking action here. E.g.: Increase a watchdog reset
  352. * counter.
  353. * -# Reset flag if the flag was set to make sure it's not falsely
  354. * detected in another reset:
  355. * - \code reset_cause_clear_causes(RESET_CAUSE_WDT); \endcode
  356. *
  357. * \section reset_cause_use_cases Advanced use cases
  358. * For more advanced use of the Reset Cause service, see the following use cases:
  359. * - \subpage reset_cause_use_case_1 : Software controlled reset
  360. */
  361. /**
  362. * \page reset_cause_use_case_1 Use case #1
  363. * In this use case, the reset cause service is used to perform a software
  364. * controlled reset.
  365. *
  366. * \section reset_cause_use_case_1_setup Setup steps
  367. *
  368. * \subsection reset_cause_use_case_1_setup_flow Workflow
  369. * -# Call soft reset. This call will not return.
  370. * - \code reset_do_soft_reset(); \endcode
  371. */
  372. #endif /* COMMON_DRIVERS_CPU_RESET_CAUSE_H */