update credits
[librepilot.git] / flight / pios / stm32f30x / pios_usb_hid_pwr.c
blob7a028a91de21cdbb551af7267f582af63deda87b
1 /******************** (C) COPYRIGHT 2010 STMicroelectronics ********************
2 * File Name : usb_pwr.c
3 * Author : MCD Application Team
4 * Version : V3.2.1
5 * Date : 07/05/2010
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 "pios.h"
18 #include "stm32f30x.h"
19 #include "usb_lib.h"
20 #include "usb_conf.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 */
31 struct {
32 __IO RESUME_STATE eState;
33 __IO uint8_t bESOFcnt;
34 } ResumeS;
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.
45 * Output : None.
46 * Return : None
47 *******************************************************************************/
48 void USB_Cable_Config(FunctionalState __attribute__((unused)) NewState)
51 /*******************************************************************************
52 * Function Name : PowerOn
53 * Description :
54 * Input : None.
55 * Output : None.
56 * Return : USB_SUCCESS.
57 *******************************************************************************/
58 RESULT PowerOn(void)
60 #ifndef STM32F10X_CL
61 uint16_t wRegVal;
63 /*** cable plugged-in ? ***/
64 USB_Cable_Config(ENABLE);
66 /*** CNTR_PWDN = 0 ***/
67 wRegVal = CNTR_FRES;
68 _SetCNTR(wRegVal);
70 /*** CNTR_FRES = 0 ***/
71 wInterrupt_Mask = 0;
72 _SetCNTR(wInterrupt_Mask);
73 /*** Clear pending interrupts ***/
74 _SetISTR(0);
75 /*** Set interrupt mask ***/
76 wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM;
77 _SetCNTR(wInterrupt_Mask);
78 #endif /* STM32F10X_CL */
80 return USB_SUCCESS;
83 /*******************************************************************************
84 * Function Name : PowerOff
85 * Description : handles switch-off conditions
86 * Input : None.
87 * Output : None.
88 * Return : USB_SUCCESS.
89 *******************************************************************************/
90 RESULT PowerOff()
92 #ifndef STM32F10X_CL
93 /* disable all ints and force USB reset */
94 _SetCNTR(CNTR_FRES);
95 /* clear interrupt status register */
96 _SetISTR(0);
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 */
104 /* ... */
106 return USB_SUCCESS;
109 /*******************************************************************************
110 * Function Name : Enter_LowPowerMode.
111 * Description : Power-off system clocks and power while entering suspend mode.
112 * Input : None.
113 * Output : None.
114 * Return : None.
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
125 * Input : None.
126 * Output : None.
127 * Return : USB_SUCCESS.
128 *******************************************************************************/
129 void Suspend(void)
131 #ifndef STM32F10X_CL
132 uint16_t wCNTR;
133 /* suspend preparation */
134 /* ... */
136 /* macrocell enters suspend mode */
137 wCNTR = _GetCNTR();
138 wCNTR |= CNTR_FSUSP;
139 _SetCNTR(wCNTR);
140 #endif /* STM32F10X_CL */
142 /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
143 /* power reduction */
144 /* ... on connected devices */
146 #ifndef STM32F10X_CL
147 /* force low-power mode in the macrocell */
148 wCNTR = _GetCNTR();
149 wCNTR |= CNTR_LPMODE;
150 _SetCNTR(wCNTR);
151 #endif /* STM32F10X_CL */
153 /* switch-off the clocks */
154 /* ... */
155 Enter_LowPowerMode();
158 /*******************************************************************************
159 * Function Name : Leave_LowPowerMode.
160 * Description : Restores system clocks and power while exiting suspend mode.
161 * Input : None.
162 * Output : None.
163 * Return : None.
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;
173 } else {
174 bDeviceState = ATTACHED;
178 /*******************************************************************************
179 * Function Name : Resume_Init
180 * Description : Handles wake-up restoring normal operations
181 * Input : None.
182 * Output : None.
183 * Return : USB_SUCCESS.
184 *******************************************************************************/
185 void Resume_Init(void)
187 #ifndef STM32F10X_CL
188 uint16_t wCNTR;
189 #endif /* STM32F10X_CL */
191 /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
192 /* restart the clocks */
193 /* ... */
195 #ifndef STM32F10X_CL
196 /* CNTR_LPMODE = 0 */
197 wCNTR = _GetCNTR();
198 wCNTR &= (~CNTR_LPMODE);
199 _SetCNTR(wCNTR);
200 #endif /* STM32F10X_CL */
202 /* restore full power */
203 /* ... on connected devices */
204 Leave_LowPowerMode();
206 #ifndef STM32F10X_CL
207 /* reset FSUSP bit */
208 _SetCNTR(IMR_MSK);
209 #endif /* STM32F10X_CL */
211 /* reverse suspend preparation */
212 /* ... */
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.
224 * Output : None.
225 * Return : None.
226 *******************************************************************************/
227 void Resume(RESUME_STATE eResumeSetVal)
229 #ifndef STM32F10X_CL
230 uint16_t wCNTR;
231 #endif /* STM32F10X_CL */
233 if (eResumeSetVal != RESUME_ESOF) {
234 ResumeS.eState = eResumeSetVal;
237 switch (ResumeS.eState) {
238 case RESUME_EXTERNAL:
239 Resume_Init();
240 ResumeS.eState = RESUME_OFF;
241 break;
242 case RESUME_INTERNAL:
243 Resume_Init();
244 ResumeS.eState = RESUME_START;
245 break;
246 case RESUME_LATER:
247 ResumeS.bESOFcnt = 2;
248 ResumeS.eState = RESUME_WAIT;
249 break;
250 case RESUME_WAIT:
251 ResumeS.bESOFcnt--;
252 if (ResumeS.bESOFcnt == 0) {
253 ResumeS.eState = RESUME_START;
255 break;
256 case RESUME_START:
257 #ifdef STM32F10X_CL
258 OTGD_FS_SetRemoteWakeup();
259 #else
260 wCNTR = _GetCNTR();
261 wCNTR |= CNTR_RESUME;
262 _SetCNTR(wCNTR);
263 #endif /* STM32F10X_CL */
264 ResumeS.eState = RESUME_ON;
265 ResumeS.bESOFcnt = 10;
266 break;
267 case RESUME_ON:
268 #ifndef STM32F10X_CL
269 ResumeS.bESOFcnt--;
270 if (ResumeS.bESOFcnt == 0) {
271 #endif /* STM32F10X_CL */
272 #ifdef STM32F10X_CL
273 OTGD_FS_ResetRemoteWakeup();
274 #else
275 wCNTR = _GetCNTR();
276 wCNTR &= (~CNTR_RESUME);
277 _SetCNTR(wCNTR);
278 #endif /* STM32F10X_CL */
279 ResumeS.eState = RESUME_OFF;
280 #ifndef STM32F10X_CL
282 #endif /* STM32F10X_CL */
283 break;
284 case RESUME_OFF:
285 case RESUME_ESOF:
286 default:
287 ResumeS.eState = RESUME_OFF;
288 break;
292 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/