gd32f10x_fwdgt.c 6.8 KB

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