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
|
||||
Reference in New Issue
Block a user