/************************************************************************** Global variables **************************************************************************/ int unit_test_usb_host(void) { static _usb_host_handle host_handle; USB_STATUS error; USB_lock(); _int_install_unexpected_isr(); if (MQX_OK != _usb_host_driver_install(USBCFG_DEFAULT_HOST_CONTROLLER)) { printf("\n Driver installation failed"); _task_block(); } error = _usb_host_init(USBCFG_DEFAULT_HOST_CONTROLLER, &host_handle); if (error == USB_OK) { error = _usb_host_driver_info_register(host_handle, (pointer)DriverInfoTable); if (error == USB_OK) { error = _usb_host_register_service(host_handle, USB_SERVICE_HOST_RESUME, NULL); } } USB_unlock(); if (error != USB_OK) { _task_block(); } if (MQX_OK != _lwevent_create(&usb_event, LWEVENT_AUTO_CLEAR)) { return -1; } /* prepare events to be auto or manual */ _lwevent_set_auto_clear(&usb_event, 0xFFFFFFFF); /* pre-set events */ _lwevent_clear(&usb_event, 0xFFFFFFFF); _lwevent_wait_ticks(&usb_event, 0x1, FALSE, 0); return 0; }
void USB_task(uint_32 param) { _usb_host_handle host_handle; USB_STATUS error; pointer usb_fs_handle = NULL; #if DEMO_USE_POOLS && defined(DEMO_MFS_POOL_ADDR) && defined(DEMO_MFS_POOL_SIZE) _MFS_pool_id = _mem_create_pool((pointer)DEMO_MFS_POOL_ADDR, DEMO_MFS_POOL_SIZE); #endif _lwsem_create(&USB_Stick,0); _lwevent_create(&USB_Event,0); USB_lock(); _int_install_unexpected_isr(); if (MQX_OK != _usb_host_driver_install(USBCFG_DEFAULT_HOST_CONTROLLER)) { printf("\n Driver installation failed"); _task_block(); } error = _usb_host_init(USBCFG_DEFAULT_HOST_CONTROLLER, &host_handle); if (error == USB_OK) { error = _usb_host_driver_info_register(host_handle, (pointer)ClassDriverInfoTable); if (error == USB_OK) { error = _usb_host_register_service(host_handle, USB_SERVICE_HOST_RESUME,NULL); } } USB_unlock(); if (error == USB_OK) { for ( ; ; ) { // Wait for insertion or removal event _lwevent_wait_ticks(&USB_Event,USB_EVENT,FALSE,0); if ( device.STATE== USB_DEVICE_ATTACHED) { if (device.SUPPORTED) { error = _usb_hostdev_select_interface(device.DEV_HANDLE, device.INTF_HANDLE, (pointer)&device.CLASS_INTF); if(error == USB_OK) { device.STATE = USB_DEVICE_INTERFACED; USB_handle = (pointer)&device.CLASS_INTF; // Install the file system usb_fs_handle = usb_filesystem_install( USB_handle, "USB:", "PM_C1:", "c:" ); if (usb_fs_handle) { // signal the application _lwsem_post(&USB_Stick); } } } else { device.STATE = USB_DEVICE_INTERFACED; } } else if ( device.STATE==USB_DEVICE_DETACHED) { _lwsem_wait(&USB_Stick); // remove the file system usb_filesystem_uninstall(usb_fs_handle); } // clear the event _lwevent_clear(&USB_Event,USB_EVENT); } } }
void USB_task(uint_32 param) { _usb_host_handle host_handle; USB_STATUS error; pointer usb_fs_handle = NULL; usb_msg_t msg; /* Store mounting point used. A: is the first one, bit #0 assigned, Z: is the last one, bit #25 assigned */ uint_32 fs_mountp = 0; #if DEMOCFG_USE_POOLS && defined(DEMOCFG_MFS_POOL_ADDR) && defined(DEMOCFG_MFS_POOL_SIZE) _MFS_pool_id = _mem_create_pool((pointer)DEMOCFG_MFS_POOL_ADDR, DEMOCFG_MFS_POOL_SIZE); #endif /* This event will inform other tasks that the filesystem on USB was successfully installed */ _lwsem_create(&USB_Stick, 0); if (MQX_OK != _lwmsgq_init(usb_taskq, 20, USB_TASKQ_GRANM)) { // lwmsgq_init failed _task_block(); } USB_lock(); _int_install_unexpected_isr(); if (MQX_OK != _usb_host_driver_install(USBCFG_DEFAULT_HOST_CONTROLLER)) { printf("\n Driver installation failed"); _task_block(); } error = _usb_host_init(USBCFG_DEFAULT_HOST_CONTROLLER, &host_handle); if (error == USB_OK) { error = _usb_host_driver_info_register(host_handle, (pointer)ClassDriverInfoTable); if (error == USB_OK) { error = _usb_host_register_service(host_handle, USB_SERVICE_HOST_RESUME,NULL); } } USB_unlock(); if (error != USB_OK) { _task_block(); } for (;;) { /* Wait for event sent as a message */ _lwmsgq_receive(&usb_taskq, (_mqx_max_type *) &msg, LWMSGQ_RECEIVE_BLOCK_ON_EMPTY, 0, 0); //if (device.STATE == USB_DEVICE_ATTACHED) { if (msg.body == USB_EVENT_ATTACH) { /* This event is not so important, because it does not inform about successfull USB stack enumeration */ } else if (msg.body == USB_EVENT_INTF && fs_mountp != 0x3FFC) { /* if mountpoints c: to z: are already used */ // Install the file system, use device->ccs as a handle usb_fs_handle = usb_filesystem_install( (pointer) msg.ccs, "USB:", "PM_C1:", "c:"); if (usb_fs_handle) { DEVICE_STRUCT_PTR dsp = (DEVICE_STRUCT_PTR) msg.ccs; dsp->mount = 'c'; // Mark file system as mounted fs_mountp |= 1 << (dsp->mount - 'a'); // Unlock the USB_Stick = signal to the application as available _lwsem_post(&USB_Stick); } } else if (msg.body == USB_EVENT_DETACH) { DEVICE_STRUCT_PTR dsp = (DEVICE_STRUCT_PTR) msg.ccs; if (dsp->mount >= 'a' && dsp->mount <= 'z') { // Lock the USB_Stick = mark as unavailable _lwsem_wait(&USB_Stick); // Remove the file system usb_filesystem_uninstall(usb_fs_handle); // Mark file system as unmounted fs_mountp &= ~(1 << (dsp->mount - 'a')); } /* Here, the device finishes its lifetime */ _mem_free(dsp); } } }