Initial commit: TXW82x FPV v2.7.0.7-42229 SDK + project sources
This commit is contained in:
426
sdk/lib/bus/rttusb/usbhost/class/adk.c
Normal file
426
sdk/lib/bus/rttusb/usbhost/class/adk.c
Normal file
@@ -0,0 +1,426 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-12-12 Yi Qiu first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
#include "adk.h"
|
||||
|
||||
#ifdef RT_USBH_ADK
|
||||
|
||||
//#define DBG_TAG "usbhost.adk"
|
||||
//#define DBG_LVL DBG_INFO
|
||||
//#include <rtdbg.h>
|
||||
|
||||
static struct uclass_driver adk_driver;
|
||||
static const char* _adk_manufacturer = RT_NULL;
|
||||
static const char* _adk_model = RT_NULL;
|
||||
static const char* _adk_description = RT_NULL;
|
||||
static const char* _adk_version = RT_NULL;
|
||||
static const char* _adk_uri = RT_NULL;
|
||||
static const char* _adk_serial = RT_NULL;
|
||||
|
||||
rt_err_t rt_usbh_adk_set_string(const char* manufacturer, const char* model,
|
||||
const char* description, const char* _version, const char* uri,
|
||||
const char* serial)
|
||||
{
|
||||
_adk_manufacturer = manufacturer;
|
||||
_adk_model = model;
|
||||
_adk_description = description;
|
||||
_adk_version = _version;
|
||||
_adk_uri = uri;
|
||||
_adk_serial = serial;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_MODULE
|
||||
#include <rtm.h>
|
||||
|
||||
RTM_EXPORT(rt_usbh_adk_set_string);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_GET_PROTOCOL request to set idle period to the usb adk device
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @duration the idle period of requesting data.
|
||||
* @report_id the report id
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
static rt_err_t rt_usbh_adk_get_protocol(struct uintf* intf, rt_uint16_t *protocol)
|
||||
{
|
||||
struct urequest setup;
|
||||
uinst_t device;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
|
||||
device = intf->device;
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_VENDOR |
|
||||
USB_REQ_TYPE_DEVICE;
|
||||
setup.request = USB_REQ_GET_PROTOCOL;
|
||||
setup.index = 0;
|
||||
setup.length = 2;
|
||||
setup.value = 0;
|
||||
|
||||
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, (void*)protocol, 2,
|
||||
timeout) == 0) return RT_EOK;
|
||||
else return -RT_FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_SEND_STRING request to set idle period to the usb adk device
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @duration the idle period of requesting data.
|
||||
* @report_id the report id
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
static rt_err_t rt_usbh_adk_send_string(struct uintf* intf, rt_uint16_t index,
|
||||
const char* str)
|
||||
{
|
||||
struct urequest setup;
|
||||
uinst_t device;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
|
||||
device = intf->device;
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR |
|
||||
USB_REQ_TYPE_DEVICE;
|
||||
setup.request = USB_REQ_SEND_STRING;
|
||||
setup.index = index;
|
||||
setup.length = rt_strlen(str) + 1;
|
||||
setup.value = 0;
|
||||
|
||||
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, (void*)str,
|
||||
rt_strlen(str) + 1, timeout) == 0) return RT_EOK;
|
||||
else return -RT_FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_START request to set idle period to the usb adk device
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @duration the idle period of requesting data.
|
||||
* @report_id the report id
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
static rt_err_t rt_usbh_adk_start(struct uintf* intf)
|
||||
{
|
||||
struct urequest setup;
|
||||
uinst_t device;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
|
||||
device = intf->device;
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR |
|
||||
USB_REQ_TYPE_DEVICE;
|
||||
setup.request = USB_REQ_START;
|
||||
setup.index = 0;
|
||||
setup.length = 0;
|
||||
setup.value = 0;
|
||||
|
||||
if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0,
|
||||
timeout) == 0) return RT_EOK;
|
||||
else return -RT_FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will read data from usb adk device
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
static rt_ssize_t rt_usbh_adk_read(rt_device_t device, rt_off_t pos, void* buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
uadk_t adk;
|
||||
rt_size_t length;
|
||||
struct uintf* intf;
|
||||
|
||||
/* check parameter */
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
RT_ASSERT(buffer != RT_NULL);
|
||||
|
||||
intf = (struct uintf*)device->user_data;
|
||||
adk = (uadk_t)intf->user_data;
|
||||
|
||||
length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_in,
|
||||
buffer, size, 300);
|
||||
|
||||
return length;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will write data to usb adk device
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
static rt_ssize_t rt_usbh_adk_write (rt_device_t device, rt_off_t pos, const void* buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
uadk_t adk;
|
||||
rt_size_t length;
|
||||
struct uintf* intf;
|
||||
|
||||
RT_ASSERT(buffer != RT_NULL);
|
||||
|
||||
intf = (struct uintf*)device->user_data;
|
||||
adk = (uadk_t)intf->user_data;
|
||||
|
||||
length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_out,
|
||||
(void*)buffer, size, 300);
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
const static struct rt_device_ops adk_device_ops =
|
||||
{
|
||||
RT_NULL;
|
||||
RT_NULL;
|
||||
RT_NULL;
|
||||
rt_usbh_adk_read;
|
||||
rt_usbh_adk_write;
|
||||
RT_NULL;
|
||||
};
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function will run adk class driver when usb device is detected and identified
|
||||
* as a adk class device, it will continue the enumulate process.
|
||||
*
|
||||
* @param arg the argument.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
static rt_err_t rt_usbh_adk_enable(void* arg)
|
||||
{
|
||||
int i = 0;
|
||||
uadk_t adk;
|
||||
struct uintf* intf = (struct uintf*)arg;
|
||||
udev_desc_t dev_desc;
|
||||
rt_uint16_t protocol;
|
||||
rt_err_t ret;
|
||||
|
||||
/* parameter check */
|
||||
if(intf == RT_NULL)
|
||||
{
|
||||
rt_kprintf("the interface is not available\n");
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
LOG_D("rt_usbh_adk_run");
|
||||
|
||||
dev_desc = &intf->device->dev_desc;
|
||||
if(dev_desc->idVendor == USB_ACCESSORY_VENDOR_ID &&
|
||||
(dev_desc->idProduct == USB_ACCESSORY_PRODUCT_ID ||
|
||||
dev_desc->idProduct == USB_ACCESSORY_ADB_PRODUCT_ID))
|
||||
{
|
||||
if(intf->intf_desc->bInterfaceSubClass != 0xFF) return -RT_ERROR;
|
||||
|
||||
LOG_D("found android accessory device");
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_D("switch device");
|
||||
|
||||
if((ret = rt_usbh_adk_get_protocol(intf, &protocol)) != RT_EOK)
|
||||
{
|
||||
rt_kprintf("rt_usbh_adk_get_protocol failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
if(protocol != 1)
|
||||
{
|
||||
rt_kprintf("read protocol failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_usbh_adk_send_string(intf,
|
||||
ACCESSORY_STRING_MANUFACTURER, _adk_manufacturer);
|
||||
rt_usbh_adk_send_string(intf,
|
||||
ACCESSORY_STRING_MODEL, _adk_model);
|
||||
rt_usbh_adk_send_string(intf,
|
||||
ACCESSORY_STRING_DESCRIPTION, _adk_description);
|
||||
rt_usbh_adk_send_string(intf,
|
||||
ACCESSORY_STRING_VERSION, _adk_version);
|
||||
rt_usbh_adk_send_string(intf,
|
||||
ACCESSORY_STRING_URI, _adk_uri);
|
||||
rt_usbh_adk_send_string(intf,
|
||||
ACCESSORY_STRING_SERIAL, _adk_serial);
|
||||
|
||||
LOG_D("manufacturer %s", _adk_manufacturer);
|
||||
LOG_D("model %s", _adk_model);
|
||||
LOG_D("description %s", _adk_description);
|
||||
LOG_D("version %s", _adk_version);
|
||||
LOG_D("uri %s", _adk_uri);
|
||||
LOG_D("serial %s", _adk_serial);
|
||||
|
||||
if((ret = rt_usbh_adk_start(intf)) != RT_EOK)
|
||||
{
|
||||
rt_kprintf("rt_usbh_adk_start failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
adk = rt_malloc(sizeof(struct uadkinst));
|
||||
RT_ASSERT(adk != RT_NULL);
|
||||
|
||||
/* initilize the data structure */
|
||||
rt_memset(adk, 0, sizeof(struct uadkinst));
|
||||
intf->user_data = (void*)adk;
|
||||
|
||||
for(i=0; i<intf->intf_desc->bNumEndpoints; i++)
|
||||
{
|
||||
uep_desc_t ep_desc;
|
||||
|
||||
/* get endpoint descriptor from interface descriptor */
|
||||
rt_usbh_get_endpoint_descriptor(intf->intf_desc, i, &ep_desc);
|
||||
if(ep_desc == RT_NULL)
|
||||
{
|
||||
rt_kprintf("rt_usb_get_endpoint_descriptor error\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* the endpoint type of adk class should be BULK */
|
||||
if((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_BULK)
|
||||
continue;
|
||||
|
||||
/* allocate pipes according to the endpoint type */
|
||||
if(ep_desc->bEndpointAddress & USB_DIR_IN)
|
||||
{
|
||||
/* allocate an in pipe for the adk instance */
|
||||
ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &adk->pipe_in,
|
||||
intf, ep_desc, RT_NULL);
|
||||
if(ret != RT_EOK) return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* allocate an output pipe for the adk instance */
|
||||
ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &adk->pipe_out,
|
||||
intf, ep_desc, RT_NULL);
|
||||
if(ret != RT_EOK) return ret;
|
||||
}
|
||||
}
|
||||
|
||||
/* check pipes infomation */
|
||||
if(adk->pipe_in == RT_NULL || adk->pipe_out == RT_NULL)
|
||||
{
|
||||
rt_kprintf("pipe error, unsupported device\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* set configuration */
|
||||
ret = rt_usbh_set_configure(intf->device, 1);
|
||||
if(ret != RT_EOK) return ret;
|
||||
|
||||
/* register adk device */
|
||||
adk->device.type = RT_Device_Class_Char;
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
adk->device.ops = &adk_device_ops;
|
||||
#else
|
||||
adk->device.init = RT_NULL;
|
||||
adk->device.open = RT_NULL;
|
||||
adk->device.close = RT_NULL;
|
||||
adk->device.read = rt_usbh_adk_read;
|
||||
adk->device.write = rt_usbh_adk_write;
|
||||
adk->device.control = RT_NULL;
|
||||
#endif
|
||||
adk->device.user_data = (void*)intf;
|
||||
|
||||
rt_device_register(&adk->device, "adkdev", RT_DEVICE_FLAG_RDWR);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will be invoked when usb device plug out is detected and it would clean
|
||||
* and release all hub class related resources.
|
||||
*
|
||||
* @param arg the argument.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
static rt_err_t rt_usbh_adk_disable(void* arg)
|
||||
{
|
||||
uadk_t adk;
|
||||
struct uintf* intf = (struct uintf*)arg;
|
||||
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
|
||||
LOG_D("rt_usbh_adk_stop");
|
||||
|
||||
adk = (uadk_t)intf->user_data;
|
||||
if(adk == RT_NULL)
|
||||
{
|
||||
rt_free(intf);
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
if(adk->pipe_in != RT_NULL)
|
||||
rt_usb_hcd_free_pipe(intf->device->hcd, adk->pipe_in);
|
||||
|
||||
if(adk->pipe_out != RT_NULL)
|
||||
rt_usb_hcd_free_pipe(intf->device->hcd, adk->pipe_out);
|
||||
|
||||
/* unregister adk device */
|
||||
rt_device_unregister(&adk->device);
|
||||
|
||||
/* free adk instance */
|
||||
if(adk != RT_NULL)
|
||||
{
|
||||
rt_free(adk);
|
||||
}
|
||||
|
||||
/* free interface instance */
|
||||
rt_free(intf);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will register adk class driver to the usb class driver manager.
|
||||
* and it should be invoked in the usb system initialization.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
ucd_t rt_usbh_class_driver_adk(void)
|
||||
{
|
||||
adk_driver.class_code = USB_CLASS_ADK;
|
||||
|
||||
adk_driver.enable = rt_usbh_adk_enable;
|
||||
adk_driver.disable = rt_usbh_adk_disable;
|
||||
|
||||
return &adk_driver;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
43
sdk/lib/bus/rttusb/usbhost/class/adk.h
Normal file
43
sdk/lib/bus/rttusb/usbhost/class/adk.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-12-12 Yi Qiu first version
|
||||
*/
|
||||
|
||||
#ifndef __ADK_H__
|
||||
#define __ADK_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
struct uadkinst
|
||||
{
|
||||
upipe_t pipe_in;
|
||||
upipe_t pipe_out;
|
||||
|
||||
struct rt_device device;
|
||||
};
|
||||
typedef struct uadkinst* uadk_t;
|
||||
|
||||
#define USB_ACCESSORY_VENDOR_ID 0x18D1
|
||||
#define USB_ACCESSORY_PRODUCT_ID 0x2D00
|
||||
#define USB_ACCESSORY_ADB_PRODUCT_ID 0x2D01
|
||||
|
||||
#define ACCESSORY_STRING_MANUFACTURER 0
|
||||
#define ACCESSORY_STRING_MODEL 1
|
||||
#define ACCESSORY_STRING_DESCRIPTION 2
|
||||
#define ACCESSORY_STRING_VERSION 3
|
||||
#define ACCESSORY_STRING_URI 4
|
||||
#define ACCESSORY_STRING_SERIAL 5
|
||||
|
||||
#define USB_REQ_GET_PROTOCOL 51
|
||||
#define USB_REQ_SEND_STRING 52
|
||||
#define USB_REQ_START 53
|
||||
|
||||
#define USB_CLASS_ADK 0xff
|
||||
|
||||
#endif
|
||||
|
||||
482
sdk/lib/bus/rttusb/usbhost/class/cdc.c
Normal file
482
sdk/lib/bus/rttusb/usbhost/class/cdc.c
Normal file
@@ -0,0 +1,482 @@
|
||||
#include "include/rttusb_host.h"
|
||||
#include "cdc.h"
|
||||
#ifdef RT_USBH_VENDOR_YUGE
|
||||
#include "yuge.h"
|
||||
#endif
|
||||
#ifdef RT_USBH_VENDOR_ZXINFO
|
||||
#include "zxinfo.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_USBH_CDC
|
||||
|
||||
#ifdef RT_USBH_CDC_THREAD
|
||||
#define EVENT_CDC_DATA_START (1 << 0)
|
||||
#define EVENT_CDC_DATA_END (1 << 1)
|
||||
|
||||
static ucdc_data_t cdc_d;
|
||||
static rt_event_t cdc_data_event;
|
||||
uint8_t buff_out[64] __attribute__((aligned(4)));
|
||||
uint8_t buff_in[64 + USB_RX_BUFF_RESERVE_SIZE] __attribute__((aligned(4)));
|
||||
#endif
|
||||
|
||||
static struct uclass_driver cdc_driver;
|
||||
|
||||
rt_err_t rt_usbh_cdc_send_command(uinst_t device, void* buffer, int nbytes)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
|
||||
USB_REQ_TYPE_INTERFACE;
|
||||
setup.bRequest = SEND_ENCAPSULATED_COMMAND;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = nbytes;
|
||||
setup.wValue = 0;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, buffer, nbytes, timeout) == nbytes)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return nbytes;
|
||||
}
|
||||
}
|
||||
}
|
||||
return RT_ERROR;
|
||||
}
|
||||
|
||||
rt_err_t rt_usbh_cdc_get_response(uinst_t device, void* buffer, int nbytes)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
int ret_size;
|
||||
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS |
|
||||
USB_REQ_TYPE_INTERFACE;
|
||||
setup.bRequest = GET_ENCAPSULATED_RESPONSE;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = nbytes;
|
||||
setup.wValue = 0;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
ret_size = rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, nbytes, timeout);
|
||||
if(ret_size > 0)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return ret_size;
|
||||
}
|
||||
}
|
||||
}
|
||||
return RT_ERROR;
|
||||
}
|
||||
|
||||
rt_err_t rt_usbh_cdc_get_line_coding(uinst_t device, int intf, void* buffer)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
int ret_size;
|
||||
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS |
|
||||
USB_REQ_TYPE_INTERFACE;
|
||||
setup.bRequest = GET_LINE_CODING;
|
||||
setup.wIndex = intf;
|
||||
setup.wLength = 7;
|
||||
setup.wValue = 0;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
ret_size = rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, 7, timeout);
|
||||
if(ret_size == 7)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return ret_size;
|
||||
}
|
||||
}
|
||||
}
|
||||
return RT_ERROR;
|
||||
}
|
||||
|
||||
rt_err_t rt_usbh_cdc_set_line_coding(uinst_t device, int intf, void* buffer)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE;
|
||||
setup.bRequest = SET_LINE_CODING;
|
||||
setup.wIndex = intf; // interface
|
||||
setup.wLength = 7;
|
||||
setup.wValue = 0;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, buffer, 7, timeout) == 7)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_err_t rt_usbh_cdc_set_control_line_state(uinst_t device, int intf, void * buffer, int len)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE;
|
||||
setup.bRequest = SET_CONTROL_LINE_STATE;
|
||||
setup.wIndex = intf; // interface
|
||||
setup.wLength = len;
|
||||
setup.wValue = 0;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
void analysis_cdc_line_coding(struct usb_cdc_line_coding * line_coding)
|
||||
{
|
||||
os_printf("=========line coding=========\n");
|
||||
os_printf("dwDTERate:%d\n", line_coding->dwDTERate);
|
||||
os_printf("bCharFormat:%d\n", line_coding->bCharFormat);
|
||||
os_printf("bParityType:%d\n", line_coding->bParityType);
|
||||
os_printf("bDataBits:%d\n", line_coding->bDataBits);
|
||||
os_printf("=============================\n");
|
||||
}
|
||||
|
||||
static rt_err_t rt_usbh_get_CDC_interface_descriptor(ucfg_desc_t cfg_desc, int num,
|
||||
uintf_desc_t* intf_desc)
|
||||
{
|
||||
rt_uint32_t ptr, depth = 0;
|
||||
udesc_t desc;
|
||||
|
||||
/* check parameter */
|
||||
RT_ASSERT(cfg_desc != RT_NULL);
|
||||
|
||||
ptr = (rt_uint32_t)cfg_desc + cfg_desc->bLength;
|
||||
while(ptr < (rt_uint32_t)cfg_desc + cfg_desc->wTotalLength)
|
||||
{
|
||||
if(depth++ > 0x40)
|
||||
{
|
||||
*intf_desc = RT_NULL;
|
||||
return -RT_EIO;
|
||||
}
|
||||
desc = (udesc_t)ptr;
|
||||
if(desc->type == USB_DESC_TYPE_INTERFACE)
|
||||
{
|
||||
if(((uintf_desc_t)desc)->bNumEndpoints == 0)
|
||||
{
|
||||
ptr = (rt_uint32_t)desc + desc->bLength;
|
||||
continue;
|
||||
}
|
||||
if(((uintf_desc_t)desc)->bInterfaceNumber == num)
|
||||
{
|
||||
*intf_desc = (uintf_desc_t)desc;
|
||||
|
||||
LOG_D("rt_usb_get_interface_descriptor: %d", num);
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
ptr = (rt_uint32_t)desc + desc->bLength;
|
||||
}
|
||||
|
||||
rt_kprintf("rt_usb_get_interface_descriptor %d failed\n", num);
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
#ifdef RT_USBH_CDC_THREAD
|
||||
|
||||
int32 demo_atcmd_cdc_trans_ctrl(const char *cmd, char *argv[], uint32 argc)
|
||||
{
|
||||
if(*argv[0] == '1') {
|
||||
rt_usbh_cdc_trans_init();
|
||||
} else if(*argv[0] == '2') {
|
||||
rt_usbh_cdc_trans_deinit();
|
||||
}
|
||||
printf("OK/n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void rt_usbh_cdc_trans_init()
|
||||
{
|
||||
if (cdc_data_event != RT_NULL) {
|
||||
rt_event_send(cdc_data_event, EVENT_CDC_DATA_START);
|
||||
}
|
||||
}
|
||||
|
||||
void rt_usbh_cdc_trans_deinit()
|
||||
{
|
||||
if (cdc_data_event != RT_NULL) {
|
||||
rt_event_send(cdc_data_event, EVENT_CDC_DATA_END);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void rt_usbh_cdc_communication_thread(void* arg)
|
||||
{
|
||||
int i;
|
||||
struct uhintf **intf = arg;
|
||||
// uhcd_t hcd = NULL;
|
||||
// uintf_desc_t intf_desc;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
rt_uint32_t e;
|
||||
struct usb_cdc_line_coding *line_coding = (struct usb_cdc_line_coding *)rt_malloc(sizeof(struct usb_cdc_line_coding) + USB_RX_BUFF_RESERVE_SIZE);
|
||||
if(line_coding == RT_NULL)
|
||||
{
|
||||
rt_kprintf("rt_usbh_cdc_communication_thread malloc line_coding failed\n");
|
||||
goto __exit;
|
||||
}
|
||||
os_printf("rt_usbh_cdc_communication_thread arg:%x\n",arg);
|
||||
|
||||
cdc_d->thread_state = 1;
|
||||
cdc_d->line_coding = line_coding;
|
||||
|
||||
while(1)
|
||||
{
|
||||
if (rt_event_recv(cdc_data_event, EVENT_CDC_DATA_START | EVENT_CDC_DATA_END,
|
||||
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
|
||||
1000, &e) != RT_EOK)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (e & EVENT_CDC_DATA_START)
|
||||
{
|
||||
|
||||
}else if(e & EVENT_CDC_DATA_END)
|
||||
{
|
||||
goto __exit;
|
||||
}else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
os_printf("cdc translate strat\n");
|
||||
|
||||
memset(line_coding, 0, sizeof(struct usb_cdc_line_coding));
|
||||
rt_usbh_cdc_get_line_coding(intf[0]->device, intf[0]->intf_desc->bInterfaceNumber, line_coding);
|
||||
analysis_cdc_line_coding(line_coding);
|
||||
|
||||
line_coding->dwDTERate = BAUD_RATE_2000000;
|
||||
line_coding->bCharFormat = STOP_BITS_1;
|
||||
line_coding->bParityType = PARITY_NONE;
|
||||
line_coding->bDataBits = DATA_BITS_8;
|
||||
|
||||
rt_usbh_cdc_set_line_coding(intf[0]->device, intf[0]->intf_desc->bInterfaceNumber, line_coding);
|
||||
analysis_cdc_line_coding(line_coding);
|
||||
|
||||
memset(line_coding, 0, sizeof(struct usb_cdc_line_coding));
|
||||
rt_usbh_cdc_get_line_coding(intf[0]->device, intf[0]->intf_desc->bInterfaceNumber, line_coding);
|
||||
|
||||
rt_usbh_cdc_set_control_line_state(intf[0]->device, intf[0]->intf_desc->bInterfaceNumber, RT_NULL, 0);
|
||||
analysis_cdc_line_coding(line_coding);
|
||||
|
||||
if(cdc_d->pipe_in == RT_NULL && cdc_d->pipe_out == RT_NULL)
|
||||
{
|
||||
for(i = 0; i < intf[1]->intf_desc->bNumEndpoints; i++)
|
||||
{
|
||||
uep_desc_t ep_desc;
|
||||
upipe_t pipe;
|
||||
rt_usbh_get_endpoint_descriptor(intf[1]->intf_desc, i, &ep_desc);
|
||||
if(ep_desc == RT_NULL)
|
||||
{
|
||||
rt_kprintf("rt_usb_get_endpoint_descriptor error\n");
|
||||
return ;
|
||||
}
|
||||
analysis_usb_ep_desc(ep_desc); //获取端点描述符 打印端点描述符信息
|
||||
/* the endpoint type of mass storage class should be BULK */
|
||||
if((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_BULK)
|
||||
continue;
|
||||
|
||||
if (rt_usb_hcd_alloc_pipe(intf[0]->device->hcd, &pipe, intf[0]->device, ep_desc) != RT_EOK) {
|
||||
rt_kprintf("alloc pipe failed\n");
|
||||
return ;
|
||||
}
|
||||
|
||||
rt_usb_instance_add_pipe(intf[0]->device, pipe);
|
||||
|
||||
if ((ep_desc->bEndpointAddress & USB_DIR_MASK) == USB_DIR_IN) {
|
||||
cdc_d->pipe_in = pipe;
|
||||
os_printf("cdc_d->pipe_in:%x pipe:%x\n",cdc_d->pipe_in,pipe);
|
||||
} else {
|
||||
cdc_d->pipe_out = pipe;
|
||||
os_printf("cdc_d->pipe_out:%x pipe:%x\n",cdc_d->pipe_out,pipe);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if(cdc_d->pipe_in != RT_NULL && cdc_d->pipe_out != RT_NULL)
|
||||
{
|
||||
buff_out[0] = 0xAA;
|
||||
buff_out[1] = 0x55;
|
||||
buff_out[2] = 0xD1;
|
||||
buff_out[3] = 0x00;
|
||||
buff_out[4] = 0x00;
|
||||
buff_out[5] = 0x00;
|
||||
buff_out[6] = 0x00;
|
||||
buff_out[7] = 0x00;
|
||||
buff_out[8] = 0x00;
|
||||
buff_out[9] = 0xD0;
|
||||
|
||||
memset(buff_in,0,10);
|
||||
|
||||
rt_usb_hcd_pipe_xfer(intf[0]->device->hcd, cdc_d->pipe_out, buff_out, 64, timeout);
|
||||
|
||||
rt_usb_hcd_pipe_xfer(intf[0]->device->hcd, cdc_d->pipe_in, buff_in, 64, timeout);
|
||||
|
||||
for(int i = 0; i < 10; i++)
|
||||
{
|
||||
printf("buff_in[%d]:%x\n",i,buff_in[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__exit:
|
||||
|
||||
if (cdc_d->line_coding != RT_NULL)
|
||||
{
|
||||
rt_free(cdc_d->line_coding);
|
||||
cdc_d->line_coding = RT_NULL;
|
||||
}
|
||||
|
||||
rt_thread_suspend(cdc_d->thread);
|
||||
cdc_d->thread_state = 0;
|
||||
|
||||
return ;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static rt_err_t rt_usbh_cdc_enable(void *arg)
|
||||
{
|
||||
struct uhintf **intf = arg;
|
||||
uhcd_t hcd = NULL;
|
||||
|
||||
if (intf[0] == NULL) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
hcd = intf[0]->device->hcd;
|
||||
os_printf("subclass %d, protocal %d\r\n",
|
||||
intf[0]->intf_desc->bInterfaceSubClass,
|
||||
intf[0]->intf_desc->bInterfaceProtocol);
|
||||
|
||||
#ifdef RT_USBH_CDC_THREAD
|
||||
cdc_d = rt_malloc(sizeof(struct ucdc_data));
|
||||
if(cdc_d == RT_NULL)
|
||||
{
|
||||
rt_kprintf("allocate cdc_d memory failed\n");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
rt_memset(cdc_d, 0, sizeof(struct ucdc_data));
|
||||
|
||||
cdc_d->device = intf[0]->device;
|
||||
|
||||
os_printf("cdc_d:%x cdc_d->device:%x hcd:%x\n",cdc_d,cdc_d->device,cdc_d->device->hcd);
|
||||
|
||||
intf[0]->user_data = (void *)cdc_d;
|
||||
|
||||
os_printf("rt_usbh_cdc_enable arg:%x\n",arg);
|
||||
|
||||
cdc_data_event = rt_event_create("cdc_data_event", RT_IPC_FLAG_FIFO);
|
||||
|
||||
cdc_d->thread = rt_thread_create("cdc_comm_thread",rt_usbh_cdc_communication_thread,arg,1024,OS_TASK_PRIORITY_NORMAL,0);
|
||||
if(cdc_d->thread != RT_NULL)
|
||||
{
|
||||
rt_thread_startup(cdc_d->thread);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef RT_USBH_VENDOR_YUGE
|
||||
if (intf[0]->device->dev_desc.idVendor == USB_VENDOR_ID_YUGE) {
|
||||
rt_usbh_yuge_at_run(intf);
|
||||
}
|
||||
#endif
|
||||
#ifdef RT_USBH_VENDOR_ZXINFO
|
||||
if (intf[0]->device->dev_desc.idVendor == USB_VENDOR_ID_ZXINFO) {
|
||||
rt_usbh_zxinfo_at_run(intf);
|
||||
}
|
||||
#endif
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
static rt_err_t rt_usbh_cdc_disable(void *arg)
|
||||
{
|
||||
struct uhintf *intf = arg;
|
||||
|
||||
#ifdef RT_USBH_CDC_THREAD
|
||||
|
||||
rt_usbh_cdc_trans_deinit();
|
||||
|
||||
while(!cdc_d->thread_state)
|
||||
{
|
||||
rt_thread_delay(1);
|
||||
}
|
||||
|
||||
if(cdc_d->thread != RT_NULL)
|
||||
{
|
||||
rt_thread_delete(cdc_d->thread);
|
||||
cdc_d->thread = RT_NULL;
|
||||
}
|
||||
|
||||
if(cdc_data_event != RT_NULL)
|
||||
{
|
||||
rt_event_delete(cdc_data_event);
|
||||
cdc_data_event = RT_NULL;
|
||||
}
|
||||
|
||||
if(cdc_d != RT_NULL)
|
||||
{
|
||||
rt_free(cdc_d);
|
||||
cdc_d = RT_NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef RT_USBH_VENDOR_YUGE
|
||||
if (intf->device->dev_desc.idVendor == USB_VENDOR_ID_YUGE) {
|
||||
rt_usbh_yuge_at_stop(intf);
|
||||
}
|
||||
#endif
|
||||
#ifdef RT_USBH_VENDOR_ZXINFO
|
||||
if (intf->device->dev_desc.idVendor == USB_VENDOR_ID_ZXINFO) {
|
||||
rt_usbh_zxinfo_at_stop(intf);
|
||||
}
|
||||
#endif
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ucd_t rt_usbh_class_driver_cdc(void)
|
||||
{
|
||||
cdc_driver.class_code = USB_CLASS_COMM;
|
||||
|
||||
cdc_driver.enable = rt_usbh_cdc_enable;
|
||||
cdc_driver.disable = rt_usbh_cdc_disable;
|
||||
|
||||
return &cdc_driver;
|
||||
}
|
||||
|
||||
#endif
|
||||
95
sdk/lib/bus/rttusb/usbhost/class/cdc.h
Normal file
95
sdk/lib/bus/rttusb/usbhost/class/cdc.h
Normal file
@@ -0,0 +1,95 @@
|
||||
#ifndef __CLASS_CDC_H__
|
||||
#define __CLASS_CDC_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#define SEND_ENCAPSULATED_COMMAND 0x00
|
||||
#define GET_ENCAPSULATED_RESPONSE 0x01
|
||||
// CDC PSTN Subclass
|
||||
#define SET_LINE_CODING 0x20
|
||||
#define GET_LINE_CODING 0x21
|
||||
#define SET_CONTROL_LINE_STATE 0x22
|
||||
|
||||
#define CDC_RX_BUFSIZE 128
|
||||
#define CDC_TX_BUFSIZE 1024
|
||||
|
||||
/* The baudrate can be defined as*/
|
||||
#define BAUD_RATE_2400 2400
|
||||
#define BAUD_RATE_4800 4800
|
||||
#define BAUD_RATE_9600 9600
|
||||
#define BAUD_RATE_19200 19200
|
||||
#define BAUD_RATE_38400 38400
|
||||
#define BAUD_RATE_57600 57600
|
||||
#define BAUD_RATE_115200 115200
|
||||
#define BAUD_RATE_230400 230400
|
||||
#define BAUD_RATE_460800 460800
|
||||
#define BAUD_RATE_921600 921600
|
||||
#define BAUD_RATE_2000000 2000000
|
||||
#define BAUD_RATE_3000000 3000000
|
||||
/* Data bits can be defined as*/
|
||||
#define DATA_BITS_5 5
|
||||
#define DATA_BITS_6 6
|
||||
#define DATA_BITS_7 7
|
||||
#define DATA_BITS_8 8
|
||||
#define DATA_BITS_9 9
|
||||
/* Stop bits can be defined as */
|
||||
#define STOP_BITS_1 0
|
||||
#define STOP_BITS_2 1
|
||||
#define STOP_BITS_3 2
|
||||
#define STOP_BITS_4 3
|
||||
/* Parity bits can be defined as */
|
||||
#define PARITY_NONE 0
|
||||
#define PARITY_ODD 1
|
||||
#define PARITY_EVEN 2
|
||||
/* Bit order can be defined as */
|
||||
#define BIT_ORDER_LSB 0
|
||||
#define BIT_ORDER_MSB 1
|
||||
/* Mode canbe defined as */
|
||||
#define NRZ_NORMAL 0 /* normal mode */
|
||||
#define NRZ_INVERTED 1 /* inverted mode */
|
||||
/* Default size of the receive data buffer */
|
||||
#define RT_SERIAL_RB_BUFSZ 64
|
||||
|
||||
struct ucdc_data
|
||||
{
|
||||
struct uinstance* device;
|
||||
upipe_t pipe_in;
|
||||
upipe_t pipe_out;
|
||||
struct usb_cdc_line_coding* line_coding;
|
||||
|
||||
rt_uint8_t rx_rbp[CDC_RX_BUFSIZE];
|
||||
struct rt_ringbuffer rx_ringbuffer;
|
||||
rt_uint8_t tx_rbp[CDC_TX_BUFSIZE];
|
||||
struct rt_ringbuffer tx_ringbuffer;
|
||||
|
||||
rt_thread_t thread;
|
||||
rt_uint32_t thread_state;
|
||||
|
||||
};
|
||||
typedef struct ucdc_data* ucdc_data_t;
|
||||
|
||||
struct usb_cdc_line_coding {
|
||||
rt_uint32_t dwDTERate;
|
||||
rt_uint8_t bCharFormat;
|
||||
rt_uint8_t bParityType;
|
||||
rt_uint8_t bDataBits;
|
||||
} __attribute__((packed));
|
||||
|
||||
rt_err_t rt_usbh_cdc_send_command(uinst_t device, void* buffer, int nbytes);
|
||||
rt_err_t rt_usbh_cdc_get_response(uinst_t device, void* buffer, int nbytes);
|
||||
rt_err_t rt_usbh_cdc_get_line_coding(uinst_t device, int intf, void* buffer);
|
||||
rt_err_t rt_usbh_cdc_set_line_coding(uinst_t device, int intf, void* buffer);
|
||||
rt_err_t rt_usbh_cdc_set_control_line_state(uinst_t device, int intf, void * buffer, int len);
|
||||
void analysis_cdc_line_coding(struct usb_cdc_line_coding * line_coding);
|
||||
void rt_usbh_cdc_trans_init();
|
||||
void rt_usbh_cdc_trans_deinit();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
361
sdk/lib/bus/rttusb/usbhost/class/chinamobile.c
Normal file
361
sdk/lib/bus/rttusb/usbhost/class/chinamobile.c
Normal file
@@ -0,0 +1,361 @@
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
#include "chinamobile.h"
|
||||
#include "cdc.h"
|
||||
#include "rndis.h"
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/netdb.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/tcpip.h"
|
||||
#include "netif/ethernetif.h"
|
||||
#include "lib/common/sysevt.h"
|
||||
|
||||
#ifdef RT_USBH_VENDOR_CHINAMOBILE
|
||||
|
||||
#define USB_VENDOR_ID_CHINAMOBILE 0x2ECC
|
||||
#define USB_PRODUCT_ID_CHINAMOBILE 0x3012 // ML307R
|
||||
|
||||
#define CHINAMOBILE_ATCMD_BUFF_SIZE 128
|
||||
|
||||
static char recv_str[CHINAMOBILE_ATCMD_BUFF_SIZE];
|
||||
|
||||
static struct uclass_driver chinamobile_driver;
|
||||
|
||||
static rt_bool_t send_recv_atcmd_check(void *context, const char *send_str,
|
||||
const char *check_str, char *ret_str)
|
||||
{
|
||||
struct usb_chinamobile_at *chinamobile_at = context;
|
||||
uinst_t device = chinamobile_at->device;
|
||||
int recv_size;
|
||||
char *recv_str = NULL;
|
||||
rt_bool_t pass = RT_FALSE;
|
||||
|
||||
os_sprintf((char *)chinamobile_at->at_cmd_buff, send_str);
|
||||
rt_usb_hcd_pipe_xfer(device->hcd, chinamobile_at->pipe_out,
|
||||
chinamobile_at->at_cmd_buff, os_strlen(send_str), 0);
|
||||
os_sleep_ms(10); // 需要点延迟给LTE模组反应
|
||||
do {
|
||||
// 每次读取会清除缓存
|
||||
os_memset(chinamobile_at->at_cmd_buff, 0, CHINAMOBILE_ATCMD_BUFF_SIZE);
|
||||
recv_size = rt_usb_hcd_pipe_xfer(device->hcd, chinamobile_at->pipe_in,
|
||||
chinamobile_at->at_cmd_buff, CHINAMOBILE_ATCMD_BUFF_SIZE, 10);
|
||||
if (recv_size > 0) {
|
||||
recv_str = os_strstr(chinamobile_at->at_cmd_buff, check_str);
|
||||
if (recv_str != NULL) {
|
||||
pass = RT_TRUE;
|
||||
if (ret_str != NULL && recv_size > os_strlen(check_str)) {
|
||||
// ret_str存在说明需要获取返回结果做额外判断,复制到函数外面
|
||||
os_memcpy(ret_str, recv_str + os_strlen(check_str),
|
||||
recv_size - os_strlen(check_str));
|
||||
ret_str[recv_size - os_strlen(check_str)] = '\0'; // 字符串结束符
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (recv_size > 0);
|
||||
|
||||
return pass;
|
||||
}
|
||||
|
||||
// 计算下行频点,FDD不知道上行频点号,猜测直接偏固定频率?
|
||||
static rt_uint16_t eutra_channel_freq_mapping(rt_uint8_t band, rt_uint16_t earfcn, rt_bool_t uplink)
|
||||
{
|
||||
rt_uint16_t freq = 0;
|
||||
|
||||
switch (band) {
|
||||
// FDD上下行频点有偏差
|
||||
case 3:
|
||||
freq = 1805 + (earfcn - 1200) / 10;
|
||||
if (uplink)
|
||||
freq -= 95;
|
||||
break;
|
||||
case 5:
|
||||
freq = 869 + (earfcn - 2400) / 10;
|
||||
if (uplink)
|
||||
freq -= 45;
|
||||
break;
|
||||
case 8:
|
||||
freq = 925 + (earfcn - 3450) / 10;
|
||||
if (uplink)
|
||||
freq -= 45;
|
||||
break;
|
||||
// TDD上下行使用相同频点
|
||||
case 34: freq = 2010 + (earfcn - 36200) / 10; break;
|
||||
case 38: freq = 2570 + (earfcn - 37750) / 10; break;
|
||||
case 39: freq = 1880 + (earfcn - 38250) / 10; break;
|
||||
case 40: freq = 2300 + (earfcn - 38650) / 10; break;
|
||||
case 41: freq = 2496 + (earfcn - 39650) / 10; break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
return freq;
|
||||
}
|
||||
|
||||
static void chinamobile_network_info(void *context, char *recv_str)
|
||||
{
|
||||
struct usb_chinamobile_at *chinamobile_at = context;
|
||||
//char *argv[16];
|
||||
char **argv = NULL;
|
||||
int argc = 0;
|
||||
rt_int16_t rsrp, rssi, sinr, rsrq;
|
||||
rt_uint16_t num_dl;
|
||||
rt_uint16_t freq_dl, freq_ul;
|
||||
rt_uint8_t band = 0;
|
||||
|
||||
argv = os_malloc(16 * sizeof(char *));
|
||||
if (argv == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取运营商
|
||||
if (send_recv_atcmd_check(chinamobile_at, "AT+COPS?\r\n", "+COPS:", recv_str) == RT_TRUE) {
|
||||
argc = os_strtok(recv_str, ",", argv, 16);
|
||||
if (argc >= 3) {
|
||||
// 0,0,"CHINA MOBILE",7
|
||||
_os_printf("%s\r\n", argv[2]);
|
||||
}
|
||||
}
|
||||
// 获取网络信息
|
||||
if (send_recv_atcmd_check(chinamobile_at, "AT+MUESTATS=\"sband\"\r\n", "+MUESTATS:", recv_str) == RT_TRUE) {
|
||||
argc = os_strtok(recv_str, ",", argv, 16);
|
||||
if (argc >= 2) {
|
||||
// "sband",39
|
||||
band = os_atoi(argv[1]);
|
||||
_os_printf("BAND: %d\r\n", band);
|
||||
}
|
||||
}
|
||||
if (send_recv_atcmd_check(chinamobile_at, "AT+MUESTATS=\"radio\"\r\n", "+MUESTATS:", recv_str) == RT_TRUE) {
|
||||
argc = os_strtok(recv_str, ",", argv, 16);
|
||||
if (argc >= 13) {
|
||||
// "radio",4,-790,-560,-32768,0,0,0A513F03,255,200,38400,197,-30
|
||||
rsrp = os_atoi(argv[2]); // 0.1dBm
|
||||
rssi = os_atoi(argv[3]); // 0.1dBm
|
||||
sinr = os_atoi(argv[9]); // 0.1dB
|
||||
rsrq = os_atoi(argv[12]); // 0.1dB
|
||||
num_dl = os_atoi(argv[10]);
|
||||
freq_dl = eutra_channel_freq_mapping(band, num_dl, 0);
|
||||
freq_ul = eutra_channel_freq_mapping(band, num_dl, 1);
|
||||
_os_printf("RSRP: %d.%d dBm\r\n", rsrp/10, os_abs(rsrp%10));
|
||||
_os_printf("RSSI: %d.%d dBm\r\n", rssi/10, os_abs(rssi%10));
|
||||
_os_printf("SINR: %d.%d dB\r\n", sinr/10, os_abs(sinr%10));
|
||||
_os_printf("RSRQ: %d.%d dB\r\n", rsrq/10, os_abs(rsrq%10));
|
||||
_os_printf("F_dl: %d MHz, F_ul: %d MHz\r\n", freq_dl, freq_ul);
|
||||
}
|
||||
}
|
||||
os_free(argv);
|
||||
}
|
||||
|
||||
// 发送一条AT命令会有多条数据返回,发送后多次读循环
|
||||
static void chinamobile_at_recv(void *context)
|
||||
{
|
||||
struct usb_chinamobile_at *chinamobile_at = context;
|
||||
|
||||
while (1) {
|
||||
if (chinamobile_at->retry > 3) {
|
||||
chinamobile_at->retry = 0;
|
||||
chinamobile_at->state = CHINAMOBILE_STATE_UNKNOW;
|
||||
}
|
||||
// os_printf("recv state:%d\r\n", chinamobile_at->state);
|
||||
switch (chinamobile_at->state) {
|
||||
case CHINAMOBILE_STATE_UNKNOW:
|
||||
chinamobile_at->retry = 0;
|
||||
chinamobile_at->state = CHINAMOBILE_STATE_CHECK_AT_STATUS;
|
||||
break;
|
||||
case CHINAMOBILE_STATE_CHECK_AT_STATUS:
|
||||
// AT查询模块是否工作启动初始化流程
|
||||
if (send_recv_atcmd_check(chinamobile_at, "AT\r\n", "OK", NULL) == RT_TRUE) {
|
||||
chinamobile_at->retry = 0;
|
||||
chinamobile_at->state = CHINAMOBILE_STATE_CHECK_SIM_STATUS;
|
||||
os_printf("AT ready\r\n");
|
||||
} else {
|
||||
chinamobile_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case CHINAMOBILE_STATE_CHECK_SIM_STATUS:
|
||||
// AT+CPIN查询SIM卡状态
|
||||
if (send_recv_atcmd_check(chinamobile_at, "AT+CPIN?\r\n", "+CPIN: READY", NULL) == RT_TRUE) {
|
||||
chinamobile_at->retry = 0;
|
||||
chinamobile_at->state = CHINAMOBILE_STATE_CHECK_PS_STATUS;
|
||||
os_printf("SIM ready\r\n");
|
||||
} else {
|
||||
chinamobile_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case CHINAMOBILE_STATE_CHECK_PS_STATUS:
|
||||
// AT+CEREG查询EPS域网络注册状态
|
||||
if (send_recv_atcmd_check(chinamobile_at, "AT+CEREG?\r\n", "+CEREG: 0,1", NULL) == RT_TRUE) {
|
||||
chinamobile_at->retry = 0;
|
||||
chinamobile_at->state = CHINAMOBILE_STATE_CHECK_PDP_CONTEXT;
|
||||
os_printf("CS ready\r\n");
|
||||
} else {
|
||||
chinamobile_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case CHINAMOBILE_STATE_CHECK_PDP_CONTEXT:
|
||||
// AT+CGDCONT查询PDP场景
|
||||
if (send_recv_atcmd_check(chinamobile_at, "AT+CGDCONT?\r\n", "+CGDCONT: 1", NULL) == RT_TRUE) {
|
||||
chinamobile_at->retry = 0;
|
||||
chinamobile_at->state = CHINAMOBILE_STATE_CHECK_IP_STATUS;
|
||||
os_printf("PDP ready\r\n");
|
||||
} else {
|
||||
chinamobile_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case CHINAMOBILE_STATE_CHECK_IP_STATUS:
|
||||
// AT+MDIALUP拨号上网
|
||||
if (send_recv_atcmd_check(chinamobile_at, "AT+MDIALUP=1,1\r\n", "+MDIALUP: 1,1,", recv_str) == RT_TRUE) {
|
||||
chinamobile_at->retry = 0;
|
||||
chinamobile_at->state = CHINAMOBILE_STATE_INITIALIZED;
|
||||
os_printf("Get IP: %s", recv_str);
|
||||
SYSEVT_NEW_LTE_EVT(SYSEVT_LTE_CONNECTED, 0);
|
||||
os_printf("dial up\r\n");
|
||||
} else {
|
||||
chinamobile_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case CHINAMOBILE_STATE_INITIALIZED:
|
||||
// 查询网络信息
|
||||
chinamobile_network_info(chinamobile_at, recv_str);
|
||||
// 定时10s检查拨号上网状态
|
||||
if (send_recv_atcmd_check(chinamobile_at, "AT+MDIALUP?\r\n", "+MDIALUP: 1,1,", NULL) == RT_TRUE) {
|
||||
chinamobile_at->retry = 0;
|
||||
os_printf("LTE status ok\r\n");
|
||||
os_sleep(10);
|
||||
} else {
|
||||
chinamobile_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
os_printf("at task end\r\n");
|
||||
}
|
||||
|
||||
static rt_err_t rt_usbh_chinamobile_enable(void *arg)
|
||||
{
|
||||
rt_err_t ret = RET_OK;
|
||||
struct uhintf *intf = arg;
|
||||
uhcd_t hcd = NULL;
|
||||
uep_desc_t ep_desc = NULL;
|
||||
struct ustring_descriptor str_desc __attribute__((aligned(4)));
|
||||
struct usb_chinamobile_at *chinamobile_at = NULL;
|
||||
struct usb_cdc_line_coding line_coding;
|
||||
upipe_t pipe;
|
||||
rt_uint8_t ep_index, i;
|
||||
|
||||
if (intf == NULL) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
hcd = intf->device->hcd;
|
||||
if (intf->device->dev_desc.idProduct == USB_PRODUCT_ID_CHINAMOBILE) {
|
||||
// 怎么有两个AT串口,先直接用第2个接口判断好了
|
||||
if (intf->intf_desc->bInterfaceNumber != 2) {
|
||||
return RET_ERR;
|
||||
}
|
||||
// 通过字符描述符判断哪个是主调试串口
|
||||
ret = rt_usbh_get_string_descriptor(intf->device, intf->intf_desc->iInterface,
|
||||
&str_desc, sizeof(struct ustring_descriptor));
|
||||
if (ret != RET_OK) {
|
||||
return ret;
|
||||
}
|
||||
for (i = 0; i < str_desc.bLength; i += 2) { // 暂时没支持UNICODE的打印,默认ASCII可以隔一个打印
|
||||
_os_printf("%c", str_desc.String[i / 2]);
|
||||
}
|
||||
_os_printf("\r\n");
|
||||
|
||||
os_printf("chinamobile_at\r\n");
|
||||
chinamobile_at = (struct usb_chinamobile_at *)os_zalloc(sizeof(struct usb_chinamobile_at));
|
||||
if (chinamobile_at == NULL) {
|
||||
os_printf("chinamobile at alloc fail\r\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
// 按照命令预期回复长度对齐预留长度
|
||||
chinamobile_at->at_cmd_buff = os_malloc(CHINAMOBILE_ATCMD_BUFF_SIZE);
|
||||
if (chinamobile_at->at_cmd_buff == NULL) {
|
||||
os_printf("at_cmd_buff alloc fail\r\n");
|
||||
os_free(chinamobile_at);
|
||||
return -ENOMEM;
|
||||
}
|
||||
chinamobile_at->device = intf->device;
|
||||
intf->user_data = chinamobile_at;
|
||||
// 顺便注册devid
|
||||
dev_register(HG_USB_AT_DEVID, (struct dev_obj *)chinamobile_at);
|
||||
|
||||
for (ep_index = 0; ep_index < intf->intf_desc->bNumEndpoints; ++ep_index) {
|
||||
rt_usbh_get_endpoint_descriptor(intf->intf_desc, ep_index, &ep_desc);
|
||||
if (ep_desc == NULL) {
|
||||
return RET_ERR;
|
||||
}
|
||||
|
||||
if ((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_BULK)
|
||||
continue;
|
||||
|
||||
if (rt_usb_hcd_alloc_pipe(intf->device->hcd, &pipe, intf->device, ep_desc) != RT_EOK) {
|
||||
rt_kprintf("alloc pipe failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
rt_usb_instance_add_pipe(intf->device, pipe);
|
||||
if ((ep_desc->bEndpointAddress & USB_DIR_MASK) == USB_DIR_IN) {
|
||||
chinamobile_at->pipe_in = pipe;
|
||||
} else {
|
||||
chinamobile_at->pipe_out = pipe;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取串口参数
|
||||
os_memset(&line_coding, 0, sizeof(line_coding));
|
||||
rt_usbh_cdc_get_line_coding(intf->device, intf->intf_desc->bInterfaceNumber, &line_coding);
|
||||
os_printf("Serial: %d %d %d %d\r\n",
|
||||
line_coding.dwDTERate, line_coding.bCharFormat, line_coding.bParityType, line_coding.bDataBits);
|
||||
|
||||
rt_thread_init(&chinamobile_at->recv_task, "at_recv", chinamobile_at_recv, chinamobile_at,
|
||||
NULL, 512, OS_TASK_PRIORITY_BELOW_NORMAL, 20);
|
||||
rt_thread_startup(&chinamobile_at->recv_task);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static rt_err_t rt_usbh_chinamobile_disable(void *arg)
|
||||
{
|
||||
struct uhintf *intf = arg;
|
||||
uhcd_t hcd = NULL;
|
||||
struct usb_chinamobile_at *chinamobile_at = NULL;
|
||||
|
||||
if (intf == NULL) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
chinamobile_at = intf->user_data;
|
||||
if (chinamobile_at) {
|
||||
hcd = intf->device->hcd;
|
||||
dev_unregister((struct dev_obj *)chinamobile_at);
|
||||
rt_thread_detach(&chinamobile_at->recv_task);
|
||||
os_free(chinamobile_at->at_cmd_buff);
|
||||
os_free(chinamobile_at);
|
||||
}
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ucd_t rt_usbh_class_driver_chinamobile(void)
|
||||
{
|
||||
chinamobile_driver.class_code = USB_CLASS_VEND_SPECIFIC;
|
||||
chinamobile_driver.vendor_id = USB_VENDOR_ID_CHINAMOBILE;
|
||||
|
||||
chinamobile_driver.enable = rt_usbh_chinamobile_enable;
|
||||
chinamobile_driver.disable = rt_usbh_chinamobile_disable;
|
||||
|
||||
return &chinamobile_driver;
|
||||
}
|
||||
|
||||
#endif
|
||||
36
sdk/lib/bus/rttusb/usbhost/class/chinamobile.h
Normal file
36
sdk/lib/bus/rttusb/usbhost/class/chinamobile.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef __CLASS_CHINAMOBILE_H__
|
||||
#define __CLASS_CHINAMOBILE_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
enum chinamobile_state {
|
||||
CHINAMOBILE_STATE_UNKNOW,
|
||||
CHINAMOBILE_STATE_CHECK_AT_STATUS,
|
||||
CHINAMOBILE_STATE_CHECK_SIM_STATUS,
|
||||
CHINAMOBILE_STATE_CHECK_PS_STATUS,
|
||||
CHINAMOBILE_STATE_CHECK_PDP_CONTEXT,
|
||||
CHINAMOBILE_STATE_CHECK_IP_STATUS,
|
||||
CHIANMOBILE_STATE_CHECK_BAND,
|
||||
CHINAMOBILE_STATE_INITIALIZED,
|
||||
};
|
||||
|
||||
struct usb_chinamobile_at {
|
||||
struct dev_obj dev;
|
||||
void *device;
|
||||
struct rt_thread recv_task;
|
||||
rt_uint8_t *at_cmd_buff;
|
||||
rt_uint32_t state;
|
||||
upipe_t pipe_in;
|
||||
upipe_t pipe_out;
|
||||
rt_uint8_t retry;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
424
sdk/lib/bus/rttusb/usbhost/class/hid.c
Normal file
424
sdk/lib/bus/rttusb/usbhost/class/hid.c
Normal file
@@ -0,0 +1,424 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-12-12 Yi Qiu first version
|
||||
* 2021-02-23 Leslie Lee update with current usb api
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
#include "hid.h"
|
||||
|
||||
#ifdef RT_USBH_HID
|
||||
|
||||
//#define DBG_TAG "usbhost.hid"
|
||||
//#define DBG_LVL DBG_INFO
|
||||
//#include <rtdbg.h>
|
||||
|
||||
static struct uclass_driver hid_driver;
|
||||
static rt_list_t _protocal_list;
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_SET_IDLE request to set idle period to the usb hid device
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @duration the idle period of requesting data.
|
||||
* @report_id the report id
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_hid_set_idle(struct uhintf* intf, int duration, int report_id)
|
||||
{
|
||||
struct urequest setup;
|
||||
struct uinstance* device;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
|
||||
device = intf->device;
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
|
||||
USB_REQ_TYPE_INTERFACE;
|
||||
setup.bRequest = USB_REQ_SET_IDLE;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = 0;
|
||||
setup.wValue = (duration << 8 )| report_id;
|
||||
|
||||
if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
|
||||
return -RT_FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_GET_REPORT request to get report from the usb hid device
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @buffer the data buffer to save usb report descriptor.
|
||||
* @param nbytes the size of buffer
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_hid_get_report(struct uhintf* intf, rt_uint8_t type,
|
||||
rt_uint8_t id, rt_uint8_t *buffer, rt_size_t size)
|
||||
{
|
||||
struct urequest setup;
|
||||
struct uinstance* device;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
|
||||
device = intf->device;
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS |
|
||||
USB_REQ_TYPE_INTERFACE;
|
||||
setup.bRequest = USB_REQ_GET_REPORT;
|
||||
setup.wIndex = intf->intf_desc->bInterfaceNumber;
|
||||
setup.wLength = size;
|
||||
setup.wValue = (type << 8 ) + id;
|
||||
|
||||
if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, size, timeout) == size)
|
||||
{
|
||||
if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -RT_FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_SET_REPORT request to set report to the usb hid device
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @buffer the data buffer to save usb report descriptor.
|
||||
* @param nbytes the size of buffer
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_hid_set_report(struct uhintf* intf, rt_uint8_t *buffer, rt_size_t size)
|
||||
{
|
||||
struct urequest setup;
|
||||
struct uinstance* device;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
|
||||
device = intf->device;
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
|
||||
USB_REQ_TYPE_INTERFACE;
|
||||
setup.bRequest = USB_REQ_SET_REPORT;
|
||||
setup.wIndex = intf->intf_desc->bInterfaceNumber;
|
||||
setup.wLength = size;
|
||||
setup.wValue = 0x02 << 8;
|
||||
|
||||
if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
|
||||
return -RT_FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_SET_PROTOCOL request to set protocal to the usb hid device.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @param protocol the protocol id.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_hid_set_protocal(struct uhintf* intf, int protocol)
|
||||
{
|
||||
struct urequest setup;
|
||||
struct uinstance* device;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
|
||||
device = intf->device;
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
|
||||
USB_REQ_TYPE_INTERFACE;
|
||||
setup.bRequest = USB_REQ_SET_PROTOCOL;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = 0;
|
||||
setup.wValue = protocol;
|
||||
|
||||
if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
return -RT_FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_GET_DESCRIPTOR request for the device instance
|
||||
* to set feature of the hub port.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @buffer the data buffer to save usb report descriptor.
|
||||
* @param nbytes the size of buffer
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_hid_get_report_descriptor(struct uhintf* intf,
|
||||
rt_uint8_t *buffer, rt_size_t size)
|
||||
{
|
||||
struct urequest setup;
|
||||
struct uinstance* device;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
|
||||
device = intf->device;
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_STANDARD|
|
||||
USB_REQ_TYPE_INTERFACE;
|
||||
setup.bRequest = USB_REQ_GET_DESCRIPTOR;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = size;
|
||||
setup.wValue = USB_DESC_TYPE_REPORT << 8;
|
||||
|
||||
if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, size, timeout) == size)
|
||||
{
|
||||
if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
return -RT_FALSE;
|
||||
return -RT_FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will register specified hid protocal to protocal list
|
||||
*
|
||||
* @param protocal the specified protocal.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_hid_protocal_register(uprotocal_t protocal)
|
||||
{
|
||||
RT_ASSERT(protocal != RT_NULL);
|
||||
|
||||
if (protocal == RT_NULL) return -RT_ERROR;
|
||||
|
||||
/* insert class driver into driver list */
|
||||
rt_list_insert_after(&_protocal_list, &(protocal->list));
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is the callback function of hid's int endpoint, it is invoked when data comes.
|
||||
*
|
||||
* @param context the context of the callback function.
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
static void rt_usbh_hid_callback(void* context)
|
||||
{
|
||||
upipe_t pipe;
|
||||
struct uhid* hid;
|
||||
int timeout = USB_TIMEOUT_LONG;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(context != RT_NULL);
|
||||
|
||||
pipe = (upipe_t)context;
|
||||
hid = (struct uhid*)((struct uhintf*)pipe->inst)->user_data;
|
||||
|
||||
/* invoke protocal callback function */
|
||||
hid->protocal->callback((void*)hid);
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(((struct uhintf*)pipe->inst)->device->hcd != RT_NULL);
|
||||
|
||||
rt_usb_hcd_pipe_xfer(((struct uhintf*)pipe->inst)->device->hcd, pipe,
|
||||
hid->buffer, pipe->ep.wMaxPacketSize, timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will find specified hid protocal from protocal list
|
||||
*
|
||||
* @param pro_id the protocal id.
|
||||
*
|
||||
* @return the found protocal or RT_NULL if there is no this protocal.
|
||||
*/
|
||||
static uprotocal_t rt_usbh_hid_protocal_find(int pro_id)
|
||||
{
|
||||
struct rt_list_node *node;
|
||||
|
||||
/* try to find protocal object */
|
||||
for (node = _protocal_list.next; node != &_protocal_list; node = node->next)
|
||||
{
|
||||
uprotocal_t protocal =
|
||||
(uprotocal_t)rt_list_entry(node, struct uprotocal, list);
|
||||
if (protocal->pro_id == pro_id) return protocal;
|
||||
}
|
||||
|
||||
/* not found */
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will run hid class driver when usb device is detected and identified
|
||||
* as a hid class device, it will continue the enumulate process.
|
||||
*
|
||||
* @param arg the argument.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
static rt_err_t rt_usbh_hid_enable(void* arg)
|
||||
{
|
||||
int i = 0, pro_id;
|
||||
uprotocal_t protocal;
|
||||
struct uhid* hid;
|
||||
struct uhintf* intf = (struct uhintf*)arg;
|
||||
|
||||
/* parameter check */
|
||||
if(intf == RT_NULL)
|
||||
{
|
||||
rt_kprintf("the interface is not available\n");
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
pro_id = intf->intf_desc->bInterfaceProtocol;
|
||||
|
||||
os_printf("HID device enable, protocal id %d\n", pro_id);
|
||||
|
||||
protocal = rt_usbh_hid_protocal_find(pro_id);
|
||||
if(protocal == RT_NULL)
|
||||
{
|
||||
rt_kprintf("can't find hid protocal %d\n", pro_id);
|
||||
intf->user_data = RT_NULL;
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
hid = rt_malloc(sizeof(struct uhid));
|
||||
RT_ASSERT(hid != RT_NULL);
|
||||
|
||||
/* initilize the data structure */
|
||||
rt_memset(hid, 0, sizeof(struct uhid));
|
||||
intf->user_data = (void*)hid;
|
||||
hid->protocal = protocal;
|
||||
|
||||
for(i = 0; i < intf->intf_desc->bNumEndpoints; i++)
|
||||
{
|
||||
rt_err_t ret;
|
||||
uep_desc_t ep_desc;
|
||||
|
||||
/* get endpoint descriptor */
|
||||
rt_usbh_get_endpoint_descriptor(intf->intf_desc, i, &ep_desc);
|
||||
if(ep_desc == RT_NULL)
|
||||
{
|
||||
rt_kprintf("rt_usbh_get_endpoint_descriptor error\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
analysis_usb_ep_desc(ep_desc); //获取端点描述符 打印端点描述符信息
|
||||
if(USB_EP_ATTR(ep_desc->bmAttributes) != USB_EP_ATTR_INT)
|
||||
continue;
|
||||
|
||||
if(!(ep_desc->bEndpointAddress & USB_DIR_IN)) continue;
|
||||
|
||||
ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &hid->pipe_in,
|
||||
intf->device, ep_desc);
|
||||
if(ret != RT_EOK) return ret;
|
||||
|
||||
rt_usb_instance_add_pipe(intf->device, hid->pipe_in);
|
||||
}
|
||||
|
||||
/* initialize hid protocal */
|
||||
hid->protocal->init((void*)intf);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will be invoked when usb device plug out is detected and it would clean
|
||||
* and release all hub class related resources.
|
||||
*
|
||||
* @param arg the argument.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
static rt_err_t rt_usbh_hid_disable(void* arg)
|
||||
{
|
||||
struct uhid* hid;
|
||||
struct uhintf* intf = (struct uhintf*)arg;
|
||||
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
|
||||
LOG_D("rt_usbh_hid_disable");
|
||||
|
||||
hid = (struct uhid*)intf->user_data;
|
||||
if(hid != RT_NULL)
|
||||
{
|
||||
if(hid->pipe_in != RT_NULL)
|
||||
{
|
||||
/* free the HID in pipe */
|
||||
rt_usb_hcd_free_pipe(intf->device->hcd, hid->pipe_in);
|
||||
}
|
||||
|
||||
/* free the hid instance */
|
||||
rt_free(hid);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will register hid class driver to the usb class driver manager.
|
||||
* and it should be invoked in the usb system initialization.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
ucd_t rt_usbh_class_driver_hid(void)
|
||||
{
|
||||
rt_list_init(&_protocal_list);
|
||||
|
||||
hid_driver.class_code = USB_CLASS_HID;
|
||||
|
||||
hid_driver.enable = rt_usbh_hid_enable;
|
||||
hid_driver.disable = rt_usbh_hid_disable;
|
||||
|
||||
return &hid_driver;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
41
sdk/lib/bus/rttusb/usbhost/class/hid.h
Normal file
41
sdk/lib/bus/rttusb/usbhost/class/hid.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-12-12 Yi Qiu first version
|
||||
*/
|
||||
|
||||
#ifndef __HID_H__
|
||||
#define __HID_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
struct uhid
|
||||
{
|
||||
upipe_t pipe_in;
|
||||
rt_uint8_t buffer[8 + USB_RX_BUFF_RESERVE_SIZE];
|
||||
uprotocal_t protocal;
|
||||
};
|
||||
typedef struct uhid uhid_t;
|
||||
|
||||
#define USB_REQ_GET_REPORT 0x01
|
||||
#define USB_REQ_GET_IDLE 0x02
|
||||
#define USB_REQ_GET_PROTOCOL 0x03
|
||||
#define USB_REQ_SET_REPORT 0x09
|
||||
#define USB_REQ_SET_IDLE 0x0a
|
||||
#define USB_REQ_SET_PROTOCOL 0x0b
|
||||
|
||||
#define USB_HID_KEYBOARD 1
|
||||
#define USB_HID_MOUSE 2
|
||||
|
||||
rt_err_t rt_usbh_hid_set_idle(struct uhintf* intf, int duration, int report_id);
|
||||
rt_err_t rt_usbh_hid_get_report(struct uhintf* intf, rt_uint8_t type, rt_uint8_t id, rt_uint8_t *buffer, rt_size_t size);
|
||||
rt_err_t rt_usbh_hid_set_report(struct uhintf* intf, rt_uint8_t *buffer, rt_size_t size);
|
||||
rt_err_t rt_usbh_hid_set_protocal(struct uhintf* intf, int protocol);
|
||||
rt_err_t rt_usbh_hid_get_report_descriptor(struct uhintf* intf, rt_uint8_t *buffer, rt_size_t size);
|
||||
rt_err_t rt_usbh_hid_protocal_register(uprotocal_t protocal);
|
||||
|
||||
#endif
|
||||
691
sdk/lib/bus/rttusb/usbhost/class/mass.c
Normal file
691
sdk/lib/bus/rttusb/usbhost/class/mass.c
Normal file
@@ -0,0 +1,691 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-12-12 Yi Qiu first version
|
||||
*/
|
||||
|
||||
#include "rtthread.h"
|
||||
#include "include/rttusb_host.h"
|
||||
#include "mass.h"
|
||||
|
||||
#ifdef RT_USBH_MSTORAGE
|
||||
|
||||
//#define DBG_TAG "usbhost.mass"
|
||||
//#define DBG_LVL DBG_INFO
|
||||
//#include <rtdbg.h>
|
||||
|
||||
extern rt_err_t rt_udisk_run(struct uhintf* intf);
|
||||
extern rt_err_t rt_udisk_stop(struct uhintf* intf);
|
||||
|
||||
static struct uclass_driver storage_driver;
|
||||
|
||||
/**
|
||||
* This function will do USBREQ_GET_MAX_LUN request for the usb interface instance.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @param max_lun the buffer to save max_lun.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
static rt_err_t _pipe_check(struct uhintf* intf, upipe_t pipe)
|
||||
{
|
||||
struct uinstance* device;
|
||||
ustor_t stor;
|
||||
int size = 0;
|
||||
struct ustorage_csw csw;
|
||||
|
||||
if(intf == RT_NULL || pipe == RT_NULL)
|
||||
{
|
||||
rt_kprintf("the interface is not available\n");
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
/* get usb device instance from the interface instance */
|
||||
device = intf->device;
|
||||
|
||||
/* get storage instance from the interface instance */
|
||||
stor = (ustor_t)intf->user_data;
|
||||
|
||||
#if 0
|
||||
rt_err_t ret;
|
||||
/* check pipe status */
|
||||
if(pipe->status == UPIPE_STATUS_OK) return RT_EOK;
|
||||
|
||||
if(pipe->status == UPIPE_STATUS_ERROR)
|
||||
{
|
||||
rt_kprintf("pipe status error\n");
|
||||
return -RT_EIO;
|
||||
}
|
||||
if(pipe->status == UPIPE_STATUS_STALL)
|
||||
{
|
||||
/* clear the pipe stall status */
|
||||
ret = rt_usbh_clear_feature(device, pipe->ep.bEndpointAddress,
|
||||
USB_FEATURE_ENDPOINT_HALT);
|
||||
if(ret != RT_EOK) return ret;
|
||||
}
|
||||
#else
|
||||
return RT_EOK;
|
||||
#endif
|
||||
|
||||
rt_thread_delay(50);
|
||||
|
||||
rt_kprintf("pipes1 0x%x, 0x%x\n", stor->pipe_in, stor->pipe_out);
|
||||
|
||||
stor->pipe_in->status = UPIPE_STATUS_OK;
|
||||
|
||||
LOG_D("clean storage in pipe stall");
|
||||
|
||||
/* it should receive csw after clear the stall feature */
|
||||
size = rt_usb_hcd_pipe_xfer(stor->pipe_in->inst->hcd,
|
||||
stor->pipe_in, &csw, SIZEOF_CSW, 100);
|
||||
if(size != SIZEOF_CSW)
|
||||
{
|
||||
rt_kprintf("receive the csw after stall failed\n");
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USBREQ_GET_MAX_LUN request for the usb interface instance.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @param max_lun the buffer to save max_lun.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
|
||||
static rt_uint8_t csw_buff[SIZEOF_CSW + USB_RX_BUFF_RESERVE_SIZE];
|
||||
|
||||
static rt_err_t rt_usb_bulk_only_xfer(struct uhintf* intf,
|
||||
ustorage_cbw_t cmd, rt_uint8_t* buffer, int timeout)
|
||||
{
|
||||
rt_size_t size;
|
||||
rt_err_t ret;
|
||||
upipe_t pipe;
|
||||
ustorage_csw_t csw = (ustorage_csw_t)csw_buff;
|
||||
ustor_t stor;
|
||||
|
||||
RT_ASSERT(cmd != RT_NULL);
|
||||
|
||||
if(intf == RT_NULL)
|
||||
{
|
||||
rt_kprintf("the interface is not available\n");
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
/* get storage instance from the interface instance */
|
||||
stor = (ustor_t)intf->user_data;
|
||||
|
||||
if(stor == RT_NULL)
|
||||
{
|
||||
rt_kprintf("stor is not available\n");
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
stor->ref ++;
|
||||
|
||||
do
|
||||
{
|
||||
/* send the cbw */
|
||||
size = rt_usb_hcd_pipe_xfer(stor->pipe_out->inst->hcd, stor->pipe_out,
|
||||
cmd, SIZEOF_CBW, timeout);
|
||||
if(size != SIZEOF_CBW)
|
||||
{
|
||||
rt_kprintf("CBW size error\n");
|
||||
goto __exit_err;
|
||||
}
|
||||
if(cmd->xfer_len != 0)
|
||||
{
|
||||
pipe = (cmd->dflags == CBWFLAGS_DIR_IN) ? stor->pipe_in :
|
||||
stor->pipe_out;
|
||||
size = rt_usb_hcd_pipe_xfer(pipe->inst->hcd, pipe, (void*)buffer,
|
||||
cmd->xfer_len, timeout);
|
||||
if(size != cmd->xfer_len)
|
||||
{
|
||||
rt_kprintf("request size %d, transfer size %d\n",
|
||||
cmd->xfer_len, size);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* receive the csw */
|
||||
size = rt_usb_hcd_pipe_xfer(stor->pipe_in->inst->hcd, stor->pipe_in,
|
||||
csw_buff, SIZEOF_CSW, timeout);
|
||||
|
||||
if(size != SIZEOF_CSW)
|
||||
{
|
||||
rt_kprintf("csw size error\n");
|
||||
goto __exit_err;
|
||||
}
|
||||
}while(0);
|
||||
|
||||
/* check in pipes status */
|
||||
ret = _pipe_check(intf, stor->pipe_in);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("in pipe error\n");
|
||||
goto __exit_err;
|
||||
}
|
||||
|
||||
/* check out pipes status */
|
||||
ret = _pipe_check(intf, stor->pipe_out);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("out pipe error\n");
|
||||
goto __exit_err;
|
||||
}
|
||||
|
||||
|
||||
/* check csw status */
|
||||
if(csw->signature != CSW_SIGNATURE || csw->tag != CBW_TAG_VALUE)
|
||||
{
|
||||
rt_kprintf("csw signature error\n");
|
||||
goto __exit_err;
|
||||
}
|
||||
|
||||
if(csw->status != 0)
|
||||
{
|
||||
rt_kprintf("csw status error:%d\n",csw->status);
|
||||
goto __exit_err;
|
||||
}
|
||||
|
||||
stor->ref --;
|
||||
return RT_EOK;
|
||||
|
||||
__exit_err:
|
||||
stor->ref --;
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USBREQ_GET_MAX_LUN request for the usb interface instance.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @param max_lun the buffer to save max_lun.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_storage_get_max_lun(struct uhintf* intf, rt_uint8_t* max_lun)
|
||||
{
|
||||
struct uinstance* device;
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
if(intf == RT_NULL)
|
||||
{
|
||||
rt_kprintf("the interface is not available\n");
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
LOG_D("rt_usbh_storage_get_max_lun");
|
||||
|
||||
/* get usb device instance from the interface instance */
|
||||
device = intf->device;
|
||||
|
||||
/* construct the request */
|
||||
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS |
|
||||
USB_REQ_TYPE_INTERFACE;
|
||||
setup.bRequest = USBREQ_GET_MAX_LUN;
|
||||
setup.wValue = intf->intf_desc->bInterfaceNumber;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = 1;
|
||||
|
||||
/* do control transfer request */
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, max_lun, 1, timeout) != 1)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) != 0)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USBREQ_MASS_STORAGE_RESET request for the usb interface instance.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_storage_reset(struct uhintf* intf)
|
||||
{
|
||||
struct urequest setup;
|
||||
struct uinstance* device;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* parameter check */
|
||||
if(intf == RT_NULL)
|
||||
{
|
||||
rt_kprintf("the interface is not available\n");
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
LOG_D("rt_usbh_storage_reset");
|
||||
|
||||
/* get usb device instance from the interface instance */
|
||||
device = intf->device;
|
||||
|
||||
/* construct the request */
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
|
||||
USB_REQ_TYPE_INTERFACE;
|
||||
setup.bRequest = USBREQ_MASS_STORAGE_RESET;
|
||||
setup.wIndex = intf->intf_desc->bInterfaceNumber;
|
||||
setup.wLength = 0;
|
||||
setup.wValue = 0;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) != 0)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will execute SCSI_READ_10 command to read data from the usb device.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @param buffer the data buffer to save read data
|
||||
* @param sector the start sector address to read.
|
||||
* @param sector the sector count to read.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_storage_read10(struct uhintf* intf, rt_uint8_t *buffer,
|
||||
rt_uint32_t sector, rt_size_t count, int timeout)
|
||||
{
|
||||
struct ustorage_cbw cmd;
|
||||
|
||||
/* parameter check */
|
||||
if(intf == RT_NULL)
|
||||
{
|
||||
rt_kprintf("interface is not available\n");
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
LOG_D("rt_usbh_storage_read10");
|
||||
|
||||
/* construct the command block wrapper */
|
||||
rt_memset(&cmd, 0, sizeof(struct ustorage_cbw));
|
||||
cmd.signature = CBW_SIGNATURE;
|
||||
cmd.tag = CBW_TAG_VALUE;
|
||||
cmd.xfer_len = SECTOR_SIZE * count;
|
||||
cmd.dflags = CBWFLAGS_DIR_IN;
|
||||
cmd.lun = 0;
|
||||
cmd.cb_len = 10;
|
||||
cmd.cb[0] = SCSI_READ_10;
|
||||
cmd.cb[1] = 0;
|
||||
cmd.cb[2] = (rt_uint8_t)(sector >> 24);
|
||||
cmd.cb[3] = (rt_uint8_t)(sector >> 16);
|
||||
cmd.cb[4] = (rt_uint8_t)(sector >> 8);
|
||||
cmd.cb[5] = (rt_uint8_t)sector;
|
||||
cmd.cb[6] = 0;
|
||||
cmd.cb[7] = (count & 0xff00) >> 8;
|
||||
cmd.cb[8] = (rt_uint8_t) count & 0xff;
|
||||
|
||||
return rt_usb_bulk_only_xfer(intf, &cmd, buffer, timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will execute SCSI_WRITE_10 command to write data to the usb device.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @param buffer the data buffer to save write data
|
||||
* @param sector the start sector address to write.
|
||||
* @param sector the sector count to write.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_storage_write10(struct uhintf* intf, rt_uint8_t *buffer,
|
||||
rt_uint32_t sector, rt_size_t count, int timeout)
|
||||
{
|
||||
struct ustorage_cbw cmd;
|
||||
|
||||
/* parameter check */
|
||||
if(intf == RT_NULL)
|
||||
{
|
||||
rt_kprintf("the interface is not available\n");
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
LOG_D("rt_usbh_storage_write10");
|
||||
|
||||
/* construct the command block wrapper */
|
||||
rt_memset(&cmd, 0, sizeof(struct ustorage_cbw));
|
||||
cmd.signature = CBW_SIGNATURE;
|
||||
cmd.tag = CBW_TAG_VALUE;
|
||||
cmd.xfer_len = SECTOR_SIZE * count;
|
||||
cmd.dflags = CBWFLAGS_DIR_OUT;
|
||||
cmd.lun = 0;
|
||||
cmd.cb_len = 10;
|
||||
cmd.cb[0] = SCSI_WRITE_10;
|
||||
cmd.cb[1] = 0;
|
||||
cmd.cb[2] = (rt_uint8_t)(sector >> 24);
|
||||
cmd.cb[3] = (rt_uint8_t)(sector >> 16);
|
||||
cmd.cb[4] = (rt_uint8_t)(sector >> 8);
|
||||
cmd.cb[5] = (rt_uint8_t)sector;
|
||||
cmd.cb[6] = 0;
|
||||
cmd.cb[7] = (count & 0xff00) >> 8;
|
||||
cmd.cb[8] = (rt_uint8_t) count & 0xff;
|
||||
|
||||
return rt_usb_bulk_only_xfer(intf, &cmd, buffer, timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will execute SCSI_REQUEST_SENSE command to get sense data.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @param buffer the data buffer to save sense data
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_storage_request_sense(struct uhintf* intf, rt_uint8_t* buffer)
|
||||
{
|
||||
struct ustorage_cbw cmd;
|
||||
int timeout = USB_TIMEOUT_LONG;
|
||||
|
||||
/* parameter check */
|
||||
if(intf == RT_NULL)
|
||||
{
|
||||
rt_kprintf("the interface is not available\n");
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
LOG_D("rt_usbh_storage_request_sense");
|
||||
|
||||
/* construct the command block wrapper */
|
||||
rt_memset(&cmd, 0, sizeof(struct ustorage_cbw));
|
||||
cmd.signature = CBW_SIGNATURE;
|
||||
cmd.tag = CBW_TAG_VALUE;
|
||||
cmd.xfer_len = 18;
|
||||
cmd.dflags = CBWFLAGS_DIR_IN;
|
||||
cmd.lun = 0;
|
||||
cmd.cb_len = 6;
|
||||
cmd.cb[0] = SCSI_REQUEST_SENSE;
|
||||
cmd.cb[4] = 18;
|
||||
|
||||
return rt_usb_bulk_only_xfer(intf, &cmd, buffer, timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will execute SCSI_TEST_UNIT_READY command to get unit ready status.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_storage_test_unit_ready(struct uhintf* intf)
|
||||
{
|
||||
struct ustorage_cbw cmd;
|
||||
int timeout = USB_TIMEOUT_LONG;
|
||||
|
||||
/* parameter check */
|
||||
if(intf == RT_NULL)
|
||||
{
|
||||
rt_kprintf("the interface is not available\n");
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
LOG_D("rt_usbh_storage_test_unit_ready");
|
||||
|
||||
/* construct the command block wrapper */
|
||||
rt_memset(&cmd, 0, sizeof(struct ustorage_cbw));
|
||||
cmd.signature = CBW_SIGNATURE;
|
||||
cmd.tag = CBW_TAG_VALUE;
|
||||
cmd.xfer_len = 0;
|
||||
cmd.dflags = CBWFLAGS_DIR_OUT;
|
||||
cmd.lun = 0;
|
||||
cmd.cb_len = 12;
|
||||
cmd.cb[0] = SCSI_TEST_UNIT_READY;
|
||||
|
||||
return rt_usb_bulk_only_xfer(intf, &cmd, RT_NULL, timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will execute SCSI_INQUIRY_CMD command to get inquiry data.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @param buffer the data buffer to save inquiry data
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_storage_inquiry(struct uhintf* intf, rt_uint8_t* buffer)
|
||||
{
|
||||
struct ustorage_cbw cmd;
|
||||
int timeout = USB_TIMEOUT_LONG;
|
||||
|
||||
/* parameter check */
|
||||
if(intf == RT_NULL)
|
||||
{
|
||||
rt_kprintf("the interface is not available\n");
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
LOG_D("rt_usbh_storage_inquiry");
|
||||
|
||||
/* construct the command block wrapper */
|
||||
rt_memset(&cmd, 0, sizeof(struct ustorage_cbw));
|
||||
cmd.signature = CBW_SIGNATURE;
|
||||
cmd.tag = CBW_TAG_VALUE;
|
||||
cmd.xfer_len = 36;
|
||||
cmd.dflags = CBWFLAGS_DIR_IN;
|
||||
cmd.lun = 0;
|
||||
cmd.cb_len = 6;//12
|
||||
cmd.cb[0] = SCSI_INQUIRY_CMD;
|
||||
cmd.cb[4] = 36;
|
||||
|
||||
return rt_usb_bulk_only_xfer(intf, &cmd, buffer, timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will execute SCSI_READ_CAPACITY command to get capacity data.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @param buffer the data buffer to save capacity data
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_storage_get_capacity(struct uhintf* intf, rt_uint8_t* buffer)
|
||||
{
|
||||
struct ustorage_cbw cmd;
|
||||
int timeout = USB_TIMEOUT_LONG;
|
||||
|
||||
/* parameter check */
|
||||
if(intf == RT_NULL)
|
||||
{
|
||||
rt_kprintf("the interface is not available\n");
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
LOG_D("rt_usbh_storage_get_capacity");
|
||||
|
||||
/* construct the command block wrapper */
|
||||
rt_memset(&cmd, 0, sizeof(struct ustorage_cbw));
|
||||
cmd.signature = CBW_SIGNATURE;
|
||||
cmd.tag = CBW_TAG_VALUE;
|
||||
cmd.xfer_len = 8;
|
||||
cmd.dflags = CBWFLAGS_DIR_IN;
|
||||
cmd.lun = 0;
|
||||
cmd.cb_len = 12;
|
||||
cmd.cb[0] = SCSI_READ_CAPACITY;
|
||||
|
||||
return rt_usb_bulk_only_xfer(intf, &cmd, buffer, timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will run mass storage class driver when usb device is detected
|
||||
* and identified as a mass storage class device, it will continue to do the enumulate
|
||||
* process.
|
||||
*
|
||||
* @param arg the argument.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
static rt_err_t rt_usbh_storage_enable(void* arg)
|
||||
{
|
||||
int i = 0;
|
||||
rt_err_t ret;
|
||||
ustor_t stor;
|
||||
upipe_t pipe;
|
||||
struct uhintf* intf = (struct uhintf*)arg;
|
||||
|
||||
/* parameter check */
|
||||
if(intf == RT_NULL)
|
||||
{
|
||||
rt_kprintf("the interface is not available\n");
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
LOG_D("subclass %d, protocal %d",
|
||||
intf->intf_desc->bInterfaceSubClass,
|
||||
intf->intf_desc->bInterfaceProtocol);
|
||||
|
||||
LOG_D("rt_usbh_storage_run");
|
||||
|
||||
/* only support SCSI subclass and bulk only protocal */
|
||||
|
||||
stor = rt_malloc(sizeof(struct ustor));
|
||||
RT_ASSERT(stor != RT_NULL);
|
||||
|
||||
/* initilize the data structure */
|
||||
rt_memset(stor, 0, sizeof(struct ustor));
|
||||
intf->user_data = (void*)stor;
|
||||
|
||||
for(i=0; i<intf->intf_desc->bNumEndpoints; i++)
|
||||
{
|
||||
uep_desc_t ep_desc;
|
||||
|
||||
/* get endpoint descriptor from interface descriptor */
|
||||
rt_usbh_get_endpoint_descriptor(intf->intf_desc, i, &ep_desc);
|
||||
if(ep_desc == RT_NULL)
|
||||
{
|
||||
rt_kprintf("rt_usb_get_endpoint_descriptor error\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
analysis_usb_ep_desc(ep_desc); //获取端点描述符 打印端点描述符信息
|
||||
/* the endpoint type of mass storage class should be BULK */
|
||||
if((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_BULK)
|
||||
continue;
|
||||
|
||||
if (rt_usb_hcd_alloc_pipe(intf->device->hcd, &pipe, intf->device, ep_desc) != RT_EOK) {
|
||||
rt_kprintf("alloc pipe failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* allocate pipes according to the endpoint type */
|
||||
if(ep_desc->bEndpointAddress & USB_DIR_IN)
|
||||
{
|
||||
/* alloc an in pipe for the storage instance */
|
||||
rt_usb_instance_add_pipe(intf->device, pipe);
|
||||
stor->pipe_in = rt_usb_instance_find_pipe(intf->device,ep_desc->bEndpointAddress);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* alloc an output pipe for the storage instance */
|
||||
rt_usb_instance_add_pipe(intf->device, pipe);
|
||||
stor->pipe_out = rt_usb_instance_find_pipe(intf->device,ep_desc->bEndpointAddress);
|
||||
}
|
||||
}
|
||||
|
||||
/* check pipes infomation */
|
||||
if(stor->pipe_in == RT_NULL || stor->pipe_out == RT_NULL)
|
||||
{
|
||||
rt_kprintf("pipe error, unsupported device\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* should implement as callback */
|
||||
ret = rt_udisk_run(intf);
|
||||
if(ret != RT_EOK) return ret;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will be invoked when usb device plug out is detected and it would clean
|
||||
* and release all mass storage class related resources.
|
||||
*
|
||||
* @param arg the argument.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
static rt_err_t rt_usbh_storage_disable(void* arg)
|
||||
{
|
||||
ustor_t stor;
|
||||
struct uhintf* intf = (struct uhintf*)arg;
|
||||
rt_uint32_t flags = 0;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
RT_ASSERT(intf->user_data != RT_NULL);
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
|
||||
LOG_D("rt_usbh_storage_stop");
|
||||
|
||||
/* get storage instance from interface instance */
|
||||
stor = (ustor_t)intf->user_data;
|
||||
|
||||
rt_udisk_stop(intf);
|
||||
intf->user_data = NULL;
|
||||
|
||||
while(1)
|
||||
{
|
||||
if(stor->ref)
|
||||
{
|
||||
os_sleep_ms(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
flags = disable_irq();
|
||||
/* free storage instance */
|
||||
if(stor != RT_NULL) rt_free(stor);
|
||||
enable_irq(flags);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will register mass storage class driver to the usb class driver manager.
|
||||
* and it should be invoked in the usb system initialization.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
ucd_t rt_usbh_class_driver_storage(void)
|
||||
{
|
||||
storage_driver.class_code = 0x08; //USB_CLASS_MASS_STORAGE
|
||||
|
||||
storage_driver.enable = rt_usbh_storage_enable;
|
||||
storage_driver.disable = rt_usbh_storage_disable;
|
||||
|
||||
return &storage_driver;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
65
sdk/lib/bus/rttusb/usbhost/class/mass.h
Normal file
65
sdk/lib/bus/rttusb/usbhost/class/mass.h
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-12-12 Yi Qiu first version
|
||||
*/
|
||||
|
||||
#ifndef __MASS_H__
|
||||
#define __MASS_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "dev.h"
|
||||
#include "devid.h"
|
||||
#include "diskio.h"
|
||||
#include "ff.h"
|
||||
|
||||
|
||||
#define MAX_PARTITION_COUNT 4
|
||||
#define SECTOR_SIZE 512
|
||||
|
||||
struct ustor_data
|
||||
{
|
||||
struct uhintf* intf;
|
||||
int udisk_id;
|
||||
const char path;
|
||||
};
|
||||
|
||||
struct ustor_device
|
||||
{
|
||||
rt_uint32_t type;
|
||||
fatfs_disk_status status;
|
||||
fatfs_disk_initialize init;
|
||||
fatfs_disk_read read;
|
||||
fatfs_disk_write write;
|
||||
fatfs_disk_ioctl ioctl;
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
struct ustor
|
||||
{
|
||||
upipe_t pipe_in;
|
||||
upipe_t pipe_out;
|
||||
rt_uint32_t capicity[2];
|
||||
|
||||
struct ustor_device dev[MAX_PARTITION_COUNT];
|
||||
rt_uint8_t dev_cnt;
|
||||
rt_uint32_t ref;
|
||||
};
|
||||
typedef struct ustor* ustor_t;
|
||||
|
||||
rt_err_t rt_usbh_storage_get_max_lun(struct uhintf* intf, rt_uint8_t* max_lun);
|
||||
rt_err_t rt_usbh_storage_reset(struct uhintf* intf);
|
||||
rt_err_t rt_usbh_storage_read10(struct uhintf* intf, rt_uint8_t *buffer,
|
||||
rt_uint32_t sector, rt_size_t count, int timeout);
|
||||
rt_err_t rt_usbh_storage_write10(struct uhintf* intf, rt_uint8_t *buffer,
|
||||
rt_uint32_t sector, rt_size_t count, int timeout);
|
||||
rt_err_t rt_usbh_storage_request_sense(struct uhintf* intf, rt_uint8_t* buffer);
|
||||
rt_err_t rt_usbh_storage_test_unit_ready(struct uhintf* intf);
|
||||
rt_err_t rt_usbh_storage_inquiry(struct uhintf* intf, rt_uint8_t* buffer);
|
||||
rt_err_t rt_usbh_storage_get_capacity(struct uhintf* intf, rt_uint8_t* buffer);
|
||||
|
||||
#endif
|
||||
349
sdk/lib/bus/rttusb/usbhost/class/quectel.c
Normal file
349
sdk/lib/bus/rttusb/usbhost/class/quectel.c
Normal file
@@ -0,0 +1,349 @@
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
#include "quectel.h"
|
||||
#include "cdc.h"
|
||||
#include "rndis.h"
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/netdb.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/tcpip.h"
|
||||
#include "netif/ethernetif.h"
|
||||
#include "lib/common/sysevt.h"
|
||||
|
||||
#ifdef RT_USBH_VENDOR_QUECTEL
|
||||
|
||||
#define USB_VENDOR_ID_QUECTEL 0x2C7C
|
||||
#define USB_PRODUCT_ID_QUECTEL 0x0903 // EC801E-CN
|
||||
|
||||
#define QUECTEL_ATCMD_BUFF_SIZE 64
|
||||
|
||||
static char recv_str[QUECTEL_ATCMD_BUFF_SIZE];
|
||||
|
||||
static struct uclass_driver quectel_driver;
|
||||
|
||||
static rt_bool_t send_recv_atcmd_check(void *context, const char *send_str,
|
||||
const char *check_str, char *ret_str)
|
||||
{
|
||||
struct usb_quectel_at *quectel_at = context;
|
||||
uinst_t device = quectel_at->device;
|
||||
int recv_size;
|
||||
char *recv_str = NULL;
|
||||
rt_bool_t pass = RT_FALSE;
|
||||
|
||||
os_sprintf((char *)quectel_at->at_cmd_buff, send_str);
|
||||
rt_usb_hcd_pipe_xfer(device->hcd, quectel_at->pipe_out,
|
||||
quectel_at->at_cmd_buff, os_strlen(send_str), 0);
|
||||
os_sleep_ms(10); // 需要点延迟给LTE模组反应
|
||||
do {
|
||||
// 每次读取会清除缓存
|
||||
os_memset(quectel_at->at_cmd_buff, 0, QUECTEL_ATCMD_BUFF_SIZE);
|
||||
recv_size = rt_usb_hcd_pipe_xfer(device->hcd, quectel_at->pipe_in,
|
||||
quectel_at->at_cmd_buff, QUECTEL_ATCMD_BUFF_SIZE, 10);
|
||||
if (recv_size > 0) {
|
||||
recv_str = os_strstr(quectel_at->at_cmd_buff, check_str);
|
||||
if (recv_str != NULL) {
|
||||
pass = RT_TRUE;
|
||||
if (ret_str != NULL && recv_size > os_strlen(check_str)) {
|
||||
// ret_str存在说明需要获取返回结果做额外判断,复制到函数外面
|
||||
os_memcpy(ret_str, recv_str + os_strlen(check_str),
|
||||
recv_size - os_strlen(check_str));
|
||||
ret_str[recv_size - os_strlen(check_str)] = '\0'; // 字符串结束符
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (recv_size > 0);
|
||||
|
||||
return pass;
|
||||
}
|
||||
|
||||
static void quectel_at_recv(void *context)
|
||||
{
|
||||
struct usb_quectel_at *quectel_at = context;
|
||||
|
||||
while (1) {
|
||||
if (quectel_at->retry > 3) {
|
||||
quectel_at->retry = 0;
|
||||
quectel_at->state = QUECTEL_STATE_UNKNOW;
|
||||
}
|
||||
// os_printf("recv state:%d\r\n", quectel_at->state);
|
||||
switch (quectel_at->state) {
|
||||
case QUECTEL_STATE_UNKNOW:
|
||||
quectel_at->retry = 0;
|
||||
quectel_at->state = QUECTEL_STATE_CHECK_AT_STATUS;
|
||||
break;
|
||||
case QUECTEL_STATE_CHECK_AT_STATUS:
|
||||
// AT查询模块是否工作启动初始化流程
|
||||
if (send_recv_atcmd_check(quectel_at, "AT\r\n", "OK", NULL) == RT_TRUE) {
|
||||
quectel_at->retry = 0;
|
||||
quectel_at->state = QUECTEL_STATE_CHECK_SIM_STATUS;
|
||||
os_printf("AT ready\r\n");
|
||||
} else {
|
||||
quectel_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case QUECTEL_STATE_CHECK_SIM_STATUS:
|
||||
if (send_recv_atcmd_check(quectel_at, "AT+CPIN?\r\n", "+CPIN: READY", NULL) == RT_TRUE) {
|
||||
quectel_at->retry = 0;
|
||||
quectel_at->state = QUECTEL_STATE_CHECK_CS_STATUS;
|
||||
os_printf("SIM ready\r\n");
|
||||
} else {
|
||||
quectel_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case QUECTEL_STATE_CHECK_CS_STATUS:
|
||||
// AT+CREG查询CS域网络注册状态
|
||||
if (send_recv_atcmd_check(quectel_at, "AT+CREG?\r\n", "+CREG: 0,1", NULL) == RT_TRUE) {
|
||||
quectel_at->retry = 0;
|
||||
quectel_at->state = QUECTEL_STATE_CHECK_PS_STATUS;
|
||||
os_printf("PS ready\r\n");
|
||||
} else {
|
||||
quectel_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case QUECTEL_STATE_CHECK_PS_STATUS:
|
||||
// AT+CEREG查询EPS域网络注册状态
|
||||
if (send_recv_atcmd_check(quectel_at, "AT+CEREG?\r\n", "+CEREG: 0,1", NULL) == RT_TRUE) {
|
||||
quectel_at->retry = 0;
|
||||
quectel_at->state = QUECTEL_STATE_CHECK_USBNET_STATUS;
|
||||
os_printf("CS ready\r\n");
|
||||
} else {
|
||||
quectel_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case QUECTEL_STATE_CHECK_USBNET_STATUS:
|
||||
// 查询usb网卡接口
|
||||
if (send_recv_atcmd_check(quectel_at, "AT+QCFG=\"usbnet\"\r\n", "+QCFG: \"usbnet\",", recv_str) == RT_TRUE) {
|
||||
if (os_atoi(recv_str) == 1) {
|
||||
quectel_at->retry = 0;
|
||||
quectel_at->state = QUECTEL_STATE_CONFIG_USBNET_STATUS;
|
||||
os_printf("USBNET is ECM\r\n");
|
||||
} else {
|
||||
quectel_at->retry = 0;
|
||||
quectel_at->state = QUECTEL_STATE_CONFIG_PDP_CONTEXT;
|
||||
os_printf("USBNET is RNDIS\r\n");
|
||||
}
|
||||
} else {
|
||||
quectel_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case QUECTEL_STATE_CONFIG_USBNET_STATUS:
|
||||
// ecm网卡,发送命令切换到rndis网卡
|
||||
if (send_recv_atcmd_check(quectel_at, "AT+QCFG=\"usbnet\",3\r\n", "OK", NULL) == RT_TRUE) {
|
||||
quectel_at->retry = 0;
|
||||
quectel_at->state = QUECTEL_STATE_POWERDOWN;
|
||||
os_printf("switch to RNDIS\r\n");
|
||||
} else {
|
||||
quectel_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case QUECTEL_STATE_CONFIG_PDP_CONTEXT:
|
||||
// AT+QICSGP设置场景参数
|
||||
if (send_recv_atcmd_check(quectel_at, "AT+QICSGP=1,1,\"UNINET\",\"\",\"\",1\r\n", "OK", NULL) == RT_TRUE) {
|
||||
quectel_at->retry = 0;
|
||||
quectel_at->state = QUECTEL_STATE_ACTIVE_PDP_CONTEXT;
|
||||
os_printf("PDP config\r\n");
|
||||
} else {
|
||||
quectel_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case QUECTEL_STATE_ACTIVE_PDP_CONTEXT:
|
||||
// AT+QIACT激活PDP场景
|
||||
if (send_recv_atcmd_check(quectel_at, "AT+QIACT=1\r\n", "OK", NULL) == RT_TRUE) {
|
||||
quectel_at->retry = 0;
|
||||
quectel_at->state = QUECTEL_STATE_CHECK_IP_STATUS;
|
||||
os_printf("PDP ready\r\n");
|
||||
} else {
|
||||
quectel_at->retry++;
|
||||
if (quectel_at->retry > 3) {
|
||||
quectel_at->retry = 0;
|
||||
quectel_at->state = QUECTEL_STATE_DEACTIVE_PDP_CONTEXT;
|
||||
}
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case QUECTEL_STATE_CHECK_IP_STATUS:
|
||||
// AT+QIACT查询激活PDP场景和IP
|
||||
if (send_recv_atcmd_check(quectel_at, "AT+QIACT?\r\n", "+QIACT: 1,1,1,", recv_str) == RT_TRUE) {
|
||||
quectel_at->retry = 0;
|
||||
quectel_at->state = QUECTEL_STATE_CONNECT_USB_ADAPTER;
|
||||
os_printf("Get IP: %s", recv_str);
|
||||
os_printf("PDP actived\r\n");
|
||||
} else {
|
||||
quectel_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case QUECTEL_STATE_CONNECT_USB_ADAPTER:
|
||||
// AT+QNETDEVCTL连接USB网卡
|
||||
if (send_recv_atcmd_check(quectel_at, "AT+QNETDEVCTL=1,1,1\r\n", "OK", recv_str) == RT_TRUE) {
|
||||
quectel_at->retry = 0;
|
||||
quectel_at->state = QUECTEL_STATE_INITIALIZED;
|
||||
SYSEVT_NEW_LTE_EVT(SYSEVT_LTE_CONNECTED, 0);
|
||||
os_printf("network conneted\r\n");
|
||||
} else {
|
||||
quectel_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case QUECTEL_STATE_INITIALIZED:
|
||||
// 定时10s不知道干什么好呢
|
||||
os_sleep(10);
|
||||
break;
|
||||
case QUECTEL_STATE_DEACTIVE_PDP_CONTEXT:
|
||||
// 失败后反激活模组
|
||||
if (send_recv_atcmd_check(quectel_at, "AT+QIDEACT=1\r\n", "OK", NULL) == RT_TRUE) {
|
||||
quectel_at->retry = 0;
|
||||
quectel_at->state = QUECTEL_STATE_CHECK_SIM_STATUS;
|
||||
os_printf("PDP deactived\r\n");
|
||||
} else {
|
||||
quectel_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case QUECTEL_STATE_POWERDOWN:
|
||||
if (send_recv_atcmd_check(quectel_at, "AT+QPOWD\r\n", "POWERED DOWN", NULL) == RT_TRUE) {
|
||||
quectel_at->retry = 0;
|
||||
os_printf("LTE power down\r\n");
|
||||
os_sleep(2);
|
||||
mcu_reset();
|
||||
} else {
|
||||
quectel_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
os_printf("at task end\r\n");
|
||||
}
|
||||
|
||||
static rt_err_t rt_usbh_quectel_enable(void *arg)
|
||||
{
|
||||
rt_err_t ret = RET_OK;
|
||||
struct uhintf *intf = arg;
|
||||
uhcd_t hcd = NULL;
|
||||
uep_desc_t ep_desc = NULL;
|
||||
struct ustring_descriptor str_desc __attribute__((aligned(4)));
|
||||
struct usb_quectel_at *quectel_at = NULL;
|
||||
struct usb_cdc_line_coding line_coding;
|
||||
upipe_t pipe;
|
||||
rt_uint8_t ep_index, i;
|
||||
|
||||
if (intf == NULL) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
hcd = intf->device->hcd;
|
||||
if (intf->device->dev_desc.idProduct == USB_PRODUCT_ID_QUECTEL) {
|
||||
// 怎么有两个AT串口,先直接用第3个接口判断好了
|
||||
if (intf->intf_desc->bInterfaceNumber != 3) {
|
||||
return RET_ERR;
|
||||
}
|
||||
// 通过字符描述符判断哪个是主调试串口
|
||||
ret = rt_usbh_get_string_descriptor(intf->device, intf->intf_desc->iInterface,
|
||||
&str_desc, sizeof(struct ustring_descriptor));
|
||||
if (ret != RET_OK) {
|
||||
return ret;
|
||||
}
|
||||
for (i = 0; i < str_desc.bLength; i += 2) { // 暂时没支持UNICODE的打印,默认ASCII可以隔一个打印
|
||||
_os_printf("%c", str_desc.String[i / 2]);
|
||||
}
|
||||
_os_printf("\r\n");
|
||||
|
||||
os_printf("quectel_at\r\n");
|
||||
quectel_at = (struct usb_quectel_at *)os_zalloc(sizeof(struct usb_quectel_at));
|
||||
if (quectel_at == NULL) {
|
||||
os_printf("quectel at alloc fail\r\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
// 按照命令预期回复长度对齐预留长度
|
||||
quectel_at->at_cmd_buff = os_malloc(QUECTEL_ATCMD_BUFF_SIZE);
|
||||
if (quectel_at->at_cmd_buff == NULL) {
|
||||
os_printf("at_cmd_buff alloc fail\r\n");
|
||||
os_free(quectel_at);
|
||||
return -ENOMEM;
|
||||
}
|
||||
quectel_at->device = intf->device;
|
||||
intf->user_data = quectel_at;
|
||||
// 顺便注册devid
|
||||
dev_register(HG_USB_AT_DEVID, (struct dev_obj *)quectel_at);
|
||||
|
||||
for (ep_index = 0; ep_index < intf->intf_desc->bNumEndpoints; ++ep_index) {
|
||||
rt_usbh_get_endpoint_descriptor(intf->intf_desc, ep_index, &ep_desc);
|
||||
if (ep_desc == NULL) {
|
||||
return RET_ERR;
|
||||
}
|
||||
|
||||
if ((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_BULK)
|
||||
continue;
|
||||
|
||||
if (rt_usb_hcd_alloc_pipe(intf->device->hcd, &pipe, intf->device, ep_desc) != RT_EOK) {
|
||||
rt_kprintf("alloc pipe failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
rt_usb_instance_add_pipe(intf->device, pipe);
|
||||
if ((ep_desc->bEndpointAddress & USB_DIR_MASK) == USB_DIR_IN) {
|
||||
quectel_at->pipe_in = pipe;
|
||||
} else {
|
||||
quectel_at->pipe_out = pipe;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取串口参数
|
||||
os_memset(&line_coding, 0, sizeof(line_coding));
|
||||
rt_usbh_cdc_get_line_coding(intf->device, intf->intf_desc->bInterfaceNumber, &line_coding);
|
||||
os_printf("Serial: %d %d %d %d\r\n",
|
||||
line_coding.dwDTERate, line_coding.bCharFormat, line_coding.bParityType, line_coding.bDataBits);
|
||||
|
||||
rt_thread_init(&quectel_at->recv_task, "at_recv", quectel_at_recv, quectel_at,
|
||||
NULL, 512, OS_TASK_PRIORITY_BELOW_NORMAL, 20);
|
||||
rt_thread_startup(&quectel_at->recv_task);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static rt_err_t rt_usbh_quectel_disable(void *arg)
|
||||
{
|
||||
struct uhintf *intf = arg;
|
||||
uhcd_t hcd = NULL;
|
||||
struct usb_quectel_at *quectel_at = NULL;
|
||||
|
||||
if (intf == NULL) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
quectel_at = intf->user_data;
|
||||
if (quectel_at) {
|
||||
hcd = intf->device->hcd;
|
||||
dev_unregister((struct dev_obj *)quectel_at);
|
||||
rt_thread_detach(&quectel_at->recv_task);
|
||||
os_free(quectel_at->at_cmd_buff);
|
||||
os_free(quectel_at);
|
||||
}
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ucd_t rt_usbh_class_driver_quectel(void)
|
||||
{
|
||||
quectel_driver.class_code = USB_CLASS_VEND_SPECIFIC;
|
||||
quectel_driver.vendor_id = USB_VENDOR_ID_QUECTEL;
|
||||
|
||||
quectel_driver.enable = rt_usbh_quectel_enable;
|
||||
quectel_driver.disable = rt_usbh_quectel_disable;
|
||||
|
||||
return &quectel_driver;
|
||||
}
|
||||
|
||||
#endif
|
||||
42
sdk/lib/bus/rttusb/usbhost/class/quectel.h
Normal file
42
sdk/lib/bus/rttusb/usbhost/class/quectel.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef __CLASS_QUECTEL_H__
|
||||
#define __CLASS_QUECTEL_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
enum quectel_state {
|
||||
QUECTEL_STATE_UNKNOW,
|
||||
QUECTEL_STATE_CHECK_AT_STATUS,
|
||||
QUECTEL_STATE_CHECK_SIM_STATUS,
|
||||
QUECTEL_STATE_CHECK_CS_STATUS,
|
||||
QUECTEL_STATE_CHECK_PS_STATUS,
|
||||
QUECTEL_STATE_CHECK_USBNET_STATUS,
|
||||
QUECTEL_STATE_CONFIG_USBNET_STATUS,
|
||||
QUECTEL_STATE_CONFIG_PDP_CONTEXT,
|
||||
QUECTEL_STATE_ACTIVE_PDP_CONTEXT,
|
||||
QUECTEL_STATE_CHECK_IP_STATUS,
|
||||
QUECTEL_STATE_CONNECT_USB_ADAPTER,
|
||||
QUECTEL_STATE_INITIALIZED,
|
||||
QUECTEL_STATE_DEACTIVE_PDP_CONTEXT,
|
||||
QUECTEL_STATE_POWERDOWN,
|
||||
};
|
||||
|
||||
struct usb_quectel_at {
|
||||
struct dev_obj dev;
|
||||
void *device;
|
||||
struct rt_thread recv_task;
|
||||
rt_uint8_t *at_cmd_buff;
|
||||
rt_uint32_t state;
|
||||
upipe_t pipe_in;
|
||||
upipe_t pipe_out;
|
||||
rt_uint8_t retry;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
439
sdk/lib/bus/rttusb/usbhost/class/rndis.c
Normal file
439
sdk/lib/bus/rttusb/usbhost/class/rndis.c
Normal file
@@ -0,0 +1,439 @@
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
#include "rndis.h"
|
||||
#include "cdc.h"
|
||||
#include "hal/usb_device.h"
|
||||
#include "lib/usb/usb_device_rndis.h"
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/netdb.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/tcpip.h"
|
||||
#include "netif/ethernetif.h"
|
||||
|
||||
#if defined(RT_USBH_WIRELESS) && defined(RT_USBH_WIRELESS_RNDIS)
|
||||
|
||||
static rt_err_t rt_rndis_msg_send_recv(struct usb_rndis *rndis, rt_uint8_t *send_buf, rt_uint32_t send_len,
|
||||
rt_uint8_t *recv_buf, rt_uint32_t recv_size, rt_uint32_t *recv_len)
|
||||
{
|
||||
USBD_CDC_RNDIS_MsgTypeDef *msg = (USBD_CDC_RNDIS_MsgTypeDef *)recv_buf;
|
||||
uinst_t device = rndis->device;
|
||||
int ret = 0;
|
||||
rt_uint32_t rndis_avial[2 + (USB_RX_BUFF_RESERVE_SIZE / 4)] = {0}; // 防止越界
|
||||
rt_uint32_t req_type = msg->Ctrl.MsgType;
|
||||
|
||||
ret = rt_usbh_cdc_send_command(device, send_buf, send_len);
|
||||
if (ret == send_len) {
|
||||
__retry:
|
||||
/* waite for the interrupt ep */
|
||||
ret = rt_usb_hcd_pipe_xfer(device->hcd, rndis->pipe_int, rndis_avial, 8, USB_TIMEOUT_BASIC);
|
||||
if (ret == 8 && rndis_avial[0] == 1 && rndis_avial[1] == 0) {
|
||||
ret = rt_usbh_cdc_get_response(device, recv_buf, recv_size);
|
||||
if (ret > 0) {
|
||||
*recv_len = ret;
|
||||
if (msg->Resp.MsgType != (0x80000000UL | req_type) ||
|
||||
msg->Resp.ReqId != rndis->req_id ||
|
||||
msg->Resp.Status != CDC_RNDIS_STATUS_SUCCESS) {
|
||||
//os_printf("retry,%d,%d,%d,%d,%d\r\n", msg->Resp.MsgType, req_type, msg->Resp.ReqId, rndis->req_id, msg->Resp.Status);
|
||||
goto __retry;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
}
|
||||
ret = RET_ERR;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static rt_err_t rt_rndis_msg_init(struct usb_rndis *rndis)
|
||||
{
|
||||
USBD_CDC_RNDIS_MsgTypeDef *msg = (USBD_CDC_RNDIS_MsgTypeDef *)rndis->msg_buffer;
|
||||
rt_uint32_t recv_len = 0;
|
||||
int ret = 0;
|
||||
|
||||
// 清理buffer空间
|
||||
os_memset(rndis->msg_buffer, 0, sizeof(USBD_CDC_RNDIS_InitMsgTypeDef));
|
||||
msg->Init.MsgType = CDC_RNDIS_INITIALIZE_MSG_ID;
|
||||
msg->Init.MsgLength = sizeof(USBD_CDC_RNDIS_InitMsgTypeDef);
|
||||
msg->Init.ReqId = ++rndis->req_id;
|
||||
msg->Init.MajorVersion = CDC_RNDIS_VERSION_MAJOR;
|
||||
msg->Init.MinorVersion = CDC_RNDIS_VERSION_MINOR;
|
||||
msg->Init.MaxTransferSize = 2048;
|
||||
ret = rt_rndis_msg_send_recv(rndis, rndis->msg_buffer, msg->Init.MsgLength,
|
||||
rndis->msg_buffer, 128, &recv_len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static rt_err_t rt_rndis_msg_keepalive(struct usb_rndis *rndis)
|
||||
{
|
||||
USBD_CDC_RNDIS_MsgTypeDef *msg = (USBD_CDC_RNDIS_MsgTypeDef *)rndis->msg_buffer;
|
||||
rt_uint32_t recv_len = 0;
|
||||
int ret = 0;
|
||||
|
||||
os_memset(rndis->msg_buffer, 0, sizeof(USBD_CDC_RNDIS_KpAliveMsgTypeDef));
|
||||
msg->KpAlive.MsgType = CDC_RNDIS_KEEPALIVE_MSG_ID;
|
||||
msg->KpAlive.MsgLength = sizeof(USBD_CDC_RNDIS_KpAliveMsgTypeDef);
|
||||
msg->KpAlive.ReqId = ++rndis->req_id;
|
||||
ret = rt_rndis_msg_send_recv(rndis, rndis->msg_buffer, msg->KpAlive.MsgLength,
|
||||
rndis->msg_buffer, 128, &recv_len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
rt_err_t rt_rndis_msg_query(struct usb_rndis *rndis, rt_uint32_t oid, rt_uint8_t *buff, rt_uint32_t *len)
|
||||
{
|
||||
USBD_CDC_RNDIS_MsgTypeDef *msg = (USBD_CDC_RNDIS_MsgTypeDef *)rndis->msg_buffer;
|
||||
rt_uint32_t recv_len = 0;
|
||||
int ret = 0;
|
||||
|
||||
os_memset(rndis->msg_buffer, 0, sizeof(USBD_CDC_RNDIS_QueryMsgTypeDef));
|
||||
msg->Query.MsgType = CDC_RNDIS_QUERY_MSG_ID;
|
||||
msg->Query.MsgLength = sizeof(USBD_CDC_RNDIS_QueryMsgTypeDef);
|
||||
msg->Query.RequestId = ++rndis->req_id;
|
||||
msg->Query.Oid = oid;
|
||||
msg->Query.InfoBufLength = 0;
|
||||
msg->Query.InfoBufOffset = 20;
|
||||
msg->Query.DeviceVcHandle = 0;
|
||||
ret = rt_rndis_msg_send_recv(rndis, rndis->msg_buffer, msg->Query.MsgLength,
|
||||
rndis->msg_buffer, 128, &recv_len);
|
||||
if (ret == RET_OK) {
|
||||
os_memcpy(buff, msg->QueryCplt.InfoBuf, msg->QueryCplt.InfoBufLength);
|
||||
*len = msg->QueryCplt.InfoBufLength;
|
||||
return RET_OK;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
rt_err_t rt_rndis_msg_set(struct usb_rndis *rndis, rt_uint32_t oid, rt_uint8_t *buff, rt_uint32_t len)
|
||||
{
|
||||
USBD_CDC_RNDIS_MsgTypeDef *msg = (USBD_CDC_RNDIS_MsgTypeDef *)rndis->msg_buffer;
|
||||
uinst_t device = rndis->device;
|
||||
rt_uint32_t recv_len = 0;
|
||||
rt_uint32_t rndis_avial[2 + (USB_RX_BUFF_RESERVE_SIZE / 4)] = {0}; // 防止越界
|
||||
int ret = 0;
|
||||
|
||||
os_memset(rndis->msg_buffer, 0, sizeof(USBD_CDC_RNDIS_SetMsgTypeDef));
|
||||
msg->Set.MsgType = CDC_RNDIS_SET_MSG_ID;
|
||||
msg->Set.MsgLength = sizeof(USBD_CDC_RNDIS_SetMsgTypeDef) + len;
|
||||
msg->Set.ReqId = ++rndis->req_id;
|
||||
msg->Set.Oid = oid;
|
||||
msg->Set.InfoBufLength = 0;
|
||||
msg->Set.InfoBufOffset = 20;
|
||||
msg->Set.DeviceVcHandle = 0;
|
||||
ret = rt_rndis_msg_send_recv(rndis, rndis->msg_buffer, msg->Set.MsgLength,
|
||||
rndis->msg_buffer, 128, &recv_len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void rt_usbh_rndis_keepalive_timer(void *args)
|
||||
{
|
||||
struct usb_rndis *rndis = args;
|
||||
|
||||
// os_printf("keep alive\r\n");
|
||||
rt_rndis_msg_keepalive(rndis);
|
||||
os_timer_start(&rndis->keepalive_timer, 5000);
|
||||
}
|
||||
|
||||
static rt_err_t rt_usbh_rndis_open(struct netdev *ndev, netdev_input_cb input_cb, netdev_event_cb evt_cb, void *priv)
|
||||
{
|
||||
rt_uint32_t flags;
|
||||
struct usb_rndis *rndis = container_of(ndev, struct usb_rndis, ndev);
|
||||
|
||||
flags = disable_irq();
|
||||
rndis->input_cb = input_cb;
|
||||
rndis->input_priv = priv;
|
||||
enable_irq(flags);
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
static rt_err_t rt_usbh_rndis_ioctl(struct netdev *ndev, rt_uint32_t cmd, rt_uint32_t param1, rt_uint32_t param2)
|
||||
{
|
||||
struct usb_rndis *rndis = container_of(ndev, struct usb_rndis, ndev);
|
||||
|
||||
switch (cmd) {
|
||||
case NETDEV_IOCTL_GET_ADDR:
|
||||
os_memcpy((rt_uint8_t *)param1, rndis->mac, 6);
|
||||
break;
|
||||
default:
|
||||
return -ENOTSUPP;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
// 注意:send_data直接将data指针前移获取预留的空间
|
||||
// 必须确认调用方是skb申请有预留足够空间,否则存在风险
|
||||
// 由于USB DMA需要4字节对齐,skb->data经过处理后,IP包头不是4字节对齐,不能前移处理
|
||||
static rt_err_t rt_usbh_rndis_send_data(struct netdev *ndev, rt_uint8_t *p_data, rt_uint32_t size)
|
||||
{
|
||||
struct usb_rndis *rndis = container_of(ndev, struct usb_rndis, ndev);
|
||||
uinst_t device = rndis->device;
|
||||
USBD_CDC_RNDIS_PacketMsgTypeDef *packet = NULL;
|
||||
|
||||
if (p_data == NULL || size == 0) {
|
||||
return -EINVAL;
|
||||
}
|
||||
if (!rndis->ready) {
|
||||
// usb没准备好就不发包
|
||||
os_printf("rndis send no ready\r\n");
|
||||
return -EIO;
|
||||
}
|
||||
packet = (USBD_CDC_RNDIS_PacketMsgTypeDef *)os_malloc(size + sizeof(USBD_CDC_RNDIS_PacketMsgTypeDef));
|
||||
if (packet == NULL) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
os_memset(packet, 0, sizeof(USBD_CDC_RNDIS_PacketMsgTypeDef));
|
||||
packet->MsgType = CDC_RNDIS_PACKET_MSG_ID;
|
||||
packet->MsgLength = size + 44;
|
||||
packet->DataOffset = 36; // 明明是44,但是抓包看全是36,不知道为什么
|
||||
packet->DataLength = size;
|
||||
hw_memcpy(packet + 1, p_data, size);
|
||||
rt_usb_hcd_pipe_xfer(device->hcd, rndis->pipe_out, packet, packet->MsgLength, 1000);
|
||||
os_free(packet);
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
// 注意:USB驱动大部分不支持scatter发送,因此需要额外拷贝
|
||||
// 调用方多数来自于LWIP,数据量可能小一点
|
||||
static rt_err_t rt_usbh_rndis_send_scatter_data(struct netdev *ndev, scatter_data *data, rt_uint32_t count)
|
||||
{
|
||||
struct usb_rndis *rndis = container_of(ndev, struct usb_rndis, ndev);
|
||||
uinst_t device = rndis->device;
|
||||
USBD_CDC_RNDIS_PacketMsgTypeDef *packet = NULL;
|
||||
rt_uint8_t *p_data;
|
||||
rt_uint32_t size = 0;
|
||||
rt_uint32_t offset = 0;
|
||||
rt_uint32_t i;
|
||||
|
||||
if (data == NULL || count == 0) {
|
||||
return -EINVAL;
|
||||
}
|
||||
if (!rndis->ready) {
|
||||
// usb没准备好就不发包
|
||||
os_printf("rndis scatter send no ready\r\n");
|
||||
return -EIO;
|
||||
}
|
||||
for (i = 0; i < count; ++i) size += data[i].size;
|
||||
packet = (USBD_CDC_RNDIS_PacketMsgTypeDef *)os_malloc(size + sizeof(USBD_CDC_RNDIS_PacketMsgTypeDef));
|
||||
if (packet == NULL) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
os_memset(packet, 0, sizeof(USBD_CDC_RNDIS_PacketMsgTypeDef));
|
||||
packet->MsgType = CDC_RNDIS_PACKET_MSG_ID;
|
||||
packet->MsgLength = size + 44;
|
||||
packet->DataOffset = 36; // 明明是44,但是抓包看全是36,不知道为什么
|
||||
packet->DataLength = size;
|
||||
// 拷贝scatter中数据
|
||||
p_data = (rt_uint8_t *)(packet + 1);
|
||||
for (i = 0; i < count; ++i) {
|
||||
hw_memcpy(p_data + offset, data[i].addr, data[i].size);
|
||||
offset += data[i].size;
|
||||
}
|
||||
rt_usb_hcd_pipe_xfer(device->hcd, rndis->pipe_out, packet, packet->MsgLength, 1000);
|
||||
os_free(packet);
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
void rt_usbh_rndis_network_init(struct usb_rndis *rndis)
|
||||
{
|
||||
struct netdev *ndev = &rndis->ndev;
|
||||
rt_uint8_t mac[8]; // 有些设备返回mac会多些字节
|
||||
rt_uint32_t recv_len = 0;
|
||||
rt_uint32_t packet_filter = CDC_RNDIS_PACKET_DIRECTED | \
|
||||
CDC_RNDIS_PACKET_ALL_MULTICAST | \
|
||||
CDC_RNDIS_PACKET_BROADCAST | \
|
||||
CDC_RNDIS_PACKET_PROMISCUOUS;
|
||||
|
||||
if (ndev) {
|
||||
// tcpip_init(NULL, NULL); // 外面wifi应该初始化过了
|
||||
rt_rndis_msg_query(rndis, OID_802_3_CURRENT_ADDRESS, mac, &recv_len);
|
||||
os_memcpy(rndis->mac, mac, 6);
|
||||
rt_rndis_msg_set(rndis, OID_GEN_CURRENT_PACKET_FILTER, (rt_uint8_t *)&packet_filter, 4);
|
||||
lwip_netif_add(ndev, "l0", NULL, NULL, NULL);
|
||||
// 利用事件驱动
|
||||
os_printf("add l0 interface!\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
static const struct netdev_hal_ops rndis_ops = {
|
||||
.open = rt_usbh_rndis_open,
|
||||
.close = NULL,
|
||||
.ioctl = rt_usbh_rndis_ioctl,
|
||||
.send_data = rt_usbh_rndis_send_data,
|
||||
.send_scatter_data = rt_usbh_rndis_send_scatter_data,
|
||||
};
|
||||
|
||||
rt_err_t rt_usbh_rndis_attach(struct usb_rndis *rndis)
|
||||
{
|
||||
if (rndis == NULL) {
|
||||
return -EIO;
|
||||
}
|
||||
// 绑定处理函数
|
||||
rndis->ndev.dev.ops = (const struct devobj_ops *)&rndis_ops;
|
||||
// 顺便注册devid
|
||||
return dev_register(HG_LTE_RNDIS_DEVID, (struct dev_obj *)rndis);
|
||||
}
|
||||
|
||||
rt_err_t rt_usbh_rndis_attach2(void)
|
||||
{
|
||||
struct usb_rndis *rndis = (struct usb_rndis *)os_zalloc(sizeof(struct usb_rndis));
|
||||
|
||||
ASSERT(rndis);
|
||||
rndis->ndev.dev.ops = (const struct devobj_ops *)&rndis_ops;
|
||||
return dev_register(HG_LTE_RNDIS_DEVID, (struct dev_obj *)rndis);
|
||||
}
|
||||
|
||||
void rndis_data_recv(void *context)
|
||||
{
|
||||
struct usb_rndis *rndis = context;
|
||||
uinst_t device = rndis->device;
|
||||
USBD_CDC_RNDIS_PacketMsgTypeDef *packet = NULL;
|
||||
rt_uint32_t flags;
|
||||
int recv_size;
|
||||
int offset;
|
||||
int shift;
|
||||
netdev_input_cb input_cb;
|
||||
void *input_priv;
|
||||
rt_uint32_t target_copyLength = 0;
|
||||
|
||||
while (1) {
|
||||
if (!rndis->ready) {
|
||||
os_sleep_ms(10); // 需要有点延迟给线程,不然占住不释放
|
||||
continue;
|
||||
}
|
||||
offset = 0;
|
||||
recv_size = rt_usb_hcd_pipe_xfer(device->hcd, rndis->pipe_in, rndis->data_buffer, 2048, 0);
|
||||
while (recv_size > 0) {
|
||||
shift = offset % 4;
|
||||
if (shift != 0) {
|
||||
os_memmove(rndis->data_buffer + offset - shift , rndis->data_buffer + offset, recv_size);
|
||||
offset -= shift;
|
||||
}
|
||||
/*上一个usb包余下的新rndis包数据*/
|
||||
if (rndis->ts_saveLength) {
|
||||
/*暂存的rndis包里有存储到该包的包头信息*/
|
||||
if (rndis->ts_saveLength < sizeof(USBD_CDC_RNDIS_PacketMsgTypeDef)) {
|
||||
/*先拷贝rndis包头大小的长度,确保能读取到该rndis包的长度信息*/
|
||||
os_memcpy(rndis->ts_buffer + rndis->ts_saveLength,
|
||||
rndis->data_buffer + offset,
|
||||
MIN(recv_size, sizeof(USBD_CDC_RNDIS_PacketMsgTypeDef)));
|
||||
rndis->ts_saveLength += MIN(recv_size, sizeof(USBD_CDC_RNDIS_PacketMsgTypeDef));
|
||||
offset += MIN(recv_size, sizeof(USBD_CDC_RNDIS_PacketMsgTypeDef));
|
||||
recv_size -= MIN(recv_size, sizeof(USBD_CDC_RNDIS_PacketMsgTypeDef));
|
||||
}
|
||||
packet = (USBD_CDC_RNDIS_PacketMsgTypeDef *)(rndis->ts_buffer);
|
||||
} else {
|
||||
packet = (USBD_CDC_RNDIS_PacketMsgTypeDef *)(rndis->data_buffer + offset);
|
||||
/*没有余留的rndis包,包类型也不对,则丢掉该包,可能丢掉一整个聚合的包,正常不应该出现这种情况*/
|
||||
if ((recv_size >= sizeof(uint32_t)) && (packet->MsgType != CDC_RNDIS_PACKET_MSG_ID)) {
|
||||
os_printf("WRONG RNDIS ID!\r\n");
|
||||
break;
|
||||
}
|
||||
/*包头对齐且长度大于一个完整包,则不用拷贝到暂存buf,直接传进lwip*/
|
||||
if ((recv_size >= sizeof(USBD_CDC_RNDIS_PacketMsgTypeDef)) && (recv_size >= packet->MsgLength)) {
|
||||
flags = disable_irq();
|
||||
input_cb = rndis->input_cb;
|
||||
input_priv = rndis->input_priv;
|
||||
enable_irq(flags);
|
||||
if (input_cb) {
|
||||
input_cb(&rndis->ndev,
|
||||
(rt_uint8_t *)packet + packet->DataOffset + 8,
|
||||
packet->DataLength,
|
||||
input_priv);
|
||||
}
|
||||
offset += packet->MsgLength;
|
||||
recv_size -= packet->MsgLength;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (recv_size > (2 * sizeof(uint32_t))) {
|
||||
target_copyLength = (packet->MsgLength - rndis->ts_saveLength);
|
||||
} else {
|
||||
target_copyLength = recv_size;
|
||||
}
|
||||
if (recv_size) {
|
||||
os_memcpy(rndis->ts_buffer + rndis->ts_saveLength,
|
||||
rndis->data_buffer + offset,
|
||||
MIN(recv_size, target_copyLength));
|
||||
}
|
||||
offset += MIN(recv_size, target_copyLength);
|
||||
rndis->ts_saveLength += MIN(recv_size, target_copyLength);
|
||||
recv_size -= MIN(recv_size, target_copyLength);
|
||||
packet = (USBD_CDC_RNDIS_PacketMsgTypeDef *)(rndis->ts_buffer);
|
||||
|
||||
if ((packet->MsgType == CDC_RNDIS_PACKET_MSG_ID) && (rndis->ts_saveLength >= packet->MsgLength)) {
|
||||
flags = disable_irq();
|
||||
input_cb = rndis->input_cb;
|
||||
input_priv = rndis->input_priv;
|
||||
enable_irq(flags);
|
||||
if (input_cb) {
|
||||
input_cb(&rndis->ndev,
|
||||
(rt_uint8_t *)packet + packet->DataOffset + 8,
|
||||
packet->DataLength,
|
||||
input_priv);
|
||||
}
|
||||
rndis->ts_saveLength -= packet->MsgLength;
|
||||
}
|
||||
}
|
||||
if (recv_size < 0) {
|
||||
// 返回负数大概是usb断线了,不再循环读数,准备释放线程了
|
||||
rndis->ready = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rt_err_t rt_usbh_rndis_run(struct usb_rndis *rndis)
|
||||
{
|
||||
if (rndis == NULL) {
|
||||
return RET_ERR;
|
||||
}
|
||||
os_printf("rndis\r\n");
|
||||
#ifndef STATIC_RNDIS_NETDEV
|
||||
rt_usbh_rndis_attach(rndis);
|
||||
rt_usbh_rndis_network_init(rndis);
|
||||
#endif
|
||||
|
||||
rt_rndis_msg_init(rndis);
|
||||
rndis->ready = 1;
|
||||
|
||||
os_timer_init(&rndis->keepalive_timer, rt_usbh_rndis_keepalive_timer,
|
||||
OS_TIMER_MODE_ONCE, rndis);
|
||||
os_timer_start(&rndis->keepalive_timer, 5000);
|
||||
rt_thread_init(&rndis->recv_task, "rndis_recv", rndis_data_recv, rndis,
|
||||
NULL, 512, OS_TASK_PRIORITY_HIGH-1, 20);
|
||||
rt_thread_startup(&rndis->recv_task);
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
rt_err_t rt_usbh_rndis_stop(struct usb_rndis *rndis)
|
||||
{
|
||||
rt_uint32_t flags;
|
||||
|
||||
if (rndis == NULL) {
|
||||
return RET_ERR;
|
||||
}
|
||||
|
||||
rndis->ready = 0;
|
||||
flags = disable_irq();
|
||||
rndis->input_cb = NULL;
|
||||
rndis->input_priv = NULL;
|
||||
enable_irq(flags);
|
||||
#ifndef STATIC_RNDIS_NETDEV
|
||||
dev_unregister((struct dev_obj *)rndis);
|
||||
lwip_netif_remove(&rndis->ndev);
|
||||
#endif
|
||||
os_timer_stop(&rndis->keepalive_timer);
|
||||
os_timer_del(&rndis->keepalive_timer);
|
||||
rt_thread_detach(&rndis->recv_task);
|
||||
os_free(rndis->msg_buffer);
|
||||
os_free(rndis->data_buffer);
|
||||
os_free(rndis->ts_buffer);
|
||||
#ifndef STATIC_RNDIS_NETDEV
|
||||
os_free(rndis);
|
||||
#endif
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
41
sdk/lib/bus/rttusb/usbhost/class/rndis.h
Normal file
41
sdk/lib/bus/rttusb/usbhost/class/rndis.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef __CLASS_RNDIS_H__
|
||||
#define __CLASS_RNDIS_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "hal/netdev.h"
|
||||
|
||||
struct usb_rndis {
|
||||
struct netdev ndev;
|
||||
netdev_input_cb input_cb;
|
||||
void *input_priv;
|
||||
void *device;
|
||||
struct rt_thread recv_task;
|
||||
struct rt_timer keepalive_timer;
|
||||
rt_uint8_t *msg_buffer;
|
||||
rt_uint8_t *data_buffer;
|
||||
//收到的下一个buf的内容,先暂存在这个buf,用于下次接收到后重组
|
||||
rt_uint8_t *ts_buffer;
|
||||
rt_uint32_t ts_saveLength;
|
||||
rt_uint32_t req_id;
|
||||
rt_uint8_t mac[6];
|
||||
upipe_t pipe_in;
|
||||
upipe_t pipe_out;
|
||||
upipe_t pipe_int;
|
||||
rt_uint8_t link_up: 1, ready: 1, resv: 6;
|
||||
};
|
||||
|
||||
rt_err_t rt_usbh_rndis_run(struct usb_rndis *rndis);
|
||||
rt_err_t rt_usbh_rndis_stop(struct usb_rndis *rndis);
|
||||
rt_err_t rt_usbh_host_rndis_attach(struct usb_rndis *rndis);
|
||||
void rndis_ctrl_recv(void *context);
|
||||
void rndis_data_recv(void *context);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
552
sdk/lib/bus/rttusb/usbhost/class/udisk.c
Normal file
552
sdk/lib/bus/rttusb/usbhost/class/udisk.c
Normal file
@@ -0,0 +1,552 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-12-12 Yi Qiu first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
|
||||
#ifdef RT_USBH_MSTORAGE
|
||||
#include "mass.h"
|
||||
|
||||
#include "dev.h"
|
||||
#include "devid.h"
|
||||
#include "diskio.h"
|
||||
#include "ff.h"
|
||||
|
||||
#include "osal_file.h"
|
||||
#include "lib/ota/fw.h"
|
||||
#include "tx_platform.h"
|
||||
#include "dev/csi/hgdvp.h"
|
||||
|
||||
#define UDISK_MAX_COUNT 8
|
||||
#define UDISK_CACHE_SIZE (512)
|
||||
|
||||
static rt_uint8_t _udisk_idset = 0;
|
||||
static rt_uint8_t udisk_ota = 0;
|
||||
|
||||
struct udisk_device
|
||||
{
|
||||
rt_uint32_t count;
|
||||
rt_uint32_t sector_size;
|
||||
struct ustor_data* user_data;
|
||||
struct uhintf* intf;
|
||||
};
|
||||
|
||||
FATFS *udisk_fs = NULL;
|
||||
static struct udisk_device usb_disk;
|
||||
|
||||
static int udisk_get_id(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i=0; i< UDISK_MAX_COUNT; i++)
|
||||
{
|
||||
if((_udisk_idset & (1 << i)) != 0) continue;
|
||||
else break;
|
||||
}
|
||||
|
||||
/* it should not happen */
|
||||
if(i == UDISK_MAX_COUNT) RT_ASSERT(0);
|
||||
|
||||
_udisk_idset |= (1 << i);
|
||||
return i;
|
||||
}
|
||||
|
||||
static void udisk_free_id(int id)
|
||||
{
|
||||
RT_ASSERT(id < UDISK_MAX_COUNT);
|
||||
|
||||
_udisk_idset &= ~(1 << id);
|
||||
}
|
||||
|
||||
static DSTATUS rt_udisk_status(void *dev)
|
||||
{
|
||||
struct udisk_device *disk = (struct udisk_device *)dev;
|
||||
if((disk==NULL) || (disk->intf == NULL))
|
||||
{
|
||||
rt_kprintf("%s disk is null!!!\n",__FUNCTION__);
|
||||
return RES_ERROR;
|
||||
}
|
||||
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
static DSTATUS rt_udisk_init(void *dev)
|
||||
{
|
||||
printf("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
struct udisk_device *disk = (struct udisk_device *)dev;
|
||||
if(!disk)
|
||||
{
|
||||
os_printf("disk is null!!!\n");
|
||||
return RES_ERROR;
|
||||
}
|
||||
|
||||
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
static DRESULT rt_udisk_read(void *dev, BYTE* buffer, DWORD sector,
|
||||
UINT count)
|
||||
{
|
||||
rt_err_t ret;
|
||||
struct uhintf* intf;
|
||||
struct ustor_data* data;
|
||||
int timeout = USB_TIMEOUT_LONG/5;
|
||||
struct udisk_device *disk = (struct udisk_device *)dev;
|
||||
|
||||
/* check parameter */
|
||||
if((disk==NULL) ||(buffer==NULL) || (disk->intf == NULL))
|
||||
{
|
||||
rt_kprintf("%s disk is null!!!\n",__FUNCTION__);
|
||||
return RES_ERROR;
|
||||
}
|
||||
|
||||
if(count > 4096) timeout *= 2;
|
||||
|
||||
data = (struct ustor_data*)disk->user_data;
|
||||
intf = disk->intf;
|
||||
|
||||
//os_printf("%s sector:%d count:%d\n",__FUNCTION__,sector,count);
|
||||
|
||||
ret = rt_usbh_storage_read10(intf, (rt_uint8_t*)buffer, sector, count, timeout);
|
||||
|
||||
if (ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("usb mass_storage read failed\n");
|
||||
return RES_ERROR;
|
||||
}
|
||||
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
static DRESULT rt_udisk_write (void *dev, BYTE* buffer, DWORD sector,
|
||||
UINT count)
|
||||
{
|
||||
rt_err_t ret;
|
||||
struct uhintf* intf;
|
||||
struct ustor_data* data;
|
||||
int timeout = USB_TIMEOUT_LONG/5;
|
||||
struct udisk_device *disk = (struct udisk_device *)dev;
|
||||
|
||||
/* check parameter */
|
||||
if((disk==NULL) ||(buffer==NULL) || (disk->intf == NULL)){
|
||||
rt_kprintf("udisk write parameter error\n");
|
||||
return RES_ERROR;
|
||||
}
|
||||
|
||||
if(count * SECTOR_SIZE > 4096) timeout *= 2;
|
||||
|
||||
data = (struct ustor_data*)disk->user_data;
|
||||
intf = disk->intf;
|
||||
|
||||
//os_printf("%s write sector:%d count:%d \n",__FUNCTION__,sector,count);
|
||||
|
||||
ret = rt_usbh_storage_write10(intf, (rt_uint8_t*)buffer, sector, count, timeout);
|
||||
if (ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("usb mass_storage write %d sector failed\n", count);
|
||||
return RES_ERROR;
|
||||
}
|
||||
|
||||
return RES_OK;
|
||||
|
||||
}
|
||||
|
||||
static DRESULT rt_udisk_control(void *dev, BYTE cmd, void *buf)
|
||||
{
|
||||
printf("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
struct udisk_device *udisk = (struct udisk_device *)dev;
|
||||
rt_uint8_t ret = RES_OK;
|
||||
//os_printf("cmd:%d\n",cmd);
|
||||
switch(cmd)
|
||||
{
|
||||
case CTRL_SYNC:
|
||||
break;
|
||||
case GET_SECTOR_COUNT:
|
||||
*(DWORD *)buf = udisk->count;
|
||||
ret = RES_OK;
|
||||
break;
|
||||
|
||||
case GET_SECTOR_SIZE:
|
||||
*(WORD *)buf = udisk->sector_size;
|
||||
ret = RES_OK;
|
||||
|
||||
break;
|
||||
case GET_BLOCK_SIZE:
|
||||
*(DWORD *)buf = 1;
|
||||
|
||||
ret = RES_OK;
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
ret = RES_ERROR;
|
||||
printf("rtos_sd_ioctl err\n");
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
const static struct rt_device_ops udisk_device_ops =
|
||||
{
|
||||
rt_udisk_init,
|
||||
RT_NULL,
|
||||
RT_NULL,
|
||||
rt_udisk_read,
|
||||
rt_udisk_write,
|
||||
rt_udisk_control
|
||||
};
|
||||
#endif
|
||||
|
||||
static struct fatfs_diskio udisk_driver =
|
||||
{
|
||||
.status = rt_udisk_status,
|
||||
.init = rt_udisk_init,
|
||||
.read = rt_udisk_read,
|
||||
.write = rt_udisk_write,
|
||||
.ioctl = rt_udisk_control,
|
||||
};
|
||||
|
||||
|
||||
void rt_udisk_ota_thread(void)
|
||||
{
|
||||
|
||||
#if DVP_EN
|
||||
void *dvp = (void *)dev_get(HG_DVP_DEVID);
|
||||
if(dvp)
|
||||
{
|
||||
dvp_close(dvp);
|
||||
}
|
||||
#endif
|
||||
|
||||
if(!udisk_ota){
|
||||
rt_uint8_t *cache_buf = NULL;
|
||||
void *fp = osal_fopen("USB:/UPDATE.BIN","r");
|
||||
if(!fp)
|
||||
{
|
||||
os_printf("udisk ota file not open\n");
|
||||
goto __udisk_ota_end;
|
||||
}
|
||||
rt_uint32_t filesize = osal_fsize(fp);
|
||||
rt_uint32_t filesize_tmp = filesize;
|
||||
rt_uint32_t readsize = UDISK_CACHE_SIZE;
|
||||
rt_uint32_t ota_offset = 0;
|
||||
cache_buf = (rt_uint8_t *)os_malloc(UDISK_CACHE_SIZE);
|
||||
if(!cache_buf)
|
||||
{
|
||||
os_printf("cache_buf malloc failed\n");
|
||||
goto __udisk_ota_end;
|
||||
}
|
||||
os_printf("filesize:%d cache_buf:%x\n",filesize,cache_buf);
|
||||
|
||||
while(filesize)
|
||||
{
|
||||
if(filesize < UDISK_CACHE_SIZE)
|
||||
{
|
||||
readsize = filesize;
|
||||
}
|
||||
|
||||
osal_fread(cache_buf,readsize,1,fp);
|
||||
libota_write_fw(filesize_tmp,ota_offset,cache_buf,readsize);
|
||||
filesize -= readsize;
|
||||
ota_offset += readsize;
|
||||
}
|
||||
|
||||
udisk_ota = 1;
|
||||
|
||||
__udisk_ota_end:
|
||||
if(fp)
|
||||
{
|
||||
osal_fclose(fp);
|
||||
}
|
||||
|
||||
if(cache_buf)
|
||||
{
|
||||
os_free(cache_buf);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will run udisk driver when usb disk is detected.
|
||||
*
|
||||
* @param intf the usb interface instance.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_udisk_run(struct uhintf* intf)
|
||||
{
|
||||
int i = 0;
|
||||
rt_err_t ret;
|
||||
//char dname[8];
|
||||
char sname[8];
|
||||
rt_align(4) rt_uint8_t max_lun[1 + USB_RX_BUFF_RESERVE_SIZE];
|
||||
rt_uint8_t *sector;
|
||||
rt_align(4) rt_uint8_t sense[18 + USB_RX_BUFF_RESERVE_SIZE];
|
||||
rt_align(4) rt_uint8_t inquiry[36 + USB_RX_BUFF_RESERVE_SIZE];
|
||||
ustor_t stor;
|
||||
|
||||
/* check parameter */
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
printf("%s %d\n",__FUNCTION__,__LINE__);
|
||||
/* set interface */
|
||||
// ret = rt_usbh_set_interface(intf->device, intf->intf_desc->bInterfaceNumber);
|
||||
// if(ret != RT_EOK)
|
||||
// rt_usbh_clear_feature(intf->device, 0, USB_FEATURE_ENDPOINT_HALT);
|
||||
/* reset mass storage class device */
|
||||
ret = rt_usbh_storage_reset(intf);
|
||||
if(ret != RT_EOK) return ret;
|
||||
|
||||
stor = (ustor_t)intf->user_data;
|
||||
stor->dev_cnt = 1;
|
||||
|
||||
/* get max logic unit number */
|
||||
ret = rt_usbh_storage_get_max_lun(intf, max_lun);
|
||||
if(ret != RT_EOK)
|
||||
rt_usbh_clear_feature(intf->device, 0, USB_FEATURE_ENDPOINT_HALT);
|
||||
|
||||
/* reset pipe in endpoint */
|
||||
if(stor->pipe_in->status == UPIPE_STATUS_STALL)
|
||||
{
|
||||
ret = rt_usbh_clear_feature(intf->device,
|
||||
stor->pipe_in->ep.bEndpointAddress, USB_FEATURE_ENDPOINT_HALT);
|
||||
printf("%s %d\n",__FUNCTION__,__LINE__);
|
||||
if(ret != RT_EOK) return ret;
|
||||
}
|
||||
|
||||
|
||||
/* reset pipe out endpoint */
|
||||
if(stor->pipe_out->status == UPIPE_STATUS_STALL)
|
||||
{
|
||||
ret = rt_usbh_clear_feature(intf->device,
|
||||
stor->pipe_out->ep.bEndpointAddress, USB_FEATURE_ENDPOINT_HALT);
|
||||
printf("%s %d\n",__FUNCTION__,__LINE__);
|
||||
if(ret != RT_EOK) return ret;
|
||||
}
|
||||
|
||||
while((ret = rt_usbh_storage_inquiry(intf, inquiry)) != RT_EOK)
|
||||
{
|
||||
if(ret == -RT_EIO) return ret;
|
||||
|
||||
rt_thread_delay(5);
|
||||
if(i++ < 10) continue;
|
||||
rt_kprintf("rt_usbh_storage_inquiry error\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
i = 0;
|
||||
|
||||
/* wait device ready */
|
||||
while((ret = rt_usbh_storage_test_unit_ready(intf)) != RT_EOK)
|
||||
{
|
||||
if(ret == -RT_EIO) return ret;
|
||||
|
||||
ret = rt_usbh_storage_request_sense(intf, sense);
|
||||
if(ret == -RT_EIO) return ret;
|
||||
|
||||
rt_thread_delay(10);
|
||||
if(i++ < 10) continue;
|
||||
|
||||
rt_kprintf("rt_usbh_storage_test_unit_ready error\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
i = 0;
|
||||
rt_memset(stor->capicity, 0, sizeof(stor->capicity));
|
||||
|
||||
/* get storage capacity */
|
||||
while((ret = rt_usbh_storage_get_capacity(intf,
|
||||
(rt_uint8_t*)stor->capicity)) != RT_EOK)
|
||||
{
|
||||
if(ret == -RT_EIO) return ret;
|
||||
|
||||
rt_thread_delay(50);
|
||||
if(i++ < 10) continue;
|
||||
|
||||
stor->capicity[0] = 2880;
|
||||
stor->capicity[1] = 0x200;
|
||||
|
||||
rt_kprintf("rt_usbh_storage_get_capacity error\n");
|
||||
break;
|
||||
}
|
||||
|
||||
stor->capicity[0] = uswap_32(stor->capicity[0]);
|
||||
stor->capicity[1] = uswap_32(stor->capicity[1]);
|
||||
stor->capicity[0] += 1;
|
||||
|
||||
rt_kprintf("capicity %d, block size %d\n",
|
||||
stor->capicity[0], stor->capicity[1]);
|
||||
|
||||
/* get the first sector to read partition table */
|
||||
sector = (rt_uint8_t*) rt_malloc (SECTOR_SIZE + USB_RX_BUFF_RESERVE_SIZE);
|
||||
if (sector == RT_NULL)
|
||||
{
|
||||
rt_kprintf("allocate partition sector buffer failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_memset(sector, 0, SECTOR_SIZE);
|
||||
|
||||
rt_kprintf("read partition table\n");
|
||||
|
||||
/* get the partition table */
|
||||
ret = rt_usbh_storage_read10(intf, sector, 0, 1, USB_TIMEOUT_LONG);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("read parition table error\n");
|
||||
|
||||
rt_free(sector);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_kprintf("finished reading partition\n");
|
||||
|
||||
|
||||
int res = 0;
|
||||
|
||||
struct ustor_data* data = rt_malloc(sizeof(struct ustor_data));
|
||||
if (data == RT_NULL)
|
||||
{
|
||||
rt_kprintf("Allocate partition data buffer failed.");
|
||||
}
|
||||
rt_memset(data, 0, sizeof(struct ustor_data));
|
||||
data->intf = intf;
|
||||
// data->udisk_id = udisk_get_id();
|
||||
// os_printf("udisk_id:%d\n",data->udisk_id);
|
||||
// os_snprintf(dname, 6, "ud%d-%d", data->udisk_id, 0);
|
||||
os_snprintf(sname, 8, "sem_ud%d", 0);
|
||||
|
||||
/* register sdcard device */
|
||||
stor->dev[0].type = 0; //RT_Device_Class_Block;
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
stor->dev[0].ops = &udisk_device_ops;
|
||||
#else
|
||||
stor->dev[0].status = rt_udisk_status;
|
||||
stor->dev[0].init = rt_udisk_init;
|
||||
stor->dev[0].read = rt_udisk_read;
|
||||
stor->dev[0].write = rt_udisk_write;
|
||||
stor->dev[0].ioctl = rt_udisk_control;
|
||||
#endif
|
||||
stor->dev[0].user_data = (void*)data;
|
||||
|
||||
usb_disk.count = stor->capicity[0];
|
||||
usb_disk.sector_size = stor->capicity[1];
|
||||
usb_disk.user_data = data;
|
||||
usb_disk.intf = intf;
|
||||
|
||||
|
||||
fatfs_register_drive(DEV_USB, &udisk_driver, &usb_disk);
|
||||
if(!udisk_fs)
|
||||
{
|
||||
udisk_fs = (FATFS *)os_malloc(sizeof(FATFS));
|
||||
}
|
||||
|
||||
if(udisk_fs)
|
||||
{
|
||||
res = f_mount(udisk_fs, "USB:", 1);
|
||||
if(res)
|
||||
{
|
||||
os_printf("%s mount fatfs err:%d\n",__FUNCTION__,res);
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
|
||||
DIR dir;
|
||||
FILINFO f_info;
|
||||
rt_uint8_t maxdir = 0;
|
||||
FRESULT rets;
|
||||
rets = f_opendir(&dir, "USB:/");
|
||||
if (rets != FR_OK) {
|
||||
printf("failed open\n");
|
||||
return 1;
|
||||
}
|
||||
os_printf("===========USB DIR===========\n");
|
||||
while (1) {
|
||||
rets = f_readdir(&dir, &f_info);
|
||||
if (rets != FR_OK) {
|
||||
break;
|
||||
}
|
||||
if (f_info.fname[0] == 0) {
|
||||
break;
|
||||
} else {
|
||||
if (f_info.fattrib) {
|
||||
printf("%s \n", f_info.fname);
|
||||
maxdir++;
|
||||
}
|
||||
}
|
||||
}
|
||||
os_printf("=============================\n");
|
||||
os_printf("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
rt_thread_t thread;
|
||||
thread = rt_thread_create("udisk_test",rt_udisk_ota_thread,NULL,4096,OS_TASK_PRIORITY_NORMAL,0);
|
||||
if(thread != RT_NULL)
|
||||
{
|
||||
rt_thread_startup(thread);
|
||||
}
|
||||
#endif
|
||||
|
||||
rt_free(sector);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will be invoked when usb disk plug out is detected and it would clean
|
||||
* and release all udisk related resources.
|
||||
*
|
||||
* @param intf the usb interface instance.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_udisk_stop(struct uhintf* intf)
|
||||
{
|
||||
int i;
|
||||
ustor_t stor;
|
||||
struct ustor_data* data;
|
||||
|
||||
/* check parameter */
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
RT_ASSERT(intf->device != RT_NULL);
|
||||
|
||||
stor = (ustor_t)intf->user_data;
|
||||
RT_ASSERT(stor != RT_NULL);
|
||||
|
||||
for(i=0; i<stor->dev_cnt; i++)
|
||||
{
|
||||
struct ustor_device *dev = &stor->dev[i];
|
||||
data = (struct ustor_data*)dev->user_data;
|
||||
usb_disk.intf = NULL;
|
||||
/* unmount filesystem */
|
||||
f_umount("USB:");
|
||||
|
||||
if(udisk_fs)
|
||||
{
|
||||
os_free(udisk_fs);
|
||||
udisk_fs = NULL;
|
||||
}
|
||||
|
||||
// udisk_free_id(data->udisk_id);
|
||||
|
||||
rt_free(data);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
89
sdk/lib/bus/rttusb/usbhost/class/ukbd.c
Normal file
89
sdk/lib/bus/rttusb/usbhost/class/ukbd.c
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2012-01-03 Yi Qiu first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
#include "hid.h"
|
||||
|
||||
#if defined(RT_USBH_HID) && defined(RT_USBH_HID_KEYBOARD)
|
||||
|
||||
static struct uprotocal kbd_protocal;
|
||||
|
||||
static rt_err_t rt_usbh_hid_kbd_callback(void* arg)
|
||||
{
|
||||
rt_uint32_t int1, int2;
|
||||
struct uhid* hid;
|
||||
|
||||
hid = (struct uhid*)arg;
|
||||
|
||||
rt_memcpy(&int1, hid->buffer, 4);
|
||||
rt_memcpy(&int2, hid->buffer+4, 4);
|
||||
|
||||
if(int1 != 0 || int2 != 0)
|
||||
{
|
||||
os_printf("key down 0x%x, 0x%x", int1, int2);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_thread_t kbd_thread;
|
||||
static void kbd_task(void* param)
|
||||
{
|
||||
struct uhintf* intf = (struct uhintf*)param;
|
||||
while (1)
|
||||
{
|
||||
if (rt_usb_hcd_pipe_xfer(intf->device->hcd, ((struct uhid*)intf->user_data)->pipe_in,
|
||||
((struct uhid*)intf->user_data)->buffer, ((struct uhid*)intf->user_data)->pipe_in->ep.wMaxPacketSize,
|
||||
USB_TIMEOUT_BASIC) == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
rt_usbh_hid_kbd_callback(intf->user_data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static rt_err_t rt_usbh_hid_kbd_init(void* arg)
|
||||
{
|
||||
struct uhintf* intf = (struct uhintf*)arg;
|
||||
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
|
||||
rt_usbh_hid_set_protocal(intf, 0);
|
||||
|
||||
rt_usbh_hid_set_idle(intf, 10, 0);
|
||||
|
||||
os_printf("start usb keyboard");
|
||||
|
||||
kbd_thread = rt_thread_create("kbd0", kbd_task, intf, 1024, 8, 100);
|
||||
rt_thread_startup(kbd_thread);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will define the hid keyboard protocal, it will be register to the protocal list.
|
||||
*
|
||||
* @return the keyboard protocal structure.
|
||||
*/
|
||||
uprotocal_t rt_usbh_hid_protocal_kbd(void)
|
||||
{
|
||||
kbd_protocal.pro_id = USB_HID_KEYBOARD;
|
||||
|
||||
kbd_protocal.init = rt_usbh_hid_kbd_init;
|
||||
kbd_protocal.callback = rt_usbh_hid_kbd_callback;
|
||||
|
||||
return &kbd_protocal;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
89
sdk/lib/bus/rttusb/usbhost/class/umouse.c
Normal file
89
sdk/lib/bus/rttusb/usbhost/class/umouse.c
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2012-01-03 Yi Qiu first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
#include "hid.h"
|
||||
|
||||
|
||||
#if defined(RT_USBH_HID) && defined(RT_USBH_HID_MOUSE)
|
||||
|
||||
static struct uprotocal mouse_protocal;
|
||||
|
||||
static rt_err_t rt_usbh_hid_mouse_callback(void* arg)
|
||||
{
|
||||
rt_uint32_t int1, int2;
|
||||
struct uhid* hid;
|
||||
|
||||
hid = (struct uhid*)arg;
|
||||
|
||||
rt_memcpy(&int1, hid->buffer, 4);
|
||||
rt_memcpy(&int2, hid->buffer+4, 4);
|
||||
|
||||
if(int1 != 0 || int2 != 0)
|
||||
{
|
||||
os_printf("key down 0x%x, 0x%x", int1, int2);
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_thread_t mouse_thread;
|
||||
static void mouse_task(void* param)
|
||||
{
|
||||
struct uhintf* intf = (struct uhintf*)param;
|
||||
while (1)
|
||||
{
|
||||
if (rt_usb_hcd_pipe_xfer(intf->device->hcd, ((struct uhid*)intf->user_data)->pipe_in,
|
||||
((struct uhid*)intf->user_data)->buffer, ((struct uhid*)intf->user_data)->pipe_in->ep.wMaxPacketSize,
|
||||
USB_TIMEOUT_BASIC) == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
rt_usbh_hid_mouse_callback(intf->user_data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static rt_err_t rt_usbh_hid_mouse_init(void* arg)
|
||||
{
|
||||
struct uhintf* intf = (struct uhintf*)arg;
|
||||
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
|
||||
rt_usbh_hid_set_protocal(intf, 0);
|
||||
|
||||
rt_usbh_hid_set_idle(intf, 0, 0);
|
||||
|
||||
mouse_thread = rt_thread_create("mouse0", mouse_task, intf, 1024, 8, 100);
|
||||
rt_thread_startup(mouse_thread);
|
||||
|
||||
os_printf("start usb mouse");
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will define the hid mouse protocal, it will be register to the protocal list.
|
||||
*
|
||||
* @return the keyboard protocal structure.
|
||||
*/
|
||||
uprotocal_t rt_usbh_hid_protocal_mouse(void)
|
||||
{
|
||||
mouse_protocal.pro_id = USB_HID_MOUSE;
|
||||
|
||||
mouse_protocal.init = rt_usbh_hid_mouse_init;
|
||||
mouse_protocal.callback = rt_usbh_hid_mouse_callback;
|
||||
|
||||
return &mouse_protocal;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
1361
sdk/lib/bus/rttusb/usbhost/class/usbh_audio.c
Normal file
1361
sdk/lib/bus/rttusb/usbhost/class/usbh_audio.c
Normal file
File diff suppressed because it is too large
Load Diff
109
sdk/lib/bus/rttusb/usbhost/class/usbh_audio.h
Normal file
109
sdk/lib/bus/rttusb/usbhost/class/usbh_audio.h
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright (c) 2022, sakumisu
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#ifndef USBH_AUDIO_H
|
||||
#define USBH_AUDIO_H
|
||||
|
||||
#include "rtthread.h"
|
||||
#include "include/rttusb_host.h"
|
||||
#include "uaudioreg.h"
|
||||
#include "dev/usb/hgusb20_v1_dev_api.h"
|
||||
#include "dev/usb/usb11_v0/hgusb11_v0_host_api.h"
|
||||
#include "lib/audio/uac/uac_host.h"
|
||||
#include "audio_usbh_msi.h"
|
||||
|
||||
#ifndef CONFIG_USBHOST_MAX_AUDIO_CLASS
|
||||
#define CONFIG_USBHOST_MAX_AUDIO_CLASS 1
|
||||
#endif
|
||||
|
||||
#define CONFIG_USBHOST_MAX_INTF_ALTSETTINGS 8
|
||||
#define CONFIG_USBHOST_DEV_NAMELEN 16
|
||||
|
||||
struct usbh_audio_format_type {
|
||||
rt_uint8_t channels;
|
||||
rt_uint8_t format_type;
|
||||
rt_uint8_t bitresolution;
|
||||
rt_uint8_t sampfreq_num;
|
||||
rt_uint32_t sampfreq[3];
|
||||
};
|
||||
|
||||
/**
|
||||
* bSourceID in feature_unit = input_terminal_id
|
||||
* bSourceID in output_terminal = feature_unit_id
|
||||
* terminal_link_id = input_terminal_id or output_terminal_id (if input_terminal_type or output_terminal_type is 0x0101)
|
||||
*
|
||||
*
|
||||
*/
|
||||
struct usbh_audio_module {
|
||||
const char *name;
|
||||
rt_uint8_t data_intf;
|
||||
rt_uint8_t input_terminal_id;
|
||||
rt_uint16_t input_terminal_type;
|
||||
rt_uint16_t input_channel_config;
|
||||
rt_uint8_t output_terminal_id;
|
||||
rt_uint16_t output_terminal_type;
|
||||
rt_uint8_t feature_unit_id;
|
||||
rt_uint8_t feature_unit_controlsize;
|
||||
rt_uint8_t feature_unit_controls[8];
|
||||
rt_uint8_t terminal_link_id;
|
||||
struct usbh_audio_format_type altsetting[CONFIG_USBHOST_MAX_INTF_ALTSETTINGS];
|
||||
};
|
||||
|
||||
struct usbh_audio {
|
||||
//struct usbh_hubport *hport;
|
||||
struct uinstance* device;
|
||||
char devname[CONFIG_USBHOST_DEV_NAMELEN];
|
||||
struct uendpoint_descriptor isoin; /* ISO IN endpoint */
|
||||
struct uendpoint_descriptor isoout; /* ISO OUT endpoint */
|
||||
|
||||
upipe_t pipe_in;
|
||||
upipe_t pipe_out;
|
||||
|
||||
rt_uint8_t ctrl_intf; /* interface number */
|
||||
rt_uint8_t minor;
|
||||
rt_uint16_t isoin_mps;
|
||||
rt_uint16_t isoout_mps;
|
||||
bool is_opened;
|
||||
rt_uint16_t bcdADC;
|
||||
rt_uint8_t bInCollection;
|
||||
rt_uint8_t num_of_intf_altsettings;
|
||||
struct usbh_audio_module module[2];
|
||||
rt_uint8_t module_num;
|
||||
rt_uint8_t *rx_buff;
|
||||
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int usbh_audio_open(struct usbh_audio *audio_class, const char *name, rt_uint32_t samp_freq);
|
||||
int usbh_audio_close(struct usbh_audio *audio_class, const char *name);
|
||||
int usbh_audio_get_min_volume(struct usbh_audio *audio_class, const char *name, rt_uint16_t *min_volume);
|
||||
int usbh_audio_get_max_volume(struct usbh_audio *audio_class, const char *name, rt_uint16_t *max_volume);
|
||||
int usbh_audio_get_cur_volume(struct usbh_audio *audio_class, const char *name, rt_uint16_t *cur_volume);
|
||||
int usbh_audio_get_res_volume(struct usbh_audio *audio_class, const char *name, rt_uint16_t *res_volume);
|
||||
int usbh_audio_set_volume(struct usbh_audio *audio_class, const char *name, rt_uint16_t volume_hex);
|
||||
int usbh_audio_set_volume_db(struct usbh_audio *audio_class, const char *name, int volume_db, int min_volume_db, int max_volume_db);
|
||||
int usbh_audio_set_mute(struct usbh_audio *audio_class, const char *name, bool mute);
|
||||
|
||||
void rtt_usbh_audio_irq(void * dev, rt_uint32_t irq, rt_uint8_t ep);
|
||||
|
||||
rt_uint32_t rtt_usbh_audio_dev_pipe_mange(rt_uint8_t dev_num, const char *name, rt_uint8_t alloc_or_free);
|
||||
|
||||
rt_uint32_t rtt_usbh_audio_user_open();
|
||||
rt_uint32_t rtt_usbh_audio_user_close();
|
||||
rt_uint32_t rtt_usbh_audio_user_stop();
|
||||
rt_uint32_t rtt_usbh_audio_user_start();
|
||||
|
||||
void usbh_audio_run(struct usbh_audio *audio_class);
|
||||
void usbh_audio_stop(struct usbh_audio *audio_class);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* USBH_AUDIO_H */
|
||||
1234
sdk/lib/bus/rttusb/usbhost/class/usbh_video.c
Normal file
1234
sdk/lib/bus/rttusb/usbhost/class/usbh_video.c
Normal file
File diff suppressed because it is too large
Load Diff
130
sdk/lib/bus/rttusb/usbhost/class/usbh_video.h
Normal file
130
sdk/lib/bus/rttusb/usbhost/class/usbh_video.h
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright (c) 2022, sakumisu
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#ifndef USBH_VIDEO_H
|
||||
#define USBH_VIDEO_H
|
||||
|
||||
#include "rtthread.h"
|
||||
#include "include/rttusb_host.h"
|
||||
#include "include/usb_video.h"
|
||||
#include "dev/usb/hgusb20_v1_dev_api.h"
|
||||
|
||||
#define USBH_VIDEO_PPB 1 //usbh video 软件双缓存区使能
|
||||
|
||||
#define USBH_VIDEO_FORMAT_UNCOMPRESSED 0
|
||||
#define USBH_VIDEO_FORMAT_MJPEG 1
|
||||
#define USBH_VIDEO_FORMAT_BASED 2
|
||||
|
||||
#define CONFIG_USBHOST_DEV_NAMELEN 16
|
||||
#define CONFIG_ALTERSETTING_MAXLEN 10
|
||||
|
||||
#define USBH_VIDEO_MAX_FORMAT_NUM 5
|
||||
#define USBH_VIDEO_MAX_FRAME_NUM 15
|
||||
|
||||
struct usbh_video_resolution {
|
||||
rt_uint16_t wWidth;
|
||||
rt_uint16_t wHeight;
|
||||
rt_uint32_t dwDefaultFrameInterval;
|
||||
};
|
||||
|
||||
struct usbh_video_format {
|
||||
struct usbh_video_resolution frame[USBH_VIDEO_MAX_FRAME_NUM];
|
||||
rt_uint8_t format_type;
|
||||
rt_uint8_t num_of_frames;
|
||||
};
|
||||
|
||||
struct usbh_videoframe {
|
||||
rt_uint8_t *frame_buf;
|
||||
rt_uint32_t frame_bufsize;
|
||||
rt_uint32_t frame_format;
|
||||
rt_uint32_t frame_size;
|
||||
};
|
||||
|
||||
struct usbh_videostreaming {
|
||||
struct usbh_videoframe *frame;
|
||||
rt_uint32_t frame_format;
|
||||
rt_uint32_t bufoffset;
|
||||
rt_uint16_t width;
|
||||
rt_uint16_t height;
|
||||
};
|
||||
|
||||
struct intf_altersetting_cfg {
|
||||
uep_desc_t ep_desc_t;
|
||||
rt_uint32_t altersetting_num;
|
||||
rt_uint32_t check_use;
|
||||
};
|
||||
|
||||
struct usbh_video {
|
||||
//struct usbh_hubport *hport;
|
||||
struct uinstance* device;
|
||||
char devname[CONFIG_USBHOST_DEV_NAMELEN];
|
||||
struct uendpoint_descriptor isoin; /* ISO IN endpoint */
|
||||
struct uendpoint_descriptor isoout; /* ISO OUT endpoint */
|
||||
|
||||
upipe_t pipe_in;
|
||||
upipe_t pipe_out;
|
||||
|
||||
rt_uint8_t ctrl_intf; /* interface number */
|
||||
rt_uint8_t data_intf; /* interface number */
|
||||
rt_uint8_t minor;
|
||||
struct video_probe_and_commit_controls probe;
|
||||
struct video_probe_and_commit_controls commit;
|
||||
rt_uint16_t isoin_mps;
|
||||
rt_uint16_t isoout_mps;
|
||||
bool is_opened;
|
||||
rt_uint8_t current_format;
|
||||
rt_uint16_t bcdVDC;
|
||||
rt_uint8_t num_of_intf_altsettings;
|
||||
rt_uint8_t num_of_formats;
|
||||
struct usbh_video_format format[USBH_VIDEO_MAX_FORMAT_NUM];
|
||||
rt_uint8_t *rx_buff;
|
||||
#if USBH_VIDEO_PPB
|
||||
volatile rt_uint8_t usbh_pingpang_flag;
|
||||
rt_uint8_t *rx_double_buff;
|
||||
#endif
|
||||
struct intf_altersetting_cfg intf_altersetting[CONFIG_ALTERSETTING_MAXLEN];
|
||||
rt_uint32_t cur_set_altersetting_num;
|
||||
rt_uint32_t video_rx_size;
|
||||
rt_uint32_t uvc_head;
|
||||
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int usbh_video_get(struct usbh_video *video_class, rt_uint8_t request, rt_uint8_t intf, rt_uint8_t entity_id, rt_uint8_t cs, rt_uint8_t *buf, rt_uint16_t len);
|
||||
int usbh_video_set(struct usbh_video *video_class, rt_uint8_t request, rt_uint8_t intf, rt_uint8_t entity_id, rt_uint8_t cs, rt_uint8_t *buf, rt_uint16_t len);
|
||||
|
||||
int usbh_video_open(struct usbh_video *video_class,
|
||||
rt_uint8_t format_type,
|
||||
rt_uint16_t wWidth,
|
||||
rt_uint16_t wHeight,
|
||||
rt_uint8_t altsetting);
|
||||
int usbh_video_close(uinst_t device, struct usbh_video *video_class);
|
||||
|
||||
void usbh_video_list_info(struct usbh_video *video_class);
|
||||
|
||||
void rtt_usbh_video_irq(void * dev, rt_uint8_t ep, uhcd_t hcd);
|
||||
void rtt_usb11h_video_irq(void * dev, rt_uint8_t ep , uhcd_t hcd);
|
||||
|
||||
rt_uint32_t rtt_usbh_video_dev_pipe_manage(rt_uint8_t dev_num, rt_uint8_t alloc_or_free);
|
||||
rt_uint32_t rtt_usbh_video_user_open(rt_uint8_t dev_num);
|
||||
rt_uint32_t rtt_usbh_video_user_close(rt_uint8_t dev_num);
|
||||
|
||||
void usbh_video_run(struct usbh_video *video_class);
|
||||
void usbh_video_stop(struct usbh_video *video_class);
|
||||
|
||||
ucd_t rt_usbh_class_driver_video(void);
|
||||
|
||||
extern void usb_host_enum_finish_init_mjpeg(uint32_t uvc_format);
|
||||
extern void usb_host_enum_finish_init_h264(uint32_t uvc_format);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* USBH_VIDEO_H */
|
||||
138
sdk/lib/bus/rttusb/usbhost/class/wireless.c
Normal file
138
sdk/lib/bus/rttusb/usbhost/class/wireless.c
Normal file
@@ -0,0 +1,138 @@
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
#include "rndis.h"
|
||||
|
||||
#ifdef RT_USBH_WIRELESS
|
||||
|
||||
static struct uclass_driver wireless_driver;
|
||||
|
||||
static rt_err_t rt_usbh_wireless_enable(void *arg)
|
||||
{
|
||||
struct uhintf **intf = arg;
|
||||
uhcd_t hcd = NULL;
|
||||
uep_desc_t ep_desc = NULL;
|
||||
struct usb_rndis *rndis = NULL;
|
||||
upipe_t pipe;
|
||||
rt_uint8_t ep_index;
|
||||
|
||||
if (intf[0] == NULL) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
hcd = intf[0]->device->hcd;
|
||||
os_printf("subclass %d, protocal %d\r\n",
|
||||
intf[0]->intf_desc->bInterfaceSubClass,
|
||||
intf[0]->intf_desc->bInterfaceProtocol);
|
||||
|
||||
// 中云信安cat1模组在iad描述符和接口描述符中不一致,兼容多了一种判断
|
||||
if ((intf[0]->intf_desc->bInterfaceSubClass == 1 && intf[0]->intf_desc->bInterfaceProtocol == 3) || \
|
||||
(intf[0]->intf_desc->bInterfaceSubClass == 2 && intf[0]->intf_desc->bInterfaceProtocol == 255)) {
|
||||
#ifdef STATIC_RNDIS_NETDEV
|
||||
rndis = (struct usb_rndis *)dev_get(HG_LTE_RNDIS_DEVID);
|
||||
#else
|
||||
rndis = (struct usb_rndis *)os_zalloc(sizeof(struct usb_rndis));
|
||||
#endif
|
||||
if (rndis == NULL) {
|
||||
os_printf("rndis alloc fail\r\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
rndis->msg_buffer = (rt_uint8_t *)os_malloc(128 + USB_RX_BUFF_RESERVE_SIZE);
|
||||
if (rndis->msg_buffer == NULL) {
|
||||
os_printf("rndis msg buffer alloc fail\r\n");
|
||||
os_free(rndis);
|
||||
return -ENOMEM;
|
||||
}
|
||||
rndis->data_buffer = (rt_uint8_t *)os_malloc(2048 + USB_RX_BUFF_RESERVE_SIZE);
|
||||
if (rndis->data_buffer == NULL) {
|
||||
os_printf("rndis data buffer alloc fail\r\n");
|
||||
os_free(rndis->msg_buffer);
|
||||
os_free(rndis);
|
||||
return -ENOMEM;
|
||||
}
|
||||
rndis->ts_buffer = (rt_uint8_t *)os_zalloc(2048 + USB_RX_BUFF_RESERVE_SIZE);
|
||||
if(rndis->ts_buffer == NULL) {
|
||||
os_printf("rndis ts_buf alloc fail\r\n");
|
||||
os_free(rndis->msg_buffer);
|
||||
os_free(rndis->data_buffer);
|
||||
os_free(rndis);
|
||||
return -ENOMEM;
|
||||
}
|
||||
rndis->ts_saveLength = 0;
|
||||
rndis->device = intf[0]->device;
|
||||
rndis->req_id = 1;
|
||||
intf[0]->user_data = rndis;
|
||||
|
||||
for (ep_index = 0; ep_index < intf[0]->intf_desc->bNumEndpoints; ++ep_index) {
|
||||
rt_usbh_get_endpoint_descriptor(intf[0]->intf_desc, ep_index, &ep_desc);
|
||||
if (ep_desc == NULL) {
|
||||
return RET_ERR;
|
||||
}
|
||||
|
||||
if ((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_INT)
|
||||
continue;
|
||||
|
||||
if ((ep_desc->bEndpointAddress & USB_DIR_MASK) == USB_DIR_IN) {
|
||||
if (rt_usb_hcd_alloc_pipe(intf[0]->device->hcd, &pipe, intf[0]->device, ep_desc) != RT_EOK) {
|
||||
rt_kprintf("alloc pipe failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
rt_usb_instance_add_pipe(intf[0]->device, pipe);
|
||||
rndis->pipe_int = pipe;
|
||||
}
|
||||
}
|
||||
|
||||
for (ep_index = 0; ep_index < intf[1]->intf_desc->bNumEndpoints; ++ep_index) {
|
||||
rt_usbh_get_endpoint_descriptor(intf[1]->intf_desc, ep_index, &ep_desc);
|
||||
if (ep_desc == NULL) {
|
||||
return RET_ERR;
|
||||
}
|
||||
|
||||
if ((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_BULK)
|
||||
continue;
|
||||
|
||||
if (rt_usb_hcd_alloc_pipe(intf[0]->device->hcd, &pipe, intf[0]->device, ep_desc) != RT_EOK) {
|
||||
rt_kprintf("alloc pipe failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
rt_usb_instance_add_pipe(intf[0]->device, pipe);
|
||||
if ((ep_desc->bEndpointAddress & USB_DIR_MASK) == USB_DIR_IN) {
|
||||
rndis->pipe_in = pipe;
|
||||
} else {
|
||||
rndis->pipe_out = pipe;
|
||||
}
|
||||
}
|
||||
|
||||
rt_usbh_rndis_run(rndis);
|
||||
}
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
static rt_err_t rt_usbh_wireless_disable(void *arg)
|
||||
{
|
||||
struct uhintf *intf = arg;
|
||||
struct usb_rndis *rndis = NULL;
|
||||
|
||||
if (intf == NULL) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
rndis = intf->user_data;
|
||||
if (rndis) {
|
||||
rt_usbh_rndis_stop(rndis);
|
||||
}
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ucd_t rt_usbh_class_driver_wireless(void)
|
||||
{
|
||||
wireless_driver.class_code = USB_CLASS_WIRELESS;
|
||||
|
||||
wireless_driver.enable = rt_usbh_wireless_enable;
|
||||
wireless_driver.disable = rt_usbh_wireless_disable;
|
||||
|
||||
return &wireless_driver;
|
||||
}
|
||||
|
||||
#endif
|
||||
472
sdk/lib/bus/rttusb/usbhost/class/yuge.c
Normal file
472
sdk/lib/bus/rttusb/usbhost/class/yuge.c
Normal file
@@ -0,0 +1,472 @@
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
#include "yuge.h"
|
||||
#include "cdc.h"
|
||||
#include "rndis.h"
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/netdb.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/tcpip.h"
|
||||
#include "netif/ethernetif.h"
|
||||
#include "lib/common/sysevt.h"
|
||||
|
||||
#ifdef RT_USBH_VENDOR_YUGE
|
||||
|
||||
// #define USB_VENDOR_ID_YUGE 0x19D1
|
||||
// #define USB_PRODUCT_ID_YUGE 0x1003 // YM310 X09
|
||||
|
||||
#define YUGE_ATCMD_BUFF_SIZE 128
|
||||
|
||||
static char recv_str[YUGE_ATCMD_BUFF_SIZE];
|
||||
|
||||
static struct uclass_driver yuge_driver;
|
||||
|
||||
__weak void mifi_led_control(rt_int16_t rsrq, rt_int16_t rssi){};
|
||||
|
||||
static rt_bool_t send_recv_atcmd_check(void *context, const char *send_str,
|
||||
const char *check_str, char *ret_str)
|
||||
{
|
||||
struct usb_yuge_at *yuge_at = context;
|
||||
uinst_t device = yuge_at->device;
|
||||
int recv_size;
|
||||
char *recv_str = NULL;
|
||||
rt_bool_t pass = RT_FALSE;
|
||||
|
||||
os_sprintf((char *)yuge_at->at_cmd_buff, send_str);
|
||||
rt_usb_hcd_pipe_xfer(device->hcd, yuge_at->pipe_out,
|
||||
yuge_at->at_cmd_buff, os_strlen(send_str), 0);
|
||||
os_sleep_ms(10); // 需要点延迟给LTE模组反应
|
||||
do {
|
||||
// 每次读取会清除缓存
|
||||
os_memset(yuge_at->at_cmd_buff, 0, YUGE_ATCMD_BUFF_SIZE);
|
||||
recv_size = rt_usb_hcd_pipe_xfer(device->hcd, yuge_at->pipe_in,
|
||||
yuge_at->at_cmd_buff, YUGE_ATCMD_BUFF_SIZE, 10);
|
||||
if (recv_size > 0) {
|
||||
recv_str = os_strstr(yuge_at->at_cmd_buff, check_str);
|
||||
if (recv_str != NULL) {
|
||||
pass = RT_TRUE;
|
||||
if (ret_str != NULL && recv_size > os_strlen(check_str)) {
|
||||
// ret_str存在说明需要获取返回结果做额外判断,复制到函数外面
|
||||
os_memcpy(ret_str, recv_str + os_strlen(check_str),
|
||||
recv_size - os_strlen(check_str));
|
||||
ret_str[recv_size - os_strlen(check_str)] = '\0'; // 字符串结束符
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (recv_size > 0);
|
||||
|
||||
return pass;
|
||||
}
|
||||
|
||||
// 计算下行频点,FDD不知道上行频点号,猜测直接偏固定频率?
|
||||
static rt_uint16_t eutra_channel_freq_mapping(rt_uint8_t band, rt_uint16_t earfcn, rt_bool_t uplink)
|
||||
{
|
||||
rt_uint16_t freq = 0;
|
||||
|
||||
switch (band) {
|
||||
// FDD上下行频点有偏差
|
||||
case 3:
|
||||
freq = 1805 + (earfcn - 1200) / 10;
|
||||
if (uplink)
|
||||
freq -= 95;
|
||||
break;
|
||||
case 5:
|
||||
freq = 869 + (earfcn - 2400) / 10;
|
||||
if (uplink)
|
||||
freq -= 45;
|
||||
break;
|
||||
case 8:
|
||||
freq = 925 + (earfcn - 3450) / 10;
|
||||
if (uplink)
|
||||
freq -= 45;
|
||||
break;
|
||||
// TDD上下行使用相同频点
|
||||
case 34: freq = 2010 + (earfcn - 36200) / 10; break;
|
||||
case 38: freq = 2570 + (earfcn - 37750) / 10; break;
|
||||
case 39: freq = 1880 + (earfcn - 38250) / 10; break;
|
||||
case 40: freq = 2300 + (earfcn - 38650) / 10; break;
|
||||
case 41: freq = 2496 + (earfcn - 39650) / 10; break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
return freq;
|
||||
}
|
||||
|
||||
static void yuge_network_info(void *context, char *recv_str)
|
||||
{
|
||||
struct usb_yuge_at *yuge_at = context;
|
||||
//char *argv[4];
|
||||
char **argv = NULL;
|
||||
int argc = 0;
|
||||
rt_int16_t rssi, ber;
|
||||
rt_uint16_t num_dl;
|
||||
rt_uint16_t freq_dl, freq_ul;
|
||||
rt_uint16_t freq_wifi = 0;
|
||||
rt_uint8_t band = 0, rxqual = 0;
|
||||
|
||||
argv = os_malloc(4 * sizeof(char *));
|
||||
if (argv == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取运营商
|
||||
if (send_recv_atcmd_check(yuge_at, "AT+COPS?\r\n", "+COPS:", recv_str) == RT_TRUE) {
|
||||
argc = os_strtok(recv_str, ",", argv, 4);
|
||||
if (argc >= 3) {
|
||||
// 0,0,"CHINA MOBILE",7
|
||||
_os_printf("%s\r\n", argv[2]);
|
||||
}
|
||||
}
|
||||
// 获取网络信息
|
||||
if (send_recv_atcmd_check(yuge_at, "AT+CCED=0,1\r\n", "+CCED:", recv_str) == RT_TRUE) {
|
||||
// argc = os_strtok(recv_str, ",", argv, 4);
|
||||
// if (argc >= 4) {
|
||||
// // "FDD LTE",46001,"LTE BAND 3",1650
|
||||
// band = os_atoi(argv[2] + 10);
|
||||
// num_dl = os_atoi(argv[3]);
|
||||
// freq_dl = eutra_channel_freq_mapping(band, num_dl, 0);
|
||||
// freq_ul = eutra_channel_freq_mapping(band, num_dl, 1);
|
||||
// _os_printf("BAND: %d\r\n", band);
|
||||
// _os_printf("F_dl: %d MHz, F_ul: %d MHz\r\n", freq_dl, freq_ul);
|
||||
// // 检查有无和wifi频点冲突
|
||||
// freq_wifi = sys_status.channel * 5 + 2407;
|
||||
// // TDD同频ul和dl一致
|
||||
// if (band == 40) {
|
||||
// if ((freq_wifi - freq_dl < 90) && sys_status.channel != 13) {
|
||||
// os_printf("!!!!!!! OVERLAP at %d and %d !!!!!!!\r\n", freq_wifi, freq_dl);
|
||||
// SYSEVT_NEW_LTE_EVT(SYSEVT_LTE_OVERLAP_WIFI, 13); // 切去高信道
|
||||
// }
|
||||
// } else if (band == 41) {
|
||||
// if ((freq_dl - freq_wifi < 90) && sys_status.channel != 1) {
|
||||
// os_printf("!!!!!!! OVERLAP at %d and %d !!!!!!!\r\n", freq_wifi, freq_dl);
|
||||
// SYSEVT_NEW_LTE_EVT(SYSEVT_LTE_OVERLAP_WIFI, 1); // 切去低信道
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
if (send_recv_atcmd_check(yuge_at, "AT+CSQ\r\n", "+CSQ:", recv_str) == RT_TRUE) {
|
||||
argc = os_strtok(recv_str, ",", argv, 4);
|
||||
if (argc >= 2) {
|
||||
// 28,99
|
||||
rssi = os_atoi(argv[0]); // dBm
|
||||
rxqual = os_atoi(argv[1]);
|
||||
mifi_led_control(0, -113 + rssi * 2);
|
||||
if (rssi == 99) {
|
||||
_os_printf("RSSI: unknow\r\n");
|
||||
} else if (rssi <= 0) {
|
||||
_os_printf("RSSI: <= -113 dBm\r\n");
|
||||
} else if (rssi >= 31) {
|
||||
_os_printf("RSSI: >= -51 dBm\r\n");
|
||||
} else {
|
||||
_os_printf("RSSI: %d dBm\r\n", -113 + rssi * 2);
|
||||
}
|
||||
// RXQUAL_0 BER < 0,2 % Assumed value = 0,14 %
|
||||
// RXQUAL_1 0,2 % < BER < 0,4 % Assumed value = 0,28 %
|
||||
// RXQUAL_2 0,4 % < BER < 0,8 % Assumed value = 0,57 %
|
||||
// RXQUAL_3 0,8 % < BER < 1,6 % Assumed value = 1,13 %
|
||||
// RXQUAL_4 1,6 % < BER < 3,2 % Assumed value = 2,26 %
|
||||
// RXQUAL_5 3,2 % < BER < 6,4 % Assumed value = 4,53 %
|
||||
// RXQUAL_6 6,4 % < BER < 12,8 % Assumed value = 9,05 %
|
||||
// RXQUAL_7 12,8 % < BER Assumed value = 18,10 %
|
||||
ber = 1 << (rxqual + 1);
|
||||
if (rxqual == 99) {
|
||||
_os_printf("BER: unknow\r\n");
|
||||
} else if (rxqual <= 0) {
|
||||
_os_printf("BER: < 0.2 %%\r\n");
|
||||
} else if (rxqual >= 7) {
|
||||
_os_printf("BER: > 12.8 %%\r\n");
|
||||
} else {
|
||||
_os_printf("BER: %d.%d %% ~ %d.%d %%\r\n", (ber/2)/10, (ber/2)%10, ber/10, ber%10);
|
||||
}
|
||||
}
|
||||
}
|
||||
os_free(argv);
|
||||
}
|
||||
|
||||
static void yuge_at_recv(void *context)
|
||||
{
|
||||
struct usb_yuge_at *yuge_at = context;
|
||||
|
||||
while (1) {
|
||||
if (yuge_at->retry > 10) {
|
||||
yuge_at->retry = 0;
|
||||
yuge_at->state = YUGE_STATE_UNKNOW;
|
||||
}
|
||||
// os_printf("recv state:%d\r\n", yuge_at->state);
|
||||
switch (yuge_at->state) {
|
||||
case YUGE_STATE_UNKNOW:
|
||||
yuge_at->retry = 0;
|
||||
yuge_at->state = YUGE_STATE_CHECK_AT_STATUS;
|
||||
break;
|
||||
case YUGE_STATE_CHECK_AT_STATUS:
|
||||
// AT查询模块是否工作启动初始化流程
|
||||
if (send_recv_atcmd_check(yuge_at, "AT\r\n", "OK", NULL) == RT_TRUE) {
|
||||
yuge_at->retry = 0;
|
||||
yuge_at->state = YUGE_STATE_CHECK_SIM_STATUS;
|
||||
os_printf("AT ready\r\n");
|
||||
} else {
|
||||
yuge_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case YUGE_STATE_CHECK_SIM_STATUS:
|
||||
if (send_recv_atcmd_check(yuge_at, "AT+CPIN?\r\n", "+CPIN: READY", NULL) == RT_TRUE) {
|
||||
yuge_at->retry = 0;
|
||||
yuge_at->state = YUGE_STATE_CHECK_CS_STATUS;
|
||||
os_printf("SIM ready\r\n");
|
||||
} else {
|
||||
yuge_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case YUGE_STATE_CHECK_CS_STATUS:
|
||||
// AT+CREG查询CS域网络注册状态
|
||||
if (send_recv_atcmd_check(yuge_at, "AT+CREG?\r\n", "+CREG: 0,1", NULL) == RT_TRUE) {
|
||||
yuge_at->retry = 0;
|
||||
yuge_at->state = YUGE_STATE_CHECK_PS_STATUS;
|
||||
os_printf("PS ready\r\n");
|
||||
} else {
|
||||
yuge_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case YUGE_STATE_CHECK_PS_STATUS:
|
||||
// AT+CEREG查询EPS域网络注册状态
|
||||
if (send_recv_atcmd_check(yuge_at, "AT+CEREG?\r\n", "+CEREG: 0,1", NULL) == RT_TRUE) {
|
||||
yuge_at->retry = 0;
|
||||
yuge_at->state = YUGE_STATE_INITIALIZED;
|
||||
SYSEVT_NEW_LTE_EVT(SYSEVT_LTE_CONNECTED, 0);
|
||||
os_printf("CS ready\r\n");
|
||||
} else {
|
||||
yuge_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case YUGE_STATE_CONFIG_PDP_CONTEXT:
|
||||
// 设置band优先级,排除band 40/41
|
||||
send_recv_atcmd_check(yuge_at, "AT+QCFG=\"band\",0x0,0x6200000095\r\n", "+QCFG: \"band\",", NULL);
|
||||
// AT+QICSGP设置场景参数
|
||||
if (send_recv_atcmd_check(yuge_at, "AT+QICSGP=1,1,\"UNINET\",\"\",\"\",1\r\n", "OK", NULL) == RT_TRUE) {
|
||||
yuge_at->retry = 0;
|
||||
yuge_at->state = YUGE_STATE_ACTIVE_PDP_CONTEXT;
|
||||
os_printf("PDP config\r\n");
|
||||
} else {
|
||||
yuge_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case YUGE_STATE_ACTIVE_PDP_CONTEXT:
|
||||
// AT+QIACT激活PDP场景
|
||||
if (send_recv_atcmd_check(yuge_at, "AT+QIACT=1\r\n", "OK", NULL) == RT_TRUE) {
|
||||
yuge_at->retry = 0;
|
||||
yuge_at->state = YUGE_STATE_CHECK_IP_STATUS;
|
||||
os_printf("PDP ready\r\n");
|
||||
} else {
|
||||
yuge_at->retry++;
|
||||
if (yuge_at->retry > 3) {
|
||||
yuge_at->retry = 0;
|
||||
yuge_at->state = YUGE_STATE_DEACTIVE_PDP_CONTEXT;
|
||||
}
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case YUGE_STATE_CHECK_IP_STATUS:
|
||||
// AT+QIACT查询激活PDP场景和IP
|
||||
if (send_recv_atcmd_check(yuge_at, "AT+QIACT?\r\n", "+QIACT: 1,1,1,", recv_str) == RT_TRUE) {
|
||||
yuge_at->retry = 0;
|
||||
yuge_at->state = YUGE_STATE_CONNECT_USB_ADAPTER;
|
||||
os_printf("Get IP: %s", recv_str);
|
||||
os_printf("PDP actived\r\n");
|
||||
} else {
|
||||
yuge_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case YUGE_STATE_CONNECT_USB_ADAPTER:
|
||||
// AT+QNETDEVCTL连接USB网卡
|
||||
if (send_recv_atcmd_check(yuge_at, "AT+QNETDEVCTL=1,1,1\r\n", "OK", recv_str) == RT_TRUE) {
|
||||
yuge_at->retry = 0;
|
||||
yuge_at->state = YUGE_STATE_INITIALIZED;
|
||||
SYSEVT_NEW_LTE_EVT(SYSEVT_LTE_CONNECTED, 0);
|
||||
os_printf("network conneted\r\n");
|
||||
} else {
|
||||
yuge_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case YUGE_STATE_INITIALIZED:
|
||||
// 定时10s不知道干什么好呢
|
||||
yuge_network_info(yuge_at, recv_str);
|
||||
os_sleep(10);
|
||||
break;
|
||||
case YUGE_STATE_DEACTIVE_PDP_CONTEXT:
|
||||
// 失败后反激活模组
|
||||
if (send_recv_atcmd_check(yuge_at, "AT+QIDEACT=1\r\n", "OK", NULL) == RT_TRUE) {
|
||||
yuge_at->retry = 0;
|
||||
yuge_at->state = YUGE_STATE_CHECK_SIM_STATUS;
|
||||
os_printf("PDP deactived\r\n");
|
||||
} else {
|
||||
yuge_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case YUGE_STATE_POWERDOWN:
|
||||
if (send_recv_atcmd_check(yuge_at, "AT+QPOWD\r\n", "POWERED DOWN", NULL) == RT_TRUE) {
|
||||
yuge_at->retry = 0;
|
||||
os_printf("LTE power down\r\n");
|
||||
os_sleep(2);
|
||||
mcu_reset();
|
||||
} else {
|
||||
yuge_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
os_printf("at task end\r\n");
|
||||
}
|
||||
|
||||
static rt_err_t rt_usbh_yuge_enable(void *arg)
|
||||
{
|
||||
rt_err_t ret = RET_OK;
|
||||
struct uhintf **intf = arg;
|
||||
uhcd_t hcd = NULL;
|
||||
uep_desc_t ep_desc = NULL;
|
||||
struct ustring_descriptor str_desc __attribute__((aligned(4)));
|
||||
struct usb_yuge_at *yuge_at = NULL;
|
||||
struct usb_cdc_line_coding line_coding;
|
||||
upipe_t pipe;
|
||||
rt_uint8_t ep_index, i;
|
||||
|
||||
if (intf[0] == NULL) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
hcd = intf[0]->device->hcd;
|
||||
os_printf("subclass %d, protocal %d\r\n",
|
||||
intf[0]->intf_desc->bInterfaceSubClass,
|
||||
intf[0]->intf_desc->bInterfaceProtocol);
|
||||
|
||||
if (intf[0]->intf_desc->bInterfaceSubClass == 2 && intf[0]->intf_desc->bInterfaceProtocol == 1) {
|
||||
// 域格的AT口也是走复合设备描述符的
|
||||
if (intf[0]->device->dev_desc.idProduct != USB_PRODUCT_ID_YUGE) {
|
||||
return RET_ERR;
|
||||
}
|
||||
// 域格有多个复合设备,第二个复合设备是AT串口,算起来接口号是3,但索引是用复合设备前面的那个所以是2
|
||||
if (intf[0]->intf_desc->bInterfaceNumber != 2) {
|
||||
return RET_ERR;
|
||||
}
|
||||
// 通过字符描述符判断哪个是主调试串口
|
||||
ret = rt_usbh_get_string_descriptor(intf[0]->device, intf[0]->intf_desc->iInterface,
|
||||
&str_desc, sizeof(struct ustring_descriptor));
|
||||
if (ret != RET_OK) {
|
||||
os_printf("no string\r\n");
|
||||
return ret;
|
||||
}
|
||||
for (i = 0; i < str_desc.bLength; i += 2) { // 暂时没支持UNICODE的打印,默认ASCII可以隔一个打印
|
||||
_os_printf("%c", str_desc.String[i / 2]);
|
||||
}
|
||||
_os_printf("\r\n");
|
||||
|
||||
os_printf("yuge_at\r\n");
|
||||
yuge_at = (struct usb_yuge_at *)os_zalloc(sizeof(struct usb_yuge_at));
|
||||
if (yuge_at == NULL) {
|
||||
os_printf("yuge at alloc fail\r\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
// 按照命令预期回复长度对齐预留长度
|
||||
yuge_at->at_cmd_buff = os_malloc(YUGE_ATCMD_BUFF_SIZE);
|
||||
if (yuge_at->at_cmd_buff == NULL) {
|
||||
os_printf("at_cmd_buff alloc fail\r\n");
|
||||
os_free(yuge_at);
|
||||
return -ENOMEM;
|
||||
}
|
||||
yuge_at->device = intf[0]->device;
|
||||
intf[0]->user_data = yuge_at;
|
||||
// 顺便注册devid
|
||||
dev_register(HG_USB_AT_DEVID, (struct dev_obj *)yuge_at);
|
||||
|
||||
for (ep_index = 0; ep_index < intf[1]->intf_desc->bNumEndpoints; ++ep_index) {
|
||||
rt_usbh_get_endpoint_descriptor(intf[1]->intf_desc, ep_index, &ep_desc);
|
||||
if (ep_desc == NULL) {
|
||||
os_printf("no ep_desc\r\n");
|
||||
return RET_ERR;
|
||||
}
|
||||
|
||||
if ((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_BULK)
|
||||
continue;
|
||||
|
||||
if (rt_usb_hcd_alloc_pipe(intf[0]->device->hcd, &pipe, intf[0]->device, ep_desc) != RT_EOK) {
|
||||
rt_kprintf("alloc pipe failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
rt_usb_instance_add_pipe(intf[0]->device, pipe);
|
||||
if ((ep_desc->bEndpointAddress & USB_DIR_MASK) == USB_DIR_IN) {
|
||||
yuge_at->pipe_in = pipe;
|
||||
} else {
|
||||
yuge_at->pipe_out = pipe;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取串口参数
|
||||
os_memset(&line_coding, 0, sizeof(line_coding));
|
||||
rt_usbh_cdc_get_line_coding(intf[0]->device, intf[0]->intf_desc->bInterfaceNumber, &line_coding);
|
||||
os_printf("Serial: %d %d %d %d\r\n",
|
||||
line_coding.dwDTERate, line_coding.bCharFormat, line_coding.bParityType, line_coding.bDataBits);
|
||||
|
||||
rt_thread_init(&yuge_at->recv_task, "at_recv", yuge_at_recv, yuge_at,
|
||||
NULL, 512, OS_TASK_PRIORITY_BELOW_NORMAL, 20);
|
||||
rt_thread_startup(&yuge_at->recv_task);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static rt_err_t rt_usbh_yuge_disable(void *arg)
|
||||
{
|
||||
struct uhintf *intf = arg;
|
||||
uhcd_t hcd = NULL;
|
||||
struct usb_yuge_at *yuge_at = NULL;
|
||||
|
||||
if (intf == NULL) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
yuge_at = intf->user_data;
|
||||
if (yuge_at) {
|
||||
hcd = intf->device->hcd;
|
||||
dev_unregister((struct dev_obj *)yuge_at);
|
||||
rt_thread_detach(&yuge_at->recv_task);
|
||||
os_free(yuge_at->at_cmd_buff);
|
||||
os_free(yuge_at);
|
||||
}
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
void rt_usbh_yuge_at_run(void *arg)
|
||||
{
|
||||
rt_usbh_yuge_enable(arg);
|
||||
}
|
||||
|
||||
void rt_usbh_yuge_at_stop(void *arg)
|
||||
{
|
||||
rt_usbh_yuge_disable(arg);
|
||||
}
|
||||
|
||||
// 中云信安使用CDC类型接口,不用注册VENDOR了,从cdc.c中调用enable处理
|
||||
ucd_t rt_usbh_class_driver_yuge(void)
|
||||
{
|
||||
yuge_driver.class_code = USB_CLASS_CDC;
|
||||
|
||||
yuge_driver.enable = rt_usbh_yuge_enable;
|
||||
yuge_driver.disable = rt_usbh_yuge_disable;
|
||||
|
||||
return &yuge_driver;
|
||||
}
|
||||
|
||||
#endif
|
||||
48
sdk/lib/bus/rttusb/usbhost/class/yuge.h
Normal file
48
sdk/lib/bus/rttusb/usbhost/class/yuge.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef __CLASS_YUGE_H__
|
||||
#define __CLASS_YUGE_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#define USB_VENDOR_ID_YUGE 0x19D1
|
||||
#define USB_PRODUCT_ID_YUGE 0x1003 // YM310 X09
|
||||
|
||||
enum quectel_state {
|
||||
YUGE_STATE_UNKNOW,
|
||||
YUGE_STATE_CHECK_AT_STATUS,
|
||||
YUGE_STATE_CHECK_SIM_STATUS,
|
||||
YUGE_STATE_CHECK_CS_STATUS,
|
||||
YUGE_STATE_CHECK_PS_STATUS,
|
||||
YUGE_STATE_CHECK_USBNET_STATUS,
|
||||
YUGE_STATE_CONFIG_USBNET_STATUS,
|
||||
YUGE_STATE_CONFIG_PDP_CONTEXT,
|
||||
YUGE_STATE_ACTIVE_PDP_CONTEXT,
|
||||
YUGE_STATE_CHECK_IP_STATUS,
|
||||
YUGE_STATE_CONNECT_USB_ADAPTER,
|
||||
YUGE_STATE_INITIALIZED,
|
||||
YUGE_STATE_DEACTIVE_PDP_CONTEXT,
|
||||
YUGE_STATE_POWERDOWN,
|
||||
};
|
||||
|
||||
struct usb_yuge_at {
|
||||
struct dev_obj dev;
|
||||
void *device;
|
||||
struct rt_thread recv_task;
|
||||
rt_uint8_t *at_cmd_buff;
|
||||
rt_uint32_t state;
|
||||
upipe_t pipe_in;
|
||||
upipe_t pipe_out;
|
||||
rt_uint8_t retry;
|
||||
};
|
||||
|
||||
void rt_usbh_yuge_at_run(void *arg);
|
||||
void rt_usbh_yuge_at_stop(void *arg);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
473
sdk/lib/bus/rttusb/usbhost/class/zxinfo.c
Normal file
473
sdk/lib/bus/rttusb/usbhost/class/zxinfo.c
Normal file
@@ -0,0 +1,473 @@
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
#include "zxinfo.h"
|
||||
#include "cdc.h"
|
||||
#include "rndis.h"
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/netdb.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/tcpip.h"
|
||||
#include "netif/ethernetif.h"
|
||||
#include "lib/common/sysevt.h"
|
||||
#include "syscfg.h"
|
||||
|
||||
#ifdef RT_USBH_VENDOR_ZXINFO
|
||||
|
||||
// #define USB_VENDOR_ID_ZXINFO 0x3361
|
||||
// #define USB_PRODUCT_ID_ZXINFO 0x7B6E // ZX800
|
||||
|
||||
#define ZXINFO_ATCMD_BUFF_SIZE 128
|
||||
|
||||
static char recv_str[ZXINFO_ATCMD_BUFF_SIZE];
|
||||
|
||||
static struct uclass_driver zxinfo_driver;
|
||||
|
||||
__weak void mifi_led_control(rt_int16_t rsrq, rt_int16_t rssi){};
|
||||
|
||||
static rt_bool_t send_recv_atcmd_check(void *context, const char *send_str,
|
||||
const char *check_str, char *ret_str)
|
||||
{
|
||||
struct usb_zxinfo_at *zxinfo_at = context;
|
||||
uinst_t device = zxinfo_at->device;
|
||||
int recv_size;
|
||||
char *recv_str = NULL;
|
||||
rt_bool_t pass = RT_FALSE;
|
||||
|
||||
os_sprintf((char *)zxinfo_at->at_cmd_buff, send_str);
|
||||
rt_usb_hcd_pipe_xfer(device->hcd, zxinfo_at->pipe_out,
|
||||
zxinfo_at->at_cmd_buff, os_strlen(send_str), 0);
|
||||
os_sleep_ms(10); // 需要点延迟给LTE模组反应
|
||||
do {
|
||||
// 每次读取会清除缓存
|
||||
os_memset(zxinfo_at->at_cmd_buff, 0, ZXINFO_ATCMD_BUFF_SIZE);
|
||||
recv_size = rt_usb_hcd_pipe_xfer(device->hcd, zxinfo_at->pipe_in,
|
||||
zxinfo_at->at_cmd_buff, ZXINFO_ATCMD_BUFF_SIZE, 10);
|
||||
if (recv_size > 0) {
|
||||
recv_str = os_strstr(zxinfo_at->at_cmd_buff, check_str);
|
||||
if (recv_str != NULL) {
|
||||
pass = RT_TRUE;
|
||||
if (ret_str != NULL && recv_size > os_strlen(check_str)) {
|
||||
// ret_str存在说明需要获取返回结果做额外判断,复制到函数外面
|
||||
os_memcpy(ret_str, recv_str + os_strlen(check_str),
|
||||
recv_size - os_strlen(check_str));
|
||||
ret_str[recv_size - os_strlen(check_str)] = '\0'; // 字符串结束符
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (recv_size > 0);
|
||||
|
||||
return pass;
|
||||
}
|
||||
|
||||
// 计算下行频点,FDD不知道上行频点号,猜测直接偏固定频率?
|
||||
static rt_uint16_t eutra_channel_freq_mapping(rt_uint8_t band, rt_uint16_t earfcn, rt_bool_t uplink)
|
||||
{
|
||||
rt_uint16_t freq = 0;
|
||||
|
||||
switch (band) {
|
||||
// FDD上下行频点有偏差
|
||||
case 3:
|
||||
freq = 1805 + (earfcn - 1200) / 10;
|
||||
if (uplink)
|
||||
freq -= 95;
|
||||
break;
|
||||
case 5:
|
||||
freq = 869 + (earfcn - 2400) / 10;
|
||||
if (uplink)
|
||||
freq -= 45;
|
||||
break;
|
||||
case 8:
|
||||
freq = 925 + (earfcn - 3450) / 10;
|
||||
if (uplink)
|
||||
freq -= 45;
|
||||
break;
|
||||
// TDD上下行使用相同频点
|
||||
case 34: freq = 2010 + (earfcn - 36200) / 10; break;
|
||||
case 38: freq = 2570 + (earfcn - 37750) / 10; break;
|
||||
case 39: freq = 1880 + (earfcn - 38250) / 10; break;
|
||||
case 40: freq = 2300 + (earfcn - 38650) / 10; break;
|
||||
case 41: freq = 2496 + (earfcn - 39650) / 10; break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
return freq;
|
||||
}
|
||||
|
||||
static void zxinfo_network_info(void *context, char *recv_str)
|
||||
{
|
||||
struct usb_zxinfo_at *zxinfo_at = context;
|
||||
//char *argv[4];
|
||||
char **argv = NULL;
|
||||
int argc = 0;
|
||||
rt_int16_t rssi, ber;
|
||||
rt_uint16_t num_dl;
|
||||
rt_uint16_t freq_dl, freq_ul;
|
||||
rt_uint16_t freq_wifi = 0;
|
||||
rt_uint8_t band = 0, rxqual = 0;
|
||||
|
||||
argv = os_malloc(4 * sizeof(char *));
|
||||
if (argv == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取运营商
|
||||
if (send_recv_atcmd_check(zxinfo_at, "AT+COPS?\r\n", "+COPS:", recv_str) == RT_TRUE) {
|
||||
argc = os_strtok(recv_str, ",", argv, 4);
|
||||
if (argc >= 3) {
|
||||
// 0,0,"CHINA MOBILE",7
|
||||
_os_printf("%s\r\n", argv[2]);
|
||||
}
|
||||
}
|
||||
// 获取网络信息
|
||||
if (send_recv_atcmd_check(zxinfo_at, "AT+QNWINFO\r\n", "+QNWINFO:", recv_str) == RT_TRUE) {
|
||||
argc = os_strtok(recv_str, ",", argv, 4);
|
||||
if (argc >= 4) {
|
||||
// "FDD LTE",46001,"LTE BAND 3",1650
|
||||
band = os_atoi(argv[2] + 10);
|
||||
num_dl = os_atoi(argv[3]);
|
||||
freq_dl = eutra_channel_freq_mapping(band, num_dl, 0);
|
||||
freq_ul = eutra_channel_freq_mapping(band, num_dl, 1);
|
||||
_os_printf("BAND: %d\r\n", band);
|
||||
_os_printf("F_dl: %d MHz, F_ul: %d MHz\r\n", freq_dl, freq_ul);
|
||||
// 检查有无和wifi频点冲突
|
||||
freq_wifi = sys_status.channel * 5 + 2407;
|
||||
// TDD同频ul和dl一致
|
||||
if (band == 40) {
|
||||
if ((freq_wifi - freq_dl < 90) && sys_status.channel != 13) {
|
||||
os_printf("!!!!!!! OVERLAP at %d and %d !!!!!!!\r\n", freq_wifi, freq_dl);
|
||||
SYSEVT_NEW_LTE_EVT(SYSEVT_LTE_OVERLAP_WIFI, 13); // 切去高信道
|
||||
}
|
||||
} else if (band == 41) {
|
||||
if ((freq_dl - freq_wifi < 90) && sys_status.channel != 1) {
|
||||
os_printf("!!!!!!! OVERLAP at %d and %d !!!!!!!\r\n", freq_wifi, freq_dl);
|
||||
SYSEVT_NEW_LTE_EVT(SYSEVT_LTE_OVERLAP_WIFI, 1); // 切去低信道
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (send_recv_atcmd_check(zxinfo_at, "AT+CSQ\r\n", "+CSQ:", recv_str) == RT_TRUE) {
|
||||
argc = os_strtok(recv_str, ",", argv, 4);
|
||||
if (argc >= 2) {
|
||||
// 28,99
|
||||
rssi = os_atoi(argv[0]); // dBm
|
||||
rxqual = os_atoi(argv[1]);
|
||||
mifi_led_control(0, -113 + rssi * 2);
|
||||
if (rssi == 99) {
|
||||
_os_printf("RSSI: unknow\r\n");
|
||||
} else if (rssi <= 0) {
|
||||
_os_printf("RSSI: <= -113 dBm\r\n");
|
||||
} else if (rssi >= 31) {
|
||||
_os_printf("RSSI: >= -51 dBm\r\n");
|
||||
} else {
|
||||
_os_printf("RSSI: %d dBm\r\n", -113 + rssi * 2);
|
||||
}
|
||||
// RXQUAL_0 BER < 0,2 % Assumed value = 0,14 %
|
||||
// RXQUAL_1 0,2 % < BER < 0,4 % Assumed value = 0,28 %
|
||||
// RXQUAL_2 0,4 % < BER < 0,8 % Assumed value = 0,57 %
|
||||
// RXQUAL_3 0,8 % < BER < 1,6 % Assumed value = 1,13 %
|
||||
// RXQUAL_4 1,6 % < BER < 3,2 % Assumed value = 2,26 %
|
||||
// RXQUAL_5 3,2 % < BER < 6,4 % Assumed value = 4,53 %
|
||||
// RXQUAL_6 6,4 % < BER < 12,8 % Assumed value = 9,05 %
|
||||
// RXQUAL_7 12,8 % < BER Assumed value = 18,10 %
|
||||
ber = 1 << (rxqual + 1);
|
||||
if (rxqual == 99) {
|
||||
_os_printf("BER: unknow\r\n");
|
||||
} else if (rxqual <= 0) {
|
||||
_os_printf("BER: < 0.2 %%\r\n");
|
||||
} else if (rxqual >= 7) {
|
||||
_os_printf("BER: > 12.8 %%\r\n");
|
||||
} else {
|
||||
_os_printf("BER: %d.%d %% ~ %d.%d %%\r\n", (ber/2)/10, (ber/2)%10, ber/10, ber%10);
|
||||
}
|
||||
}
|
||||
}
|
||||
os_free(argv);
|
||||
}
|
||||
|
||||
static void zxinfo_at_recv(void *context)
|
||||
{
|
||||
struct usb_zxinfo_at *zxinfo_at = context;
|
||||
|
||||
while (1) {
|
||||
if (zxinfo_at->retry > 10) {
|
||||
zxinfo_at->retry = 0;
|
||||
zxinfo_at->state = ZXINFO_STATE_UNKNOW;
|
||||
}
|
||||
// os_printf("recv state:%d\r\n", zxinfo_at->state);
|
||||
switch (zxinfo_at->state) {
|
||||
case ZXINFO_STATE_UNKNOW:
|
||||
zxinfo_at->retry = 0;
|
||||
zxinfo_at->state = ZXINFO_STATE_CHECK_AT_STATUS;
|
||||
break;
|
||||
case ZXINFO_STATE_CHECK_AT_STATUS:
|
||||
// AT查询模块是否工作启动初始化流程
|
||||
if (send_recv_atcmd_check(zxinfo_at, "AT\r\n", "OK", NULL) == RT_TRUE) {
|
||||
zxinfo_at->retry = 0;
|
||||
zxinfo_at->state = ZXINFO_STATE_CHECK_SIM_STATUS;
|
||||
os_printf("AT ready\r\n");
|
||||
} else {
|
||||
zxinfo_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case ZXINFO_STATE_CHECK_SIM_STATUS:
|
||||
if (send_recv_atcmd_check(zxinfo_at, "AT+CPIN?\r\n", "+CPIN: READY", NULL) == RT_TRUE) {
|
||||
zxinfo_at->retry = 0;
|
||||
zxinfo_at->state = ZXINFO_STATE_CHECK_CS_STATUS;
|
||||
os_printf("SIM ready\r\n");
|
||||
} else {
|
||||
zxinfo_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case ZXINFO_STATE_CHECK_CS_STATUS:
|
||||
// AT+CREG查询CS域网络注册状态
|
||||
if (send_recv_atcmd_check(zxinfo_at, "AT+CREG?\r\n", "+CREG: 0,1", NULL) == RT_TRUE) {
|
||||
zxinfo_at->retry = 0;
|
||||
zxinfo_at->state = ZXINFO_STATE_CHECK_PS_STATUS;
|
||||
os_printf("PS ready\r\n");
|
||||
} else {
|
||||
zxinfo_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case ZXINFO_STATE_CHECK_PS_STATUS:
|
||||
// AT+CEREG查询EPS域网络注册状态
|
||||
if (send_recv_atcmd_check(zxinfo_at, "AT+CEREG?\r\n", "+CEREG: 0,1", NULL) == RT_TRUE) {
|
||||
zxinfo_at->retry = 0;
|
||||
zxinfo_at->state = ZXINFO_STATE_INITIALIZED;
|
||||
SYSEVT_NEW_LTE_EVT(SYSEVT_LTE_CONNECTED, 0);
|
||||
os_printf("CS ready\r\n");
|
||||
} else {
|
||||
zxinfo_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case ZXINFO_STATE_CONFIG_PDP_CONTEXT:
|
||||
// 设置band优先级,排除band 40/41
|
||||
send_recv_atcmd_check(zxinfo_at, "AT+QCFG=\"band\",0x0,0x6200000095\r\n", "+QCFG: \"band\",", NULL);
|
||||
// AT+QICSGP设置场景参数
|
||||
if (send_recv_atcmd_check(zxinfo_at, "AT+QICSGP=1,1,\"UNINET\",\"\",\"\",1\r\n", "OK", NULL) == RT_TRUE) {
|
||||
zxinfo_at->retry = 0;
|
||||
zxinfo_at->state = ZXINFO_STATE_ACTIVE_PDP_CONTEXT;
|
||||
os_printf("PDP config\r\n");
|
||||
} else {
|
||||
zxinfo_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case ZXINFO_STATE_ACTIVE_PDP_CONTEXT:
|
||||
// AT+QIACT激活PDP场景
|
||||
if (send_recv_atcmd_check(zxinfo_at, "AT+QIACT=1\r\n", "OK", NULL) == RT_TRUE) {
|
||||
zxinfo_at->retry = 0;
|
||||
zxinfo_at->state = ZXINFO_STATE_CHECK_IP_STATUS;
|
||||
os_printf("PDP ready\r\n");
|
||||
} else {
|
||||
zxinfo_at->retry++;
|
||||
if (zxinfo_at->retry > 3) {
|
||||
zxinfo_at->retry = 0;
|
||||
zxinfo_at->state = ZXINFO_STATE_DEACTIVE_PDP_CONTEXT;
|
||||
}
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case ZXINFO_STATE_CHECK_IP_STATUS:
|
||||
// AT+QIACT查询激活PDP场景和IP
|
||||
if (send_recv_atcmd_check(zxinfo_at, "AT+QIACT?\r\n", "+QIACT: 1,1,1,", recv_str) == RT_TRUE) {
|
||||
zxinfo_at->retry = 0;
|
||||
zxinfo_at->state = ZXINFO_STATE_CONNECT_USB_ADAPTER;
|
||||
os_printf("Get IP: %s", recv_str);
|
||||
os_printf("PDP actived\r\n");
|
||||
} else {
|
||||
zxinfo_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case ZXINFO_STATE_CONNECT_USB_ADAPTER:
|
||||
// AT+QNETDEVCTL连接USB网卡
|
||||
if (send_recv_atcmd_check(zxinfo_at, "AT+QNETDEVCTL=1,1,1\r\n", "OK", recv_str) == RT_TRUE) {
|
||||
zxinfo_at->retry = 0;
|
||||
zxinfo_at->state = ZXINFO_STATE_INITIALIZED;
|
||||
SYSEVT_NEW_LTE_EVT(SYSEVT_LTE_CONNECTED, 0);
|
||||
os_printf("network conneted\r\n");
|
||||
} else {
|
||||
zxinfo_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case ZXINFO_STATE_INITIALIZED:
|
||||
// 定时10s不知道干什么好呢
|
||||
zxinfo_network_info(zxinfo_at, recv_str);
|
||||
os_sleep(10);
|
||||
break;
|
||||
case ZXINFO_STATE_DEACTIVE_PDP_CONTEXT:
|
||||
// 失败后反激活模组
|
||||
if (send_recv_atcmd_check(zxinfo_at, "AT+QIDEACT=1\r\n", "OK", NULL) == RT_TRUE) {
|
||||
zxinfo_at->retry = 0;
|
||||
zxinfo_at->state = ZXINFO_STATE_CHECK_SIM_STATUS;
|
||||
os_printf("PDP deactived\r\n");
|
||||
} else {
|
||||
zxinfo_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
case ZXINFO_STATE_POWERDOWN:
|
||||
if (send_recv_atcmd_check(zxinfo_at, "AT+QPOWD\r\n", "POWERED DOWN", NULL) == RT_TRUE) {
|
||||
zxinfo_at->retry = 0;
|
||||
os_printf("LTE power down\r\n");
|
||||
os_sleep(2);
|
||||
mcu_reset();
|
||||
} else {
|
||||
zxinfo_at->retry++;
|
||||
os_sleep(1);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
os_printf("at task end\r\n");
|
||||
}
|
||||
|
||||
static rt_err_t rt_usbh_zxinfo_enable(void *arg)
|
||||
{
|
||||
rt_err_t ret = RET_OK;
|
||||
struct uhintf **intf = arg;
|
||||
uhcd_t hcd = NULL;
|
||||
uep_desc_t ep_desc = NULL;
|
||||
struct ustring_descriptor str_desc __attribute__((aligned(4)));
|
||||
struct usb_zxinfo_at *zxinfo_at = NULL;
|
||||
struct usb_cdc_line_coding line_coding;
|
||||
upipe_t pipe;
|
||||
rt_uint8_t ep_index, i;
|
||||
|
||||
if (intf[0] == NULL) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
hcd = intf[0]->device->hcd;
|
||||
os_printf("subclass %d, protocal %d\r\n",
|
||||
intf[0]->intf_desc->bInterfaceSubClass,
|
||||
intf[0]->intf_desc->bInterfaceProtocol);
|
||||
|
||||
if (intf[0]->intf_desc->bInterfaceSubClass == 2 && intf[0]->intf_desc->bInterfaceProtocol == 1) {
|
||||
// 中云信安的AT口也是走复合设备描述符的
|
||||
if (intf[0]->device->dev_desc.idProduct != USB_PRODUCT_ID_ZXINFO) {
|
||||
return RET_ERR;
|
||||
}
|
||||
// 中云信安有多个复合设备,第二个复合设备是AT串口,算起来接口号是5,但索引是用复合设备前面的那个所以是4
|
||||
if (intf[0]->intf_desc->bInterfaceNumber != 4) {
|
||||
return RET_ERR;
|
||||
}
|
||||
// 通过字符描述符判断哪个是主调试串口
|
||||
ret = rt_usbh_get_string_descriptor(intf[0]->device, intf[0]->intf_desc->iInterface,
|
||||
&str_desc, sizeof(struct ustring_descriptor));
|
||||
if (ret != RET_OK) {
|
||||
os_printf("no string\r\n");
|
||||
return ret;
|
||||
}
|
||||
for (i = 0; i < str_desc.bLength; i += 2) { // 暂时没支持UNICODE的打印,默认ASCII可以隔一个打印
|
||||
_os_printf("%c", str_desc.String[i / 2]);
|
||||
}
|
||||
_os_printf("\r\n");
|
||||
|
||||
os_printf("zxinfo_at\r\n");
|
||||
zxinfo_at = (struct usb_zxinfo_at *)os_zalloc(sizeof(struct usb_zxinfo_at));
|
||||
if (zxinfo_at == NULL) {
|
||||
os_printf("zxinfo at alloc fail\r\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
// 按照命令预期回复长度对齐预留长度
|
||||
zxinfo_at->at_cmd_buff = os_malloc(ZXINFO_ATCMD_BUFF_SIZE);
|
||||
if (zxinfo_at->at_cmd_buff == NULL) {
|
||||
os_printf("at_cmd_buff alloc fail\r\n");
|
||||
os_free(zxinfo_at);
|
||||
return -ENOMEM;
|
||||
}
|
||||
zxinfo_at->device = intf[0]->device;
|
||||
intf[0]->user_data = zxinfo_at;
|
||||
// 顺便注册devid
|
||||
dev_register(HG_USB_AT_DEVID, (struct dev_obj *)zxinfo_at);
|
||||
|
||||
for (ep_index = 0; ep_index < intf[1]->intf_desc->bNumEndpoints; ++ep_index) {
|
||||
rt_usbh_get_endpoint_descriptor(intf[1]->intf_desc, ep_index, &ep_desc);
|
||||
if (ep_desc == NULL) {
|
||||
os_printf("no ep_desc\r\n");
|
||||
return RET_ERR;
|
||||
}
|
||||
|
||||
if ((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_BULK)
|
||||
continue;
|
||||
|
||||
if (rt_usb_hcd_alloc_pipe(intf[0]->device->hcd, &pipe, intf[0]->device, ep_desc) != RT_EOK) {
|
||||
rt_kprintf("alloc pipe failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
rt_usb_instance_add_pipe(intf[0]->device, pipe);
|
||||
if ((ep_desc->bEndpointAddress & USB_DIR_MASK) == USB_DIR_IN) {
|
||||
zxinfo_at->pipe_in = pipe;
|
||||
} else {
|
||||
zxinfo_at->pipe_out = pipe;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取串口参数
|
||||
os_memset(&line_coding, 0, sizeof(line_coding));
|
||||
rt_usbh_cdc_get_line_coding(intf[0]->device, intf[0]->intf_desc->bInterfaceNumber, &line_coding);
|
||||
os_printf("Serial: %d %d %d %d\r\n",
|
||||
line_coding.dwDTERate, line_coding.bCharFormat, line_coding.bParityType, line_coding.bDataBits);
|
||||
|
||||
rt_thread_init(&zxinfo_at->recv_task, "at_recv", zxinfo_at_recv, zxinfo_at,
|
||||
NULL, 512, OS_TASK_PRIORITY_BELOW_NORMAL, 20);
|
||||
rt_thread_startup(&zxinfo_at->recv_task);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static rt_err_t rt_usbh_zxinfo_disable(void *arg)
|
||||
{
|
||||
struct uhintf *intf = arg;
|
||||
uhcd_t hcd = NULL;
|
||||
struct usb_zxinfo_at *zxinfo_at = NULL;
|
||||
|
||||
if (intf == NULL) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
zxinfo_at = intf->user_data;
|
||||
if (zxinfo_at) {
|
||||
hcd = intf->device->hcd;
|
||||
dev_unregister((struct dev_obj *)zxinfo_at);
|
||||
rt_thread_detach(&zxinfo_at->recv_task);
|
||||
os_free(zxinfo_at->at_cmd_buff);
|
||||
os_free(zxinfo_at);
|
||||
}
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
void rt_usbh_zxinfo_at_run(void *arg)
|
||||
{
|
||||
rt_usbh_zxinfo_enable(arg);
|
||||
}
|
||||
|
||||
void rt_usbh_zxinfo_at_stop(void *arg)
|
||||
{
|
||||
rt_usbh_zxinfo_disable(arg);
|
||||
}
|
||||
|
||||
// 中云信安使用CDC类型接口,不用注册VENDOR了,从cdc.c中调用enable处理
|
||||
ucd_t rt_usbh_class_driver_zxinfo(void)
|
||||
{
|
||||
zxinfo_driver.class_code = USB_CLASS_CDC;
|
||||
|
||||
zxinfo_driver.enable = rt_usbh_zxinfo_enable;
|
||||
zxinfo_driver.disable = rt_usbh_zxinfo_disable;
|
||||
|
||||
return &zxinfo_driver;
|
||||
}
|
||||
|
||||
#endif
|
||||
48
sdk/lib/bus/rttusb/usbhost/class/zxinfo.h
Normal file
48
sdk/lib/bus/rttusb/usbhost/class/zxinfo.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef __CLASS_ZXINFO_H__
|
||||
#define __CLASS_ZXINFO_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#define USB_VENDOR_ID_ZXINFO 0x3361
|
||||
#define USB_PRODUCT_ID_ZXINFO 0x7B6E // ZX800
|
||||
|
||||
enum zxinfo_state {
|
||||
ZXINFO_STATE_UNKNOW,
|
||||
ZXINFO_STATE_CHECK_AT_STATUS,
|
||||
ZXINFO_STATE_CHECK_SIM_STATUS,
|
||||
ZXINFO_STATE_CHECK_CS_STATUS,
|
||||
ZXINFO_STATE_CHECK_PS_STATUS,
|
||||
ZXINFO_STATE_CHECK_USBNET_STATUS,
|
||||
ZXINFO_STATE_CONFIG_USBNET_STATUS,
|
||||
ZXINFO_STATE_CONFIG_PDP_CONTEXT,
|
||||
ZXINFO_STATE_ACTIVE_PDP_CONTEXT,
|
||||
ZXINFO_STATE_CHECK_IP_STATUS,
|
||||
ZXINFO_STATE_CONNECT_USB_ADAPTER,
|
||||
ZXINFO_STATE_INITIALIZED,
|
||||
ZXINFO_STATE_DEACTIVE_PDP_CONTEXT,
|
||||
ZXINFO_STATE_POWERDOWN,
|
||||
};
|
||||
|
||||
struct usb_zxinfo_at {
|
||||
struct dev_obj dev;
|
||||
void *device;
|
||||
struct rt_thread recv_task;
|
||||
rt_uint8_t *at_cmd_buff;
|
||||
rt_uint32_t state;
|
||||
upipe_t pipe_in;
|
||||
upipe_t pipe_out;
|
||||
rt_uint8_t retry;
|
||||
};
|
||||
|
||||
void rt_usbh_zxinfo_at_run(void *arg);
|
||||
void rt_usbh_zxinfo_at_stop(void *arg);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
155
sdk/lib/bus/rttusb/usbhost/core/driver.c
Normal file
155
sdk/lib/bus/rttusb/usbhost/core/driver.c
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-03-12 Yi Qiu first version
|
||||
* 2021-02-23 Leslie Lee provide possibility for multi usb host
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtservice.h>
|
||||
#include <include/rttusb_host.h>
|
||||
|
||||
static rt_list_t _driver_list;
|
||||
static rt_bool_t _driver_list_created = RT_FALSE;
|
||||
|
||||
/**
|
||||
* This function will initilize the usb class driver related data structure,
|
||||
* and it should be invoked in the usb system initialization.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_class_driver_init(void)
|
||||
{
|
||||
if (_driver_list_created == RT_FALSE)
|
||||
{
|
||||
rt_list_init(&_driver_list);
|
||||
_driver_list_created = RT_TRUE;
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will register an usb class driver to the class driver manager.
|
||||
*
|
||||
* @param drv the pointer of the usb class driver.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
|
||||
rt_err_t rt_usbh_class_driver_register(ucd_t drv)
|
||||
{
|
||||
if (drv == RT_NULL) return -RT_ERROR;
|
||||
|
||||
if (rt_usbh_class_driver_find(drv->class_code, drv->subclass_code, drv->vendor_id) == RT_NULL)
|
||||
{
|
||||
/* insert class driver into driver list */
|
||||
rt_list_insert_after(&_driver_list, &(drv->list));
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will removes a previously registed usb class driver.
|
||||
*
|
||||
* @param drv the pointer of the usb class driver structure.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_class_driver_unregister(ucd_t drv)
|
||||
{
|
||||
RT_ASSERT(drv != RT_NULL);
|
||||
|
||||
/* remove class driver from driver list */
|
||||
rt_list_remove(&(drv->list));
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will run an usb class driver.
|
||||
*
|
||||
* @param drv the pointer of usb class driver.
|
||||
* @param args the parameter of run function.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_class_driver_enable(ucd_t drv, void* args)
|
||||
{
|
||||
RT_ASSERT(drv != RT_NULL);
|
||||
|
||||
if(drv->enable != RT_NULL)
|
||||
drv->enable(args);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will stop a usb class driver.
|
||||
*
|
||||
* @param drv the pointer of usb class driver structure.
|
||||
* @param args the argument of the stop function.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_class_driver_disable(ucd_t drv, void* args)
|
||||
{
|
||||
RT_ASSERT(drv != RT_NULL);
|
||||
|
||||
if(drv->disable != RT_NULL)
|
||||
drv->disable(args);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This function finds a usb class driver by specified class code and subclass code.
|
||||
*
|
||||
* @param class_code the usb class driver's class code.
|
||||
* @param subclass_code the usb class driver's sub class code.
|
||||
*
|
||||
* @return the registered usb class driver on successful, or RT_NULL on failure.
|
||||
*/
|
||||
ucd_t rt_usbh_class_driver_find(int class_code, int subclass_code, int vendor_id)
|
||||
{
|
||||
struct rt_list_node *node;
|
||||
|
||||
/* enter critical */
|
||||
if (rt_thread_self() != RT_NULL)
|
||||
rt_enter_critical();
|
||||
|
||||
/* try to find driver object */
|
||||
for (node = _driver_list.next; node != &_driver_list; node = node->next)
|
||||
{
|
||||
ucd_t drv =
|
||||
(ucd_t)rt_list_entry(node, struct uclass_driver, list);
|
||||
if (drv->class_code == class_code)
|
||||
{
|
||||
if (drv->class_code == USB_CLASS_VEND_SPECIFIC)
|
||||
{
|
||||
/* need check vendor id */
|
||||
if (drv->vendor_id != vendor_id)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
/* leave critical */
|
||||
if (rt_thread_self() != RT_NULL)
|
||||
rt_exit_critical();
|
||||
|
||||
return drv;
|
||||
}
|
||||
}
|
||||
|
||||
/* leave critical */
|
||||
if (rt_thread_self() != RT_NULL)
|
||||
rt_exit_critical();
|
||||
|
||||
/* not found */
|
||||
return RT_NULL;
|
||||
}
|
||||
952
sdk/lib/bus/rttusb/usbhost/core/hub.c
Normal file
952
sdk/lib/bus/rttusb/usbhost/core/hub.c
Normal file
@@ -0,0 +1,952 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-12-12 Yi Qiu first version
|
||||
* 2021-02-23 Leslie Lee provide possibility for multi usb host
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
#include "dev/usb/hgusb20_v1_dev_api.h"
|
||||
|
||||
#define USB_THREAD_STACK_SIZE 2048
|
||||
|
||||
//#define DBG_TAG "usb.host.hub"
|
||||
//#define DBG_LVL DBG_INFO
|
||||
//#include <rtdbg.h>
|
||||
|
||||
|
||||
// static struct rt_messagequeue *usb_mq;
|
||||
static struct uclass_driver hub_driver;
|
||||
static rt_sem_t hub_sem = RT_NULL;
|
||||
static rt_thread_t usbh_hub_thread = RT_NULL;
|
||||
// static struct uhub root_hub;
|
||||
|
||||
static rt_err_t root_hub_ctrl(struct uhcd *hcd, rt_uint16_t port, rt_uint8_t cmd, void *args)
|
||||
{
|
||||
switch(cmd)
|
||||
{
|
||||
case RH_GET_PORT_STATUS:
|
||||
(*(rt_uint32_t *)args) = hcd->roothub->port_status[port-1];
|
||||
break;
|
||||
case RH_SET_PORT_STATUS:
|
||||
hcd->roothub->port_status[port-1] = (*(rt_uint32_t *)args);
|
||||
break;
|
||||
case RH_CLEAR_PORT_FEATURE:
|
||||
switch(((rt_uint32_t)args))
|
||||
{
|
||||
case PORT_FEAT_C_CONNECTION:
|
||||
hcd->roothub->port_status[port-1] &= ~PORT_CCSC;
|
||||
break;
|
||||
case PORT_FEAT_C_ENABLE:
|
||||
hcd->roothub->port_status[port-1] &= ~PORT_PESC;
|
||||
break;
|
||||
case PORT_FEAT_C_SUSPEND:
|
||||
hcd->roothub->port_status[port-1] &= ~PORT_PSSC;
|
||||
break;
|
||||
case PORT_FEAT_C_OVER_CURRENT:
|
||||
hcd->roothub->port_status[port-1] &= ~PORT_POCIC;
|
||||
break;
|
||||
case PORT_FEAT_C_RESET:
|
||||
hcd->roothub->port_status[port-1] &= ~PORT_PRSC;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case RH_SET_PORT_FEATURE:
|
||||
switch((rt_uint32_t)args)
|
||||
{
|
||||
case PORT_FEAT_CONNECTION:
|
||||
hcd->roothub->port_status[port-1] |= PORT_CCSC;
|
||||
break;
|
||||
case PORT_FEAT_ENABLE:
|
||||
hcd->roothub->port_status[port-1] |= PORT_PESC;
|
||||
break;
|
||||
case PORT_FEAT_SUSPEND:
|
||||
hcd->roothub->port_status[port-1] |= PORT_PSSC;
|
||||
break;
|
||||
case PORT_FEAT_OVER_CURRENT:
|
||||
hcd->roothub->port_status[port-1] |= PORT_POCIC;
|
||||
break;
|
||||
case PORT_FEAT_RESET:
|
||||
hcd->ops->reset_port(port);
|
||||
break;
|
||||
case PORT_FEAT_POWER:
|
||||
break;
|
||||
case PORT_FEAT_LOWSPEED:
|
||||
break;
|
||||
case PORT_FEAT_HIGHSPEED:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return -RT_ERROR;
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
void rt_usbh_root_hub_connect_handler(struct uhcd *hcd, rt_uint8_t port, rt_bool_t isHS)
|
||||
{
|
||||
struct uhost_msg msg;
|
||||
|
||||
if(hcd == NULL || hcd->roothub == NULL)
|
||||
{
|
||||
rt_kprintf("[%s]:hcd or hcd->roothub is NULL!!!\r\n",__FUNCTION__);
|
||||
return;
|
||||
}
|
||||
|
||||
msg.type = USB_MSG_CONNECT_IRQ;
|
||||
msg.content.hub = hcd->roothub;
|
||||
|
||||
rt_usbh_event_signal(hcd, &msg);
|
||||
}
|
||||
|
||||
void rt_usbh_root_hub_disconnect_handler(struct uhcd *hcd, rt_uint8_t port)
|
||||
{
|
||||
struct uhost_msg msg;
|
||||
|
||||
if(hcd == NULL || hcd->roothub == NULL)
|
||||
{
|
||||
rt_kprintf("[%s]:hcd or hcd->roothub is NULL!!!\r\n",__FUNCTION__);
|
||||
return;
|
||||
}
|
||||
|
||||
msg.type = USB_MSG_DISCONNECT_IRQ;
|
||||
msg.content.hub = hcd->roothub;
|
||||
|
||||
rt_usbh_event_signal(hcd, &msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_GET_DESCRIPTOR bRequest for the device instance
|
||||
* to get usb hub descriptor.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @buffer the data buffer to save usb hub descriptor.
|
||||
* @param nbytes the size of buffer
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer, rt_size_t nbytes)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
LOG_D("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_DEVICE;
|
||||
setup.bRequest = USB_REQ_GET_DESCRIPTOR;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = nbytes;
|
||||
setup.wValue = USB_DESC_TYPE_HUB << 8;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, nbytes, timeout) == nbytes)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_GET_STATUS bRequest for the device instance
|
||||
* to get usb hub status.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @buffer the data buffer to save usb hub status.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint32_t* buffer)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
LOG_D("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_DEVICE;
|
||||
setup.bRequest = USB_REQ_GET_STATUS;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = 4;
|
||||
setup.wValue = 0;
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, 4, timeout) == 4)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_GET_STATUS bRequest for the device instance
|
||||
* to get hub port status.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @port the hub port to get status.
|
||||
* @buffer the data buffer to save usb hub status.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_hub_get_port_status(uhub_t hub, rt_uint16_t port, rt_uint32_t* buffer)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(hub != RT_NULL);
|
||||
|
||||
//LOG_D("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
/* get roothub port status */
|
||||
if(hub->is_roothub)
|
||||
{
|
||||
root_hub_ctrl(hub->hcd, port, RH_GET_PORT_STATUS,
|
||||
(void*)buffer);
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_OTHER;
|
||||
setup.bRequest = USB_REQ_GET_STATUS;
|
||||
setup.wIndex = port;
|
||||
setup.wLength = 4;
|
||||
setup.wValue = 0;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(hub->hcd, hub->self->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(hub->hcd, hub->self->pipe_ep0_in, buffer, 4, timeout) == 4)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(hub->hcd, hub->self->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_CLEAR_FEATURE bRequest for the device instance
|
||||
* to clear feature of the hub port.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @port the hub port.
|
||||
* @feature feature to be cleared.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_hub_clear_port_feature(uhub_t hub, rt_uint16_t port, rt_uint16_t feature)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(hub != RT_NULL);
|
||||
|
||||
LOG_D("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
/* clear roothub feature */
|
||||
if(hub->is_roothub)
|
||||
{
|
||||
root_hub_ctrl(hub->hcd, port, RH_CLEAR_PORT_FEATURE,
|
||||
(void*)(rt_uint32_t)feature);
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
|
||||
USB_REQ_TYPE_OTHER;
|
||||
setup.bRequest = USB_REQ_CLEAR_FEATURE;
|
||||
setup.wIndex = port;
|
||||
setup.wLength = 0;
|
||||
setup.wValue = feature;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(hub->hcd, hub->self->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(hub->hcd, hub->self->pipe_ep0_in, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_SET_FEATURE bRequest for the device instance
|
||||
* to set feature of the hub port.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @port the hub port.
|
||||
* @feature feature to be set.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_hub_set_port_feature(uhub_t hub, rt_uint16_t port,
|
||||
rt_uint16_t feature)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(hub != RT_NULL);
|
||||
|
||||
LOG_D("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
/* clear roothub feature */
|
||||
if(hub->is_roothub)
|
||||
{
|
||||
root_hub_ctrl(hub->hcd, port, RH_SET_PORT_FEATURE,
|
||||
(void*)(rt_uint32_t)feature);
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
|
||||
USB_REQ_TYPE_OTHER;
|
||||
setup.bRequest = USB_REQ_SET_FEATURE;
|
||||
setup.wIndex = port;
|
||||
setup.wLength = 0;
|
||||
setup.wValue = feature;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(hub->hcd, hub->self->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(hub->hcd, hub->self->pipe_ep0_in, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will rest hub port, it is invoked when sub device attached to the hub port.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
* @param port the hub port.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_hub_reset_port(uhub_t hub, rt_uint16_t port)
|
||||
{
|
||||
rt_err_t ret;
|
||||
rt_uint32_t pstatus;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(hub != RT_NULL);
|
||||
|
||||
LOG_D("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
// rt_thread_delay(50);
|
||||
|
||||
/* reset hub port */
|
||||
ret = rt_usbh_hub_set_port_feature(hub, port, PORT_FEAT_RESET);
|
||||
if(ret != RT_EOK) return ret;
|
||||
|
||||
while(1)
|
||||
{
|
||||
ret = rt_usbh_hub_get_port_status(hub, port, &pstatus);
|
||||
if(!(pstatus & PORT_PRS)) break;
|
||||
}
|
||||
|
||||
/* clear port reset feature */
|
||||
ret = rt_usbh_hub_clear_port_feature(hub, port, PORT_FEAT_C_RESET);
|
||||
if(ret != RT_EOK) return ret;
|
||||
|
||||
// rt_thread_delay(50);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do debouce, it is invoked when sub device attached to the hub port.
|
||||
*
|
||||
* @param device the usb instance.
|
||||
* @param port the hub port.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_hub_port_debounce(uhub_t hub, rt_uint16_t port)
|
||||
{
|
||||
rt_err_t ret;
|
||||
int i = 0, times = 20;
|
||||
rt_uint32_t pstatus;
|
||||
rt_bool_t connect = RT_TRUE;
|
||||
int delayticks = USB_DEBOUNCE_TIME;
|
||||
if (delayticks < 1)
|
||||
delayticks = 1;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(hub != RT_NULL);
|
||||
|
||||
LOG_D("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
for(i=0; i<times; i++)
|
||||
{
|
||||
ret = rt_usbh_hub_get_port_status(hub, port, &pstatus);
|
||||
if(ret != RT_EOK) return ret;
|
||||
|
||||
if(!(pstatus & PORT_CCS))
|
||||
{
|
||||
connect = RT_FALSE;
|
||||
break;
|
||||
}
|
||||
|
||||
if((pstatus & PORT_CCS))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
rt_thread_delay(delayticks);
|
||||
}
|
||||
LOG_D("%s %d pstatus:%X\n",__FUNCTION__,__LINE__,pstatus);
|
||||
if(connect) return RT_EOK;
|
||||
else return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will poll all the hub ports to detect port status, especially connect and
|
||||
* disconnect events.
|
||||
*
|
||||
* @param intf the interface instance.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
static rt_err_t rt_usbh_hub_port_change(uhub_t hub)
|
||||
{
|
||||
int i;
|
||||
rt_bool_t reconnect;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(hub != RT_NULL);
|
||||
|
||||
LOG_D("%s %d\n",__FUNCTION__,__LINE__);
|
||||
|
||||
if(!hub->is_roothub){
|
||||
rt_usbh_hub_alloc_ep0_pipe(hub);
|
||||
}
|
||||
|
||||
/* get usb device instance */
|
||||
for (i = 0; i < hub->num_ports; i++)
|
||||
{
|
||||
rt_err_t ret;
|
||||
struct uinstance* device;
|
||||
rt_uint32_t pstatus = 0;
|
||||
|
||||
reconnect = RT_FALSE;
|
||||
|
||||
/* get hub port status */
|
||||
ret = rt_usbh_hub_get_port_status(hub, i + 1, &pstatus);
|
||||
if(ret != RT_EOK) continue;
|
||||
|
||||
LOG_D("port %d status 0x%x", i + 1, pstatus);
|
||||
|
||||
/* check port status change */
|
||||
if (pstatus & PORT_CCSC)
|
||||
{
|
||||
/* clear port status change feature */
|
||||
rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_CONNECTION);
|
||||
rt_usbh_hub_get_port_status(hub, i + 1, &pstatus);
|
||||
reconnect = RT_TRUE;
|
||||
}
|
||||
|
||||
if(pstatus & PORT_PESC)
|
||||
{
|
||||
rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_ENABLE);
|
||||
reconnect = RT_TRUE;
|
||||
}
|
||||
|
||||
if(reconnect)
|
||||
{
|
||||
if(hub->child[i] != RT_NULL && hub->child[i]->status != DEV_STATUS_IDLE)
|
||||
{
|
||||
rt_usbh_detach_instance(hub->child[i]);
|
||||
|
||||
/* Child device have been detach. Set hub->child[i] to NULL. */
|
||||
hub->child[i] = RT_NULL;
|
||||
}
|
||||
|
||||
ret = rt_usbh_hub_port_debounce(hub, i + 1);
|
||||
if(ret != RT_EOK) continue;
|
||||
|
||||
/* allocate an usb instance for new connected device */
|
||||
device = rt_usbh_alloc_instance(hub->hcd);
|
||||
if(device == RT_NULL) break;
|
||||
|
||||
/* reset usb roothub port */
|
||||
rt_usbh_hub_reset_port(hub, i + 1);
|
||||
|
||||
ret = rt_usbh_hub_get_port_status(hub, i + 1, &pstatus);
|
||||
if(ret != RT_EOK) continue;
|
||||
|
||||
ret = rt_usbh_hub_get_port_status(hub, i + 1, &pstatus);
|
||||
if(ret != RT_EOK) continue;
|
||||
|
||||
os_printf("pstatus:%x\n",pstatus);
|
||||
/* set usb device speed */
|
||||
if (pstatus & PORT_HSDA) {
|
||||
device->speed = RTTUSB_DEV_HighSpeed;
|
||||
} else if (pstatus & PORT_LSDA) {
|
||||
device->speed = RTTUSB_DEV_LowSpeed;
|
||||
} else {
|
||||
device->speed = RTTUSB_DEV_FullSpeed;
|
||||
}
|
||||
device->parent_hub = hub;
|
||||
device->hcd = hub->hcd;
|
||||
device->port = i + 1;
|
||||
hub->child[i] = device;
|
||||
|
||||
os_printf("hub device speed:%x device port:%x\n",device->speed,device->port);
|
||||
|
||||
if (!hub->is_roothub)
|
||||
{
|
||||
struct uhub_param hub_param;
|
||||
hub_param.hub_addr = 1;
|
||||
hub_param.hub_mtt_en = 0;
|
||||
hub_param.dev_addr = 0;
|
||||
hub_param.dev_port = device->port;
|
||||
hub_param.dev_speed = device->speed;
|
||||
rt_usb_hcd_hub_port_set(hub->hcd, &hub_param);
|
||||
}
|
||||
|
||||
/* attatch the usb instance to the hcd */
|
||||
rt_usbh_attatch_instance(device);
|
||||
}
|
||||
}
|
||||
os_printf("%s %d return!\n",__FUNCTION__,__LINE__);
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//hub下行端点暂不支持热插拔
|
||||
void usbh_hub_thread_entry(void *arg)
|
||||
{
|
||||
upipe_t pipe;
|
||||
uhub_t hub;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
RT_ASSERT(arg != RT_NULL);
|
||||
|
||||
pipe = (upipe_t)arg;
|
||||
hub = (uhub_t)pipe->user_data;
|
||||
|
||||
while(1)
|
||||
{
|
||||
rt_sem_take(hub_sem, RT_WAITING_FOREVER);
|
||||
|
||||
LOG_D("hub int xfer...");
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(pipe->inst->hcd != RT_NULL);
|
||||
|
||||
rt_usb_hcd_pipe_xfer(hub->self->hcd, pipe, hub->buffer, pipe->ep.wMaxPacketSize, timeout);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is the callback function of hub's int endpoint, it is invoked when data comes.
|
||||
*
|
||||
* @param context the context of the callback function.
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
static void rt_usbh_hub_irq(void* context)
|
||||
{
|
||||
upipe_t pipe;
|
||||
uhub_t hub;
|
||||
struct uhost_msg msg;
|
||||
|
||||
RT_ASSERT(context != RT_NULL);
|
||||
|
||||
pipe = (upipe_t)context;
|
||||
hub = (uhub_t)pipe->user_data;
|
||||
|
||||
if(pipe->status != UPIPE_STATUS_OK)
|
||||
{
|
||||
LOG_D("hub irq error");
|
||||
rt_sem_release(hub_sem);
|
||||
return;
|
||||
}
|
||||
os_printf("%s %d\n",__FUNCTION__,__LINE__);
|
||||
msg.type = USB_MSG_CONNECT_CHANGE;
|
||||
msg.content.hub = hub;
|
||||
rt_usbh_event_signal(pipe->inst->hcd, &msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will run usb hub class driver when usb hub is detected and identified
|
||||
* as a hub class device, it will continue to do the enumulate process.
|
||||
*
|
||||
* @param arg the argument.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
|
||||
static rt_err_t rt_usbh_hub_enable(void *arg)
|
||||
{
|
||||
int i = 0;
|
||||
rt_err_t ret = RT_EOK;
|
||||
uep_desc_t ep_desc = RT_NULL;
|
||||
uhub_t hub;
|
||||
upipe_t pipe;
|
||||
struct uinstance* device;
|
||||
struct uhintf* intf = (struct uhintf*)arg;
|
||||
upipe_t pipe_in = RT_NULL;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
/* paremeter check */
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
|
||||
/* get usb device instance */
|
||||
device = intf->device;
|
||||
LOG_D("=======%s %d=======\n",__FUNCTION__,__LINE__);
|
||||
/* create a hub instance */
|
||||
hub = rt_malloc(sizeof(struct uhub));
|
||||
RT_ASSERT(hub != RT_NULL);
|
||||
rt_memset(hub, 0, sizeof(struct uhub));
|
||||
|
||||
/* make interface instance's user data point to hub instance */
|
||||
intf->user_data = (void*)hub;
|
||||
|
||||
/* get hub descriptor head */
|
||||
ret = rt_usbh_hub_get_descriptor(device, (rt_uint8_t*)&hub->hub_desc, 8);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("get hub descriptor failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* get full hub descriptor */
|
||||
ret = rt_usbh_hub_get_descriptor(device, (rt_uint8_t*)&hub->hub_desc,
|
||||
hub->hub_desc.length);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("get hub descriptor again failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* get hub ports number */
|
||||
/* If hub device supported ports over USB_HUB_PORT_NUM(Ex: 8 port hub). Set hub->num_ports to USB_HUB_PORT_NUM */
|
||||
if(hub->hub_desc.num_ports > USB_HUB_PORT_NUM)
|
||||
hub->num_ports = USB_HUB_PORT_NUM;
|
||||
else
|
||||
hub->num_ports = hub->hub_desc.num_ports;
|
||||
|
||||
hub->hcd = device->hcd;
|
||||
hub->self = device;
|
||||
|
||||
/* reset all hub ports */
|
||||
for (i = 0; i < hub->num_ports; i++)
|
||||
{
|
||||
rt_usbh_hub_set_port_feature(hub, i + 1, PORT_FEAT_POWER);
|
||||
rt_thread_delay(hub->hub_desc.pwron_to_good
|
||||
* 2 * RT_TICK_PER_SECOND / 1000 );
|
||||
}
|
||||
|
||||
if(intf->intf_desc->bNumEndpoints != 1)
|
||||
return -RT_ERROR;
|
||||
|
||||
/* get endpoint descriptor from interface descriptor */
|
||||
rt_usbh_get_endpoint_descriptor(intf->intf_desc, 0, &ep_desc);
|
||||
if(ep_desc == RT_NULL)
|
||||
{
|
||||
rt_kprintf("rt_usb_get_endpoint_descriptor error\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* the endpoint type of hub class should be interrupt */
|
||||
if( USB_EP_ATTR(ep_desc->bmAttributes) == USB_EP_ATTR_INT)
|
||||
{
|
||||
/* the endpoint direction of hub class should be in */
|
||||
if(ep_desc->bEndpointAddress & USB_DIR_IN)
|
||||
{
|
||||
analysis_usb_ep_desc(ep_desc);
|
||||
|
||||
if (rt_usb_hcd_alloc_pipe(device->hcd, &pipe, device, ep_desc) != RT_EOK) {
|
||||
rt_kprintf("alloc pipe failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_usb_instance_add_pipe(device, pipe);
|
||||
|
||||
/* allocate a pipe according to the endpoint type */
|
||||
pipe_in = rt_usb_instance_find_pipe(device,ep_desc->bEndpointAddress);
|
||||
if(pipe_in == RT_NULL)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
rt_usb_pipe_add_callback(pipe_in,rt_usbh_hub_irq);
|
||||
}
|
||||
else return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(device->hcd != RT_NULL);
|
||||
pipe_in->user_data = hub;
|
||||
|
||||
|
||||
hub_sem = rt_sem_create("hub_sem", 0, RT_IPC_FLAG_FIFO);
|
||||
if (hub_sem == RT_NULL) {
|
||||
os_printf("hub sem create err!!!\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
usbh_hub_thread = rt_thread_create("usbh_hub_thread",
|
||||
usbh_hub_thread_entry,
|
||||
pipe_in,
|
||||
1024,
|
||||
OS_TASK_PRIORITY_BELOW_NORMAL,
|
||||
20);
|
||||
|
||||
if(usbh_hub_thread != RT_NULL)
|
||||
rt_thread_startup(usbh_hub_thread);
|
||||
|
||||
rt_usb_hcd_pipe_xfer(hub->hcd, pipe_in, hub->buffer,
|
||||
pipe_in->ep.wMaxPacketSize, timeout);
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will be invoked when usb hub plug out is detected and it would clean
|
||||
* and release all hub class related resources.
|
||||
*
|
||||
* @param arg the argument.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
static rt_err_t rt_usbh_hub_disable(void* arg)
|
||||
{
|
||||
int i;
|
||||
uhub_t hub;
|
||||
struct uhintf* intf = (struct uhintf*)arg;
|
||||
|
||||
/* paremeter check */
|
||||
RT_ASSERT(intf != RT_NULL);
|
||||
|
||||
LOG_D("rt_usbh_hub_stop");
|
||||
hub = (uhub_t)intf->user_data;
|
||||
|
||||
for(i=0; i<hub->num_ports; i++)
|
||||
{
|
||||
if(hub->child[i] != RT_NULL)
|
||||
rt_usbh_detach_instance(hub->child[i]);
|
||||
}
|
||||
|
||||
rt_thread_delete(usbh_hub_thread);
|
||||
usbh_hub_thread = RT_NULL;
|
||||
|
||||
rt_sem_delete(hub_sem);
|
||||
hub_sem = RT_NULL;
|
||||
|
||||
if(hub != RT_NULL) rt_free(hub);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will register hub class driver to the usb class driver manager.
|
||||
* and it should be invoked in the usb system initialization.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
ucd_t rt_usbh_class_driver_hub(void)
|
||||
{
|
||||
hub_driver.class_code = USB_CLASS_HUB;
|
||||
|
||||
hub_driver.enable = rt_usbh_hub_enable;
|
||||
hub_driver.disable = rt_usbh_hub_disable;
|
||||
|
||||
return &hub_driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is the main entry of usb hub thread, it is in charge of
|
||||
* processing all messages received from the usb message buffer.
|
||||
*
|
||||
* @param parameter the parameter of the usb host thread.
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
static void rt_usbh_hub_thread_entry(void* parameter)
|
||||
{
|
||||
uhcd_t hcd = (uhcd_t)parameter;
|
||||
while(1)
|
||||
{
|
||||
struct uhost_msg msg;
|
||||
|
||||
/* receive message */
|
||||
if (rt_mq_recv(hcd->usb_mq, &msg, sizeof(struct uhost_msg), RT_WAITING_FOREVER) < 0)
|
||||
continue;
|
||||
|
||||
//USB CONNECT 同步状态设置
|
||||
if (msg.type == USB_MSG_CONNECT_IRQ) {
|
||||
uhub_t hub = msg.content.hub;
|
||||
rt_bool_t isHS = RT_TRUE;
|
||||
rt_uint8_t port = 1;
|
||||
hub->port_status[port - 1] |= PORT_CCS | PORT_CCSC;
|
||||
if(isHS)
|
||||
{
|
||||
hub->port_status[port - 1] &= ~PORT_LSDA;
|
||||
}
|
||||
else
|
||||
{
|
||||
hub->port_status[port - 1] |= PORT_LSDA;
|
||||
}
|
||||
}
|
||||
|
||||
//USB DISCONNECT 同步状态设置
|
||||
if (msg.type == USB_MSG_DISCONNECT_IRQ) {
|
||||
uhub_t hub = msg.content.hub;
|
||||
rt_uint8_t port = 1;
|
||||
hub->port_status[port - 1] |= PORT_CCSC;
|
||||
hub->port_status[port - 1] &= ~PORT_CCS;
|
||||
}
|
||||
|
||||
switch (msg.type)
|
||||
{
|
||||
case USB_MSG_CONNECT_IRQ:
|
||||
case USB_MSG_DISCONNECT_IRQ:
|
||||
case USB_MSG_CONNECT_CHANGE:
|
||||
rt_usbh_hub_port_change(msg.content.hub);
|
||||
if(!msg.content.hub->is_roothub)
|
||||
{
|
||||
rt_sem_release(hub_sem);
|
||||
}
|
||||
break;
|
||||
case USB_MSG_CALLBACK:
|
||||
/* invoke callback */
|
||||
msg.content.cb.function(msg.content.cb.context);
|
||||
break;
|
||||
case USB_MSG_DELETE_THREAD:
|
||||
goto __exit_end;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
__exit_end:
|
||||
hcd->thread_status = 0;
|
||||
/* 挂起线程等待删除 */
|
||||
rt_thread_suspend(hcd->thread);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will post an message to the usb message queue,
|
||||
*
|
||||
* @param msg the message to be posted
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_event_signal(uhcd_t hcd, struct uhost_msg* msg)
|
||||
{
|
||||
RT_ASSERT(msg != RT_NULL);
|
||||
|
||||
/* send message to usb message queue */
|
||||
rt_mq_send(hcd->usb_mq, (void*)msg, sizeof(struct uhost_msg));
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will initialize usb hub thread.
|
||||
*
|
||||
* @return none.
|
||||
*
|
||||
*/
|
||||
void rt_usbh_hub_init(uhcd_t hcd, const char* dev)
|
||||
{
|
||||
rt_thread_t thread;
|
||||
/* create root hub for hcd */
|
||||
hcd->roothub = rt_malloc(sizeof(struct uhub));
|
||||
if(hcd->roothub == RT_NULL)
|
||||
{
|
||||
LOG_E("hcd->roothub: allocate buffer failed.");
|
||||
return;
|
||||
}
|
||||
rt_memset(hcd->roothub, 0, sizeof(struct uhub));
|
||||
hcd->roothub->is_roothub = RT_TRUE;
|
||||
hcd->roothub->hcd = hcd;
|
||||
hcd->roothub->num_ports = hcd->num_ports;
|
||||
/* create usb message queue */
|
||||
|
||||
if(hcd->usb_mq == RT_NULL)
|
||||
{
|
||||
hcd->usb_mq = rt_mq_create(NULL, 32, 16, RT_IPC_FLAG_FIFO);
|
||||
}
|
||||
|
||||
if(hcd->thread == RT_NULL)
|
||||
{
|
||||
/* create usb hub thread */
|
||||
thread = rt_thread_create((const char *)dev, rt_usbh_hub_thread_entry, hcd,
|
||||
USB_THREAD_STACK_SIZE, OS_TASK_PRIORITY_ABOVE_NORMAL, 20); //OS_TASK_PRIORITY_BELOW_NORMAL
|
||||
if(thread != RT_NULL)
|
||||
{
|
||||
/* startup usb host thread */
|
||||
rt_thread_startup(thread);
|
||||
}else{
|
||||
os_printf("hub thread start fail\n");
|
||||
}
|
||||
|
||||
hcd->thread = thread;
|
||||
hcd->thread_status = 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void rt_usbh_hub_deinit(uhcd_t hcd)
|
||||
{
|
||||
struct uhost_msg msg;
|
||||
rt_uint32_t timeout = 100;
|
||||
|
||||
if(hcd->thread != RT_NULL)
|
||||
{
|
||||
msg.type = USB_MSG_DELETE_THREAD;
|
||||
msg.content.hub = hcd->roothub;
|
||||
rt_usbh_event_signal(hcd, &msg);
|
||||
|
||||
do {
|
||||
os_sleep_ms(10);
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
os_printf("usbh wait hcd thread timeout!!!\n");
|
||||
break;
|
||||
}
|
||||
} while (hcd->thread_status);
|
||||
|
||||
|
||||
rt_thread_delete(hcd->thread);
|
||||
|
||||
|
||||
if(hcd->usb_mq)
|
||||
{
|
||||
rt_mq_delete(hcd->usb_mq);
|
||||
hcd->usb_mq = NULL;
|
||||
}
|
||||
|
||||
if(hcd->roothub)
|
||||
{
|
||||
rt_free(hcd->roothub);
|
||||
hcd->roothub = NULL;
|
||||
}
|
||||
|
||||
hcd->thread = NULL;
|
||||
|
||||
}
|
||||
}
|
||||
122
sdk/lib/bus/rttusb/usbhost/core/usbhost.c
Normal file
122
sdk/lib/bus/rttusb/usbhost/core/usbhost.c
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-12-12 Yi Qiu first version
|
||||
* 2021-02-23 Leslie Lee provide possibility for multi usb host
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
|
||||
#define USB_HOST_CONTROLLER_NAME "usbh"
|
||||
|
||||
#if defined(RT_USBH_HID_KEYBOARD) || defined(RT_USBH_HID_MOUSE)
|
||||
#include <hid.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function will initialize the usb host stack, all the usb class driver and
|
||||
* host controller driver are also be initialized here.
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
rt_err_t rt_usb_host_init(uint32 devid, const char *dev)
|
||||
{
|
||||
ucd_t drv;
|
||||
uhcd_t uhc;
|
||||
|
||||
uhc = (uhcd_t)dev_get(devid);
|
||||
if(uhc == RT_NULL)
|
||||
{
|
||||
rt_kprintf("can't find usb host controller\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* initialize usb hub */
|
||||
uhc->devid = devid;
|
||||
rt_usbh_hub_init((uhcd_t)uhc, dev);
|
||||
|
||||
/* initialize class driver */
|
||||
rt_usbh_class_driver_init();
|
||||
|
||||
#ifdef RT_USBH_MSTORAGE
|
||||
/* register mass storage class driver */
|
||||
drv = rt_usbh_class_driver_storage();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
#endif
|
||||
#ifdef RT_USBH_HID
|
||||
extern ucd_t rt_usbh_class_driver_hid(void);
|
||||
/* register mass storage class driver */
|
||||
drv = rt_usbh_class_driver_hid();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
#ifdef RT_USBH_HID_MOUSE
|
||||
{
|
||||
extern uprotocal_t rt_usbh_hid_protocal_mouse(void);
|
||||
rt_usbh_hid_protocal_register(rt_usbh_hid_protocal_mouse());
|
||||
}
|
||||
#endif
|
||||
#ifdef RT_USBH_HID_KEYBOARD
|
||||
{
|
||||
extern uprotocal_t rt_usbh_hid_protocal_kbd(void);
|
||||
rt_usbh_hid_protocal_register(rt_usbh_hid_protocal_kbd());
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
#ifdef RT_USBH_CDC
|
||||
extern ucd_t rt_usbh_class_driver_cdc(void);
|
||||
drv = rt_usbh_class_driver_cdc();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
#endif
|
||||
#ifdef RT_USBH_UVC
|
||||
extern ucd_t rt_usbh_class_driver_video(void);
|
||||
drv = rt_usbh_class_driver_video();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
#endif
|
||||
#ifdef RT_USBH_UAC
|
||||
extern ucd_t rt_usbh_class_driver_audio(void);
|
||||
drv = rt_usbh_class_driver_audio();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
#endif
|
||||
#ifdef RT_USBH_WIRELESS
|
||||
extern ucd_t rt_usbh_class_driver_wireless(void);
|
||||
drv = rt_usbh_class_driver_wireless();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
#endif
|
||||
#ifdef RT_USBH_VENDOR_QUECTEL
|
||||
extern ucd_t rt_usbh_class_driver_quectel(void);
|
||||
drv = rt_usbh_class_driver_quectel();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
#endif
|
||||
#ifdef RT_USBH_VENDOR_CHINAMOBILE
|
||||
extern ucd_t rt_usbh_class_driver_chinamobile(void);
|
||||
drv = rt_usbh_class_driver_chinamobile();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
#endif
|
||||
|
||||
/* register hub class driver */
|
||||
drv = rt_usbh_class_driver_hub();
|
||||
rt_usbh_class_driver_register(drv);
|
||||
|
||||
/* initialize usb host controller */
|
||||
rt_device_init(uhc);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_usb_host_deinit(uint32 devid)
|
||||
{
|
||||
uhcd_t uhc;
|
||||
|
||||
uhc = (uhcd_t)dev_get(devid);
|
||||
if(uhc == RT_NULL)
|
||||
{
|
||||
rt_kprintf("can't find usb host controller\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_usbh_hub_deinit((uhcd_t)uhc);
|
||||
return RT_EOK;
|
||||
}
|
||||
878
sdk/lib/bus/rttusb/usbhost/core/usbhost_core.c
Normal file
878
sdk/lib/bus/rttusb/usbhost/core/usbhost_core.c
Normal file
@@ -0,0 +1,878 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-12-12 Yi Qiu first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <include/rttusb_host.h>
|
||||
|
||||
//#define DBG_TAG "usbhost.core"
|
||||
//#define DBG_LVL DBG_INFO
|
||||
//#include <rtdbg.h>
|
||||
|
||||
static struct uinstance dev[USB_MAX_DEVICE];
|
||||
|
||||
/**
|
||||
* This function will allocate an usb device instance from system.
|
||||
*
|
||||
* @param parent the hub instance to which the new allocated device attached.
|
||||
* @param port the hub port.
|
||||
*
|
||||
* @return the allocate instance on successful, or RT_NULL on failure.
|
||||
*/
|
||||
uinst_t rt_usbh_alloc_instance(uhcd_t uhcd)
|
||||
{
|
||||
int i;
|
||||
|
||||
/* lock scheduler */
|
||||
rt_enter_critical();
|
||||
|
||||
for(i=0; i<USB_MAX_DEVICE; i++)
|
||||
{
|
||||
/* to find an idle instance handle */
|
||||
if(dev[i].status != DEV_STATUS_IDLE) continue;
|
||||
|
||||
/* initialize the usb device instance */
|
||||
rt_memset(&dev[i], 0, sizeof(struct uinstance));
|
||||
|
||||
dev[i].status = DEV_STATUS_BUSY;
|
||||
dev[i].index = i + 1;
|
||||
dev[i].address = 0;
|
||||
dev[i].max_packet_size = 0x8;
|
||||
rt_list_init(&dev[i].pipe);
|
||||
dev[i].hcd = uhcd;
|
||||
/* unlock scheduler */
|
||||
rt_exit_critical();
|
||||
return &dev[i];
|
||||
}
|
||||
|
||||
/* unlock scheduler */
|
||||
rt_exit_critical();
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will attatch an usb device instance to a host controller,
|
||||
* and do device enumunation process.
|
||||
*
|
||||
* @param hcd the host controller driver.
|
||||
* @param device the usb device instance.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
static struct uendpoint_descriptor ep0_out_desc =
|
||||
{
|
||||
/*endpoint descriptor*/
|
||||
USB_DESC_LENGTH_ENDPOINT,
|
||||
USB_DESC_TYPE_ENDPOINT,
|
||||
0x00 | USB_DIR_OUT,
|
||||
USB_EP_ATTR_CONTROL,
|
||||
0x00,
|
||||
0x00,
|
||||
};
|
||||
static struct uendpoint_descriptor ep0_in_desc =
|
||||
{
|
||||
/*endpoint descriptor*/
|
||||
USB_DESC_LENGTH_ENDPOINT,
|
||||
USB_DESC_TYPE_ENDPOINT,
|
||||
0x00 | USB_DIR_IN,
|
||||
USB_EP_ATTR_CONTROL,
|
||||
0x00,
|
||||
0x00,
|
||||
};
|
||||
|
||||
|
||||
void analysis_usb_dev_desc(udev_desc_t desc, uint32 desclen)
|
||||
{
|
||||
udev_desc_t dev_desc = (udev_desc_t)desc;
|
||||
os_printf("=================device desc=================\r\n");
|
||||
printf("bLength:%#x\n",dev_desc->bLength);
|
||||
printf("type:%#x\n",dev_desc->type);
|
||||
printf("bcdUSB:%#x\n",dev_desc->bcdUSB);
|
||||
printf("bDeviceClass:%#x\n",dev_desc->bDeviceClass);
|
||||
printf("bDeviceSubClass:%#x\n",dev_desc->bDeviceSubClass);
|
||||
printf("bDeviceProtocol:%#x\n",dev_desc->bDeviceProtocol);
|
||||
printf("bMaxPacketSize0:%#x\n",dev_desc->bMaxPacketSize0);
|
||||
printf("idVendor:%#x\n",dev_desc->idVendor);
|
||||
printf("idProduct:%#x\n",dev_desc->idProduct);
|
||||
printf("bcdDevice:%#x\n",dev_desc->bcdDevice);
|
||||
printf("iManufacturer:%#x\n",dev_desc->iManufacturer);
|
||||
printf("iProduct:%#x\n",dev_desc->iProduct);
|
||||
printf("iSerialNumber:%#x\n",dev_desc->iSerialNumber);
|
||||
printf("bNumConfigurations:%#x\n",dev_desc->bNumConfigurations);
|
||||
}
|
||||
|
||||
void analysis_usb_cfg_desc(ucfg_desc_t desc, uint32 desclen)
|
||||
{
|
||||
ucfg_desc_t cfg_desc = (ucfg_desc_t)desc;
|
||||
os_printf("=================config desc=================\r\n");
|
||||
printf("bLength:%#x\n",cfg_desc->bLength);
|
||||
printf("bDescriptorType:%#x\n",cfg_desc->type);
|
||||
printf("wTotalLength:%#x\n",cfg_desc->wTotalLength);
|
||||
printf("bNumInterfaces:%#x\n",cfg_desc->bNumInterfaces);
|
||||
printf("bConfigurationValue:%#x\n",cfg_desc->bConfigurationValue);
|
||||
printf("iConfiguration:%#x\n",cfg_desc->iConfiguration);
|
||||
printf("bmAttributes:%#x\n",cfg_desc->bmAttributes);
|
||||
printf("bMaxPower:%#x\n",cfg_desc->MaxPower);
|
||||
}
|
||||
|
||||
void analysis_usb_intf_desc(uintf_desc_t desc, uint32 desclen)
|
||||
{
|
||||
uintf_desc_t intf_desc = (uintf_desc_t)desc;
|
||||
os_printf("=================intf desc=================\r\n");
|
||||
printf("bLength:%#x\n",intf_desc->bLength);
|
||||
printf("bDescriptorType:%#x\n",intf_desc->type);
|
||||
printf("bInterfaceNumber:%#x\n",intf_desc->bInterfaceNumber);
|
||||
printf("bAlternateSetting:%d\n",intf_desc->bAlternateSetting);
|
||||
printf("bNumEndpoints:%d\n",intf_desc->bNumEndpoints);
|
||||
printf("bInterfaceClass:%#x\n",intf_desc->bInterfaceClass);
|
||||
printf("bInterfaceSubClass:%#x\n",intf_desc->bInterfaceSubClass);
|
||||
printf("bInterfaceProtocol:%d\n",intf_desc->bInterfaceProtocol);
|
||||
printf("iInterface:%#x\n",intf_desc->iInterface);
|
||||
}
|
||||
|
||||
void analysis_usb_ep_desc(uep_desc_t ep_desc)
|
||||
{
|
||||
os_printf("=================ep desc=================\r\n");
|
||||
printf("bLength:%#x\n",ep_desc->bLength);
|
||||
printf("type:%#x\n",ep_desc->type);
|
||||
printf("bEndpointAddress:%#x\n",ep_desc->bEndpointAddress);
|
||||
printf("bmAttributes:%#x\n",ep_desc->bmAttributes);
|
||||
printf("wMaxPacketSize:%#x\n",ep_desc->wMaxPacketSize);
|
||||
printf("bInterval:%#x\n",ep_desc->bInterval);
|
||||
}
|
||||
|
||||
void analysis_usb_iad_desc(uiad_desc_t iad_desc)
|
||||
{
|
||||
os_printf("=================iad desc=================\r\n");
|
||||
printf("bLength:%#x\n",iad_desc->bLength);
|
||||
printf("bDescriptorType:%#x\n",iad_desc->bDescriptorType);
|
||||
printf("bFirstInterface:%#x\n",iad_desc->bFirstInterface);
|
||||
printf("bInterfaceCount:%#x\n",iad_desc->bInterfaceCount);
|
||||
printf("bFunctionClass:%#x\n",iad_desc->bFunctionClass);
|
||||
printf("bFunctionSubClass:%#x\n",iad_desc->bFunctionSubClass);
|
||||
printf("bFunctionProtocol:%#x\n",iad_desc->bFunctionProtocol);
|
||||
printf("iFunction:%#x\n",iad_desc->iFunction);
|
||||
}
|
||||
|
||||
rt_err_t rt_usbh_hub_alloc_ep0_pipe(uhub_t hub)
|
||||
{
|
||||
if (!hub->is_roothub) {
|
||||
struct uinstance* device = hub->self;
|
||||
rt_usb_hub_ep0_open_pipe(device->hcd, &ep0_out_desc);
|
||||
rt_usb_hub_ep0_open_pipe(device->hcd, &ep0_in_desc);
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_usbh_attatch_instance(uinst_t device)
|
||||
{
|
||||
int i = 0;
|
||||
rt_err_t ret = RT_EOK;
|
||||
rt_uint8_t cfg_desc_buff[18];
|
||||
udev_desc_t dev_desc;
|
||||
uintf_desc_t intf_desc = RT_NULL;
|
||||
uiad_desc_t iad_desc;
|
||||
ucfg_desc_t cfg_desc;
|
||||
// uep_desc_t ep_desc;
|
||||
// rt_uint8_t ep_index;
|
||||
rt_uint8_t iad_index;
|
||||
// upipe_t pipe;
|
||||
ucd_t drv;
|
||||
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
rt_memset(cfg_desc_buff, 0, sizeof(cfg_desc_buff));
|
||||
cfg_desc = (ucfg_desc_t)cfg_desc_buff;
|
||||
dev_desc = &device->dev_desc;
|
||||
device->cfg_desc = RT_NULL;
|
||||
|
||||
/* alloc address 0 ep0 pipe*/
|
||||
ep0_out_desc.wMaxPacketSize = 8;
|
||||
ep0_in_desc.wMaxPacketSize = 8;
|
||||
rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_out, device, &ep0_out_desc);
|
||||
rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_in, device, &ep0_in_desc);
|
||||
|
||||
LOG_D("start enumnation");
|
||||
|
||||
/* get device descriptor head */
|
||||
ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_DEVICE, (void*)dev_desc, 8);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("get device descriptor head failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
analysis_usb_dev_desc(dev_desc, 8);//第一次获取设备描述符 打印设备描述符信息
|
||||
|
||||
/* reset bus */
|
||||
rt_usbh_hub_reset_port(device->parent_hub, device->port);
|
||||
rt_thread_delay(2);
|
||||
rt_usbh_hub_clear_port_feature(device->parent_hub, device->port, PORT_FEAT_C_CONNECTION);
|
||||
/* set device address */
|
||||
ret = rt_usbh_set_address(device);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("set device address failed\n");
|
||||
return ret;
|
||||
}
|
||||
/* free address 0 ep0 pipe*/
|
||||
|
||||
rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_out);
|
||||
rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_in);
|
||||
|
||||
/* set device max packet size */
|
||||
ep0_out_desc.wMaxPacketSize = device->dev_desc.bMaxPacketSize0;
|
||||
ep0_in_desc.wMaxPacketSize = device->dev_desc.bMaxPacketSize0;
|
||||
|
||||
/* alloc true address ep0 pipe*/
|
||||
rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_out, device, &ep0_out_desc);
|
||||
rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_in, device, &ep0_in_desc);
|
||||
LOG_D("get device descriptor length %d",
|
||||
dev_desc->bLength);
|
||||
|
||||
/* get full device descriptor again */
|
||||
ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_DEVICE, (void*)dev_desc, dev_desc->bLength);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("get full device descriptor failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
analysis_usb_dev_desc(dev_desc, dev_desc->bLength); //第二次获取设备描述符 打印设备描述符信息
|
||||
|
||||
LOG_D("Vendor ID 0x%x", dev_desc->idVendor);
|
||||
LOG_D("Product ID 0x%x", dev_desc->idProduct);
|
||||
|
||||
/* get configuration descriptor head */
|
||||
ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_CONFIGURATION, cfg_desc, 18);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("get configuration descriptor head failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
analysis_usb_cfg_desc(cfg_desc, 18); //第一次获取配置描述符 打印配置描述符信息
|
||||
|
||||
/* alloc memory for configuration descriptor */
|
||||
device->cfg_desc = (ucfg_desc_t)rt_zalloc(cfg_desc->wTotalLength);
|
||||
if(device->cfg_desc == RT_NULL)
|
||||
{
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
rt_memset(device->cfg_desc, 0, cfg_desc->wTotalLength);
|
||||
|
||||
/* get full configuration descriptor */
|
||||
ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_CONFIGURATION,
|
||||
device->cfg_desc, cfg_desc->wTotalLength);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("get full configuration descriptor failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
analysis_usb_cfg_desc(device->cfg_desc, device->cfg_desc->wTotalLength); //第二次获取配置描述符 打印配置描述符信息
|
||||
|
||||
/* set configuration */
|
||||
ret = rt_usbh_set_configure(device, 1);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
for(i=0; i<device->cfg_desc->bNumInterfaces; i++)
|
||||
{
|
||||
/* check IAD */
|
||||
rt_usbh_get_interface_associtaion_descriptor(device->cfg_desc, i, &iad_desc);
|
||||
if (iad_desc != RT_NULL)
|
||||
{
|
||||
analysis_usb_iad_desc(iad_desc);
|
||||
for (iad_index = iad_desc->bFirstInterface;
|
||||
iad_index < iad_desc->bFirstInterface + iad_desc->bInterfaceCount;
|
||||
++iad_index)
|
||||
{
|
||||
rt_usbh_get_interface_descriptor(device->cfg_desc, iad_index, &intf_desc);
|
||||
if (intf_desc == RT_NULL)
|
||||
{
|
||||
rt_kprintf("rt_usb_get_interface_descriptor error\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
analysis_usb_intf_desc(intf_desc, 0);//获取接口描述符,打印接口描述符信息
|
||||
|
||||
LOG_D("interface class 0x%x, subclass 0x%x",
|
||||
intf_desc->bInterfaceClass,
|
||||
intf_desc->bInterfaceSubClass);
|
||||
#if 0 // 因为我们的USB端口不够用,不能每个EP都分配,改到driver里面分配
|
||||
/* alloc pipe*/
|
||||
for (ep_index = 0; ep_index < intf_desc->bNumEndpoints; ++ep_index)
|
||||
{
|
||||
rt_usbh_get_endpoint_descriptor(intf_desc, ep_index, &ep_desc);
|
||||
if(ep_desc != RT_NULL)
|
||||
{
|
||||
if(rt_usb_hcd_alloc_pipe(device->hcd, &pipe, device, ep_desc) != RT_EOK)
|
||||
{
|
||||
rt_kprintf("alloc pipe failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
rt_usb_instance_add_pipe(device,pipe);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("get endpoint desc failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
device->intf[iad_index] = (struct uhintf *)rt_malloc(sizeof(struct uhintf));
|
||||
LOG_D("rt malloc intf[%d]\n",iad_index);
|
||||
if(device->intf[iad_index] == RT_NULL)
|
||||
{
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
device->intf[iad_index]->drv = RT_NULL;
|
||||
device->intf[iad_index]->device = device;
|
||||
device->intf[iad_index]->intf_desc = intf_desc;
|
||||
device->intf[iad_index]->user_data = RT_NULL;
|
||||
|
||||
}
|
||||
|
||||
i += iad_desc->bInterfaceCount - 1; // 前面for循环还会+1补回去
|
||||
// 通过接口关联class查找对应驱动
|
||||
drv = rt_usbh_class_driver_find(iad_desc->bFunctionClass,
|
||||
iad_desc->bFunctionSubClass, dev_desc->idVendor);
|
||||
if (drv != RT_NULL)
|
||||
{
|
||||
// 给接口关联的第一个接口添加驱动,后续就能通过怎么样找到?
|
||||
device->intf[iad_desc->bFirstInterface]->drv = drv;
|
||||
// 复合设备传递参数是指针的指针
|
||||
ret = rt_usbh_class_driver_enable(drv, (void *)&device->intf[iad_desc->bFirstInterface]);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("interface %d run class driver error\n", i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("find usb device driver failed\n");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* get interface descriptor through configuration descriptor */
|
||||
ret = rt_usbh_get_interface_descriptor(device->cfg_desc, i, &intf_desc);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("rt_usb_get_interface_descriptor error\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
analysis_usb_intf_desc(intf_desc, 0);//获取接口描述符,打印接口描述符信息
|
||||
LOG_D("interface class 0x%x, subclass 0x%x",
|
||||
intf_desc->bInterfaceClass,
|
||||
intf_desc->bInterfaceSubClass);
|
||||
#if 0 // 因为我们的USB端口不够用,不能每个EP都分配,改到driver里面分配
|
||||
/* alloc pipe*/
|
||||
for(ep_index = 0; ep_index < intf_desc->bNumEndpoints; ep_index++)
|
||||
{
|
||||
rt_usbh_get_endpoint_descriptor(intf_desc, ep_index, &ep_desc);
|
||||
if(ep_desc != RT_NULL)
|
||||
{
|
||||
if(rt_usb_hcd_alloc_pipe(device->hcd, &pipe, device, ep_desc) != RT_EOK)
|
||||
{
|
||||
rt_kprintf("alloc pipe failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
rt_usb_instance_add_pipe(device,pipe);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("get endpoint desc failed\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
/* find driver by class code found in interface descriptor */
|
||||
drv = rt_usbh_class_driver_find(intf_desc->bInterfaceClass,
|
||||
intf_desc->bInterfaceSubClass, dev_desc->idVendor);
|
||||
|
||||
if(drv != RT_NULL)
|
||||
{
|
||||
/* allocate memory for interface device */
|
||||
device->intf[i] = (struct uhintf*)rt_malloc(sizeof(struct uhintf));
|
||||
if(device->intf[i] == RT_NULL)
|
||||
{
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
device->intf[i]->drv = drv;
|
||||
device->intf[i]->device = device;
|
||||
device->intf[i]->intf_desc = intf_desc;
|
||||
device->intf[i]->user_data = RT_NULL;
|
||||
|
||||
/* open usb class driver */
|
||||
// 非复合设备传递参数是指针
|
||||
ret = rt_usbh_class_driver_enable(drv, (void*)device->intf[i]);
|
||||
if(ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("interface %d run class driver error\n", i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("find usb device driver failed\n");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will detach an usb device instance from its host controller,
|
||||
* and release all resource.
|
||||
*
|
||||
* @param device the usb device instance.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_detach_instance(uinst_t device)
|
||||
{
|
||||
int i = 0;
|
||||
rt_list_t * l;
|
||||
if(device == RT_NULL)
|
||||
{
|
||||
rt_kprintf("no usb instance to detach\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* free configration descriptor */
|
||||
if (device->cfg_desc) {
|
||||
for (i = 0; i < device->cfg_desc->bNumInterfaces; i++)
|
||||
{
|
||||
if (device->intf[i] == RT_NULL) continue;
|
||||
if (device->intf[i]->drv == RT_NULL)
|
||||
{
|
||||
LOG_D("rt free intf[%d]\n",i);
|
||||
rt_free(device->intf[i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
RT_ASSERT(device->intf[i]->device == device);
|
||||
|
||||
LOG_D("free interface instance %d", i);
|
||||
rt_usbh_class_driver_disable(device->intf[i]->drv, (void*)device->intf[i]);
|
||||
rt_free(device->intf[i]);
|
||||
LOG_D("rt free intf[%d]\n",i);
|
||||
}
|
||||
rt_free(device->cfg_desc);
|
||||
}
|
||||
|
||||
rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_out);
|
||||
rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_in);
|
||||
|
||||
while(device->pipe.next!= &device->pipe)
|
||||
{
|
||||
l = device->pipe.next;
|
||||
rt_list_remove(l);
|
||||
rt_usb_hcd_free_pipe(device->hcd,rt_list_entry(l,struct upipe,list));
|
||||
}
|
||||
rt_memset(device, 0, sizeof(struct uinstance));
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_GET_DESCRIPTO' bRequest for the usb device instance,
|
||||
*
|
||||
* @param device the usb device instance.
|
||||
* @param type the type of descriptor bRequest.
|
||||
* @param buffer the data buffer to save requested data
|
||||
* @param nbytes the size of buffer
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_get_descriptor(uinst_t device, rt_uint8_t type, void* buffer,
|
||||
int nbytes)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_STANDARD |
|
||||
USB_REQ_TYPE_DEVICE;
|
||||
setup.bRequest = USB_REQ_GET_DESCRIPTOR;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = nbytes;
|
||||
setup.wValue = type << 8;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, nbytes, timeout) == nbytes)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will do USB_REQ_GET_DESCRIPTO' bRequest for the usb device instance,
|
||||
*
|
||||
* @param device the usb device instance.
|
||||
* @param intf the interface number.
|
||||
* @param buffer the data buffer to save requested data
|
||||
* @param nbytes the size of buffer
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_get_string_descriptor(uinst_t device, int intf, void* buffer,
|
||||
int nbytes)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_STANDARD |
|
||||
USB_REQ_TYPE_DEVICE;
|
||||
setup.bRequest = USB_REQ_GET_DESCRIPTOR;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = nbytes;
|
||||
setup.wValue = (USB_DESC_TYPE_STRING << 8) | intf;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, nbytes, timeout) > 0)
|
||||
{
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will set an address to the usb device.
|
||||
*
|
||||
* @param device the usb device instance.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_set_address(uinst_t device)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
LOG_D("rt_usb_set_address");
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
|
||||
USB_REQ_TYPE_DEVICE;
|
||||
setup.bRequest = USB_REQ_SET_ADDRESS;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = 0;
|
||||
setup.wValue = device->index;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) == 0)
|
||||
{
|
||||
device->address = device->index;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will set a configuration to the usb device.
|
||||
*
|
||||
* @param device the usb device instance.
|
||||
* @param config the configuration number.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_set_configure(uinst_t device, int config)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* check parameter */
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
|
||||
USB_REQ_TYPE_DEVICE;
|
||||
setup.bRequest = USB_REQ_SET_CONFIGURATION;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = 0;
|
||||
setup.wValue = config;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) != 0)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will set an interface to the usb device.
|
||||
*
|
||||
* @param device the usb device instance.
|
||||
* @param intf the interface number.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_set_interface(uinst_t device, int intf)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* check parameter */
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
|
||||
USB_REQ_TYPE_INTERFACE;
|
||||
setup.bRequest = USB_REQ_SET_INTERFACE;
|
||||
setup.wIndex = 0;
|
||||
setup.wLength = 0;
|
||||
setup.wValue = intf;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) != 0)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will clear feature for the endpoint of the usb device.
|
||||
*
|
||||
* @param device the usb device instance.
|
||||
* @param endpoint the endpoint number of the usb device.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_clear_feature(uinst_t device, int endpoint, int feature)
|
||||
{
|
||||
struct urequest setup;
|
||||
int timeout = USB_TIMEOUT_BASIC;
|
||||
|
||||
/* check parameter */
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
|
||||
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
|
||||
USB_REQ_TYPE_ENDPOINT;
|
||||
setup.bRequest = USB_REQ_CLEAR_FEATURE;
|
||||
setup.wIndex = endpoint;
|
||||
setup.wLength = 0;
|
||||
setup.wValue = feature;
|
||||
|
||||
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) != 0)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will get an interface descriptor from the configuration descriptor.
|
||||
*
|
||||
* @param cfg_desc the point of configuration descriptor structure.
|
||||
* @param num the number of interface descriptor.
|
||||
* @intf_desc the point of interface descriptor point.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_get_interface_descriptor(ucfg_desc_t cfg_desc, int num,
|
||||
uintf_desc_t* intf_desc)
|
||||
{
|
||||
rt_uint32_t ptr, depth = 0;
|
||||
udesc_t desc;
|
||||
|
||||
/* check parameter */
|
||||
RT_ASSERT(cfg_desc != RT_NULL);
|
||||
|
||||
ptr = (rt_uint32_t)cfg_desc + cfg_desc->bLength;
|
||||
while(ptr < (rt_uint32_t)cfg_desc + cfg_desc->wTotalLength)
|
||||
{
|
||||
if(depth++ > 0x40)
|
||||
{
|
||||
*intf_desc = RT_NULL;
|
||||
return -RT_EIO;
|
||||
}
|
||||
desc = (udesc_t)ptr;
|
||||
if(desc->type == USB_DESC_TYPE_INTERFACE)
|
||||
{
|
||||
if(((uintf_desc_t)desc)->bInterfaceNumber == num)
|
||||
{
|
||||
*intf_desc = (uintf_desc_t)desc;
|
||||
|
||||
LOG_D("rt_usb_get_interface_descriptor: %d", num);
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
ptr = (rt_uint32_t)desc + desc->bLength;
|
||||
}
|
||||
|
||||
rt_kprintf("rt_usb_get_interface_descriptor %d failed\n", num);
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will get an interface associtaion descriptor from the configuration descriptor.
|
||||
*
|
||||
* @param cfg_desc the point of configuration descriptor structure.
|
||||
* @param num the number of interface descriptor.
|
||||
* @iad_desc the point of interface associtaion descriptor point.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_get_interface_associtaion_descriptor(ucfg_desc_t cfg_desc, int num,
|
||||
uiad_desc_t* iad_desc)
|
||||
{
|
||||
rt_uint32_t ptr, depth = 0;
|
||||
udesc_t desc;
|
||||
rt_uint16_t last_len = 0;
|
||||
|
||||
/* check parameter */
|
||||
RT_ASSERT(cfg_desc != RT_NULL);
|
||||
|
||||
ptr = (rt_uint32_t)cfg_desc + cfg_desc->bLength;
|
||||
while(ptr < (rt_uint32_t)cfg_desc + cfg_desc->wTotalLength)
|
||||
{
|
||||
if(depth++ > 0x40)
|
||||
{
|
||||
*iad_desc = RT_NULL;
|
||||
return -RT_EIO;
|
||||
}
|
||||
desc = (udesc_t)ptr;
|
||||
// 寻找接口描述符
|
||||
if (desc->type == USB_DESC_TYPE_INTERFACE)
|
||||
{
|
||||
if (((uintf_desc_t)desc)->bInterfaceNumber == num)
|
||||
{
|
||||
// 找到目标接口描述后,检查上一个描述符是不是接口关联描述符
|
||||
ptr -= last_len;
|
||||
desc = (udesc_t)ptr;
|
||||
if (desc->type == USB_DESC_TYPE_IAD)
|
||||
{
|
||||
*iad_desc = (uiad_desc_t)desc;
|
||||
return RT_EOK;
|
||||
}
|
||||
else
|
||||
{
|
||||
*iad_desc = NULL;
|
||||
return -RT_EIO;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 解析下一个描述符
|
||||
ptr = (rt_uint32_t)desc + desc->bLength;
|
||||
last_len = desc->bLength;
|
||||
}
|
||||
// 无法解析
|
||||
*iad_desc = NULL;
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will get an endpoint descriptor from the interface descriptor.
|
||||
*
|
||||
* @param intf_desc the point of interface descriptor structure.
|
||||
* @param num the number of endpoint descriptor.
|
||||
* @param ep_desc the point of endpoint descriptor point.
|
||||
*
|
||||
* @return the error code, RT_EOK on successfully.
|
||||
*/
|
||||
rt_err_t rt_usbh_get_endpoint_descriptor(uintf_desc_t intf_desc, int num,
|
||||
uep_desc_t* ep_desc)
|
||||
{
|
||||
int count = 0, depth = 0;
|
||||
rt_uint32_t ptr;
|
||||
udesc_t desc;
|
||||
|
||||
/* check parameter */
|
||||
RT_ASSERT(intf_desc != RT_NULL);
|
||||
RT_ASSERT(num < intf_desc->bNumEndpoints);
|
||||
*ep_desc = RT_NULL;
|
||||
|
||||
ptr = (rt_uint32_t)intf_desc + intf_desc->bLength;
|
||||
while(count < intf_desc->bNumEndpoints)
|
||||
{
|
||||
if(depth++ > 0x20)
|
||||
{
|
||||
*ep_desc = RT_NULL;
|
||||
return -RT_EIO;
|
||||
}
|
||||
desc = (udesc_t)ptr;
|
||||
if(desc->type == USB_DESC_TYPE_ENDPOINT)
|
||||
{
|
||||
if(num == count)
|
||||
{
|
||||
*ep_desc = (uep_desc_t)desc;
|
||||
|
||||
LOG_D("rt_usb_get_endpoint_descriptor: %d", num);
|
||||
return RT_EOK;
|
||||
}
|
||||
else count++;
|
||||
}
|
||||
ptr = (rt_uint32_t)desc + desc->bLength;
|
||||
}
|
||||
|
||||
rt_kprintf("rt_usb_get_endpoint_descriptor %d failed\n", num);
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
int rt_usb_hcd_pipe_xfer(uhcd_t hcd, upipe_t pipe, void* buffer, int nbytes, int timeout)
|
||||
{
|
||||
int ret_size;
|
||||
rt_uint8_t * pbuffer = (rt_uint8_t *)buffer;
|
||||
|
||||
LOG_D("pipe transform remain size: %d", nbytes);
|
||||
ret_size = hcd->ops->pipe_xfer(pipe, USBH_PID_DATA, pbuffer, nbytes, timeout);
|
||||
if(ret_size <= nbytes)
|
||||
{
|
||||
return ret_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return nbytes;
|
||||
}
|
||||
|
||||
int rt_usb_hcd_hub_port_set(uhcd_t hcd, uhub_param_t param)
|
||||
{
|
||||
int ret = RT_ERROR;
|
||||
if (hcd->ops->hub_port_set) {
|
||||
ret = hcd->ops->hub_port_set(param);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user