Rev 111 | Rev 308 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 111 | Rev 275 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /******************** (C) COPYRIGHT 2006 STMicroelectronics ******************** |
1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** |
2 | * File Name : usb_pwr.c |
2 | * File Name : usb_pwr.c |
3 | * Author : MCD Application Team |
3 | * Author : MCD Application Team |
- | 4 | * Version : V2.0.0 |
|
4 | * Date First Issued : 10/01/2006 : V1.0 |
5 | * Date : 09/29/2008 |
5 | * Description : Connection/disconnection & power management |
6 | * Description : Connection/disconnection & power management |
6 | ******************************************************************************** |
7 | ******************************************************************************** |
7 | * History: |
- | |
8 | * 10/01/2006 : V1.0 |
- | |
9 | ******************************************************************************** |
- | |
10 | * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
8 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
11 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. |
9 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. |
12 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, |
10 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, |
13 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE |
11 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE |
14 | * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING |
12 | * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING |
15 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
13 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
16 | *******************************************************************************/ |
14 | *******************************************************************************/ |
- | 15 | ||
17 | /* Includes ------------------------------------------------------------------*/ |
16 | /* Includes ------------------------------------------------------------------*/ |
- | 17 | #include "91x_lib.h" |
|
18 | #include "usb_lib.h" |
18 | #include "usb_lib.h" |
- | 19 | #include "usb_conf.h" |
|
19 | #include "usb_pwr.h" |
20 | #include "usb_pwr.h" |
20 | #include "usb.h" |
21 | #include "usb.h" |
- | 22 | ||
21 | /* Private typedef -----------------------------------------------------------*/ |
23 | /* Private typedef -----------------------------------------------------------*/ |
22 | /* Private define ------------------------------------------------------------*/ |
24 | /* Private define ------------------------------------------------------------*/ |
23 | /* Private macro -------------------------------------------------------------*/ |
25 | /* Private macro -------------------------------------------------------------*/ |
24 | /* Private variables ---------------------------------------------------------*/ |
26 | /* Private variables ---------------------------------------------------------*/ |
25 | volatile bool fCellSuspended; |
- | |
26 | volatile u8 bDeviceState=UNCONNECTED; /* USB device status */ |
27 | vu32 bDeviceState = UNCONNECTED; /* USB device status */ |
27 | volatile bool fSuspendEnabled=TRUE; /* true when suspend is possible */ |
28 | volatile bool fSuspendEnabled = TRUE; /* true when suspend is possible */ |
28 | 29 | ||
29 | struct { |
30 | struct |
- | 31 | { |
|
30 | volatile RESUME_STATE eState; |
32 | volatile RESUME_STATE eState; |
31 | volatile u8 bESOFcnt; |
33 | volatile u8 bESOFcnt; |
- | 34 | } |
|
32 | } ResumeS; |
35 | ResumeS; |
- | 36 | ||
33 | /* Extern variables ----------------------------------------------------------*/ |
37 | /* Extern variables ----------------------------------------------------------*/ |
34 | /* Private function prototypes -----------------------------------------------*/ |
38 | /* Private function prototypes -----------------------------------------------*/ |
35 | /* Extern function prototypes ------------------------------------------------*/ |
39 | /* Extern function prototypes ------------------------------------------------*/ |
36 | /* Private functions ---------------------------------------------------------*/ |
40 | /* Private functions ---------------------------------------------------------*/ |
- | 41 | ||
37 | /******************************************************************************* |
42 | /******************************************************************************* |
38 | * Function Name : PowerOn |
43 | * Function Name : PowerOn |
39 | * Description : |
44 | * Description : USB IP power ON Routine. |
40 | * Input : None. |
45 | * Input : None. |
41 | * Output : None. |
46 | * Output : None. |
42 | * Return : USB_SUCCESS. |
47 | * Return : USB_SUCCESS. |
43 | *******************************************************************************/ |
48 | *******************************************************************************/ |
44 | RESULT PowerOn(void) |
49 | RESULT PowerOn(void) |
45 | { |
50 | { |
46 | u16 wRegVal; |
51 | u16 wRegVal; |
- | 52 | ||
- | 53 | /*** cable plugged-in ? ***/ |
|
- | 54 | USB_CableConfig(ENABLE); |
|
Line 47... | Line -... | ||
47 | - | ||
48 | /*** cable plugged-in ? ***/ |
- | |
49 | /*while(!CablePluggedIn());*/ |
- | |
50 | USB_CableConfig(ENABLE); |
- | |
51 | 55 | ||
52 | /*** CNTR_PWDN = 0 ***/ |
56 | /*** CNTR_PWDN = 0 ***/ |
53 | wRegVal = CNTR_FRES; |
57 | wRegVal = CNTR_FRES; |
54 | _SetCNTR(wRegVal); |
58 | _SetCNTR(wRegVal); |
55 | 59 | ||
56 | /*** CNTR_FRES = 0 ***/ |
60 | /*** CNTR_FRES = 0 ***/ |
57 | wInterrupt_Mask = 0; |
61 | wInterrupt_Mask = 0; |
58 | _SetCNTR(wInterrupt_Mask); |
62 | _SetCNTR(wInterrupt_Mask); |
59 | /*** Clear pending interrupts ***/ |
63 | /*** Clear pending interrupts ***/ |
60 | _SetISTR(0); |
64 | _SetISTR(0); |
61 | /*** Set interrupt mask ***/ |
65 | /*** Set interrupt mask ***/ |
62 | wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; |
66 | wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; |
Line 63... | Line 67... | ||
63 | _SetCNTR(wInterrupt_Mask); |
67 | _SetCNTR(wInterrupt_Mask); |
64 | 68 | ||
- | 69 | return USB_SUCCESS; |
|
65 | return USB_SUCCESS; |
70 | } |
66 | } |
71 | |
67 | /******************************************************************************* |
72 | /******************************************************************************* |
68 | * Function Name : PowerOff |
73 | * Function Name : PowerOff |
69 | * Description : handles switch-off conditions |
74 | * Description : handles switch-off conditions |
70 | * Input : None. |
75 | * Input : None. |
71 | * Output : None. |
76 | * Output : None. |
72 | * Return : USB_SUCCESS. |
77 | * Return : USB_SUCCESS. |
73 | *******************************************************************************/ |
78 | *******************************************************************************/ |
74 | RESULT PowerOff() |
79 | RESULT PowerOff() |
75 | { |
80 | { |
76 | /* disable all ints and force USB reset */ |
81 | /* disable all ints and force USB reset */ |
77 | _SetCNTR(CNTR_FRES); |
82 | _SetCNTR(CNTR_FRES); |
78 | /* clear interrupt status register */ |
83 | /* clear interrupt status register */ |
79 | _SetISTR(0); |
84 | _SetISTR(0); |
80 | /* Disable the Pull-Up*/ |
85 | /* Disable the Pull-Up*/ |
81 | USB_CableConfig(DISABLE); |
86 | USB_CableConfig(DISABLE); |
82 | /* switch-off device */ |
87 | /* switch-off device */ |
83 | _SetCNTR(CNTR_FRES+CNTR_PDWN); |
88 | _SetCNTR(CNTR_FRES + CNTR_PDWN); |
Line 84... | Line 89... | ||
84 | /* sw variables reset */ |
89 | /* sw variables reset */ |
85 | /* ... */ |
90 | /* ... */ |
- | 91 | ||
86 | 92 | return USB_SUCCESS; |
|
87 | return USB_SUCCESS; |
93 | } |
88 | } |
94 | |
89 | /******************************************************************************* |
95 | /******************************************************************************* |
90 | * Function Name : Suspend |
96 | * Function Name : Suspend |
91 | * Description : sets suspend mode operating conditions |
97 | * Description : sets suspend mode operating conditions |
92 | * Input : None. |
98 | * Input : None. |
93 | * Output : None. |
99 | * Output : None. |
94 | * Return : USB_SUCCESS. |
100 | * Return : USB_SUCCESS. |
95 | *******************************************************************************/ |
101 | *******************************************************************************/ |
96 | void Suspend(void) |
- | |
97 | { |
102 | void Suspend(void) |
98 | u16 wCNTR; |
- | |
99 | fCellSuspended= TRUE; |
- | |
100 | /* suspend preparation */ |
- | |
101 | /* ... */ |
- | |
102 | - | ||
103 | /* macrocell enters suspend mode */ |
- | |
104 | wCNTR = _GetCNTR(); |
- | |
105 | wCNTR |= CNTR_FSUSP; |
- | |
106 | _SetCNTR(wCNTR); |
- | |
107 | - | ||
108 | /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */ |
- | |
109 | /* power reduction */ |
- | |
110 | /* ... on connected devices */ |
- | |
111 | - | ||
112 | /* force low-power mode in the macrocell */ |
- | |
113 | wCNTR = _GetCNTR(); |
- | |
114 | wCNTR |= CNTR_LPMODE; |
- | |
115 | _SetCNTR(wCNTR); |
103 | { |
116 | - | ||
Line -... | Line 104... | ||
- | 104 | u16 wCNTR; |
|
- | 105 | /* suspend preparation */ |
|
- | 106 | /* ... */ |
|
- | 107 | ||
- | 108 | /* macrocell enters suspend mode */ |
|
- | 109 | wCNTR = _GetCNTR(); |
|
- | 110 | wCNTR |= CNTR_FSUSP; |
|
- | 111 | _SetCNTR(wCNTR); |
|
- | 112 | ||
- | 113 | /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */ |
|
- | 114 | /* power reduction */ |
|
- | 115 | /* ... on connected devices */ |
|
- | 116 | ||
- | 117 | ||
- | 118 | /* force low-power mode in the macrocell */ |
|
- | 119 | wCNTR = _GetCNTR(); |
|
- | 120 | wCNTR |= CNTR_LPMODE; |
|
- | 121 | _SetCNTR(wCNTR); |
|
117 | /* switch-off the clocks */ |
122 | |
- | 123 | /* switch-off the clocks */ |
|
118 | /* ... */ |
124 | /* ... */ |
119 | 125 | USB_EnterLowPowerMode(); |
|
120 | 126 | } |
|
121 | } |
127 | |
122 | /******************************************************************************* |
128 | /******************************************************************************* |
123 | * Function Name : Resume_Init |
129 | * Function Name : Resume_Init |
124 | * Description : Handles wake-up restoring normal operations |
130 | * Description : Handles wake-up restoring normal operations |
125 | * Input : None. |
131 | * Input : None. |
126 | * Output : None. |
132 | * Output : None. |
127 | * Return : USB_SUCCESS. |
133 | * Return : USB_SUCCESS. |
- | 134 | *******************************************************************************/ |
|
- | 135 | void Resume_Init(void) |
|
- | 136 | { |
|
- | 137 | u16 wCNTR; |
|
- | 138 | /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */ |
|
- | 139 | /* restart the clocks */ |
|
- | 140 | /* ... */ |
|
- | 141 | ||
- | 142 | /* CNTR_LPMODE = 0 */ |
|
- | 143 | wCNTR = _GetCNTR(); |
|
- | 144 | wCNTR &= (~CNTR_LPMODE); |
|
- | 145 | _SetCNTR(wCNTR); |
|
Line 128... | Line -... | ||
128 | *******************************************************************************/ |
- | |
129 | void Resume_Init(void) |
- | |
130 | { |
- | |
131 | u16 wCNTR; |
- | |
132 | - | ||
133 | fCellSuspended= FALSE; |
- | |
134 | - | ||
135 | /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */ |
- | |
136 | /* restart the clocks */ |
- | |
137 | /* ... */ |
- | |
138 | - | ||
139 | /* CNTR_LPMODE = 0 */ |
- | |
140 | wCNTR = _GetCNTR(); |
- | |
141 | wCNTR &= (~CNTR_LPMODE); |
- | |
142 | _SetCNTR(wCNTR); |
146 | |
143 | 147 | /* restore full power */ |
|
144 | /* restore full power */ |
- | |
145 | /* ... on connected devices */ |
- | |
146 | - | ||
Line -... | Line 148... | ||
- | 148 | /* ... on connected devices */ |
|
- | 149 | USB_LeaveLowPowerMode(); |
|
147 | /* reset FSUSP bit */ |
150 | |
- | 151 | /* reset FSUSP bit */ |
|
148 | _SetCNTR(IMR_MSK); |
152 | _SetCNTR(IMR_MSK); |
149 | 153 | ||
150 | /* reverse suspend preparation */ |
154 | /* reverse suspend preparation */ |
151 | /* ... */ |
155 | /* ... */ |
152 | 156 | } |
|
Line 163... | Line 167... | ||
163 | * Output : None. |
167 | * Output : None. |
164 | * Return : None. |
168 | * Return : None. |
165 | *******************************************************************************/ |
169 | *******************************************************************************/ |
166 | void Resume(RESUME_STATE eResumeSetVal) |
170 | void Resume(RESUME_STATE eResumeSetVal) |
167 | { |
171 | { |
168 | u16 wCNTR; |
172 | u16 wCNTR; |
169 | u16 wRegVal; |
- | |
Line 170... | Line 173... | ||
170 | 173 | ||
171 | if(eResumeSetVal != RESUME_ESOF) |
174 | if (eResumeSetVal != RESUME_ESOF) |
Line 172... | Line 175... | ||
172 | ResumeS.eState = eResumeSetVal; |
175 | ResumeS.eState = eResumeSetVal; |
173 | 176 | ||
174 | switch(ResumeS.eState) |
177 | switch (ResumeS.eState) |
175 | { |
178 | { |
176 | case RESUME_EXTERNAL: |
- | |
177 | Resume_Init(); |
- | |
178 | wRegVal = GetFNR(); |
- | |
179 | if(wRegVal & FNR_RXDP) //10 & 11 false conditions |
- | |
180 | { |
- | |
181 | Suspend(); |
179 | case RESUME_EXTERNAL: |
182 | } |
180 | Resume_Init(); |
183 | ResumeS.eState = RESUME_OFF; |
181 | ResumeS.eState = RESUME_OFF; |
184 | break; |
182 | break; |
185 | case RESUME_INTERNAL: |
183 | case RESUME_INTERNAL: |
186 | Resume_Init(); |
184 | Resume_Init(); |
187 | ResumeS.eState = RESUME_START; |
185 | ResumeS.eState = RESUME_START; |
188 | break; |
186 | break; |
189 | case RESUME_LATER: |
187 | case RESUME_LATER: |
190 | ResumeS.bESOFcnt = 2; |
188 | ResumeS.bESOFcnt = 2; |
191 | ResumeS.eState = RESUME_WAIT; |
189 | ResumeS.eState = RESUME_WAIT; |
192 | break; |
190 | break; |
193 | case RESUME_WAIT: |
191 | case RESUME_WAIT: |
194 | ResumeS.bESOFcnt--; |
192 | ResumeS.bESOFcnt--; |
195 | if(ResumeS.bESOFcnt == 0) |
193 | if (ResumeS.bESOFcnt == 0) |
196 | ResumeS.eState = RESUME_START; |
194 | ResumeS.eState = RESUME_START; |
197 | break; |
195 | break; |
198 | case RESUME_START: |
196 | case RESUME_START: |
199 | wCNTR = _GetCNTR(); |
197 | wCNTR = _GetCNTR(); |
200 | wCNTR |= CNTR_RESUME; |
198 | wCNTR |= CNTR_RESUME; |
201 | _SetCNTR(wCNTR); |
199 | _SetCNTR(wCNTR); |
202 | ResumeS.eState = RESUME_ON; |
200 | ResumeS.eState = RESUME_ON; |
203 | ResumeS.bESOFcnt = 10; |
201 | ResumeS.bESOFcnt = 10; |
204 | break; |
202 | break; |
205 | case RESUME_ON: |
203 | case RESUME_ON: |
206 | ResumeS.bESOFcnt--; |
204 | ResumeS.bESOFcnt--; |
207 | if(ResumeS.bESOFcnt == 0) |
205 | if (ResumeS.bESOFcnt == 0) |
208 | { |
206 | { |
209 | wCNTR = _GetCNTR(); |
207 | wCNTR = _GetCNTR(); |
210 | wCNTR &= (~CNTR_RESUME); |
208 | wCNTR &= (~CNTR_RESUME); |
211 | _SetCNTR(wCNTR); |
209 | _SetCNTR(wCNTR); |
212 | ResumeS.eState = RESUME_OFF; |
210 | ResumeS.eState = RESUME_OFF; |
213 | } |
211 | } |
214 | break; |
212 | break; |
215 | case RESUME_OFF: |
213 | case RESUME_OFF: |
216 | case RESUME_ESOF: |
214 | case RESUME_ESOF: |
217 | default: |
215 | default: |
218 | ResumeS.eState = RESUME_OFF; |
216 | ResumeS.eState = RESUME_OFF; |
219 | break; |
217 | break; |
- | 218 | } |
|
220 | } |
219 | } |