dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_usb_hid_pwr.c
Go to the documentation of this file.
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 *******************************************************************************/
15 
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"
22 
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 */
29 
30 struct {
32  __IO uint8_t bESOFcnt;
33 } ResumeS;
34 
35 /* Extern variables ----------------------------------------------------------*/
36 /* Private function prototypes -----------------------------------------------*/
37 /* Extern function prototypes ------------------------------------------------*/
38 /* Private functions ---------------------------------------------------------*/
39 
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(FunctionalState NewState)
48 {
49 }
50 
51 /*******************************************************************************
52 * Function Name : PowerOn
53 * Description :
54 * Input : None.
55 * Output : None.
56 * Return : USB_SUCCESS.
57 *******************************************************************************/
58 RESULT PowerOn(void)
59 {
60 #ifndef STM32F10X_CL
61  uint16_t wRegVal;
62 
63  /*** cable plugged-in ? ***/
64  USB_Cable_Config(ENABLE);
65 
66  /*** CNTR_PWDN = 0 ***/
67  wRegVal = CNTR_FRES;
68  _SetCNTR(wRegVal);
69 
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 */
79 
80  return USB_SUCCESS;
81 }
82 
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()
91 {
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 */
102 
103  /* sw variables reset */
104  /* ... */
105 
106  return USB_SUCCESS;
107 }
108 
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  *******************************************************************************/
117 {
118  /* Set the device state to suspend */
120 }
121 
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)
130 {
131 #ifndef STM32F10X_CL
132  uint16_t wCNTR;
133  /* suspend preparation */
134  /* ... */
135 
136  /* macrocell enters suspend mode */
137  wCNTR = _GetCNTR();
138  wCNTR |= CNTR_FSUSP;
139  _SetCNTR(wCNTR);
140 #endif /* STM32F10X_CL */
141 
142  /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
143  /* power reduction */
144  /* ... on connected devices */
145 
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 */
152 
153  /* switch-off the clocks */
154  /* ... */
156 
157 }
158 
159 /*******************************************************************************
160  * Function Name : Leave_LowPowerMode.
161  * Description : Restores system clocks and power while exiting suspend mode.
162  * Input : None.
163  * Output : None.
164  * Return : None.
165  *******************************************************************************/
167 {
168  DEVICE_INFO *pInfo = &Device_Info;
169 
170  /* Set the device state to the correct state */
171  if (pInfo->Current_Configuration != 0) {
172  /* Device configured */
174  } else {
176  }
177 }
178 
179 /*******************************************************************************
180 * Function Name : Resume_Init
181 * Description : Handles wake-up restoring normal operations
182 * Input : None.
183 * Output : None.
184 * Return : USB_SUCCESS.
185 *******************************************************************************/
186 void Resume_Init(void)
187 {
188 #ifndef STM32F10X_CL
189  uint16_t wCNTR;
190 #endif /* STM32F10X_CL */
191 
192  /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
193  /* restart the clocks */
194  /* ... */
195 
196 #ifndef STM32F10X_CL
197  /* CNTR_LPMODE = 0 */
198  wCNTR = _GetCNTR();
199  wCNTR &= (~CNTR_LPMODE);
200  _SetCNTR(wCNTR);
201 #endif /* STM32F10X_CL */
202 
203  /* restore full power */
204  /* ... on connected devices */
206 
207 #ifndef STM32F10X_CL
208  /* reset FSUSP bit */
209  _SetCNTR(IMR_MSK);
210 #endif /* STM32F10X_CL */
211 
212  /* reverse suspend preparation */
213  /* ... */
214 
215 }
216 
217 /*******************************************************************************
218 * Function Name : Resume
219 * Description : This is the state machine handling resume operations and
220 * timing sequence. The control is based on the Resume structure
221 * variables and on the ESOF interrupt calling this subroutine
222 * without changing machine state.
223 * Input : a state machine value (RESUME_STATE)
224 * RESUME_ESOF doesn't change ResumeS.eState allowing
225 * decrementing of the ESOF counter in different states.
226 * Output : None.
227 * Return : None.
228 *******************************************************************************/
229 void Resume(RESUME_STATE eResumeSetVal)
230 {
231 #ifndef STM32F10X_CL
232  uint16_t wCNTR;
233 #endif /* STM32F10X_CL */
234 
235  if (eResumeSetVal != RESUME_ESOF)
236  ResumeS.eState = eResumeSetVal;
237 
238  switch (ResumeS.eState) {
239  case RESUME_EXTERNAL:
240  Resume_Init();
241  ResumeS.eState = RESUME_OFF;
242  break;
243  case RESUME_INTERNAL:
244  Resume_Init();
245  ResumeS.eState = RESUME_START;
246  break;
247  case RESUME_LATER:
248  ResumeS.bESOFcnt = 2;
249  ResumeS.eState = RESUME_WAIT;
250  break;
251  case RESUME_WAIT:
252  ResumeS.bESOFcnt--;
253  if (ResumeS.bESOFcnt == 0)
254  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
281  }
282 #endif /* STM32F10X_CL */
283  break;
284  case RESUME_OFF:
285  case RESUME_ESOF:
286  default:
287  ResumeS.eState = RESUME_OFF;
288  break;
289  }
290 }
291 
292 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
RESULT PowerOff()
__IO uint32_t bDeviceState
enum _RESUME_STATE RESUME_STATE
USB HID layer functions header.
void Leave_LowPowerMode(void)
__IO bool fSuspendEnabled
void Resume_Init(void)
void Resume(RESUME_STATE eResumeSetVal)
void Enter_LowPowerMode(void)
RESULT PowerOn(void)
__IO RESUME_STATE eState
struct @21 ResumeS
void Suspend(void)
__IO uint8_t bESOFcnt
void USB_Cable_Config(FunctionalState NewState)