gd32vf103_wwdgt.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*!
  2. \file gd32vf103_wwdgt.c
  3. \brief WWDGT 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_wwdgt.h"
  31. /* write value to WWDGT_CTL_CNT bit field */
  32. #define CTL_CNT(regval) (BITS(0,6) & ((uint32_t)(regval) << 0))
  33. /* write value to WWDGT_CFG_WIN bit field */
  34. #define CFG_WIN(regval) (BITS(0,6) & ((uint32_t)(regval) << 0))
  35. /*!
  36. \brief reset the window watchdog timer configuration
  37. \param[in] none
  38. \param[out] none
  39. \retval none
  40. */
  41. void wwdgt_deinit(void)
  42. {
  43. rcu_periph_reset_enable(RCU_WWDGTRST);
  44. rcu_periph_reset_disable(RCU_WWDGTRST);
  45. }
  46. /*!
  47. \brief start the window watchdog timer counter
  48. \param[in] none
  49. \param[out] none
  50. \retval none
  51. */
  52. void wwdgt_enable(void)
  53. {
  54. WWDGT_CTL |= WWDGT_CTL_WDGTEN;
  55. }
  56. /*!
  57. \brief configure the window watchdog timer counter value
  58. \param[in] counter_value: 0x00 - 0x7F
  59. \param[out] none
  60. \retval none
  61. */
  62. void wwdgt_counter_update(uint16_t counter_value)
  63. {
  64. uint32_t reg = 0U;
  65. reg = (WWDGT_CTL & (~WWDGT_CTL_CNT));
  66. reg |= CTL_CNT(counter_value);
  67. WWDGT_CTL = reg;
  68. }
  69. /*!
  70. \brief configure counter value, window value, and prescaler divider value
  71. \param[in] counter: 0x00 - 0x7F
  72. \param[in] window: 0x00 - 0x7F
  73. \param[in] prescaler: wwdgt prescaler value
  74. only one parameter can be selected which is shown as below:
  75. \arg WWDGT_CFG_PSC_DIV1: the time base of window watchdog counter = (PCLK1/4096)/1
  76. \arg WWDGT_CFG_PSC_DIV2: the time base of window watchdog counter = (PCLK1/4096)/2
  77. \arg WWDGT_CFG_PSC_DIV4: the time base of window watchdog counter = (PCLK1/4096)/4
  78. \arg WWDGT_CFG_PSC_DIV8: the time base of window watchdog counter = (PCLK1/4096)/8
  79. \param[out] none
  80. \retval none
  81. */
  82. void wwdgt_config(uint16_t counter, uint16_t window, uint32_t prescaler)
  83. {
  84. uint32_t reg_cfg = 0U, reg_ctl = 0U;
  85. /* clear WIN and PSC bits, clear CNT bit */
  86. reg_cfg = (WWDGT_CFG &(~(WWDGT_CFG_WIN|WWDGT_CFG_PSC)));
  87. reg_ctl = (WWDGT_CTL &(~WWDGT_CTL_CNT));
  88. /* configure WIN and PSC bits, configure CNT bit */
  89. reg_cfg |= CFG_WIN(window);
  90. reg_cfg |= prescaler;
  91. reg_ctl |= CTL_CNT(counter);
  92. WWDGT_CTL = reg_ctl;
  93. WWDGT_CFG = reg_cfg;
  94. }
  95. /*!
  96. \brief check early wakeup interrupt state of WWDGT
  97. \param[in] none
  98. \param[out] none
  99. \retval FlagStatus: SET or RESET
  100. */
  101. FlagStatus wwdgt_flag_get(void)
  102. {
  103. if(WWDGT_STAT & WWDGT_STAT_EWIF){
  104. return SET;
  105. }
  106. return RESET;
  107. }
  108. /*!
  109. \brief clear early wakeup interrupt state of WWDGT
  110. \param[in] none
  111. \param[out] none
  112. \retval none
  113. */
  114. void wwdgt_flag_clear(void)
  115. {
  116. WWDGT_STAT &= (~WWDGT_STAT_EWIF);
  117. }
  118. /*!
  119. \brief enable early wakeup interrupt of WWDGT
  120. \param[in] none
  121. \param[out] none
  122. \retval none
  123. */
  124. void wwdgt_interrupt_enable(void)
  125. {
  126. WWDGT_CFG |= WWDGT_CFG_EWIE;
  127. }