![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
USB composite device example program, drag-and-drop flash writer.
Dependencies: SWD USBDevice mbed BaseDAP
tests/RamDisk.cpp@1:ea8e179320d7, 2013-09-28 (annotated)
- Committer:
- va009039
- Date:
- Sat Sep 28 03:21:14 2013 +0000
- Revision:
- 1:ea8e179320d7
- Parent:
- 0:2385683c867a
add USBMSD_Drop class. add CDC(Virtual COM) and HID(for example CMSIS-DAP), but KL25Z not work.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 1:ea8e179320d7 | 1 | // RamDisk.cpp 2013/9/21 |
va009039 | 0:2385683c867a | 2 | #include "mbed.h" |
va009039 | 0:2385683c867a | 3 | #include "RamDisk.h" |
va009039 | 1:ea8e179320d7 | 4 | #include "mydebug.h" |
va009039 | 0:2385683c867a | 5 | |
va009039 | 1:ea8e179320d7 | 6 | RamDisk::RamDisk() : _head(NULL), use_size(0) |
va009039 | 0:2385683c867a | 7 | { |
va009039 | 0:2385683c867a | 8 | _sectors = 128; // 64KB(512*128) |
va009039 | 0:2385683c867a | 9 | } |
va009039 | 0:2385683c867a | 10 | |
va009039 | 0:2385683c867a | 11 | int RamDisk::read(uint8_t * data, uint32_t block) |
va009039 | 0:2385683c867a | 12 | { |
va009039 | 1:ea8e179320d7 | 13 | //DBG("block=%d", block); |
va009039 | 0:2385683c867a | 14 | SectorData* p = find(block); |
va009039 | 0:2385683c867a | 15 | if (p) { |
va009039 | 0:2385683c867a | 16 | memcpy(data, p->data, 512); |
va009039 | 0:2385683c867a | 17 | return 0; |
va009039 | 0:2385683c867a | 18 | } |
va009039 | 0:2385683c867a | 19 | memset(data, 0x00, 512); |
va009039 | 0:2385683c867a | 20 | return 0; |
va009039 | 0:2385683c867a | 21 | } |
va009039 | 0:2385683c867a | 22 | |
va009039 | 0:2385683c867a | 23 | static bool is_blank(const uint8_t* data) |
va009039 | 0:2385683c867a | 24 | { |
va009039 | 0:2385683c867a | 25 | for(int i = 0; i < 512; i++) { |
va009039 | 0:2385683c867a | 26 | if (data[i]) { |
va009039 | 0:2385683c867a | 27 | return false; |
va009039 | 0:2385683c867a | 28 | } |
va009039 | 0:2385683c867a | 29 | } |
va009039 | 0:2385683c867a | 30 | return true; |
va009039 | 0:2385683c867a | 31 | } |
va009039 | 0:2385683c867a | 32 | |
va009039 | 0:2385683c867a | 33 | int RamDisk::write(const uint8_t * data, uint32_t block) |
va009039 | 0:2385683c867a | 34 | { |
va009039 | 0:2385683c867a | 35 | SectorData* p = find(block); |
va009039 | 0:2385683c867a | 36 | if (p) { |
va009039 | 1:ea8e179320d7 | 37 | DBG("update block=%d", block); |
va009039 | 0:2385683c867a | 38 | memcpy(p->data, data, 512); |
va009039 | 0:2385683c867a | 39 | return 0; |
va009039 | 0:2385683c867a | 40 | } |
va009039 | 0:2385683c867a | 41 | if (is_blank(data)) { |
va009039 | 0:2385683c867a | 42 | return 0; |
va009039 | 0:2385683c867a | 43 | } |
va009039 | 1:ea8e179320d7 | 44 | DBG("new block=%d", block); |
va009039 | 0:2385683c867a | 45 | p = new SectorData; |
va009039 | 1:ea8e179320d7 | 46 | TEST_ASSERT(p); |
va009039 | 0:2385683c867a | 47 | p->block = block; |
va009039 | 0:2385683c867a | 48 | memcpy(p->data, data, 512); |
va009039 | 0:2385683c867a | 49 | p->next = _head; |
va009039 | 0:2385683c867a | 50 | _head = p; |
va009039 | 1:ea8e179320d7 | 51 | use_size += 512; |
va009039 | 1:ea8e179320d7 | 52 | //DBG("use_size: %d", use_size); |
va009039 | 1:ea8e179320d7 | 53 | TEST_ASSERT(use_size < (512*16)); |
va009039 | 0:2385683c867a | 54 | return 0; |
va009039 | 0:2385683c867a | 55 | } |
va009039 | 0:2385683c867a | 56 | |
va009039 | 0:2385683c867a | 57 | uint32_t RamDisk::sectors() |
va009039 | 0:2385683c867a | 58 | { |
va009039 | 0:2385683c867a | 59 | return _sectors; |
va009039 | 0:2385683c867a | 60 | } |
va009039 | 0:2385683c867a | 61 | |
va009039 | 0:2385683c867a | 62 | SectorData* RamDisk::find(uint16_t block) |
va009039 | 0:2385683c867a | 63 | { |
va009039 | 0:2385683c867a | 64 | SectorData* p = _head; |
va009039 | 0:2385683c867a | 65 | while(p) { |
va009039 | 0:2385683c867a | 66 | if (p->block == block) { |
va009039 | 0:2385683c867a | 67 | break; |
va009039 | 0:2385683c867a | 68 | } |
va009039 | 0:2385683c867a | 69 | p = p->next; |
va009039 | 0:2385683c867a | 70 | } |
va009039 | 0:2385683c867a | 71 | return p; |
va009039 | 0:2385683c867a | 72 | } |
va009039 | 0:2385683c867a | 73 | |
va009039 | 0:2385683c867a | 74 | void RamDisk::exportData(Stream* pc) |
va009039 | 0:2385683c867a | 75 | { |
va009039 | 0:2385683c867a | 76 | pc->printf("\n\n\n\n\n"); |
va009039 | 0:2385683c867a | 77 | SectorData* p = _head; |
va009039 | 0:2385683c867a | 78 | while(p) { |
va009039 | 0:2385683c867a | 79 | pc->printf("const uint8_t sector_%d[] = {\n", p->block); |
va009039 | 0:2385683c867a | 80 | for(int i = 0; i < 512; i++) { |
va009039 | 0:2385683c867a | 81 | pc->printf("0x%02x,", p->data[i]); |
va009039 | 0:2385683c867a | 82 | if (i%16 == 15) { |
va009039 | 0:2385683c867a | 83 | pc->printf("\n"); |
va009039 | 0:2385683c867a | 84 | } |
va009039 | 0:2385683c867a | 85 | } |
va009039 | 0:2385683c867a | 86 | pc->printf("};\n"); |
va009039 | 0:2385683c867a | 87 | p = p->next; |
va009039 | 0:2385683c867a | 88 | } |
va009039 | 0:2385683c867a | 89 | |
va009039 | 0:2385683c867a | 90 | pc->printf("const SectorIndex sector_index[] = {\n"); |
va009039 | 0:2385683c867a | 91 | p = _head; |
va009039 | 0:2385683c867a | 92 | while(p) { |
va009039 | 0:2385683c867a | 93 | pc->printf("{%d, sector_%d},\n", p->block, p->block); |
va009039 | 0:2385683c867a | 94 | p = p->next; |
va009039 | 0:2385683c867a | 95 | } |
va009039 | 0:2385683c867a | 96 | pc->printf("{-1, NULL},\n"); |
va009039 | 0:2385683c867a | 97 | pc->printf("};\n"); |
va009039 | 0:2385683c867a | 98 | pc->printf("\n\n\n\n\n"); |
va009039 | 0:2385683c867a | 99 | } |