V17: Added support for 16bit flashrom slot

This commit is contained in:
sanni 2016-07-10 00:20:21 +02:00 committed by GitHub
parent 6ab6e1caac
commit 3b4796e0f2
2 changed files with 572 additions and 43 deletions

View File

@ -2,8 +2,8 @@
Nintendo Cart Reader for Arduino Mega2560
Author: sanni
Date: 2016-07-04
Version: V16
Date: 2016-07-10
Version: V17
SD lib: https://github.com/greiman/SdFat
LCD lib: https://github.com/adafruit/Adafruit_SSD1306
@ -29,7 +29,7 @@
Snes9x - SuperFX Sram Fix
**********************************************************************************/
char ver[5] = "V16";
char ver[5] = "V17";
/******************************************
Choose Output
@ -102,7 +102,8 @@ SdFile myFile;
#define mode_NPFlash 4
#define mode_NPGame 5
#define mode_GB 6
#define mode_FLASH 7
#define mode_FLASH8 7
#define mode_FLASH16 8
/******************************************
Variables
@ -294,6 +295,11 @@ const char n64MenuItem1[] PROGMEM = "Cart Slot";
const char n64MenuItem2[] PROGMEM = "Controller";
const char* const menuOptionsN64[] PROGMEM = {n64MenuItem1, n64MenuItem2};
// Flash Submenu
const char flashMenuItem1[] PROGMEM = "8bit slot";
const char flashMenuItem2[] PROGMEM = "16bit slot";
const char* const menuOptionsFlash[] PROGMEM = {flashMenuItem1, flashMenuItem2};
void mainMenu() {
// create menu with title and 6 options to choose from
unsigned char modeMenu;
@ -305,7 +311,7 @@ void mainMenu() {
switch (modeMenu)
{
case 0:
// create menu with title and 4 options to choose from
// create menu with title and 2 options to choose from
unsigned char n64Dev;
// Copy menuOptions of of progmem
convertPgm(menuOptionsN64, 2);
@ -352,10 +358,29 @@ void mainMenu() {
break;
case 4:
display_Clear();
display_Update();
setup_Flash();
mode = mode_FLASH;
// create menu with title and 2 options to choose from
unsigned char flashSlot;
// Copy menuOptions of of progmem
convertPgm(menuOptionsFlash, 2);
flashSlot = question_box("Select flashrom slot", menuOptions, 2, 0);
// wait for user choice to come back from the question box menu
switch (flashSlot)
{
case 0:
display_Clear();
display_Update();
setup_Flash8();
mode = mode_FLASH8;
break;
case 1:
display_Clear();
display_Update();
setup_Flash16();
mode = mode_FLASH16;
break;
}
break;
case 5:
@ -1080,8 +1105,11 @@ void loop() {
else if (mode == mode_SNES) {
snesMenu();
}
else if (mode == mode_FLASH) {
flashromMenu();
else if (mode == mode_FLASH8) {
flashromMenu8();
}
else if (mode == mode_FLASH16) {
flashromMenu16();
}
else if (mode == mode_NP) {
npMenu();

View File

@ -16,22 +16,32 @@ unsigned long blank;
/******************************************
Menu
*****************************************/
// Flash menu items
const char flashMenuItem1[] PROGMEM = "Blankcheck";
const char flashMenuItem2[] PROGMEM = "Erase";
const char flashMenuItem3[] PROGMEM = "Read";
const char flashMenuItem4[] PROGMEM = "Write";
const char flashMenuItem5[] PROGMEM = "ID";
const char flashMenuItem6[] PROGMEM = "Print";
const char flashMenuItem7[] PROGMEM = "Reset";
const char* const menuOptionsFLASH[] PROGMEM = {flashMenuItem1, flashMenuItem2, flashMenuItem3, flashMenuItem4, flashMenuItem5, flashMenuItem6, flashMenuItem7};
// 8bit Flash menu items
const char flash8MenuItem1[] PROGMEM = "Blankcheck";
const char flash8MenuItem2[] PROGMEM = "Erase";
const char flash8MenuItem3[] PROGMEM = "Read";
const char flash8MenuItem4[] PROGMEM = "Write";
const char flash8MenuItem5[] PROGMEM = "ID";
const char flash8MenuItem6[] PROGMEM = "Print";
const char flash8MenuItem7[] PROGMEM = "Reset";
const char* const menuOptionsFLASH8[] PROGMEM = {flash8MenuItem1, flash8MenuItem2, flash8MenuItem3, flash8MenuItem4, flash8MenuItem5, flash8MenuItem6, flash8MenuItem7};
void flashromMenu() {
// 16bit Flash menu items
const char flash16MenuItem1[] PROGMEM = "Blankcheck";
const char flash16MenuItem2[] PROGMEM = "Erase";
const char flash16MenuItem3[] PROGMEM = "Read";
const char flash16MenuItem4[] PROGMEM = "Write";
const char flash16MenuItem5[] PROGMEM = "ID";
const char flash16MenuItem6[] PROGMEM = "Print";
const char flash16MenuItem7[] PROGMEM = "Reset";
const char* const menuOptionsFLASH16[] PROGMEM = {flash16MenuItem1, flash16MenuItem2, flash16MenuItem3, flash16MenuItem4, flash16MenuItem5, flash16MenuItem6, flash16MenuItem7};
void flashromMenu8() {
// create menu with title and 7 options to choose from
unsigned char mainMenu;
// Copy menuOptions of of progmem
convertPgm(menuOptionsFLASH, 7);
mainMenu = question_box("Flashrom Writer", menuOptions, 7, 0);
convertPgm(menuOptionsFLASH8, 7);
mainMenu = question_box("Flashrom Writer 8", menuOptions, 7, 0);
// wait for user choice to come back from the question box menu
switch (mainMenu)
@ -82,7 +92,7 @@ void flashromMenu() {
case 3:
filePath[0] = '\0';
sd.chdir("/");
fileBrowser("Select File");
fileBrowser("Select file");
display_Clear();
time = millis();
if (flashromType == 1)
@ -156,10 +166,99 @@ void flashromMenu() {
wait();
}
void flashromMenu16() {
// create menu with title "Flashrom Writer 16" and 7 options to choose from
unsigned char mainMenu;
// Copy menuOptions of of progmem
convertPgm(menuOptionsFLASH16, 7);
mainMenu = question_box("Flashrom Writer 16", menuOptions, 7, 0);
// wait for user choice to come back from the question box menu
switch (mainMenu)
{
case 0:
display_Clear();
println_Msg(F("Blankcheck"));
display_Update();
time = millis();
resetFlash16();
blankcheck16();
break;
case 1:
display_Clear();
println_Msg(F("Erase Flashrom"));
display_Update();
time = millis();
eraseFlash16();
delay(1000);
busyCheck16();
println_Msg(F("Flashrom erased."));
display_Update();
resetFlash16();
break;
case 2:
display_Clear();
time = millis();
resetFlash16();
readFlash16();
break;
case 3:
filePath[0] = '\0';
sd.chdir("/");
fileBrowser("Select file");
display_Clear();
time = millis();
writeFlash16();
delay(100);
resetFlash16();
delay(100);
verifyFlash16();
break;
case 4:
time = 0;
display_Clear();
println_Msg(F("ID Flashrom"));
idFlash16();
println_Msg(flashid);
display_Update();
resetFlash16();
break;
case 5:
time = 0;
display_Clear();
println_Msg(F("Print first 70Bytes"));
display_Update();
resetFlash16();
printFlash16(70);
break;
case 6:
time = 0;
display_Clear();
display_Update();
resetFlash16();
delay(500);
asm volatile (" jmp 0");
break;
}
if (time != 0) {
print_Msg(F("Operation took: "));
print_Msg((millis() - time) / 1000, DEC);
println_Msg("s");
display_Update();
}
wait();
}
/******************************************
Setup
*****************************************/
void setup_Flash() {
void setup_Flash8() {
// Set Address Pins to Output
//A0-A7
DDRF = 0xFF;
@ -237,15 +336,82 @@ idtheflash:
wait();
}
void setup_Flash16() {
// Set Address Pins to Output
//A0-A7
DDRF = 0xFF;
//A8-A15
DDRK = 0xFF;
//A16-A23
DDRL = 0xFF;
// Set Control Pins to Output OE(PH1) BYTE(PH3) WE(PH4) CE(PH6)
DDRH |= (1 << 1) | (1 << 3) | (1 << 4) | (1 << 6);
// Setting OE(PH1) BYTE(PH3) WE(PH4) HIGH
PORTH |= (1 << 1) | (1 << 3) | (1 << 4);
// Setting CE(PH6) LOW
PORTH &= ~(1 << 6);
// Set Data Pins (D0-D15) to Input
DDRC = 0x00;
DDRA = 0x00;
// Disable Internal Pullups
PORTC = 0x00;
PORTA = 0x00;
// ID flash
idFlash16();
resetFlash16();
println_Msg(F("Flashrom Writer 16bit"));
println_Msg(" ");
print_Msg(F("Flash ID: "));
println_Msg(flashid);
if (strcmp(flashid, "C2F1") == 0) {
println_Msg(F("MX29F1610 detected"));
println_Msg(" ");
flashSize = 2097152;
flashromType = 2;
}
else if (strcmp(flashid, "C2F9") == 0) {
println_Msg(F("MX29L3211 detected"));
println_Msg(F("ATTENTION 3.3V"));
flashSize = 4194304;
flashromType = 2;
}
else {
print_Error(F("Unknown flashrom"), true);
println_Msg(" ");
}
println_Msg(" ");
println_Msg(F("Press Button..."));
display_Update();
wait();
}
/******************************************
I/O Functions
*****************************************/
// Switch data pins to read
void dataIn_Flash() {
void dataIn8() {
// Set to Input
DDRC = 0x00;
}
// Switch data pins to write
void dataOut16() {
DDRC = 0xFF;
DDRA = 0xFF;
}
// Switch data pins to read
void dataIn16() {
// Set to Input and activate pull-up resistors
DDRC = 0x00;
DDRA = 0x00;
}
/******************************************
Low level functions
*****************************************/
@ -295,6 +461,53 @@ byte readByte_Flash(unsigned long myAddress) {
return tempByte;
}
void writeWord_Flash(unsigned long myAddress, word myData) {
PORTF = myAddress & 0xFF;
PORTK = (myAddress >> 8) & 0xFF;
PORTL = (myAddress >> 16) & 0xFF;
PORTC = myData;
PORTA = (myData >> 8) & 0xFF;
// Arduino running at 16Mhz -> one nop = 62.5ns
// Wait till output is stable
__asm__("nop\n\t");
// Switch WE(PH4) to LOW
PORTH &= ~(1 << 4);
// Leave WE low for at least 60ns
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Switch WE(PH4) to HIGH
PORTH |= (1 << 4);
// Leave WE high for at least 50ns
__asm__("nop\n\t");
}
word readWord_Flash(unsigned long myAddress) {
PORTF = myAddress & 0xFF;
PORTK = (myAddress >> 8) & 0xFF;
PORTL = (myAddress >> 16) & 0xFF;
// Arduino running at 16Mhz -> one nop = 62.5ns
__asm__("nop\n\t");
// Setting OE(PH1) LOW
PORTH &= ~(1 << 1);
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Read
word tempWord = ( ( PINA & 0xFF ) << 8 ) | ( PINC & 0xFF );
// Setting OE(PH1) HIGH
PORTH |= (1 << 1);
__asm__("nop\n\t");
return tempWord;
}
/******************************************
29F032 flashrom functions
*****************************************/
@ -306,7 +519,7 @@ void resetFlash29F032() {
writeByte_Flash(0x555, 0xf0);
// Set data pins to input again
dataIn_Flash();
dataIn8();
}
void idFlash29F032() {
@ -319,7 +532,7 @@ void idFlash29F032() {
writeByte_Flash(0x555, 0x90);
// Set data pins to input again
dataIn_Flash();
dataIn8();
// Read the two id bytes into a string
sprintf(flashid, "%02X%02X", readByte_Flash(0), readByte_Flash(1));
@ -338,7 +551,7 @@ void eraseFlash29F032() {
writeByte_Flash(0x555, 0x10);
// Set data pins to input again
dataIn_Flash();
dataIn8();
// Read the status register
byte statusReg = readByte_Flash(0);
@ -373,6 +586,7 @@ void writeFlash29F032() {
// Blink led
if (currByte % 2048 == 0)
PORTB ^= (1 << 4);
for (unsigned long c = 0; c < 512; c++) {
// Write command sequence
writeByte_Flash(0x555, 0xaa);
@ -384,7 +598,7 @@ void writeFlash29F032() {
}
}
// Set data pins to input again
dataIn_Flash();
dataIn8();
// Close the file:
myFile.close();
@ -397,7 +611,7 @@ void writeFlash29F032() {
void busyCheck29F032(byte c) {
// Set data pins to input
dataIn_Flash();
dataIn8();
// Setting OE(PH1) CE(PH6)LOW
PORTH &= ~((1 << 1) | (1 << 6));
@ -427,7 +641,7 @@ void resetFlash29F1610() {
writeByte_Flash(0x5555 << 1, 0xf0);
// Set data pins to input again
dataIn_Flash();
dataIn8();
}
void writeFlash29F1610() {
@ -443,13 +657,17 @@ void writeFlash29F1610() {
dataOut();
for (unsigned long currByte = 0; currByte < flashSize; currByte += 128) {
// Fill SDBuffer with 1 page at a time then write it repeat until all bytes are written
// Fill sdBuffer with 1 page at a time then write it repeat until all bytes are written
myFile.read(sdBuffer, 128);
// Blink led
if (currByte % 3072 == 0)
PORTB ^= (1 << 4);
// Check if write is complete
delayMicroseconds(100);
busyCheck29F1610();
// Write command sequence
writeByte_Flash(0x5555 << 1, 0xaa);
writeByte_Flash(0x2aaa << 1, 0x55);
@ -459,14 +677,10 @@ void writeFlash29F1610() {
for (byte c = 0; c < 128; c++) {
writeByte_Flash(currByte + c, sdBuffer[c]);
}
// Check if write is complete
delayMicroseconds(100);
busyCheck29F1610();
}
// Set data pins to input again
dataIn_Flash();
dataIn8();
// Close the file:
myFile.close();
@ -487,7 +701,7 @@ void idFlash29F1610() {
writeByte_Flash(0x5555 << 1, 0x90);
// Set data pins to input again
dataIn_Flash();
dataIn8();
// Read the two id bytes into a string
sprintf(flashid, "%02X%02X", readByte_Flash(0), readByte_Flash(2));
@ -503,7 +717,7 @@ byte readStatusReg() {
writeByte_Flash(0x5555 << 1, 0x70);
// Set data pins to input again
dataIn_Flash();
dataIn8();
// Read the status register
byte statusReg = readByte_Flash(0);
@ -523,13 +737,13 @@ void eraseFlash29F1610() {
writeByte_Flash(0x5555 << 1, 0x10);
// Set data pins to input again
dataIn_Flash();
dataIn8();
}
// Delay between write operations based on status register
void busyCheck29F1610() {
// Set data pins to input
dataIn_Flash();
dataIn8();
// Read the status register
byte statusReg = readByte_Flash(0);
@ -576,7 +790,7 @@ void verifyFlash() {
blank = 0;
for (unsigned long currByte = 0; currByte < flashSize; currByte += 512) {
//fill SDBuffer
//fill sdBuffer
myFile.read(sdBuffer, 512);
for (unsigned long c = 0; c < 512; c++) {
if (readByte_Flash(currByte + c) != sdBuffer[c]) {
@ -657,6 +871,293 @@ void printFlash(int numBytes) {
display_Update();
}
/******************************************
29L3211 16bit flashrom functions
*****************************************/
void resetFlash16() {
// Set data pins to output
dataOut16();
// Reset command sequence
writeWord_Flash(0x5555, 0xaa);
writeWord_Flash(0x2aaa, 0x55);
writeWord_Flash(0x5555, 0xf0);
// Set data pins to input again
dataIn16();
}
void writeFlash16() {
// Create filepath
sprintf(filePath, "%s/%s", filePath, fileName);
println_Msg(F("Flashing file "));
println_Msg(filePath);
display_Update();
// Open file on sd card
if (myFile.open(filePath, O_READ)) {
// Set data pins to output
dataOut16();
// Fill sdBuffer with 1 page at a time then write it repeat until all bytes are written
int d = 0;
for (unsigned long currByte = 0; currByte < flashSize / 2; currByte += 64) {
myFile.read(sdBuffer, 128);
// Blink led
if (currByte % 2048 == 0)
PORTB ^= (1 << 4);
// Check if write is complete
delayMicroseconds(100);
busyCheck16();
// Write command sequence
writeWord_Flash(0x5555, 0xaa);
writeWord_Flash(0x2aaa, 0x55);
writeWord_Flash(0x5555, 0xa0);
// Write one full page at a time
for (byte c = 0; c < 64; c++) {
word currWord = ((sdBuffer[d + 1] << 8) | sdBuffer[d]);
writeWord_Flash(currByte + c, currWord);
d += 2;
}
d = 0;
}
// Set data pins to input again
dataIn16();
// Close the file:
myFile.close();
}
else {
println_Msg(F("Can't open file on SD."));
display_Update();
}
}
void idFlash16() {
// Set data pins to output
dataOut16();
// ID command sequence
writeWord_Flash(0x5555, 0xaa);
writeWord_Flash(0x2aaa, 0x55);
writeWord_Flash(0x5555, 0x90);
// Set data pins to input again
dataIn16();
// Read the two id bytes into a string
sprintf(flashid, "%02X%02X", readWord_Flash(0) & 0xFF, readWord_Flash(1) & 0xFF);
}
byte readStatusReg16() {
// Set data pins to output
dataOut16();
// Status reg command sequence
writeWord_Flash(0x5555, 0xaa);
writeWord_Flash(0x2aaa, 0x55);
writeWord_Flash(0x5555, 0x70);
// Set data pins to input again
dataIn16();
// Read the status register
byte statusReg = readWord_Flash(0);
return statusReg;
}
void eraseFlash16() {
// Set data pins to output
dataOut16();
// Erase command sequence
writeWord_Flash(0x5555, 0xaa);
writeWord_Flash(0x2aaa, 0x55);
writeWord_Flash(0x5555, 0x80);
writeWord_Flash(0x5555, 0xaa);
writeWord_Flash(0x2aaa, 0x55);
writeWord_Flash(0x5555, 0x10);
// Set data pins to input again
dataIn16();
}
void blankcheck16() {
println_Msg(F("Please wait..."));
display_Update();
blank = 1;
for (unsigned long currByte = 0; currByte < flashSize / 2; currByte++) {
if (readWord_Flash(currByte) != 0xFFFF) {
currByte = flashSize / 2;
blank = 0;
}
}
if (blank) {
println_Msg(F("Flashrom is empty."));
display_Update();
}
else {
println_Msg(F("Error: Not blank!"));
display_Update();
}
}
void verifyFlash16() {
println_Msg(F("Verifying against"));
println_Msg(filePath);
display_Update();
// Open file on sd card
if (myFile.open(filePath, O_READ)) {
blank = 0;
word d = 0;
for (unsigned long currByte = 0; currByte < flashSize / 2; currByte += 256) {
//fill sdBuffer
myFile.read(sdBuffer, 512);
for (unsigned long c = 0; c < 256; c++) {
word currWord = ((sdBuffer[d] << 8) | sdBuffer[d + 1]);
// Swap bytes in word
currWord = ((currWord >> 8) & 0x00FF00FF) | ((currWord & 0x00FF00FF) << 8);
if (readWord_Flash(currByte + c) != currWord) {
blank++;
}
d += 2;
}
d = 0;
}
if (blank == 0) {
println_Msg(F("Flashrom verified OK"));
display_Update();
}
else {
println_Msg(F("Verification ERROR!"));
print_Msg(blank);
print_Error(F("B did not verify."), false);
display_Update();
}
// Close the file:
myFile.close();
}
else {
println_Msg(F("Can't open file on SD."));
display_Update();
}
}
void readFlash16() {
// Reset to root directory
sd.chdir("/");
// Get name, add extension and convert to char array for sd lib
EEPROM_readAnything(0, foldern);
sd.mkdir("FLASH", true);
sd.chdir("FLASH");
sprintf(fileName, "FL%d", foldern);
strcat(fileName, ".bin");
// write new folder number back to eeprom
foldern = foldern + 1;
EEPROM_writeAnything(0, foldern);
display_Clear();
print_Msg(F("Saving as "));
print_Msg(fileName);
println_Msg(F("..."));
display_Update();
// Open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
println_Msg(F("Can't create file on SD."));
display_Update();
while (1);
}
word d = 0;
for (unsigned long currByte = 0; currByte < flashSize / 2; currByte += 256) {
for (word c = 0; c < 256; c++) {
word currWord = readWord_Flash(currByte + c);
// Swap bytes in word
currWord = ((currWord >> 8) & 0x00FF00FF) | ((currWord & 0x00FF00FF) << 8);
// Split word into two bytes
// Left
sdBuffer[d] = (( currWord >> 8 ) & 0xFF);
// Right
sdBuffer[d + 1] = (currWord & 0xFF);
d += 2;
}
myFile.write(sdBuffer, 512);
d = 0;
}
// Close the file:
myFile.close();
println_Msg(F("Finished reading."));
display_Update();
}
void printFlash16(int numBytes) {
/*
right_byte = short_val & 0xFF;
left_byte = ( short_val >> 8 ) & 0xFF
short_val = ( ( left_byte & 0xFF ) << 8 ) | ( right_byte & 0xFF );
*/
char buffer[3];
for (int currByte = 0; currByte < numBytes / 2; currByte += 5) {
for (int c = 0; c < 5; c++) {
word currWord = readWord_Flash(currByte + c);
// Swap bytes in word
currWord = ((currWord >> 8) & 0x00FF00FF) | ((currWord & 0x00FF00FF) << 8);
// Split word into two bytes
byte right_byte = currWord & 0xFF;
byte left_byte = ( currWord >> 8 ) & 0xFF;
itoa (left_byte, buffer, 16);
for (int i = 0; i < 2 - strlen(buffer); i++) {
print_Msg('0');
}
// Now print the significant bits
print_Msg(buffer);
itoa (right_byte, buffer, 16);
for (int i = 0; i < 2 - strlen(buffer); i++) {
print_Msg('0');
}
// Now print the significant bits
print_Msg(buffer);
}
println_Msg("");
}
display_Update();
}
// Delay between write operations based on status register
void busyCheck16() {
// Set data pins to input
dataIn16();
// Read the status register
word statusReg = readWord_Flash(0);
while ((statusReg | 0xFF7F) != 0xFFFF) {
statusReg = readWord_Flash(0);
}
// Set data pins to output
dataOut16();
}
//******************************************
// End of File
//******************************************