Subversion Repositories NaviCtrl

Rev

Rev 41 | Details | Compare with Previous | Last modification | View Log | RSS feed

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