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 "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"
23 
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 */
30 
31 struct {
33  __IO uint8_t bESOFcnt;
34 } ResumeS;
35 
36 /* Extern variables ----------------------------------------------------------*/
37 /* Private function prototypes -----------------------------------------------*/
38 /* Extern function prototypes ------------------------------------------------*/
39 /* Private functions ---------------------------------------------------------*/
40 
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 NewState)
49 {
50 }
51 
52 /*******************************************************************************
53 * Function Name : PowerOn
54 * Description :
55 * Input : None.
56 * Output : None.
57 * Return : USB_SUCCESS.
58 *******************************************************************************/
59 RESULT PowerOn(void)
60 {
61 #ifndef STM32F10X_CL
62  uint16_t wRegVal;
63 
64  /*** cable plugged-in ? ***/
65  USB_Cable_Config(ENABLE);
66 
67  /*** CNTR_PWDN = 0 ***/
68  wRegVal = CNTR_FRES;
69  _SetCNTR(wRegVal);
70 
71  /*** CNTR_FRES = 0 ***/
72  wInterrupt_Mask = 0;
73  _SetCNTR(wInterrupt_Mask);
74  /*** Clear pending interrupts ***/
75  _SetISTR(0);
76  /*** Set interrupt mask ***/
77  wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM;
78  _SetCNTR(wInterrupt_Mask);
79 #endif /* STM32F10X_CL */
80 
81  return USB_SUCCESS;
82 }
83 
84 /*******************************************************************************
85 * Function Name : PowerOff
86 * Description : handles switch-off conditions
87 * Input : None.
88 * Output : None.
89 * Return : USB_SUCCESS.
90 *******************************************************************************/
91 RESULT PowerOff()
92 {
93 #ifndef STM32F10X_CL
94  /* disable all ints and force USB reset */
95  _SetCNTR(CNTR_FRES);
96  /* clear interrupt status register */
97  _SetISTR(0);
98  /* Disable the Pull-Up */
99  USB_Cable_Config(DISABLE);
100  /* switch-off device */
101  _SetCNTR(CNTR_FRES + CNTR_PDWN);
102 #endif /* STM32F10X_CL */
103 
104  /* sw variables reset */
105  /* ... */
106 
107  return USB_SUCCESS;
108 }
109 
110 /*******************************************************************************
111  * Function Name : Enter_LowPowerMode.
112  * Description : Power-off system clocks and power while entering suspend mode.
113  * Input : None.
114  * Output : None.
115  * Return : None.
116  *******************************************************************************/
118 {
119  /* Set the device state to suspend */
121 }
122 
123 /*******************************************************************************
124 * Function Name : Suspend
125 * Description : sets suspend mode operating conditions
126 * Input : None.
127 * Output : None.
128 * Return : USB_SUCCESS.
129 *******************************************************************************/
130 void Suspend(void)
131 {
132 #ifndef STM32F10X_CL
133  uint16_t wCNTR;
134  /* suspend preparation */
135  /* ... */
136 
137  /* macrocell enters suspend mode */
138  wCNTR = _GetCNTR();
139  wCNTR |= CNTR_FSUSP;
140  _SetCNTR(wCNTR);
141 #endif /* STM32F10X_CL */
142 
143  /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
144  /* power reduction */
145  /* ... on connected devices */
146 
147 #ifndef STM32F10X_CL
148  /* force low-power mode in the macrocell */
149  wCNTR = _GetCNTR();
150  wCNTR |= CNTR_LPMODE;
151  _SetCNTR(wCNTR);
152 #endif /* STM32F10X_CL */
153 
154  /* switch-off the clocks */
155  /* ... */
157 
158 }
159 
160 /*******************************************************************************
161  * Function Name : Leave_LowPowerMode.
162  * Description : Restores system clocks and power while exiting suspend mode.
163  * Input : None.
164  * Output : None.
165  * Return : None.
166  *******************************************************************************/
168 {
169  DEVICE_INFO *pInfo = &Device_Info;
170 
171  /* Set the device state to the correct state */
172  if (pInfo->Current_Configuration != 0) {
173  /* Device configured */
175  } else {
177  }
178 }
179 
180 /*******************************************************************************
181 * Function Name : Resume_Init
182 * Description : Handles wake-up restoring normal operations
183 * Input : None.
184 * Output : None.
185 * Return : USB_SUCCESS.
186 *******************************************************************************/
187 void Resume_Init(void)
188 {
189 #ifndef STM32F10X_CL
190  uint16_t wCNTR;
191 #endif /* STM32F10X_CL */
192 
193  /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
194  /* restart the clocks */
195  /* ... */
196 
197 #ifndef STM32F10X_CL
198  /* CNTR_LPMODE = 0 */
199  wCNTR = _GetCNTR();
200  wCNTR &= (~CNTR_LPMODE);
201  _SetCNTR(wCNTR);
202 #endif /* STM32F10X_CL */
203 
204  /* restore full power */
205  /* ... on connected devices */
207 
208 #ifndef STM32F10X_CL
209  /* reset FSUSP bit */
210  _SetCNTR(IMR_MSK);
211 #endif /* STM32F10X_CL */
212 
213  /* reverse suspend preparation */
214  /* ... */
215 
216 }
217 
218 /*******************************************************************************
219 * Function Name : Resume
220 * Description : This is the state machine handling resume operations and
221 * timing sequence. The control is based on the Resume structure
222 * variables and on the ESOF interrupt calling this subroutine
223 * without changing machine state.
224 * Input : a state machine value (RESUME_STATE)
225 * RESUME_ESOF doesn't change ResumeS.eState allowing
226 * decrementing of the ESOF counter in different states.
227 * Output : None.
228 * Return : None.
229 *******************************************************************************/
230 void Resume(RESUME_STATE eResumeSetVal)
231 {
232 #ifndef STM32F10X_CL
233  uint16_t wCNTR;
234 #endif /* STM32F10X_CL */
235 
236  if (eResumeSetVal != RESUME_ESOF)
237  ResumeS.eState = eResumeSetVal;
238 
239  switch (ResumeS.eState) {
240  case RESUME_EXTERNAL:
241  Resume_Init();
242  ResumeS.eState = RESUME_OFF;
243  break;
244  case RESUME_INTERNAL:
245  Resume_Init();
246  ResumeS.eState = RESUME_START;
247  break;
248  case RESUME_LATER:
249  ResumeS.bESOFcnt = 2;
250  ResumeS.eState = RESUME_WAIT;
251  break;
252  case RESUME_WAIT:
253  ResumeS.bESOFcnt--;
254  if (ResumeS.bESOFcnt == 0)
255  ResumeS.eState = RESUME_START;
256  break;
257  case RESUME_START:
258 #ifdef STM32F10X_CL
259  OTGD_FS_SetRemoteWakeup();
260 #else
261  wCNTR = _GetCNTR();
262  wCNTR |= CNTR_RESUME;
263  _SetCNTR(wCNTR);
264 #endif /* STM32F10X_CL */
265  ResumeS.eState = RESUME_ON;
266  ResumeS.bESOFcnt = 10;
267  break;
268  case RESUME_ON:
269 #ifndef STM32F10X_CL
270  ResumeS.bESOFcnt--;
271  if (ResumeS.bESOFcnt == 0) {
272 #endif /* STM32F10X_CL */
273 #ifdef STM32F10X_CL
274  OTGD_FS_ResetRemoteWakeup();
275 #else
276  wCNTR = _GetCNTR();
277  wCNTR &= (~CNTR_RESUME);
278  _SetCNTR(wCNTR);
279 #endif /* STM32F10X_CL */
280  ResumeS.eState = RESUME_OFF;
281 #ifndef STM32F10X_CL
282  }
283 #endif /* STM32F10X_CL */
284  break;
285  case RESUME_OFF:
286  case RESUME_ESOF:
287  default:
288  ResumeS.eState = RESUME_OFF;
289  break;
290  }
291 }
292 
293 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
Main PiOS header to include all the compiled in PiOS options.
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)