Added to LPC4088-USBHost the USBHostMidi class. Plugin an usb midi interface to the usb host of lpc4088 allows to send midi event to the midi interface. For the moment I can not be able to get event from the interface by using the attacheNoteOn or other triggers...

Dependencies:   FATFileSystem mbed-rtos

Fork of LPC4088-USBHost by Norimasa Okamoto

Committer:
Grag38
Date:
Mon Apr 06 12:46:58 2015 +0000
Revision:
1:d652de69bd1a
Parent:
0:148fca6fd246
Added USBHostMidi to drive midi interface.; ; Tested to send Midi messages from LPC4088. This works.; ; Need to test with incomming events.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 0:148fca6fd246 1 // BaseUsbHostLib.cpp 2014/4/22
va009039 0:148fca6fd246 2 #include "USBHost.h"
va009039 0:148fca6fd246 3 //#define DEBUG
va009039 0:148fca6fd246 4 #include "BaseUsbHostDebug.h"
va009039 0:148fca6fd246 5 #define TEST
va009039 0:148fca6fd246 6 #include "BaseUsbHostTest.h"
va009039 0:148fca6fd246 7
va009039 0:148fca6fd246 8 //#define DBG_USE_POSIX_MEMALIGN
va009039 0:148fca6fd246 9
va009039 0:148fca6fd246 10 #ifdef DBG_USE_POSIX_MEMALIGN
va009039 0:148fca6fd246 11 void* usb_ram_malloc(size_t size, int aligment)
va009039 0:148fca6fd246 12 {
va009039 0:148fca6fd246 13 TEST_ASSERT(aligment >= 4);
va009039 0:148fca6fd246 14 TEST_ASSERT(!(aligment & 3));
va009039 0:148fca6fd246 15 void* p;
va009039 0:148fca6fd246 16 if (posix_memalign(&p, aligment, size) == 0) {
va009039 0:148fca6fd246 17 return p;
va009039 0:148fca6fd246 18 }
va009039 0:148fca6fd246 19 return NULL;
va009039 0:148fca6fd246 20 }
va009039 0:148fca6fd246 21
va009039 0:148fca6fd246 22 void usb_ram_free(void* p)
va009039 0:148fca6fd246 23 {
va009039 0:148fca6fd246 24 free(p);
va009039 0:148fca6fd246 25 }
va009039 0:148fca6fd246 26 #else
va009039 0:148fca6fd246 27
va009039 0:148fca6fd246 28 #define CHUNK_SIZE 64
va009039 0:148fca6fd246 29
va009039 0:148fca6fd246 30 #if defined(TARGET_LPC1768)
va009039 0:148fca6fd246 31 #define USB_RAM_BASE 0x2007C000
va009039 0:148fca6fd246 32 #define USB_RAM_SIZE 0x4000
va009039 0:148fca6fd246 33 #define BLOCK_COUNT (USB_RAM_SIZE/CHUNK_SIZE)
va009039 0:148fca6fd246 34 #elif defined(TARGET_LPC4088)
va009039 0:148fca6fd246 35 #define USB_RAM_BASE 0x20000000
va009039 0:148fca6fd246 36 #define USB_RAM_SIZE 0x4000
va009039 0:148fca6fd246 37 #define BLOCK_COUNT (USB_RAM_SIZE/CHUNK_SIZE)
va009039 0:148fca6fd246 38 #else
va009039 0:148fca6fd246 39 #error "target error"
va009039 0:148fca6fd246 40 #endif
va009039 0:148fca6fd246 41
va009039 0:148fca6fd246 42 static uint8_t* ram = NULL;
va009039 0:148fca6fd246 43 static uint8_t* map;
va009039 0:148fca6fd246 44
va009039 0:148fca6fd246 45 static void usb_ram_init()
va009039 0:148fca6fd246 46 {
va009039 0:148fca6fd246 47 USB_INFO("USB_RAM: 0x%p(%d)", USB_RAM_BASE, USB_RAM_SIZE);
va009039 0:148fca6fd246 48 ram = (uint8_t*)USB_RAM_BASE;
va009039 0:148fca6fd246 49 TEST_ASSERT((int)ram%256 == 0);
va009039 0:148fca6fd246 50 map = (uint8_t*)malloc(BLOCK_COUNT);
va009039 0:148fca6fd246 51 TEST_ASSERT(map);
va009039 0:148fca6fd246 52 memset(map, 0, BLOCK_COUNT);
va009039 0:148fca6fd246 53 }
va009039 0:148fca6fd246 54
va009039 0:148fca6fd246 55 // first fit malloc
va009039 0:148fca6fd246 56 void* usb_ram_malloc(size_t size, int aligment)
va009039 0:148fca6fd246 57 {
va009039 0:148fca6fd246 58 TEST_ASSERT(aligment >= 4);
va009039 0:148fca6fd246 59 TEST_ASSERT(!(aligment & 3));
va009039 0:148fca6fd246 60 if (ram == NULL) {
va009039 0:148fca6fd246 61 usb_ram_init();
va009039 0:148fca6fd246 62 }
va009039 0:148fca6fd246 63 int needs = (size+CHUNK_SIZE-1)/CHUNK_SIZE;
va009039 0:148fca6fd246 64 void* p = NULL;
va009039 0:148fca6fd246 65 for(int idx = 0; idx < BLOCK_COUNT;) {
va009039 0:148fca6fd246 66 bool found = true;
va009039 0:148fca6fd246 67 for(int i = 0; i < needs; i++) {
va009039 0:148fca6fd246 68 int block = map[idx + i];
va009039 0:148fca6fd246 69 if (block != 0) {
va009039 0:148fca6fd246 70 idx += block;
va009039 0:148fca6fd246 71 found = false;
va009039 0:148fca6fd246 72 break;
va009039 0:148fca6fd246 73 }
va009039 0:148fca6fd246 74 }
va009039 0:148fca6fd246 75 if (!found) {
va009039 0:148fca6fd246 76 continue;
va009039 0:148fca6fd246 77 }
va009039 0:148fca6fd246 78 p = ram+idx*CHUNK_SIZE;
va009039 0:148fca6fd246 79 if ((int)p % aligment) {
va009039 0:148fca6fd246 80 idx++;
va009039 0:148fca6fd246 81 continue;
va009039 0:148fca6fd246 82 }
va009039 0:148fca6fd246 83 TEST_ASSERT((idx + needs) <= BLOCK_COUNT);
va009039 0:148fca6fd246 84 for(int i = 0; i < needs; i++) {
va009039 0:148fca6fd246 85 map[idx + i] = needs - i;
va009039 0:148fca6fd246 86 }
va009039 0:148fca6fd246 87 break;
va009039 0:148fca6fd246 88 }
va009039 0:148fca6fd246 89 TEST_ASSERT(p);
va009039 0:148fca6fd246 90 return p;
va009039 0:148fca6fd246 91 }
va009039 0:148fca6fd246 92
va009039 0:148fca6fd246 93 void usb_ram_free(void* p)
va009039 0:148fca6fd246 94 {
va009039 0:148fca6fd246 95 TEST_ASSERT(p >= ram);
va009039 0:148fca6fd246 96 TEST_ASSERT(p < (ram+CHUNK_SIZE*BLOCK_COUNT));
va009039 0:148fca6fd246 97 int idx = ((int)p-(int)ram)/CHUNK_SIZE;
va009039 0:148fca6fd246 98 int block = map[idx];
va009039 0:148fca6fd246 99 TEST_ASSERT(block >= 1);
va009039 0:148fca6fd246 100 for(int i =0; i < block; i++) {
va009039 0:148fca6fd246 101 map[idx + i] = 0;
va009039 0:148fca6fd246 102 }
va009039 0:148fca6fd246 103 }
va009039 0:148fca6fd246 104
va009039 0:148fca6fd246 105 #endif // DBG_USE_POSIX_MEMALIGN
va009039 0:148fca6fd246 106
va009039 0:148fca6fd246 107