usb_pwr.c

来自「这是一个ARM7的程序」· C语言 代码 · 共 268 行

C
268
字号
/******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
* File Name          : usb_pwr.c
* Author             : MCD Application Team
* Date First Issued  : 27/10/2003
* Description        : connection/disconnection & power management
*
********************************************************************************/

#include "71x_lib.h"
#include "USB_lib.h"
#include "USB_conf.h"
#include "USB_pwr.h"


#define AD_DISP (0x001F81FC)
#define DE_DISP (0x0067807E)
#define PO_DISP (0x001F81BC)
#define AT_DISP (0x090401FC)
#define SU_DISP (0x001B81DA)

volatile BOOL fCellSuspended;
volatile BYTE bDeviceState=UNCONNECTED; /* USB device status */
DWORD dwLedVal; /* alpha-display value */
volatile BOOL fSuspendEnabled=TRUE;  /* true when suspend is possible */

struct {
	volatile RESUME_STATE eState;
	volatile BYTE bESOFcnt;
} ResumeS;


/*==========================================================================*/
/* PowerOn() */
/* handles switch-on conditions */
/* INPUT : */
/* OUTPUT: */
/*==========================================================================*/
RESULT PowerOn()
{
 WORD wRegVal;

	/*** cable plugged-in ? ***/
	/*
		while(!CablePluggedIn());
	*/
	/*** CNTR_PWDN = 0 ***/
	wRegVal = CNTR_FRES;
	_SetCNTR(wRegVal);

	/*** CNTR_FRES = 0 ***/
    wInterrupt_Mask = 0;
    _SetCNTR(wInterrupt_Mask);
	/*** Clear pending interrupts ***/
    _SetISTR(0);
	/*** Set interrupt mask ***/
    wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM;
    _SetCNTR(wInterrupt_Mask);

	return SUCCESS;
} /* PowerOn */

/*==========================================================================*/
/* PowerOff() */
/* handles switch-off conditions */
/* INPUT : */
/* OUTPUT: */
/*==========================================================================*/
RESULT PowerOff()
{
	/* disable all ints and force USB reset */
    _SetCNTR(CNTR_FRES);
    /* clear interrupt status register */
    _SetISTR(0);

    /* switch-off device */
    _SetCNTR(CNTR_FRES+CNTR_PDWN);
    /* sw variables reset */
	/* ... */

	return SUCCESS;
} /* PowerOff */
/*==========================================================================*/
/* Suspend() */
/* sets suspend mode operating conditions */
/* INPUT : */
/* OUTPUT: */
/*==========================================================================*/
void Suspend(void)
{
 WORD wCNTR;
    fCellSuspended= TRUE;
	/* suspend preparation */
	/* ... */

	/* macrocell enters suspend mode */
    wCNTR = _GetCNTR();
    wCNTR |= CNTR_FSUSP;
    _SetCNTR(wCNTR);

/* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
	/* power reduction */
	/* ... on connected devices */

    /* force low-power mode in the macrocell */
    wCNTR = _GetCNTR();
    wCNTR |= CNTR_LPMODE;
    _SetCNTR(wCNTR);

    /* switch-off the clocks */
    /* ... */
    GPIO_BitWrite (GPIO0,0,1);	// Set P0.0 (ON)
    GPIO_BitWrite (GPIO0,1,0);	// Set P0.1 (OFF)


    //if (VRstate == DISABLE)
    //    {
    //	PCU->PWRCTRL |=0x8000;    	// disable MVR during STOP mode
    //	PCU->PWRCTRL |=0x0010;
    //    }
        //if (Flashstate == DISABLE)      
        FLASHR->CR0 |=0x8000;    // stop the flash during stop mode
        EIC_IRQConfig(ENABLE);    	//Enable the IRQ

       // XTI->CTRL |=0x03;         	//set bit ID1St and WKUP-int bits in XTI_CTRL register
       // XTI->CTRL &=0x03;        	//Reset the STOP bit in XTI_CTRL register
	//RCCU->CFR &=0xBFFF;  	//Reset the STOP_I bit in the CLK_FLAG register
        
      // Write '1' to Stop Bit
      //	XTI->CTRL |= 0x04;
      // Write '0' to Stop Bit
      //	XTI->CTRL &= 0x03;
      // Write '1' to Stop Bit
      /*	XTI->CTRL |= 0x04; 
        __asm
      {
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
      } */

}/* Suspend */

/*==========================================================================*/
/* Resume_Init() */
/* handles wake-up restoring normal operations */
/* INPUT : */
/* OUTPUT: */
/*==========================================================================*/
void Resume_Init(void)
{
  WORD i;
  WORD wCNTR;

    fCellSuspended= FALSE;

/* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
    /* restart the clocks */
    /* ...  */

    /* CNTR_LPMODE = 0 */
    wCNTR = _GetCNTR();
    wCNTR &= (~CNTR_LPMODE);
    _SetCNTR(wCNTR);

	/* restore full power */
	/* ... on connected devices */

     
       RCCU_PLL1Config ( RCCU_PLL1_Mul_16 , RCCU_Div_2 ) ;
  
     //RCCU_PLL1Config ( RCCU_Mul_16 , RCCU_Div_1 ) ;
      //  GPIO_WordWrite (GPIO0, 0x0000);	// Set P0.0-P0.3 (ON)
       GPIO_BitWrite (GPIO0,0,0);	// Set P0.0 (OFF)
       GPIO_BitWrite (GPIO0,1,0);	// Set P0.1 (ON)


      // Wait PLL to lock
      //while(RCCU_FlagStatus(RCCU_PLL1_LOCK)==RESET);

      // for ( i = 0x7f ; i> 0; i--)

      // Select PLL1_Output as RCLK clock
         RCCU_RCLKSourceConfig ( RCCU_PLL1_Output ) ;


   	/* reset FSUSP bit */
   	_SetCNTR(IMR_MSK);

	/* reverse suspend preparation */
	/* ... */

}/* Resume_Init() */

/*==========================================================================*/
/* Resume() */
/* This is the state machine handling resume operations and timing sequence. */
/* The control is based on the ResumeS structure variables and on the ESOF */
/* interrupt calling this subroutine without changing machine state.*/
/* */
/* IN  : a state machine value (RESUME_STATE) */
/*       RESUME_ESOF does'nt change ResumeS.eState */
/*		  allowing decrementing of the ESOF counter in different states */
/* OUT : none */
/*==========================================================================*/
void Resume(RESUME_STATE eResumeSetVal)
{
 WORD wCNTR;
 WORD wRegVal;

	if(eResumeSetVal != RESUME_ESOF)
			ResumeS.eState = eResumeSetVal;

	switch(ResumeS.eState)
	{
		case RESUME_EXTERNAL:
			Resume_Init();
	        wRegVal = GetFNR();
    		/*if(wRegVal & FNR_RXDP) //10 & 11 false conditions 
     		{
    	     		Suspend();
     		}*/
		    ResumeS.eState = RESUME_OFF;
			break;
		case RESUME_INTERNAL:
			Resume_Init();
		    ResumeS.eState = RESUME_START;
			break;
		case RESUME_LATER:
			ResumeS.bESOFcnt = 2;
		    ResumeS.eState = RESUME_WAIT;
			break;
		case RESUME_WAIT:
			ResumeS.bESOFcnt--;
			if(ResumeS.bESOFcnt == 0)
			    ResumeS.eState = RESUME_START;
			break;
		case RESUME_START:
		    wCNTR = _GetCNTR();
		    wCNTR |= CNTR_RESUME;
		    _SetCNTR(wCNTR);
		    ResumeS.eState = RESUME_ON;
		    ResumeS.bESOFcnt = 10;
			break;
		case RESUME_ON:
			ResumeS.bESOFcnt--;
			if(ResumeS.bESOFcnt == 0)
			{
			    wCNTR = _GetCNTR();
			    wCNTR &= (~CNTR_RESUME);
			    _SetCNTR(wCNTR);
			    ResumeS.eState = RESUME_OFF;
			}
			break;
		case RESUME_OFF:
		case RESUME_ESOF:
		default:
		    ResumeS.eState = RESUME_OFF;
			break;
	}
}/* Resume() */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?