gd32f3x0_fwdgt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*!
  2. \file gd32f3x0_fwdgt.c
  3. \brief FWDGT driver
  4. \version 2017-06-06, V1.0.0, firmware for GD32F3x0
  5. \version 2019-06-01, V2.0.0, firmware for GD32F3x0
  6. */
  7. /*
  8. Copyright (c) 2019, GigaDevice Semiconductor Inc.
  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 "gd32f3x0_fwdgt.h"
  31. /*!
  32. \brief enable write access to FWDGT_PSC and FWDGT_RLD
  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 window value
  63. \param[in] window_value: specify window value(0x0000 - 0x0FFF)
  64. \param[out] none
  65. \retval ErrStatus: ERROR or SUCCESS
  66. */
  67. ErrStatus fwdgt_window_value_config(uint16_t window_value)
  68. {
  69. uint32_t time_index = FWDGT_WND_TIMEOUT;
  70. uint32_t flag_status = RESET;
  71. /* enable write access to FWDGT_WND */
  72. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  73. /* wait until the WUD flag to be reset */
  74. do{
  75. flag_status = FWDGT_STAT & FWDGT_STAT_WUD;
  76. }while((--time_index > 0U) && (RESET != flag_status));
  77. if (RESET != flag_status){
  78. return ERROR;
  79. }
  80. FWDGT_WND = WND_WND(window_value);
  81. return SUCCESS;
  82. }
  83. /*!
  84. \brief reload the counter of FWDGT
  85. \param[in] none
  86. \param[out] none
  87. \retval none
  88. */
  89. void fwdgt_counter_reload(void)
  90. {
  91. FWDGT_CTL = FWDGT_KEY_RELOAD;
  92. }
  93. /*!
  94. \brief configure counter reload value, and prescaler divider value
  95. \param[in] reload_value: specify reload value(0x0000 - 0x0FFF)
  96. \param[in] prescaler_div: FWDGT prescaler value
  97. only one parameter can be selected which is shown as below:
  98. \arg FWDGT_PSC_DIV4: FWDGT prescaler set to 4
  99. \arg FWDGT_PSC_DIV8: FWDGT prescaler set to 8
  100. \arg FWDGT_PSC_DIV16: FWDGT prescaler set to 16
  101. \arg FWDGT_PSC_DIV32: FWDGT prescaler set to 32
  102. \arg FWDGT_PSC_DIV64: FWDGT prescaler set to 64
  103. \arg FWDGT_PSC_DIV128: FWDGT prescaler set to 128
  104. \arg FWDGT_PSC_DIV256: FWDGT prescaler set to 256
  105. \param[out] none
  106. \retval ErrStatus: ERROR or SUCCESS
  107. */
  108. ErrStatus fwdgt_config(uint16_t reload_value, uint8_t prescaler_div)
  109. {
  110. uint32_t timeout = FWDGT_PSC_TIMEOUT;
  111. uint32_t flag_status = RESET;
  112. /* enable write access to FWDGT_PSC,and FWDGT_RLD */
  113. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  114. /* wait until the PUD flag to be reset */
  115. do{
  116. flag_status = FWDGT_STAT & FWDGT_STAT_PUD;
  117. }while((--timeout > 0U) && (RESET != flag_status));
  118. if (RESET != flag_status){
  119. return ERROR;
  120. }
  121. /* configure FWDGT */
  122. FWDGT_PSC = (uint32_t)prescaler_div;
  123. timeout = FWDGT_RLD_TIMEOUT;
  124. /* wait until the RUD flag to be reset */
  125. do{
  126. flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
  127. }while((--timeout > 0U) && (RESET != flag_status));
  128. if (RESET != flag_status){
  129. return ERROR;
  130. }
  131. FWDGT_RLD = RLD_RLD(reload_value);
  132. /* reload the counter */
  133. FWDGT_CTL = FWDGT_KEY_RELOAD;
  134. return SUCCESS;
  135. }
  136. /*!
  137. \brief get flag state of FWDGT
  138. \param[in] flag: flag to get
  139. only one parameter can be selected which is shown as below:
  140. \arg FWDGT_FLAG_PUD: a write operation to FWDGT_PSC register is on going
  141. \arg FWDGT_FLAG_RUD: a write operation to FWDGT_RLD register is on going
  142. \arg FWDGT_FLAG_WUD: a write operation to FWDGT_WND register is on going
  143. \param[out] none
  144. \retval FlagStatus: SET or RESET
  145. */
  146. FlagStatus fwdgt_flag_get(uint16_t flag)
  147. {
  148. if(FWDGT_STAT & flag){
  149. return SET;
  150. }
  151. return RESET;
  152. }