gd32e23x_fwdgt.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*!
  2. \file gd32e23x_fwdgt.c
  3. \brief FWDGT driver
  4. \version 2024-02-22, V2.1.0, firmware for GD32E23x
  5. */
  6. /*
  7. Copyright (c) 2024, GigaDevice Semiconductor Inc.
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright notice, this
  12. list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holder nor the names of its contributors
  17. may be used to endorse or promote products derived from this software without
  18. specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. OF SUCH DAMAGE.
  29. */
  30. #include "gd32e23x_fwdgt.h"
  31. /*!
  32. \brief enable write access to FWDGT_PSC and FWDGT_RLD and FWDGT_WND
  33. \param[in] none
  34. \param[out] none
  35. \retval none
  36. */
  37. void fwdgt_write_enable(void)
  38. {
  39. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  40. }
  41. /*!
  42. \brief disable write access to FWDGT_PSC,FWDGT_RLD and FWDGT_WND
  43. \param[in] none
  44. \param[out] none
  45. \retval none
  46. */
  47. void fwdgt_write_disable(void)
  48. {
  49. FWDGT_CTL = FWDGT_WRITEACCESS_DISABLE;
  50. }
  51. /*!
  52. \brief start the free watchdog timer counter
  53. \param[in] none
  54. \param[out] none
  55. \retval none
  56. */
  57. void fwdgt_enable(void)
  58. {
  59. FWDGT_CTL = FWDGT_KEY_ENABLE;
  60. }
  61. /*!
  62. \brief configure the free watchdog timer counter prescaler value
  63. \param[in] prescaler_value: specify prescaler value
  64. only one parameter can be selected which is shown as below:
  65. \arg FWDGT_PSC_DIV4: FWDGT prescaler set to 4
  66. \arg FWDGT_PSC_DIV8: FWDGT prescaler set to 8
  67. \arg FWDGT_PSC_DIV16: FWDGT prescaler set to 16
  68. \arg FWDGT_PSC_DIV32: FWDGT prescaler set to 32
  69. \arg FWDGT_PSC_DIV64: FWDGT prescaler set to 64
  70. \arg FWDGT_PSC_DIV128: FWDGT prescaler set to 128
  71. \arg FWDGT_PSC_DIV256: FWDGT prescaler set to 256
  72. \param[out] none
  73. \retval ErrStatus: ERROR or SUCCESS
  74. */
  75. ErrStatus fwdgt_prescaler_value_config(uint16_t prescaler_value)
  76. {
  77. uint32_t timeout = FWDGT_PSC_TIMEOUT;
  78. uint32_t flag_status = RESET;
  79. /* enable write access to FWDGT_PSC */
  80. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  81. /* wait until the PUD flag to be reset */
  82. do{
  83. flag_status = FWDGT_STAT & FWDGT_STAT_PUD;
  84. }while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
  85. if ((uint32_t)RESET != flag_status){
  86. return ERROR;
  87. }
  88. /* configure FWDGT */
  89. FWDGT_PSC = (uint32_t)prescaler_value;
  90. return SUCCESS;
  91. }
  92. /*!
  93. \brief configure the free watchdog timer counter reload value
  94. \param[in] reload_value: specify reload value(0x0000 - 0x0FFF)
  95. \param[out] none
  96. \retval ErrStatus: ERROR or SUCCESS
  97. */
  98. ErrStatus fwdgt_reload_value_config(uint16_t reload_value)
  99. {
  100. uint32_t timeout = FWDGT_RLD_TIMEOUT;
  101. uint32_t flag_status = RESET;
  102. /* enable write access to FWDGT_RLD */
  103. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  104. /* wait until the RUD flag to be reset */
  105. do{
  106. flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
  107. }while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
  108. if ((uint32_t)RESET != flag_status){
  109. return ERROR;
  110. }
  111. FWDGT_RLD = RLD_RLD(reload_value);
  112. return SUCCESS;
  113. }
  114. /*!
  115. \brief configure the free watchdog timer counter window value
  116. \param[in] window_value: specify window value(0x0000 - 0x0FFF)
  117. \param[out] none
  118. \retval ErrStatus: ERROR or SUCCESS
  119. */
  120. ErrStatus fwdgt_window_value_config(uint16_t window_value)
  121. {
  122. uint32_t time_index = FWDGT_WND_TIMEOUT;
  123. uint32_t flag_status = RESET;
  124. /* enable write access to FWDGT_WND */
  125. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  126. /* wait until the WUD flag to be reset */
  127. do{
  128. flag_status = FWDGT_STAT & FWDGT_STAT_WUD;
  129. }while((--time_index > 0U) && ((uint32_t)RESET != flag_status));
  130. if ((uint32_t)RESET != flag_status){
  131. return ERROR;
  132. }
  133. FWDGT_WND = WND_WND(window_value);
  134. return SUCCESS;
  135. }
  136. /*!
  137. \brief reload the counter of FWDGT
  138. \param[in] none
  139. \param[out] none
  140. \retval none
  141. */
  142. void fwdgt_counter_reload(void)
  143. {
  144. FWDGT_CTL = FWDGT_KEY_RELOAD;
  145. }
  146. /*!
  147. \brief configure counter reload value, and prescaler divider value
  148. \param[in] reload_value: specify reload value(0x0000 - 0x0FFF)
  149. \param[in] prescaler_div: FWDGT prescaler value
  150. only one parameter can be selected which is shown as below:
  151. \arg FWDGT_PSC_DIV4: FWDGT prescaler set to 4
  152. \arg FWDGT_PSC_DIV8: FWDGT prescaler set to 8
  153. \arg FWDGT_PSC_DIV16: FWDGT prescaler set to 16
  154. \arg FWDGT_PSC_DIV32: FWDGT prescaler set to 32
  155. \arg FWDGT_PSC_DIV64: FWDGT prescaler set to 64
  156. \arg FWDGT_PSC_DIV128: FWDGT prescaler set to 128
  157. \arg FWDGT_PSC_DIV256: FWDGT prescaler set to 256
  158. \param[out] none
  159. \retval ErrStatus: ERROR or SUCCESS
  160. */
  161. ErrStatus fwdgt_config(uint16_t reload_value, uint8_t prescaler_div)
  162. {
  163. uint32_t timeout = FWDGT_PSC_TIMEOUT;
  164. uint32_t flag_status = RESET;
  165. /* enable write access to FWDGT_PSC,and FWDGT_RLD */
  166. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  167. /* wait until the PUD flag to be reset */
  168. do{
  169. flag_status = FWDGT_STAT & FWDGT_STAT_PUD;
  170. }while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
  171. if ((uint32_t)RESET != flag_status){
  172. return ERROR;
  173. }
  174. /* configure FWDGT */
  175. FWDGT_PSC = (uint32_t)prescaler_div;
  176. timeout = FWDGT_RLD_TIMEOUT;
  177. /* wait until the RUD flag to be reset */
  178. do{
  179. flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
  180. }while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
  181. if ((uint32_t)RESET != flag_status){
  182. return ERROR;
  183. }
  184. FWDGT_RLD = RLD_RLD(reload_value);
  185. /* reload the counter */
  186. FWDGT_CTL = FWDGT_KEY_RELOAD;
  187. return SUCCESS;
  188. }
  189. /*!
  190. \brief get flag state of FWDGT
  191. \param[in] flag: flag to get
  192. only one parameter can be selected which is shown as below:
  193. \arg FWDGT_FLAG_PUD: a write operation to FWDGT_PSC register is on going
  194. \arg FWDGT_FLAG_RUD: a write operation to FWDGT_RLD register is on going
  195. \arg FWDGT_FLAG_WUD: a write operation to FWDGT_WND register is on going
  196. \param[out] none
  197. \retval FlagStatus: SET or RESET
  198. */
  199. FlagStatus fwdgt_flag_get(uint16_t flag)
  200. {
  201. if(RESET != (FWDGT_STAT & flag)){
  202. return SET;
  203. }
  204. return RESET;
  205. }