Subversion Repositories NaviCtrl

Rev

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