core/teeny_usb_device.c
// init the endpoint from config descriptor
init the endpoint from config descriptor
init the endpoint from config descriptor
static int tusb_init_endpoint_by_config(tusb_device_t *dev)
{
    tusb_ep_config ep_cfg[TUSB_MAX_EP_PAIR_COUNT * 2];
    if (!dev->descriptors)
    {
        TUSB_LOGE("Device descriptors not set\n");
        return -1;
    }
    if (!dev->descriptors->configs || !dev->descriptors->config_count)
    {
        TUSB_LOGE("Device config descriptor not set\n");
        return -1;
    }
    if (!dev->config)
    {
        TUSB_LOGE("Wrong config number, %d\n", dev->config);
        return -1;
    }
    if (dev->descriptors->config_count < dev->config)
    {
        TUSB_LOGE("Device config descriptor number (%d) less than request (%d)\n",
                  (int)dev->descriptors->config_count, dev->config);
        return -1;
    }
    desc_t cfg = dev->descriptors->configs[dev->config - 1];
    if (!cfg)
    {
        TUSB_LOGE("Config descritor not set at %d\n", dev->config);
        return -1;
    }
    uint16_t total_len = cfg[2] | (cfg[3] << 8);
    uint16_t index = 0;
    uint8_t bmAttr = cfg[7];
    dev->status = 0;
    if (bmAttr & 0x40)
    {
        SetBusPowered(dev);
    }
    if (bmAttr & 0x20)
    {
        SetRemoteWakeup(dev);
    }
    uint16_t bcdUSB = dev->descriptors->device[2] | (dev->descriptors->device[3]<<8);
    ep_cfg[0].addr = 0x80;
    ep_cfg[0].attribute = USBD_EP_TYPE_CTRL;
    if(bcdUSB >= 0x300){
        ep_cfg[0].mps = 1<<dev->descriptors->device[7];
    }else{
        ep_cfg[0].mps = dev->descriptors->device[7];
    }
    ep_cfg[1].addr = 0x00;
    ep_cfg[1].attribute = USBD_EP_TYPE_CTRL;
    ep_cfg[1].mps = ep_cfg[0].mps;
#ifdef TUSB_SUPPORT_USB3
    ep_cfg[0].burst_count = 1;
    ep_cfg[1].burst_count = 1;
#endif
    int valid_intf = 0;
    int ep_count = 2;
    while (index < total_len)
    {
        uint8_t bLength = cfg[index];
        uint8_t bType = cfg[index + 1];
        if (bType == USB_INTERFACE_DESCRIPTOR_TYPE)
        {
            valid_intf = 0;
            uint8_t bIntfNum = cfg[index + 2];
            uint8_t bIntfAlt = cfg[index + 3];
            if (bIntfNum < TUSB_MAX_INTERFACE_COUNT)
            {
                if (dev->alt_cfg[bIntfNum] == bIntfAlt)
                {
                    valid_intf = 1;
                }
            }
            else
            {
                TUSB_LOGW("Interface number(%d) large than TUSB_MAX_INTERFACE_COUNT, ignore it\n", bIntfNum);
            }
        }
        else if (bType == USB_ENDPOINT_DESCRIPTOR_TYPE)
        {
            if (valid_intf)
            {
                if (ep_count < TUSB_MAX_EP_PAIR_COUNT * 2)
                {
                    uint8_t ep_addr = cfg[index + 2];
                    if ((ep_addr & 0x7f) < TUSB_MAX_EP_PAIR_COUNT)
                    {
                        tusb_ep_config *epc = &ep_cfg[ep_count];
                        epc->addr = ep_addr;
                        epc->attribute = cfg[index + 3];
                        epc->mps = cfg[index + 4] | (cfg[index + 5] << 8);
#ifdef TUSB_SUPPORT_USB3
                        epc->burst_count = 1;
#endif
                        ep_count++;
                    }
                    else
                    {
                        TUSB_LOGW("Endpoint addr(0x%02x) large than TUSB_MAX_EP_PAIR_COUNT, ignore it\n", ep_addr);
                    }
                }
                else
                {
                    TUSB_LOGW("Endpoint count overflow, ignore it\n");
                }
            }
        }else if(bType == USB_SS_ENDPOINT_COMPANION_TYPE){
#ifdef TUSB_SUPPORT_USB3
            tusb_ep_config *epc = &ep_cfg[ep_count-1];
            epc->burst_count = cfg[index + 2] + 1;
#endif
        }
        index += bLength;
    }
    TUSB_LOGD("Endpoint config summary:\n");
    for (int i = 0; i < ep_count; i++)
    {
        const tusb_ep_config *epc = &ep_cfg[i];
#ifdef TUSB_SUPPORT_USB3
        TUSB_LOGD("Addr: 0x%02x Attr: 0x%02x MPS:%4d Burst:%d\n", epc->addr, epc->attribute, epc->mps, epc->burst_count);
#else
        TUSB_LOGD("Addr: 0x%02x Attr: 0x%02x MPS:%d\n", epc->addr, epc->attribute, epc->mps);
#endif
    }
    tusb_dev_drv_setup_endpoint(GetDriver(dev), ep_cfg, ep_count, 0);
    return 0;
}
static int tusb_init_endpoint_by_config ( tusb_device_t * dev ) { tusb_ep_config ep_cfg [ TUSB_MAX_EP_PAIR_COUNT * 2 ] ; if ( ! dev -> descriptors ) { TUSB_LOGE ( " \n " ) ; return -1 ; } if ( ! dev -> descriptors -> configs || ! dev -> descriptors -> config_count ) { TUSB_LOGE ( " \n " ) ; return -1 ; } if ( ! dev -> config ) { TUSB_LOGE ( " \n " , dev -> config ) ; return -1 ; } if ( dev -> descriptors -> config_count < dev -> config ) { TUSB_LOGE ( " \n " , ( int ) dev -> descriptors -> config_count , dev -> config ) ; return -1 ; } desc_t cfg = dev -> descriptors -> configs [ dev -> config - 1 ] ; if ( ! cfg ) { TUSB_LOGE ( " \n " , dev -> config ) ; return -1 ; } uint16_t total_len = cfg [ 2 ] | ( cfg [ 3 ] << 8 ) ; uint16_t index = 0 ; uint8_t bmAttr = cfg [ 7 ] ; dev -> status = 0 ; if ( bmAttr & 0x40 ) { SetBusPowered ( dev ) ; } if ( bmAttr & 0x20 ) { SetRemoteWakeup ( dev ) ; } uint16_t bcdUSB = dev -> descriptors -> device [ 2 ] | ( dev -> descriptors -> device [ 3 ] << 8 ) ; ep_cfg [ 0 ] . addr = 0x80 ; ep_cfg [ 0 ] . attribute = USBD_EP_TYPE_CTRL ; if ( bcdUSB >= 0x300 ) { ep_cfg [ 0 ] . mps = 1 << dev -> descriptors -> device [ 7 ] ; } else { ep_cfg [ 0 ] . mps = dev -> descriptors -> device [ 7 ] ; } ep_cfg [ 1 ] . addr = 0x00 ; ep_cfg [ 1 ] . attribute = USBD_EP_TYPE_CTRL ; ep_cfg [ 1 ] . mps = ep_cfg [ 0 ] . mps ; #ifdef TUSB_SUPPORT_USB3 ep_cfg [ 0 ] . burst_count = 1 ; ep_cfg [ 1 ] . burst_count = 1 ; #endif int valid_intf = 0 ; int ep_count = 2 ; while ( index < total_len ) { uint8_t bLength = cfg [ index ] ; uint8_t bType = cfg [ index + 1 ] ; if ( bType == USB_INTERFACE_DESCRIPTOR_TYPE ) { valid_intf = 0 ; uint8_t bIntfNum = cfg [ index + 2 ] ; uint8_t bIntfAlt = cfg [ index + 3 ] ; if ( bIntfNum < TUSB_MAX_INTERFACE_COUNT ) { if ( dev -> alt_cfg [ bIntfNum ] == bIntfAlt ) { valid_intf = 1 ; } } else { TUSB_LOGW ( " \n " , bIntfNum ) ; } } else if ( bType == USB_ENDPOINT_DESCRIPTOR_TYPE ) { if ( valid_intf ) { if ( ep_count < TUSB_MAX_EP_PAIR_COUNT * 2 ) { uint8_t ep_addr = cfg [ index + 2 ] ; if ( ( ep_addr & 0x7f ) < TUSB_MAX_EP_PAIR_COUNT ) { tusb_ep_config * epc = & ep_cfg [ ep_count ] ; epc -> addr = ep_addr ; epc -> attribute = cfg [ index + 3 ] ; epc -> mps = cfg [ index + 4 ] | ( cfg [ index + 5 ] << 8 ) ; #ifdef TUSB_SUPPORT_USB3 epc -> burst_count = 1 ; #endif ep_count ++ ; } else { TUSB_LOGW ( " \n " , ep_addr ) ; } } else { TUSB_LOGW ( " \n " ) ; } } } else if ( bType == USB_SS_ENDPOINT_COMPANION_TYPE ) { #ifdef TUSB_SUPPORT_USB3 tusb_ep_config * epc = & ep_cfg [ ep_count - 1 ] ; epc -> burst_count = cfg [ index + 2 ] + 1 ; #endif } index += bLength ; } TUSB_LOGD ( " \n " ) ; for ( int i = 0 ; i < ep_count ; i ++ ) { const tusb_ep_config * epc = & ep_cfg [ i ] ; #ifdef TUSB_SUPPORT_USB3 TUSB_LOGD ( " \n " , epc -> addr , epc -> attribute , epc -> mps , epc -> burst_count ) ; #else TUSB_LOGD ( " \n " , epc -> addr , epc -> attribute , epc -> mps ) ; #endif } tusb_dev_drv_setup_endpoint ( GetDriver ( dev ) , ep_cfg , ep_count , 0 ) ; return 0 ; }
init the endpoint from config descriptor
init the endpoint from config descriptor
// bus powered // remote wakeup // collect endpoint config here