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 ------------------------------------------------------------------*/
17 #include "stm32f10x.h"
20 #include "pios_usb_hid_pwr.h"
21 #include "pios_usb_hid.h"
23 /* Private typedef -----------------------------------------------------------*/
24 /* Private define ------------------------------------------------------------*/
25 /* Private macro -------------------------------------------------------------*/
26 /* Private variables ---------------------------------------------------------*/
27 __IO
uint32_t bDeviceState
= UNCONNECTED
; /* USB device status */
28 __IO
bool fSuspendEnabled
= TRUE
; /* true when suspend is possible */
31 __IO RESUME_STATE eState
;
32 __IO
uint8_t bESOFcnt
;
35 /* Extern variables ----------------------------------------------------------*/
36 /* Private function prototypes -----------------------------------------------*/
37 /* Extern function prototypes ------------------------------------------------*/
38 /* Private functions ---------------------------------------------------------*/
40 /*******************************************************************************
41 * Function Name : USB_Cable_Config.
42 * Description : Software Connection/Disconnection of USB Cable.
43 * Input : NewState: new state.
46 *******************************************************************************/
47 void USB_Cable_Config(__attribute__((unused
)) FunctionalState NewState
)
50 /*******************************************************************************
51 * Function Name : PowerOn
55 * Return : USB_SUCCESS.
56 *******************************************************************************/
62 /*** cable plugged-in ? ***/
63 USB_Cable_Config(ENABLE
);
65 /*** CNTR_PWDN = 0 ***/
69 /*** CNTR_FRES = 0 ***/
71 _SetCNTR(wInterrupt_Mask
);
72 /*** Clear pending interrupts ***/
74 /*** Set interrupt mask ***/
75 wInterrupt_Mask
= CNTR_RESETM
| CNTR_SUSPM
| CNTR_WKUPM
;
76 _SetCNTR(wInterrupt_Mask
);
77 #endif /* STM32F10X_CL */
82 /*******************************************************************************
83 * Function Name : PowerOff
84 * Description : handles switch-off conditions
87 * Return : USB_SUCCESS.
88 *******************************************************************************/
92 /* disable all ints and force USB reset */
94 /* clear interrupt status register */
96 /* Disable the Pull-Up */
97 USB_Cable_Config(DISABLE
);
98 /* switch-off device */
99 _SetCNTR(CNTR_FRES
+ CNTR_PDWN
);
100 #endif /* STM32F10X_CL */
102 /* sw variables reset */
108 /*******************************************************************************
109 * Function Name : Enter_LowPowerMode.
110 * Description : Power-off system clocks and power while entering suspend mode.
114 *******************************************************************************/
115 void Enter_LowPowerMode(void)
117 /* Set the device state to suspend */
118 bDeviceState
= SUSPENDED
;
121 /*******************************************************************************
122 * Function Name : Suspend
123 * Description : sets suspend mode operating conditions
126 * Return : USB_SUCCESS.
127 *******************************************************************************/
132 /* suspend preparation */
135 /* macrocell enters suspend mode */
139 #endif /* STM32F10X_CL */
141 /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
142 /* power reduction */
143 /* ... on connected devices */
146 /* force low-power mode in the macrocell */
148 wCNTR
|= CNTR_LPMODE
;
150 #endif /* STM32F10X_CL */
152 /* switch-off the clocks */
154 Enter_LowPowerMode();
157 /*******************************************************************************
158 * Function Name : Leave_LowPowerMode.
159 * Description : Restores system clocks and power while exiting suspend mode.
163 *******************************************************************************/
164 void Leave_LowPowerMode(void)
166 DEVICE_INFO
*pInfo
= &Device_Info
;
168 /* Set the device state to the correct state */
169 if (pInfo
->Current_Configuration
!= 0) {
170 /* Device configured */
171 bDeviceState
= CONFIGURED
;
173 bDeviceState
= ATTACHED
;
177 /*******************************************************************************
178 * Function Name : Resume_Init
179 * Description : Handles wake-up restoring normal operations
182 * Return : USB_SUCCESS.
183 *******************************************************************************/
184 void Resume_Init(void)
188 #endif /* STM32F10X_CL */
190 /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
191 /* restart the clocks */
195 /* CNTR_LPMODE = 0 */
197 wCNTR
&= (~CNTR_LPMODE
);
199 #endif /* STM32F10X_CL */
201 /* restore full power */
202 /* ... on connected devices */
203 Leave_LowPowerMode();
206 /* reset FSUSP bit */
208 #endif /* STM32F10X_CL */
210 /* reverse suspend preparation */
214 /*******************************************************************************
215 * Function Name : Resume
216 * Description : This is the state machine handling resume operations and
217 * timing sequence. The control is based on the Resume structure
218 * variables and on the ESOF interrupt calling this subroutine
219 * without changing machine state.
220 * Input : a state machine value (RESUME_STATE)
221 * RESUME_ESOF doesn't change ResumeS.eState allowing
222 * decrementing of the ESOF counter in different states.
225 *******************************************************************************/
226 void Resume(RESUME_STATE eResumeSetVal
)
230 #endif /* STM32F10X_CL */
232 if (eResumeSetVal
!= RESUME_ESOF
) {
233 ResumeS
.eState
= eResumeSetVal
;
236 switch (ResumeS
.eState
) {
237 case RESUME_EXTERNAL
:
239 ResumeS
.eState
= RESUME_OFF
;
241 case RESUME_INTERNAL
:
243 ResumeS
.eState
= RESUME_START
;
246 ResumeS
.bESOFcnt
= 2;
247 ResumeS
.eState
= RESUME_WAIT
;
251 if (ResumeS
.bESOFcnt
== 0) {
252 ResumeS
.eState
= RESUME_START
;
257 OTGD_FS_SetRemoteWakeup();
260 wCNTR
|= CNTR_RESUME
;
262 #endif /* STM32F10X_CL */
263 ResumeS
.eState
= RESUME_ON
;
264 ResumeS
.bESOFcnt
= 10;
269 if (ResumeS
.bESOFcnt
== 0) {
270 #endif /* STM32F10X_CL */
272 OTGD_FS_ResetRemoteWakeup();
275 wCNTR
&= (~CNTR_RESUME
);
277 #endif /* STM32F10X_CL */
278 ResumeS
.eState
= RESUME_OFF
;
281 #endif /* STM32F10X_CL */
286 ResumeS
.eState
= RESUME_OFF
;
291 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/