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