am_hal_wdt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. //*****************************************************************************
  2. //
  3. // am_hal_wdt.c
  4. //! @file
  5. //!
  6. //! @brief Hardware abstraction layer for the Watchdog Timer module.
  7. //!
  8. //! @addtogroup wdt2 Watchdog Timer (WDT)
  9. //! @ingroup apollo2hal
  10. //! @{
  11. //
  12. //*****************************************************************************
  13. //*****************************************************************************
  14. //
  15. // Copyright (c) 2017, Ambiq Micro
  16. // All rights reserved.
  17. //
  18. // Redistribution and use in source and binary forms, with or without
  19. // modification, are permitted provided that the following conditions are met:
  20. //
  21. // 1. Redistributions of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // 2. Redistributions in binary form must reproduce the above copyright
  25. // notice, this list of conditions and the following disclaimer in the
  26. // documentation and/or other materials provided with the distribution.
  27. //
  28. // 3. Neither the name of the copyright holder nor the names of its
  29. // contributors may be used to endorse or promote products derived from this
  30. // software without specific prior written permission.
  31. //
  32. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  33. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  36. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  37. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  38. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  40. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. // POSSIBILITY OF SUCH DAMAGE.
  43. //
  44. // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #include <stdint.h>
  48. #include <stdbool.h>
  49. #include "am_mcu_apollo.h"
  50. //*****************************************************************************
  51. //
  52. // Adjacency check
  53. //
  54. // This is related to the timer read workaround. This macro checks to see if
  55. // the two supplied count values are within one "tick" of eachother. It should
  56. // still pass in the event of a timer rollover. The "B" read is assumed to
  57. // follow the "A" read. The macro returns "TRUE" when the adjacent timer reads
  58. // can be used.
  59. //
  60. //*****************************************************************************
  61. #define adjacent(A, B) (((A) == (B)) || (((A) + 1) == (B)) || ((B) == 0))
  62. //*****************************************************************************
  63. //
  64. //! @brief Configure the watchdog timer.
  65. //!
  66. //! @param psConfig - pointer to a configuration structure containing the
  67. //! desired watchdog settings.
  68. //!
  69. //! This function will set the watchdog configuration register based on the
  70. //! user's desired settings listed in the structure referenced by psConfig. If
  71. //! the structure indicates that watchdog interrupts are desired, this function
  72. //! will also set the interrupt enable bit in the configuration register.
  73. //!
  74. //! @note In order to actually receive watchdog interrupt and/or watchdog reset
  75. //! events, the caller will also need to make sure that the watchdog interrupt
  76. //! vector is enabled in the ARM NVIC, and that watchdog resets are enabled in
  77. //! the reset generator module. Otherwise, the watchdog-generated interrupt and
  78. //! reset events will have no effect.
  79. //!
  80. //! @return None.
  81. //
  82. //*****************************************************************************
  83. void
  84. am_hal_wdt_init(const am_hal_wdt_config_t *psConfig)
  85. {
  86. uint32_t ui32ConfigVal;
  87. uint16_t ui16IntCount, ui16ResetCount;
  88. bool bResetEnabled = psConfig->ui32Config & AM_HAL_WDT_ENABLE_RESET;
  89. bool bInterruptEnabled = psConfig->ui32Config & AM_HAL_WDT_ENABLE_INTERRUPT;
  90. //
  91. // Read the desired settings from the psConfig structure.
  92. //
  93. ui16IntCount = psConfig->ui16InterruptCount;
  94. ui16ResetCount = psConfig->ui16ResetCount;
  95. //
  96. // Write the interrupt and reset count values to a temporary variable.
  97. //
  98. // Accept the passed Config value, but clear the Counts that we are about to set.
  99. ui32ConfigVal = psConfig->ui32Config & ~(AM_REG_WDT_CFG_INTVAL_M | AM_REG_WDT_CFG_RESVAL_M);
  100. ui32ConfigVal |= AM_WRITE_SM(AM_REG_WDT_CFG_INTVAL, ui16IntCount);
  101. ui32ConfigVal |= AM_WRITE_SM(AM_REG_WDT_CFG_RESVAL, ui16ResetCount);
  102. //
  103. // If interrupts should be enabled, set the appropriate bit in the
  104. // temporary variable. Also, enable the interrupt in INTEN register in the
  105. // watchdog module.
  106. //
  107. if ( bInterruptEnabled )
  108. {
  109. //
  110. // Enable the watchdog interrupt if the configuration calls for them.
  111. //
  112. AM_REGn(WDT, 0, INTEN) |= AM_REG_WDT_INTEN_WDT_M;
  113. }
  114. else
  115. {
  116. //
  117. // Disable the watchdog interrupt if the configuration doesn't call for
  118. // watchdog interrupts.
  119. //
  120. AM_REGn(WDT, 0, INTEN) &= ~AM_REG_WDT_INTEN_WDT_M;
  121. }
  122. //
  123. // If resets should be enabled, set the appropriate bit in the temporary
  124. // variable.
  125. //
  126. if ( bResetEnabled )
  127. {
  128. //
  129. // Also enable watchdog resets in the reset module.
  130. //
  131. AM_REG(RSTGEN, CFG) |= AM_REG_RSTGEN_CFG_WDREN_M;
  132. }
  133. else
  134. {
  135. //
  136. // Disable watchdog resets in the reset module.
  137. //
  138. AM_REG(RSTGEN, CFG) &= ~AM_REG_RSTGEN_CFG_WDREN_M;
  139. }
  140. //
  141. // Check for a user specified clock select. If none specified then
  142. // set 128Hz.
  143. //
  144. if ( !(psConfig->ui32Config & AM_REG_WDT_CFG_CLKSEL_M) )
  145. {
  146. ui32ConfigVal |= AM_REG_WDT_CFG_CLKSEL_128HZ;
  147. }
  148. //
  149. // Write the saved value to the watchdog configuration register.
  150. //
  151. AM_REGn(WDT, 0, CFG) = ui32ConfigVal;
  152. }
  153. //*****************************************************************************
  154. //
  155. //! @brief Starts the watchdog timer.
  156. //!
  157. //! Enables the watchdog timer tick using the 'enable' bit in the watchdog
  158. //! configuration register. This function does not perform any locking of the
  159. //! watchdog timer, so it can be disabled or reconfigured later.
  160. //!
  161. //! @return None.
  162. //
  163. //*****************************************************************************
  164. void
  165. am_hal_wdt_start(void)
  166. {
  167. //
  168. // Make sure the watchdog timer is in the "reset" state, and then set the
  169. // enable bit to start counting.
  170. //
  171. AM_REGn(WDT, 0, CFG) |= AM_REG_WDT_CFG_WDTEN_M;
  172. AM_REGn(WDT, 0, RSTRT) |= AM_REG_WDT_RSTRT_RSTRT_KEYVALUE;
  173. }
  174. //*****************************************************************************
  175. //
  176. //! @brief Stops the watchdog timer.
  177. //!
  178. //! Disables the watchdog timer tick by clearing the 'enable' bit in the
  179. //! watchdog configuration register.
  180. //!
  181. //! @return None.
  182. //
  183. //*****************************************************************************
  184. void
  185. am_hal_wdt_halt(void)
  186. {
  187. //
  188. // Clear the watchdog enable bit.
  189. //
  190. AM_REGn(WDT, 0, CFG) &= ~AM_REG_WDT_CFG_WDTEN_M;
  191. }
  192. //*****************************************************************************
  193. //
  194. //! @brief Locks the watchdog configuration and starts the watchdog timer.
  195. //!
  196. //! This function sets the watchdog "lock" register, which prevents software
  197. //! from re-configuring the watchdog. This action will also set the enable bit
  198. //! for the watchdog timer, so it will start counting immediately.
  199. //!
  200. //! @return None.
  201. //
  202. //*****************************************************************************
  203. void
  204. am_hal_wdt_lock_and_start(void)
  205. {
  206. //
  207. // Write the 'key' value to the watchdog lock register.
  208. //
  209. AM_REGn(WDT, 0, LOCK) = AM_REG_WDT_LOCK_LOCK_KEYVALUE;
  210. }
  211. //*****************************************************************************
  212. //
  213. //! @brief Read the state of the wdt interrupt status.
  214. //!
  215. //! @param bEnabledOnly - return the status of only the enabled interrupts.
  216. //!
  217. //! This function extracts the interrupt status bits and returns the enabled or
  218. //! raw based on bEnabledOnly.
  219. //!
  220. //! @return WDT interrupt status.
  221. //
  222. //*****************************************************************************
  223. uint32_t
  224. am_hal_wdt_int_status_get(bool bEnabledOnly)
  225. {
  226. if (bEnabledOnly)
  227. {
  228. uint32_t u32RetVal = AM_REG(WDT, INTSTAT);
  229. return u32RetVal & AM_REG(WDT, INTEN);
  230. }
  231. else
  232. {
  233. return AM_REG(WDT, INTSTAT);
  234. }
  235. }
  236. //*****************************************************************************
  237. //
  238. //! @brief Set the state of the wdt interrupt status bit.
  239. //!
  240. //! This function sets the interrupt bit.
  241. //!
  242. //! @return None
  243. //
  244. //*****************************************************************************
  245. void
  246. am_hal_wdt_int_set(void)
  247. {
  248. AM_REG(WDT, INTSET) = AM_REG_WDT_INTSET_WDT_M;
  249. }
  250. //*****************************************************************************
  251. //
  252. //! @brief Clear the state of the wdt interrupt status bit.
  253. //!
  254. //! This function clear the interrupt bit.
  255. //!
  256. //! @return None
  257. //
  258. //*****************************************************************************
  259. void
  260. am_hal_wdt_int_clear(void)
  261. {
  262. AM_REGn(WDT, 0, INTCLR) = AM_REG_WDT_INTCLR_WDT_M;
  263. }
  264. //*****************************************************************************
  265. //
  266. //! @brief Enable the wdt interrupt.
  267. //!
  268. //! This function enable the interrupt.
  269. //!
  270. //! @return None
  271. //
  272. //*****************************************************************************
  273. void
  274. am_hal_wdt_int_enable(void)
  275. {
  276. AM_REG(WDT, INTEN) |= AM_REG_WDT_INTSET_WDT_M;
  277. }
  278. //*****************************************************************************
  279. //
  280. //! @brief Return the enabled WDT interrupts.
  281. //!
  282. //! This function returns the enabled WDT interrupts.
  283. //!
  284. //! @return enabled WDT interrupts.
  285. //
  286. //*****************************************************************************
  287. uint32_t
  288. am_hal_wdt_int_enable_get(void)
  289. {
  290. return AM_REG(WDT, INTEN);
  291. }
  292. //*****************************************************************************
  293. //
  294. //! @brief Disable the wdt interrupt.
  295. //!
  296. //! This function disablee the interrupt.
  297. //!
  298. //! @return None
  299. //
  300. //*****************************************************************************
  301. void
  302. am_hal_wdt_int_disable(void)
  303. {
  304. AM_REG(WDT, INTEN) &= ~AM_REG_WDT_INTSET_WDT_M;
  305. }
  306. //*****************************************************************************
  307. //
  308. // Static function for reading the WDT counter value.
  309. //
  310. //*****************************************************************************
  311. #if defined(__GNUC_STDC_INLINE__)
  312. __attribute__((naked))
  313. static
  314. void
  315. back2back_read_asm(uint32_t *pui32Array, uint32_t *pui32Register)
  316. {
  317. // pui32Array[] is a pointer to a 3 word data array provided by the caller.
  318. // pui32Register = address of the timer to be read.
  319. __asm
  320. (
  321. // Do 3 back-to-back reads of the register
  322. " ldr r2, [r1, #0]\n" // Get counter register value
  323. " ldr r3, [r1, #0]\n" // Get counter register value again
  324. " ldr r1, [r1, #0]\n" // Get counter register value for a third time
  325. " str r2, [r0, #0]\n" // Store register value to variable
  326. " str r3, [r0, #4]\n" // Store register value to variable
  327. " str r1, [r0, #8]\n" // Store register value to variable
  328. " bx lr\n"
  329. );
  330. }
  331. #elif defined(__ARMCC_VERSION)
  332. __asm static uint32_t
  333. back2back_read_asm(uint32_t *pui32Array, uint32_t *pui32Register)
  334. {
  335. ldr r2, [r1, #0] // Get TMRn register value
  336. ldr r3, [r1, #0] // Get TMRn register value again
  337. ldr r1, [r1, #0] // Get TMRn register value for a third time
  338. str r2, [r0, #0] // Store register value to variable
  339. str r3, [r0, #4] // Store register value to variable
  340. str r1, [r0, #8] // Store register value to variable
  341. bx lr
  342. }
  343. #elif defined(__IAR_SYSTEMS_ICC__)
  344. #pragma diag_suppress = Pe940 // Suppress IAR compiler warning about missing
  345. // return statement on a non-void function
  346. __stackless static uint32_t
  347. back2back_read_asm(uint32_t *pui32Array, uint32_t *pui32Register)
  348. {
  349. __asm(" ldr r2, [r1, #0]"); // Get TMRn register value
  350. __asm(" ldr r3, [r1, #0]"); // Get TMRn register value again
  351. __asm(" ldr r1, [r1, #0]"); // Get TMRn register value for a third time
  352. __asm(" str r2, [r0, #0]"); // Store register value to variable
  353. __asm(" str r3, [r0, #4]"); // Store register value to variable
  354. __asm(" str r1, [r0, #8]"); // Store register value to variable
  355. __asm(" bx lr");
  356. }
  357. #pragma diag_default = Pe940 // Restore IAR compiler warning
  358. #endif
  359. //*****************************************************************************
  360. //
  361. //! @brief Get the wdt counter value.
  362. //!
  363. //! This function reads the current value of watch dog timer counter register.
  364. //!
  365. //! WARNING caller is responsible for masking interrutps before calling this
  366. //! function.
  367. //!
  368. //! @return None
  369. //
  370. //*****************************************************************************
  371. uint32_t
  372. am_hal_wdt_counter_get(void)
  373. {
  374. uint32_t ui32Values[3] = {0};
  375. uint32_t ui32Value;
  376. //
  377. // First, go read the value from the counter register 3 times
  378. // back to back in assembly language.
  379. //
  380. back2back_read_asm(ui32Values, (uint32_t *)AM_REG_WDTn(0));
  381. //
  382. // Now, we'll figure out which of the three values is the correct time.
  383. //
  384. if (ui32Values[0] == ui32Values[1])
  385. {
  386. //
  387. // If the first two values match, then neither one was a bad read.
  388. // We'll take this as the current time.
  389. //
  390. ui32Value = ui32Values[1];
  391. }
  392. else
  393. {
  394. //
  395. // If the first two values didn't match, then one of them might be bad.
  396. // If one of the first two values is bad, then the third one should
  397. // always be correct. We'll take the third value as the correct count.
  398. //
  399. ui32Value = ui32Values[2];
  400. //
  401. // If all of the statements about the architecture are true, the third
  402. // value should be correct, and it should always be within one count of
  403. // either the first or the second value.
  404. //
  405. // Just in case, we'll check against the previous two values to make
  406. // sure that our final answer was reasonable. If it isn't, we will
  407. // flag it as a "bad read", and fail this assert statement.
  408. //
  409. // This shouldn't ever happen, and it hasn't ever happened in any of
  410. // our tests so far.
  411. //
  412. am_hal_debug_assert_msg((adjacent(ui32Values[1], ui32Values[2]) ||
  413. adjacent(ui32Values[0], ui32Values[2])),
  414. "Bad CDT read");
  415. }
  416. return ui32Value;
  417. }
  418. //*****************************************************************************
  419. //
  420. // End Doxygen group.
  421. //! @}
  422. //
  423. //*****************************************************************************