gd32f3x0_fwdgt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*!
  2. \file gd32f3x0_fwdgt.c
  3. \brief FWDGT driver
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0_fwdgt.h"
  10. /*!
  11. \brief disable write access to FWDGT_PSC,FWDGT_RLD and FWDGT_WND
  12. \param[in] none
  13. \param[out] none
  14. \retval none
  15. */
  16. void fwdgt_write_disable(void)
  17. {
  18. FWDGT_CTL = FWDGT_WRITEACCESS_DISABLE;
  19. }
  20. /*!
  21. \brief reload the counter of FWDGT
  22. \param[in] none
  23. \param[out] none
  24. \retval none
  25. */
  26. void fwdgt_counter_reload(void)
  27. {
  28. FWDGT_CTL = FWDGT_KEY_RELOAD;
  29. }
  30. /*!
  31. \brief start the free watchdog timer counter
  32. \param[in] none
  33. \param[out] none
  34. \retval none
  35. */
  36. void fwdgt_enable(void)
  37. {
  38. FWDGT_CTL = FWDGT_KEY_ENABLE;
  39. }
  40. /*!
  41. \brief configure the free watchdog timer counter window value
  42. \param[in] window_value: specify window value(0x0000 - 0x0FFF)
  43. \param[out] none
  44. \retval ErrStatus: ERROR or SUCCESS
  45. */
  46. ErrStatus fwdgt_window_value_config(uint16_t window_value)
  47. {
  48. uint32_t time_index = FWDGT_WND_TIMEOUT;
  49. uint32_t flag_status = RESET;
  50. /* enable write access to FWDGT_WND */
  51. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  52. /* wait until the WUD flag to be reset */
  53. do{
  54. flag_status = FWDGT_STAT & FWDGT_STAT_WUD;
  55. }while((--time_index > 0U) && ((uint32_t)RESET != flag_status));
  56. if ((uint32_t)RESET != flag_status){
  57. return ERROR;
  58. }
  59. FWDGT_WND = WND_WND(window_value);
  60. return SUCCESS;
  61. }
  62. /*!
  63. \brief configure counter reload value, and prescaler divider value
  64. \param[in] reload_value: specify reload value(0x0000 - 0x0FFF)
  65. \param[in] prescaler_div: FWDGT prescaler value
  66. \arg FWDGT_PSC_DIV4: FWDGT prescaler set to 4
  67. \arg FWDGT_PSC_DIV8: FWDGT prescaler set to 8
  68. \arg FWDGT_PSC_DIV16: FWDGT prescaler set to 16
  69. \arg FWDGT_PSC_DIV32: FWDGT prescaler set to 32
  70. \arg FWDGT_PSC_DIV64: FWDGT prescaler set to 64
  71. \arg FWDGT_PSC_DIV128: FWDGT prescaler set to 128
  72. \arg FWDGT_PSC_DIV256: FWDGT prescaler set to 256
  73. \param[out] none
  74. \retval ErrStatus: ERROR or SUCCESS
  75. */
  76. ErrStatus fwdgt_config(uint16_t reload_value, uint8_t prescaler_div)
  77. {
  78. uint32_t timeout = FWDGT_PSC_TIMEOUT;
  79. uint32_t flag_status = RESET;
  80. /* enable write access to FWDGT_PSC,and FWDGT_RLD */
  81. FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  82. /* wait until the PUD flag to be reset */
  83. do{
  84. flag_status = FWDGT_STAT & FWDGT_STAT_PUD;
  85. }while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
  86. if ((uint32_t)RESET != flag_status){
  87. return ERROR;
  88. }
  89. /* configure FWDGT */
  90. FWDGT_PSC = (uint32_t)prescaler_div;
  91. timeout = FWDGT_RLD_TIMEOUT;
  92. /* wait until the RUD flag to be reset */
  93. do{
  94. flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
  95. }while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
  96. if ((uint32_t)RESET != flag_status){
  97. return ERROR;
  98. }
  99. FWDGT_RLD = RLD_RLD(reload_value);
  100. /* reload the counter */
  101. FWDGT_CTL = FWDGT_KEY_RELOAD;
  102. return SUCCESS;
  103. }
  104. /*!
  105. \brief get flag state of FWDGT
  106. \param[in] flag: flag to get
  107. \arg FWDGT_FLAG_PUD: a write operation to FWDGT_PSC register is on going
  108. \arg FWDGT_FLAG_RUD: a write operation to FWDGT_RLD register is on going
  109. \arg FWDGT_FLAG_WUD: a write operation to FWDGT_WND register is on going
  110. \param[out] none
  111. \retval FlagStatus: SET or RESET
  112. */
  113. FlagStatus fwdgt_flag_get(uint16_t flag)
  114. {
  115. if(FWDGT_STAT & flag){
  116. return SET;
  117. }
  118. return RESET;
  119. }