forked from ShendoXT/memcarduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemCARDuino.ino
executable file
·222 lines (180 loc) · 5.53 KB
/
MemCARDuino.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
/*
MemCARDuino - Arduino PlayStation 1 Memory Card reader
Shendo 2013. - 2016.
Compatible with Arduino/Genuino Uno, Duemilanove, Diecimila, Nano, Mini,
basically any board with ATmega168/P or ATmega328/P.
*/
#include "Arduino.h"
//Device Firmware identifier
#define IDENTIFIER "MCDINO" //MemCARDuino
#define VERSION 0x04 //Firmware version byte (Major.Minor)
//Commands
#define GETID 0xA0 //Get identifier
#define GETVER 0xA1 //Get firmware version
#define MCREAD 0xA2 //Memory Card Read (frame)
#define MCWRITE 0xA3 //Memory Card Write (frame)
//Responses
#define ERROR 0xE0 //Invalid command received (error)
//Memory Card Responses
//0x47 - Good
//0x4E - BadChecksum
//0xFF - BadSector
//Define pins
#define DataPin 12 //Data
#define CmdPin 11 //Command
#define AttPin 10 //Attention (Select)
#define ClockPin 13 //Clock
#define AckPin 2 //Acknowledge
byte ReadByte = 0;
volatile int state = HIGH;
bool CompatibleMode = false;
//Set up pins for communication
void PinSetup()
{
byte clr = 0;
pinMode(DataPin, INPUT);
pinMode(CmdPin, OUTPUT);
pinMode(AttPin, OUTPUT);
pinMode(ClockPin, OUTPUT);
pinMode(AckPin, INPUT);
//Set up SPI on Arduino (250 kHz, clock active when low, reading on falling edge of the clock)
SPCR = 0x7F;
clr=SPSR;
clr=SPDR;
digitalWrite(DataPin, HIGH); //Activate pullup resistor
digitalWrite(CmdPin, LOW);
digitalWrite(AttPin, HIGH);
digitalWrite(ClockPin, HIGH);
digitalWrite(AckPin, HIGH); //Activate pullup resistor
//Set up interrupt on pin 2 (INT.0) for Acknowledge signal
attachInterrupt(0, ACK, FALLING);
}
//Acknowledge routine
void ACK()
{
state = !state;
}
//Send a command to PlayStation port using SPI
byte SendCommand(byte CommandByte, int Delay)
{
if(!CompatibleMode) Delay = 3000; //Timeout
state = HIGH; //Set high state for ACK signal
SPDR = CommandByte; //Start the transmission
while (!(SPSR & (1<<SPIF))); //Wait for the end of the transmission
//Wait for the ACK signal from the Memory Card
while(state == HIGH)
{
Delay--;
delayMicroseconds(1);
if(Delay == 0){
//Timeout reached, card doesn't report ACK properly
CompatibleMode = true;
break;
}
}
return SPDR; //Return the received byte
}
//Read a frame from Memory Card and send it to serial port
void ReadFrame(unsigned int Address)
{
byte AddressMSB = Address & 0xFF;
byte AddressLSB = (Address >> 8) & 0xFF;
//Use ACK detection mode by default
CompatibleMode = false;
//Activate device
PORTB &= 0xFB; //Set pin 10 (AttPin, LOW)
SendCommand(0x81, 500); //Access Memory Card
SendCommand(0x52, 500); //Send read command
SendCommand(0x00, 500); //Memory Card ID1
SendCommand(0x00, 500); //Memory Card ID2
SendCommand(AddressMSB, 500); //Address MSB
SendCommand(AddressLSB, 500); //Address LSB
SendCommand(0x00, 2800); //Memory Card ACK1
SendCommand(0x00, 2800); //Memory Card ACK2
SendCommand(0x00, 2800); //Confirm MSB
SendCommand(0x00, 2800); //Confirm LSB
//Get 128 byte data from the frame
for (int i = 0; i < 128; i++)
{
Serial.write(SendCommand(0x00, 150));
}
Serial.write(SendCommand(0x00, 500)); //Checksum (MSB xor LSB xor Data)
Serial.write(SendCommand(0x00, 500)); //Memory Card status byte
//Deactivate device
PORTB |= 4; //Set pin 10 (AttPin, HIGH)
}
//Write a frame from the serial port to the Memory Card
void WriteFrame(unsigned int Address)
{
byte AddressMSB = Address & 0xFF;
byte AddressLSB = (Address >> 8) & 0xFF;
byte ReadData[128];
int DelayCounter = 30;
//Use ACK detection mode by default
CompatibleMode = false;
//Activate device
PORTB &= 0xFB; //Set pin 10 (AttPin, LOW)
SendCommand(0x81, 300); //Access Memory Card
SendCommand(0x57, 300); //Send write command
SendCommand(0x00, 300); //Memory Card ID1
SendCommand(0x00, 300); //Memory Card ID2
SendCommand(AddressMSB, 300); //Address MSB
SendCommand(AddressLSB, 300); //Address LSB
//Copy 128 bytes from the serial input
for (int i = 0; i < 128; i++)
{
while(!Serial.available())
{
DelayCounter--;
if(DelayCounter == 0)return; //If there is no response for 30ms stop writing (prevents lock on MemCARDuino)
delay(1);
}
ReadData[i] = Serial.read();
}
//Write 128 byte data to the frame
for (int i = 0; i < 128; i++)
{
SendCommand(ReadData[i], 150);
}
SendCommand(Serial.read(), 200); //Checksum (MSB xor LSB xor Data)
SendCommand(0x00, 200); //Memory Card ACK1
SendCommand(0x00, 200); //Memory Card ACK2
Serial.write(SendCommand(0x00, 200)); //Memory Card status byte
//Deactivate device
PORTB |= 4; //Set pin 10 (AttPin, HIGH)
}
void setup()
{
//Set up serial communication
Serial.begin(38400);
//Set up pins
PinSetup();
}
void loop()
{
//Listen for commands
if(Serial.available() > 0)
{
ReadByte = Serial.read();
switch(ReadByte)
{
default:
Serial.write(ERROR);
break;
case GETID:
Serial.write(IDENTIFIER);
break;
case GETVER:
Serial.write(VERSION);
break;
case MCREAD:
delay(5);
ReadFrame(Serial.read() | Serial.read() << 8);
break;
case MCWRITE:
delay(5);
WriteFrame(Serial.read() | Serial.read() << 8);
break;
}
}
}