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

📄 usb.c

📁 USBN9603的DEMO程序
💻 C
📖 第 1 页 / 共 3 页
字号:
/*----------------------------------------------------------------------------
 *  Copyright (c) 2001 by National Semiconductor Corporation
 *  National Semiconductor Corporation
 *
 *  All rights reserved
 *
 *<<<-------------------------------------------------------------------------
 * File Contents:
 *	usb.c - USB related functions
 *
 *  Project: USB Demo firmware
 *  Author : Yan Nosovitsky	
 *  Date   : Oct 2001
 *----------------------------------------------------------------------->>>*/
 
#include "..\include\all.h"

////////////////////////////////////////////////////
///				Externs
////////////////////////////////////////////////////

extern const int usbn9604_tx_endpoint_addr[];
extern const int usbn9604_rx_endpoint_addr[];
extern const int fifo_sizes[];


////////////////////////////////////////////////////
///				Globals
////////////////////////////////////////////////////


//Data buffers
extern control_buffer_t control_send_buffer;
extern control_buffer_t control_receive_buffer;

extern void Bulk_Parser(USB_request_t *req);
extern void Iso_Parser(USB_request_t *req);
extern void Gen_Parser(USB_request_t *req);
extern void Interrupt_Parser(USB_request_t *req);

/* The USB device status */
DEVICE_status_t device_status;
Device_buffers_t device_buffers;
USB_Device_ID_t usb_device_id;

////////////////////////////////////////////////////
///				Prototypes
////////////////////////////////////////////////////
void usb_dev_enable_ep(const USB_endpoint_desc_t *ep);
void usb_dev_disable_ep(const USB_endpoint_desc_t *ep);
byte get_deep_switch_settings() { return GET_DIP_SW1()&0x3f; }
void set_usb_device_id_value() 
{ usb_device_id.USB_Device_ID_Value = GET_DIP_SW1()&0x3f; }

/*----------------------------------------------------------------------------------------------
*	Prototype
*		void USBN9604_init(void)
*
*	Parameters
*		None
*
*	Returns
*		None
*
*	Description
*		Initializes the USBN9604
----------------------------------------------------------------------------------------------*/

void USBN9604_init(void)
{
	volatile unsigned int i;
	/*-----------------------------------------------------------------------
	* Set the alternate function pins to interrupt[iopd1] from USB INTR pin 
	*-----------------------------------------------------------------------*/
    PDALT |= 0x06;
    /*--------------
    * Hardware reset
    *---------------*/
	/*---------------------------------------
	 * Enable pull-up at port E 
	 *  This port alternate function is GPIO 
	 *--------------------------------------*/
	PEALT &= ~0x40;
	//for (i = 0; i < 0xffff; i++); //Give the USB some time to reset itself
	/*---------------------------------
	* Enable the IOPE6 pull-up in order 
	* to output the USB from reset 
	*---------------------------------*/
	PEWPU = 0x40;
	/*-----------------------------------
	 * USB node should have 2^14 cycles 
	 * of idle run after reset	
	 *---------------------------------*/
	for (i = 0; i < 0x4000; i++); 
	
	/*------------------------------------------------
	 * check if chip stabilized if not reset the chip
	 *------------------------------------------------*/
	for (i = 0; i < 0xffff; i++){
		write_usb(CCONF, i % 0x10);
		if (read_usb(CCONF) != (i % 0x10))
			WATCHDOG_RESET;
	}
    /*--------------------------------------------------------------
	 * Initialize the clock generator as input for the SCANPSC100F
     * prior to this point, the clock output will be 4 Mhz.  After,  
     * it will be (48 MHz/CLKDIV)
     *-------------------------------------------------------------*/ 
	if (GET_DIP_SW1() & 0x80)
		write_usb(CCONF, CLKDIV_SLOW-1);
	else 
		write_usb(CCONF, CLKDIV_FAST-1);

	write_7seg(read_usb(CCONF));

    /*---------------------------------------------------------------
	 * Give a software reset, then set ints to active high push pull 
	 *--------------------------------------------------------------*/
    write_usb(MCNTRL, SRST);	

    /*------------------------------------
    * Wait for end of the initiated reset 
    *------------------------------------*/
    while(read_usb(MCNTRL) & SRST);

    /* ----------------------------------------------------
	 * Set Rising Edge interrupt type and internal voltage 
	 *----------------------------------------------------*/
    write_usb(MCNTRL, INT_H_P | VGE);

    /*-------------------------
    * mask all USB node events
    *------------------------*/
	DISABLE_NODE_INTS


	/*-----------------------
	* Set up interrupt masks 
	*-----------------------*/
	/*----------------------
	 * NAK OUT FIFO 0 evnt 
	 *----------------------*/ 
    ENABLE_NAK_INTS(NAK_OUT0)			
	/*-------------------
	* enable TX  events 
	*------------------*/
    ENABLE_TX_INTS(TX_FIFO0|TX_FIFO1|TX_FIFO2|TX_FIFO3)
    /*-----------------
    * enable RX  events 
    *-----------------*/		
    ENABLE_RX_INTS(RX_FIFO0|RX_FIFO1|RX_FIFO2|RX_FIFO3)
    /*-----------------------------
    * ALT events include DMA event
    *-----------------------------*/	
    ENABLE_ALT_INTS(ALT_SD3|ALT_RESET|ALT_DMA)		
	
	/*----------------------------
	* Enable all below interrupts 
	*----------------------------*/
    ENABLE_NODE_INTS(INTR_E|RX_EV|NAK|TX_EV|ALT)
    /*----------------------------------------------
    * Workaround for Voltage Regulator Output issue
    *-----------------------------------------------*/
	write_usb(0x1f,0x40);
   
   
    reset_usb();
	/*---------------
	* Go operational 
	*---------------*/
    GOTO_STATE(OPR_ST)				


	ATTACH_NODE
	for (i = 0; i < 0xffff; i++);

}

/*----------------------------------------------------------------------------------------------
*	Prototype
*		void reset_usb(void)
*
*	Parameters
*		None
*
*	Returns
*		None
*
*	Description
*		Reset USB
----------------------------------------------------------------------------------------------*/
void reset_usb(void)
{

	/*------------------------------------
	 * set default address for endpoint 0
	 *-----------------------------------/
	SET_EP_ADDRESS(EPC0, 0x0)
    /*----------------------------------------------
	 * Set usb default device address (FAR register)
	 *---------------------------------------------*/
	SET_USB_DEVICE_ADDRESS(0x0)
   /*-----------------------------------------
	*enable USB device address (FAR register)
	*----------------------------------------*/
	USB_DEVICE_ADDRESS_ENABLE
	
	/*---------------------------------------------------------
	 * enable responce to the default address
	 * regardless to the value of the EPC0 and FAR registers
	 *--------------------------------------------------------*/

/*	Reset all endpoints */
/*	for (i=1; i<MAX_NUM_OF_ENDPOINTS; i++) 
	{
		if (usb_dev_endpoints[i] != NULL) 
			usb_dev_disable_ep(usb_dev_endpoints[i]);
	}
*/
	FLUSHTX0 //ep0
	FLUSHTX1 //ep1
	FLUSHTX2 //ep3
	FLUSHTX3 //ep5

	FLUSHRX0 //ep0
	FLUSHRX1 //ep2
	FLUSHRX2 //ep4

    /*----------------------
    * Global initalizations 
    *----------------------*/
	clear_control_buffer(&control_send_buffer);
	clear_control_buffer(&control_receive_buffer);
	endpoint_status_init();


	/*--------------------
	* Enable the receiver 
	*-------------------*/
    ENABLE_RX0	
	
}

/*----------------------------------------------------------------------------------------------
*	Prototype
*		void usb_device_reset(void)
*
*	Parameters
*		None
*
*	Returns
*		None
*
*	Description
*		 USB device reset
----------------------------------------------------------------------------------------------*/

void usb_device_reset(void)
{
    device_status.last_req = RESERVED_REQ;
    if (DEVICE_STATE(device_status) == DEV_ADDRESS || DEVICE_STATE(device_status) == DEV_CONFIGURED)
		SET_DEVICE_STATE(device_status, DEV_ATTACHED);

	device_buffers.zero_data = 0;

}
/*====================================================================
 *			USB Control Pipe Protocol Definitions
 *====================================================================*/

/*=================================================================
 *					USB device Descriptors
 *=================================================================*/

/*------------------------------
 * Device USB device descriptor
 *-----------------------------*/
const USB_device_desc_t usb_device_desc =
{
    sizeof(USB_device_desc_t),
    DEVICE_DESCRIPTOR,
    USB_SPEC_VERSION,
    CLASS_NOT_DEFINED, 		/* CLASS_VENDOR, vendor specific */
    0,						/* Device Sub-Class */
    0,						/* Device Protocol */
    EP0_FIFO_SIZE,			/* Max Packet Size for EndPoint Zero*/
    NSC_ID_VENDOR,
    NSC_ID_PRODUCT,
    0x0100, 				/* device release number: 01.00 NSC_BCDDEVICE */
    STR_MANUFACTURER,
    STR_PRODUCT,
    0,						/* Device's serial number */
    1						/* Num of configurations */
};



/*==================================================================
 *						Endpoints Descriptors					   
 *==================================================================*/


/*-------------------------------------
* Device Long Configuration descriptor 
*------------------------------------*/
const USB_long_config_desc_t usb_dev_long_config_desc =
{
	
	/*---------------------------------
	 * Device Configuration descriptor 
	 *--------------------------------*/
	{
    sizeof(USB_config_desc_t), 		/* CONFIG_DESC_LENGTH */
    CONFIG_DESCRIPTOR,
	sizeof(USB_long_config_desc_t), /* TOTAL CONFIG_DESC_LENGTH */
    1, 								/* one interface supported */
    1, 								/* Configuration number */
    0, 								/* no descriptor string STR_PRODUCT */
    0x80, 							/* bus powered SELF_POWERED, */
    100 							/* 0.2A (200mA)	 Max power consumption */
	},

	/*-------------------------------------------------
	 * USB device interface descriptor setting 0
	 * This setting does not contains Isochronous endpoints
	 * as required by the USB standard. FW does not use this setting
	 * for demo purpose, it exists only in order to achive 
	 * standard complience.
	 *------------------------------------------------*/
	{
    sizeof(USB_interface_desc_t),
    INTERFACE_DESCRIPTOR,
    0,								/* The only interface concurrently supported by this configuration */
    0,								/* Alternate Setting */
    NUM_OF_ENDPOINTS_FOR_ALT_0, 	/* Num of endpoints of this interface excluding endpoint zero */
    CLASS_VENDOR,					/* Vendor specific */
    0,								/* Sub class */
    CLASS_VENDOR,					/* Vendor Specific Interface Protocol */
    0
	},

	{
	/*------------------------------------------------
	* The IN endpoint 1 is used for bulk data transfer 
	*-----------------------------------------------*/
	{
    sizeof(USB_endpoint_desc_t),
    ENDPOINT_DESCRIPTOR,
    {
	ENDPOINT_1,
	0,
	IN
    },
    BULK_ENDPOINT,
    TX_BULK_EP_FIFO_SIZE,			/* Max Packet Size */
    0								/* Irrelevant */
	},
	
	/*-------------------------------------------------
	* The OUT endpoint 2 is used for bulk data transfer 
	*------------------------------------------------*/
	{
    sizeof(USB_endpoint_desc_t),
    ENDPOINT_DESCRIPTOR,
    {
	ENDPOINT_2,
	0,
	OUT
    },
    BULK_ENDPOINT,
    RX_BULK_EP_FIFO_SIZE,			/* Max Packet Size */
    0								/* Irrelevant */
	},
		
	/*-----------------------------------------------------
	* The IN endpoint 5 is used for interrupt data transfer 
	*-----------------------------------------------------*/
	{
    sizeof(USB_endpoint_desc_t),
    ENDPOINT_DESCRIPTOR,
    {
	ENDPOINT_5,
	0,
	IN
    },
    INTERRUPT_ENDPOINT,
    TX_INTR_EP_FIFO_SIZE,			/* Max Packet Size */
    0x1								/* Interrupt Interval, 1ms */
	}
	},
	/*-----------------------------------------
	 * USB device interface descriptor setting 1.
	 * This setting is used for the demo and contains
	 * all the endpoints (including Isochronus).
	 *----------------------------------------*/
	{
    sizeof(USB_interface_desc_t),
    INTERFACE_DESCRIPTOR,
    0,								/* The only interface concurrently supported by this configuration */
    1,								/* Alternate Setting */
    NUM_OF_ENDPOINTS_FOR_ALT_1,		/* Num of endpoints of this interface excluding endpoint zero */
    CLASS_VENDOR,					/* Vendor specific */
    0,								/* Sub class */
    CLASS_VENDOR,					/* Vendor Specific Interface Protocol */
    0
	},
	{
	/*------------------------------------------------
	* The IN endpoint 1 is used for bulk data transfer 
	*-----------------------------------------------*/
	{
    sizeof(USB_endpoint_desc_t),
    ENDPOINT_DESCRIPTOR,
    {
	ENDPOINT_1,
	0,
	IN
    },
    BULK_ENDPOINT,
    TX_BULK_EP_FIFO_SIZE,			/* Max Packet Size */
    0								/* Irrelevant */
	},
	
	/*-------------------------------------------------
	* The OUT endpoint 2 is used for bulk data transfer 
	*------------------------------------------------*/
	{
    sizeof(USB_endpoint_desc_t),
    ENDPOINT_DESCRIPTOR,
    {
	ENDPOINT_2,
	0,
	OUT
    },
    BULK_ENDPOINT,
    RX_BULK_EP_FIFO_SIZE,			/* Max Packet Size */
    0								/* Irrelevant */
	},

	/*--------------------------------------------------------
	* The IN endpoint 3 is used for isochronous data transfer 
	*-------------------------------------------------------*/
	{
    sizeof(USB_endpoint_desc_t),
    ENDPOINT_DESCRIPTOR,
    {
	ENDPOINT_3,
	0,
	IN
    },
    ISOCHRONOUS_ENDPOINT,
    TX_ISO_EP_FIFO_SIZE,			/* Max Packet Size */
    1								/* Irrelevant */
	},
	
	/*--------------------------------------------------------
	* The OUT endpoint 4 is used for isochronous data transfer 
	*--------------------------------------------------------*/
	{
    sizeof(USB_endpoint_desc_t),
    ENDPOINT_DESCRIPTOR,
    {
	ENDPOINT_4,
	0,
	OUT
    },
    ISOCHRONOUS_ENDPOINT,
    RX_ISO_EP_FIFO_SIZE,			/* Max Packet Size */
    1								/* Irrelevant */
	},

	/*-----------------------------------------------------
	* The IN endpoint 5 is used for interrupt data transfer 
	*-----------------------------------------------------*/
	{
    sizeof(USB_endpoint_desc_t),
    ENDPOINT_DESCRIPTOR,
    {
	ENDPOINT_5,
	0,
	IN
    },
    INTERRUPT_ENDPOINT,
    TX_INTR_EP_FIFO_SIZE,			/* Max Packet Size */
    0x1								/* Interrupt Interval, 1ms */
	}
	}
};

/*----------------------------
* List of endpoint descriptors 
*---------------------------*/
const USB_endpoint_desc_t *usb_dev_endpoints[2][7] =
{
{
    NULL,															/* Endpoint 0 */
    &usb_dev_long_config_desc.usb_dev_endpoint_alt_0_desc[0],		/* Endpoint 1 */
    &usb_dev_long_config_desc.usb_dev_endpoint_alt_0_desc[1],		/* Endpoint 2 */
    NULL,															/* Endpoint 3 */
    NULL,															/* Endpoint 4 */
    &usb_dev_long_config_desc.usb_dev_endpoint_alt_0_desc[2],		/* Endpoint 5 */
    NULL 															/* Endpoint 6 */
},
{
    NULL,															/* Endpoint 0 */
    &usb_dev_long_config_desc.usb_dev_endpoint_alt_1_desc[0],		/* Endpoint 1 */
    &usb_dev_long_config_desc.usb_dev_endpoint_alt_1_desc[1],		/* Endpoint 2 */
    &usb_dev_long_config_desc.usb_dev_endpoint_alt_1_desc[2],		/* Endpoint 3 */
    &usb_dev_long_config_desc.usb_dev_endpoint_alt_1_desc[3],		/* Endpoint 4 */
    &usb_dev_long_config_desc.usb_dev_endpoint_alt_1_desc[4],		/* Endpoint 5 */
    NULL 															/* Endpoint 6 */
}
};


/*=================================================================*
 *					Vendor Device Descriptor
 *=================================================================*/


USB_Device_ID_t usb_device_id =
{
	0x0,			/* default usb device id*/
	HARDWARE_USB_ID	/* default usb device id source */
};
	

⌨️ 快捷键说明

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