GD32F1x0: USB/GD32_USB_Device_Library/Core/src/usbd_pwr.c Source File

GD32F1x0

usbd_pwr.c
Go to the documentation of this file.
1 
11 /* Includes ------------------------------------------------------------------*/
12 #include "usbd_pwr.h"
13 
14 /* Private function prototypes ----------------------------------------------- */
15 static void ResumeMCU (void);
16 
17 #ifdef USB_DEVICE_LOW_PWR_MODE_SUPPORT
18 static void LeaveLowPowerMode (void);
19 #endif
20 
36 __IO RESUME_STATE State = RESUME_OFF;
37 __IO uint8_t ESOFcount = 0;
38 __IO uint8_t SuspendEnabled = 1;
39 __IO uint8_t RemoteWakeupOn = 0;
40 
54 static void ResumeMCU (void)
55 {
56  /* Clear low_power mode bit in CTLR */
57  _SetCTLR(_GetCTLR() & (~CTLR_LOWM));
58 
59 #ifdef USB_DEVICE_LOW_PWR_MODE_SUPPORT
60 
61  /* Restore normal operations */
62  LeaveLowPowerMode();
63 
64 #endif
65 
66  /* Clear SETSPS bit */
67  _SetCTLR(_GetCTLR() & (~CTLR_SETSPS));
68 }
69 
70 #ifdef USB_DEVICE_LOW_PWR_MODE_SUPPORT
71 
77 static void LeaveLowPowerMode (void)
78 {
79  /* Restore system clock */
80 
81  /* Enable HSE */
82  RCC_HSEConfig(RCC_HSE_ON);
83 
84  /* Wait till HSE is ready */
85  while(RCC_GetBitState(RCC_FLAG_HSESTB) == RESET);
86 
87  /* Enable PLL */
88  RCC_PLL_Enable(ENABLE);
89 
90  /* Wait till PLL is ready */
91  while(RCC_GetBitState(RCC_FLAG_PLLSTB) == RESET);
92 
93  /* Select PLL as system clock source */
94  RCC_CK_SYSConfig(RCC_SYSCLKSOURCE_PLLCLK);
95 
96  /* Wait till PLL is used as system clock source */
97  while(RCC_GetCK_SYSSource() != 0x08);
98 
99  /* Low power sleep on exit disabled */
100  NVIC_SystemLowPowerConfig(NVIC_LOWPOWER_SLEEPONEXIT, DISABLE);
101 }
102 
103 #endif
104 
110 void USBD_Suspend (void)
111 {
112  /* Set suspend and low-power mode in the macrocell */
113  _SetCTLR(_GetCTLR() | CTLR_SETSPS | CTLR_LOWM);
114 
115 #ifdef USB_DEVICE_LOW_PWR_MODE_SUPPORT
116 
117  /* When wakeup flag in not set, enter system to DEEP_SLEEP mode */
118  if((_GetIFR() & IFR_WKUPIF) == 0)
119  {
120  /* Enter DEEP_SLEEP mode with LDO in low power mode */
121  PWR_DEEPSLEEPMode_Entry(PWR_LDO_LOWPOWER, PWR_DEEPSLEEPENTRY_WFI);
122  }
123  else
124  {
125  /* Clear wakeup flag */
126  _SetIFR(CLR_WKUPIF);
127 
128  /* Clear SETSPS to abort entry in suspend mode */
129  _SetCTLR(_GetCTLR() & (~CTLR_SETSPS));
130  }
131 
132 #endif
133 }
134 
148 void USBD_Resume (RESUME_STATE ResumeValue)
149 {
150  if (ResumeValue != RESUME_ESOF) State = ResumeValue;
151 
152  switch (State)
153  {
154  case RESUME_EXTERNAL:
155  if (RemoteWakeupOn == 0)
156  {
157  ResumeMCU();
158  State = RESUME_OFF;
159  }
160  else
161  {
162  /* If resume is detected during the RemoteWakeup signalling, need keep RemoteWakeup handling */
163  State = RESUME_ON;
164  }
165  break;
166 
167  case RESUME_INTERNAL:
168  ResumeMCU();
169  RemoteWakeupOn = 1;
170 
171  case RESUME_LATER:
172  ESOFcount = 15;
173  _SetCTLR(_GetCTLR() | CTLR_RSREQ);
174  State = RESUME_ON;
175  break;
176 
177  case RESUME_ON:
178  ESOFcount--;
179  if (ESOFcount == 0)
180  {
181  _SetCTLR(_GetCTLR() & (~CTLR_RSREQ));
182  State = RESUME_OFF;
183  RemoteWakeupOn = 0;
184  }
185  break;
186 
187  case RESUME_OFF:
188  case RESUME_ESOF:
189  default:
190  break;
191  }
192 }
193 
210 /************************ (C) COPYRIGHT 2014 GIGADEVICE *****END OF FILE****/
#define CTLR_RSREQ
Definition: usb_regs.h:160
#define CTLR_SETSPS
Definition: usb_regs.h:161
void USBD_Suspend(void)
Set usb device to suspend mode.
Definition: usbd_pwr.c:110
Power management header file.
#define IFR_WKUPIF
Definition: usb_regs.h:172
void USBD_Resume(RESUME_STATE ResumeValue)
Resume state machine handling.
Definition: usbd_pwr.c:148
#define CTLR_LOWM
Definition: usb_regs.h:162
#define CLR_WKUPIF
Definition: usb_regs.h:183
Generated by   doxygen 1.8.10