Various fixes around Companion trainer mode (#7116)
[opentx.git] / radio / src / targets / simu / simudisk.cpp
blob5e8dfcd827cb6d32a2c14f7e17e10fe5916a16e8
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
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)
22 #include "opentx.h"
23 #include "ff.h"
24 #include "diskio.h"
25 #include <time.h>
26 #include <stdio.h>
27 #include <sys/stat.h>
29 FILE * diskImage = 0;
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);
37 return 1;
40 int ff_req_grant (_SYNC_t sobj) /* Lock sync object */
42 pthread_mutex_lock(&ioMutex);
43 return 1;
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);
54 return 1;
57 DWORD get_fattime (void)
59 time_t tim = time(0);
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);
77 noDiskStatus = 0;
81 DSTATUS disk_initialize (BYTE pdrv)
83 traceDiskStatus();
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)
91 ++noDiskStatus;
92 // TRACE_SIMPGMSPACE("disk_status(%u)", pdrv);
93 return (DSTATUS)0;
96 DRESULT __disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count)
98 if (diskImage == 0) return RES_NOTRDY;
99 traceDiskStatus();
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);
103 return RES_OK;
106 DRESULT __disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count)
108 if (diskImage == 0) return RES_NOTRDY;
109 traceDiskStatus();
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);
113 return RES_OK;
116 DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff)
118 if (diskImage == 0) return RES_NOTRDY;
119 traceDiskStatus();
120 TRACE_SIMPGMSPACE("disk_ioctl(%u, %u, %p)", pdrv, cmd, buff);
121 if (pdrv) return RES_PARERR;
123 DRESULT res;
124 BYTE *ptr = (BYTE *)buff;
126 if (cmd == CTRL_POWER) {
127 switch (*ptr) {
128 case 0: /* Sub control code == 0 (POWER_OFF) */
129 res = RES_OK;
130 break;
131 case 1: /* Sub control code == 1 (POWER_ON) */
132 res = RES_OK;
133 break;
134 case 2: /* Sub control code == 2 (POWER_GET) */
135 *(ptr+1) = (BYTE)1; /* fake powered */
136 res = RES_OK;
137 break;
138 default :
139 res = RES_PARERR;
141 return res;
144 switch(cmd) {
145 /* Generic command (Used by FatFs) */
146 case CTRL_SYNC : /* Complete pending write process (needed at _FS_READONLY == 0) */
147 break;
149 case GET_SECTOR_COUNT: /* Get media size (needed at _USE_MKFS == 1) */
151 struct stat buf;
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);
156 return RES_OK;
158 return RES_ERROR;
161 case GET_SECTOR_SIZE: /* Get sector size (needed at _MAX_SS != _MIN_SS) */
162 TRACE_SIMPGMSPACE("disk_ioctl(GET_SECTOR_SIZE) = 512");
163 *(WORD*)buff = 512;
164 res = RES_OK;
165 break;
167 case GET_BLOCK_SIZE : /* Get erase block size (needed at _USE_MKFS == 1) */
168 *(WORD*)buff = 512 * 4;
169 res = RES_OK;
170 break;
172 case CTRL_TRIM : /* Inform device that the data on the block of sectors is no longer used (needed at _USE_TRIM == 1) */
173 break;
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 */
179 return RES_PARERR;
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 */
193 default:
194 return RES_PARERR;
196 return RES_OK;
199 void sdInit(void)
201 // ioMutex = CoCreateMutex();
202 // if (ioMutex >= CFG_MAX_MUTEX ) {
203 // // sd error
204 // return;
205 // }
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
209 sdGetFreeSectors();
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
216 #endif
218 else {
219 TRACE_SIMPGMSPACE("f_mount() failed");
223 void sdDone()
225 if (sdMounted()) {
226 audioQueue.stopSD();
227 #if defined(LOG_TELEMETRY)
228 f_close(&g_telemetryFile);
229 #endif
230 f_mount(NULL, "", 0); // unmount SD
234 uint32_t sdMounted()
236 return g_FATFS_Obj.fs_type != 0;
239 uint32_t sdIsHC()
241 return sdGetSize() > 2000000;
244 uint32_t sdGetSpeed()
246 return 330000;
249 #endif // #if defined(SIMU_DISKIO)