gd32vf103_fwdgt.c 5.1 KB

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