⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb_pwr.c

📁 意法半導體STR710,USB範例程式,模擬U盤
💻 C
字号:
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
PROJECT  : ARM USB: Tutorial
MODULE  :  USB_pwr.c
AUTHOR :  MCD Application Team
CREATION DATE : 21/04/2004
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
DESCRIPTION:	connection/disconnection & power management
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/

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

/*  .3.2 2..2 2..2 1..1 1..1 1..0 0..0 0..0 */
/*  1098 7654 3210 9876 5432 1098 7654 3210 */
/*    .T SRNM KHGF EDCB A				     leds digit1 */
/*   .                   TSR NMKH GFED CBA   leds digit2 */
/*  0000 0000 0001 1111 1000 0001 1111 1100  AD = 0x001F81FC */
/*  0000 1001 0000 0100 0000 0001 1111 1100  AT = 0x090401FC */
/*  0000 0000 0110 0111 1000 0000 0111 1110  DE = 0x0067807E */
/*  0000 0000 0001 1111 1000 0001 1011 1100  PO = 0x001F81BC */
#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=USB_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;


	/*** 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 */
	/* ... */
	/* fpga clock off */
//Slim to add code
    
	return SUCCESS;
} /* PowerOff */
/*==========================================================================*/
/* Suspend() */
/* sets suspend mode operating conditions */
/* INPUT : */
/* OUTPUT: */
/*==========================================================================*/
void Suspend(void)
{
 WORD wCNTR;
    fCellSuspended= USB_TRUE;
	/* suspend preparation */
	/* ... */
	
	/* macrocell enters suspend mode */
    wCNTR = _GetCNTR();
    wCNTR |= CNTR_FSUSP; 
    _SetCNTR(wCNTR);

/* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */    
	/* power reduction */
	/* ... on connected devices */
	/* set FPGA clock to 1 MHz */
//Slim to add code

    /* force low-power mode in the macrocell */
    wCNTR = _GetCNTR();
    wCNTR |= CNTR_LPMODE; 
    _SetCNTR(wCNTR);
    
    /* switch-off the clocks */
    /* ... */
    
/* ------------------ ONLY WITH ....                ---------------------- */    

}/* Suspend */

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

    fCellSuspended= USB_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 */
	/* restore FPGA 48 MHz clock	*/
//Slim to add code
    
/* ------------------ ONLY WITH ....                ---------------------- */    

   	/* 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() */
/*==========================================================================*/
/* DisplayDevState() */
/* writes on the alphanumeric display the state of the USB device */
/*==========================================================================*/
void DisplayDevState(void)
{
	if(fCellSuspended)
	{
		WriteAlphaDisp(SU_DISP); /* display = "SU" */
 	}
 	else
 	{		
	  switch(bDeviceState)
	  {
 		case UNCONNECTED:
 			WriteAlphaDisp(UNCONNECTED); /* display = ".." */
			break;
		case ATTACHED:
			WriteAlphaDisp(AT_DISP); /* display = "AT" */
 			break;
		case POWERED:
			WriteAlphaDisp(PO_DISP); /* display = "PO" */
 			break;
		case DEFAULT:
			WriteAlphaDisp(DE_DISP); /* display = "DE" */
 			break;
		case ADDRESSED:
			dwLedVal = 0; /* prepare for next state */		
			WriteAlphaDisp(AD_DISP); /* display = "AD" */
 			break;
		case CONFIGURED:
			WriteAlphaDisp(dwLedVal++); /* rotating segments */
 			break;
 		default:	
 			break;
 	  }		
	}
} /* DisplayDevState */
/*==========================================================================*/
/* WriteAlphaDisp() */
/* writes alphanumeric display */
/*==========================================================================*/
void WriteAlphaDisp(DWORD dwLedVal)
{
	/* Put user defined main loop procedures here */

} /* WriteAlphaDisp */

⌨️ 快捷键说明

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