wdog.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /******************************************************************************
  2. *
  3. * @brief Provide common watchdog module routines.
  4. *
  5. * @history:
  6. * Jun. 25, 2013 modified the watch dog unlock sequence and disable sequence
  7. ******************************************************************************/
  8. #include "common.h"
  9. #include "wdog.h"
  10. /******************************************************************************
  11. * Global variables
  12. ******************************************************************************/
  13. /******************************************************************************
  14. * Constants and macros
  15. ******************************************************************************/
  16. /******************************************************************************
  17. * Local types
  18. ******************************************************************************/
  19. /******************************************************************************
  20. * Local function prototypes
  21. ******************************************************************************/
  22. /******************************************************************************
  23. * Local variables
  24. ******************************************************************************/
  25. /******************************************************************************
  26. * Local functions
  27. ******************************************************************************/
  28. /******************************************************************************
  29. * Global functions
  30. ******************************************************************************/
  31. /******************************************************************************
  32. * define watchdog API list
  33. *
  34. *//*! @addtogroup wdog_api_list
  35. * @{
  36. *******************************************************************************/
  37. /*****************************************************************************//*!
  38. *
  39. * @brief Watchdog ETMer disable routine.
  40. *
  41. * @param none
  42. *
  43. * @return none
  44. *
  45. * @ Pass/ Fail criteria: none
  46. * @see WDOG_Enable
  47. *****************************************************************************/
  48. void WDOG_Disable(void)
  49. {
  50. uint8_t u8Cs1 = WDOG->CS1;
  51. uint8_t u8Cs2 = WDOG->CS2;
  52. uint16_t u16TOVAL = WDOG->TOVAL;
  53. uint16_t u16WIN = WDOG->WIN;
  54. u8Cs1 &= ~WDOG_CS1_EN_MASK;
  55. /* First unlock the watchdog so that we can write to registers */
  56. WDOG_Unlock();
  57. WDOG->CS2 = u8Cs2;
  58. WDOG->TOVAL = u16TOVAL;
  59. WDOG->WIN = u16WIN;
  60. WDOG->CS1 = u8Cs1;
  61. }
  62. /*****************************************************************************//*!
  63. *
  64. * @brief Watchdog ETMer disable routine with update enabled.
  65. *
  66. * Disable watchdog but the watchdog can be enabled and updated later.
  67. *
  68. * @param none
  69. *
  70. * @return none
  71. *
  72. * @ Pass/ Fail criteria: none
  73. * @see WDOG_Enable
  74. *****************************************************************************/
  75. void WDOG_DisableWDOGEnableUpdate(void)
  76. {
  77. uint8_t u8Cs1 = WDOG->CS1;
  78. uint8_t u8Cs2 = WDOG->CS2;
  79. uint16_t u16TOVAL = WDOG->TOVAL;
  80. uint16_t u16WIN = WDOG->WIN;
  81. u8Cs1 &= ~WDOG_CS1_EN_MASK;
  82. u8Cs1 |= WDOG_CS1_UPDATE_MASK;
  83. /* First unlock the watchdog so that we can write to registers */
  84. //WDOG_Unlock();
  85. WDOG->CS2 = u8Cs2;
  86. WDOG->TOVAL = u16TOVAL;
  87. WDOG->WIN = u16WIN;
  88. WDOG->CS1 = u8Cs1;
  89. }
  90. /*****************************************************************************//*!
  91. *
  92. * @brief Watchdog ETMer enable routine.
  93. *
  94. * @param none
  95. *
  96. * @return none
  97. *
  98. * @ Pass/ Fail criteria: none
  99. * @see WDOG_Disable
  100. *****************************************************************************/
  101. void WDOG_Enable(void)
  102. {
  103. uint8_t u8Cs1 = WDOG->CS1;
  104. u8Cs1 |= WDOG_CS1_EN_MASK;
  105. /* First unlock the watchdog so that we can write to registers */
  106. WDOG_Unlock();
  107. WDOG->CS1 = u8Cs1;
  108. }
  109. /*****************************************************************************//*!
  110. *
  111. * @brief initialize watchdog.
  112. *
  113. * @param[in] pConfig poiner to watchdog configuration strcture.
  114. *
  115. * @return none
  116. *
  117. * @ Pass/ Fail criteria: none
  118. *
  119. * @warning make sure that WDOG is not initialized after reset or WDOG update is enabled
  120. * after reset by calling WDOG_EnableUpdate / WDOG_DisableWDOGEnableUpdate.
  121. *
  122. * @see WDOG_EnableUpdate, WDOG_DisableWDOGEnableUpdate
  123. *
  124. *****************************************************************************/
  125. void WDOG_Init(WDOG_ConfigPtr pConfig)
  126. {
  127. uint8_t u8Cs1;
  128. uint8_t u8Cs2;
  129. uint16_t u16Toval;
  130. uint16_t u16Win;
  131. u8Cs1 = 0x80; /* default CS1 register value */
  132. u8Cs2 = 0;
  133. u16Toval = pConfig->u16ETMeOut;
  134. u16Win = pConfig->u16WinETMe;
  135. if(pConfig->sBits.bDisable)
  136. {
  137. u8Cs1 &= ~WDOG_CS1_EN_MASK;
  138. }
  139. if(pConfig->sBits.bIntEnable)
  140. {
  141. u8Cs1 |= WDOG_CS1_INT_MASK;
  142. }
  143. if(pConfig->sBits.bStopEnable)
  144. {
  145. u8Cs1 |= WDOG_CS1_STOP_MASK;
  146. }
  147. if(pConfig->sBits.bDbgEnable)
  148. {
  149. u8Cs1 |= WDOG_CS1_DBG_MASK;
  150. }
  151. if(pConfig->sBits.bWaitEnable)
  152. {
  153. u8Cs1 |= WDOG_CS1_WAIT_MASK;
  154. }
  155. if(pConfig->sBits.bUpdateEnable)
  156. {
  157. u8Cs1 |= WDOG_CS1_UPDATE_MASK;
  158. }
  159. if(pConfig->sBits.bWinEnable)
  160. {
  161. u8Cs2 |= WDOG_CS2_WIN_MASK;
  162. }
  163. if(pConfig->sBits.bPrescaler)
  164. {
  165. u8Cs2 |= WDOG_CS2_PRES_MASK;
  166. }
  167. u8Cs2 |= (pConfig->sBits.bClkSrc & 0x03);
  168. /* write regisers */
  169. WDOG_Unlock(); /* unlock watchdog first */
  170. WDOG->CS2 = u8Cs2;
  171. WDOG->TOVAL8B.TOVALL = u16Toval;
  172. WDOG->TOVAL8B.TOVALH = u16Toval >> 8;
  173. WDOG->WIN8B.WINL = u16Win;
  174. WDOG->WIN8B.WINH = u16Win >> 8;
  175. WDOG->CS1 = u8Cs1;
  176. }
  177. /*****************************************************************************//*!
  178. *
  179. * @brief initialize watchdog to the default state.
  180. *
  181. * @param none
  182. *
  183. * @return none
  184. *
  185. * @ Pass/ Fail criteria: none
  186. * @warning make sure that WDOG update is enabled after reset by calling WDOG_EnableUpdate.
  187. * or by calling WDOG_DisableWDOGEnableUpdate.
  188. *
  189. * @see WDOG_DisableWDOGEnableUpdate, WDOG_EnableUpdate
  190. *
  191. *****************************************************************************/
  192. void WDOG_DeInit(void)
  193. {
  194. WDOG_Unlock();
  195. WDOG->CS2 = WDOG_CS2_DEFAULT_VALUE;
  196. WDOG->TOVAL = WDOG_TOVAL_DEFAULT_VALUE;
  197. WDOG->WIN = WDOG_WIN_DEFAULT_VALUE;
  198. WDOG->CS1 = WDOG_CS1_DEFAULT_VALUE;
  199. }
  200. /*****************************************************************************//*!
  201. *
  202. * @brief feed/refresh watchdog.
  203. *
  204. * @param none
  205. *
  206. * @return none
  207. *
  208. * @ Pass/ Fail criteria: none
  209. *****************************************************************************/
  210. void WDOG_Feed(void)
  211. {
  212. DisableInterrupts;
  213. WDOG->CNT = 0x02A6;
  214. WDOG->CNT = 0x80B4;
  215. EnableInterrupts;
  216. }
  217. /*****************************************************************************//*!
  218. *
  219. * @brief enable update of WDOG.
  220. *
  221. * @param none
  222. *
  223. * @return none
  224. *
  225. * @ Pass/ Fail criteria: none
  226. * @warning this must be the last step of writing control bits sequence.
  227. *****************************************************************************/
  228. void WDOG_EnableUpdate(void)
  229. {
  230. uint8_t u8Cs1 = WDOG->CS1;
  231. uint8_t u8Cs2 = WDOG->CS2;
  232. uint16_t u16TOVAL = WDOG->TOVAL;
  233. uint16_t u16WIN = WDOG->WIN;
  234. u8Cs1 |= WDOG_CS1_UPDATE_MASK;
  235. /* First unlock the watchdog so that we can write to registers */
  236. WDOG_Unlock();
  237. WDOG->CS2 = u8Cs2;
  238. WDOG->TOVAL = u16TOVAL;
  239. WDOG->WIN = u16WIN;
  240. WDOG->CS1 = u8Cs1;
  241. }
  242. /*****************************************************************************//*!
  243. *
  244. * @brief disable update of WDOG.
  245. *
  246. * @param none
  247. *
  248. * @return none
  249. *
  250. * @ Pass/ Fail criteria: none
  251. * @warning this must be the last step of writing control bits sequence.
  252. *****************************************************************************/
  253. void WDOG_DisableUpdate(void)
  254. {
  255. uint8_t u8Cs1 = WDOG->CS1;
  256. uint8_t u8Cs2 = WDOG->CS2;
  257. uint16_t u16TOVAL = WDOG->TOVAL;
  258. uint16_t u16WIN = WDOG->WIN;
  259. u8Cs1 &= ~WDOG_CS1_UPDATE_MASK;
  260. /* First unlock the watchdog so that we can write to registers */
  261. WDOG_Unlock();
  262. WDOG->CS2 = u8Cs2;
  263. WDOG->TOVAL = u16TOVAL;
  264. WDOG->WIN = u16WIN;
  265. WDOG->CS1 = u8Cs1;
  266. }
  267. /********************************************************************/
  268. /*! @} End of wdog_api_list */