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