5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #if defined(SIMU_DISKIO)
30 FATFS g_FATFS_Obj
= {0};
32 RTOS_MUTEX_HANDLE ioMutex
;
34 int ff_cre_syncobj (BYTE vol
, _SYNC_t
* sobj
) /* Create a sync object */
36 pthread_mutex_init(&ioMutex
, 0);
40 int ff_req_grant (_SYNC_t sobj
) /* Lock sync object */
42 pthread_mutex_lock(&ioMutex
);
46 void ff_rel_grant (_SYNC_t sobj
) /* Unlock sync object */
48 pthread_mutex_unlock(&ioMutex
);
51 int ff_del_syncobj (_SYNC_t sobj
) /* Delete a sync object */
53 pthread_mutex_destroy(&ioMutex
);
57 DWORD
get_fattime (void)
60 const struct tm
* t
= gmtime(&tim
);
62 /* Pack date and time into a DWORD variable */
63 return ((DWORD
)(t
->tm_year
- 80) << 25)
64 | ((uint32_t)(t
->tm_mon
+1) << 21)
65 | ((uint32_t)t
->tm_mday
<< 16)
66 | ((uint32_t)t
->tm_hour
<< 11)
67 | ((uint32_t)t
->tm_min
<< 5)
68 | ((uint32_t)t
->tm_sec
>> 1);
71 unsigned int noDiskStatus
= 0;
73 void traceDiskStatus()
75 if (noDiskStatus
> 0) {
76 TRACE_SIMPGMSPACE("disk_status() called %d times", noDiskStatus
);
81 DSTATUS
disk_initialize (BYTE pdrv
)
84 TRACE_SIMPGMSPACE("disk_initialize(%u)", pdrv
);
85 diskImage
= fopen("sdcard.image", "rb+");
86 return diskImage
? (DSTATUS
)0 : (DSTATUS
)STA_NODISK
;
89 DSTATUS
disk_status (BYTE pdrv
)
92 // TRACE_SIMPGMSPACE("disk_status(%u)", pdrv);
96 DRESULT
__disk_read (BYTE pdrv
, BYTE
* buff
, DWORD sector
, UINT count
)
98 if (diskImage
== 0) return RES_NOTRDY
;
100 TRACE_SIMPGMSPACE("disk_read(%u, %p, %u, %u)", pdrv
, buff
, sector
, count
);
101 fseek(diskImage
, sector
*512, SEEK_SET
);
102 fread(buff
, count
, 512, diskImage
);
106 DRESULT
__disk_write (BYTE pdrv
, const BYTE
* buff
, DWORD sector
, UINT count
)
108 if (diskImage
== 0) return RES_NOTRDY
;
110 TRACE_SIMPGMSPACE("disk_write(%u, %p, %u, %u)", pdrv
, buff
, sector
, count
);
111 fseek(diskImage
, sector
*512, SEEK_SET
);
112 fwrite(buff
, count
, 512, diskImage
);
116 DRESULT
disk_ioctl (BYTE pdrv
, BYTE cmd
, void* buff
)
118 if (diskImage
== 0) return RES_NOTRDY
;
120 TRACE_SIMPGMSPACE("disk_ioctl(%u, %u, %p)", pdrv
, cmd
, buff
);
121 if (pdrv
) return RES_PARERR
;
124 BYTE
*ptr
= (BYTE
*)buff
;
126 if (cmd
== CTRL_POWER
) {
128 case 0: /* Sub control code == 0 (POWER_OFF) */
131 case 1: /* Sub control code == 1 (POWER_ON) */
134 case 2: /* Sub control code == 2 (POWER_GET) */
135 *(ptr
+1) = (BYTE
)1; /* fake powered */
145 /* Generic command (Used by FatFs) */
146 case CTRL_SYNC
: /* Complete pending write process (needed at _FS_READONLY == 0) */
149 case GET_SECTOR_COUNT
: /* Get media size (needed at _USE_MKFS == 1) */
152 if (stat("sdcard.image", &buf
) == 0) {
153 DWORD noSectors
= buf
.st_size
/ 512;
154 *(DWORD
*)buff
= noSectors
;
155 TRACE_SIMPGMSPACE("disk_ioctl(GET_SECTOR_COUNT) = %u", noSectors
);
161 case GET_SECTOR_SIZE
: /* Get sector size (needed at _MAX_SS != _MIN_SS) */
162 TRACE_SIMPGMSPACE("disk_ioctl(GET_SECTOR_SIZE) = 512");
167 case GET_BLOCK_SIZE
: /* Get erase block size (needed at _USE_MKFS == 1) */
168 *(WORD
*)buff
= 512 * 4;
172 case CTRL_TRIM
: /* Inform device that the data on the block of sectors is no longer used (needed at _USE_TRIM == 1) */
175 /* Generic command (Not used by FatFs) */
176 case CTRL_LOCK
: /* Lock/Unlock media removal */
177 case CTRL_EJECT
: /* Eject media */
178 case CTRL_FORMAT
: /* Create physical format on the media */
182 /* MMC/SDC specific ioctl command */
183 // case MMC_GET_TYPE 10 /* Get card type */
184 // case MMC_GET_CSD 11 /* Get CSD */
185 // case MMC_GET_CID 12 /* Get CID */
186 // case MMC_GET_OCR 13 /* Get OCR */
187 // case MMC_GET_SDSTAT 14 /* Get SD status */
189 /* ATA/CF specific ioctl command */
190 // case ATA_GET_REV 20 /* Get F/W revision */
191 // case ATA_GET_MODEL 21 /* Get model name */
192 // case ATA_GET_SN 22 /* Get serial number */
201 // ioMutex = CoCreateMutex();
202 // if (ioMutex >= CFG_MAX_MUTEX ) {
207 if (f_mount(&g_FATFS_Obj
, "", 1) == FR_OK
) {
208 // call sdGetFreeSectors() now because f_getfree() takes a long time first time it's called
211 #if defined(LOG_TELEMETRY)
212 f_open(&g_telemetryFile
, LOGS_PATH
"/telemetry.log", FA_OPEN_ALWAYS
| FA_WRITE
);
213 if (f_size(&g_telemetryFile
) > 0) {
214 f_lseek(&g_telemetryFile
, f_size(&g_telemetryFile
)); // append
219 TRACE_SIMPGMSPACE("f_mount() failed");
227 #if defined(LOG_TELEMETRY)
228 f_close(&g_telemetryFile
);
230 f_mount(NULL
, "", 0); // unmount SD
236 return g_FATFS_Obj
.fs_type
!= 0;
241 return sdGetSize() > 2000000;
244 uint32_t sdGetSpeed()
249 #endif // #if defined(SIMU_DISKIO)