USB host library, support isochronous,bulk,interrupt and control.
Dependents: BaseUsbHost_example BaseJpegDecode_example SimpleJpegDecode_example
Import programBaseUsbHost_example
BaseUsbHost example program
Diff: BaseUsbHost.cpp
- Revision:
- 3:ae77d63a1eda
- Parent:
- 0:b7d6879637a8
- Child:
- 4:d931d24c2f81
--- a/BaseUsbHost.cpp Tue Dec 11 15:26:54 2012 +0000 +++ b/BaseUsbHost.cpp Sun Jan 06 11:45:18 2013 +0000 @@ -1,4 +1,4 @@ -// BaseUsbHost.cpp 2012/12/4 +// BaseUsbHost.cpp 2012/12/25 #include "mbed.h" #include "rtos.h" #include "BaseUsbHost.h" @@ -95,8 +95,6 @@ } LPC_USB->HcHCCA = (uint32_t)pHcca; LPC_USB->HcInterruptStatus |= LPC_USB->HcInterruptStatus; /* Clear Interrrupt Status */ - - //LPC_USB->HcInterruptEnable = MIE|WDH|RHSC|FNO; LPC_USB->HcInterruptEnable = OR_INTR_ENABLE_MIE|OR_INTR_ENABLE_WDH|OR_INTR_ENABLE_FNO; LPC_USB->HcRhPortStatus1 = OR_RH_PORT_CSC; @@ -212,6 +210,77 @@ ed->Control |= size<<16; } +int BaseEp::transfer(uint8_t* buf, int len) +{ + HCTD* data_td = m_pED->TailTd; + TEST_ASSERT(data_td); + if (data_td == NULL) { + return USB_ERROR; + } + data_td->CurrBufPtr = buf; + data_td->buf_top = buf; + data_td->buf_size = len; + data_td->BufEnd = const_cast<uint8_t*>(buf)+len-1; + HCTD* blank_td = new_HCTD(); + TEST_ASSERT(blank_td); + if (blank_td == NULL) { + return USB_ERROR_MEMORY; + } + data_td->Next = reinterpret_cast<uint32_t>(blank_td); + m_pED->TailTd = blank_td; + enable(); + return USB_PROCESSING; +} + +int BaseEp::status(uint32_t millisec) +{ + HCTD* td = get_queue_HCTD(millisec); + if (td == NULL) { + return USB_PROCESSING; + } + uint8_t cc = td->Control>>28; + int ret; + switch(cc) { + case 0: + case 8: + case 9: + ret = td->buf_size; + if (td->CurrBufPtr) { + ret = td->CurrBufPtr - td->buf_top; + } + break; + case 4: + ret = USB_ERROR_STALL; + break; + case 5: + ret = USB_ERROR_DEVICE_NOT_RESPONDING; + break; + default: + DBG("ConditionCode=%d\n", cc); + ret = USB_ERROR; + break; + } + delete_HCTD(td); + return ret; +} + +int BaseEp::send_receive(uint8_t* buf, int len, int millisec) +{ + if (m_td_queue_count == 0) { + int rc = transfer(buf, len); + if (rc != USB_PROCESSING) { + return rc; + } + m_td_queue_count++; + } + + int ret = status(millisec); + if (ret != USB_PROCESSING) { + m_td_queue_count--; + } + return ret; +} + HCTD* BaseEp::new_HCTD(int buf_size) { HCTD* td; @@ -269,9 +338,15 @@ HCTD* td = get_queue_HCTD(millisec); if (td) { if (td->Control & TD_CC) { + uint8_t cc = td->Control>>28; DBG_TD(td); delete_HCTD(td); - return USB_ERROR; + switch(cc) { + case 4: + return USB_ERROR_STALL; + default: + return USB_ERROR; + } } delete_HCTD(td); if (td == wait_td) {