driver_wdt.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * @File name : driver_wdt.c
  3. * @Author : Bluetrum IOT Team
  4. * @Date : 2023-02-15
  5. * @Description : This file provides functions to manage the most functionalities
  6. * of the WDT peripheral.
  7. *
  8. * Copyright (c) by Bluetrum, All Rights reserved.
  9. */
  10. #include "driver_wdt.h"
  11. /**
  12. * @brief Enable or disable the WDT peripheral.
  13. * @param state: the state of the WDT peripheral.
  14. * this parameter can be: ENABLE or DISABLE.
  15. * @retval None
  16. */
  17. void wdt_cmd(FUNCTIONAL_STATE state)
  18. {
  19. uint32_t temp_reg = WDT->con;
  20. if (state) {
  21. temp_reg |= WDTCON_WDTEN_WR_0;
  22. } else {
  23. temp_reg &= ~WDTCON_WDTEN_WR;
  24. temp_reg |= (uint32_t)(0x0a << 4);
  25. }
  26. WDT->con = temp_reg;
  27. }
  28. /**
  29. * @brief Enable or disable the WDT reset.
  30. * @param state: the state of the WDT reset.
  31. * this parameter can be: ENABLE or DISABLE.
  32. * @retval None
  33. */
  34. void wdt_reset_cmd(FUNCTIONAL_STATE state)
  35. {
  36. uint32_t temp_reg = WDT->con;
  37. if (state) {
  38. temp_reg |= WDTCON_RSTEN_WR_0;
  39. } else {
  40. temp_reg &= ~WDTCON_RSTEN_WR;
  41. temp_reg |= (uint32_t)(0x0a << 8);
  42. }
  43. WDT->con = temp_reg;
  44. }
  45. /**
  46. * @brief Enable or disable the WDT interrupt.
  47. * @param state: the state of the WDT interrupt.
  48. * this parameter can be: ENABLE or DISABLE.
  49. * @retval None
  50. */
  51. void wdt_it_cmd(FUNCTIONAL_STATE state)
  52. {
  53. uint32_t temp_reg = WDT->con;
  54. temp_reg &= ~WDTCON_WDTIE_WR;
  55. if (state) {
  56. temp_reg |= (uint32_t)(0x05 << 12);
  57. } else {
  58. temp_reg |= (uint32_t)(0x0a << 12);
  59. }
  60. WDT->con = temp_reg;
  61. }
  62. /**
  63. * @brief Secect the WDT clock.
  64. * @param clk: The WDT clock.
  65. * this parameter can be one of the following values:
  66. * @arg WDT_CLK_RC32K: RC32K.
  67. * @arg WDT_CLK_X32K: X32K from 26M divider.
  68. * @retval None.
  69. */
  70. void wdt_clk_select(WDT_CLK_TYPEDEF clk)
  71. {
  72. uint32_t temp_reg = WDT->con;
  73. temp_reg &= ~WDTCON_WDTCSEL_WR;
  74. temp_reg |= clk;
  75. if(clk) {
  76. temp_reg |= (uint32_t)(0x05 << 16);
  77. } else {
  78. temp_reg |= (uint32_t)(0x0a << 16);
  79. }
  80. WDT->con = temp_reg;
  81. }
  82. /**
  83. * @brief Secect the WDT time.
  84. * @param time: The WDT time.
  85. * this parameter can be one of the following values:
  86. * @arg WDT_TIME_1MS: 1ms.
  87. * @arg WDT_TIME_256MS: 256ms.
  88. * @arg WDT_TIME_512MS: 512ms.
  89. * @arg WDT_TIME_1024MS: 1024ms.
  90. * @arg WDT_TIME_2048MS: 2048ms.
  91. * @arg WDT_TIME_4096MS: 4096ms.
  92. * @arg WDT_TIME_8192MS: 8192ms.
  93. * @arg WDT_TIME_16384MS: 16384ms.
  94. * @retval None.
  95. */
  96. void wdt_time_select(WDT_TIME_TYPEDEF time)
  97. {
  98. uint32_t temp_reg;
  99. temp_reg = WDT->con;
  100. temp_reg &= ~(WDTCON_TMRSEL | WDTCON_TMRSEL_WR);
  101. temp_reg |= time;
  102. temp_reg |= (0x0a << 24);
  103. WDT->con = temp_reg;
  104. }
  105. /**
  106. * @brief Get the WDT flag.
  107. * @param wdt_flag: specifies the flag to set.
  108. * this parameter can be one of the following values:
  109. * @arg WDT_FLAG_PENDING: WDT pending.
  110. * @retval The state of wdt_flag (SET or RESET).
  111. */
  112. FLAG_STATE wdt_get_flag(uint32_t wdt_flag)
  113. {
  114. if ((WDT->con & WDT_FLAG_PENDING) != RESET) {
  115. return SET;
  116. } else {
  117. return RESET;
  118. }
  119. }
  120. /**
  121. * @brief Clear the WDT pending.
  122. * @param None
  123. * @retval None
  124. */
  125. void wdt_clear(void)
  126. {
  127. WDT->con |= (uint32_t)0x0a;
  128. }