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

📄 usb.c

📁 usbn9603等时传输的firmware
💻 C
📖 第 1 页 / 共 3 页
字号:
	u_int desc_value = REQ_VALUE(req).descriptor.bDescriptorType; 

	if (desc_recipient == DEVICE_REQ) {
		if (desc_value == UJA_DEVICE_DESCRIPTOR) {
			//receive descriptor through the control data stage
			//fill the control_receive_buffer with the request
			//WAITING for a new data from the host with descriptor
			//enable RX0 for data stage 
			ENABLE_RX(ENDPOINT_0);
			//while()
			switch (desc_index) {
			case HARDWARE_UJA_ID:
				// Capture current setting of the dip switch
				uja_device_id.UJA_Device_ID_Value = GET_DIP_SW1()&0x3f;
				uja_device_id.UJA_Device_ID_Source = HARDWARE_UJA_ID;
				break;
			case SOFTWARE_UJA_ID:
				//Update new uja ID value according to the value received through the request 
				uja_device_id.UJA_Device_ID_Value = ((UJA_vendor_device_desc_t*)(control_receive_buffer.data))->bDeviceIdValue;
				uja_device_id.UJA_Device_ID_Source = SOFTWARE_UJA_ID;
				break;
			}
			zero_length_data_response(ENDPOINT_0);
		}else //unknown device descriptor
			STALL_EP0;
	}else //unsupported recipient
		STALL_EP0;
}
/*
void usb_vendor_dev_set_descriptor(USB_request_t *req)
{
    int desc_length, received_bytes;
    int desc_index = REQ_VALUE(req).descriptor.bDescriptorIndex;
    char *desc_buf;
	UJA_vendor_device_desc_t vendor_desc_buf;
    u_int max_desc_length = REQ_LENGTH(req);
	byte desc_recipient = REQ_RECIPIENT(req);
	u_int desc_value = REQ_VALUE(req); 

	if (REQ_RECIPIENT(req) == DEVICE_REQ) {
		if (REQ_VALUE(req).descriptor.bDescriptorType == UJA_DEVICE_DESCRIPTOR) {
			//receive descriptor itself through the data stage
			
			switch (desc_index) {
			case HARDWARE_UJA_ID:
				// Capture current setting of the dip switch
				uja_device_id.UJA_Device_ID_Value = GET_DIP_SW1()&0x3f;//get_deep_switch_settings();
				uja_device_id.UJA_Device_ID_Source = HARDWARE_UJA_ID;
				break;
			case SOFTWARE_UJA_ID:

				// FATHER IMPLEMENTATION REQUIRED
				//????????????????????????
				//receive_data(ENDPOINT_0, (char *)&vendor_desc_buf, &received_bytes);
				//memcpy((void *)&vendor_desc_buf, (void *)req->data, sizeof(UJA_vendor_device_desc_t));
				
				uja_device_id.UJA_Device_ID_Value = vendor_desc_buf.bDeviceIdValue;
				uja_device_id.UJA_Device_ID_Source = SOFTWARE_UJA_ID;
				//set_uja_device_id_value(uja_device_id.UJA_Device_ID_Value);
				break;
			}
			zero_length_data_response(ENDPOINT_0);
		}else //unknown device descriptor
			STALL_EP0;
	}else //unsupported recipient
		STALL_EP0;
}

*/

/*----------------------------------------------------------------------------
 *               usb_std_dev_set_descriptor
 *
 *  Handles the SET_DESCRIPTOR device request from the USB host.
 *  Not supported request. Stall Endpoint Zero.
 *
 *  Input: pointer to struct of USB request
 *  Output: None
 *--------------------------------------------------------------------------*/
void usb_std_dev_set_descriptor(USB_request_t *req)
{
    STALL_EP0;
}

/*----------------------------------------------------------------------------
 *               usb_dev_get_config
 *
 *  Handles the GET_CONFIGURATION device request from the USB host.
 *
 *  Input: pointer to struct of USB request
 *  Output: None
 *--------------------------------------------------------------------------*/
void usb_dev_get_config(USB_request_t *req)
{
    if (DEVICE_STATE(device_status) == DEV_ATTACHED) {
		STALL_EP0;
		return;
	}
	//wValue, wIndex, wLength must be zero. 
	//Otherwize device behavior is not specified.
	if (IS_REQ_VALUE_NOT_ZERO(req)){
		STALL_EP0;
		return;
	}
	if (IS_REQ_INDEX_NOT_ZERO(req)){
		STALL_EP0;
		return;
	}
	if (REQ_LENGTH(req) != 0x1){
		STALL_EP0;
		return;
	}
	switch (REQ_RECIPIENT(req)) {
	case DEVICE_REQ:
	    //returns configuration number as appears at the device configuration register 
		device_buffers.state = (DEVICE_STATE(device_status) == DEV_CONFIGURED)?
	    			uja_dev_long_config_desc.uja_dev_config_desc.bConfigurationValue : 0;
	    send_control_data((byte *)&device_buffers.state, sizeof(device_buffers.state));
	    break;
	case INTERFACE_REQ:
	case ENDPOINT_REQ:
	case OTHER_REQ:
	default:
	    STALL_EP0;
	    break;
    }
}

/*----------------------------------------------------------------------------
 *               usb_dev_set_config
 *
 *  Handles the SET_CONFIGURATION device request from the USB host.
 *
 *  Input: pointer to struct of USB request
 *  Output: None
 *--------------------------------------------------------------------------*/


void usb_dev_set_config(USB_request_t *req)
{
   int i;

	//wIndex, wLength must be zero. 
	//Otherwize device behavior is not specified.

	if (IS_REQ_INDEX_NOT_ZERO(req))
	{
		STALL_EP0;
		return;
	}
	if (REQ_LENGTH(req) != 0)
	{
		STALL_EP0;
		return;
	}    
	switch (REQ_RECIPIENT(req)) 
	{
	case DEVICE_REQ:
	    if (DEVICE_STATE(device_status) == DEV_ATTACHED) 
		{
			STALL_EP0;
			break;
	    }
	    if (REQ_VALUE(req).as_bytes.lsb == 0) 
		{
			/* The zero value places the device in unconfigured state */
			if (DEVICE_STATE(device_status) == DEV_CONFIGURED) 
			{
 	    		
				/* Deactivate current configuration */
				for (i=1; i<MAX_NUM_OF_ENDPOINTS; i++) 
				{
					if (uja_dev_endpoints[i] != NULL) 
					{
						usb_dev_disable_ep(uja_dev_endpoints[i]);
						write_usb(EPC_ADDR(i), 0x00); //disable endpoint
					}
				}

				SET_DEVICE_STATE(device_status, DEV_ADDRESS);
				zero_length_data_response(ENDPOINT_0);
			}
	    }
	    else if (REQ_VALUE(req).as_bytes.lsb == uja_dev_long_config_desc.uja_dev_config_desc.bConfigurationValue) 
		{
	    	/* Activate this configuration */
    		for (i=0; i<MAX_NUM_OF_ENDPOINTS; i++)
				if (uja_dev_endpoints[i] != NULL) 
				{
					usb_dev_enable_ep(uja_dev_endpoints[i]);
				}
   	    	endpoint_status_init();
			SET_DEVICE_STATE(device_status, DEV_CONFIGURED);
			zero_length_data_response(ENDPOINT_0);
	    }
		else /*wrong configuration value*/
			STALL_EP0;
    	break;
	case INTERFACE_REQ:
	case ENDPOINT_REQ:
	case OTHER_REQ:
	default:
	    /* If configuration value is unsupported STALL Enpoint Zero */
	    STALL_EP0;
	    break;
    }
}

/*----------------------------------------------------------------------------
 *               usb_dev_get_interface
 *
 *  Handles the GET_INTERFACE device request from the USB host.
 *
 *  Input: pointer to struct of USB request
 *  Output: None
 *--------------------------------------------------------------------------*/
void usb_dev_get_interface(USB_request_t *req)
{
    byte interface_no  = REQ_INDEX(req).as_bytes.lsb;
	
	if (DEVICE_STATE(device_status) != DEV_CONFIGURED) 
	{
		STALL_EP0;
		return;
	}

	if (interface_no != 0) 
	{//only single interface is supported
		STALL_EP0;
		return;
	}
	if (IS_REQ_VALUE_NOT_ZERO(req)) 
	{
		STALL_EP0;
		return;
	}
	if (REQ_LENGTH(req) != 0x1) 
	{
		STALL_EP0;
		return;
	}
	
    switch (REQ_RECIPIENT(req)) 
	{
	case INTERFACE_REQ:
		device_buffers.state = uja_dev_long_config_desc.uja_interface_desc.bAlternateSetting;
	    send_control_data((byte *)&device_buffers.state, sizeof(device_buffers.state));
	    break;
	    /* If Interface value is unsupported STALL Enpoint Zero */
	case DEVICE_REQ:
	case ENDPOINT_REQ:
	case OTHER_REQ:
	default:
	    STALL_EP0;
	    break;
    }
}


/*----------------------------------------------------------------------------
 *               usb_dev_set_interface
 *
 *  Handles the SET_INTERFACE device request from the USB host.
 *
 *  Input: pointer to struct of USB request
 *  Output: None
 *--------------------------------------------------------------------------*/
void usb_dev_set_interface(USB_request_t *req)
{
	/*There is no alternate settings for the defined interface*/
	STALL_EP0;
}

/*----------------------------------------------------------------------------
 *               usb_dev_sync_frame
 *
 *  Handles the SYNCH_FRAME device request from the USB host.
 *
 *  The SYNCH_FRAME device request is not a supported device request
 *  and stalls endpoint 0 immediately.
 *
 *  Input: pointer to struct of USB request
 *  Output: None
 *--------------------------------------------------------------------------*/
void usb_dev_sync_frame(USB_request_t *req)
{
    STALL_EP0;
}


/*----------------------------------------------------------------------------
 *               USB_req_reserved(int_no)
 *
 *  Place holder for undefined functions in Standard and Vendor device
 *  request arrays. When it's called that wrong request has been received,
 *  then stall endpoint 0. The input req is for compatibility only.
 *
 *--------------------------------------------------------------------------*/
void USB_req_reserved(USB_request_t *req)
{
    STALL_EP0;
}


/*----------------------------------------------------------------------------
 *               USB_req_reserved(int_no)
 *
 *  Place holder for undefined functions in Standard and Vendor device
 *  request arrays. When it's called that wrong request has been received,
 *  then stall endpoint 0. The input req is for compatibility only.
 *
 *--------------------------------------------------------------------------*/
void USB_req_undefined(USB_request_t *req)
{
    //undefined	but don't stall;
}


/*----------------------------------------------------------------------------
 *               usb_dev_enable_ep(int_no)
 *
 *  Enables the IN/OUT endpoints
 *
 *  Input: pointer to the endpoint struct
 *  Output: None
 *--------------------------------------------------------------------------*/
void usb_dev_enable_ep(const USB_endpoint_desc_t *ep)
{
    endpoint_t ep_no = ep->bEndpointAddress.address;

    if (ep->bEndpointAddress.direction == IN)
		FLUSH_TXEP(ep_no);
	else
		FLUSH_RXEP(ep_no);

	//enable endpoint & set its address
	write_usb(EPC_ADDR(ep_no), (byte)ep_no | EP_EN);

    if (ep->bEndpointAddress.direction == IN)
		ENABLE_TX(ep_no);
    else
		ENABLE_RX(ep_no);
}

/*----------------------------------------------------------------------------
 *               usb_dev_disable_ep(int_no)
 *
 *  Disables the IN/OUT endpoints
 *
 *  Input: pointer to the endpoint struct
 *  Output: None
 *--------------------------------------------------------------------------*/
void usb_dev_disable_ep(const USB_endpoint_desc_t *ep)
{
    if (ep->bEndpointAddress.direction == IN)
		FLUSH_TXEP(ep->bEndpointAddress.address);
	else
		FLUSH_RXEP(ep->bEndpointAddress.address);

}


/*----------------------------------------------------------------------------
 *               USB_device_req_handler
 *
 *  Handles the device requests from the USB host.
 *
 *  Check the request, if the request has at least one error, it stalls
 *  endpoint 0. Calls the request specific function.
 *
 *  Input: None
 *
 *  Output: None
 *--------------------------------------------------------------------------*/
void USB_device_req_handler() 
{
	byte *msg = control_receive_buffer.data;
    USB_request_t *req = (USB_request_t *)msg;

    if (msg == NULL) return;

    switch (REQ_TYPE(req)) {
	case STANDARD_REQ:
	    (*usb_std_device_req[REQ_DEVICE(req)])(req);
	    device_status.last_req = REQ_DEVICE(req);
	    break;
	case VENDOR_REQ:
	    (*usb_vendor_device_req[REQ_VENDOR_TYPE(req)])(req);
//	    device_status.last_req = REQ_DEVICE(req);
	    break;
    case CLASS_REQ:
	default:
	    STALL_EP0;
	    break;
    }
}



⌨️ 快捷键说明

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