Simple USBHost WebCam for EA LPC4088 QSB/LPC1768 test program
Dependencies: LPC4088-USBHost mbed
EA LPC4088 QSB/LPC1768をUSBホストにしてWebカメラからJPEG画像を読み取るテストプログラムです。
The usage is the same as KL46Z-USBHostC270_example.
使い方はKL46Z-USBHostC270_exampleと同じです。
動作確認: Logitech C270,Logitech Q200R(Qcam Orbit AF)
LPC4088-USBHost/USBHost/BaseUsbHostLib.cpp@0:c972ee42b455, 2014-04-24 (annotated)
- Committer:
- va009039
- Date:
- Thu Apr 24 05:38:45 2014 +0000
- Revision:
- 0:c972ee42b455
first commit,sync rev.29.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:c972ee42b455 | 1 | // BaseUsbHostLib.cpp 2014/4/22 |
va009039 | 0:c972ee42b455 | 2 | #include "USBHost.h" |
va009039 | 0:c972ee42b455 | 3 | //#define DEBUG |
va009039 | 0:c972ee42b455 | 4 | #include "BaseUsbHostDebug.h" |
va009039 | 0:c972ee42b455 | 5 | #define TEST |
va009039 | 0:c972ee42b455 | 6 | #include "BaseUsbHostTest.h" |
va009039 | 0:c972ee42b455 | 7 | |
va009039 | 0:c972ee42b455 | 8 | //#define DBG_USE_POSIX_MEMALIGN |
va009039 | 0:c972ee42b455 | 9 | |
va009039 | 0:c972ee42b455 | 10 | #ifdef DBG_USE_POSIX_MEMALIGN |
va009039 | 0:c972ee42b455 | 11 | void* usb_ram_malloc(size_t size, int aligment) |
va009039 | 0:c972ee42b455 | 12 | { |
va009039 | 0:c972ee42b455 | 13 | TEST_ASSERT(aligment >= 4); |
va009039 | 0:c972ee42b455 | 14 | TEST_ASSERT(!(aligment & 3)); |
va009039 | 0:c972ee42b455 | 15 | void* p; |
va009039 | 0:c972ee42b455 | 16 | if (posix_memalign(&p, aligment, size) == 0) { |
va009039 | 0:c972ee42b455 | 17 | return p; |
va009039 | 0:c972ee42b455 | 18 | } |
va009039 | 0:c972ee42b455 | 19 | return NULL; |
va009039 | 0:c972ee42b455 | 20 | } |
va009039 | 0:c972ee42b455 | 21 | |
va009039 | 0:c972ee42b455 | 22 | void usb_ram_free(void* p) |
va009039 | 0:c972ee42b455 | 23 | { |
va009039 | 0:c972ee42b455 | 24 | free(p); |
va009039 | 0:c972ee42b455 | 25 | } |
va009039 | 0:c972ee42b455 | 26 | #else |
va009039 | 0:c972ee42b455 | 27 | |
va009039 | 0:c972ee42b455 | 28 | #define CHUNK_SIZE 64 |
va009039 | 0:c972ee42b455 | 29 | |
va009039 | 0:c972ee42b455 | 30 | #if defined(TARGET_LPC1768) |
va009039 | 0:c972ee42b455 | 31 | #define USB_RAM_BASE 0x2007C000 |
va009039 | 0:c972ee42b455 | 32 | #define USB_RAM_SIZE 0x4000 |
va009039 | 0:c972ee42b455 | 33 | #define BLOCK_COUNT (USB_RAM_SIZE/CHUNK_SIZE) |
va009039 | 0:c972ee42b455 | 34 | #elif defined(TARGET_LPC4088) |
va009039 | 0:c972ee42b455 | 35 | #define USB_RAM_BASE 0x20000000 |
va009039 | 0:c972ee42b455 | 36 | #define USB_RAM_SIZE 0x4000 |
va009039 | 0:c972ee42b455 | 37 | #define BLOCK_COUNT (USB_RAM_SIZE/CHUNK_SIZE) |
va009039 | 0:c972ee42b455 | 38 | #else |
va009039 | 0:c972ee42b455 | 39 | #error "target error" |
va009039 | 0:c972ee42b455 | 40 | #endif |
va009039 | 0:c972ee42b455 | 41 | |
va009039 | 0:c972ee42b455 | 42 | static uint8_t* ram = NULL; |
va009039 | 0:c972ee42b455 | 43 | static uint8_t* map; |
va009039 | 0:c972ee42b455 | 44 | |
va009039 | 0:c972ee42b455 | 45 | static void usb_ram_init() |
va009039 | 0:c972ee42b455 | 46 | { |
va009039 | 0:c972ee42b455 | 47 | USB_INFO("USB_RAM: 0x%p(%d)", USB_RAM_BASE, USB_RAM_SIZE); |
va009039 | 0:c972ee42b455 | 48 | ram = (uint8_t*)USB_RAM_BASE; |
va009039 | 0:c972ee42b455 | 49 | TEST_ASSERT((int)ram%256 == 0); |
va009039 | 0:c972ee42b455 | 50 | map = (uint8_t*)malloc(BLOCK_COUNT); |
va009039 | 0:c972ee42b455 | 51 | TEST_ASSERT(map); |
va009039 | 0:c972ee42b455 | 52 | memset(map, 0, BLOCK_COUNT); |
va009039 | 0:c972ee42b455 | 53 | } |
va009039 | 0:c972ee42b455 | 54 | |
va009039 | 0:c972ee42b455 | 55 | // first fit malloc |
va009039 | 0:c972ee42b455 | 56 | void* usb_ram_malloc(size_t size, int aligment) |
va009039 | 0:c972ee42b455 | 57 | { |
va009039 | 0:c972ee42b455 | 58 | TEST_ASSERT(aligment >= 4); |
va009039 | 0:c972ee42b455 | 59 | TEST_ASSERT(!(aligment & 3)); |
va009039 | 0:c972ee42b455 | 60 | if (ram == NULL) { |
va009039 | 0:c972ee42b455 | 61 | usb_ram_init(); |
va009039 | 0:c972ee42b455 | 62 | } |
va009039 | 0:c972ee42b455 | 63 | int needs = (size+CHUNK_SIZE-1)/CHUNK_SIZE; |
va009039 | 0:c972ee42b455 | 64 | void* p = NULL; |
va009039 | 0:c972ee42b455 | 65 | for(int idx = 0; idx < BLOCK_COUNT;) { |
va009039 | 0:c972ee42b455 | 66 | bool found = true; |
va009039 | 0:c972ee42b455 | 67 | for(int i = 0; i < needs; i++) { |
va009039 | 0:c972ee42b455 | 68 | int block = map[idx + i]; |
va009039 | 0:c972ee42b455 | 69 | if (block != 0) { |
va009039 | 0:c972ee42b455 | 70 | idx += block; |
va009039 | 0:c972ee42b455 | 71 | found = false; |
va009039 | 0:c972ee42b455 | 72 | break; |
va009039 | 0:c972ee42b455 | 73 | } |
va009039 | 0:c972ee42b455 | 74 | } |
va009039 | 0:c972ee42b455 | 75 | if (!found) { |
va009039 | 0:c972ee42b455 | 76 | continue; |
va009039 | 0:c972ee42b455 | 77 | } |
va009039 | 0:c972ee42b455 | 78 | p = ram+idx*CHUNK_SIZE; |
va009039 | 0:c972ee42b455 | 79 | if ((int)p % aligment) { |
va009039 | 0:c972ee42b455 | 80 | idx++; |
va009039 | 0:c972ee42b455 | 81 | continue; |
va009039 | 0:c972ee42b455 | 82 | } |
va009039 | 0:c972ee42b455 | 83 | TEST_ASSERT((idx + needs) <= BLOCK_COUNT); |
va009039 | 0:c972ee42b455 | 84 | for(int i = 0; i < needs; i++) { |
va009039 | 0:c972ee42b455 | 85 | map[idx + i] = needs - i; |
va009039 | 0:c972ee42b455 | 86 | } |
va009039 | 0:c972ee42b455 | 87 | break; |
va009039 | 0:c972ee42b455 | 88 | } |
va009039 | 0:c972ee42b455 | 89 | TEST_ASSERT(p); |
va009039 | 0:c972ee42b455 | 90 | return p; |
va009039 | 0:c972ee42b455 | 91 | } |
va009039 | 0:c972ee42b455 | 92 | |
va009039 | 0:c972ee42b455 | 93 | void usb_ram_free(void* p) |
va009039 | 0:c972ee42b455 | 94 | { |
va009039 | 0:c972ee42b455 | 95 | TEST_ASSERT(p >= ram); |
va009039 | 0:c972ee42b455 | 96 | TEST_ASSERT(p < (ram+CHUNK_SIZE*BLOCK_COUNT)); |
va009039 | 0:c972ee42b455 | 97 | int idx = ((int)p-(int)ram)/CHUNK_SIZE; |
va009039 | 0:c972ee42b455 | 98 | int block = map[idx]; |
va009039 | 0:c972ee42b455 | 99 | TEST_ASSERT(block >= 1); |
va009039 | 0:c972ee42b455 | 100 | for(int i =0; i < block; i++) { |
va009039 | 0:c972ee42b455 | 101 | map[idx + i] = 0; |
va009039 | 0:c972ee42b455 | 102 | } |
va009039 | 0:c972ee42b455 | 103 | } |
va009039 | 0:c972ee42b455 | 104 | |
va009039 | 0:c972ee42b455 | 105 | #endif // DBG_USE_POSIX_MEMALIGN |
va009039 | 0:c972ee42b455 | 106 | |
va009039 | 0:c972ee42b455 | 107 |