1 /******************** (C) COPYRIGHT 2010 STMicroelectronics ********************
2 * File Name : usb_pwr.c
3 * Author : MCD Application Team
6 * Description : Connection/disconnection & power management
7 ********************************************************************************
8 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
9 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
10 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
11 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
12 * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
13 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
14 *******************************************************************************/
16 /* Includes ------------------------------------------------------------------*/
18 #include "stm32f30x.h"
21 #include "pios_usb_hid_pwr.h"
22 #include "pios_usb_hid.h"
24 /* Private typedef -----------------------------------------------------------*/
25 /* Private define ------------------------------------------------------------*/
26 /* Private macro -------------------------------------------------------------*/
27 /* Private variables ---------------------------------------------------------*/
28 __IO
uint32_t bDeviceState
= UNCONNECTED
; /* USB device status */
29 __IO
bool fSuspendEnabled
= true; /* true when suspend is possible */
32 __IO RESUME_STATE eState
;
33 __IO
uint8_t bESOFcnt
;
36 /* Extern variables ----------------------------------------------------------*/
37 /* Private function prototypes -----------------------------------------------*/
38 /* Extern function prototypes ------------------------------------------------*/
39 /* Private functions ---------------------------------------------------------*/
41 /*******************************************************************************
42 * Function Name : USB_Cable_Config.
43 * Description : Software Connection/Disconnection of USB Cable.
44 * Input : NewState: new state.
47 *******************************************************************************/
48 void USB_Cable_Config(FunctionalState
__attribute__((unused
)) NewState
)
51 /*******************************************************************************
52 * Function Name : PowerOn
56 * Return : USB_SUCCESS.
57 *******************************************************************************/
63 /*** cable plugged-in ? ***/
64 USB_Cable_Config(ENABLE
);
66 /*** CNTR_PWDN = 0 ***/
70 /*** CNTR_FRES = 0 ***/
72 _SetCNTR(wInterrupt_Mask
);
73 /*** Clear pending interrupts ***/
75 /*** Set interrupt mask ***/
76 wInterrupt_Mask
= CNTR_RESETM
| CNTR_SUSPM
| CNTR_WKUPM
;
77 _SetCNTR(wInterrupt_Mask
);
78 #endif /* STM32F10X_CL */
83 /*******************************************************************************
84 * Function Name : PowerOff
85 * Description : handles switch-off conditions
88 * Return : USB_SUCCESS.
89 *******************************************************************************/
93 /* disable all ints and force USB reset */
95 /* clear interrupt status register */
97 /* Disable the Pull-Up */
98 USB_Cable_Config(DISABLE
);
99 /* switch-off device */
100 _SetCNTR(CNTR_FRES
+ CNTR_PDWN
);
101 #endif /* STM32F10X_CL */
103 /* sw variables reset */
109 /*******************************************************************************
110 * Function Name : Enter_LowPowerMode.
111 * Description : Power-off system clocks and power while entering suspend mode.
115 *******************************************************************************/
116 void Enter_LowPowerMode(void)
118 /* Set the device state to suspend */
119 bDeviceState
= SUSPENDED
;
122 /*******************************************************************************
123 * Function Name : Suspend
124 * Description : sets suspend mode operating conditions
127 * Return : USB_SUCCESS.
128 *******************************************************************************/
133 /* suspend preparation */
136 /* macrocell enters suspend mode */
140 #endif /* STM32F10X_CL */
142 /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
143 /* power reduction */
144 /* ... on connected devices */
147 /* force low-power mode in the macrocell */
149 wCNTR
|= CNTR_LPMODE
;
151 #endif /* STM32F10X_CL */
153 /* switch-off the clocks */
155 Enter_LowPowerMode();
158 /*******************************************************************************
159 * Function Name : Leave_LowPowerMode.
160 * Description : Restores system clocks and power while exiting suspend mode.
164 *******************************************************************************/
165 void Leave_LowPowerMode(void)
167 DEVICE_INFO
*pInfo
= &Device_Info
;
169 /* Set the device state to the correct state */
170 if (pInfo
->Current_Configuration
!= 0) {
171 /* Device configured */
172 bDeviceState
= CONFIGURED
;
174 bDeviceState
= ATTACHED
;
178 /*******************************************************************************
179 * Function Name : Resume_Init
180 * Description : Handles wake-up restoring normal operations
183 * Return : USB_SUCCESS.
184 *******************************************************************************/
185 void Resume_Init(void)
189 #endif /* STM32F10X_CL */
191 /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
192 /* restart the clocks */
196 /* CNTR_LPMODE = 0 */
198 wCNTR
&= (~CNTR_LPMODE
);
200 #endif /* STM32F10X_CL */
202 /* restore full power */
203 /* ... on connected devices */
204 Leave_LowPowerMode();
207 /* reset FSUSP bit */
209 #endif /* STM32F10X_CL */
211 /* reverse suspend preparation */
215 /*******************************************************************************
216 * Function Name : Resume
217 * Description : This is the state machine handling resume operations and
218 * timing sequence. The control is based on the Resume structure
219 * variables and on the ESOF interrupt calling this subroutine
220 * without changing machine state.
221 * Input : a state machine value (RESUME_STATE)
222 * RESUME_ESOF doesn't change ResumeS.eState allowing
223 * decrementing of the ESOF counter in different states.
226 *******************************************************************************/
227 void Resume(RESUME_STATE eResumeSetVal
)
231 #endif /* STM32F10X_CL */
233 if (eResumeSetVal
!= RESUME_ESOF
) {
234 ResumeS
.eState
= eResumeSetVal
;
237 switch (ResumeS
.eState
) {
238 case RESUME_EXTERNAL
:
240 ResumeS
.eState
= RESUME_OFF
;
242 case RESUME_INTERNAL
:
244 ResumeS
.eState
= RESUME_START
;
247 ResumeS
.bESOFcnt
= 2;
248 ResumeS
.eState
= RESUME_WAIT
;
252 if (ResumeS
.bESOFcnt
== 0) {
253 ResumeS
.eState
= RESUME_START
;
258 OTGD_FS_SetRemoteWakeup();
261 wCNTR
|= CNTR_RESUME
;
263 #endif /* STM32F10X_CL */
264 ResumeS
.eState
= RESUME_ON
;
265 ResumeS
.bESOFcnt
= 10;
270 if (ResumeS
.bESOFcnt
== 0) {
271 #endif /* STM32F10X_CL */
273 OTGD_FS_ResetRemoteWakeup();
276 wCNTR
&= (~CNTR_RESUME
);
278 #endif /* STM32F10X_CL */
279 ResumeS
.eState
= RESUME_OFF
;
282 #endif /* STM32F10X_CL */
287 ResumeS
.eState
= RESUME_OFF
;
292 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/