audio_mic.c 15.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2019-09-07     flybreak     the first version
 */

#include <rthw.h>
#include <rtdevice.h>
#include "drivers/usb_device.h"

#include "uaudioreg.h"

#define DBG_TAG              "usbd.audio.mic"
#define DBG_LVL              DBG_INFO
#include <rtdbg.h>

#define RECORD_SAMPLERATE   16000
#define RECORD_CHANNEL      1
#define RESOLUTION_BITS     16

#define RESOLUTION_BYTE     (RESOLUTION_BITS / 8)
#define RECORD_PER_MS_SZ    ((RECORD_SAMPLERATE * RECORD_CHANNEL * RESOLUTION_BYTE) / 1000)
#define RECORD_BUFFER_SZ    (RECORD_PER_MS_SZ * 20)  /* 20ms */

#if defined(RT_USBD_MIC_DEVICE_NAME)
    #define MIC_DEVICE_NAME    RT_USBD_MIC_DEVICE_NAME
#else
    #define MIC_DEVICE_NAME    "mic0"
#endif

#define EVENT_RECORD_START   (1 << 0)
#define EVENT_RECORD_STOP    (1 << 1)
#define EVENT_RECORD_DATA    (1 << 2)

#define MIC_INTF_STR_INDEX 8
/*
 * uac mic descriptor define
 */

#define UAC_CS_INTERFACE            0x24
#define UAC_CS_ENDPOINT             0x25

#define UAC_MAX_PACKET_SIZE         64
#define UAC_EP_MAX_PACKET_SIZE      32
#define UAC_CHANNEL_NUM             RECORD_CHANNEL

struct uac_ac_descriptor
{
#ifdef RT_USB_DEVICE_COMPOSITE
    struct uiad_descriptor iad_desc;
#endif
    struct uinterface_descriptor intf_desc;
    struct usb_audio_control_descriptor hdr_desc;
    struct usb_audio_input_terminal it_desc;
    struct usb_audio_output_terminal ot_desc;
#if UAC_USE_FEATURE_UNIT
    struct usb_audio_feature_unit feature_unit_desc;
#endif
};

struct uac_as_descriptor
{
    struct uinterface_descriptor intf_desc;
    struct usb_audio_streaming_interface_descriptor hdr_desc;
    struct usb_audio_streaming_type1_descriptor format_type_desc;
    struct uendpoint_descriptor ep_desc;
    struct usb_audio_streaming_endpoint_descriptor as_ep_desc;
};

/*
 * uac mic device type
 */

struct uac_audio_mic
{
    rt_device_t  dev;
    rt_event_t   event;
    rt_uint8_t   open_count;

    rt_uint8_t  *buffer;
    rt_uint32_t  buffer_index;

    uep_t        ep;
};
static struct uac_audio_mic mic;

ALIGN(4)
static struct udevice_descriptor dev_desc =
{
    USB_DESC_LENGTH_DEVICE,     //bLength;
    USB_DESC_TYPE_DEVICE,       //type;
    USB_BCD_VERSION,            //bcdUSB;
    USB_CLASS_DEVICE,           //bDeviceClass;
    0x00,                       //bDeviceSubClass;
    0x00,                       //bDeviceProtocol;
    UAC_MAX_PACKET_SIZE,        //bMaxPacketSize0;
    _VENDOR_ID,                 //idVendor;
    _PRODUCT_ID,                //idProduct;
    USB_BCD_DEVICE,             //bcdDevice;
    USB_STRING_MANU_INDEX,      //iManufacturer;
    USB_STRING_PRODUCT_INDEX,   //iProduct;
    USB_STRING_SERIAL_INDEX,    //iSerialNumber;Unused.
    USB_DYNAMIC,                //bNumConfigurations;
};

//FS and HS needed
ALIGN(4)
static struct usb_qualifier_descriptor dev_qualifier =
{
    sizeof(dev_qualifier),          //bLength
    USB_DESC_TYPE_DEVICEQUALIFIER,  //bDescriptorType
    0x0200,                         //bcdUSB
    USB_CLASS_AUDIO,                //bDeviceClass
    0x00,                           //bDeviceSubClass
    0x00,                           //bDeviceProtocol
    64,                             //bMaxPacketSize0
    0x01,                           //bNumConfigurations
    0,
};

ALIGN(4)
const static char *_ustring[] =
{
    "Language",
    "RT-Thread Team.",
    "RT-Thread Audio Microphone",
    "32021919830108",
    "Configuration",
    "Interface",
};

ALIGN(4)
static struct uac_ac_descriptor ac_desc =
{
#ifdef RT_USB_DEVICE_COMPOSITE
    /* Interface Association Descriptor */
    {
        USB_DESC_LENGTH_IAD,
        USB_DESC_TYPE_IAD,
        USB_DYNAMIC,
        0x02,
        USB_CLASS_AUDIO,
        USB_SUBCLASS_AUDIOSTREAMING,
        0x00,
        0x00,
    },
#endif
    /* Interface Descriptor */
    {
        USB_DESC_LENGTH_INTERFACE,
        USB_DESC_TYPE_INTERFACE,
        USB_DYNAMIC,
        0x00,
        0x00,
        USB_CLASS_AUDIO,
        USB_SUBCLASS_AUDIOCONTROL,
        0x00,
#ifdef RT_USB_DEVICE_COMPOSITE
        MIC_INTF_STR_INDEX,
#else
        0x00,
#endif
    },
    /* Header Descriptor */
    {
        sizeof(struct usb_audio_control_descriptor),
        UAC_CS_INTERFACE,
        UDESCSUB_AC_HEADER,
        0x0100,    /* Version: 1.00 */
        0x001E,    /* Total length: 30 */
        0x01,      /* Total number of interfaces: 1 */
        {0x01},    /* Interface number: 1 */
    },
    /*  Input Terminal Descriptor */
    {
        sizeof(struct usb_audio_input_terminal),
        UAC_CS_INTERFACE,
        UDESCSUB_AC_INPUT,
        0x01,      /* Terminal ID: 1 */
        0x0201,    /* Terminal Type: Microphone (0x0201) */
        0x00,      /* Assoc Terminal: 0 */
        0x01,      /* Number Channels: 1 */
        0x0000,    /* Channel Config: 0x0000 */
        0x00,      /* Channel Names: 0 */
        0x00,      /* Terminal: 0 */
    },
    /*  Output Terminal Descriptor */
    {
        sizeof(struct usb_audio_output_terminal),
        UAC_CS_INTERFACE,
        UDESCSUB_AC_OUTPUT,
        0x02,      /* Terminal ID: 2 */
        0x0101,    /* Terminal Type: USB Streaming (0x0101) */
        0x00,      /* Assoc Terminal: 0 */
        0x01,      /* Source ID: 1 */
        0x00,      /* Terminal: 0 */
    },
#if UAC_USE_FEATURE_UNIT
    /*  Feature unit Descriptor */
    {
        sizeof(struct usb_audio_feature_unit),
        UAC_CS_INTERFACE,
        UDESCSUB_AC_FEATURE,
        0x02,
        0x01,
        0x01,
        0x00,
        0x01,
    },
#endif
};

ALIGN(4)
static struct uinterface_descriptor as_desc0 =
{
    USB_DESC_LENGTH_INTERFACE,
    USB_DESC_TYPE_INTERFACE,
    USB_DYNAMIC,
    0x00,
    0x00,
    USB_CLASS_AUDIO,
    USB_SUBCLASS_AUDIOSTREAMING,
    0x00,
    0x00,
};

ALIGN(4)
static struct uac_as_descriptor as_desc =
{
    /* Interface Descriptor */
    {
        USB_DESC_LENGTH_INTERFACE,
        USB_DESC_TYPE_INTERFACE,
        USB_DYNAMIC,
        0x01,
        0x01,
        USB_CLASS_AUDIO,
        USB_SUBCLASS_AUDIOSTREAMING,
        0x00,
        0x00,
    },
    /* General AS Descriptor */
    {
        sizeof(struct usb_audio_streaming_interface_descriptor),
        UAC_CS_INTERFACE,
        AS_GENERAL,
        0x02,      /* Terminal ID: 2 */
        0x01,      /* Interface delay in frames: 1 */
        UA_FMT_PCM,
    },
    /* Format type i Descriptor */
    {
        sizeof(struct usb_audio_streaming_type1_descriptor),
        UAC_CS_INTERFACE,
        FORMAT_TYPE,
        FORMAT_TYPE_I,
        UAC_CHANNEL_NUM,
        2,         /* Subframe Size: 2 */
        RESOLUTION_BITS,
        0x01,      /* Samples Frequence Type: 1 */
        {0},       /* Samples Frequence */
    },
    /* Endpoint Descriptor */
    {
        USB_DESC_LENGTH_ENDPOINT,
        USB_DESC_TYPE_ENDPOINT,
        USB_DYNAMIC | USB_DIR_IN,
        USB_EP_ATTR_ISOC,
        UAC_EP_MAX_PACKET_SIZE,
        0x01,
    },
    /* AS Endpoint Descriptor */
    {
        sizeof(struct usb_audio_streaming_endpoint_descriptor),
        UAC_CS_ENDPOINT,
        AS_GENERAL,
    },
};

void mic_entry(void *parameter)
{
    struct rt_audio_caps caps = {0};
    rt_uint32_t e, index;

    mic.buffer = rt_malloc(RECORD_BUFFER_SZ);
    if (mic.buffer == RT_NULL)
    {
        LOG_E("malloc failed");
        goto __exit;
    }

    mic.dev = rt_device_find(MIC_DEVICE_NAME);
    if (mic.dev == RT_NULL)
    {
        LOG_E("can't find device:%s", MIC_DEVICE_NAME);
        goto __exit;
    }

    while (1)
    {
        if (rt_event_recv(mic.event, EVENT_RECORD_START | EVENT_RECORD_STOP,
                          RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
                          1000, &e) != RT_EOK)
        {
            continue;
        }
        if (mic.open_count == 0)
        {
            continue;
        }
        LOG_D("record start");

        rt_device_open(mic.dev, RT_DEVICE_OFLAG_RDONLY);

        caps.main_type               = AUDIO_TYPE_INPUT;
        caps.sub_type                = AUDIO_DSP_PARAM;
        caps.udata.config.samplerate = RECORD_SAMPLERATE;
        caps.udata.config.channels   = RECORD_CHANNEL;
        caps.udata.config.samplebits = RESOLUTION_BITS;
        rt_device_control(mic.dev, AUDIO_CTL_CONFIGURE, &caps);

        while (1)
        {
            if (rt_event_recv(mic.event, EVENT_RECORD_DATA | EVENT_RECORD_STOP,
                              RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
                              1000, &e) != RT_EOK)
            {
                if (mic.open_count > 0)
                    continue;
                else
                    break;
            }
            if (e & EVENT_RECORD_DATA)
            {
                index = (mic.buffer_index >= RECORD_BUFFER_SZ / 2) ? 0 : (RECORD_BUFFER_SZ / 2);
                rt_device_read(mic.dev, 0, mic.buffer + index, RECORD_BUFFER_SZ / 2);
            }
            else if (e & EVENT_RECORD_STOP)
            {
                break;
            }
        }
        LOG_D("record stop");
        rt_device_close(mic.dev);
    }

__exit:
    if (mic.buffer)
        rt_free(mic.buffer);
}

static rt_err_t _record_start(ufunction_t func)
{
    mic.ep->request.buffer = RT_NULL;
    mic.ep->request.size = UAC_EP_MAX_PACKET_SIZE;
    mic.ep->request.req_type = UIO_REQUEST_WRITE;
    rt_usbd_io_request(func->device, mic.ep, &mic.ep->request);

    mic.open_count ++;
    rt_event_send(mic.event, EVENT_RECORD_START);
    return 0;
}

static rt_err_t _record_stop(ufunction_t func)
{
    mic.open_count --;
    rt_event_send(mic.event, EVENT_RECORD_STOP);
    return 0;
}

static rt_err_t _ep_data_in_handler(ufunction_t func, rt_size_t size)
{
    RT_ASSERT(func != RT_NULL);
    LOG_D("_ep_data_in_handler");

    mic.ep->request.buffer = mic.buffer + mic.buffer_index;
    mic.ep->request.size = UAC_EP_MAX_PACKET_SIZE;
    mic.ep->request.req_type = UIO_REQUEST_WRITE;
    rt_usbd_io_request(func->device, mic.ep, &mic.ep->request);

    mic.buffer_index += UAC_EP_MAX_PACKET_SIZE;
    if (mic.buffer_index >= RECORD_BUFFER_SZ)
    {
        mic.buffer_index = 0;
        rt_event_send(mic.event, EVENT_RECORD_DATA);
    }
    else if (mic.buffer_index == RECORD_BUFFER_SZ / 2)
    {
        rt_event_send(mic.event, EVENT_RECORD_DATA);
    }

    return RT_EOK;
}

static rt_err_t _interface_as_handler(ufunction_t func, ureq_t setup)
{
    RT_ASSERT(func != RT_NULL);
    RT_ASSERT(func->device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

    LOG_D("_interface_as_handler");

    if ((setup->request_type & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_STANDARD)
    {
        switch (setup->bRequest)
        {
        case USB_REQ_GET_INTERFACE:
            break;
        case USB_REQ_SET_INTERFACE:
            LOG_D("set interface handler");
            if (setup->wValue == 1)
            {
                _record_start(func);
            }
            else if (setup->wValue == 0)
            {
                _record_stop(func);
            }
            break;
        default:
            LOG_D("unknown uac request 0x%x", setup->bRequest);
            return -RT_ERROR;
        }
    }

    return RT_EOK;
}

static rt_err_t _function_enable(ufunction_t func)
{
    RT_ASSERT(func != RT_NULL);

    LOG_D("uac function enable");

    return RT_EOK;
}

static rt_err_t _function_disable(ufunction_t func)
{
    RT_ASSERT(func != RT_NULL);

    LOG_D("uac function disable");
    _record_stop(func);
    return RT_EOK;
}

static struct ufunction_ops ops =
{
    _function_enable,
    _function_disable,
    RT_NULL,
};
/**
 * This function will configure uac descriptor.
 *
 * @param comm the communication interface number.
 * @param data the data interface number.
 *
 * @return RT_EOK on successful.
 */
static rt_err_t _uac_descriptor_config(struct uac_ac_descriptor *ac,
                                       rt_uint8_t cintf_nr, struct uac_as_descriptor *as, rt_uint8_t sintf_nr)
{
    ac->hdr_desc.baInterfaceNr[0] = sintf_nr;
#ifdef RT_USB_DEVICE_COMPOSITE
    ac->iad_desc.bFirstInterface = cintf_nr;
#endif

    return RT_EOK;
}

static rt_err_t _uac_samplerate_config(struct uac_as_descriptor *as, rt_uint32_t samplerate)
{
    as->format_type_desc.tSamFreq[0 * 3 + 2] = samplerate >> 16 & 0xff;
    as->format_type_desc.tSamFreq[0 * 3 + 1] = samplerate >> 8 & 0xff;
    as->format_type_desc.tSamFreq[0 * 3 + 0] = samplerate & 0xff;
    return RT_EOK;
}

/**
 * This function will create a uac function instance.
 *
 * @param device the usb device object.
 *
 * @return RT_EOK on successful.
 */
ufunction_t rt_usbd_function_uac_mic_create(udevice_t device)
{
    ufunction_t func;
    uintf_t intf_ac, intf_as;
    ualtsetting_t setting_as0;
    ualtsetting_t setting_ac, setting_as;
    struct uac_as_descriptor *as_desc_t;

    /* parameter check */
    RT_ASSERT(device != RT_NULL);

#ifdef RT_USB_DEVICE_COMPOSITE
    rt_usbd_device_set_interface_string(device, MIC_INTF_STR_INDEX, _ustring[2]);
#else
    /* set usb device string description */
    rt_usbd_device_set_string(device, _ustring);
#endif
    /* create a uac function */
    func = rt_usbd_function_new(device, &dev_desc, &ops);
    //not support HS
    //rt_usbd_device_set_qualifier(device, &dev_qualifier);

    /* create interface */
    intf_ac = rt_usbd_interface_new(device, RT_NULL);
    intf_as = rt_usbd_interface_new(device, _interface_as_handler);

    /* create alternate setting */
    setting_ac = rt_usbd_altsetting_new(sizeof(struct uac_ac_descriptor));
    setting_as0 = rt_usbd_altsetting_new(sizeof(struct uinterface_descriptor));
    setting_as = rt_usbd_altsetting_new(sizeof(struct uac_as_descriptor));
    /* config desc in alternate setting */
    rt_usbd_altsetting_config_descriptor(setting_ac, &ac_desc,
                                         (rt_off_t) & ((struct uac_ac_descriptor *)0)->intf_desc);
    rt_usbd_altsetting_config_descriptor(setting_as0, &as_desc0, 0);
    rt_usbd_altsetting_config_descriptor(setting_as, &as_desc,
                                         (rt_off_t) & ((struct uac_as_descriptor *)0)->intf_desc);
    /* configure the uac interface descriptor */
    _uac_descriptor_config(setting_ac->desc, intf_ac->intf_num, setting_as->desc, intf_as->intf_num);
    _uac_samplerate_config(setting_as->desc, RECORD_SAMPLERATE);

    /* create endpoint */
    as_desc_t = (struct uac_as_descriptor *)setting_as->desc;
    mic.ep = rt_usbd_endpoint_new(&as_desc_t->ep_desc, _ep_data_in_handler);

    /* add the endpoint to the alternate setting */
    rt_usbd_altsetting_add_endpoint(setting_as, mic.ep);

    /* add the alternate setting to the interface, then set default setting of the interface */
    rt_usbd_interface_add_altsetting(intf_ac, setting_ac);
    rt_usbd_set_altsetting(intf_ac, 0);
    rt_usbd_interface_add_altsetting(intf_as, setting_as0);
    rt_usbd_interface_add_altsetting(intf_as, setting_as);
    rt_usbd_set_altsetting(intf_as, 0);

    /* add the interface to the uac function */
    rt_usbd_function_add_interface(func, intf_ac);
    rt_usbd_function_add_interface(func, intf_as);

    return func;
}

int audio_mic_init(void)
{
    rt_thread_t mic_tid;
    mic.event = rt_event_create("mic_event", RT_IPC_FLAG_FIFO);

    mic_tid = rt_thread_create("mic_thread",
                               mic_entry, RT_NULL,
                               1024,
                               5, 10);

    if (mic_tid != RT_NULL)
        rt_thread_startup(mic_tid);
    return RT_EOK;
}
INIT_COMPONENT_EXPORT(audio_mic_init);

/*
 *  register uac class
 */
struct udclass uac_class =
{
    .rt_usbd_function_create = rt_usbd_function_uac_mic_create
};

int rt_usbd_uac_mic_class_register(void)
{
    rt_usbd_class_register(&uac_class);
    return 0;
}
INIT_PREV_EXPORT(rt_usbd_uac_mic_class_register);