Subversion Repositories NaviCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
275 killagreg 1
/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
1 ingob 2
* File Name          : usb_pwr.c
3
* Author             : MCD Application Team
275 killagreg 4
* Version            : V2.0.0
5
* Date               : 09/29/2008
1 ingob 6
* Description        : Connection/disconnection & power management
7
********************************************************************************
275 killagreg 8
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
1 ingob 9
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
10
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
11
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
12
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
13
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
14
*******************************************************************************/
275 killagreg 15
 
1 ingob 16
/* Includes ------------------------------------------------------------------*/
275 killagreg 17
#include "91x_lib.h"
41 ingob 18
#include "usb_lib.h"
275 killagreg 19
#include "usb_conf.h"
41 ingob 20
#include "usb_pwr.h"
111 killagreg 21
#include "usb.h"
275 killagreg 22
 
1 ingob 23
/* Private typedef -----------------------------------------------------------*/
24
/* Private define ------------------------------------------------------------*/
25
/* Private macro -------------------------------------------------------------*/
26
/* Private variables ---------------------------------------------------------*/
275 killagreg 27
vu32 bDeviceState = UNCONNECTED; /* USB device status */
28
volatile bool fSuspendEnabled = TRUE;  /* true when suspend is possible */
1 ingob 29
 
275 killagreg 30
struct
31
{
32
  volatile RESUME_STATE eState;
33
  volatile u8 bESOFcnt;
34
}
35
ResumeS;
36
 
1 ingob 37
/* Extern variables ----------------------------------------------------------*/
38
/* Private function prototypes -----------------------------------------------*/
39
/* Extern function prototypes ------------------------------------------------*/
40
/* Private functions ---------------------------------------------------------*/
275 killagreg 41
 
1 ingob 42
/*******************************************************************************
43
* Function Name  : PowerOn
275 killagreg 44
* Description    : USB IP power ON Routine.
1 ingob 45
* Input          : None.
46
* Output         : None.
47
* Return         : USB_SUCCESS.
48
*******************************************************************************/
49
RESULT PowerOn(void)
50
{
275 killagreg 51
  u16 wRegVal;
1 ingob 52
 
275 killagreg 53
  /*** cable plugged-in ? ***/
54
  USB_CableConfig(ENABLE);
1 ingob 55
 
275 killagreg 56
  /*** CNTR_PWDN = 0 ***/
57
  wRegVal = CNTR_FRES;
58
  _SetCNTR(wRegVal);
1 ingob 59
 
275 killagreg 60
  /*** CNTR_FRES = 0 ***/
61
  wInterrupt_Mask = 0;
62
  _SetCNTR(wInterrupt_Mask);
63
  /*** Clear pending interrupts ***/
64
  _SetISTR(0);
65
  /*** Set interrupt mask ***/
66
  wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM;
67
  _SetCNTR(wInterrupt_Mask);
68
 
69
  return USB_SUCCESS;
1 ingob 70
}
275 killagreg 71
 
1 ingob 72
/*******************************************************************************
73
* Function Name  : PowerOff
74
* Description    : handles switch-off conditions
75
* Input          : None.
76
* Output         : None.
77
* Return         : USB_SUCCESS.
78
*******************************************************************************/
79
RESULT PowerOff()
80
{
275 killagreg 81
  /* disable all ints and force USB reset */
82
  _SetCNTR(CNTR_FRES);
83
  /* clear interrupt status register */
84
  _SetISTR(0);
85
  /* Disable the Pull-Up*/
86
  USB_CableConfig(DISABLE);
87
  /* switch-off device */
88
  _SetCNTR(CNTR_FRES + CNTR_PDWN);
89
  /* sw variables reset */
90
  /* ... */
1 ingob 91
 
275 killagreg 92
  return USB_SUCCESS;
1 ingob 93
}
275 killagreg 94
 
1 ingob 95
/*******************************************************************************
96
* Function Name  : Suspend
97
* Description    : sets suspend mode operating conditions
98
* Input          : None.
99
* Output         : None.
100
* Return         : USB_SUCCESS.
101
*******************************************************************************/
102
void Suspend(void)
103
{
275 killagreg 104
  u16 wCNTR;
105
  /* suspend preparation */
106
  /* ... */
1 ingob 107
 
275 killagreg 108
  /* macrocell enters suspend mode */
109
  wCNTR = _GetCNTR();
110
  wCNTR |= CNTR_FSUSP;
111
  _SetCNTR(wCNTR);
1 ingob 112
 
275 killagreg 113
  /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
114
  /* power reduction */
115
  /* ... on connected devices */
1 ingob 116
 
117
 
275 killagreg 118
  /* force low-power mode in the macrocell */
119
  wCNTR = _GetCNTR();
120
  wCNTR |= CNTR_LPMODE;
121
  _SetCNTR(wCNTR);
1 ingob 122
 
275 killagreg 123
  /* switch-off the clocks */
124
  /* ... */
125
  USB_EnterLowPowerMode();
126
}
1 ingob 127
 
128
/*******************************************************************************
129
* Function Name  : Resume_Init
130
* Description    : Handles wake-up restoring normal operations
131
* Input          : None.
132
* Output         : None.
133
* Return         : USB_SUCCESS.
134
*******************************************************************************/
135
void Resume_Init(void)
136
{
137
  u16 wCNTR;
275 killagreg 138
  /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
139
  /* restart the clocks */
140
  /* ...  */
1 ingob 141
 
275 killagreg 142
  /* CNTR_LPMODE = 0 */
143
  wCNTR = _GetCNTR();
144
  wCNTR &= (~CNTR_LPMODE);
145
  _SetCNTR(wCNTR);
1 ingob 146
 
275 killagreg 147
  /* restore full power */
148
  /* ... on connected devices */
149
  USB_LeaveLowPowerMode();
1 ingob 150
 
275 killagreg 151
  /* reset FSUSP bit */
152
  _SetCNTR(IMR_MSK);
1 ingob 153
 
275 killagreg 154
  /* reverse suspend preparation */
155
  /* ... */
156
}
1 ingob 157
 
158
/*******************************************************************************
159
* Function Name  : Resume
160
* Description    : This is the state machine handling resume operations and
161
*                 timing sequence. The control is based on the Resume structure
162
*                 variables and on the ESOF interrupt calling this subroutine
163
*                 without changing machine state.
164
* Input          : a state machine value (RESUME_STATE)
165
*                  RESUME_ESOF doesn't change ResumeS.eState allowing
166
*                  decrementing of the ESOF counter in different states.
167
* Output         : None.
168
* Return         : None.
169
*******************************************************************************/
170
void Resume(RESUME_STATE eResumeSetVal)
171
{
275 killagreg 172
  u16 wCNTR;
1 ingob 173
 
275 killagreg 174
  if (eResumeSetVal != RESUME_ESOF)
175
    ResumeS.eState = eResumeSetVal;
1 ingob 176
 
275 killagreg 177
  switch (ResumeS.eState)
178
  {
179
    case RESUME_EXTERNAL:
180
      Resume_Init();
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
  }
1 ingob 219
}
275 killagreg 220
 
221
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/