drv_usbd.c 18.2 KB
Newer Older
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
/*
 * File      : drv_usbc.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2009 RT-Thread Develop Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2017-12-04     ZYH          first implementation
 */
#include <usb/include/usb_device_config.h>
#include <usb/include/usb.h>
#include <rtthread.h>
#include <usb/phy/usb_phy.h>
#include <usb/device/usb_device.h>
#include <usb/device/usb_device_dci.h>
#include <rtdevice.h>
#ifdef RT_USING_EHCI0_AS_DEVICE
#ifdef RT_USING_EHCI1_AS_DEVICE
#error Can not using 2 controller as usb device
#endif
#endif
lymzzyh's avatar
lymzzyh 已提交
26
#define FSL_USB_HS
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

/* USB PHY condfiguration */
#define BOARD_USB_PHY_D_CAL (0x0CU)
#define BOARD_USB_PHY_TXCAL45DP (0x06U)
#define BOARD_USB_PHY_TXCAL45DM (0x06U)
#define BOARD_XTAL0_CLK_HZ 24000000U

#ifdef RT_USING_EHCI0_AS_DEVICE
static usb_device_handle ehci0_handle;
static struct udcd _fsl_udc_0;
#endif
#ifdef RT_USING_EHCI1_AS_DEVICE
static usb_device_handle ehci1_handle;
static struct udcd _fsl_udc_1;
#endif



static usb_status_t usb_device_callback(usb_device_handle handle, uint32_t callbackEvent, void *eventParam);
static usb_status_t usb_device_endpoint_callback(usb_device_handle handle, usb_device_endpoint_callback_message_struct_t *message, void *callbackParam);
void USB_DeviceIsrEnable(uint8_t controllerId)
{
    uint8_t irqNumber;
#if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)
    uint8_t usbDeviceEhciIrq[] = USBHS_IRQS;
    irqNumber = usbDeviceEhciIrq[controllerId - kUSB_ControllerEhci0];
#endif
/* Install isr, set priority, and enable IRQ. */
#if defined(__GIC_PRIO_BITS)
    GIC_SetPriority((IRQn_Type)irqNumber, 3);
#else
    NVIC_SetPriority((IRQn_Type)irqNumber, 3);
#endif
    EnableIRQ((IRQn_Type)irqNumber);
}
void USB_DeviceClockInit(uint8_t controllerId)
{
#if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)
    usb_phy_config_struct_t phyConfig = {
        BOARD_USB_PHY_D_CAL, BOARD_USB_PHY_TXCAL45DP, BOARD_USB_PHY_TXCAL45DM,
    };
#endif
#if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)
    if (controllerId == kUSB_ControllerEhci0)
    {
        CLOCK_EnableUsbhs0PhyPllClock(kCLOCK_Usbphy480M, 480000000U);
        CLOCK_EnableUsbhs0Clock(kCLOCK_Usb480M, 480000000U);
    }
    else
    {
        CLOCK_EnableUsbhs1PhyPllClock(kCLOCK_Usbphy480M, 480000000U);
        CLOCK_EnableUsbhs1Clock(kCLOCK_Usb480M, 480000000U);
    }
    USB_EhciPhyInit(controllerId, BOARD_XTAL0_CLK_HZ, &phyConfig);
#endif
}
#ifdef RT_USING_EHCI0_AS_DEVICE
lymzzyh's avatar
lymzzyh 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#ifdef FSL_USB_HS
static struct ep_id _ehci0_ep_pool[] =
{
    {0x0,  USB_EP_ATTR_CONTROL,     USB_DIR_INOUT,  64, ID_ASSIGNED  },
    {0x1,  USB_EP_ATTR_BULK,        USB_DIR_IN,     512, ID_UNASSIGNED},
    {0x1,  USB_EP_ATTR_BULK,        USB_DIR_OUT,    512, ID_UNASSIGNED},
    {0x2,  USB_EP_ATTR_INT,         USB_DIR_IN,     512, ID_UNASSIGNED},
    {0x2,  USB_EP_ATTR_INT,         USB_DIR_OUT,    512, ID_UNASSIGNED},
    {0x3,  USB_EP_ATTR_BULK,        USB_DIR_IN,     512, ID_UNASSIGNED},
    {0x3,  USB_EP_ATTR_BULK,        USB_DIR_OUT,    512, ID_UNASSIGNED},
    {0x4,  USB_EP_ATTR_INT,         USB_DIR_IN,     512, ID_UNASSIGNED},
    {0x4,  USB_EP_ATTR_INT,         USB_DIR_OUT,    512, ID_UNASSIGNED},
    {0x5,  USB_EP_ATTR_BULK,        USB_DIR_IN,     512, ID_UNASSIGNED},
    {0x5,  USB_EP_ATTR_BULK,        USB_DIR_OUT,    512, ID_UNASSIGNED},
    {0x6,  USB_EP_ATTR_INT,         USB_DIR_IN,     512, ID_UNASSIGNED},
    {0x6,  USB_EP_ATTR_INT,         USB_DIR_OUT,    512, ID_UNASSIGNED},
    {0x7,  USB_EP_ATTR_BULK,        USB_DIR_IN,     512, ID_UNASSIGNED},
    {0x7,  USB_EP_ATTR_BULK,        USB_DIR_OUT,    512, ID_UNASSIGNED},
    {0xFF, USB_EP_ATTR_TYPE_MASK,   USB_DIR_MASK,   0,  ID_ASSIGNED  },
};
#else
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
static struct ep_id _ehci0_ep_pool[] =
{
    {0x0,  USB_EP_ATTR_CONTROL,     USB_DIR_INOUT,  64, ID_ASSIGNED  },
    {0x1,  USB_EP_ATTR_BULK,        USB_DIR_IN,     64, ID_UNASSIGNED},
    {0x1,  USB_EP_ATTR_BULK,        USB_DIR_OUT,    64, ID_UNASSIGNED},
    {0x2,  USB_EP_ATTR_INT,         USB_DIR_IN,     64, ID_UNASSIGNED},
    {0x2,  USB_EP_ATTR_INT,         USB_DIR_OUT,    64, ID_UNASSIGNED},
    {0x3,  USB_EP_ATTR_BULK,        USB_DIR_IN,     64, ID_UNASSIGNED},
    {0x3,  USB_EP_ATTR_BULK,        USB_DIR_OUT,    64, ID_UNASSIGNED},
    {0x4,  USB_EP_ATTR_INT,         USB_DIR_IN,     64, ID_UNASSIGNED},
    {0x4,  USB_EP_ATTR_INT,         USB_DIR_OUT,    64, ID_UNASSIGNED},
    {0x5,  USB_EP_ATTR_BULK,        USB_DIR_IN,     64, ID_UNASSIGNED},
    {0x5,  USB_EP_ATTR_BULK,        USB_DIR_OUT,    64, ID_UNASSIGNED},
    {0x6,  USB_EP_ATTR_INT,         USB_DIR_IN,     64, ID_UNASSIGNED},
    {0x6,  USB_EP_ATTR_INT,         USB_DIR_OUT,    64, ID_UNASSIGNED},
    {0x7,  USB_EP_ATTR_BULK,        USB_DIR_IN,     64, ID_UNASSIGNED},
    {0x7,  USB_EP_ATTR_BULK,        USB_DIR_OUT,    64, ID_UNASSIGNED},
    {0xFF, USB_EP_ATTR_TYPE_MASK,   USB_DIR_MASK,   0,  ID_ASSIGNED  },
};
lymzzyh's avatar
lymzzyh 已提交
124
#endif
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
void USB_OTG1_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();
    
    USB_DeviceEhciIsrFunction(ehci0_handle);
    /* leave interrupt */
    rt_interrupt_leave();
}
static rt_err_t _ehci0_ep_set_stall(rt_uint8_t address)
{
    USB_DeviceStallEndpoint(ehci0_handle, address);
    return RT_EOK;
}
static rt_err_t _ehci0_ep_clear_stall(rt_uint8_t address)
{
    USB_DeviceUnstallEndpoint(ehci0_handle, address);
    return RT_EOK;
}

static rt_err_t _ehci0_set_address(rt_uint8_t address)
{
    USB_DeviceSetStatus(ehci0_handle, kUSB_DeviceStatusAddress, &address);
    return RT_EOK;
}
static rt_err_t _ehci0_set_config(rt_uint8_t address)
{
    return RT_EOK;
}
static rt_err_t _ehci0_ep_enable(uep_t ep)
{
    usb_device_endpoint_init_struct_t ep_init;
    usb_device_endpoint_callback_struct_t ep_callback;
lymzzyh's avatar
lymzzyh 已提交
158
    rt_uint32_t param = ep->ep_desc->bEndpointAddress;
159 160 161 162 163 164 165
    RT_ASSERT(ep != RT_NULL);
    RT_ASSERT(ep->ep_desc != RT_NULL);
    ep_init.maxPacketSize = ep->ep_desc->wMaxPacketSize;
    ep_init.endpointAddress = ep->ep_desc->bEndpointAddress;
    ep_init.transferType = ep->ep_desc->bmAttributes;
    ep_init.zlt = 0;
    ep_callback.callbackFn = usb_device_endpoint_callback;
lymzzyh's avatar
lymzzyh 已提交
166
    ep_callback.callbackParam = (void *)param;
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
    ep_callback.isBusy = 0;
    USB_DeviceInitEndpoint(ehci0_handle,&ep_init,&ep_callback);
    return RT_EOK;
}
static rt_err_t _ehci0_ep_disable(uep_t ep)
{
    RT_ASSERT(ep != RT_NULL);
    RT_ASSERT(ep->ep_desc != RT_NULL);
    USB_DeviceDeinitEndpoint(ehci0_handle, ep->ep_desc->bEndpointAddress);
    return RT_EOK;
}
static rt_size_t _ehci0_ep_read(rt_uint8_t address, void *buffer)
{
    rt_size_t size = 0;

    RT_ASSERT(buffer != RT_NULL);

    return size;
}
static rt_size_t _ehci0_ep_read_prepare(rt_uint8_t address, void *buffer, rt_size_t size)
{
    USB_DeviceRecvRequest(ehci0_handle, address, buffer, size);
    return size;
}
static rt_size_t _ehci0_ep_write(rt_uint8_t address, void *buffer, rt_size_t size)
{
    USB_DeviceSendRequest(ehci0_handle, address, buffer, size);
    return size;
}
static rt_err_t _ehci0_ep0_send_status(void)
{	
    _ehci0_ep_write(0x00, NULL, 0);
    return RT_EOK;
}
static rt_err_t _ehci0_suspend(void)
{
    return RT_EOK;
}

static rt_err_t _ehci0_wakeup(void)
{
    return RT_EOK;
}
const static struct udcd_ops _ehci0_udc_ops =
{
    _ehci0_set_address,
    _ehci0_set_config,
    _ehci0_ep_set_stall,
    _ehci0_ep_clear_stall,
    _ehci0_ep_enable,
    _ehci0_ep_disable,
    _ehci0_ep_read_prepare,
    _ehci0_ep_read,
    _ehci0_ep_write,
    _ehci0_ep0_send_status,
    _ehci0_suspend,
    _ehci0_wakeup,
};
static rt_err_t drv_ehci0_usbd_init(rt_device_t device)
{
    usb_status_t result;
    USB_DeviceClockInit(kUSB_ControllerEhci0);
    
    result = USB_DeviceInit(kUSB_ControllerEhci0,usb_device_callback,&ehci0_handle);
    RT_ASSERT(ehci0_handle);
    if(result == kStatus_USB_Success)
    {
        USB_DeviceIsrEnable(kUSB_ControllerEhci0);
        USB_DeviceRun(ehci0_handle);
    }
    else
    {
        rt_kprintf("USB_DeviceInit ehci0 error\r\n");
        return RT_ERROR;
    }
    return RT_EOK;
}
#endif
#ifdef RT_USING_EHCI1_AS_DEVICE
lymzzyh's avatar
lymzzyh 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
#ifdef FSL_USB_HS
static struct ep_id _ehci1_ep_pool[] =
{
    {0x0,  USB_EP_ATTR_CONTROL,     USB_DIR_INOUT,  64, ID_ASSIGNED  },
    {0x1,  USB_EP_ATTR_BULK,        USB_DIR_IN,     512, ID_UNASSIGNED},
    {0x1,  USB_EP_ATTR_BULK,        USB_DIR_OUT,    512, ID_UNASSIGNED},
    {0x2,  USB_EP_ATTR_INT,         USB_DIR_IN,     512, ID_UNASSIGNED},
    {0x2,  USB_EP_ATTR_INT,         USB_DIR_OUT,    512, ID_UNASSIGNED},
    {0x3,  USB_EP_ATTR_BULK,        USB_DIR_IN,     512, ID_UNASSIGNED},
    {0x3,  USB_EP_ATTR_BULK,        USB_DIR_OUT,    512, ID_UNASSIGNED},
    {0x4,  USB_EP_ATTR_INT,         USB_DIR_IN,     512, ID_UNASSIGNED},
    {0x4,  USB_EP_ATTR_INT,         USB_DIR_OUT,    512, ID_UNASSIGNED},
    {0x5,  USB_EP_ATTR_BULK,        USB_DIR_IN,     512, ID_UNASSIGNED},
    {0x5,  USB_EP_ATTR_BULK,        USB_DIR_OUT,    512, ID_UNASSIGNED},
    {0x6,  USB_EP_ATTR_INT,         USB_DIR_IN,     512, ID_UNASSIGNED},
    {0x6,  USB_EP_ATTR_INT,         USB_DIR_OUT,    512, ID_UNASSIGNED},
    {0x7,  USB_EP_ATTR_BULK,        USB_DIR_IN,     512, ID_UNASSIGNED},
    {0x7,  USB_EP_ATTR_BULK,        USB_DIR_OUT,    512, ID_UNASSIGNED},
    {0xFF, USB_EP_ATTR_TYPE_MASK,   USB_DIR_MASK,   0,  ID_ASSIGNED  },
};
#else
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
static struct ep_id _ehci1_ep_pool[] =
{
    {0x0,  USB_EP_ATTR_CONTROL,     USB_DIR_INOUT,  64, ID_ASSIGNED  },
    {0x1,  USB_EP_ATTR_BULK,        USB_DIR_IN,     64, ID_UNASSIGNED},
    {0x1,  USB_EP_ATTR_BULK,        USB_DIR_OUT,    64, ID_UNASSIGNED},
    {0x2,  USB_EP_ATTR_INT,         USB_DIR_IN,     64, ID_UNASSIGNED},
    {0x2,  USB_EP_ATTR_INT,         USB_DIR_OUT,    64, ID_UNASSIGNED},
    {0x3,  USB_EP_ATTR_BULK,        USB_DIR_IN,     64, ID_UNASSIGNED},
    {0x3,  USB_EP_ATTR_BULK,        USB_DIR_OUT,    64, ID_UNASSIGNED},
    {0x4,  USB_EP_ATTR_INT,         USB_DIR_IN,     64, ID_UNASSIGNED},
    {0x4,  USB_EP_ATTR_INT,         USB_DIR_OUT,    64, ID_UNASSIGNED},
    {0x5,  USB_EP_ATTR_BULK,        USB_DIR_IN,     64, ID_UNASSIGNED},
    {0x5,  USB_EP_ATTR_BULK,        USB_DIR_OUT,    64, ID_UNASSIGNED},
    {0x6,  USB_EP_ATTR_INT,         USB_DIR_IN,     64, ID_UNASSIGNED},
    {0x6,  USB_EP_ATTR_INT,         USB_DIR_OUT,    64, ID_UNASSIGNED},
    {0x7,  USB_EP_ATTR_BULK,        USB_DIR_IN,     64, ID_UNASSIGNED},
    {0x7,  USB_EP_ATTR_BULK,        USB_DIR_OUT,    64, ID_UNASSIGNED},
    {0xFF, USB_EP_ATTR_TYPE_MASK,   USB_DIR_MASK,   0,  ID_ASSIGNED  },
};
lymzzyh's avatar
lymzzyh 已提交
286
#endif
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
void USB_OTG2_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();
    USB_DeviceEhciIsrFunction(ehci1_handle);
    /* leave interrupt */
    rt_interrupt_leave();
}
static rt_err_t _ehci1_ep_set_stall(rt_uint8_t address)
{
    USB_DeviceStallEndpoint(ehci1_handle, address);
    return RT_EOK;
}
static rt_err_t _ehci1_ep_clear_stall(rt_uint8_t address)
{
    USB_DeviceUnstallEndpoint(ehci1_handle, address);
    return RT_EOK;
}

static rt_err_t _ehci1_set_address(rt_uint8_t address)
{
    USB_DeviceSetStatus(ehci1_handle, kUSB_DeviceStatusAddress, &address);
    return RT_EOK;
}
static rt_err_t _ehci1_set_config(rt_uint8_t address)
{
    return RT_EOK;
}
static rt_err_t _ehci1_ep_enable(uep_t ep)
{
    usb_device_endpoint_init_struct_t ep_init;
    usb_device_endpoint_callback_struct_t ep_callback;
    RT_ASSERT(ep != RT_NULL);
    RT_ASSERT(ep->ep_desc != RT_NULL);
    ep_init.maxPacketSize = ep->ep_desc->wMaxPacketSize;
    ep_init.endpointAddress = ep->ep_desc->bEndpointAddress;
    ep_init.transferType = ep->ep_desc->bmAttributes;
    ep_init.zlt = 0;
    ep_callback.callbackFn = usb_device_endpoint_callback;
    ep_callback.callbackParam = (void *)ep_init.endpointAddress;
    ep_callback.isBusy = 0;
    USB_DeviceInitEndpoint(ehci1_handle,&ep_init,&ep_callback);
    return RT_EOK;
}
static rt_err_t _ehci1_ep_disable(uep_t ep)
{
    RT_ASSERT(ep != RT_NULL);
    RT_ASSERT(ep->ep_desc != RT_NULL);
    USB_DeviceDeinitEndpoint(ehci1_handle, ep->ep_desc->bEndpointAddress);
    return RT_EOK;
}
static rt_size_t _ehci1_ep_read(rt_uint8_t address, void *buffer)
{
    rt_size_t size = 0;

    RT_ASSERT(buffer != RT_NULL);

    return size;
}
static rt_size_t _ehci1_ep_read_prepare(rt_uint8_t address, void *buffer, rt_size_t size)
{
    USB_DeviceRecvRequest(ehci1_handle, address, buffer, size);
    return size;
}
static rt_size_t _ehci1_ep_write(rt_uint8_t address, void *buffer, rt_size_t size)
{
    USB_DeviceSendRequest(ehci1_handle, address, buffer, size);
    return size;
}
static rt_err_t _ehci1_ep0_send_status(void)
{	
    _ehci1_ep_write(0x00, NULL, 0);
    return RT_EOK;
}
static rt_err_t _ehci1_suspend(void)
{
    return RT_EOK;
}

static rt_err_t _ehci1_wakeup(void)
{
    return RT_EOK;
}
const static struct udcd_ops _ehci1_udc_ops =
{
    _ehci1_set_address,
    _ehci1_set_config,
    _ehci1_ep_set_stall,
    _ehci1_ep_clear_stall,
    _ehci1_ep_enable,
    _ehci1_ep_disable,
    _ehci1_ep_read_prepare,
    _ehci1_ep_read,
    _ehci1_ep_write,
    _ehci1_ep0_send_status,
    _ehci1_suspend,
    _ehci1_wakeup,
};
static rt_err_t drv_ehci1_usbd_init(rt_device_t device)
{
    usb_status_t result;
    USB_DeviceClockInit(kUSB_ControllerEhci1);
    
    result = USB_DeviceInit(kUSB_ControllerEhci1,usb_device_callback,&ehci1_handle);
    RT_ASSERT(ehci1_handle);
    if(result == kStatus_USB_Success)
    {
        USB_DeviceIsrEnable(kUSB_ControllerEhci1);
        USB_DeviceRun(ehci1_handle);
    }
    else
    {
        rt_kprintf("USB_DeviceInit ehci1 error\r\n");
        return RT_ERROR;
    }
    return RT_EOK;
}
#endif



#if defined(RT_USING_EHCI0_AS_DEVICE) && defined(RT_USING_EHCI1_AS_DEVICE)
#error Can not using both now 
#endif
static int rt_usbd_init(void)
{
    #ifdef RT_USING_EHCI0_AS_DEVICE
    rt_memset((void *)&_fsl_udc_0, 0, sizeof(struct udcd));
    _fsl_udc_0.parent.type = RT_Device_Class_USBDevice;
    _fsl_udc_0.parent.init = drv_ehci0_usbd_init;
    _fsl_udc_0.ops = &_ehci0_udc_ops;
    /* Register endpoint infomation */
    _fsl_udc_0.ep_pool = _ehci0_ep_pool;
    _fsl_udc_0.ep0.id = &_ehci0_ep_pool[0];
lymzzyh's avatar
lymzzyh 已提交
421 422 423 424 425
    #ifdef FSL_USB_HS
    _fsl_udc_0.device_is_hs = RT_TRUE;
    #else
    _fsl_udc_0.device_is_hs = RT_FALSE;
    #endif
426 427 428 429 430 431 432 433 434 435 436
    rt_device_register((rt_device_t)&_fsl_udc_0, "usbd", 0);
    rt_usb_device_init();
    #endif
    #ifdef RT_USING_EHCI1_AS_DEVICE
    rt_memset((void *)&_fsl_udc_1, 0, sizeof(struct udcd));
    _fsl_udc_1.parent.type = RT_Device_Class_USBDevice;
    _fsl_udc_1.parent.init = drv_ehci1_usbd_init;
    _fsl_udc_1.ops = &_ehci1_udc_ops;
    /* Register endpoint infomation */
    _fsl_udc_1.ep_pool = _ehci1_ep_pool;
    _fsl_udc_1.ep0.id = &_ehci1_ep_pool[0];
lymzzyh's avatar
lymzzyh 已提交
437 438 439 440 441
    #ifdef FSL_USB_HS
    _fsl_udc_1.device_is_hs = RT_TRUE;
    #else
    _fsl_udc_1.device_is_hs = RT_FALSE;
    #endif
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
    rt_device_register((rt_device_t)&_fsl_udc_1, "usbd", 0);
    rt_usb_device_init();
    #endif
    return 0;
}
INIT_DEVICE_EXPORT(rt_usbd_init);
static usb_status_t usb_device_endpoint_callback(usb_device_handle handle, usb_device_endpoint_callback_message_struct_t *message, void *callbackParam)
{
    rt_uint32_t ep_addr = (rt_uint32_t)callbackParam;
    usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
    udcd_t udcd = RT_NULL;
    uint8_t state;
    if(deviceHandle->controllerId == kUSB_ControllerEhci0)
    {
        #ifdef RT_USING_EHCI0_AS_DEVICE
        udcd = &_fsl_udc_0;
        #endif
    }
    else
    {
        #ifdef RT_USING_EHCI1_AS_DEVICE
        udcd = &_fsl_udc_1;
        #endif
    }
    if(message->isSetup)
    {
lymzzyh's avatar
lymzzyh 已提交
468
        //rt_kprintf("1udcd:%#08X\n",udcd);
469 470 471 472 473 474 475 476 477 478 479 480 481
        rt_usbd_ep0_setup_handler(udcd, (struct urequest*)message->buffer);
    }
    else if(ep_addr == 0x00)
    {
        USB_DeviceGetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
        if(state == kUSB_DeviceStateAddressing)
        {
            if (kStatus_USB_Success == USB_DeviceSetStatus(handle, kUSB_DeviceStatusAddress, NULL))
            {
                state = kUSB_DeviceStateAddress;
                USB_DeviceSetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
            }
        }
lymzzyh's avatar
lymzzyh 已提交
482
        //rt_kprintf("2udcd:%#08X\n",udcd);
483 484 485 486 487 488 489 490 491 492 493 494 495
        rt_usbd_ep0_out_handler(udcd,message->length);
    }
    else if(ep_addr == 0x80)
    {
        USB_DeviceGetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
        if(state == kUSB_DeviceStateAddressing)
        {
            if (kStatus_USB_Success == USB_DeviceSetStatus(handle, kUSB_DeviceStatusAddress, NULL))
            {
                state = kUSB_DeviceStateAddress;
                USB_DeviceSetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
            }
        }
lymzzyh's avatar
lymzzyh 已提交
496
        //rt_kprintf("3udcd:%#08X\n",udcd);
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
        rt_usbd_ep0_in_handler(udcd);
    }
    else if(ep_addr&0x80)
    {
        rt_usbd_ep_in_handler(udcd, ep_addr, message->length);
    }
    else
    {
        rt_usbd_ep_out_handler(udcd, ep_addr, message->length);
    }
    return kStatus_USB_Success;
}
static usb_status_t usb_device_callback(usb_device_handle handle, uint32_t callbackEvent, void *eventParam)
{
    usb_status_t error = kStatus_USB_Error;
    usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
    usb_device_endpoint_init_struct_t ep0_init = 
    {
        0x40,
        0x00,
        USB_EP_ATTR_CONTROL,
        0
    };
    usb_device_endpoint_callback_struct_t ep0_callback = 
    {
        usb_device_endpoint_callback,
        0,
        0
    };
    udcd_t udcd = RT_NULL;
    if(deviceHandle->controllerId == kUSB_ControllerEhci0)
    {
        #ifdef RT_USING_EHCI0_AS_DEVICE
        udcd = &_fsl_udc_0;
        #endif
    }
    else
    {
        #ifdef RT_USING_EHCI1_AS_DEVICE
        udcd = &_fsl_udc_1;
        #endif
    }
    switch (callbackEvent)
    {
    case kUSB_DeviceEventBusReset:
        ep0_init.endpointAddress = 0x00;
        ep0_callback.callbackParam = (void *)0x00;
        USB_DeviceInitEndpoint(deviceHandle,&ep0_init,&ep0_callback);
        ep0_init.endpointAddress = 0x80;
        ep0_callback.callbackParam = (void *)0x80;
        USB_DeviceInitEndpoint(deviceHandle,&ep0_init,&ep0_callback);
        rt_usbd_reset_handler(udcd);
        break;
    case kUSB_DeviceEventAttach:
        rt_usbd_connect_handler(udcd);
        break;
    case kUSB_DeviceEventDetach:
        rt_usbd_disconnect_handler(udcd);
        break;
    }
    return error;
}