gd32f10x_fwdgt.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. */
  8. /*
  9. Copyright (c) 2018, GigaDevice Semiconductor Inc.
  10. All rights reserved.
  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. /* write value to FWDGT_CTL_CMD bit field */
  34. #define CTL_CMD(regval) (BITS(0,15) & ((uint32_t)(regval) << 0))
  35. /* write value to FWDGT_RLD_RLD bit field */
  36. #define RLD_RLD(regval) (BITS(0,11) & ((uint32_t)(regval) << 0))
  37. /*!
  38. \brief enable write access to FWDGT_PSC and FWDGT_RLD
  39. \param[in] none
  40. \param[out] none
  41. \retval none
  42. */
  43. void fwdgt_write_enable(void)
  44. {
  45. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  46. }
  47. /*!
  48. \brief disable write access to FWDGT_PSC and FWDGT_RLD
  49. \param[in] none
  50. \param[out] none
  51. \retval none
  52. */
  53. void fwdgt_write_disable(void)
  54. {
  55. FWDGT_CTL = FWDGT_WRITEACCESS_DISABLE;
  56. }
  57. /*!
  58. \brief start the free watchdog timer counter
  59. \param[in] none
  60. \param[out] none
  61. \retval none
  62. */
  63. void fwdgt_enable(void)
  64. {
  65. FWDGT_CTL = FWDGT_KEY_ENABLE;
  66. }
  67. /*!
  68. \brief reload the counter of FWDGT
  69. \param[in] none
  70. \param[out] none
  71. \retval none
  72. */
  73. void fwdgt_counter_reload(void)
  74. {
  75. FWDGT_CTL = FWDGT_KEY_RELOAD;
  76. }
  77. /*!
  78. \brief configure counter reload value, and prescaler divider value
  79. \param[in] reload_value: specify reload value(0x0000 - 0x0FFF)
  80. \param[in] prescaler_div: FWDGT prescaler value
  81. only one parameter can be selected which is shown as below:
  82. \arg FWDGT_PSC_DIV4: FWDGT prescaler set to 4
  83. \arg FWDGT_PSC_DIV8: FWDGT prescaler set to 8
  84. \arg FWDGT_PSC_DIV16: FWDGT prescaler set to 16
  85. \arg FWDGT_PSC_DIV32: FWDGT prescaler set to 32
  86. \arg FWDGT_PSC_DIV64: FWDGT prescaler set to 64
  87. \arg FWDGT_PSC_DIV128: FWDGT prescaler set to 128
  88. \arg FWDGT_PSC_DIV256: FWDGT prescaler set to 256
  89. \param[out] none
  90. \retval ErrStatus: ERROR or SUCCESS
  91. */
  92. ErrStatus fwdgt_config(uint16_t reload_value, uint8_t prescaler_div)
  93. {
  94. uint32_t timeout = FWDGT_PSC_TIMEOUT;
  95. uint32_t flag_status = RESET;
  96. /* enable write access to FWDGT_PSC,and FWDGT_RLD */
  97. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  98. /* wait until the PUD flag to be reset */
  99. do{
  100. flag_status = FWDGT_STAT & FWDGT_STAT_PUD;
  101. }while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
  102. if((uint32_t)RESET != flag_status){
  103. return ERROR;
  104. }
  105. /* configure FWDGT */
  106. FWDGT_PSC = (uint32_t)prescaler_div;
  107. timeout = FWDGT_RLD_TIMEOUT;
  108. /* wait until the RUD flag to be reset */
  109. do{
  110. flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
  111. }while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
  112. if((uint32_t)RESET != flag_status){
  113. return ERROR;
  114. }
  115. FWDGT_RLD = RLD_RLD(reload_value);
  116. /* reload the counter */
  117. FWDGT_CTL = FWDGT_KEY_RELOAD;
  118. return SUCCESS;
  119. }
  120. /*!
  121. \brief get flag state of FWDGT
  122. \param[in] flag: flag to get
  123. only one parameter can be selected which is shown as below:
  124. \arg FWDGT_FLAG_PUD: a write operation to FWDGT_PSC register is on going
  125. \arg FWDGT_FLAG_RUD: a write operation to FWDGT_RLD register is on going
  126. \param[out] none
  127. \retval FlagStatus: SET or RESET
  128. */
  129. FlagStatus fwdgt_flag_get(uint16_t flag)
  130. {
  131. if(FWDGT_STAT & flag){
  132. return SET;
  133. }
  134. return RESET;
  135. }