Blame view

Drivers/GD32F30x_standard_peripheral/Source/gd32f30x_pmu.c 11 KB
95ce2328   李外   完成USB移植,测试正常,
1
2
3
4
5
6
7
8
9
10
11
12
13
  /*!
      \file    gd32f30x_pmu.c
      \brief   PMU 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
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
  OF SUCH DAMAGE.
  */
  
  
  #include "gd32f30x_pmu.h"
  
  /*!
      \brief      reset PMU register
      \param[in]  none
      \param[out] none
      \retval     none
  */
  void pmu_deinit(void)
  {
      /* reset PMU */
      rcu_periph_reset_enable(RCU_PMURST);
      rcu_periph_reset_disable(RCU_PMURST);
  }
  
  /*!
      \brief      select low voltage detector threshold
      \param[in]  lvdt_n:
        \arg        PMU_LVDT_0: voltage threshold is 2.1V
        \arg        PMU_LVDT_1: voltage threshold is 2.3V
        \arg        PMU_LVDT_2: voltage threshold is 2.4V
        \arg        PMU_LVDT_3: voltage threshold is 2.6V
        \arg        PMU_LVDT_4: voltage threshold is 2.7V
        \arg        PMU_LVDT_5: voltage threshold is 2.9V
        \arg        PMU_LVDT_6: voltage threshold is 3.0V
        \arg        PMU_LVDT_7: voltage threshold is 3.1V
      \param[out] none
      \retval     none
  */
  void pmu_lvd_select(uint32_t lvdt_n)
  {
      /* disable LVD */
      PMU_CTL &= ~PMU_CTL_LVDEN;
      /* clear LVDT bits */
      PMU_CTL &= ~PMU_CTL_LVDT;
      /* set LVDT bits according to lvdt_n */
      PMU_CTL |= lvdt_n;
      /* enable LVD */
      PMU_CTL |= PMU_CTL_LVDEN;
  }
  
  /*!
      \brief      select LDO output voltage
                  this bit set by software when the main PLL closed, before closing PLL, change the system clock to IRC16M or HXTAL
      \param[in]  ldo_output:
        \arg        PMU_LDOVS_LOW: LDO output voltage low mode
        \arg        PMU_LDOVS_MID: LDO output voltage mid mode
        \arg        PMU_LDOVS_HIGH: LDO output voltage high mode
      \param[out] none
      \retval     none
  */
  void pmu_ldo_output_select(uint32_t ldo_output)
  {
      PMU_CTL &= ~PMU_CTL_LDOVS;
      PMU_CTL |= ldo_output;
  }
  
  /*!
      \brief      disable PMU lvd
      \param[in]  none
      \param[out] none
      \retval     none
  */
  void pmu_lvd_disable(void)
  {
      /* disable LVD */
      PMU_CTL &= ~PMU_CTL_LVDEN;
  }
  
  /*!
      \brief      switch high-driver mode
                  this bit set by software only when IRC16M or HXTAL used as system clock
      \param[in]  highdr_switch:
        \arg        PMU_HIGHDR_SWITCH_NONE: disable high-driver mode switch
        \arg        PMU_HIGHDR_SWITCH_EN: enable high-driver mode switch
      \param[out] none
      \retval     none
  */
  void pmu_highdriver_switch_select(uint32_t highdr_switch)
  {
      /* wait for HDRF flag set */
3a54a732   李外   V100.0.16 GD32F3...
120
      while(SET != pmu_flag_get(PMU_FLAG_HDRF)) {
95ce2328   李外   完成USB移植,测试正常,
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
      }
      PMU_CTL &= ~PMU_CTL_HDS;
      PMU_CTL |= highdr_switch;
  }
  
  /*!
      \brief      enable high-driver mode
                  this bit set by software only when IRC16M or HXTAL used as system clock
      \param[in]  none
      \param[out] none
      \retval     none
  */
  void pmu_highdriver_mode_enable(void)
  {
      PMU_CTL |= PMU_CTL_HDEN;
  }
  
  /*!
      \brief      disable high-driver mode
      \param[in]  none
      \param[out] none
      \retval     none
  */
  void pmu_highdriver_mode_disable(void)
  {
      PMU_CTL &= ~PMU_CTL_HDEN;
  }
  
  /*!
      \brief      enable low-driver mode in deep-sleep mode
      \param[in]  none
      \param[out] none
      \retval     none
  */
  void pmu_lowdriver_mode_enable(void)
  {
      PMU_CTL |= PMU_CTL_LDEN;
  }
  
  /*!
      \brief      disable low-driver mode in deep-sleep mode
      \param[in]  none
      \param[out] none
      \retval     none
  */
  void pmu_lowdriver_mode_disable(void)
  {
      PMU_CTL &= ~PMU_CTL_LDEN;
  }
  
  /*!
      \brief      driver mode when use low power LDO
      \param[in]  mode:
        \arg        PMU_NORMALDR_LOWPWR:  normal driver when use low power LDO
        \arg        PMU_LOWDR_LOWPWR:  low-driver mode enabled when LDEN is 11 and use low power LDO
      \param[out] none
      \retval     none
  */
  void pmu_lowpower_driver_config(uint32_t mode)
  {
      PMU_CTL &= ~PMU_CTL_LDLP;
      PMU_CTL |= mode;
  }
  
  /*!
      \brief      driver mode when use normal power LDO
      \param[in]  mode:
        \arg        PMU_NORMALDR_NORMALPWR:  normal driver when use normal power LDO
        \arg        PMU_LOWDR_NORMALPWR:  low-driver mode enabled when LDEN is 11 and use normal power LDO
      \param[out] none
      \retval     none
  */
  void pmu_normalpower_driver_config(uint32_t mode)
  {
      PMU_CTL &= ~PMU_CTL_LDNP;
      PMU_CTL |= mode;
  }
  
  /*!
      \brief      PMU work in sleep mode
      \param[in]  sleepmodecmd:
        \arg        WFI_CMD: use WFI command
        \arg        WFE_CMD: use WFE command
      \param[out] none
      \retval     none
  */
  void pmu_to_sleepmode(uint8_t sleepmodecmd)
  {
      /* clear sleepdeep bit of Cortex-M4 system control register */
      SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
3a54a732   李外   V100.0.16 GD32F3...
211
  
95ce2328   李外   完成USB移植,测试正常,
212
      /* select WFI or WFE command to enter sleep mode */
3a54a732   李外   V100.0.16 GD32F3...
213
      if(WFI_CMD == sleepmodecmd) {
95ce2328   李外   完成USB移植,测试正常,
214
          __WFI();
3a54a732   李外   V100.0.16 GD32F3...
215
      } else {
95ce2328   李外   完成USB移植,测试正常,
216
217
218
219
220
221
222
223
          __WFE();
      }
  }
  
  /*!
      \brief      PMU work in deepsleep mode
      \param[in]  ldo:
                  only one parameter can be selected which is shown as below:
3a54a732   李外   V100.0.16 GD32F3...
224
        \arg        PMU_LDO_NORMAL: LDO work in normal power mode when pmu enter deepsleep mode
95ce2328   李外   完成USB移植,测试正常,
225
226
227
228
229
230
231
232
233
234
235
236
237
238
        \arg        PMU_LDO_LOWPOWER: LDO work in low power mode when pmu enter deepsleep mode
      \param[in]  lowdrive:
                  only one parameter can be selected which is shown as below:
        \arg        PMU_LOWDRIVER_ENABLE: low-driver mode enable in deep-sleep mode
        \arg        PMU_LOWDRIVER_DISABLE: low-driver mode disable in deep-sleep mode
      \param[in]  deepsleepmodecmd:
                  only one parameter can be selected which is shown as below:
        \arg        WFI_CMD: use WFI command
        \arg        WFE_CMD: use WFE command
      \param[out] none
      \retval     none
  */
  void pmu_to_deepsleepmode(uint32_t ldo, uint32_t lowdrive, uint8_t deepsleepmodecmd)
  {
3a54a732   李外   V100.0.16 GD32F3...
239
      static uint32_t reg_snap[ 4 ];
95ce2328   李外   完成USB移植,测试正常,
240
241
242
243
244
245
246
      /* clear stbmod and ldolp bits */
      PMU_CTL &= ~((uint32_t)(PMU_CTL_STBMOD | PMU_CTL_LDOLP | PMU_CTL_LDEN | PMU_CTL_LDNP | PMU_CTL_LDLP));
  
      /* set ldolp bit according to pmu_ldo */
      PMU_CTL |= ldo;
  
      /* low drive mode config in deep-sleep mode */
3a54a732   李外   V100.0.16 GD32F3...
247
248
      if(PMU_LOWDRIVER_ENABLE == lowdrive) {
          if(PMU_LDO_NORMAL == ldo) {
95ce2328   李外   完成USB移植,测试正常,
249
              PMU_CTL |= (uint32_t)(PMU_CTL_LDEN | PMU_CTL_LDNP);
3a54a732   李外   V100.0.16 GD32F3...
250
          } else {
95ce2328   李外   完成USB移植,测试正常,
251
252
253
254
255
256
257
              PMU_CTL |= (uint32_t)(PMU_CTL_LDEN | PMU_CTL_LDLP);
          }
      }
  
      /* set sleepdeep bit of Cortex-M4 system control register */
      SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  
3a54a732   李外   V100.0.16 GD32F3...
258
259
260
261
      reg_snap[ 0 ] = REG32(0xE000E010U);
      reg_snap[ 1 ] = REG32(0xE000E100U);
      reg_snap[ 2 ] = REG32(0xE000E104U);
      reg_snap[ 3 ] = REG32(0xE000E108U);
95ce2328   李外   完成USB移植,测试正常,
262
  
3a54a732   李外   V100.0.16 GD32F3...
263
264
265
266
      REG32(0xE000E010U) &= 0x00010004U;
      REG32(0xE000E180U)  = 0XFF7FF83DU;
      REG32(0xE000E184U)  = 0XFFFFF8FFU;
      REG32(0xE000E188U)  = 0xFFFFFFFFU;
95ce2328   李外   完成USB移植,测试正常,
267
268
  
      /* select WFI or WFE command to enter deepsleep mode */
3a54a732   李外   V100.0.16 GD32F3...
269
      if(WFI_CMD == deepsleepmodecmd) {
95ce2328   李外   完成USB移植,测试正常,
270
          __WFI();
3a54a732   李外   V100.0.16 GD32F3...
271
      } else {
95ce2328   李外   完成USB移植,测试正常,
272
273
274
275
276
          __SEV();
          __WFE();
          __WFE();
      }
  
3a54a732   李外   V100.0.16 GD32F3...
277
278
279
280
281
      REG32(0xE000E010U) = reg_snap[ 0 ] ;
      REG32(0xE000E100U) = reg_snap[ 1 ] ;
      REG32(0xE000E104U) = reg_snap[ 2 ] ;
      REG32(0xE000E108U) = reg_snap[ 3 ] ;
  
95ce2328   李外   完成USB移植,测试正常,
282
283
284
285
286
287
      /* reset sleepdeep bit of Cortex-M4 system control register */
      SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  }
  
  /*!
      \brief      pmu work in standby mode
3a54a732   李外   V100.0.16 GD32F3...
288
      \param[in]  none
95ce2328   李外   完成USB移植,测试正常,
289
290
291
      \param[out] none
      \retval     none
  */
3a54a732   李外   V100.0.16 GD32F3...
292
  void pmu_to_standbymode(void)
95ce2328   李外   完成USB移植,测试正常,
293
  {
95ce2328   李外   完成USB移植,测试正常,
294
295
      /* set stbmod bit */
      PMU_CTL |= PMU_CTL_STBMOD;
3a54a732   李外   V100.0.16 GD32F3...
296
  
95ce2328   李外   完成USB移植,测试正常,
297
298
      /* reset wakeup flag */
      PMU_CTL |= PMU_CTL_WURST;
3a54a732   李外   V100.0.16 GD32F3...
299
300
301
302
303
304
305
306
307
  
      /* set sleepdeep bit of Cortex-M4 system control register */
      SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  
      REG32(0xE000E010U) &= 0x00010004U;
      REG32(0xE000E180U)  = 0XFFFFFFF7U;
      REG32(0xE000E184U)  = 0XFFFFFDFFU;
      REG32(0xE000E188U)  = 0xFFFFFFFFU;
  
95ce2328   李外   完成USB移植,测试正常,
308
      /* select WFI or WFE command to enter standby mode */
3a54a732   李外   V100.0.16 GD32F3...
309
      __WFI();
95ce2328   李外   完成USB移植,测试正常,
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
  }
  
  /*!
      \brief      enable backup domain write
      \param[in]  none
      \param[out] none
      \retval     none
  */
  void pmu_backup_write_enable(void)
  {
      PMU_CTL |= PMU_CTL_BKPWEN;
  }
  
  /*!
      \brief      disable backup domain write
      \param[in]  none
      \param[out] none
      \retval     none
  */
  void pmu_backup_write_disable(void)
  {
      PMU_CTL &= ~PMU_CTL_BKPWEN;
  }
  
  /*!
      \brief      enable wakeup pin
      \param[in]  none
      \param[out] none
      \retval     none
  */
  void pmu_wakeup_pin_enable(void)
  {
      PMU_CS |= PMU_CS_WUPEN;
  }
  
  /*!
      \brief      disable wakeup pin
      \param[in]  none
      \param[out] none
      \retval     none
  */
  void pmu_wakeup_pin_disable(void)
  {
      PMU_CS &= ~PMU_CS_WUPEN;
  }
  
  /*!
      \brief      get flag state
      \param[in]  flag:
        \arg        PMU_FLAG_WAKEUP: wakeup flag
        \arg        PMU_FLAG_STANDBY: standby flag
        \arg        PMU_FLAG_LVD: lvd flag
        \arg        PMU_FLAG_LDOVSRF: LDO voltage select ready flag
        \arg        PMU_FLAG_HDRF: high-driver ready flag
        \arg        PMU_FLAG_HDSRF: high-driver switch ready flag
3a54a732   李外   V100.0.16 GD32F3...
365
        \arg        PMU_FLAG_LDRF: low-driver mode ready flag
95ce2328   李外   完成USB移植,测试正常,
366
367
368
369
370
      \param[out] none
      \retval     FlagStatus SET or RESET
  */
  FlagStatus pmu_flag_get(uint32_t flag)
  {
3a54a732   李外   V100.0.16 GD32F3...
371
      if(PMU_CS & flag) {
95ce2328   李外   完成USB移植,测试正常,
372
          return  SET;
3a54a732   李外   V100.0.16 GD32F3...
373
      } else {
95ce2328   李外   完成USB移植,测试正常,
374
375
376
377
378
379
380
381
382
383
384
385
386
387
          return  RESET;
      }
  }
  
  /*!
      \brief      clear flag bit
      \param[in]  flag:
        \arg        PMU_FLAG_RESET_WAKEUP: reset wakeup flag
        \arg        PMU_FLAG_RESET_STANDBY: reset standby flag
      \param[out] none
      \retval     none
  */
  void pmu_flag_clear(uint32_t flag)
  {
3a54a732   李外   V100.0.16 GD32F3...
388
      switch(flag) {
95ce2328   李外   完成USB移植,测试正常,
389
390
391
392
393
394
395
396
397
398
399
400
      case PMU_FLAG_RESET_WAKEUP:
          /* reset wakeup flag */
          PMU_CTL |= PMU_CTL_WURST;
          break;
      case PMU_FLAG_RESET_STANDBY:
          /* reset standby flag */
          PMU_CTL |= PMU_CTL_STBRST;
          break;
      default :
          break;
      }
  }