forked from halsampson/FloppyReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MFMReader.cc
397 lines (310 loc) · 11.6 KB
/
MFMReader.cc
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
// FDD MFM reader
// for TI LM4F120XL dev board
// sends MFM data to FloppyReader.cpp
#include "inc\lm4f120h5qr.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
// High density floppy can generate transitions every < 1us, taking 2 bits to send timing interval
// so BaudRate should be > 2 * 10 / 8 = 2.5 MBaud
const int BaudRate = 3000000;
/* special pins to avoid
PB0 3.6V max
PB1 3.6V max
PD4 USB0DM 3.6V max
PD5 USB0DP 3.6V max
*/
// Port A:
const int U0Rx = 1 << 0; // PA0
const int U0Tx = 1 << 1; // PA1
// Port C
const int U1Rx = 1 << 4; // PC4
const int U1Tx = 1 << 5; // PC5
// Port D: Inputs
const int Index = 1 << 0; // PD0
const int Track00 = 1 << 1; // PD1
const int ReadData = 1 << 2; // PD2
const int DiskChange = 1 << 3; // PD3
// Port E: Outputs
const int Direction = 1 << 1; // PE1 Keep normally Hi = StepOut
const int Step = 1 << 2; // PE2 Keep normally Hi 10ms after Direction change
const int Side0 = 1 << 3; // PE3
const int HiDensity = 1 << 4; // PE4 300 vs. 360 RPM
const int MotorOff = 1 << 5; // PE5
// Port F:
const int SW2 = 1 << 0;
const int RED = 1 << 1; // Index
const int BLU = 1 << 2; // Track00
const int GRN = 1 << 3; // DiskChange
const int SW1 = 1 << 4;
const unsigned int SysClock = 80 * 1000 * 1000; // max allowed by MINSYSDIV using PLL
void initCpuClock(unsigned int xtal = SYSCTL_XTAL_16MHZ) {
// power up main oscillator & PLL; continue to use PIOSC
SYSCTL_RCC_R = SYSCTL_RCC_R & ~(SYSCTL_RCC_OSCSRC_M | SYSCTL_RCC_XTAL_M | SYSCTL_RCC_PWRDN | SYSCTL_RCC_USESYSDIV) | SYSCTL_RCC_BYPASS | SYSCTL_RCC_OSCSRC_INT | xtal;
SYSCTL_RCC2_R = SYSCTL_RCC2_R & ~(SYSCTL_RCC2_OSCSRC2_M | SYSCTL_RCC2_PWRDN2) | SYSCTL_RCC2_USERCC2 | SYSCTL_RCC2_BYPASS2 | SYSCTL_RCC2_OSCSRC2_IO;
SYSCTL_MISC_R = SYSCTL_INT_PLL_LOCK; // Clear the PLL lock interrupt
SYSCTL_RCC_R &= ~SYSCTL_RCC_MOSCDIS; // enable main osc
SYSCTL_RCC2_R = SYSCTL_RCC2_R & ~(SYSCTL_RCC2_SYSDIV2_M | SYSCTL_RCC2_OSCSRC2_M | SYSCTL_RCC2_SYSDIV2LSB) | SYSCTL_SYSDIV_2_5;
for (int delay = 64; delay--;);
while (!(SYSCTL_RIS_R & SYSCTL_INT_PLL_LOCK)); // wait for PLL lock
SYSCTL_RCC2_R &= ~SYSCTL_RCC2_BYPASS2; // switch to PLL clock
}
void initUART0(void) {
SYSCTL_RCGC1_R |= SYSCTL_RCGC1_UART0;
SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOA;
SYSCTL_RCGCUART_R |= SYSCTL_RCGCUART_R0; // default
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGC2_GPIOA; // enable Port A
UART0_CTL_R &= ~UART_CTL_UARTEN;
int baud = BaudRate;
if (baud * 16 > SysClock) {
UART0_CTL_R |= UART_CTL_HSE; // UART clock = 8 * BaudRate
baud /= 2;
} else UART0_CTL_R &= ~UART_CTL_HSE; // UART clock = 16 * BaudRate
int baudX64 = (SysClock * 8 / baud + 1) / 2;
UART0_IBRD_R = baudX64 / 64;
UART0_FBRD_R = baudX64 % 64;
UART0_LCRH_R = UART_LCRH_WLEN_8 | UART_LCRH_FEN;
UART0_CC_R = 0; // SysClock default
UART0_CTL_R |= UART_CTL_RXE | UART_CTL_TXE | UART_CTL_UARTEN;
GPIO_PORTA_AFSEL_R |= U0Rx | U0Tx;
GPIO_PORTA_DR8R_R = U0Tx; // 8 mA drive strength
GPIO_PORTA_DEN_R = U0Rx | U0Tx;
GPIO_PORTA_PCTL_R |= 0x11; // pins 0 and 1 default
GPIO_PORTA_AMSEL_R &= ~(U0Rx | U0Tx);
}
void initUART1(void) {
SYSCTL_RCGC1_R |= SYSCTL_RCGC1_UART1;
SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOC;
SYSCTL_RCGCUART_R |= SYSCTL_RCGCUART_R1;
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGC2_GPIOC; // enable Port C
UART1_CTL_R &= ~UART_CTL_UARTEN;
int baud = BaudRate;
if (baud * 16 > SysClock) {
UART1_CTL_R |= UART_CTL_HSE;
baud /= 2;
} else UART1_CTL_R &= ~UART_CTL_HSE;
int baudX64 = (SysClock * 8 / baud + 1) / 2;
UART1_IBRD_R = baudX64 / 64;
UART1_FBRD_R = baudX64 % 64;
UART1_LCRH_R = UART_LCRH_WLEN_8 | UART_LCRH_FEN; // FIFO doesn't help long string of 0s or 1s much
UART1_CC_R = 0; // SysClock default
UART1_CTL_R |= UART_CTL_RXE | UART_CTL_TXE | UART_CTL_UARTEN;
GPIO_PORTC_AFSEL_R |= U1Rx | U1Tx;
GPIO_PORTC_DR8R_R = U1Tx; // 8 mA drive strength
GPIO_PORTC_DEN_R = U1Rx | U1Tx;
GPIO_PORTC_PCTL_R |= 0x220000; // pins 4 and 5
GPIO_PORTC_AMSEL_R &= ~(U1Rx | U1Tx);
}
inline bool rxRdy(void) {
return !(UART1_FR_R & UART_FR_RXFE); // Rx FIFO not empty
}
inline unsigned char rxChar(void) {
return UART1_DR_R;
}
inline void txWait(void) {
while (UART1_FR_R & UART_FR_TXFF); // wait for Tx FIFO room
}
inline void sendByte(unsigned char c) {
UART1_DR_R = c;
}
void sendStr(char* str) {
while (*str) {
txWait();
sendByte(*str++);
}
}
void initGPIO() {
SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOD | SYSCTL_RCGC2_GPIOE | SYSCTL_RCGC2_GPIOF; // Port enable (clock gating)
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGC2_GPIOD | SYSCTL_RCGC2_GPIOE | SYSCTL_RCGC2_GPIOF; // ??
GPIO_PORTD_DIR_R = 0; // inputs
GPIO_PORTD_PUR_R = 0x0F; // 20K pull-ups
GPIO_PORTD_DEN_R = 0x0F; // digital enable
GPIO_PORTE_DIR_R = 0x3E; // outputs
GPIO_PORTE_ODR_R = 0x3E; // open drain
GPIO_PORTE_PUR_R = 0x3E; // for disconnected checking
GPIO_PORTE_DR2R_R = 0;
GPIO_PORTE_DR8R_R = 0x3E;// drive strength 8mA (5V / 330 ohms = 15 mA -> < 1V out for TTL)
GPIO_PORTE_DATA_R= 0x3E; // normally Hi
GPIO_PORTE_DEN_R = 0x3E; // digital enable
GPIO_PORTF_LOCK_R = GPIO_LOCK_KEY; // for PF0 NMI
GPIO_PORTF_CR_R = 0xFF; // commit NMI
GPIO_PORTF_DIR_R = 0x0E; // LED outputs
GPIO_PORTF_ODR_R = 0x0E; // open drain
GPIO_PORTF_PUR_R = 0x1F; // 20K pull-ups
GPIO_PORTF_DEN_R = 0x1F; // digital enable
}
void delay_us(int us) {
unsigned int endCount = WTIMER3_TAV_R + us * (SysClock / 1000000) + 10; // incl. overhead
while ((int)(WTIMER3_TAV_R - endCount) < 0);
}
void delay_ms(int ms) { // up to 26.8 secs
unsigned int endCount = WTIMER3_TAV_R + ms * (SysClock / 1000) + 10; // incl. overhead
while ((int)(WTIMER3_TAV_R - endCount) < 0);
}
void step(bool out = false) {
if (!out) {
GPIO_PORTE_DATA_BITS_R[Direction] = 0; // step in
delay_ms(10);
}
GPIO_PORTE_DATA_BITS_R[Step] = 0;
delay_us(1);
GPIO_PORTE_DATA_BITS_R[Step] = Step;
delay_ms(10);
GPIO_PORTE_DATA_BITS_R[Direction] = Direction; // keep Hi vs. total current limit
}
unsigned int motorTimeout;
void motor(bool on = true) {
GPIO_PORTE_DATA_BITS_R[MotorOff] = on ? 0 : MotorOff;
}
void initTimer(void) {
SYSCTL_RCGCWTIMER_R = SYSCTL_RCGCWTIMER_R3;
GPIO_PORTD_AFSEL_R |= ReadData;
GPIO_PORTD_PCTL_R |= GPIO_PCTL_PD2_WT3CCP0; // ReadData to WTIMER3
WTIMER3_CTL_R &= ~(TIMER_CTL_TBEN | TIMER_CTL_TAEN);
WTIMER3_CFG_R = 4; // 32 bit mode
WTIMER3_TAMR_R = TIMER_TAMR_TACDIR | TIMER_TAMR_TACMR | TIMER_TAMR_TAMR_CAP; // count up, edge-time mode
WTIMER3_CTL_R |= TIMER_CTL_TAEVENT_NEG;
WTIMER3_TAILR_R = 0xFFFFFFFF; // default
WTIMER3_CTL_R |= TIMER_CTL_TAEN;
// timer TAV_R should be counting; TAR_R captures edge times
}
const int DiscRPM = 300; // min - sonme are 360 RPM
const int RevClocks = SysClock / (DiscRPM / 60) * 1015 / 1000; // includes +1.5% max speed error
const int PastIndex = RevClocks * 12 / 16; // time to look for necxt index hole
const int SlowBitClocks = 2 * SysClock / 1000000; // 2, 3, 4 X 2 us
const int HistogramSize = SlowBitClocks * 6; // max
int histogram[HistogramSize]; // 960
// MFM bit time interval thresholds
// can set using 'A'djust command
int BitClocks = SlowBitClocks; // 300 RPM 2/3/4 us
int bitTime1p5 = BitClocks * 3 / 2;
int bitTime2p5 = BitClocks * 5 / 2;
int bitTime3p5 = BitClocks * 7 / 2;
int bitTime4p5 = BitClocks * 9 / 2;
void readTrack(int side = 0, int density = 0) {
GPIO_PORTE_DATA_BITS_R[HiDensity] = density ? 0 : HiDensity; // also 2 steps for 48 tpi
GPIO_PORTE_DATA_BITS_R[Side0] = side ? 0 : Side0; // Lo = Side1 select
if (GPIO_PORTE_DATA_BITS_R[MotorOff]) {
motor();
delay_ms(500); // wait until up to speed
}
motorTimeout = WTIMER3_TAV_R + SysClock; // 1 second
unsigned int indexTimeout = WTIMER3_TAV_R + 3 * RevClocks;
while (!(GPIO_PORTD_DATA_BITS_R[Index])) { //wait for no index hole
if ((int)(WTIMER3_TAV_R - indexTimeout) > 0) {
sendByte('H'); // status: stuck at index Hole
return;
}
}
while (GPIO_PORTD_DATA_BITS_R[Index]) { //wait for index hole
if ((int)(WTIMER3_TAV_R - indexTimeout) > 0) {
sendByte('I'); // status: no Index -- WHY??
return;
}
}
unsigned int revPastIndex = WTIMER3_TAV_R + PastIndex;
unsigned int noDataEnd = WTIMER3_TAV_R + 2 * RevClocks;
unsigned int prevEdgeTime = WTIMER3_TAR_R;
int mfmBits = 1;
int mfmPos = 1; // clock then data
int runtTime = 0;
while (1) {
while (!(WTIMER3_RIS_R & TIMER_RIS_CAERIS)) { // wait for neg edge capture vs. interrupt?
if ((int)(WTIMER3_TAV_R - revPastIndex) > 0) {
if (!GPIO_PORTD_DATA_BITS_R[Index]) // index hole at end of track
return; // done with track
if ((int)(WTIMER3_TAV_R - noDataEnd) > 0) { // prevent stuck if no data
sendByte('D'); // no Data
return;
}
}
}
int bitInterval = WTIMER3_TAR_R - prevEdgeTime; // captured count between neg edges
prevEdgeTime = WTIMER3_TAR_R;
WTIMER3_ICR_R |= TIMER_ICR_CAECINT; // clear status
bitInterval += runtTime;
if (bitInterval >= HistogramSize)
continue;
histogram[bitInterval]++; // diagnostic
if (bitInterval < bitTime1p5) {
runtTime = bitInterval;
continue;
}
runtTime = 0;
int bitCount;
if (bitInterval < bitTime2p5)
bitCount = 2;
else if (bitInterval < bitTime3p5)
bitCount = 3;
else if (bitInterval < bitTime4p5)
bitCount = 4;
else continue; // not in synch
mfmBits <<= bitCount;
mfmBits |= 1;
if ((mfmPos += bitCount) >= 8) {
mfmPos -= 8;
sendByte((mfmBits >> mfmPos) & 0xFF);
}
}
}
int main(void) {
initCpuClock();
initGPIO();
initUART1();
initTimer();
while (rxRdy())
volatile int flush = rxChar();
while (1) {
GPIO_PORTF_DATA_R = (GPIO_PORTD_DATA_BITS_R[Index | Track00] << 1 | GPIO_PORTD_DATA_BITS_R[DiskChange]) ^ 0xE; // update status LEDs
if (rxRdy()) switch(rxChar() & 0x5F) { // command character received
case 'A' : // Adjust bit timing
delay_ms(16); // Rx FIFO should have all 4 values -- better a check header
unsigned char* p = (unsigned char*)&bitTime1p5;
for (int count = 4 * sizeof(int); count--;)
*p++ = UART1_DR_R;
break;
case 'B' : step(true); break; // step Back
case 'S' : step(); break; // Step: twice on high density drives
case 'Z' : // retract to track Zero
int maxSteps = 80; // for hi density drives
while (GPIO_PORTD_DATA_BITS_R[Track00] && maxSteps-- >= 0) step(true); // to track Zero
break;
case 'R' : readTrack(); break;
case 'T' : readTrack(1); break; // read side 1
case 'D' : readTrack(0, 1); break; // HiDensity
case 'H' : // sned Histogram data
histogram[0] = 0xBE66A7ED; // marker
p = (unsigned char*)&histogram;
for (int i = sizeof(histogram); i--;) {
txWait();
sendByte(*p);
*p++ = 0;
}
break;
case 'I' : sendStr("FDD MFM reader " __DATE__ " " __TIME__ "\n"); break; // Identify
case 'M' : // Motor on
motorTimeout = WTIMER3_TAV_R + SysClock; // 1 second
motor();
break;
case 'Q' : sendByte(GPIO_PORTD_DATA_BITS_R[Index | Track00 | DiskChange]); break; // status
}
if (!GPIO_PORTE_DATA_BITS_R[MotorOff] && (int)(WTIMER3_TAV_R - motorTimeout) > 0) { // timeout
motor(false);
GPIO_PORTE_DATA_BITS_R[Side0 | HiDensity] = 0xFF; // Hi to keep pin currents low
}
if (!GPIO_PORTF_DATA_BITS_R[SW1]) { // SW1 pressed
readTrack();
GPIO_PORTF_DATA_BITS_R[GRN] = GRN;
while (!GPIO_PORTF_DATA_BITS_R[SW1]);
}
if (!GPIO_PORTF_DATA_BITS_R[SW2]) { // SW2 pressed
step();
int longPress = 500;
while (!GPIO_PORTF_DATA_BITS_R[SW2]) { // still pressed
delay_ms(1);
if (!longPress--)
while (GPIO_PORTD_DATA_BITS_R[Track00]) step(true);
}
}
}
}