Simple USBHost MSD(USB flash drive) for EA LPC4088 QSB test program
Dependencies: LPC4088-USBHost mbed
EA LPC4088をUSBホストにしてUSBフラッシュメモリ(USB flash drive)を読み書きするテストプログラムです。
https://bitbucket.org/va009039/lpc4088_usbhost
LPC4088-USBHost/USBHost/BaseUsbHostLib.cpp@0:11152e69fc05, 2014-04-22 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:11152e69fc05 | 1 | // BaseUsbHostLib.cpp 2014/4/22 |
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 | //#define DBG_USE_POSIX_MEMALIGN |
va009039 | 0:11152e69fc05 | 9 | |
va009039 | 0:11152e69fc05 | 10 | #ifdef DBG_USE_POSIX_MEMALIGN |
va009039 | 0:11152e69fc05 | 11 | void* usb_ram_malloc(size_t size, int aligment) |
va009039 | 0:11152e69fc05 | 12 | { |
va009039 | 0:11152e69fc05 | 13 | TEST_ASSERT(aligment >= 4); |
va009039 | 0:11152e69fc05 | 14 | TEST_ASSERT(!(aligment & 3)); |
va009039 | 0:11152e69fc05 | 15 | void* p; |
va009039 | 0:11152e69fc05 | 16 | if (posix_memalign(&p, aligment, size) == 0) { |
va009039 | 0:11152e69fc05 | 17 | return p; |
va009039 | 0:11152e69fc05 | 18 | } |
va009039 | 0:11152e69fc05 | 19 | return NULL; |
va009039 | 0:11152e69fc05 | 20 | } |
va009039 | 0:11152e69fc05 | 21 | |
va009039 | 0:11152e69fc05 | 22 | void usb_ram_free(void* p) |
va009039 | 0:11152e69fc05 | 23 | { |
va009039 | 0:11152e69fc05 | 24 | free(p); |
va009039 | 0:11152e69fc05 | 25 | } |
va009039 | 0:11152e69fc05 | 26 | #else |
va009039 | 0:11152e69fc05 | 27 | |
va009039 | 0:11152e69fc05 | 28 | #define CHUNK_SIZE 64 |
va009039 | 0:11152e69fc05 | 29 | |
va009039 | 0:11152e69fc05 | 30 | #if defined(TARGET_LPC1768) |
va009039 | 0:11152e69fc05 | 31 | #define USB_RAM_BASE 0x2007C000 |
va009039 | 0:11152e69fc05 | 32 | #define USB_RAM_SIZE 0x4000 |
va009039 | 0:11152e69fc05 | 33 | #define BLOCK_COUNT (USB_RAM_SIZE/CHUNK_SIZE) |
va009039 | 0:11152e69fc05 | 34 | #elif defined(TARGET_LPC4088) |
va009039 | 0:11152e69fc05 | 35 | #define USB_RAM_BASE 0x20000000 |
va009039 | 0:11152e69fc05 | 36 | #define USB_RAM_SIZE 0x4000 |
va009039 | 0:11152e69fc05 | 37 | #define BLOCK_COUNT (USB_RAM_SIZE/CHUNK_SIZE) |
va009039 | 0:11152e69fc05 | 38 | #else |
va009039 | 0:11152e69fc05 | 39 | #error "target error" |
va009039 | 0:11152e69fc05 | 40 | #endif |
va009039 | 0:11152e69fc05 | 41 | |
va009039 | 0:11152e69fc05 | 42 | static uint8_t* ram = NULL; |
va009039 | 0:11152e69fc05 | 43 | static uint8_t* map; |
va009039 | 0:11152e69fc05 | 44 | |
va009039 | 0:11152e69fc05 | 45 | static void usb_ram_init() |
va009039 | 0:11152e69fc05 | 46 | { |
va009039 | 0:11152e69fc05 | 47 | USB_INFO("USB_RAM: 0x%p(%d)", USB_RAM_BASE, USB_RAM_SIZE); |
va009039 | 0:11152e69fc05 | 48 | ram = (uint8_t*)USB_RAM_BASE; |
va009039 | 0:11152e69fc05 | 49 | TEST_ASSERT((int)ram%256 == 0); |
va009039 | 0:11152e69fc05 | 50 | map = (uint8_t*)malloc(BLOCK_COUNT); |
va009039 | 0:11152e69fc05 | 51 | TEST_ASSERT(map); |
va009039 | 0:11152e69fc05 | 52 | memset(map, 0, BLOCK_COUNT); |
va009039 | 0:11152e69fc05 | 53 | } |
va009039 | 0:11152e69fc05 | 54 | |
va009039 | 0:11152e69fc05 | 55 | // first fit malloc |
va009039 | 0:11152e69fc05 | 56 | void* usb_ram_malloc(size_t size, int aligment) |
va009039 | 0:11152e69fc05 | 57 | { |
va009039 | 0:11152e69fc05 | 58 | TEST_ASSERT(aligment >= 4); |
va009039 | 0:11152e69fc05 | 59 | TEST_ASSERT(!(aligment & 3)); |
va009039 | 0:11152e69fc05 | 60 | if (ram == NULL) { |
va009039 | 0:11152e69fc05 | 61 | usb_ram_init(); |
va009039 | 0:11152e69fc05 | 62 | } |
va009039 | 0:11152e69fc05 | 63 | int needs = (size+CHUNK_SIZE-1)/CHUNK_SIZE; |
va009039 | 0:11152e69fc05 | 64 | void* p = NULL; |
va009039 | 0:11152e69fc05 | 65 | for(int idx = 0; idx < BLOCK_COUNT;) { |
va009039 | 0:11152e69fc05 | 66 | bool found = true; |
va009039 | 0:11152e69fc05 | 67 | for(int i = 0; i < needs; i++) { |
va009039 | 0:11152e69fc05 | 68 | int block = map[idx + i]; |
va009039 | 0:11152e69fc05 | 69 | if (block != 0) { |
va009039 | 0:11152e69fc05 | 70 | idx += block; |
va009039 | 0:11152e69fc05 | 71 | found = false; |
va009039 | 0:11152e69fc05 | 72 | break; |
va009039 | 0:11152e69fc05 | 73 | } |
va009039 | 0:11152e69fc05 | 74 | } |
va009039 | 0:11152e69fc05 | 75 | if (!found) { |
va009039 | 0:11152e69fc05 | 76 | continue; |
va009039 | 0:11152e69fc05 | 77 | } |
va009039 | 0:11152e69fc05 | 78 | p = ram+idx*CHUNK_SIZE; |
va009039 | 0:11152e69fc05 | 79 | if ((int)p % aligment) { |
va009039 | 0:11152e69fc05 | 80 | idx++; |
va009039 | 0:11152e69fc05 | 81 | continue; |
va009039 | 0:11152e69fc05 | 82 | } |
va009039 | 0:11152e69fc05 | 83 | TEST_ASSERT((idx + needs) <= BLOCK_COUNT); |
va009039 | 0:11152e69fc05 | 84 | for(int i = 0; i < needs; i++) { |
va009039 | 0:11152e69fc05 | 85 | map[idx + i] = needs - i; |
va009039 | 0:11152e69fc05 | 86 | } |
va009039 | 0:11152e69fc05 | 87 | break; |
va009039 | 0:11152e69fc05 | 88 | } |
va009039 | 0:11152e69fc05 | 89 | TEST_ASSERT(p); |
va009039 | 0:11152e69fc05 | 90 | return p; |
va009039 | 0:11152e69fc05 | 91 | } |
va009039 | 0:11152e69fc05 | 92 | |
va009039 | 0:11152e69fc05 | 93 | void usb_ram_free(void* p) |
va009039 | 0:11152e69fc05 | 94 | { |
va009039 | 0:11152e69fc05 | 95 | TEST_ASSERT(p >= ram); |
va009039 | 0:11152e69fc05 | 96 | TEST_ASSERT(p < (ram+CHUNK_SIZE*BLOCK_COUNT)); |
va009039 | 0:11152e69fc05 | 97 | int idx = ((int)p-(int)ram)/CHUNK_SIZE; |
va009039 | 0:11152e69fc05 | 98 | int block = map[idx]; |
va009039 | 0:11152e69fc05 | 99 | TEST_ASSERT(block >= 1); |
va009039 | 0:11152e69fc05 | 100 | for(int i =0; i < block; i++) { |
va009039 | 0:11152e69fc05 | 101 | map[idx + i] = 0; |
va009039 | 0:11152e69fc05 | 102 | } |
va009039 | 0:11152e69fc05 | 103 | } |
va009039 | 0:11152e69fc05 | 104 | |
va009039 | 0:11152e69fc05 | 105 | #endif // DBG_USE_POSIX_MEMALIGN |
va009039 | 0:11152e69fc05 | 106 | |
va009039 | 0:11152e69fc05 | 107 |