drv_wdt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (C) 2022-2024, Xiaohua Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-02-08 CDT first version
  9. * 2023-12-01 CDT added swdt support
  10. */
  11. #include "board.h"
  12. #ifdef BSP_USING_WDT_TMR
  13. #include <math.h>
  14. #include <string.h>
  15. // #define DRV_DEBUG
  16. #define LOG_TAG "drv_wdt"
  17. #include <drv_log.h>
  18. enum
  19. {
  20. WDT_INIT_ING,
  21. WDT_INIT_OVER,
  22. WDT_IS_ENABLE
  23. };
  24. static struct rt_watchdog_ops _ops;
  25. #ifdef BSP_USING_WDT
  26. struct hc32_wdt_obj
  27. {
  28. rt_watchdog_t watchdog;
  29. stc_wdt_init_t stcwdg;
  30. rt_uint32_t pclk3;
  31. rt_uint8_t sta;
  32. rt_uint8_t index;
  33. };
  34. static struct hc32_wdt_obj hc32_wdt;
  35. struct time_match
  36. {
  37. uint32_t u32ClockDiv;
  38. uint32_t u32CountPeriod;
  39. float timeout_s;
  40. };
  41. static uint32_t const Div[] = {4U, 64U, 128U, 256U, 512U, 1024U, 2048U, 8192U};
  42. static uint32_t const Peri[] = {256U, 4096U, 16384U, 65536U};
  43. static struct time_match wdt_match[(sizeof(Div) / sizeof(Div[0])) * (sizeof(Peri) / sizeof(Peri[0]))];
  44. static void wdt_match_init(uint32_t clock)
  45. {
  46. int i, j;
  47. for (i = 0; i < (sizeof(Div) / sizeof(Div[0])); i++)
  48. {
  49. for (j = 0; j < (sizeof(Peri) / sizeof(Peri[0])); j++)
  50. {
  51. wdt_match[j + i * (sizeof(Peri) / sizeof(Peri[0]))].u32ClockDiv = Div[i];
  52. wdt_match[j + i * (sizeof(Peri) / sizeof(Peri[0]))].u32CountPeriod = Peri[j];
  53. wdt_match[j + i * (sizeof(Peri) / sizeof(Peri[0]))].timeout_s = (Div[i] * Peri[j]) / (float)clock;
  54. }
  55. }
  56. }
  57. static void wdt_match_sort(void)
  58. {
  59. int i, j;
  60. struct time_match Temp;
  61. /* bubble sort */
  62. for (i = 0; i < ((sizeof(Div) / sizeof(Div[0])) * (sizeof(Peri) / sizeof(Peri[0])) - 1); i++)
  63. {
  64. for (j = 0; j < ((sizeof(Div) / sizeof(Div[0])) * (sizeof(Peri) / sizeof(Peri[0])) - i - 1); j++)
  65. {
  66. if (wdt_match[j].timeout_s > wdt_match[j + 1].timeout_s)
  67. {
  68. memcpy(&Temp, &wdt_match[j], sizeof(struct time_match));
  69. memcpy(&wdt_match[j], &wdt_match[j + 1], sizeof(struct time_match));
  70. memcpy(&wdt_match[j + 1], &Temp, sizeof(struct time_match));
  71. }
  72. }
  73. }
  74. }
  75. static int wdt_match_find_index(uint32_t time_out)
  76. {
  77. int i;
  78. /* Min and Max case */
  79. if (time_out <= wdt_match[0].timeout_s)
  80. {
  81. return 0;
  82. }
  83. else if (time_out >= wdt_match[((sizeof(Div) / sizeof(Div[0])) * (sizeof(Peri) / sizeof(Peri[0]))) - 1].timeout_s)
  84. {
  85. return (((sizeof(Div) / sizeof(Div[0])) * (sizeof(Peri) / sizeof(Peri[0]))) - 1);
  86. }
  87. /* Other case */
  88. for (i = 1; i < (((sizeof(Div) / sizeof(Div[0])) * (sizeof(Peri) / sizeof(Peri[0]))) - 1); i++)
  89. {
  90. if (time_out >= wdt_match[i].timeout_s && time_out < wdt_match[i + 1].timeout_s)
  91. {
  92. /* Min difference */
  93. if (time_out - wdt_match[i].timeout_s < wdt_match[i + 1].timeout_s - time_out)
  94. {
  95. return i;
  96. }
  97. else
  98. {
  99. return (i + 1);
  100. }
  101. }
  102. }
  103. /* Not match case */
  104. return (((sizeof(Div) / sizeof(Div[0])) * (sizeof(Peri) / sizeof(Peri[0]))) - 1);
  105. }
  106. static rt_uint32_t wdt_match_find_period(rt_uint32_t Period)
  107. {
  108. rt_uint32_t CountPeriod = 0U;
  109. switch (Period)
  110. {
  111. case 256U:
  112. CountPeriod = WDT_CNT_PERIOD256;
  113. break;
  114. case 4096U:
  115. CountPeriod = WDT_CNT_PERIOD4096;
  116. break;
  117. case 16384U:
  118. CountPeriod = WDT_CNT_PERIOD16384;
  119. break;
  120. case 65536U:
  121. CountPeriod = WDT_CNT_PERIOD65536;
  122. break;
  123. default:
  124. break;
  125. }
  126. return CountPeriod;
  127. }
  128. static rt_uint32_t wdt_get_timeout_s(void)
  129. {
  130. /* timeout(s) = PERI * DIV / PCLK3 */
  131. return ((rt_uint32_t)(wdt_match[hc32_wdt.index].u32CountPeriod * wdt_match[hc32_wdt.index].u32ClockDiv / (float)hc32_wdt.pclk3));
  132. }
  133. static rt_uint32_t wdt_get_timeleft_s(void)
  134. {
  135. /* wdt is down counter */
  136. return ((rt_uint32_t)(WDT_GetCountValue() * wdt_match[hc32_wdt.index].u32ClockDiv / (float)hc32_wdt.pclk3));
  137. }
  138. static rt_err_t _wdt_init(rt_watchdog_t *wdt)
  139. {
  140. hc32_wdt.pclk3 = CLK_GetBusClockFreq(CLK_BUS_PCLK3);
  141. if (!hc32_wdt.pclk3)
  142. {
  143. LOG_E("pclk3 getbusclockfreq failed.");
  144. return -RT_ERROR;
  145. }
  146. wdt_match_init(hc32_wdt.pclk3);
  147. wdt_match_sort();
  148. hc32_wdt.stcwdg.u32RefreshRange = WDT_RANGE_0TO100PCT;
  149. #ifdef BSP_WDT_CONTINUE_COUNT
  150. hc32_wdt.stcwdg.u32LPMCount = WDT_LPM_CNT_CONTINUE;
  151. #else
  152. hc32_wdt.stcwdg.u32LPMCount = WDT_LPM_CNT_STOP;
  153. #endif
  154. hc32_wdt.stcwdg.u32ExceptionType = WDT_EXP_TYPE_RST;
  155. hc32_wdt.sta = WDT_INIT_ING;
  156. /* WDT_CR register only support write once,so can't call WDT_Init of ther */
  157. return RT_EOK;
  158. }
  159. static rt_err_t _wdt_control(rt_watchdog_t *wdt, int cmd, void *arg)
  160. {
  161. switch (cmd)
  162. {
  163. /* feed the watchdog */
  164. case RT_DEVICE_CTRL_WDT_KEEPALIVE:
  165. /* Prevention of unexpected start-up when feed dog */
  166. if (hc32_wdt.sta == WDT_IS_ENABLE)
  167. {
  168. WDT_FeedDog();
  169. }
  170. break;
  171. /* set watchdog timeout */
  172. case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
  173. hc32_wdt.index = wdt_match_find_index((*((rt_uint32_t *)arg)));
  174. hc32_wdt.stcwdg.u32CountPeriod = wdt_match_find_period(wdt_match[hc32_wdt.index].u32CountPeriod);
  175. hc32_wdt.stcwdg.u32ClockDiv = ((uint32_t)log2(wdt_match[hc32_wdt.index].u32ClockDiv) << WDT_CR_CKS_POS);
  176. if (WDT_Init(&hc32_wdt.stcwdg) != LL_OK)
  177. {
  178. LOG_E("wdg set timeout failed.");
  179. return -RT_ERROR;
  180. }
  181. hc32_wdt.sta = WDT_INIT_OVER;
  182. LOG_D("wdg set timeout successful. timeout = %d s", wdt_get_timeout_s());
  183. break;
  184. case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
  185. (*((rt_uint32_t *)arg)) = wdt_get_timeout_s();
  186. break;
  187. case RT_DEVICE_CTRL_WDT_START:
  188. if (hc32_wdt.sta == WDT_INIT_ING)
  189. {
  190. LOG_E("please set the timeout values.");
  191. return -RT_ERROR;
  192. }
  193. /* First reload counter to start WDT */
  194. WDT_FeedDog();
  195. hc32_wdt.sta = WDT_IS_ENABLE;
  196. break;
  197. case RT_DEVICE_CTRL_WDT_GET_TIMELEFT:
  198. (*((rt_uint32_t *)arg)) = wdt_get_timeleft_s();
  199. break;
  200. default:
  201. LOG_W("This command is not supported.");
  202. return -RT_ERROR;
  203. }
  204. return RT_EOK;
  205. }
  206. int rt_wdt_init(void)
  207. {
  208. _ops.init = &_wdt_init;
  209. _ops.control = &_wdt_control;
  210. hc32_wdt.watchdog.ops = &_ops;
  211. /* register watchdog device */
  212. if (rt_hw_watchdog_register(&hc32_wdt.watchdog, "wdt", RT_DEVICE_FLAG_DEACTIVATE, RT_NULL) != RT_EOK)
  213. {
  214. LOG_E("wdt device register failed.");
  215. return -RT_ERROR;
  216. }
  217. LOG_D("wdt device register success.");
  218. return RT_EOK;
  219. }
  220. INIT_BOARD_EXPORT(rt_wdt_init);
  221. #else /* BSP_USING_WDT */
  222. struct hc32_swdt_obj
  223. {
  224. rt_watchdog_t watchdog;
  225. stc_swdt_init_t stcwdg;
  226. rt_uint32_t swdtclk;
  227. rt_uint8_t sta;
  228. rt_uint8_t index;
  229. };
  230. static struct hc32_swdt_obj hc32_swdt;
  231. struct time_match
  232. {
  233. uint32_t u32ClockDiv;
  234. uint32_t u32CountPeriod;
  235. float timeout_s;
  236. };
  237. static uint32_t const Div[] = {1U, 16U, 32U, 64U, 128U, 256U, 2048U};
  238. static uint32_t const Peri[] = {256U, 4096U, 16384U, 65536U};
  239. static struct time_match swdt_match[(sizeof(Div) / sizeof(Div[0])) * (sizeof(Peri) / sizeof(Peri[0]))];
  240. static void swdt_match_init(uint32_t clock)
  241. {
  242. int i, j;
  243. for (i = 0; i < (sizeof(Div) / sizeof(Div[0])); i++)
  244. {
  245. for (j = 0; j < (sizeof(Peri) / sizeof(Peri[0])); j++)
  246. {
  247. swdt_match[j + i * (sizeof(Peri) / sizeof(Peri[0]))].u32ClockDiv = Div[i];
  248. swdt_match[j + i * (sizeof(Peri) / sizeof(Peri[0]))].u32CountPeriod = Peri[j];
  249. swdt_match[j + i * (sizeof(Peri) / sizeof(Peri[0]))].timeout_s = (Div[i] * Peri[j]) / (float)clock;
  250. }
  251. }
  252. }
  253. static void swdt_match_sort(void)
  254. {
  255. int i, j;
  256. struct time_match Temp;
  257. /* bubble sort */
  258. for (i = 0; i < ((sizeof(Div) / sizeof(Div[0])) * (sizeof(Peri) / sizeof(Peri[0])) - 1); i++)
  259. {
  260. for (j = 0; j < ((sizeof(Div) / sizeof(Div[0])) * (sizeof(Peri) / sizeof(Peri[0])) - i - 1); j++)
  261. {
  262. if (swdt_match[j].timeout_s > swdt_match[j + 1].timeout_s)
  263. {
  264. memcpy(&Temp, &swdt_match[j], sizeof(struct time_match));
  265. memcpy(&swdt_match[j], &swdt_match[j + 1], sizeof(struct time_match));
  266. memcpy(&swdt_match[j + 1], &Temp, sizeof(struct time_match));
  267. }
  268. }
  269. }
  270. }
  271. static int swdt_match_find_index(uint32_t time_out)
  272. {
  273. int i;
  274. /* Min and Max case */
  275. if (time_out <= swdt_match[0].timeout_s)
  276. {
  277. return 0;
  278. }
  279. else if (time_out >= swdt_match[((sizeof(Div) / sizeof(Div[0])) * (sizeof(Peri) / sizeof(Peri[0]))) - 1].timeout_s)
  280. {
  281. return (((sizeof(Div) / sizeof(Div[0])) * (sizeof(Peri) / sizeof(Peri[0]))) - 1);
  282. }
  283. /* Other case */
  284. for (i = 1; i < (((sizeof(Div) / sizeof(Div[0])) * (sizeof(Peri) / sizeof(Peri[0]))) - 1); i++)
  285. {
  286. if (time_out >= swdt_match[i].timeout_s && time_out < swdt_match[i + 1].timeout_s)
  287. {
  288. /* Min difference */
  289. if (time_out - swdt_match[i].timeout_s < swdt_match[i + 1].timeout_s - time_out)
  290. {
  291. return i;
  292. }
  293. else
  294. {
  295. return (i + 1);
  296. }
  297. }
  298. }
  299. /* Not match case */
  300. return (((sizeof(Div) / sizeof(Div[0])) * (sizeof(Peri) / sizeof(Peri[0]))) - 1);
  301. }
  302. static rt_uint32_t swdt_match_find_period(rt_uint32_t Period)
  303. {
  304. rt_uint32_t CountPeriod = 0U;
  305. switch (Period)
  306. {
  307. case 256U:
  308. CountPeriod = SWDT_CNT_PERIOD256;
  309. break;
  310. case 4096U:
  311. CountPeriod = SWDT_CNT_PERIOD4096;
  312. break;
  313. case 16384U:
  314. CountPeriod = SWDT_CNT_PERIOD16384;
  315. break;
  316. case 65536U:
  317. CountPeriod = SWDT_CNT_PERIOD65536;
  318. break;
  319. default:
  320. break;
  321. }
  322. return CountPeriod;
  323. }
  324. static rt_uint32_t swdt_get_timeout_s(void)
  325. {
  326. /* timeout(s) = PERI * DIV / SWDTCLK */
  327. return ((rt_uint32_t)(swdt_match[hc32_swdt.index].u32CountPeriod * swdt_match[hc32_swdt.index].u32ClockDiv / (float)hc32_swdt.swdtclk));
  328. }
  329. static rt_uint32_t swdt_get_timeleft_s(void)
  330. {
  331. /* swdt is down counter */
  332. return ((rt_uint32_t)(SWDT_GetCountValue() * swdt_match[hc32_swdt.index].u32ClockDiv / (float)hc32_swdt.swdtclk));
  333. }
  334. static rt_err_t swdt_init(rt_watchdog_t *swdt)
  335. {
  336. hc32_swdt.swdtclk = 10000U;
  337. swdt_match_init(hc32_swdt.swdtclk);
  338. swdt_match_sort();
  339. hc32_swdt.stcwdg.u32RefreshRange = SWDT_RANGE_0TO100PCT;
  340. #ifdef BSP_WDT_CONTINUE_COUNT
  341. hc32_swdt.stcwdg.u32LPMCount = SWDT_LPM_CNT_CONTINUE;
  342. #else
  343. hc32_swdt.stcwdg.u32LPMCount = SWDT_LPM_CNT_STOP;
  344. #endif
  345. hc32_swdt.stcwdg.u32ExceptionType = SWDT_EXP_TYPE_RST;
  346. hc32_swdt.sta = WDT_INIT_ING;
  347. /* SWDT_CR register only support write once,so can't call swdt_Init of ther */
  348. return RT_EOK;
  349. }
  350. static rt_err_t swdt_control(rt_watchdog_t *swdt, int cmd, void *arg)
  351. {
  352. switch (cmd)
  353. {
  354. /* feed the watchdog */
  355. case RT_DEVICE_CTRL_WDT_KEEPALIVE:
  356. /* Prevention of unexpected start-up when feed dog */
  357. if (hc32_swdt.sta == WDT_IS_ENABLE)
  358. {
  359. SWDT_FeedDog();
  360. }
  361. break;
  362. /* set watchdog timeout */
  363. case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
  364. hc32_swdt.index = swdt_match_find_index((*((rt_uint32_t *)arg)));
  365. hc32_swdt.stcwdg.u32CountPeriod = swdt_match_find_period(swdt_match[hc32_swdt.index].u32CountPeriod);
  366. hc32_swdt.stcwdg.u32ClockDiv = ((uint32_t)log2(swdt_match[hc32_swdt.index].u32ClockDiv) << SWDT_CR_CKS_POS);
  367. if (SWDT_Init(&hc32_swdt.stcwdg) != LL_OK)
  368. {
  369. LOG_E("swdg set timeout failed.");
  370. return -RT_ERROR;
  371. }
  372. hc32_swdt.sta = WDT_INIT_OVER;
  373. LOG_D("swdg set timeout successful. timeout = %d s", swdt_get_timeout_s());
  374. break;
  375. case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
  376. (*((rt_uint32_t *)arg)) = swdt_get_timeout_s();
  377. break;
  378. case RT_DEVICE_CTRL_WDT_START:
  379. if (hc32_swdt.sta == WDT_INIT_ING)
  380. {
  381. LOG_E("please set the timeout values.");
  382. return -RT_ERROR;
  383. }
  384. /* First reload counter to start swdt */
  385. SWDT_FeedDog();
  386. hc32_swdt.sta = WDT_IS_ENABLE;
  387. break;
  388. case RT_DEVICE_CTRL_WDT_GET_TIMELEFT:
  389. (*((rt_uint32_t *)arg)) = swdt_get_timeleft_s();
  390. break;
  391. default:
  392. LOG_W("This command is not supported.");
  393. return -RT_ERROR;
  394. }
  395. return RT_EOK;
  396. }
  397. static int rt_hw_swdt_init(void)
  398. {
  399. _ops.init = &swdt_init;
  400. _ops.control = &swdt_control;
  401. hc32_swdt.watchdog.ops = &_ops;
  402. /* register watchdog device */
  403. if (rt_hw_watchdog_register(&hc32_swdt.watchdog, "swdt", RT_DEVICE_FLAG_DEACTIVATE, RT_NULL) != RT_EOK)
  404. {
  405. LOG_E("swdt device register failed.");
  406. return -RT_ERROR;
  407. }
  408. LOG_D("swdt device register success.");
  409. return RT_EOK;
  410. }
  411. INIT_BOARD_EXPORT(rt_hw_swdt_init);
  412. #endif /* BSP_USING_SWDT */
  413. #endif