forked from Mirror/GodMode9
Code formatting for card_spi.c
This commit is contained in:
parent
84d5f800a9
commit
bef427dfdb
@ -155,10 +155,10 @@ int CardSPIWaitWriteEnd(CardSPIType type) {
|
||||
int res = 0;
|
||||
u64 time_start = timer_start();
|
||||
|
||||
do{
|
||||
do {
|
||||
res = CardSPIWriteRead(type, &cmd, 1, &statusReg, 1, 0, 0);
|
||||
if(res) return res;
|
||||
if(timer_msec(time_start) > 1000) return 1;
|
||||
if (res) return res;
|
||||
if (timer_msec(time_start) > 1000) return 1;
|
||||
} while(statusReg & SPI_FLG_WIP);
|
||||
|
||||
return 0;
|
||||
@ -173,26 +173,26 @@ int CardSPIEnableWriting_regular(CardSPIType type) {
|
||||
u8 cmd = SPI_CMD_WREN, statusReg = 0;
|
||||
int res = CardSPIWriteRead(type, &cmd, 1, NULL, 0, 0, 0);
|
||||
|
||||
if(res) return res;
|
||||
if (res) return res;
|
||||
cmd = SPI_CMD_RDSR;
|
||||
|
||||
do{
|
||||
do {
|
||||
res = CardSPIWriteRead(type, &cmd, 1, &statusReg, 1, 0, 0);
|
||||
if(res) return res;
|
||||
if (res) return res;
|
||||
} while(statusReg & ~SPI_FLG_WEL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CardSPIEnableWriting(CardSPIType type) {
|
||||
if(type == NO_CHIP) return 1;
|
||||
if (type == NO_CHIP) return 1;
|
||||
return type->enableWriting(type);
|
||||
}
|
||||
|
||||
int _SPIWriteTransaction(CardSPIType type, void* cmd, u32 cmdSize, const void* data, u32 dataSize) {
|
||||
int res;
|
||||
if( (res = CardSPIEnableWriting(type)) ) return res;
|
||||
if( (res = CardSPIWriteRead(type, cmd, cmdSize, NULL, 0, (void*) ((u8*) data), dataSize)) ) return res;
|
||||
if ((res = CardSPIEnableWriting(type))) return res;
|
||||
if ((res = CardSPIWriteRead(type, cmd, cmdSize, NULL, 0, (void*) ((u8*) data), dataSize))) return res;
|
||||
return CardSPIWaitWriteEnd(type);
|
||||
}
|
||||
|
||||
@ -202,33 +202,33 @@ int CardSPIReadJEDECIDAndStatusReg(CardSPIType type, u32* id, u8* statusReg) {
|
||||
u8 idbuf[3] = { 0 };
|
||||
u32 id_ = 0;
|
||||
int res = CardSPIWaitWriteEnd(type);
|
||||
if(res) return res;
|
||||
if (res) return res;
|
||||
|
||||
if((res = CardSPIWriteRead(type, &cmd, 1, idbuf, 3, 0, 0))) return res;
|
||||
if ((res = CardSPIWriteRead(type, &cmd, 1, idbuf, 3, 0, 0))) return res;
|
||||
|
||||
id_ = (idbuf[0] << 16) | (idbuf[1] << 8) | idbuf[2];
|
||||
cmd = SPI_CMD_RDSR;
|
||||
|
||||
if((res = CardSPIWriteRead(type, &cmd, 1, ®, 1, 0, 0))) return res;
|
||||
if ((res = CardSPIWriteRead(type, &cmd, 1, ®, 1, 0, 0))) return res;
|
||||
|
||||
if(id) *id = id_;
|
||||
if(statusReg) *statusReg = reg;
|
||||
if (id) *id = id_;
|
||||
if (statusReg) *statusReg = reg;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 CardSPIGetPageSize(CardSPIType type) {
|
||||
if(type == NO_CHIP) return 0;
|
||||
if (type == NO_CHIP) return 0;
|
||||
return type->pageSize;
|
||||
}
|
||||
|
||||
u32 CardSPIGetEraseSize(CardSPIType type) {
|
||||
if(type == NO_CHIP) return 0;
|
||||
if (type == NO_CHIP) return 0;
|
||||
return type->eraseSize;
|
||||
}
|
||||
|
||||
u32 CardSPIGetCapacity(CardSPIType type) {
|
||||
if(type == NO_CHIP) return 0;
|
||||
if (type == NO_CHIP) return 0;
|
||||
return type->capacity;
|
||||
}
|
||||
|
||||
@ -257,11 +257,11 @@ int CardSPIWriteSaveData_24bit_erase_program(CardSPIType type, u32 offset, const
|
||||
int res;
|
||||
|
||||
u8 *newData = NULL;
|
||||
if(offset % eraseSize || size < eraseSize) {
|
||||
if (offset % eraseSize || size < eraseSize) {
|
||||
u32 sectorStart = (offset / eraseSize) * eraseSize;
|
||||
newData = malloc(eraseSize);
|
||||
if(!newData) return 1;
|
||||
if( (res = CardSPIReadSaveData(type, sectorStart, newData, eraseSize)) ) {
|
||||
if (!newData) return 1;
|
||||
if ((res = CardSPIReadSaveData(type, sectorStart, newData, eraseSize))) {
|
||||
free(newData);
|
||||
return res;
|
||||
}
|
||||
@ -270,7 +270,7 @@ int CardSPIWriteSaveData_24bit_erase_program(CardSPIType type, u32 offset, const
|
||||
offset = sectorStart;
|
||||
}
|
||||
|
||||
if( (res = CardSPIEraseSector(type, offset)) ) {
|
||||
if ((res = CardSPIEraseSector(type, offset))) {
|
||||
free(newData);
|
||||
return res;
|
||||
}
|
||||
@ -279,7 +279,7 @@ int CardSPIWriteSaveData_24bit_erase_program(CardSPIType type, u32 offset, const
|
||||
cmd[1] = (u8)(pos >> 16);
|
||||
cmd[2] = (u8)(pos >> 8);
|
||||
cmd[3] = (u8) pos;
|
||||
if( (res = _SPIWriteTransaction(type, cmd, 4, (void*) ((u8*) data - offset + pos), pageSize)) ) {
|
||||
if ((res = _SPIWriteTransaction(type, cmd, 4, (void*) ((u8*) data - offset + pos), pageSize))) {
|
||||
free(newData);
|
||||
return res;
|
||||
}
|
||||
@ -290,17 +290,17 @@ int CardSPIWriteSaveData_24bit_erase_program(CardSPIType type, u32 offset, const
|
||||
}
|
||||
|
||||
int CardSPIWriteSaveData(CardSPIType type, u32 offset, const void* data, u32 size) {
|
||||
if(type == NO_CHIP) return 1;
|
||||
if (type == NO_CHIP) return 1;
|
||||
|
||||
if(size == 0) return 0;
|
||||
if (size == 0) return 0;
|
||||
size = min(size, CardSPIGetCapacity(type) - offset);
|
||||
u32 end = offset + size;
|
||||
u32 pos = offset;
|
||||
u32 writeSize = type->writeSize;
|
||||
if(writeSize == 0) return 0xC8E13404;
|
||||
if (writeSize == 0) return 0xC8E13404;
|
||||
|
||||
int res = CardSPIWaitWriteEnd(type);
|
||||
if(res) return res;
|
||||
if (res) return res;
|
||||
|
||||
while(pos < end) {
|
||||
u32 remaining = end - pos;
|
||||
@ -308,7 +308,7 @@ int CardSPIWriteSaveData(CardSPIType type, u32 offset, const void* data, u32 siz
|
||||
|
||||
u32 dataSize = (remaining < nb) ? remaining : nb;
|
||||
|
||||
if( (res = type->writeSaveData(type, pos, (void*) ((u8*) data - offset + pos), dataSize)) ) return res;
|
||||
if ((res = type->writeSaveData(type, pos, (void*) ((u8*) data - offset + pos), dataSize))) return res;
|
||||
|
||||
pos = ((pos / writeSize) + 1) * writeSize; // truncate
|
||||
}
|
||||
@ -323,18 +323,18 @@ int CardSPIReadSaveData_9bit(CardSPIType type, u32 pos, void* data, u32 size) {
|
||||
u32 end = pos + size;
|
||||
|
||||
u32 read = 0;
|
||||
if(pos < 0x100) {
|
||||
if (pos < 0x100) {
|
||||
u32 len = 0x100 - pos;
|
||||
cmd[0] = SPI_512B_EEPROM_CMD_RDLO;
|
||||
cmd[1] = (u8) pos;
|
||||
|
||||
int res = CardSPIWriteRead(type, cmd, cmdSize, data, len, NULL, 0);
|
||||
if(res) return res;
|
||||
if (res) return res;
|
||||
|
||||
read += len;
|
||||
}
|
||||
|
||||
if(end >= 0x100) {
|
||||
if (end >= 0x100) {
|
||||
u32 len = end - 0x100;
|
||||
|
||||
cmd[0] = SPI_512B_EEPROM_CMD_RDHI;
|
||||
@ -342,7 +342,7 @@ int CardSPIReadSaveData_9bit(CardSPIType type, u32 pos, void* data, u32 size) {
|
||||
|
||||
int res = CardSPIWriteRead(type, cmd, cmdSize, (void*)((u8*)data + read), len, NULL, 0);
|
||||
|
||||
if(res) return res;
|
||||
if (res) return res;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -361,12 +361,12 @@ int CardSPIReadSaveData_24bit(CardSPIType type, u32 offset, void* data, u32 size
|
||||
}
|
||||
|
||||
int CardSPIReadSaveData(CardSPIType type, u32 offset, void* data, u32 size) {
|
||||
if(type == NO_CHIP) return 1;
|
||||
if (type == NO_CHIP) return 1;
|
||||
|
||||
if(size == 0) return 0;
|
||||
if (size == 0) return 0;
|
||||
|
||||
int res = CardSPIWaitWriteEnd(type);
|
||||
if(res) return res;
|
||||
if (res) return res;
|
||||
|
||||
size = (size <= CardSPIGetCapacity(type) - offset) ? size : CardSPIGetCapacity(type) - offset;
|
||||
|
||||
@ -389,14 +389,14 @@ int CardSPIEraseSector_real(CardSPIType type, u32 offset) {
|
||||
u8 cmd[4] = { type->eraseCommand, (u8)(offset >> 16), (u8)(offset >> 8), (u8) offset };
|
||||
|
||||
int res = CardSPIWaitWriteEnd(type);
|
||||
if(res) return res;
|
||||
if (res) return res;
|
||||
|
||||
return _SPIWriteTransaction(type, cmd, 4, NULL, 0);
|
||||
}
|
||||
|
||||
|
||||
int CardSPIEraseSector(CardSPIType type, u32 offset) {
|
||||
if(type == NO_CHIP) return 1;
|
||||
if (type == NO_CHIP) return 1;
|
||||
return type->eraseSector(type, offset);
|
||||
}
|
||||
|
||||
@ -441,12 +441,12 @@ int _SPIIsDataMirrored(CardSPIType type, int size, bool* mirrored) {
|
||||
|
||||
int res;
|
||||
|
||||
if( (res = CardSPIReadSaveData(type, offset0, &buf1, 1)) ) return res;
|
||||
if( (res = CardSPIReadSaveData(type, offset1, &buf2, 1)) ) return res;
|
||||
if ((res = CardSPIReadSaveData(type, offset0, &buf1, 1))) return res;
|
||||
if ((res = CardSPIReadSaveData(type, offset1, &buf2, 1))) return res;
|
||||
buf3=~buf1;
|
||||
if( (res = CardSPIWriteSaveData(type, offset0, &buf3, 1)) ) return res;
|
||||
if( (res = CardSPIReadSaveData(type, offset1, &buf4, 1)) ) return res;
|
||||
if( (res = CardSPIWriteSaveData(type, offset0, &buf1, 1)) ) return res;
|
||||
if ((res = CardSPIWriteSaveData(type, offset0, &buf3, 1))) return res;
|
||||
if ((res = CardSPIReadSaveData(type, offset1, &buf4, 1))) return res;
|
||||
if ((res = CardSPIWriteSaveData(type, offset0, &buf1, 1))) return res;
|
||||
|
||||
*mirrored = buf2 != buf4;
|
||||
return 0;
|
||||
@ -462,7 +462,7 @@ int CardSPIGetCardSPIType(CardSPIType* type, int infrared) {
|
||||
u32 maxTries = (infrared == -1) ? 2 : 1; // note: infrared = -1 fails 1/3 of the time
|
||||
while(tries < maxTries){
|
||||
res = CardSPIReadJEDECIDAndStatusReg(t, &jedec, &sr); // dummy
|
||||
if(res) return res;
|
||||
if (res) return res;
|
||||
|
||||
if ((sr & 0xfd) == 0x00 && (jedec != 0x00ffffff)) { break; }
|
||||
if ((sr & 0xfd) == 0xF0 && (jedec == 0x00ffffff)) { t = EEPROM_512B; break; }
|
||||
@ -472,14 +472,14 @@ int CardSPIGetCardSPIType(CardSPIType* type, int infrared) {
|
||||
t = &FLASH_INFRARED_DUMMY;
|
||||
}
|
||||
|
||||
if(t == EEPROM_512B) { *type = t; return 0; }
|
||||
else if(t == &EEPROM_STD_DUMMY) {
|
||||
if (t == EEPROM_512B) { *type = t; return 0; }
|
||||
else if (t == &EEPROM_STD_DUMMY) {
|
||||
bool mirrored = false;
|
||||
size_t i;
|
||||
|
||||
for(i = 0; i < sizeof(EEPROMTypes) / sizeof(CardSPITypeData) - 1; i++) {
|
||||
if( (res = _SPIIsDataMirrored(t, CardSPIGetCapacity(EEPROMTypes + i), &mirrored)) ) return res;
|
||||
if(mirrored) {
|
||||
if ((res = _SPIIsDataMirrored(t, CardSPIGetCapacity(EEPROMTypes + i), &mirrored))) return res;
|
||||
if (mirrored) {
|
||||
*type = EEPROMTypes + i;
|
||||
return 0;
|
||||
}
|
||||
@ -488,13 +488,13 @@ int CardSPIGetCardSPIType(CardSPIType* type, int infrared) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
else if(t == &FLASH_INFRARED_DUMMY) {
|
||||
else if (t == &FLASH_INFRARED_DUMMY) {
|
||||
size_t i;
|
||||
|
||||
if(infrared == 0) *type = NO_CHIP; // did anything go wrong?
|
||||
if (infrared == 0) *type = NO_CHIP; // did anything go wrong?
|
||||
|
||||
for(i = 0; i < sizeof(FlashInfraredTypes) / sizeof(CardSPITypeData); i++) {
|
||||
if(FlashInfraredTypes[i].jedecId == jedec) {
|
||||
if (FlashInfraredTypes[i].jedecId == jedec) {
|
||||
*type = FlashInfraredTypes + i;
|
||||
return 0;
|
||||
}
|
||||
@ -507,10 +507,10 @@ int CardSPIGetCardSPIType(CardSPIType* type, int infrared) {
|
||||
else {
|
||||
size_t i;
|
||||
|
||||
if(infrared == 1) *type = NO_CHIP; // did anything go wrong?
|
||||
if (infrared == 1) *type = NO_CHIP; // did anything go wrong?
|
||||
|
||||
for(i = 0; i < sizeof(FlashStdTypes) / sizeof(CardSPITypeData); i++) {
|
||||
if(FlashStdTypes[i].jedecId == jedec) {
|
||||
if (FlashStdTypes[i].jedecId == jedec) {
|
||||
*type = FlashStdTypes + i;
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user