Blame view

Drivers/GD32F30x_standard_peripheral/Source/gd32f30x_fwdgt.c 6.99 KB
95ce2328   李外   完成USB移植,测试正常,
1
2
3
4
5
6
7
8
9
10
11
12
13
  /*!
      \file    gd32f30x_fwdgt.c
      \brief   FWDGT driver
  
      \version 2017-02-10, V1.0.0, firmware for GD32F30x
      \version 2018-10-10, V1.1.0, firmware for GD32F30x
      \version 2018-12-25, V2.0.0, firmware for GD32F30x
      \version 2020-09-30, V2.1.0, firmware for GD32F30x
  */
  
  /*
      Copyright (c) 2020, GigaDevice Semiconductor Inc.
  
3a54a732   李外   V100.0.16 GD32F3...
14
      Redistribution and use in source and binary forms, with or without modification,
95ce2328   李外   完成USB移植,测试正常,
15
16
  are permitted provided that the following conditions are met:
  
3a54a732   李外   V100.0.16 GD32F3...
17
      1. Redistributions of source code must retain the above copyright notice, this
95ce2328   李外   完成USB移植,测试正常,
18
         list of conditions and the following disclaimer.
3a54a732   李外   V100.0.16 GD32F3...
19
20
      2. Redistributions in binary form must reproduce the above copyright notice,
         this list of conditions and the following disclaimer in the documentation
95ce2328   李外   完成USB移植,测试正常,
21
         and/or other materials provided with the distribution.
3a54a732   李外   V100.0.16 GD32F3...
22
23
      3. Neither the name of the copyright holder nor the names of its contributors
         may be used to endorse or promote products derived from this software without
95ce2328   李外   完成USB移植,测试正常,
24
25
         specific prior written permission.
  
3a54a732   李外   V100.0.16 GD32F3...
26
27
28
29
30
31
32
33
34
      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
95ce2328   李外   完成USB移植,测试正常,
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  OF SUCH DAMAGE.
  */
  
  #include "gd32f30x_fwdgt.h"
  
  /* write value to FWDGT_CTL_CMD bit field */
  #define CTL_CMD(regval)             (BITS(0,15) & ((uint32_t)(regval) << 0))
  /* write value to FWDGT_RLD_RLD bit field */
  #define RLD_RLD(regval)             (BITS(0,11) & ((uint32_t)(regval) << 0))
  
  /*!
      \brief      enable write access to FWDGT_PSC and FWDGT_RLD
      \param[in]  none
      \param[out] none
      \retval     none
  */
  void fwdgt_write_enable(void)
  {
      FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  }
  
  /*!
      \brief      disable write access to FWDGT_PSC and FWDGT_RLD
      \param[in]  none
      \param[out] none
      \retval     none
  */
  void fwdgt_write_disable(void)
  {
      FWDGT_CTL = FWDGT_WRITEACCESS_DISABLE;
  }
  
  /*!
      \brief      start the free watchdog timer counter
      \param[in]  none
      \param[out] none
      \retval     none
  */
  void fwdgt_enable(void)
  {
      FWDGT_CTL = FWDGT_KEY_ENABLE;
  }
  
  /*!
3a54a732   李外   V100.0.16 GD32F3...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
      \brief      configure the free watchdog timer counter prescaler value
      \param[in]  prescaler_value: specify prescaler value
                  only one parameter can be selected which is shown as below:
        \arg        FWDGT_PSC_DIV4: FWDGT prescaler set to 4
        \arg        FWDGT_PSC_DIV8: FWDGT prescaler set to 8
        \arg        FWDGT_PSC_DIV16: FWDGT prescaler set to 16
        \arg        FWDGT_PSC_DIV32: FWDGT prescaler set to 32
        \arg        FWDGT_PSC_DIV64: FWDGT prescaler set to 64
        \arg        FWDGT_PSC_DIV128: FWDGT prescaler set to 128
        \arg        FWDGT_PSC_DIV256: FWDGT prescaler set to 256
      \param[out] none
      \retval     ErrStatus: ERROR or SUCCESS
  */
  ErrStatus fwdgt_prescaler_value_config(uint16_t prescaler_value)
  {
      uint32_t timeout = FWDGT_PSC_TIMEOUT;
      uint32_t flag_status = RESET;
  
      /* enable write access to FWDGT_PSC */
      FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  
      /* wait until the PUD flag to be reset */
      do {
          flag_status = FWDGT_STAT & FWDGT_STAT_PUD;
      } while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
  
      if((uint32_t)RESET != flag_status) {
          return ERROR;
      }
  
      /* configure FWDGT */
      FWDGT_PSC = (uint32_t)prescaler_value;
  
      return SUCCESS;
  }
  
  /*!
      \brief      configure the free watchdog timer counter reload value
      \param[in]  reload_value: specify reload value(0x0000 - 0x0FFF)
      \param[out] none
      \retval     ErrStatus: ERROR or SUCCESS
  */
  ErrStatus fwdgt_reload_value_config(uint16_t reload_value)
  {
      uint32_t timeout = FWDGT_RLD_TIMEOUT;
      uint32_t flag_status = RESET;
  
      /* enable write access to FWDGT_RLD */
      FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
  
      /* wait until the RUD flag to be reset */
      do {
          flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
      } while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
  
      if((uint32_t)RESET != flag_status) {
          return ERROR;
      }
  
      FWDGT_RLD = RLD_RLD(reload_value);
  
      return SUCCESS;
  }
  
  /*!
95ce2328   李外   完成USB移植,测试正常,
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
      \brief      reload the counter of FWDGT
      \param[in]  none
      \param[out] none
      \retval     none
  */
  void fwdgt_counter_reload(void)
  {
      FWDGT_CTL = FWDGT_KEY_RELOAD;
  }
  
  /*!
      \brief      configure counter reload value, and prescaler divider value
      \param[in]  reload_value: specify reload value(0x0000 - 0x0FFF)
      \param[in]  prescaler_div: FWDGT prescaler value
                  only one parameter can be selected which is shown as below:
        \arg        FWDGT_PSC_DIV4: FWDGT prescaler set to 4
        \arg        FWDGT_PSC_DIV8: FWDGT prescaler set to 8
        \arg        FWDGT_PSC_DIV16: FWDGT prescaler set to 16
        \arg        FWDGT_PSC_DIV32: FWDGT prescaler set to 32
        \arg        FWDGT_PSC_DIV64: FWDGT prescaler set to 64
        \arg        FWDGT_PSC_DIV128: FWDGT prescaler set to 128
        \arg        FWDGT_PSC_DIV256: FWDGT prescaler set to 256
      \param[out] none
      \retval     ErrStatus: ERROR or SUCCESS
  */
  ErrStatus fwdgt_config(uint16_t reload_value, uint8_t prescaler_div)
  {
      uint32_t timeout = FWDGT_PSC_TIMEOUT;
      uint32_t flag_status = RESET;
3a54a732   李外   V100.0.16 GD32F3...
173
  
95ce2328   李外   完成USB移植,测试正常,
174
175
      /* enable write access to FWDGT_PSC,and FWDGT_RLD */
      FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
3a54a732   李外   V100.0.16 GD32F3...
176
  
95ce2328   李外   完成USB移植,测试正常,
177
      /* wait until the PUD flag to be reset */
3a54a732   李外   V100.0.16 GD32F3...
178
179
180
181
182
      do {
          flag_status = FWDGT_STAT & FWDGT_STAT_PUD;
      } while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
  
      if((uint32_t)RESET != flag_status) {
95ce2328   李外   完成USB移植,测试正常,
183
184
185
186
187
188
189
190
          return ERROR;
      }
  
      /* configure FWDGT */
      FWDGT_PSC = (uint32_t)prescaler_div;
  
      timeout = FWDGT_RLD_TIMEOUT;
      /* wait until the RUD flag to be reset */
3a54a732   李外   V100.0.16 GD32F3...
191
192
193
194
195
      do {
          flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
      } while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
  
      if((uint32_t)RESET != flag_status) {
95ce2328   李外   完成USB移植,测试正常,
196
197
          return ERROR;
      }
3a54a732   李外   V100.0.16 GD32F3...
198
  
95ce2328   李外   完成USB移植,测试正常,
199
      FWDGT_RLD = RLD_RLD(reload_value);
3a54a732   李外   V100.0.16 GD32F3...
200
  
95ce2328   李外   完成USB移植,测试正常,
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
      /* reload the counter */
      FWDGT_CTL = FWDGT_KEY_RELOAD;
  
      return SUCCESS;
  }
  
  /*!
      \brief      get flag state of FWDGT
      \param[in]  flag: flag to get
                  only one parameter can be selected which is shown as below:
        \arg        FWDGT_FLAG_PUD: a write operation to FWDGT_PSC register is on going
        \arg        FWDGT_FLAG_RUD: a write operation to FWDGT_RLD register is on going
      \param[out] none
      \retval     FlagStatus: SET or RESET
  */
  FlagStatus fwdgt_flag_get(uint16_t flag)
  {
3a54a732   李外   V100.0.16 GD32F3...
218
      if(FWDGT_STAT & flag) {
95ce2328   李外   完成USB移植,测试正常,
219
          return SET;
3a54a732   李外   V100.0.16 GD32F3...
220
      }
95ce2328   李外   完成USB移植,测试正常,
221
222
223
  
      return RESET;
  }