gd32f4xx_fwdgt.c 5.0 KB

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