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

📄 init.c

📁 Mitsubishi M30245 SampleCode
💻 C
字号:
/*******************************************************************
 *
 *    DESCRIPTION: M30245 Mit_USB Demonstration Application
 *
 *    AUTHOR: Mitsubishi DEC-E
 *
 *    HISTORY: 0.5 5/30/02 Initial creation for NC30
 *			   1.0 6/18/02 First official release
 *			   1.1 6/27/02 Updated ISO routines
 *			   1.2 8/07/02 Updated for Rev B Starter Kit Board
 *
 *******************************************************************/

/** include files **/

#include "sfr245.h"
#include "extern.h"

// Function Protorypes
void StartPll(void);
void USBCoreStart(void);
void RegisterInit(void);
void TimerA0Init(void);
void TimerA1Init(void);
void InitAD(void);
void InitUART3(void);
void USBEndpointInit(void);

/*
 ** RegisterInit
 *
 *  FILENAME: init.c
 *
 *  PARAMETERS: None
 *
 *  DESCRIPTION: Loads default values into specific registers for startup
 *				 mainly used for Vbus detect to provide signal that the 
 *				 device is ready to run USB operations.
 *
 *  RETURNS: Nothing
 *
 */
void RegisterInit(void){

	p7 = 0xff;			// Setup P7 so that LED's on board can be used
	pd7 = 0xff;

	prcr = 0x03;		// Disable protection of the clock & PLL registers
	cm0 = 0x08;			// Enable CM16 & CM17 for high speed mode
	cm1 = 0x00;			// BCLK/1. Should = 16MHz
	prcr = 0x00;		// Enable protection of the clock & PLL registers

	pu20 = 1;			// Enable pullups on P8_0 to P8_4
	pu25 = 1;			// Enable pullups on P10_4-P10_7

	usbad = 0x81;		// Enable P90 for D+ pullup and enable Vbus detect
	ir_vbsic = 0;		// Clear the Vbus detect interrupt (per spec)
	vbsic = 0x04;		// Enable the Vbus detect interrupt (level 4)

	// Really crude way to initialize structs...change in next rev...
	DataTransferFlags.NewEP2Data = 0;
	DataTransferFlags.BulkDataReceived = 0;
	DataTransferFlags.SendBulkData = 0;
	DataTransferFlags.EP3INReady = 0;
	DataTransferFlags.EP1DataTransmit = 0;
	DataTransferFlags.EP3DataReceived = 0;

	USBFlags.EP1INStallStatus = 0;
	USBFlags.EP2INStallStatus = 0;
	USBFlags.EP3INStallStatus = 0;
	USBFlags.EP1OUTStallStatus = 0;
	USBFlags.EP3OUTStallStatus = 0;
	USBFlags.Attached = 0;
	USBFlags.Powered = 0;
	USBFlags.Default = 0;
	USBFlags.Addressed = 0;
	USBFlags.Configured = 0;
	USBFlags.InitUSB = 0;
	USBFlags.SetupPacketReceived = 0;
	USBFlags.SetupEndFlag = 0;
	USBFlags.SendBulkData = 0;
	USBFlags.USBFirstStart = 0;
}

/*
 ** TimerA1Init
 *
 *  FILENAME: init.c 
 *
 *  PARAMETERS: None
 *
 *  DESCRIPTION: This timer is used to provide the source to sample
 *				 the A/D and switches on the SK board. It polls these
 *				 values every 10mS.
 *
 *  RETURNS: Nothing
 *
 */
void TimerA1Init(void){

	ta1mr = 0x80;		// Place Timer A1 in timer mode
	ta1 = 0x1388;		// Timer value 10mS

	ir_ta1ic = 0;		// Clear out interrupt request bit
	ta1ic = 0x02;		// Set the interrupt request to level 2

	ta1s = 1;			// Start the timer here
}	

/*
 ** InitAD
 *
 *  FILENAME: init.c 
 *
 *  PARAMETERS: None
 *
 *  DESCRIPTION: Initalizes the onboard A/D converter to sample the 
 *				 light sensor (CDS cell).
 *
 *  RETURNS: Nothing
 *
 */
void InitAD(void){

	adcon0 = 0x00;		// initialize A/D input AD0 selected 
	adcon1 = 0x20;		// set up 8 bit conversion & Vref connected
	adcon2 = 0x01;		// sample and hold enabled 
}

/*
 ** InitUART3
 *
 *  FILENAME: init.c
 *
 *  PARAMETERS: None  
 *
 *  DESCRIPTION: Sets up UART3 as the output for outgoing ISO data from the
 *				 HOST. Can be viewed on hyperterminal using 9600, 8, N, 1 
 *	
 *  RETURNS: Nothing
 *
 */
void InitUART3(void){
const char text[] = "<< ISO streaming test >>\n\r   Mouse Move Test\n\r-------------------------\n\r";
int i = 0x00;

  	u3mr = 0x05;		/* set mode register */
  	u3c0 = 0x18; 		/* set control register	*/
	u3brg = 0x67;		/* set bit rate generator */
						/*  (16Mhz/16/9600)-1 */
  	u3tb = 0;			/* clear transmit buffer */
	u3irs = 1;			// enable transmit interrupt.....
	te_u3 = 1;			/* Enable UART3 transmit */
	re_u3 = 1; 			/* Enable UART3 receive	*/
	s3tic = 0x02;		/* Enable UART3 receive interrupt, */
						/*  priority level 4 */
	u3tbl = 0x30;							

	/* Send out an opening message */
	u3tbl = 0x0d;			 // The following sends text at the beginning 
	while(ti_u3 == 0);	 //  of the program to hyperterminal
	u3tbl = 0x0a;
	for (i=0; i<=72; i++){	 // This loop reads in the text string and
		while(ti_u3 == 0); //  puts it in the transmit buffer
		u3tbl = text[i];
	}

	/* clear out the display buffer */
	/* a zero will be used to tell where the end of the data is */
	/* in the ring buffer */
	for( i = 0; i < 256; i++)
		disp_buffer[i] = 0;
}

/*
 ** StartPll
 *
 *  FILENAME: init.c
 *
 *  PARAMETERS: None
 *
 *  DESCRIPTION: Starts the onboard PLL for the USB core's use. 
 *
 *  RETURNS: Nothing
 *
 */
void StartPll(void){
unsigned char DelayOne = 0xff;		// Temporary delay variables
unsigned char DelayTwo = 0xff;

	prcr = 0x03;			// Disable protection of the clock & PLL registers

	fsp = 0x01;				// Xin = 16MHz -> Fpin = 4MHz
	fsm = 0x05;				// Fpin = 4MHz -> Fvco/Fusb = 48MHz
	fsd = 0x01;				// Fvco = 48MHz -> Fsyn = 12MHz (if necessary)
	fsc = 0x43;				// debug....

	while ( DelayTwo != 0 ){			// Simple DO->WHILE to count while the 
		while ( DelayOne != 0 ){		// PLL comes online and stabilizes
			DelayOne--;					// These values should give us a delay of
		}								// Approxiamtely 3mS or so before we check

		DelayOne = 0xff;				// Reload small delay		
		DelayTwo--;						// and one tick off large delay
	}

	while ( !ls ){						// Check to see if PLL is locked in 
		DelayOne = 0x32;				// if not wait 0.1 mS and check again
		DelayTwo = 0x0b;		 

		while ( DelayTwo != 0){			// Simple DO->WHILE to count while the 
			while ( DelayOne != 0 ){	 
				DelayOne--;				// These values should give us a delay of	 
			}					 		// Approxiamtely 0.1 mS or before we check	 
			DelayOne = 0xff;			// Reload small delay				 
			DelayTwo--;					// and one tick off large delay 
		}						 
	}
}

/*
 ** USBCoreStart
 *
 *  FILENAME: init.c
 *
 *  PARAMETERS: None
 *
 *  DESCRIPTION: Initalizes the USB core.
 *
 *  RETURNS: Nothing
 *
 */
void USBCoreStart(void){
unsigned char DelayOne = 0x05;		// Temporary delay variables

	usbc = 0x20;					// Enable USB core clock
 	usbc = 0xe0;					// Enable USB core clock & SOF output
	
	for ( ; DelayOne; DelayOne--){	// Delay to allow USB core stabilization
		asm("NOP");
	}
}

/*
 ** USBEndpointInit
 *
 *  FILENAME: init.c 
 *
 *  PARAMETERS: None
 *
 *  DESCRIPTION: Sets default values for EP0 and enables the various interrupts
 *				 needed for USB operations. Note that SUSPEND is not enabled here
 *				 as stopping the processor would cause problems for the USB
 *				 monitor.
 *
 *  RETURNS: Nothing
 *
 */
void USBEndpointInit(void){

	ep0mp = 0x0008;			// EP0 MAXP packet size = 64, continuous mode
	usbie = 0x0000;

//	ir_suspic = 0;			// ensure that the USB SUSPEND interrupt is cleared

//	suspic = 0x03;			// Enable the SUSPEND interrupt

	asm("NOP");
	asm("NOP");

	ir_rsmic = 0;			// ensure that the USB RESUME interrupt is cleared

	rsmic = 0x03;			// Enable the USB RESUME interrupt

	asm("NOP");
	asm("NOP");

	ir_rstic = 0;			// ensure that USB RESET interrupt is cleared

	rstic = 0x03;			// Enable the RESET interrupt (level 3)	

	asm("NOP");
	asm("NOP");

	ir_ep0ic = 0;			// ensure that EP0 interrupt is cleared

	ep0ic = 0x03;			// Enable the EP0 interrupt (level 3)	

	asm("NOP");
	asm("NOP");

	ir_usbfic = 0;			// ensure that USB function interrupt is cleared

	usbfic = 0x07;			// Enable the USB function interrupt (level 3)	
	
	asm("NOP");
	asm("NOP");

	ir_sofic = 0;			// Ensure that the USB SOF interrupt is cleared

	sofic = 0x03;			// Enable the USB SOF interrupt (level 3)

}

⌨️ 快捷键说明

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