Simple USBHost MSD(USB flash drive) for EA LPC4088 QSB test program

Dependencies:   LPC4088-USBHost mbed

EA LPC4088をUSBホストにしてUSBフラッシュメモリ(USB flash drive)を読み書きするテストプログラムです。
/media/uploads/va009039/lpc4088-msd-1.jpg
/media/uploads/va009039/lpc4088-msd-2.png

https://bitbucket.org/va009039/lpc4088_usbhost

Committer:
va009039
Date:
Tue Apr 22 10:54:52 2014 +0000
Revision:
0:11152e69fc05
first commit,sync rev.25.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 0:11152e69fc05 1 // BaseUsbHostIsoEp.cpp 2014/4/21
va009039 0:11152e69fc05 2 #include "USBHost.h"
va009039 0:11152e69fc05 3 //#define DEBUG
va009039 0:11152e69fc05 4 #include "BaseUsbHostDebug.h"
va009039 0:11152e69fc05 5 //#define TEST
va009039 0:11152e69fc05 6 #include "BaseUsbHostTest.h"
va009039 0:11152e69fc05 7
va009039 0:11152e69fc05 8 HCITD::HCITD(USBEndpoint* obj, uint16_t FrameNumber, int FrameCount, uint16_t PacketSize) {
va009039 0:11152e69fc05 9 Control = 0xe0000000 | // CC ConditionCode NOT ACCESSED
va009039 0:11152e69fc05 10 ((FrameCount-1) << 24)| // FC FrameCount
va009039 0:11152e69fc05 11 TD_DELAY_INT(0) | // DI DelayInterrupt
va009039 0:11152e69fc05 12 FrameNumber; // SF StartingFrame
va009039 0:11152e69fc05 13 BufferPage0 = const_cast<uint8_t*>(buf);
va009039 0:11152e69fc05 14 BufferEnd = const_cast<uint8_t*>(buf) + PacketSize * FrameCount - 1;
va009039 0:11152e69fc05 15 Next = NULL;
va009039 0:11152e69fc05 16 ep = obj;
va009039 0:11152e69fc05 17 uint32_t addr = reinterpret_cast<uint32_t>(buf);
va009039 0:11152e69fc05 18 for(int i = 0; i < FrameCount; i++) {
va009039 0:11152e69fc05 19 uint16_t offset = addr & 0x0fff;
va009039 0:11152e69fc05 20 if ((addr&0xfffff000) == (reinterpret_cast<uint32_t>(BufferEnd)&0xfffff000)) {
va009039 0:11152e69fc05 21 offset |= 0x1000;
va009039 0:11152e69fc05 22 }
va009039 0:11152e69fc05 23 OffsetPSW[i] = 0xe000|offset;
va009039 0:11152e69fc05 24 addr += PacketSize;
va009039 0:11152e69fc05 25 }
va009039 0:11152e69fc05 26 }
va009039 0:11152e69fc05 27
va009039 0:11152e69fc05 28 IsochronousEp::IsochronousEp(int addr, uint8_t ep, uint16_t size):USBEndpoint(addr, ep, size)
va009039 0:11152e69fc05 29 {
va009039 0:11152e69fc05 30 setType(ISOCHRONOUS_ENDPOINT);
va009039 0:11152e69fc05 31 DBG("%p FA:%d EP:%02X MPS:%d\n", this, addr, ep, size);
va009039 0:11152e69fc05 32 TEST_ASSERT(m_pED);
va009039 0:11152e69fc05 33
va009039 0:11152e69fc05 34 m_pED->Control |= (1 << 15); // F Format ITD
va009039 0:11152e69fc05 35
va009039 0:11152e69fc05 36 TEST_ASSERT(size >= 128 && size <= 1023);
va009039 0:11152e69fc05 37 m_PacketSize = size;
va009039 0:11152e69fc05 38 m_FrameCount = 4; // 1-8
va009039 0:11152e69fc05 39 TEST_ASSERT(m_FrameCount >= 1 && m_FrameCount <= 8);
va009039 0:11152e69fc05 40 m_itd_queue_count = 0;
va009039 0:11152e69fc05 41 reset();
va009039 0:11152e69fc05 42 HCITD* itd = new_HCITD(this);
va009039 0:11152e69fc05 43 m_pED->TailTd = reinterpret_cast<HCTD*>(itd);
va009039 0:11152e69fc05 44 m_pED->HeadTd = reinterpret_cast<HCTD*>(itd);
va009039 0:11152e69fc05 45 TEST_ASSERT(itd);
va009039 0:11152e69fc05 46 if (itd == NULL) {
va009039 0:11152e69fc05 47 return;
va009039 0:11152e69fc05 48 }
va009039 0:11152e69fc05 49 HCCA* hcca = reinterpret_cast<HCCA*>(LPC_USB->HcHCCA);
va009039 0:11152e69fc05 50 TEST_ASSERT(hcca);
va009039 0:11152e69fc05 51 if (hcca == NULL) {
va009039 0:11152e69fc05 52 return;
va009039 0:11152e69fc05 53 }
va009039 0:11152e69fc05 54 for(int i = 0; i < 32; i++) {
va009039 0:11152e69fc05 55 if (hcca->InterruptTable[i] == NULL) {
va009039 0:11152e69fc05 56 hcca->InterruptTable[i] = m_pED;
va009039 0:11152e69fc05 57 } else {
va009039 0:11152e69fc05 58 HCED* nextEd = hcca->InterruptTable[i];
va009039 0:11152e69fc05 59 while(nextEd->Next && nextEd->Next != m_pED) {
va009039 0:11152e69fc05 60 nextEd = nextEd->Next;
va009039 0:11152e69fc05 61 }
va009039 0:11152e69fc05 62 nextEd->Next = m_pED;
va009039 0:11152e69fc05 63 }
va009039 0:11152e69fc05 64 }
va009039 0:11152e69fc05 65 DBG_ED(m_pED);
va009039 0:11152e69fc05 66 }
va009039 0:11152e69fc05 67
va009039 0:11152e69fc05 68 void IsochronousEp::reset(int delay_ms)
va009039 0:11152e69fc05 69 {
va009039 0:11152e69fc05 70 m_FrameNumber = LPC_USB->HcFmNumber + delay_ms;
va009039 0:11152e69fc05 71 }
va009039 0:11152e69fc05 72
va009039 0:11152e69fc05 73 HCITD* IsochronousEp::new_HCITD(USBEndpoint* obj)
va009039 0:11152e69fc05 74 {
va009039 0:11152e69fc05 75 HCITD* itd = new(m_PacketSize*m_FrameCount)HCITD(obj, m_FrameNumber, m_FrameCount, m_PacketSize);
va009039 0:11152e69fc05 76 if (itd == NULL) {
va009039 0:11152e69fc05 77 return NULL;
va009039 0:11152e69fc05 78 }
va009039 0:11152e69fc05 79 m_FrameNumber += m_FrameCount;
va009039 0:11152e69fc05 80 return itd;
va009039 0:11152e69fc05 81 }
va009039 0:11152e69fc05 82
va009039 0:11152e69fc05 83 HCITD* IsochronousEp::isochronousReveive(int millisec)
va009039 0:11152e69fc05 84 {
va009039 0:11152e69fc05 85 TEST_ASSERT(m_itd_queue_count >= 0);
va009039 0:11152e69fc05 86 while(m_itd_queue_count < 3 && m_itd_queue_count < HCTD_QUEUE_SIZE) {
va009039 0:11152e69fc05 87 HCITD* itd = reinterpret_cast<HCITD*>(m_pED->TailTd);
va009039 0:11152e69fc05 88 TEST_ASSERT(itd);
va009039 0:11152e69fc05 89 if (itd == NULL) {
va009039 0:11152e69fc05 90 return NULL;
va009039 0:11152e69fc05 91 }
va009039 0:11152e69fc05 92 HCITD* blank_itd = new_HCITD(this);
va009039 0:11152e69fc05 93 TEST_ASSERT(blank_itd);
va009039 0:11152e69fc05 94 if (blank_itd == NULL) {
va009039 0:11152e69fc05 95 return NULL;
va009039 0:11152e69fc05 96 }
va009039 0:11152e69fc05 97 itd->Next = blank_itd;
va009039 0:11152e69fc05 98 m_pED->TailTd = reinterpret_cast<HCTD*>(blank_itd);
va009039 0:11152e69fc05 99 m_itd_queue_count++;
va009039 0:11152e69fc05 100 //DBG_IED(m_pED);
va009039 0:11152e69fc05 101 USBHALHost::enable(ISOCHRONOUS_ENDPOINT); // Enable Periodic
va009039 0:11152e69fc05 102 }
va009039 0:11152e69fc05 103
va009039 0:11152e69fc05 104 HCITD* itd = get_queue_HCITD(millisec);
va009039 0:11152e69fc05 105 if (itd) {
va009039 0:11152e69fc05 106 m_itd_queue_count--;
va009039 0:11152e69fc05 107 }
va009039 0:11152e69fc05 108 return itd;
va009039 0:11152e69fc05 109 }
va009039 0:11152e69fc05 110
va009039 0:11152e69fc05 111 HCITD* IsochronousEp::get_queue_HCITD(int millisec)
va009039 0:11152e69fc05 112 {
va009039 0:11152e69fc05 113 for(int i = 0; i < 16; i++) {
va009039 0:11152e69fc05 114 osEvent evt = m_queue.get(millisec);
va009039 0:11152e69fc05 115 if (evt.status == osEventMessage) {
va009039 0:11152e69fc05 116 HCITD* itd = reinterpret_cast<HCITD*>(evt.value.p);
va009039 0:11152e69fc05 117 TEST_ASSERT(itd);
va009039 0:11152e69fc05 118 return itd;
va009039 0:11152e69fc05 119 } else if (evt.status == osOK) {
va009039 0:11152e69fc05 120 continue;
va009039 0:11152e69fc05 121 } else if (evt.status == osEventTimeout) {
va009039 0:11152e69fc05 122 return NULL;
va009039 0:11152e69fc05 123 } else {
va009039 0:11152e69fc05 124 DBG("evt.status: %02x\n", evt.status);
va009039 0:11152e69fc05 125 TEST_ASSERT(evt.status == osEventMessage);
va009039 0:11152e69fc05 126 return NULL;
va009039 0:11152e69fc05 127 }
va009039 0:11152e69fc05 128 }
va009039 0:11152e69fc05 129 return NULL;
va009039 0:11152e69fc05 130 }
va009039 0:11152e69fc05 131