Simple USBHost library for Nucleo F446RE/F411RE/F401RE FRDM-KL46Z/KL25Z/F64F LPC4088/LPC1768
Dependents: F401RE-BTstack_example F401RE-USBHostMSD_HelloWorld
Fork of KL46Z-USBHost by
USBHALHost2_LPC4088.cpp
00001 #if defined(TARGET_LPC4088)||defined(TARGET_LPC1768) 00002 #include "USBHALHost.h" 00003 00004 #undef USB_TEST_ASSERT 00005 void usb_test_assert_internal(const char *expr, const char *file, int line); 00006 #define USB_TEST_ASSERT(EXPR) while(!(EXPR)){usb_test_assert_internal(#EXPR,__FILE__,__LINE__);} 00007 00008 #define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\n");}while(0); 00009 00010 //#define DBG_USE_POSIX_MEMALIGN 00011 00012 #ifdef DBG_USE_POSIX_MEMALIGN 00013 void* usb_ram_malloc(size_t size, int aligment) 00014 { 00015 TEST_ASSERT(aligment >= 4); 00016 TEST_ASSERT(!(aligment & 3)); 00017 void* p; 00018 if (posix_memalign(&p, aligment, size) == 0) { 00019 return p; 00020 } 00021 return NULL; 00022 } 00023 00024 void usb_ram_free(void* p) 00025 { 00026 free(p); 00027 } 00028 #else 00029 00030 #define CHUNK_SIZE 64 00031 00032 #if defined(TARGET_LPC1768) 00033 #define USB_RAM_BASE 0x2007C000 00034 #define USB_RAM_SIZE 0x4000 00035 #define BLOCK_COUNT (USB_RAM_SIZE/CHUNK_SIZE) 00036 #elif defined(TARGET_LPC4088) 00037 #define USB_RAM_BASE 0x20000000 00038 #define USB_RAM_SIZE 0x4000 00039 #define BLOCK_COUNT (USB_RAM_SIZE/CHUNK_SIZE) 00040 #else 00041 #error "target error" 00042 #endif 00043 00044 static uint8_t* ram = NULL; 00045 static uint8_t* map; 00046 00047 static void usb_ram_init() 00048 { 00049 USB_INFO("USB_RAM: 0x%p(%d)", USB_RAM_BASE, USB_RAM_SIZE); 00050 ram = (uint8_t*)USB_RAM_BASE; 00051 USB_TEST_ASSERT((int)ram%256 == 0); 00052 map = (uint8_t*)malloc(BLOCK_COUNT); 00053 USB_TEST_ASSERT(map); 00054 memset(map, 0, BLOCK_COUNT); 00055 } 00056 00057 // first fit malloc 00058 void* usb_ram_malloc(size_t size, int aligment) 00059 { 00060 USB_TEST_ASSERT(aligment >= 4); 00061 USB_TEST_ASSERT(!(aligment & 3)); 00062 if (ram == NULL) { 00063 usb_ram_init(); 00064 } 00065 int needs = (size+CHUNK_SIZE-1)/CHUNK_SIZE; 00066 void* p = NULL; 00067 for(int idx = 0; idx < BLOCK_COUNT;) { 00068 bool found = true; 00069 for(int i = 0; i < needs; i++) { 00070 int block = map[idx + i]; 00071 if (block != 0) { 00072 idx += block; 00073 found = false; 00074 break; 00075 } 00076 } 00077 if (!found) { 00078 continue; 00079 } 00080 p = ram+idx*CHUNK_SIZE; 00081 if ((int)p % aligment) { 00082 idx++; 00083 continue; 00084 } 00085 USB_TEST_ASSERT((idx + needs) <= BLOCK_COUNT); 00086 for(int i = 0; i < needs; i++) { 00087 map[idx + i] = needs - i; 00088 } 00089 break; 00090 } 00091 USB_TEST_ASSERT(p); 00092 return p; 00093 } 00094 00095 void usb_ram_free(void* p) 00096 { 00097 USB_TEST_ASSERT(p >= ram); 00098 USB_TEST_ASSERT(p < (ram+CHUNK_SIZE*BLOCK_COUNT)); 00099 int idx = ((int)p-(int)ram)/CHUNK_SIZE; 00100 int block = map[idx]; 00101 USB_TEST_ASSERT(block >= 1); 00102 for(int i =0; i < block; i++) { 00103 map[idx + i] = 0; 00104 } 00105 } 00106 00107 #endif // DBG_USE_POSIX_MEMALIGN 00108 00109 void print_td(FILE* stream, HCTD* td) 00110 { 00111 if (td == NULL) { 00112 fprintf(stream, "TD %p:\n", td); 00113 return; 00114 } 00115 uint32_t* p = reinterpret_cast<uint32_t*>(td); 00116 fprintf(stream, "TD %p: %08X %08X %08X %08X", p, p[0], p[1], p[2], p[3]); 00117 fprintf(stream, " ep=%p\n", td->parent); 00118 uint8_t* bp = reinterpret_cast<uint8_t*>(p[1]); 00119 uint8_t* be = reinterpret_cast<uint8_t*>(p[3]); 00120 if (bp) { 00121 fprintf(stream, "BF %p:", bp); 00122 while(bp <= be) { 00123 fprintf(stream, " %02X", *bp); 00124 bp++; 00125 } 00126 fprintf(stream, "\n"); 00127 } 00128 } 00129 00130 void print_ed(FILE* stream, HCED* ed) 00131 { 00132 uint32_t* p = reinterpret_cast<uint32_t*>(ed); 00133 while(p) { 00134 fprintf(stream, "ED %p: %08X %08X %08X %08X\n", p, p[0], p[1], p[2], p[3]); 00135 HCTD* td = reinterpret_cast<HCTD*>(p[2] & ~3); 00136 HCTD* tdtail = reinterpret_cast<HCTD*>(p[1]); 00137 while(td != NULL && td != tdtail) { 00138 print_td(stream, td); 00139 td = td->Next; 00140 } 00141 p = reinterpret_cast<uint32_t*>(p[3]); 00142 } 00143 } 00144 00145 void print_itd(FILE* stream, HCITD* itd) 00146 { 00147 if (itd == NULL) { 00148 fprintf(stream, "ITD %p:\n", itd); 00149 return; 00150 } 00151 uint32_t* p = reinterpret_cast<uint32_t*>(itd); 00152 fprintf(stream, "ITD %p: %08X %08X %08X %08X", p, p[0], p[1], p[2], p[3]); 00153 fprintf(stream, " ep=%p\n", itd->parent); 00154 uint16_t* offset = reinterpret_cast<uint16_t*>(p+4); 00155 fprintf(stream, "ITD %p: %04X %04X %04X %04X %04X %04X %04X %04X\n", offset, 00156 offset[0], offset[1], offset[2], offset[3], offset[4], offset[5], offset[6], offset[7]); 00157 } 00158 00159 void print_ied(FILE* stream, HCED* ed) 00160 { 00161 uint32_t* p = reinterpret_cast<uint32_t*>(ed); 00162 while(p) { 00163 fprintf(stream, "ED %p: %08X %08X %08X %08X\n", p, p[0], p[1], p[2], p[3]); 00164 HCITD* itd = reinterpret_cast<HCITD*>(p[2] & ~3); 00165 HCITD* itdtail = reinterpret_cast<HCITD*>(p[1]); 00166 while(itd != NULL && itd != itdtail) { 00167 print_itd(stream, itd); 00168 itd = itd->Next; 00169 } 00170 p = reinterpret_cast<uint32_t*>(p[3]); 00171 } 00172 } 00173 00174 void print_bytes(FILE* stream, char* s, uint8_t* buf, int len) 00175 { 00176 fprintf(stream, "%s %d:", s, len); 00177 for(int i = 0; i < len; i++) { 00178 fprintf(stream, " %02X", buf[i]); 00179 } 00180 fprintf(stream, "\n"); 00181 } 00182 00183 void print_hex(FILE* stream, uint8_t* p, int len) 00184 { 00185 for(int i = 0; i < len; i++) { 00186 if (i%16 == 0) { 00187 fprintf(stream, "%p:", p); 00188 } 00189 fprintf(stream, " %02X", *p); 00190 p++; 00191 if (i%16 == 15) { 00192 fprintf(stream, "\n"); 00193 } 00194 } 00195 fprintf(stream, "\n"); 00196 } 00197 00198 void assert_print(const char* pf, int line, const char* msg) { 00199 printf("\n\n%s@%d %s ASSERT!\n\n", pf, line, msg); 00200 exit(1); 00201 } 00202 00203 #endif // defined(TARGET_LPC4088)||defined(TARGET_LPC1768) 00204 00205
Generated on Wed Jul 13 2022 05:41:27 by 1.7.2