Synch transfer fix
[SCSI2SD-V6.git] / src / firmware / config.c
1 // Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
2 //
3 // This file is part of SCSI2SD.
4 //
5 // SCSI2SD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // SCSI2SD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
17
18 #include "config.h"
19 #include "led.h"
20
21 #include "scsi.h"
22 #include "scsiPhy.h"
23 #include "sd.h"
24 #include "disk.h"
25 #include "trace.h"
26 #include "bootloader.h"
27 #include "bsp.h"
28 #include "spinlock.h"
29
30 #include "../../include/scsi2sd.h"
31 #include "../../include/hidpacket.h"
32
33 #include "usb_device/usb_device.h"
34 #include "usb_device/usbd_hid.h"
35 #include "usb_device/usbd_composite.h"
36 #include "bsp_driver_sd.h"
37
38
39 #include <string.h>
40
41 static const uint16_t FIRMWARE_VERSION = 0x060E;
42
43 // 1 flash row
44 static const uint8_t DEFAULT_CONFIG[128] =
45 {
46 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x3F, 0x00,
47 0x00, 0x02, 0x3F, 0x00, 0xFF, 0x00, 0x20, 0x63, 0x6F, 0x64, 0x65, 0x73,
48 0x72, 0x63, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x53,
49 0x43, 0x53, 0x49, 0x32, 0x53, 0x44, 0x20, 0x31, 0x2E, 0x30, 0x31, 0x32,
50 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
51 0x37, 0x38, 0x00, 0x00
52 };
53
54
55 static uint8_t s2s_cfg[S2S_CFG_SIZE] S2S_DMA_ALIGN;
56 static uint8_t configDmaBuf[512] S2S_DMA_ALIGN; // For SD card writes.
57
58
59 enum USB_STATE
60 {
61 USB_IDLE,
62 USB_DATA_SENT
63 };
64
65
66 static int usbInEpState;
67
68 static void s2s_debugTimer();
69
70 // Debug timer to log via USB.
71 // Timer 6 & 7 is a simple counter with no external IO supported.
72 static s2s_lock_t usbDevLock = s2s_lock_init;
73 TIM_HandleTypeDef htim7;
74 static int debugTimerStarted = 0;
75 void TIM7_IRQHandler()
76 {
77 HAL_TIM_IRQHandler(&htim7);
78 }
79 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
80 {
81 if (s2s_spin_trylock(&usbDevLock)) {
82 s2s_debugTimer();
83 s2s_spin_unlock(&usbDevLock);
84 }
85 }
86
87 void s2s_configInit(S2S_BoardCfg* config)
88 {
89
90 usbInEpState = USB_IDLE;
91
92
93 if ((blockDev.state & DISK_PRESENT) && sdDev.capacity)
94 {
95 int cfgSectors = (S2S_CFG_SIZE + 511) / 512;
96 BSP_SD_ReadBlocks_DMA(
97 (uint32_t*) &s2s_cfg[0],
98 (sdDev.capacity - cfgSectors) * 512ll,
99 512,
100 cfgSectors);
101
102 memcpy(config, s2s_cfg, sizeof(S2S_BoardCfg));
103
104 if (memcmp(config->magic, "BCFG", 4))
105 {
106 // Invalid SD card config, use default.
107 memset(&s2s_cfg[0], 0, S2S_CFG_SIZE);
108 memcpy(config, s2s_cfg, sizeof(S2S_BoardCfg));
109 memcpy(config->magic, "BCFG", 4);
110 config->selectionDelay = 255; // auto
111 config->flags6 = S2S_CFG_ENABLE_TERMINATOR;
112
113 memcpy(
114 &s2s_cfg[0] + sizeof(S2S_BoardCfg),
115 DEFAULT_CONFIG,
116 sizeof(S2S_TargetCfg));
117 }
118 }
119 else
120 {
121 // No SD card, use existing config if valid
122 if (memcmp(config->magic, "BCFG", 4))
123 {
124 // Not valid, use empty config with no disks.
125 memset(&s2s_cfg[0], 0, S2S_CFG_SIZE);
126 memcpy(config, s2s_cfg, sizeof(S2S_BoardCfg));
127 config->selectionDelay = 255; // auto
128 config->flags6 = S2S_CFG_ENABLE_TERMINATOR;
129 }
130 }
131 }
132
133 static void debugInit(void)
134 {
135 if (debugTimerStarted == 1) return;
136
137 debugTimerStarted = 1;
138 // 10ms debug timer to capture logs over USB
139 __TIM7_CLK_ENABLE();
140 htim7.Instance = TIM7;
141 htim7.Init.Prescaler = 10800 - 1; // 16bit. 108MHz down to 10KHz
142 htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
143 htim7.Init.Period = 100 - 1; // 16bit. 10KHz down to 10ms.
144 htim7.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
145 HAL_TIM_Base_Init(&htim7);
146 HAL_TIM_Base_Start_IT(&htim7);
147
148 HAL_NVIC_SetPriority(TIM7_IRQn, 10, 0);
149 HAL_NVIC_EnableIRQ(TIM7_IRQn);
150 }
151
152
153 static void
154 pingCommand()
155 {
156 uint8_t response[] =
157 {
158 S2S_CFG_STATUS_GOOD
159 };
160 hidPacket_send(response, sizeof(response));
161 }
162
163 static void
164 sdInfoCommand()
165 {
166 uint8_t response[sizeof(sdDev.csd) + sizeof(sdDev.cid)];
167 memcpy(response, sdDev.csd, sizeof(sdDev.csd));
168 memcpy(response + sizeof(sdDev.csd), sdDev.cid, sizeof(sdDev.cid));
169
170 hidPacket_send(response, sizeof(response));
171 }
172
173
174 static void
175 scsiTestCommand()
176 {
177 int resultCode = scsiSelfTest();
178 uint8_t response[] =
179 {
180 resultCode == 0 ? S2S_CFG_STATUS_GOOD : S2S_CFG_STATUS_ERR,
181 resultCode
182 };
183 hidPacket_send(response, sizeof(response));
184 }
185
186 static void
187 scsiDevInfoCommand()
188 {
189 uint8_t response[] =
190 {
191 FIRMWARE_VERSION >> 8,
192 FIRMWARE_VERSION & 0xff,
193 sdDev.capacity >> 24,
194 sdDev.capacity >> 16,
195 sdDev.capacity >> 8,
196 sdDev.capacity
197 };
198 hidPacket_send(response, sizeof(response));
199 }
200
201 static void
202 debugCommand()
203 {
204 uint8_t response[32];
205 memcpy(&response, &scsiDev.cdb, 12);
206 response[12] = scsiDev.msgIn;
207 response[13] = scsiDev.msgOut;
208 response[14] = scsiDev.lastStatus;
209 response[15] = scsiDev.lastSense;
210 response[16] = scsiDev.phase;
211 response[17] = *SCSI_STS_SCSI;
212 response[18] = scsiDev.target != NULL ? scsiDev.target->syncOffset : 0;
213 response[19] = scsiDev.target != NULL ? scsiDev.target->syncPeriod : 0;
214 response[20] = scsiDev.minSyncPeriod;
215 response[21] = scsiDev.rstCount;
216 response[22] = scsiDev.selCount;
217 response[23] = scsiDev.msgCount;
218 response[24] = scsiDev.cmdCount;
219 response[25] = scsiDev.watchdogTick;
220 response[26] = blockDev.state;
221 response[27] = scsiDev.lastSenseASC >> 8;
222 response[28] = scsiDev.lastSenseASC;
223 response[29] = *SCSI_STS_DBX;
224 response[30] = LastTrace;
225 response[31] = 0; // Unused
226 hidPacket_send(response, sizeof(response));
227 }
228
229 static void
230 sdWriteCommand(const uint8_t* cmd, size_t cmdSize)
231 {
232 if (cmdSize < 517)
233 {
234 return; // ignore.
235 }
236 uint32_t lba =
237 (((uint32_t)cmd[1]) << 24) |
238 (((uint32_t)cmd[2]) << 16) |
239 (((uint32_t)cmd[3]) << 8) |
240 ((uint32_t)cmd[4]);
241
242 memcpy(configDmaBuf, &cmd[5], 512);
243 BSP_SD_WriteBlocks_DMA((uint32_t*) configDmaBuf, lba * 512ll, 512, 1);
244
245 uint8_t response[] =
246 {
247 S2S_CFG_STATUS_GOOD
248 };
249 hidPacket_send(response, sizeof(response));
250 }
251
252 static void
253 sdReadCommand(const uint8_t* cmd, size_t cmdSize)
254 {
255 if (cmdSize < 5)
256 {
257 return; // ignore.
258 }
259 uint32_t lba =
260 (((uint32_t)cmd[1]) << 24) |
261 (((uint32_t)cmd[2]) << 16) |
262 (((uint32_t)cmd[3]) << 8) |
263 ((uint32_t)cmd[4]);
264
265 BSP_SD_ReadBlocks_DMA((uint32_t*) configDmaBuf, lba * 512ll, 512, 1);
266 hidPacket_send(configDmaBuf, 512);
267 }
268
269 static void
270 processCommand(const uint8_t* cmd, size_t cmdSize)
271 {
272 switch (cmd[0])
273 {
274 case S2S_CMD_PING:
275 pingCommand();
276 break;
277
278 case S2S_CMD_REBOOT:
279 s2s_enterBootloader();
280 break;
281
282 case S2S_CMD_SDINFO:
283 sdInfoCommand();
284 break;
285
286 case S2S_CMD_SCSITEST:
287 scsiTestCommand();
288 break;
289
290 case S2S_CMD_DEVINFO:
291 scsiDevInfoCommand();
292 break;
293
294 case S2S_CMD_SD_WRITE:
295 sdWriteCommand(cmd, cmdSize);
296 break;
297
298 case S2S_CMD_SD_READ:
299 sdReadCommand(cmd, cmdSize);
300 break;
301
302 case S2S_CMD_DEBUG:
303 if (debugTimerStarted == 0) {
304 debugInit();
305 }
306 debugCommand();
307 break;
308
309 case S2S_CMD_NONE: // invalid
310 default:
311 break;
312 }
313 }
314
315 void s2s_configPoll()
316 {
317 s2s_spin_lock(&usbDevLock);
318
319 if (!USBD_Composite_IsConfigured(&hUsbDeviceFS))
320 {
321 usbInEpState = USB_IDLE;
322 goto out;
323 }
324
325 if (USBD_HID_IsReportReady(&hUsbDeviceFS))
326 {
327 s2s_ledOn();
328
329 // The host sent us some data!
330 uint8_t hidBuffer[USBHID_LEN];
331 int byteCount = USBD_HID_GetReport(&hUsbDeviceFS, hidBuffer, sizeof(hidBuffer));
332 hidPacket_recv(hidBuffer, byteCount);
333
334 size_t cmdSize;
335 const uint8_t* cmd = hidPacket_getPacket(&cmdSize);
336 if (cmd && (cmdSize > 0))
337 {
338 processCommand(cmd, cmdSize);
339 }
340
341 s2s_ledOff();
342 }
343
344 switch (usbInEpState)
345 {
346 case USB_IDLE:
347 {
348 uint8_t hidBuffer[USBHID_LEN];
349 const uint8_t* nextChunk = hidPacket_getHIDBytes(hidBuffer);
350
351 if (nextChunk)
352 {
353 USBD_HID_SendReport (&hUsbDeviceFS, nextChunk, sizeof(hidBuffer));
354 usbInEpState = USB_DATA_SENT;
355 }
356 }
357 break;
358
359 case USB_DATA_SENT:
360 if (!USBD_HID_IsBusy(&hUsbDeviceFS))
361 {
362 // Data accepted.
363 usbInEpState = USB_IDLE;
364 }
365 break;
366 }
367
368 out:
369 s2s_spin_unlock(&usbDevLock);
370 }
371
372 void s2s_debugTimer()
373 {
374 if (!USBD_Composite_IsConfigured(&hUsbDeviceFS))
375 {
376 usbInEpState = USB_IDLE;
377 return;
378 }
379
380 if (USBD_HID_IsReportReady(&hUsbDeviceFS))
381 {
382 uint8_t hidBuffer[USBHID_LEN];
383 int byteCount = USBD_HID_GetReport(&hUsbDeviceFS, hidBuffer, sizeof(hidBuffer));
384 hidPacket_recv(hidBuffer, byteCount);
385
386 size_t cmdSize;
387 const uint8_t* cmd = hidPacket_peekPacket(&cmdSize);
388 // This is called from an ISR, only process simple commands.
389 if (cmd && (cmdSize > 0))
390 {
391 if (cmd[0] == S2S_CMD_DEBUG)
392 {
393 hidPacket_getPacket(&cmdSize);
394 debugCommand();
395 }
396 else if (cmd[0] == S2S_CMD_PING)
397 {
398 hidPacket_getPacket(&cmdSize);
399 pingCommand();
400 }
401 }
402 }
403
404 switch (usbInEpState)
405 {
406 case USB_IDLE:
407 {
408 uint8_t hidBuffer[USBHID_LEN];
409 const uint8_t* nextChunk = hidPacket_getHIDBytes(hidBuffer);
410
411 if (nextChunk)
412 {
413 USBD_HID_SendReport (&hUsbDeviceFS, nextChunk, sizeof(hidBuffer));
414 usbInEpState = USB_DATA_SENT;
415 }
416 }
417 break;
418
419 case USB_DATA_SENT:
420 if (!USBD_HID_IsBusy(&hUsbDeviceFS))
421 {
422 // Data accepted.
423 usbInEpState = USB_IDLE;
424 }
425 break;
426 }
427 }
428
429
430
431 // Public method for storing MODE SELECT results.
432 void s2s_configSave(int scsiId, uint16_t bytesPerSector)
433 {
434 S2S_TargetCfg* cfg = (S2S_TargetCfg*) s2s_getConfigById(scsiId);
435 cfg->bytesPerSector = bytesPerSector;
436
437 BSP_SD_WriteBlocks_DMA(
438 (uint32_t*) &s2s_cfg[0],
439 (sdDev.capacity - S2S_CFG_SIZE) * 512ll,
440 512,
441 (S2S_CFG_SIZE + 511) / 512);
442 }
443
444
445 const S2S_TargetCfg* s2s_getConfigByIndex(int i)
446 {
447 return (const S2S_TargetCfg*)
448 (s2s_cfg + sizeof(S2S_BoardCfg) + (i * sizeof(S2S_TargetCfg)));
449 }
450
451 const S2S_TargetCfg* s2s_getConfigById(int scsiId)
452 {
453 int i;
454 for (i = 0; i < S2S_MAX_TARGETS; ++i)
455 {
456 const S2S_TargetCfg* tgt = s2s_getConfigByIndex(i);
457 if ((tgt->scsiId & S2S_CFG_TARGET_ID_BITS) == scsiId)
458 {
459 return tgt;
460 }
461 }
462 return NULL;
463
464 }
465