Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * The MIT License (MIT) |
| 3 | * |
| 4 | * Copyright (c) 2019 Ha Thach (tinyusb.org) |
| 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 | * This file is part of the TinyUSB stack. |
| 25 | */ |
| 26 | |
| 27 | #include "tusb.h" |
| 28 | |
| 29 | #if CFG_TUH_MSC |
| 30 | //--------------------------------------------------------------------+ |
| 31 | // INCLUDE |
| 32 | //--------------------------------------------------------------------+ |
| 33 | #include "ffconf.h" |
| 34 | #include "diskio.h" |
| 35 | //--------------------------------------------------------------------+ |
| 36 | // MACRO CONSTANT TYPEDEF |
| 37 | //--------------------------------------------------------------------+ |
| 38 | // TODO change it to portable init |
| 39 | static DSTATUS disk_state[CFG_TUH_DEVICE_MAX]; |
| 40 | |
| 41 | //--------------------------------------------------------------------+ |
| 42 | // INTERNAL OBJECT & FUNCTION DECLARATION |
| 43 | //--------------------------------------------------------------------+ |
| 44 | |
| 45 | //--------------------------------------------------------------------+ |
| 46 | // IMPLEMENTATION |
| 47 | //--------------------------------------------------------------------+ |
| 48 | static DRESULT wait_for_io_complete(uint8_t usb_addr) |
| 49 | { |
| 50 | // TODO with RTOS, this should use semaphore instead of blocking |
| 51 | while ( !tuh_msc_ready(usb_addr) ) |
| 52 | { |
| 53 | // TODO should have timeout here |
| 54 | #if CFG_TUSB_OS != OPT_OS_NONE |
| 55 | osal_task_delay(10); |
| 56 | #endif |
| 57 | } |
| 58 | |
| 59 | return RES_OK; |
| 60 | } |
| 61 | |
| 62 | void diskio_init(void) |
| 63 | { |
| 64 | memset(disk_state, STA_NOINIT, CFG_TUH_DEVICE_MAX); |
| 65 | } |
| 66 | |
| 67 | //pdrv Specifies the physical drive number. |
| 68 | DSTATUS disk_initialize ( BYTE pdrv ) |
| 69 | { |
| 70 | disk_state[pdrv] &= (~STA_NOINIT); // clear NOINIT bit |
| 71 | return disk_state[pdrv]; |
| 72 | } |
| 73 | |
| 74 | void disk_deinitialize ( BYTE pdrv ) |
| 75 | { |
| 76 | disk_state[pdrv] |= STA_NOINIT; // set NOINIT bit |
| 77 | } |
| 78 | |
| 79 | DSTATUS disk_status (BYTE pdrv) |
| 80 | { |
| 81 | return disk_state[pdrv]; |
| 82 | } |
| 83 | |
| 84 | //pdrv |
| 85 | // Specifies the physical drive number --> == dev_addr-1 |
| 86 | //buff |
| 87 | // Pointer to the byte array to store the read data. The size of buffer must be in sector size * sector count. |
| 88 | //sector |
| 89 | // Specifies the start sector number in logical block address (LBA). |
| 90 | //count |
| 91 | // Specifies number of sectors to read. The value can be 1 to 128. Generally, a multiple sector transfer request |
| 92 | // must not be split into single sector transactions to the device, or you may not get good read performance. |
| 93 | DRESULT disk_read (BYTE pdrv, BYTE*buff, DWORD sector, BYTE count) |
| 94 | { |
| 95 | uint8_t usb_addr = pdrv+1; |
| 96 | |
| 97 | if ( TUSB_ERROR_NONE != tuh_msc_read10(usb_addr, 0, buff, sector, count) ) return RES_ERROR; |
| 98 | |
| 99 | return wait_for_io_complete(usb_addr); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, BYTE count) |
| 104 | { |
| 105 | uint8_t usb_addr = pdrv+1; |
| 106 | |
| 107 | if ( TUSB_ERROR_NONE != tuh_msc_write10(usb_addr, 0, buff, sector, count) ) return RES_ERROR; |
| 108 | |
| 109 | return wait_for_io_complete(usb_addr); |
| 110 | } |
| 111 | |
| 112 | /* [IN] Drive number */ |
| 113 | /* [IN] Control command code */ |
| 114 | /* [I/O] Parameter and data buffer */ |
| 115 | DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff) |
| 116 | { |
| 117 | (void) buff; (void) pdrv; // compiler warnings |
| 118 | |
| 119 | if (cmd != CTRL_SYNC) return RES_ERROR; |
| 120 | return RES_OK; |
| 121 | } |
| 122 | |
| 123 | static inline uint8_t month2number(char* p_ch) |
| 124 | { |
| 125 | char const * const month_str[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 126 | |
| 127 | for(uint8_t i=0; i<12; i++) |
| 128 | { |
| 129 | if ( strncmp(p_ch, month_str[i], 3) == 0 ) return i+1; |
| 130 | } |
| 131 | |
| 132 | return 1; |
| 133 | } |
| 134 | |
| 135 | static inline uint8_t c2i(char ch) |
| 136 | { |
| 137 | return ch - '0'; |
| 138 | } |
| 139 | |
| 140 | DWORD get_fattime (void) |
| 141 | { |
| 142 | union { |
| 143 | struct { |
| 144 | DWORD second : 5; |
| 145 | DWORD minute : 6; |
| 146 | DWORD hour : 5; |
| 147 | DWORD day_in_month : 5; |
| 148 | DWORD month : 4; |
| 149 | DWORD year : 7; |
| 150 | }; |
| 151 | |
| 152 | DWORD value; |
| 153 | } timestamp; |
| 154 | |
| 155 | //------------- Date is compiled date-------------// |
| 156 | char compile_date[] = __DATE__; // eg. "Sep 26 2013" |
| 157 | char* p_ch; |
| 158 | |
| 159 | p_ch = strtok (compile_date, " "); |
| 160 | timestamp.month = month2number(p_ch); |
| 161 | |
| 162 | p_ch = strtok (NULL, " "); |
| 163 | timestamp.day_in_month = 10*c2i(p_ch[0])+ c2i(p_ch[1]); |
| 164 | |
| 165 | p_ch = strtok (NULL, " "); |
| 166 | timestamp.year = 1000*c2i(p_ch[0]) + 100*c2i(p_ch[1]) + 10*c2i(p_ch[2]) + c2i(p_ch[3]) - 1980; |
| 167 | |
| 168 | //------------- Time each time this function call --> sec ++ -------------// |
| 169 | static uint8_t sec = 0; |
| 170 | static uint8_t min = 0; |
| 171 | static uint8_t hour = 0; |
| 172 | |
| 173 | if (++sec >= 60) |
| 174 | { |
| 175 | sec = 0; |
| 176 | if (++min >= 60) |
| 177 | { |
| 178 | min = 0; |
| 179 | if (++hour >= 24) |
| 180 | { |
| 181 | hour = 0; // assume demo wont call this function more than 24*60*60 times |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | timestamp.hour = hour; |
| 187 | timestamp.minute = min; |
| 188 | timestamp.second = sec; |
| 189 | |
| 190 | return timestamp.value; |
| 191 | } |
| 192 | |
| 193 | #endif // CFG_TUH_MSC |