update credits
[librepilot.git] / flight / pios / stm32f10x / pios_usb_hid_pwr.c
blobffa9c3495466a49fe95f87433d83ed3473f9cf29
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 "stm32f10x.h"
18 #include "usb_lib.h"
19 #include "usb_conf.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 */
30 struct {
31 __IO RESUME_STATE eState;
32 __IO uint8_t bESOFcnt;
33 } ResumeS;
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.
44 * Output : None.
45 * Return : None
46 *******************************************************************************/
47 void USB_Cable_Config(__attribute__((unused)) FunctionalState NewState)
50 /*******************************************************************************
51 * Function Name : PowerOn
52 * Description :
53 * Input : None.
54 * Output : None.
55 * Return : USB_SUCCESS.
56 *******************************************************************************/
57 RESULT PowerOn(void)
59 #ifndef STM32F10X_CL
60 uint16_t wRegVal;
62 /*** cable plugged-in ? ***/
63 USB_Cable_Config(ENABLE);
65 /*** CNTR_PWDN = 0 ***/
66 wRegVal = CNTR_FRES;
67 _SetCNTR(wRegVal);
69 /*** CNTR_FRES = 0 ***/
70 wInterrupt_Mask = 0;
71 _SetCNTR(wInterrupt_Mask);
72 /*** Clear pending interrupts ***/
73 _SetISTR(0);
74 /*** Set interrupt mask ***/
75 wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM;
76 _SetCNTR(wInterrupt_Mask);
77 #endif /* STM32F10X_CL */
79 return USB_SUCCESS;
82 /*******************************************************************************
83 * Function Name : PowerOff
84 * Description : handles switch-off conditions
85 * Input : None.
86 * Output : None.
87 * Return : USB_SUCCESS.
88 *******************************************************************************/
89 RESULT PowerOff()
91 #ifndef STM32F10X_CL
92 /* disable all ints and force USB reset */
93 _SetCNTR(CNTR_FRES);
94 /* clear interrupt status register */
95 _SetISTR(0);
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 */
103 /* ... */
105 return USB_SUCCESS;
108 /*******************************************************************************
109 * Function Name : Enter_LowPowerMode.
110 * Description : Power-off system clocks and power while entering suspend mode.
111 * Input : None.
112 * Output : None.
113 * Return : None.
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
124 * Input : None.
125 * Output : None.
126 * Return : USB_SUCCESS.
127 *******************************************************************************/
128 void Suspend(void)
130 #ifndef STM32F10X_CL
131 uint16_t wCNTR;
132 /* suspend preparation */
133 /* ... */
135 /* macrocell enters suspend mode */
136 wCNTR = _GetCNTR();
137 wCNTR |= CNTR_FSUSP;
138 _SetCNTR(wCNTR);
139 #endif /* STM32F10X_CL */
141 /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
142 /* power reduction */
143 /* ... on connected devices */
145 #ifndef STM32F10X_CL
146 /* force low-power mode in the macrocell */
147 wCNTR = _GetCNTR();
148 wCNTR |= CNTR_LPMODE;
149 _SetCNTR(wCNTR);
150 #endif /* STM32F10X_CL */
152 /* switch-off the clocks */
153 /* ... */
154 Enter_LowPowerMode();
157 /*******************************************************************************
158 * Function Name : Leave_LowPowerMode.
159 * Description : Restores system clocks and power while exiting suspend mode.
160 * Input : None.
161 * Output : None.
162 * Return : None.
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;
172 } else {
173 bDeviceState = ATTACHED;
177 /*******************************************************************************
178 * Function Name : Resume_Init
179 * Description : Handles wake-up restoring normal operations
180 * Input : None.
181 * Output : None.
182 * Return : USB_SUCCESS.
183 *******************************************************************************/
184 void Resume_Init(void)
186 #ifndef STM32F10X_CL
187 uint16_t wCNTR;
188 #endif /* STM32F10X_CL */
190 /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
191 /* restart the clocks */
192 /* ... */
194 #ifndef STM32F10X_CL
195 /* CNTR_LPMODE = 0 */
196 wCNTR = _GetCNTR();
197 wCNTR &= (~CNTR_LPMODE);
198 _SetCNTR(wCNTR);
199 #endif /* STM32F10X_CL */
201 /* restore full power */
202 /* ... on connected devices */
203 Leave_LowPowerMode();
205 #ifndef STM32F10X_CL
206 /* reset FSUSP bit */
207 _SetCNTR(IMR_MSK);
208 #endif /* STM32F10X_CL */
210 /* reverse suspend preparation */
211 /* ... */
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.
223 * Output : None.
224 * Return : None.
225 *******************************************************************************/
226 void Resume(RESUME_STATE eResumeSetVal)
228 #ifndef STM32F10X_CL
229 uint16_t wCNTR;
230 #endif /* STM32F10X_CL */
232 if (eResumeSetVal != RESUME_ESOF) {
233 ResumeS.eState = eResumeSetVal;
236 switch (ResumeS.eState) {
237 case RESUME_EXTERNAL:
238 Resume_Init();
239 ResumeS.eState = RESUME_OFF;
240 break;
241 case RESUME_INTERNAL:
242 Resume_Init();
243 ResumeS.eState = RESUME_START;
244 break;
245 case RESUME_LATER:
246 ResumeS.bESOFcnt = 2;
247 ResumeS.eState = RESUME_WAIT;
248 break;
249 case RESUME_WAIT:
250 ResumeS.bESOFcnt--;
251 if (ResumeS.bESOFcnt == 0) {
252 ResumeS.eState = RESUME_START;
254 break;
255 case RESUME_START:
256 #ifdef STM32F10X_CL
257 OTGD_FS_SetRemoteWakeup();
258 #else
259 wCNTR = _GetCNTR();
260 wCNTR |= CNTR_RESUME;
261 _SetCNTR(wCNTR);
262 #endif /* STM32F10X_CL */
263 ResumeS.eState = RESUME_ON;
264 ResumeS.bESOFcnt = 10;
265 break;
266 case RESUME_ON:
267 #ifndef STM32F10X_CL
268 ResumeS.bESOFcnt--;
269 if (ResumeS.bESOFcnt == 0) {
270 #endif /* STM32F10X_CL */
271 #ifdef STM32F10X_CL
272 OTGD_FS_ResetRemoteWakeup();
273 #else
274 wCNTR = _GetCNTR();
275 wCNTR &= (~CNTR_RESUME);
276 _SetCNTR(wCNTR);
277 #endif /* STM32F10X_CL */
278 ResumeS.eState = RESUME_OFF;
279 #ifndef STM32F10X_CL
281 #endif /* STM32F10X_CL */
282 break;
283 case RESUME_OFF:
284 case RESUME_ESOF:
285 default:
286 ResumeS.eState = RESUME_OFF;
287 break;
291 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/