forked from dhansel/ArduinoFDC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiskio.cpp
132 lines (106 loc) · 3.35 KB
/
diskio.cpp
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
// -----------------------------------------------------------------------------
// 3.5"/5.25" DD/HD Disk controller for Arduino
// Copyright (C) 2021 David Hansel
//
// This program 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 program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// -----------------------------------------------------------------------------
#include "diskio.h"
#include "ArduinoFDC.h"
DSTATUS disk_status(BYTE pdrv)
{
// STA_NOINIT: Drive not initialized
// STA_NODISK: No medium in the drive
// STA_PROTECT: Write protected
return ArduinoFDC.isWriteProtected() ? STA_PROTECT : 0;
}
DSTATUS disk_initialize(BYTE pdrv)
{
return ((ArduinoFDC.haveDisk() ? 0 : (STA_NODISK|STA_NOINIT)) |
(ArduinoFDC.isWriteProtected() ? STA_PROTECT : 0));
}
DRESULT disk_read(BYTE pdrv, BYTE *buf, DWORD sec, UINT count)
{
DRESULT res = RES_OK;
byte numsec = ArduinoFDC.numSectors();
if( count!=1 || sec>2*numsec*ArduinoFDC.numTracks() )
res = RES_PARERR;
else
{
byte head = 0;
byte track = sec / (numsec*2);
byte sector = sec % (numsec*2);
if( sector >= numsec ) { head = 1; sector -= numsec; }
byte r = S_NOHEADER, retry = 5;
while( retry>0 && r!=S_OK ) { r = ArduinoFDC.readSector(track, head, sector+1, buf); retry--; }
if( r==S_OK )
{
memmove(buf, buf+1, 512);
res = RES_OK;
}
else if( r==S_NOTREADY )
res = RES_NOTRDY;
else
res = RES_ERROR;
}
return res;
}
DRESULT disk_write(BYTE pdrv, BYTE *buf, DWORD sec, UINT count)
{
DRESULT res = RES_OK;
byte numsec = ArduinoFDC.numSectors();
if( count!=1 || sec>2*numsec*ArduinoFDC.numTracks() )
res = RES_PARERR;
else
{
byte head = 0;
byte track = sec / (numsec*2);
byte sector = sec % (numsec*2);
if( sector >= numsec ) { head = 1; sector -= numsec; }
memmove(buf+1, buf, 512);
byte r = S_NOHEADER, retry = 3;
while( retry>0 && r!=S_OK ) { r = ArduinoFDC.writeSector(track, head, sector+1, buf, true); retry--; }
memmove(buf, buf+1, 512);
if( r==S_OK )
res = RES_OK;
else if( r==S_NOTREADY )
res = RES_NOTRDY;
else if( r==S_READONLY )
res = RES_WRPRT;
else
res = RES_ERROR;
}
return res;
}
DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void* buff)
{
DRESULT res = RES_ERROR;
switch( cmd )
{
case CTRL_SYNC:
res = RES_OK;
break;
case GET_SECTOR_COUNT:
*((DWORD *) buff) = (DWORD) ArduinoFDC.numSectors() * (DWORD) ArduinoFDC.numTracks() * 2;
res = RES_OK;
break;
case GET_SECTOR_SIZE:
*((DWORD *) buff) = 512;
break;
case GET_BLOCK_SIZE:
*((DWORD *) buff) = 1;
break;
}
return res;
}