gd32e230_fwdgt.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*!
  2. \file gd32e230_fwdgt.c
  3. \brief FWDGT driver
  4. \version 2018-06-19, V1.0.0, firmware for GD32E230
  5. */
  6. /*
  7. Copyright (c) 2018, GigaDevice Semiconductor Inc.
  8. All rights reserved.
  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 "gd32e230_fwdgt.h"
  31. /* write value to FWDGT_CTL_CMD bit field */
  32. #define CTL_CMD(regval) (BITS(0,15) & ((uint32_t)(regval) << 0U)) /*!< write value to FWDGT_CTL_CMD bit field */
  33. /* write value to FWDGT_RLD_RLD bit field */
  34. #define RLD_RLD(regval) (BITS(0,11) & ((uint32_t)(regval) << 0U)) /*!< write value to FWDGT_RLD_RLD bit field */
  35. /* write value to FWDGT_WND_WND bit field */
  36. #define WND_WND(regval) (BITS(0,11) & ((uint32_t)(regval) << 0U)) /*!< write value to FWDGT_WND_WND bit field */
  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,FWDGT_RLD and FWDGT_WND
  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 configure the free watchdog timer counter prescaler value
  69. \param[in] prescaler_value: specify prescaler value
  70. only one parameter can be selected which is shown as below:
  71. \arg FWDGT_PSC_DIV4: FWDGT prescaler set to 4
  72. \arg FWDGT_PSC_DIV8: FWDGT prescaler set to 8
  73. \arg FWDGT_PSC_DIV16: FWDGT prescaler set to 16
  74. \arg FWDGT_PSC_DIV32: FWDGT prescaler set to 32
  75. \arg FWDGT_PSC_DIV64: FWDGT prescaler set to 64
  76. \arg FWDGT_PSC_DIV128: FWDGT prescaler set to 128
  77. \arg FWDGT_PSC_DIV256: FWDGT prescaler set to 256
  78. \param[out] none
  79. \retval ErrStatus: ERROR or SUCCESS
  80. */
  81. ErrStatus fwdgt_prescaler_value_config(uint16_t prescaler_value)
  82. {
  83. uint32_t timeout = FWDGT_PSC_TIMEOUT;
  84. uint32_t flag_status = RESET;
  85. /* enable write access to FWDGT_PSC */
  86. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  87. /* wait until the PUD flag to be reset */
  88. do{
  89. flag_status = FWDGT_STAT & FWDGT_STAT_PUD;
  90. }while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
  91. if ((uint32_t)RESET != flag_status){
  92. return ERROR;
  93. }
  94. /* configure FWDGT */
  95. FWDGT_PSC = (uint32_t)prescaler_value;
  96. return SUCCESS;
  97. }
  98. /*!
  99. \brief configure the free watchdog timer counter reload value
  100. \param[in] reload_value: specify reload value(0x0000 - 0x0FFF)
  101. \param[out] none
  102. \retval ErrStatus: ERROR or SUCCESS
  103. */
  104. ErrStatus fwdgt_reload_value_config(uint16_t reload_value)
  105. {
  106. uint32_t timeout = FWDGT_RLD_TIMEOUT;
  107. uint32_t flag_status = RESET;
  108. /* enable write access to FWDGT_RLD */
  109. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  110. /* wait until the RUD flag to be reset */
  111. do{
  112. flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
  113. }while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
  114. if ((uint32_t)RESET != flag_status){
  115. return ERROR;
  116. }
  117. FWDGT_RLD = RLD_RLD(reload_value);
  118. return SUCCESS;
  119. }
  120. /*!
  121. \brief configure the free watchdog timer counter window value
  122. \param[in] window_value: specify window value(0x0000 - 0x0FFF)
  123. \param[out] none
  124. \retval ErrStatus: ERROR or SUCCESS
  125. */
  126. ErrStatus fwdgt_window_value_config(uint16_t window_value)
  127. {
  128. uint32_t time_index = FWDGT_WND_TIMEOUT;
  129. uint32_t flag_status = RESET;
  130. /* enable write access to FWDGT_WND */
  131. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  132. /* wait until the WUD flag to be reset */
  133. do{
  134. flag_status = FWDGT_STAT & FWDGT_STAT_WUD;
  135. }while((--time_index > 0U) && ((uint32_t)RESET != flag_status));
  136. if ((uint32_t)RESET != flag_status){
  137. return ERROR;
  138. }
  139. FWDGT_WND = WND_WND(window_value);
  140. return SUCCESS;
  141. }
  142. /*!
  143. \brief reload the counter of FWDGT
  144. \param[in] none
  145. \param[out] none
  146. \retval none
  147. */
  148. void fwdgt_counter_reload(void)
  149. {
  150. FWDGT_CTL = FWDGT_KEY_RELOAD;
  151. }
  152. /*!
  153. \brief configure counter reload value, and prescaler divider value
  154. \param[in] reload_value: specify reload value(0x0000 - 0x0FFF)
  155. \param[in] prescaler_div: FWDGT prescaler value
  156. only one parameter can be selected which is shown as below:
  157. \arg FWDGT_PSC_DIV4: FWDGT prescaler set to 4
  158. \arg FWDGT_PSC_DIV8: FWDGT prescaler set to 8
  159. \arg FWDGT_PSC_DIV16: FWDGT prescaler set to 16
  160. \arg FWDGT_PSC_DIV32: FWDGT prescaler set to 32
  161. \arg FWDGT_PSC_DIV64: FWDGT prescaler set to 64
  162. \arg FWDGT_PSC_DIV128: FWDGT prescaler set to 128
  163. \arg FWDGT_PSC_DIV256: FWDGT prescaler set to 256
  164. \param[out] none
  165. \retval ErrStatus: ERROR or SUCCESS
  166. */
  167. ErrStatus fwdgt_config(uint16_t reload_value, uint8_t prescaler_div)
  168. {
  169. uint32_t timeout = FWDGT_PSC_TIMEOUT;
  170. uint32_t flag_status = RESET;
  171. /* enable write access to FWDGT_PSC,and FWDGT_RLD */
  172. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  173. /* wait until the PUD flag to be reset */
  174. do{
  175. flag_status = FWDGT_STAT & FWDGT_STAT_PUD;
  176. }while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
  177. if ((uint32_t)RESET != flag_status){
  178. return ERROR;
  179. }
  180. /* configure FWDGT */
  181. FWDGT_PSC = (uint32_t)prescaler_div;
  182. timeout = FWDGT_RLD_TIMEOUT;
  183. /* wait until the RUD flag to be reset */
  184. do{
  185. flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
  186. }while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
  187. if ((uint32_t)RESET != flag_status){
  188. return ERROR;
  189. }
  190. FWDGT_RLD = RLD_RLD(reload_value);
  191. /* reload the counter */
  192. FWDGT_CTL = FWDGT_KEY_RELOAD;
  193. return SUCCESS;
  194. }
  195. /*!
  196. \brief get flag state of FWDGT
  197. \param[in] flag: flag to get
  198. only one parameter can be selected which is shown as below:
  199. \arg FWDGT_FLAG_PUD: a write operation to FWDGT_PSC register is on going
  200. \arg FWDGT_FLAG_RUD: a write operation to FWDGT_RLD register is on going
  201. \arg FWDGT_FLAG_WUD: a write operation to FWDGT_WND register is on going
  202. \param[out] none
  203. \retval FlagStatus: SET or RESET
  204. */
  205. FlagStatus fwdgt_flag_get(uint16_t flag)
  206. {
  207. if(RESET != (FWDGT_STAT & flag)){
  208. return SET;
  209. }
  210. return RESET;
  211. }