GD32F10x USB-Device: E:/USB Libraries/GD32_USB_Device_Library/Core/src/usbd_pwr.c Source File

GD32F103 Firmware

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 
142 void USBD_Resume (RESUME_STATE ResumeValue)
143 {
144  if (ResumeValue != RESUME_ESOF) State = ResumeValue;
145 
146  switch (State)
147  {
148  case RESUME_EXTERNAL:
149  if (RemoteWakeupOn == 0)
150  {
151  ResumeMCU();
152  State = RESUME_OFF;
153  }
154  else
155  {
156  /* If resume is detected during the RemoteWakeup signalling, need keep RemoteWakeup handling */
157  State = RESUME_ON;
158  }
159  break;
160 
161  case RESUME_INTERNAL:
162  ResumeMCU();
163  RemoteWakeupOn = 1;
164 
165  case RESUME_LATER:
166  ESOFcount = 15;
167  _SetCTLR(_GetCTLR() | CTLR_RSREQ);
168  State = RESUME_ON;
169  break;
170 
171  case RESUME_ON:
172  ESOFcount--;
173  if (ESOFcount == 0)
174  {
175  _SetCTLR(_GetCTLR() & (~CTLR_RSREQ));
176  State = RESUME_OFF;
177  RemoteWakeupOn = 0;
178  }
179  break;
180 
181  case RESUME_OFF:
182  case RESUME_ESOF:
183  default:
184  break;
185  }
186 }
187 
204 /************************ (C) COPYRIGHT 2014 GIGADEVICE *****END OF FILE****/
#define CTLR_RSREQ
Definition: usb_regs.h:149
#define CTLR_SETSPS
Definition: usb_regs.h:150
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:161
void USBD_Resume(RESUME_STATE ResumeValue)
Resume state machine handling.
Definition: usbd_pwr.c:142
#define CTLR_LOWM
Definition: usb_regs.h:151
#define CLR_WKUPIF
Definition: usb_regs.h:172
Generated on Fri Feb 6 2015 14:56:36 for GD32F10x USB-Device by   doxygen 1.8.8