/
FloppyDriveController.sketch.ino
641 lines (515 loc) · 24 KB
/
FloppyDriveController.sketch.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/* ArduinoFloppyReader (and writer)
*
* Copyright (C) 2017-2018 Robert Smith (@RobSmithDev)
* http://amiga.robsmithdev.co.uk
*
* This sketch is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this sketch; if not, see http://www.gnu.org/licenses
*/
/////////////////////////////////////////////////////////////////////////////////////////////////////
// This sketch manages the interface between the floppy drive and the computer as well as the //
// low-level disk reading and writing. For more information and how to connect your Arduino //
// to a floppy drive and computer visit http://amiga.robsmithdev.co.uk //
/////////////////////////////////////////////////////////////////////////////////////////////////
#define BAUDRATE 2000000 // The baudrate that we want to communicate over (2M)
#define BAUD_PRESCALLER_NORMAL_MODE (((F_CPU / (BAUDRATE * 16UL))) - 1)
#define BAUD_PRESCALLER_DOUBLESPEED_MODE (((F_CPU / (BAUDRATE * 8UL))) - 1)
#define UART_USE_DOUBLESPEED_MODE // We're using double speed mode
#define MOTOR_TRACK_DECREASE HIGH // Motor directions for PIN settings
#define MOTOR_TRACK_INCREASE LOW
// PIN 2 - INDEX PULSE PIN - used to detect a specific point on the track for sync. Not used by standard Amiga disks but some copy protection uses it.
#define PIN_INDEX_DETECTED 2 // Pin used to detect the index pulse
#define PIN_INDEX_PORT PIND
#define PIN_INDEX_MASK B00000100
// PIN 3 - WRITE DATA
#define PIN_WRITE_DATA 3 // Raw triggering of writing data to the disk
#define PIN_WRITE_DATA_PORT PORTD // The actual port the above pin is on
#define PIN_WRITE_DATA_MASK B00001000 // The mask used to set this pin high or low
// PIN 4 - READ DATA
#define PIN_READ_DATA 4 // Reads RAW floppy data on this pin
#define PIN_READ_DATA_MASK B00010000 // The mask for the port
#define PIN_READ_DATA_PORT PIND // The port the above pin is on
// PIN 5, 6 and 7 - DRIVE, HEAD MOTOR DIRECTION and CONTROL
#define PIN_DRIVE_ENABLE_MOTOR 5 // Turn on and off the motor on the drive
#define PIN_MOTOR_DIR 6 // Stepper motor output to choose the direction the head should move
#define PIN_MOTOR_STEP 7 // Stepper motor step line to move the head position
// PIN 8 - Used to detect track 0 while moving the head
#define PIN_DETECT_TRACK_0 8 // Used to see if the drive is at track 0
// PIN 9 - HEAD SELECTION
#define PIN_HEAD_SELECT 9 // Choose upper and lower head on the drive
// PIN A0 - WRITE GATE (Floppy Write Enable)
#define PIN_WRITE_GATE A0 // This pin enables writing to the disk
#define PIN_WRITE_GATE_PORT PORTC // The actual port the above pin is on
#define PIN_WRITE_GATE_MASK B00000001 // The port pin mask for the gate
// PIN A1 - CHECK WRITE PROTECTION
#define PIN_WRITE_PROTECTED A1 // To check if the disk is write protected
// PIN A2 - CTS Pin from UART
#define PIN_CTS A2 // Pin linked to the CTS pin
#define PIN_CTS_PORT PORTC // Port the CTS pin is on
#define PIN_CTS_MASK B00000100 // Binary mask to control it with
// PIN 13 - Activity LED
#define PIN_ACTIVITY_LED 13 // Standard LED on Arduinos. We're just using this as a read/write status flag
// Paula on the Amiga used to find the SYNC WORDS and then read 0x1900 further WORDS. A dos track is 11968 bytes in size, theritical revolution is 12800 bytes.
#define RAW_TRACKDATA_LENGTH (0x1900*2+0x440) // Paula assumed it was 12868 bytes, so we read that, plus thre size of a sectors
// The current track that the head is over
int currentTrack = 0;
// If the drive has been switched on or not
bool driveEnabled = 0;
// If we're in WRITING mode or not
bool inWriteMode = 0;
// Because we turned off interrupts delay() doesnt work!
void smalldelay(unsigned long delayTime) {
delayTime*=(F_CPU/(9*1000L));
for (unsigned long loops=0; loops<delayTime; ++loops) {
asm volatile("nop\n\t"::);
}
}
// Step the head once. This seems to be an acceptable speed for the head
void stepDirectionHead() {
smalldelay(5);
digitalWrite(PIN_MOTOR_STEP,LOW);
smalldelay(5);
digitalWrite(PIN_MOTOR_STEP,HIGH);
}
// Prepare serial port - We dont want to use the arduino serial library as we want to use faster speeds and no serial interrupts
void prepSerialInterface() {
#ifdef UART_USE_DOUBLESPEED_MODE
UBRR0H = (uint8_t)(BAUD_PRESCALLER_DOUBLESPEED_MODE>>8);
UBRR0L = (uint8_t)(BAUD_PRESCALLER_DOUBLESPEED_MODE);
UCSR0A |= 1<<U2X0;
#else
UBRR0H = (uint8_t)(BAUD_PRESCALLER_NORMAL_MODE>>8);
UBRR0L = (uint8_t)(BAUD_PRESCALLER_NORMAL_MODE);
UCSR0A &= ~(1<<U2X0);
#endif
// UCSROA is a status register only (apart from U2Xn):
// • Bit 7 – RXCn: USART Receive Complete
// • Bit 6 – TXCn: USART Transmit Complete
// • Bit 5 – UDREn: USART Data Register Empty
// • Bit 4 – FEn: Frame Error
// • Bit 3 – DORn: Data OverRun/
// • Bit 2 – UPEn: USART Parity Error
// • Bit 1 – U2Xn: Double the USART Transmission Speed
// • Bit 0 – MPCMn: Multi-processor Communication Mode
UCSR0B = (0<<RXCIE0) | // Disable ReceiveCompleteInteruptEnable
(0<<TXCIE0) | // Disable TransmitCompleteInteruptEnable
(0<<UDRIE0) | // Disable UsartDataRegisterEmptyInteruptEnable
(1<<RXEN0) | // Enable RX
(1<<TXEN0) | // Enable TX
(0<<UCSZ02) ; // Clear the 9-bit character mode bit
UCSR0C = (0<<UMSEL01) | (0<<UMSEL00) | // UsartModeSelect - Asynchronous (00=Async, 01=Sync, 10=Reserved, 11=Master SPI)
(0<<UPM01) | (0<<UPM00) | // UsartParatyMode - Disabled (00=Off, 01=Reserved, 10=Even, 11=Odd)
(0<<USBS0) | // UsartStopBitSelect (0=1 Stop bit, 1 = 2Stop Bits)
(1<<UCSZ01) | (1<<UCSZ00); // UsartCharacterSiZe - 8-bit (00=5Bit, 01=6Bit, 10=7Bit, 11=8Bit, must be 11 for 9-bit)
}
// Directly read a byte from the UART0 (serial)
inline byte readByteFromUART() {
while (!( UCSR0A & ( 1 << RXC0 ))){}; // Wait for data to be available
return UDR0; // Read it
}
// Directly write a byte to the UART0
inline void writeByteToUART(const char value) {
while(!(UCSR0A & (1<<UDRE0))); // Wait until the last byte has been sent
UDR0 = value; // And send another
}
// Main arduino setup
void setup() {
// Do these right away to prevent the disk being written to
digitalWrite(PIN_WRITE_GATE, HIGH);
digitalWrite(PIN_WRITE_DATA, HIGH);
pinMode(PIN_WRITE_GATE,OUTPUT);
pinMode(PIN_WRITE_DATA,OUTPUT);
pinMode(PIN_CTS,OUTPUT);
pinMode(PIN_WRITE_PROTECTED, INPUT_PULLUP);
pinMode(PIN_DETECT_TRACK_0, INPUT_PULLUP);
pinMode(PIN_READ_DATA,INPUT_PULLUP);
pinMode(PIN_INDEX_DETECTED,INPUT_PULLUP);
// Prepre the pin inputs and outputs
pinMode(PIN_DRIVE_ENABLE_MOTOR, OUTPUT);
pinMode(PIN_HEAD_SELECT, OUTPUT);
digitalWrite(PIN_DRIVE_ENABLE_MOTOR,HIGH);
digitalWrite(PIN_HEAD_SELECT,LOW);
pinMode(PIN_MOTOR_DIR, OUTPUT);
pinMode(PIN_MOTOR_STEP,OUTPUT);
pinMode(PIN_ACTIVITY_LED,OUTPUT);
digitalWrite(PIN_ACTIVITY_LED,LOW);
// Disable all interrupts - we dont want them!
cli();
TIMSK0=0;
TIMSK1=0;
TIMSK2=0;
PCICR = 0;
PCIFR = 0;
PCMSK0 = 0;
PCMSK2 = 0;
PCMSK1 = 0;
// Setup the USART
prepSerialInterface();
}
// Run a diagnostics test command
void runDiagnostic() {
// See what test to run
byte test = readByteFromUART();
switch (test) {
case '1': // Turn off CTS
PIN_CTS_PORT &= (~PIN_CTS_MASK);
writeByteToUART('1');
readByteFromUART();
writeByteToUART('1');
break;
case '2': // Turn on CTS
PIN_CTS_PORT|=PIN_CTS_MASK;
writeByteToUART('1');
readByteFromUART();
writeByteToUART('1');
break;
case '3': // Index pulse test (with timeout)
{
bool state1 = false;
bool state2 = false;
// At the 300 RPM (5 turns per second) this runs at, this loop needs to run a few times to check for index pulses. This runs for approx 1 second
for (unsigned int b=0; b<20; b++) {
for (unsigned int a=0; a<60000; a++) {
if (PIN_INDEX_PORT & PIN_INDEX_MASK) state1=true; else state2=true;
if (state1&&state2) break;
}
if (state1&&state2) break;
}
if (state1&&state2) {
writeByteToUART('1');
} else {
writeByteToUART('0');
}
}
break;
case '4': // Data pulse test (with timeout)
{
bool state1 = false;
bool state2 = false;
for (unsigned int b=0; b<20; b++) {
for (unsigned int a=0; a<60000; a++) {
if (PIN_READ_DATA_PORT & PIN_READ_DATA_MASK) state1=true; else state2=true;
if (state1&&state2) break;
}
if (state1&&state2) break;
}
if (state1&&state2) {
writeByteToUART('1');
} else {
writeByteToUART('0');
}
}
break;
default:
writeByteToUART('0');
break;
}
}
// Rewinds the head back to Track 0
bool goToTrack0() {
digitalWrite(PIN_MOTOR_DIR,MOTOR_TRACK_DECREASE); // Set the direction to go backwards
int counter=0;
while (digitalRead(PIN_DETECT_TRACK_0) != LOW) {
stepDirectionHead(); // Keep moving the head until we see the TRACK 0 detection pin
counter++;
// If this happens we;ve steps twice as many as needed and still havent found track 0
if (counter>170) {
return false;
}
}
currentTrack = 0; // Reset the track number
return true;
}
// Goto a specific track. During testing it was easier for the track number to be supplied as two ASCII characters, so I left it like this
bool gotoTrackX() {
// Read the bytes
byte track1 = readByteFromUART();
byte track2 = readByteFromUART();
// Validate
if ((track1<'0') || (track1>'9')) return false;
if ((track2<'0') || (track2>'9')) return false;
// Calculate target track and validate
int track = ((track1-'0')*10) + (track2-'0');
if (track<0) return false;
if (track>81) return false; // yes amiga could read track 81!
// Exit if its already been reached
if (track == currentTrack) return true;
// And step the head until we reach this track number
if (currentTrack < track) {
digitalWrite(PIN_MOTOR_DIR,MOTOR_TRACK_INCREASE); // Move OUT
while (currentTrack < track) {
stepDirectionHead();
currentTrack++;
}
} else {
digitalWrite(PIN_MOTOR_DIR,MOTOR_TRACK_DECREASE); // Move IN
while (currentTrack > track) {
stepDirectionHead();
currentTrack--;
}
}
return true;
}
// 256 byte circular buffer - don't change this, we abuse the unsigned char to overflow back to zero!
#define SERIAL_BUFFER_SIZE 256
#define SERIAL_BUFFER_START (SERIAL_BUFFER_SIZE-16)
unsigned char SERIAL_BUFFER[SERIAL_BUFFER_SIZE];
#define CHECK_SERIAL() if (UCSR0A & ( 1 << RXC0 )) { \
SERIAL_BUFFER[serialWritePos++] = UDR0; \
serialBytesInUse++; \
} else \
if (serialBytesInUse<SERIAL_BUFFER_START) { \
PIN_CTS_PORT &= (~PIN_CTS_MASK); \
PIN_CTS_PORT|=PIN_CTS_MASK; \
}
// Small Macro to write a '1' pulse to the drive if a bit is set based on the supplied bitmask
#define WRITE_BIT(value,bitmask) if (currentByte & bitmask) { \
while (TCNT2<value) {}; \
PIN_WRITE_DATA_PORT&=~PIN_WRITE_DATA_MASK; \
} else { \
while (TCNT2<value) {}; \
PIN_WRITE_DATA_PORT|=PIN_WRITE_DATA_MASK; \
}
// Write a track to disk from the UART - the data should be pre-MFM encoded raw track data where '1's are the pulses/phase reversals to trigger
void writeTrackFromUART() {
// Configure timer 2 just as a counter in NORMAL mode
TCCR2A = 0 ; // No physical output port pins and normal operation
TCCR2B = bit(CS20); // Precale = 1
// Check if its write protected. You can only do this after the write gate has been pulled low
if (digitalRead(PIN_WRITE_PROTECTED) == LOW) {
writeByteToUART('N');
digitalWrite(PIN_WRITE_GATE,HIGH);
return;
} else writeByteToUART('Y');
// Find out how many bytes they want to send
unsigned char highByte = readByteFromUART();
unsigned char lowByte = readByteFromUART();
unsigned char waitForIndex = readByteFromUART();
PIN_CTS_PORT|=PIN_CTS_MASK; // stop any more data coming in!
unsigned short numBytes = (((unsigned short)highByte)<<8) | lowByte;
writeByteToUART('!');
register unsigned char currentByte;
// Signal we're ready for another byte to come
PIN_CTS_PORT &= (~PIN_CTS_MASK);
// Fill our buffer to give us a head start
for (int a=0; a<SERIAL_BUFFER_START; a++) {
// Wait for it
while (!( UCSR0A & ( 1 << RXC0 ))){};
// Save the byte
SERIAL_BUFFER[a] = UDR0;
}
// Stop more bytes coming in, although we expect one more
PIN_CTS_PORT|=PIN_CTS_MASK;
// Setup buffer parameters
unsigned char serialReadPos = 0;
unsigned char serialWritePos = SERIAL_BUFFER_START;
unsigned int serialBytesInUse = SERIAL_BUFFER_START;
digitalWrite(PIN_ACTIVITY_LED,HIGH);
// Enable writing
PIN_WRITE_GATE_PORT&=~PIN_WRITE_GATE_MASK;
// While the INDEX pin is high wait. Might as well write from the start of the track
if (waitForIndex)
while (PIN_INDEX_PORT & PIN_INDEX_MASK) {};
// Reset the counter, ready for writing
TCNT2=0;
// Loop them bytes - ideally I wanted to use an ISR here, but theres just too much overhead even with naked ISRs to do this (with preserving registers etc)
for (register unsigned int a=0; a<numBytes; a++) {
// Should never happen, but we'll wait here if theres no data
if (serialBytesInUse<1) {
// This can;t happen and causes a write failure
digitalWrite(PIN_ACTIVITY_LED,LOW);
writeByteToUART('X'); // Thus means buffer underflow. PC wasn't sending us data fast enough
PIN_WRITE_GATE_PORT|=PIN_WRITE_GATE_MASK;
TCCR2B = 0; // No Clock (turn off)
return;
}
// Read a buye from the buffer
currentByte = SERIAL_BUFFER[serialReadPos++];
serialBytesInUse--;
// Theres a small possability, looking at the decompiled ASM (and less likely even with these few extra instructions) we actually might get back here before the TCNT2 overflows back to zero causing this to write early
while (TCNT2>=240) {}
// Now we write the data. Hopefully by the time we get back to the top everything is ready again
WRITE_BIT(0x10,B10000000);
CHECK_SERIAL();
WRITE_BIT(0x30,B01000000);
CHECK_SERIAL();
WRITE_BIT(0x50,B00100000);
CHECK_SERIAL();
WRITE_BIT(0x70,B00010000);
CHECK_SERIAL();
WRITE_BIT(0x90,B00001000);
CHECK_SERIAL();
WRITE_BIT(0xB0,B00000100);
CHECK_SERIAL();
WRITE_BIT(0xD0,B00000010);
CHECK_SERIAL();
WRITE_BIT(0xF0,B00000001);
}
PIN_WRITE_GATE_PORT|=PIN_WRITE_GATE_MASK;
// Done!
writeByteToUART('1');
digitalWrite(PIN_ACTIVITY_LED,LOW);
// Disable the 500khz signal
TCCR2B = 0; // No Clock (turn off)
}
// Read the track using a timings to calculate which MFM sequence has been triggered
void readTrackDataFast() {
// Configure timer 2 just as a counter in NORMAL mode
TCCR2A = 0 ; // No physical output port pins and normal operation
TCCR2B = bit(CS20); // Precale = 1
// First wait for the serial port to be available
while(!(UCSR0A & (1<<UDRE0)));
// Signal we're active
digitalWrite(PIN_ACTIVITY_LED,HIGH);
// Force data to be stored in a register
register unsigned char DataOutputByte = 0;
// While the INDEX pin is high wait if the other end requires us to
if (readByteFromUART())
while (PIN_INDEX_PORT & PIN_INDEX_MASK) {};
// Prepare the two counter values as follows:
TCNT2=0; // Reset the counter
register unsigned char counter;
long totalBits=0;
long target = ((long)RAW_TRACKDATA_LENGTH)*(long)8;
while (totalBits<target) {
for (register unsigned char bits=0; bits<4; bits++) {
// Wait while pin is high
while (PIN_READ_DATA_PORT & PIN_READ_DATA_MASK) {};
counter = TCNT2, TCNT2 = 0; // reset - must be done with a COMMA
DataOutputByte<<=2;
// DO NOT USE BRACES HERE, use the "," or the optomiser messes it up
if (counter<80) DataOutputByte|=B00000001,totalBits+=2; else // this accounts for just a '1' or a '01' as two '1' arent allowed in a row
if (counter>111) DataOutputByte|=B00000011,totalBits+=4; else DataOutputByte|=B00000010,totalBits+=3;
// Wait until pin is high again
while (!(PIN_READ_DATA_PORT & PIN_READ_DATA_MASK)) {};
}
UDR0 = DataOutputByte;
}
// Because of the above rules the actual valid two-bit sequences output are 01, 10 and 11, so we use 00 to say "END OF DATA"
writeByteToUART(0);
// turn off the status LED
digitalWrite(PIN_ACTIVITY_LED,LOW);
// Disable the counter
TCCR2B = 0; // No Clock (turn off)
}
// The main command loop
void loop() {
PIN_CTS_PORT &= (~PIN_CTS_MASK); // Allow data incoming
PIN_WRITE_GATE_PORT|=PIN_WRITE_GATE_MASK; // always turn writing off
// Read the command from the PC
byte command = readByteFromUART();
switch (command) {
// Command: "?" Means information about the firmware
case '?':writeByteToUART('1'); // Success
writeByteToUART('V'); // Followed
writeByteToUART('1'); // By
writeByteToUART('.'); // Version
writeByteToUART('2'); // Number
break;
// Command "." means go back to track 0
case '.': if (!driveEnabled) writeByteToUART('0'); else {
if (goToTrack0()) // reset
writeByteToUART('1');
else writeByteToUART('#');
}
break;
// Command "#" means goto track. Should be formatted as #00 or #32 etc
case '#': // Goto Track
if (!driveEnabled) {
readByteFromUART();
readByteFromUART();
writeByteToUART('0');
} else
if (gotoTrackX()) {
smalldelay(100); // wait for drive
writeByteToUART('1');
} else writeByteToUART('0');
break;
// Command "[" select LOWER disk side
case '[': digitalWrite(PIN_HEAD_SELECT,LOW);
writeByteToUART('1');
break;
// Command "]" select UPPER disk side
case ']': digitalWrite(PIN_HEAD_SELECT,HIGH);
writeByteToUART('1');
break;
// Command "<" Read track from the drive
case '<': if (!driveEnabled) writeByteToUART('0'); else {
writeByteToUART('1');
readTrackDataFast();
}
break;
// Command ">" Write track to the drive
case '>': if (!driveEnabled) writeByteToUART('0'); else
if (!inWriteMode) writeByteToUART('0'); else {
writeByteToUART('1');
writeTrackFromUART();
}
break;
// Turn off the drive motor
case '-': digitalWrite(PIN_DRIVE_ENABLE_MOTOR,HIGH);
digitalWrite(PIN_WRITE_GATE,HIGH);
driveEnabled = 0;
writeByteToUART('1');
inWriteMode = 0;
break;
// Turn on the drive motor and setup in READ MODE
case '+': if (inWriteMode) {
// Ensure writing is turned off
digitalWrite(PIN_DRIVE_ENABLE_MOTOR,HIGH);
digitalWrite(PIN_WRITE_GATE,HIGH);
smalldelay(100);
driveEnabled = 0;
inWriteMode = 0;
}
if (!driveEnabled) {
digitalWrite(PIN_DRIVE_ENABLE_MOTOR,LOW);
driveEnabled = 1;
smalldelay(750); // wait for drive
}
writeByteToUART('1');
break;
// Turn on the drive motor and setup in WRITE MODE
case '~': if (driveEnabled) {
digitalWrite(PIN_WRITE_GATE,HIGH);
digitalWrite(PIN_DRIVE_ENABLE_MOTOR,HIGH);
driveEnabled = 0;
smalldelay(100);
}
// We're writing!
digitalWrite(PIN_WRITE_GATE,LOW);
// Gate has to be pulled LOW BEFORE we turn the drive on
digitalWrite(PIN_DRIVE_ENABLE_MOTOR,LOW);
// Raise the write gate again
digitalWrite(PIN_WRITE_GATE,HIGH);
smalldelay(750); // wait for drive
// At this point we can see the status of the write protect flag
if (digitalRead(PIN_WRITE_PROTECTED) == LOW) {
writeByteToUART('0');
inWriteMode = 0;
digitalWrite(PIN_DRIVE_ENABLE_MOTOR,HIGH);
//digitalWrite(PIN_WRITE_GATE,HIGH);
} else {
inWriteMode = 1;
driveEnabled = 1;
writeByteToUART('1');
}
break;
case '&': runDiagnostic();
break;
// We don't recognise the command!
default:
writeByteToUART('!'); // error
break;
}
}