Hello!
I'm looking to devise a way to handle connection and disconnection of a USB Flash Drive using the UART2DSC project as a base. I wanted to ask a couple questions regarding what would be the "clean" way to handle such a thing?
My thought is to use a timer and essentially poll the USB host connection status every 250ms or so and act upon that. The device configuration and timer thread looks like this:
vos_tcb_t *tcbPOLL_USB;
VOS_HANDLE hTIMER; //USB Poll Timer
void POLL_USB();
void main(void){
// USB Host configuration context
usbhost_context_t usbhostContext;
//TIMER Configuration context
tmr_context_t timerContext;
// Initialise FAT File System Driver
fatdrv_init(VOS_DEV_FAT_FILE_SYSTEM);
// Initialise BOMS Device Driver
boms_init(VOS_DEV_BOMS);
// Initialise USB Host
usbhostContext.if_count = 8;
usbhostContext.ep_count = 16;
usbhostContext.xfer_count = 2;
usbhostContext.iso_xfer_count = 2;
usbhost_init(VOS_DEV_USBHOST_1, -1, &usbhostContext);
//Initialise Timer0 - USB Poll Device Status - Connect/Disconnect
timerContext.timer_identifier = TIMER_0;
tmr_init(TIMER_0, &timerContext);
tcbPOLL_USB = vos_create_thread_ex(24, 256, POLL_USB, "PollUSB", 0);
/*Snipped - Other Device Config*/
vos_start_scheduler();
}
//POLL_USB TIMER THREAD
void POLL_USB(){
tmr_ioctl_cb_t tmr_iocb;
uint8_t usb_connstate;
static uint8_t usb_connstate_prev = PORT_STATE_DISCONNECTED;
hTIMER = vos_dev_open(TIMER0);
tmr_iocb.ioctl_code = VOS_IOCTL_TIMER_SET_TICK_SIZE;
tmr_iocb.param = TIMER_TICK_MS;
vos_dev_ioctl(hTIMER,&tmr_iocb);
tmr_iocb.ioctl_code = VOS_IOCTL_TIMER_SET_COUNT;
tmr_iocb.param = 250;
vos_dev_ioctl(hTIMER,&tmr_iocb);
vos_iocb.ioctl_code = VOS_IOCTL_TIMER_SET_MODE;
tmr_iocb.param = TIMER_MODE_CONTINUOUS;
vos_dev_ioctl(hTIMER,&tmr_iocb);
tmr_iocb.ioctl_code = VOS_IOCTL_TIMER_START;
vos_dev_ioctl(hTIMER,&tmr_iocb);
while (1){
tmr_iocb.ioctl_code = VOS_IOCTL_TIMER_WAIT_ON_COMPLETE;
vos_dev_ioctl(hTIMER, &tmr_iocb);
usb_connstate = usbhost_connect_state(hUSBHOST_1);
if (usb_connstate == PORT_STATE_DISCONNECTED){
if (usb_connstate_prev != PORT_STATE_DISCONNECTED){
//Connected -> Disconnected --Detach BOMS and FAT File System
boms_detach(hBOMS);
fat_detach(hFAT_FILE_SYSTEM);
}
}
if (usb_connstat == PORT_STATE_ENUMERATED){
if (usb_connstate_prev != PORT_STATE_ENUMERATED){
//Un-enumerated -> Enumerated --Attach BOMS and FAT File system
hBOMS = boms_attach(hUSBHOST_1, VOS_DEV_BOMS);
hFAT_FILE_SYSTEM = fat_attach(hBOMS, VOS_DEV_FAT_FILE_SYSTEM);
//attach the stdio file system to the FAT file system
fsAttach(hFAT_FILE_SYSTEM);
}
}
usb_connstate_prev = usb_connstate;
}
}
Would it all be just as simple at this above? Do I have to do anything extra to cleanly detach the BOMS and FAT when the device has been removed? Would I need to re-initialize them via their respective init_ functions or is detach/attach functions adequate after the initial init_ calls at the start of main()? Is the call to fsAttach necessary after a disconnect/reconnect? or should I only call that once on initialization?