|
| 1 | +typedef struct global global_t; |
| 2 | +#define usb_callback_data_t global_t |
| 3 | + |
| 4 | +#include <usbdrvce.h> |
| 5 | +#include <msddrvce.h> |
| 6 | +#include <tice.h> |
| 7 | + |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdbool.h> |
| 10 | +#include <stddef.h> |
| 11 | +#include <stdlib.h> |
| 12 | +#include <string.h> |
| 13 | + |
| 14 | +#define MAX_PARTITIONS 255 |
| 15 | + |
| 16 | +enum { USB_RETRY_INIT = USB_USER_ERROR }; |
| 17 | + |
| 18 | +struct global |
| 19 | +{ |
| 20 | + usb_device_t usb; |
| 21 | + msd_t msd; |
| 22 | +}; |
| 23 | + |
| 24 | +static usb_error_t handleUsbEvent(usb_event_t event, void *event_data, |
| 25 | + usb_callback_data_t *global) |
| 26 | +{ |
| 27 | + switch (event) |
| 28 | + { |
| 29 | + case USB_DEVICE_DISCONNECTED_EVENT: |
| 30 | + puts("usb device disconnected"); |
| 31 | + if (global->usb) |
| 32 | + msd_Close(&global->msd); |
| 33 | + global->usb = NULL; |
| 34 | + break; |
| 35 | + case USB_DEVICE_CONNECTED_EVENT: |
| 36 | + puts("usb device connected"); |
| 37 | + return usb_ResetDevice(event_data); |
| 38 | + case USB_DEVICE_ENABLED_EVENT: |
| 39 | + puts("usb device enabled"); |
| 40 | + global->usb = event_data; |
| 41 | + break; |
| 42 | + case USB_DEVICE_DISABLED_EVENT: |
| 43 | + puts("usb device disabled"); |
| 44 | + return USB_RETRY_INIT; |
| 45 | + default: |
| 46 | + break; |
| 47 | + } |
| 48 | + |
| 49 | + return USB_SUCCESS; |
| 50 | +} |
| 51 | + |
| 52 | +int main(void) |
| 53 | +{ |
| 54 | + static msd_partition_t partitions[MAX_PARTITIONS]; |
| 55 | + static global_t global; |
| 56 | + uint8_t num_partitions; |
| 57 | + msd_info_t msdinfo; |
| 58 | + usb_error_t usberr; |
| 59 | + msd_error_t msderr; |
| 60 | + |
| 61 | + memset(&global, 0, sizeof(global_t)); |
| 62 | + os_SetCursorPos(1, 0); |
| 63 | + |
| 64 | + // usb initialization loop; waits for something to be plugged in |
| 65 | + do |
| 66 | + { |
| 67 | + global.usb = NULL; |
| 68 | + |
| 69 | + usberr = usb_Init(handleUsbEvent, &global, NULL, USB_DEFAULT_INIT_FLAGS); |
| 70 | + if (usberr != USB_SUCCESS) |
| 71 | + { |
| 72 | + puts("usb init error."); |
| 73 | + goto error; |
| 74 | + } |
| 75 | + |
| 76 | + while (usberr == USB_SUCCESS) |
| 77 | + { |
| 78 | + if (global.usb != NULL) |
| 79 | + break; |
| 80 | + |
| 81 | + // break out if a key is pressed |
| 82 | + if (os_GetCSC()) |
| 83 | + { |
| 84 | + puts("exiting demo, press a key"); |
| 85 | + goto error; |
| 86 | + } |
| 87 | + |
| 88 | + usberr = usb_WaitForInterrupt(); |
| 89 | + } |
| 90 | + } while (usberr == USB_RETRY_INIT); |
| 91 | + |
| 92 | + if (usberr != USB_SUCCESS) |
| 93 | + { |
| 94 | + puts("usb enable error."); |
| 95 | + goto error; |
| 96 | + } |
| 97 | + |
| 98 | + // initialize the msd device |
| 99 | + msderr = msd_Open(&global.msd, global.usb); |
| 100 | + if (msderr != MSD_SUCCESS) |
| 101 | + { |
| 102 | + puts("failed opening msd"); |
| 103 | + goto error; |
| 104 | + } |
| 105 | + |
| 106 | + puts("detected drive"); |
| 107 | + |
| 108 | + // get block count and size |
| 109 | + msderr = msd_Info(&global.msd, &msdinfo); |
| 110 | + if (msderr != MSD_SUCCESS) |
| 111 | + { |
| 112 | + puts("error getting msd info"); |
| 113 | + msd_Close(&global.msd); |
| 114 | + goto error; |
| 115 | + } |
| 116 | + |
| 117 | + // print msd sector number and size |
| 118 | + printf("block size: %u bytes\n", (uint24_t)msdinfo.bsize); |
| 119 | + printf("num blocks: %u\n", (uint24_t)msdinfo.bnum); |
| 120 | + |
| 121 | + num_partitions = msd_FindPartitions(&global.msd, partitions, MAX_PARTITIONS); |
| 122 | + |
| 123 | + printf("num paritions: %u\n", num_partitions); |
| 124 | + |
| 125 | + for (uint8_t i = 0; i < num_partitions; ++i) |
| 126 | + { |
| 127 | + uint32_t first_lba = partitions[i].first_lba; |
| 128 | + uint32_t last_lba = partitions[i].last_lba; |
| 129 | + |
| 130 | + printf("%lu - %lu\n", first_lba, last_lba); |
| 131 | + } |
| 132 | + |
| 133 | + // close the msd device |
| 134 | + msd_Close(&global.msd); |
| 135 | + |
| 136 | + // cleanup and return |
| 137 | + usb_Cleanup(); |
| 138 | + os_GetKey(); |
| 139 | + return 0; |
| 140 | + |
| 141 | +error: |
| 142 | + usb_Cleanup(); |
| 143 | + os_GetKey(); |
| 144 | + return -1; |
| 145 | +} |
0 commit comments