gd32e23x_fwdgt.c 7.6 KB

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