gd32f10x_wwdgt.c 4.6 KB

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