|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2020-2021 Damien P. George |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + * |
| 24 | + */ |
| 25 | +#include "tusb.h" |
| 26 | +#if CFG_TUD_MSC |
| 27 | +#include "mpconfigboard.h" |
| 28 | +#include "hardware/flash.h" |
| 29 | +#include "hardware/sync.h" |
| 30 | +#include "pico/stdlib.h" |
| 31 | + |
| 32 | +// This implementation does Not support Flash sector caching. |
| 33 | +#if MICROPY_FATFS_MAX_SS != FLASH_SECTOR_SIZE |
| 34 | +#error MICROPY_FATFS_MAX_SS must be the same size as FLASH_SECTOR_SIZE |
| 35 | +#endif |
| 36 | + |
| 37 | +#define BLOCK_SIZE (FLASH_SECTOR_SIZE) |
| 38 | +#define BLOCK_COUNT (MICROPY_HW_FLASH_STORAGE_BYTES / BLOCK_SIZE) |
| 39 | +#define FLASH_BASE_ADDR (PICO_FLASH_SIZE_BYTES - MICROPY_HW_FLASH_STORAGE_BYTES) |
| 40 | +#define FLASH_MMAP_ADDR (XIP_BASE + FLASH_BASE_ADDR) |
| 41 | + |
| 42 | +static bool ejected = false; |
| 43 | + |
| 44 | +// Invoked when received SCSI_CMD_INQUIRY |
| 45 | +// Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively |
| 46 | +void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) { |
| 47 | + const char vid[] = "Micropy"; |
| 48 | + const char pid[] = "Mass Storage"; |
| 49 | + const char rev[] = "1.0"; |
| 50 | + |
| 51 | + strncpy((char *)vendor_id, vid, 8); |
| 52 | + strncpy((char *)product_id, pid, 16); |
| 53 | + strncpy((char *)product_rev, rev<
10000
/span>, 4); |
| 54 | +} |
| 55 | + |
| 56 | +// Invoked when received Test Unit Ready command. |
| 57 | +// return true allowing host to read/write this LUN e.g SD card inserted |
| 58 | +bool tud_msc_test_unit_ready_cb(uint8_t lun) { |
| 59 | + if (ejected) { |
| 60 | + tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00); |
| 61 | + return false; |
| 62 | + } |
| 63 | + return true; |
| 64 | +} |
| 65 | + |
| 66 | +// Invoked when received SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size |
| 67 | +// Application update block count and block size |
| 68 | +void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size) { |
| 69 | + *block_size = BLOCK_SIZE; |
| 70 | + *block_count = BLOCK_COUNT; |
| 71 | +} |
| 72 | + |
| 73 | +// Invoked when received Start Stop Unit command |
| 74 | +// - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage |
| 75 | +// - Start = 1 : active mode, if load_eject = 1 : load disk storage |
| 76 | +bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) { |
| 77 | + if (load_eject) { |
| 78 | + if (start) { |
| 79 | + // load disk storage |
| 80 | + ejected = false; |
| 81 | + } else { |
| 82 | + // unload disk storage |
| 83 | + ejected = true; |
| 84 | + } |
| 85 | + } |
| 86 | + return true; |
| 87 | +} |
| 88 | + |
| 89 | +// Callback invoked when received READ10 command. |
| 90 | +// Copy disk's data to buffer (up to bufsize) and return number of copied bytes. |
| 91 | +int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize) { |
| 92 | + uint32_t count = bufsize / BLOCK_SIZE; |
| 93 | + memcpy(buffer, (void *)(FLASH_MMAP_ADDR + lba * BLOCK_SIZE), count * BLOCK_SIZE); |
| 94 | + return count * BLOCK_SIZE; |
| 95 | +} |
| 96 | + |
| 97 | +// Callback invoked when received WRITE10 command. |
| 98 | +// Process data in buffer to disk's storage and return number of written bytes |
| 99 | +int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize) { |
| 100 | + uint32_t count = bufsize / BLOCK_SIZE; |
| 101 | + uint32_t ints = save_and_disable_interrupts(); |
| 102 | + flash_range_erase(FLASH_BASE_ADDR + lba * BLOCK_SIZE, count * BLOCK_SIZE); |
| 103 | + flash_range_program(FLASH_BASE_ADDR + lba * BLOCK_SIZE, buffer, count * BLOCK_SIZE); |
| 104 | + restore_interrupts(ints); |
| 105 | + return count * BLOCK_SIZE; |
| 106 | +} |
| 107 | + |
| 108 | +// Callback invoked when received an SCSI command not in built-in list below |
| 109 | +// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE |
| 110 | +// - READ10 and WRITE10 has their own callbacks |
| 111 | +int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, uint16_t bufsize) { |
| 112 | + int32_t resplen = 0; |
| 113 | + switch (scsi_cmd[0]) { |
| 114 | + case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL: |
| 115 | + // Sync the logical unit if needed. |
| 116 | + break; |
| 117 | + |
| 118 | + default: |
| 119 | + // Set Sense = Invalid Command Operation |
| 120 | + tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); |
| 121 | + // negative means error -> tinyusb could stall and/or response with failed status |
| 122 | + resplen = -1; |
| 123 | + break; |
| 124 | + } |
| 125 | + return resplen; |
| 126 | +} |
| 127 | +#endif |
0 commit comments