Sergey Pastor / grbl1
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usb_pwr.c Source File

usb_pwr.c

00001 /******************** (C) COPYRIGHT 2011 STMicroelectronics ********************
00002 * File Name          : usb_pwr.c
00003 * Author             : MCD Application Team
00004 * Version            : V3.3.0
00005 * Date               : 21-March-2011
00006 * Description        : Connection/disconnection & power management
00007 ********************************************************************************
00008 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00009 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
00010 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
00011 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
00012 * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
00013 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00014 *******************************************************************************/
00015 
00016 /* Includes ------------------------------------------------------------------*/
00017 #ifdef STM32L1XX_MD
00018  #include "stm32l1xx.h"
00019 #else
00020  #include "stm32f10x.h"
00021 #endif /* STM32L1XX_MD */
00022  
00023 #include "usb_lib.h"
00024 #include "usb_conf.h"
00025 #include "usb_pwr.h"
00026 #include "hw_config.h"
00027 
00028 /* Private typedef -----------------------------------------------------------*/
00029 /* Private define ------------------------------------------------------------*/
00030 /* Private macro -------------------------------------------------------------*/
00031 /* Private variables ---------------------------------------------------------*/
00032 __IO uint32_t bDeviceState = UNCONNECTED; /* USB device status */
00033 __IO bool fSuspendEnabled = TRUE;  /* true when suspend is possible */
00034 
00035 struct
00036 {
00037   __IO RESUME_STATE eState;
00038   __IO uint8_t bESOFcnt;
00039 }ResumeS;
00040 
00041 /* Extern variables ----------------------------------------------------------*/
00042 /* Private function prototypes -----------------------------------------------*/
00043 /* Extern function prototypes ------------------------------------------------*/
00044 /* Private functions ---------------------------------------------------------*/
00045 
00046 /*******************************************************************************
00047 * Function Name  : PowerOn
00048 * Description    :
00049 * Input          : None.
00050 * Output         : None.
00051 * Return         : USB_SUCCESS.
00052 *******************************************************************************/
00053 RESULT PowerOn(void)
00054 {
00055 #ifndef STM32F10X_CL
00056   uint16_t wRegVal;
00057 
00058   /*** cable plugged-in ? ***/
00059   USB_Cable_Config(ENABLE);
00060 
00061   /*** CNTR_PWDN = 0 ***/
00062   wRegVal = CNTR_FRES;
00063   _SetCNTR(wRegVal);
00064 
00065   /*** CNTR_FRES = 0 ***/
00066   wInterrupt_Mask = 0;
00067   _SetCNTR(wInterrupt_Mask);
00068   /*** Clear pending interrupts ***/
00069   _SetISTR(0);
00070   /*** Set interrupt mask ***/
00071   wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM;
00072   _SetCNTR(wInterrupt_Mask);
00073 #endif /* STM32F10X_CL */
00074 
00075   return USB_SUCCESS;
00076 }
00077 
00078 /*******************************************************************************
00079 * Function Name  : PowerOff
00080 * Description    : handles switch-off conditions
00081 * Input          : None.
00082 * Output         : None.
00083 * Return         : USB_SUCCESS.
00084 *******************************************************************************/
00085 RESULT PowerOff()
00086 {
00087 #ifndef STM32F10X_CL
00088   /* disable all interrupts and force USB reset */
00089   _SetCNTR(CNTR_FRES);
00090   /* clear interrupt status register */
00091   _SetISTR(0);
00092   /* Disable the Pull-Up*/
00093   USB_Cable_Config(DISABLE);
00094   /* switch-off device */
00095   _SetCNTR(CNTR_FRES + CNTR_PDWN);
00096 #endif /* STM32F10X_CL */
00097 
00098   /* sw variables reset */
00099   /* ... */
00100 
00101   return USB_SUCCESS;
00102 }
00103 
00104 /*******************************************************************************
00105 * Function Name  : Suspend
00106 * Description    : sets suspend mode operating conditions
00107 * Input          : None.
00108 * Output         : None.
00109 * Return         : USB_SUCCESS.
00110 *******************************************************************************/
00111 void Suspend(void)
00112 {
00113 #ifndef STM32F10X_CL
00114   uint16_t wCNTR;
00115   /* suspend preparation */
00116   /* ... */
00117 
00118   /* macrocell enters suspend mode */
00119   wCNTR = _GetCNTR();
00120   wCNTR |= CNTR_FSUSP;
00121   _SetCNTR(wCNTR);
00122 #endif /* STM32F10X_CL */
00123 
00124   /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
00125   /* power reduction */
00126   /* ... on connected devices */
00127 
00128 #ifndef STM32F10X_CL
00129   /* force low-power mode in the macrocell */
00130   wCNTR = _GetCNTR();
00131   wCNTR |= CNTR_LPMODE;
00132   _SetCNTR(wCNTR);
00133 #endif /* STM32F10X_CL */
00134 
00135   /* switch-off the clocks */
00136   /* ... */
00137   Enter_LowPowerMode();
00138 
00139 }
00140 
00141 /*******************************************************************************
00142 * Function Name  : Resume_Init
00143 * Description    : Handles wake-up restoring normal operations
00144 * Input          : None.
00145 * Output         : None.
00146 * Return         : USB_SUCCESS.
00147 *******************************************************************************/
00148 void Resume_Init(void)
00149 {
00150 #ifndef STM32F10X_CL
00151   uint16_t wCNTR;
00152 #endif /* STM32F10X_CL */ 
00153 
00154   /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
00155   /* restart the clocks */
00156   /* ...  */
00157 
00158 #ifndef STM32F10X_CL
00159   /* CNTR_LPMODE = 0 */
00160   wCNTR = _GetCNTR();
00161   wCNTR &= (~CNTR_LPMODE);
00162   _SetCNTR(wCNTR);
00163 #endif /* STM32F10X_CL */ 
00164 
00165   /* restore full power */
00166   /* ... on connected devices */
00167   Leave_LowPowerMode();
00168 
00169 #ifndef STM32F10X_CL
00170   /* reset FSUSP bit */
00171   _SetCNTR(IMR_MSK);
00172 #endif /* STM32F10X_CL */
00173 
00174   /* reverse suspend preparation */
00175   /* ... */
00176 
00177 }
00178 
00179 /*******************************************************************************
00180 * Function Name  : Resume
00181 * Description    : This is the state machine handling resume operations and
00182 *                 timing sequence. The control is based on the Resume structure
00183 *                 variables and on the ESOF interrupt calling this subroutine
00184 *                 without changing machine state.
00185 * Input          : a state machine value (RESUME_STATE)
00186 *                  RESUME_ESOF doesn't change ResumeS.eState allowing
00187 *                  decrementing of the ESOF counter in different states.
00188 * Output         : None.
00189 * Return         : None.
00190 *******************************************************************************/
00191 void Resume(RESUME_STATE eResumeSetVal)
00192 {
00193 #ifndef STM32F10X_CL
00194   uint16_t wCNTR;
00195 #endif /* STM32F10X_CL */
00196 
00197   if (eResumeSetVal != RESUME_ESOF)
00198     ResumeS.eState = eResumeSetVal;
00199 
00200   switch (ResumeS.eState)
00201   {
00202     case RESUME_EXTERNAL:
00203       Resume_Init();
00204       ResumeS.eState = RESUME_OFF;
00205       break;
00206     case RESUME_INTERNAL:
00207       Resume_Init();
00208       ResumeS.eState = RESUME_START;
00209       break;
00210     case RESUME_LATER:
00211       ResumeS.bESOFcnt = 2;
00212       ResumeS.eState = RESUME_WAIT;
00213       break;
00214     case RESUME_WAIT:
00215       ResumeS.bESOFcnt--;
00216       if (ResumeS.bESOFcnt == 0)
00217         ResumeS.eState = RESUME_START;
00218       break;
00219     case RESUME_START:
00220      #ifdef STM32F10X_CL
00221       OTGD_FS_SetRemoteWakeup();
00222      #else 
00223       wCNTR = _GetCNTR();
00224       wCNTR |= CNTR_RESUME;
00225       _SetCNTR(wCNTR);
00226      #endif /* STM32F10X_CL */
00227       ResumeS.eState = RESUME_ON;
00228       ResumeS.bESOFcnt = 10;
00229       break;
00230     case RESUME_ON:
00231     #ifndef STM32F10X_CL      
00232       ResumeS.bESOFcnt--;
00233       if (ResumeS.bESOFcnt == 0)
00234       {
00235      #endif /* STM32F10X_CL */    
00236        #ifdef STM32F10X_CL
00237         OTGD_FS_ResetRemoteWakeup();
00238        #else
00239         wCNTR = _GetCNTR();
00240         wCNTR &= (~CNTR_RESUME);
00241         _SetCNTR(wCNTR);
00242        #endif /* STM32F10X_CL */
00243         ResumeS.eState = RESUME_OFF;
00244      #ifndef STM32F10X_CL
00245       }
00246      #endif /* STM32F10X_CL */
00247       break;
00248     case RESUME_OFF:
00249     case RESUME_ESOF:
00250     default:
00251       ResumeS.eState = RESUME_OFF;
00252       break;
00253   }
00254 }
00255 
00256 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/