forked from ShendoXT/memcarduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemCARDuino.ino
304 lines (254 loc) · 7.77 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
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
/*
MemCARDuino - Arduino PlayStation 1 Memory Card reader
Shendo 2013. - 2023.
Compatible with:
* Arduino Uno, Duemilanove, Diecimila, Nano, Mini, Fio (ATmega168/P or ATmega328/P).
* Arduino Leonardo, Micro (ATmega32U4)
* Arduino Mega 2560
* Espressif ESP8266, ESP32
* Raspberry Pi Pico
*/
#include "Arduino.h"
#include <SPI.h>
//Device Firmware identifier
#define IDENTIFIER "MCDINO" //MemCARDuino
#define VERSION 0x06 //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
#ifndef ICACHE_RAM_ATTR
#define ICACHE_RAM_ATTR
#endif
//Define pins for each known platform
#ifdef ESP8266
#define DataPin 12 //MISO aka Data
#define AttPin 15 //Attention (SS)
#define AckPin 2 //Acknowledge
#elif defined (ESP32)
#define DataPin 19
#define CmndPin 23
#define AttPin 5
#define ClockPin 18
#define AckPin 22
#elif defined (ARDUINO_ARCH_MBED_RP2040)
#define DataPin 16
#define CmndPin 19
#define AttPin 17
#define ClockPin 18
#define AckPin 20
#elif defined (ARDUINO_AVR_MEGA2560)
#define DataPin 50
#define AttPin 53
#define AckPin 2
#else
#define DataPin 12
#define AttPin 10
#define AckPin 2
#endif
volatile int state = HIGH;
bool CompatibleMode = false;
byte ReadData[128];
//Set up pins for communication
void PinSetup()
{
pinMode(AttPin, OUTPUT);
pinMode(AckPin, INPUT_PULLUP);
digitalWrite(AttPin, HIGH);
#ifdef ARDUINO_ARCH_MBED_RP2040
pinMode(ClockPin, OUTPUT);
pinMode(CmndPin, OUTPUT);
digitalWrite(ClockPin, HIGH);
digitalWrite(CmndPin, HIGH);
#else
#if ESP32
SPI.begin(ClockPin, DataPin, CmndPin, -1);
#else
SPI.begin();
#endif
/* Memory Cards on PS1 are accessed at 250Khz but for the compatibility sake
* we will use 125Khz. For higher speeds external pull-ups are recommended.*/
SPI.beginTransaction(SPISettings(125000, LSBFIRST, SPI_MODE3));
#endif
//Enable pullup on MISO Data line
#if defined(__AVR_ATmega32U4__)
/* Arduino Leonardo and Micro do not have exposed ICSP pins
* so we have to enable pullup by referencing the port*/
PORTB = (1<<PB3);
#elif defined(ESP8266)
/* ESP8266 needs to have each pin reconfigured for a specific purpose.
* If we just write pin as INPUT_PULLUP it will lose it's function as MISO line
* Therefore we need to adjust it as MISO with pullup...*/
PIN_PULLUP_EN(PERIPHS_IO_MUX_MTDI_U);
#else
pinMode(DataPin, INPUT_PULLUP);
#endif
/* Set up interrupt for ACK signal from the Memory Card.
*
* Should be FALLING because signal goes LOW for ~5uS during ACK
* but ESP8266 for some reason doesn't register it.
* So since it goes HIGH anyway after that we will use RISING */
attachInterrupt(digitalPinToInterrupt(AckPin), ACK, RISING);
}
//Acknowledge routine
ICACHE_RAM_ATTR void ACK()
{
state = !state;
}
//Software SPI bit bang, for devices without SPI or with SPI issues
#ifdef ARDUINO_ARCH_MBED_RP2040
byte SoftTransfer(byte data)
{
byte outData = 0;
for (int i = 0; i < 8; i++) {
digitalWrite(CmndPin, !!(data & (1 << i)));
//Clock
digitalWrite(ClockPin, LOW);
delayMicroseconds(2);
//Sample input data
outData |= digitalRead(DataPin) << i;
digitalWrite(ClockPin, HIGH);
delayMicroseconds(2);
}
return outData;
}
#endif
//Send a command to PlayStation port using SPI
byte SendCommand(byte CommandByte, int Timeout, int Delay)
{
if(!CompatibleMode) Timeout = 3000;
state = HIGH; //Set high state for ACK signal
//Delay for a bit (values simulating delays between real PS1 and Memory Card)
delayMicroseconds(Delay);
//Send data on the SPI bus
#ifdef ARDUINO_ARCH_MBED_RP2040
/* Raspberry Pi Pico currently has some issues with SPI data corruption.
* So for now we are gonna do some bit banging, Pico has plenty of power to do it in software.*/
byte data = SoftTransfer(CommandByte);
#else
byte data = SPI.transfer(CommandByte);
#endif
//Wait for the ACK signal from the Memory Card
while(state == HIGH)
{
Timeout--;
delayMicroseconds(1);
if(Timeout == 0){
//Timeout reached, card doesn't report ACK properly
CompatibleMode = true;
break;
}
}
return data; //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
digitalWrite(AttPin, LOW);
delayMicroseconds(20);
SendCommand(0x81, 500, 70); //Access Memory Card
SendCommand(0x52, 500, 45); //Send read command
SendCommand(0x00, 500, 45); //Memory Card ID1
SendCommand(0x00, 500, 45); //Memory Card ID2
SendCommand(AddressMSB, 500, 45); //Address MSB
SendCommand(AddressLSB, 500, 45); //Address LSB
SendCommand(0x00, 2800, 45); //Memory Card ACK1
SendCommand(0x00, 2800, 0); //Memory Card ACK2
SendCommand(0x00, 2800, 0); //Confirm MSB
SendCommand(0x00, 2800, 0); //Confirm LSB
//Get 128 byte data from the frame
for (int i = 0; i < 128; i++)
{
Serial.write(SendCommand(0x00, 150, 0));
}
Serial.write(SendCommand(0x00, 500, 0)); //Checksum (MSB xor LSB xor Data)
Serial.write(SendCommand(0x00, 0, 0)); //Memory Card status byte
//Deactivate device
digitalWrite(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;
int DelayCounter = 30;
//Use ACK detection mode by default
CompatibleMode = false;
//Activate device
digitalWrite(AttPin, LOW);
delayMicroseconds(20);
SendCommand(0x81, 300, 45); //Access Memory Card
SendCommand(0x57, 300, 45); //Send write command
SendCommand(0x00, 300, 45); //Memory Card ID1
SendCommand(0x00, 300, 45); //Memory Card ID2
SendCommand(AddressMSB, 300, 45); //Address MSB
SendCommand(AddressLSB, 300, 45); //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, 0);
}
SendCommand(Serial.read(), 200, 0); //Checksum (MSB xor LSB xor Data)
SendCommand(0x00, 200, 0); //Memory Card ACK1
SendCommand(0x00, 200, 0); //Memory Card ACK2
Serial.write(SendCommand(0x00, 0, 0)); //Memory Card status byte
//Deactivate device
digitalWrite(AttPin, HIGH);
}
void setup()
{
//Set up serial communication
Serial.begin(115200);
//Set up pins
PinSetup();
}
void loop()
{
//Listen for commands
if(Serial.available() > 0)
{
switch(Serial.read())
{
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;
}
}
}