Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FreescaleIAP SimpleDMA eeprom mbed-rtos mbed
Fork of CDMS_QM_03MAR2017_Flash_with_obsrs by
Diff: cdms_sd.h
- Revision:
- 214:6848a51af734
- Parent:
- 209:63e9c8f8b5d2
- Child:
- 223:7c802e1ccd30
--- a/cdms_sd.h Sat Jul 02 15:28:21 2016 +0000
+++ b/cdms_sd.h Sun Jul 03 09:33:33 2016 +0000
@@ -1,4 +1,4 @@
-//SPI spi(PTE1, PTE3, PTE2); // MOSI,MISO, CLOCK microcontroller(in order)
+//SPI spi(PTE1, PTE3, PTE2); // MOSI,MISO, CLOCK microcontroller(in order)
//DigitalOut cs_sd(PTE22);
//Serial sd1(USBTX,USBRX);
@@ -67,7 +67,7 @@
uint64_t sectors;
int FCTN_CDMS_SD_INIT()
-{
+{
int i = initialise_card();
debug_if(SD_DBG, "init card = %d\n", i);
sectors = sd_sectors();
@@ -77,7 +77,7 @@
debug("\rSet 512-byte block timed out\r\n");
return 1;
} else {
- //printf("\rDisk initialization successfull\r\n");
+ //printf("\rDisk initialization successfull\r\n");
}
spi.frequency(1000000); // Set to 1MHz for data transfer
@@ -90,7 +90,7 @@
uint32_t start_fsc;
uint8_t buffer[512];
int b=disk_read(buffer, SD_MNG_SECT);
-
+
fsc=(uint32_t)(buffer[0]<<24)+(uint32_t)(buffer[1]<<16)+(uint32_t)(buffer[2]<<8)+(uint32_t)buffer[3];
start_fsc=(uint32_t)(buffer[4]<<24)+(uint32_t)(buffer[5]<<16)+(uint32_t)(buffer[6]<<8)+(uint32_t)buffer[7];
FSC_CURRENT[1] = fsc;
@@ -126,8 +126,7 @@
SD_MNG_SECT += SD_LIB_WRITES/(int)0xFFFF;
SD_LIB_WRITES = SD_LIB_WRITES%(int)0xFFFF;
disk_read(buffer,SD_MNG_SECT);
- if(sid==0x01)
- {
+ if(sid==0x01) {
fsc=(uint32_t)(buffer[0]<<24)+(uint32_t)(buffer[1]<<16)+(uint32_t)(buffer[2]<<8)+(uint32_t)buffer[3];
start_fsc=(uint32_t)(buffer[4]<<24)+(uint32_t)(buffer[5]<<16)+(uint32_t)(buffer[6]<<8)+(uint32_t)buffer[7];
fsc++;
@@ -135,25 +134,22 @@
buffer[1]=(uint8_t) (fsc>>16 & 0xFF);
buffer[2]=(uint8_t) (fsc>>8 & 0xFF);
buffer[3]=(uint8_t) (fsc & 0xFF);
- if(fsc > SD_SCP_LAST-SD_SCP_FIRST+1)
- {
+ if(fsc > SD_SCP_LAST-SD_SCP_FIRST+1) {
start_fsc = start_fsc+1;
buffer[4]=(uint8_t) (start_fsc>>24 & 0xFF);
buffer[5]=(uint8_t) (start_fsc>>16 & 0xFF);
buffer[6]=(uint8_t) (start_fsc>>8 & 0xFF);
buffer[7]=(uint8_t) (start_fsc & 0xFF);
}
-
+
i = disk_write(buffer,SD_MNG_SECT);
- if(i == 0)
- {
+ if(i == 0) {
FSC_CURRENT[1] = fsc;
FSC_LAST[1] = start_fsc;
return i;
- }
+ }
}
- if(sid==0x02)
- {
+ if(sid==0x02) {
fsc=(uint32_t)(buffer[8]<<24)+(uint32_t)(buffer[9]<<16)+(uint32_t)(buffer[10]<<8)+(uint32_t)buffer[11];
start_fsc=(uint32_t)(buffer[12]<<24)+(uint32_t)(buffer[13]<<16)+(uint32_t)(buffer[14]<<8)+(uint32_t)buffer[15];
fsc++;
@@ -161,8 +157,7 @@
buffer[9]=(uint8_t) (fsc>>16 & 0xFF);
buffer[10]=(uint8_t) (fsc>>8 & 0xFF);
buffer[11]=(uint8_t) (fsc & 0xFF);
- if(fsc > SD_SFF_AT_LAST-SD_SFF_AT_FIRST+1)
- {
+ if(fsc > SD_SFF_AT_LAST-SD_SFF_AT_FIRST+1) {
start_fsc = start_fsc+1;
buffer[12]=(uint8_t) (start_fsc>>24 & 0xFF);
buffer[13]=(uint8_t) (start_fsc>>16 & 0xFF);
@@ -170,15 +165,13 @@
buffer[15]=(uint8_t) (start_fsc & 0xFF);
}
i = disk_write(buffer,SD_MNG_SECT);
- if(i == 0)
- {
+ if(i == 0) {
FSC_CURRENT[2] = fsc;
FSC_LAST[2] = start_fsc;
return i;
}
}
- if(sid==0x03)
- {
+ if(sid==0x03) {
fsc=(uint32_t)(buffer[16]<<24)+(uint32_t)(buffer[17]<<16)+(uint32_t)(buffer[18]<<8)+(uint32_t)buffer[19];
start_fsc=(uint32_t)(buffer[20]<<24)+(uint32_t)(buffer[21]<<16)+(uint32_t)(buffer[22]<<8)+(uint32_t)buffer[23];
fsc++;
@@ -186,8 +179,7 @@
buffer[17]=(uint8_t) (fsc>>16 & 0xFF);
buffer[18]=(uint8_t) (fsc>>8 & 0xFF);
buffer[19]=(uint8_t) (fsc & 0xFF);
- if(fsc > SD_SFF_BT_LAST-SD_SFF_BT_FIRST+1)
- {
+ if(fsc > SD_SFF_BT_LAST-SD_SFF_BT_FIRST+1) {
start_fsc = start_fsc+1;
buffer[20]=(uint8_t) (start_fsc>>24 & 0xFF);
buffer[21]=(uint8_t) (start_fsc>>16 & 0xFF);
@@ -195,15 +187,13 @@
buffer[23]=(uint8_t) (start_fsc & 0xFF);
}
i = disk_write(buffer,SD_MNG_SECT);
- if(i == 0)
- {
+ if(i == 0) {
FSC_CURRENT[3] = fsc;
FSC_LAST[3] = start_fsc;
return i;
}
}
- if(sid==0x04)
- {
+ if(sid==0x04) {
fsc=(uint32_t)(buffer[24]<<24)+(uint32_t)(buffer[25]<<16)+(uint32_t)(buffer[26]<<8)+(uint32_t)buffer[27];
start_fsc=(uint32_t)(buffer[28]<<24)+(uint32_t)(buffer[29]<<16)+(uint32_t)(buffer[30]<<8)+(uint32_t)buffer[31];
fsc++;
@@ -211,8 +201,7 @@
buffer[25]=(uint8_t) (fsc>>16 & 0xFF);
buffer[26]=(uint8_t) (fsc>>8 & 0xFF);
buffer[27]=(uint8_t) (fsc & 0xFF);
- if(fsc > SD_HK_ARCH_LAST-SD_HK_ARCH_FIRST+1)
- {
+ if(fsc > SD_HK_ARCH_LAST-SD_HK_ARCH_FIRST+1) {
start_fsc = start_fsc+1;
buffer[28]=(uint8_t) (start_fsc>>24 & 0xFF);
buffer[29]=(uint8_t) (start_fsc>>16 & 0xFF);
@@ -220,15 +209,13 @@
buffer[31]=(uint8_t) (start_fsc & 0xFF);
}
i = disk_write(buffer,SD_MNG_SECT);
- if(i == 0)
- {
+ if(i == 0) {
FSC_CURRENT[4] = fsc;
FSC_LAST[4] = start_fsc;
return i;
}
}
- if(sid==0x05)
- {
+ if(sid==0x05) {
fsc=(uint32_t)(buffer[32]<<24)+(uint32_t)(buffer[33]<<16)+(uint32_t)(buffer[34]<<8)+(uint32_t)buffer[35];
start_fsc=(uint32_t)(buffer[36]<<24)+(uint32_t)(buffer[37]<<16)+(uint32_t)(buffer[38]<<8)+(uint32_t)buffer[39];
fsc++;
@@ -236,8 +223,7 @@
buffer[33]=(uint8_t) (fsc>>16 & 0xFF);
buffer[34]=(uint8_t) (fsc>>8 & 0xFF);
buffer[35]=(uint8_t) (fsc & 0xFF);
- if(fsc > LOG_LAST-LOG_FIRST+1)
- {
+ if(fsc > LOG_LAST-LOG_FIRST+1) {
start_fsc = start_fsc+1;
buffer[36]=(uint8_t) (start_fsc>>24 & 0xFF);
buffer[37]=(uint8_t) (start_fsc>>16 & 0xFF);
@@ -245,8 +231,7 @@
buffer[39]=(uint8_t) (start_fsc & 0xFF);
}
i = disk_write(buffer,SD_MNG_SECT);
- if(i == 0)
- {
+ if(i == 0) {
FSC_CURRENT[5] = fsc;
FSC_LAST[5] = start_fsc;
return i;
@@ -260,61 +245,51 @@
{
uint32_t block_number;
int result = 10;
- if(sid==0x01)
- {
+ if(sid==0x01) {
//block_number=SD_SCP_FIRST+(fsc%(SD_SCP_LAST-SD_SCP_FIRST+1))-1;
block_number=SD_SCP_FIRST+fsc;
result= disk_write(buffer,block_number);
- if(result == 0)
- {
+ if(result == 0) {
if(INCREMENT_SD_LIB(sid) == 0)
SD_LIB_WRITES++;
}
return result;
}
- if(sid==0x02)
- {
+ if(sid==0x02) {
//block_number= SD_SFF_AT_FIRST+(fsc%(SD_SFF_AT_LAST - SD_SFF_AT_FIRST+1))-1;
block_number= SD_SFF_AT_FIRST+fsc;
result= disk_write(buffer,block_number);
- if(result == 0)
- {
+ if(result == 0) {
if(INCREMENT_SD_LIB(sid) == 0)
SD_LIB_WRITES++;
}
return result;
}
- if(sid==0x03)
- {
+ if(sid==0x03) {
//block_number= SD_SFF_BT_FIRST +(fsc%(SD_SFF_BT_LAST - SD_SFF_BT_FIRST +1))-1;
block_number= SD_SFF_BT_FIRST +fsc;
result= disk_write(buffer,block_number);
- if(result == 0)
- {
+ if(result == 0) {
if(INCREMENT_SD_LIB(sid) == 0)
SD_LIB_WRITES++;
}
return result;
}
- if(sid==0x04)
- {
+ if(sid==0x04) {
//block_number=SD_HK_ARCH_FIRST +(fsc%(SD_HK_ARCH_LAST - SD_HK_ARCH_FIRST +1))-1;
block_number=SD_HK_ARCH_FIRST +fsc;
result= disk_write(buffer,block_number);
- if(result == 0)
- {
+ if(result == 0) {
if(INCREMENT_SD_LIB(sid) == 0)
SD_LIB_WRITES++;
}
return result;
}
- if(sid==0x05)
- {
+ if(sid==0x05) {
//block_number= LOG_FIRST +(fsc%(LOG_FIRST - LOG_FIRST +1))-1;
block_number= LOG_FIRST +fsc;
result= disk_write(buffer,block_number);
- if(result == 0)
- {
+ if(result == 0) {
if(INCREMENT_SD_LIB(sid) == 0)
SD_LIB_WRITES++;
}
@@ -330,48 +305,37 @@
int result;
if(SD_SW_EN_DS == 1)
return 0x89;
- if(sid==0x01)
- {
- if(!(FSC_LAST[1]<=fsc && fsc<=FSC_CURRENT[1])){
+ if(sid==0x01) {
+ if(!(FSC_LAST[1]<=fsc && fsc<=FSC_CURRENT[1])) {
return 0x86;
}
block_number=SD_SCP_FIRST + fsc;
result= disk_read(buffer,block_number);
- }
- else if(sid==0x02)
- {
- if(!(FSC_LAST[2]<=fsc && fsc<=FSC_CURRENT[2])){
+ } else if(sid==0x02) {
+ if(!(FSC_LAST[2]<=fsc && fsc<=FSC_CURRENT[2])) {
return 0x86;
}
block_number=SD_SFF_AT_FIRST + fsc;
result= disk_read(buffer,block_number);
- }
- else if(sid==0x03)
- {
- if(!(FSC_LAST[3]<=fsc && fsc<=FSC_CURRENT[3])){
+ } else if(sid==0x03) {
+ if(!(FSC_LAST[3]<=fsc && fsc<=FSC_CURRENT[3])) {
return 0x86;
}
block_number=SD_SFF_BT_FIRST + fsc;
result= disk_read(buffer,block_number);
- }
- else if(sid==0x04)
- {
- if(!(FSC_LAST[4]<=fsc && fsc<=FSC_CURRENT[4])){
+ } else if(sid==0x04) {
+ if(!(FSC_LAST[4]<=fsc && fsc<=FSC_CURRENT[4])) {
return 0x86;
}
block_number=SD_HK_ARCH_FIRST + fsc;
result= disk_read(buffer,block_number);
- }
- else if(sid==0x05)
- {
- if(!(FSC_LAST[5]<=fsc && fsc<=FSC_CURRENT[5])){
+ } else if(sid==0x05) {
+ if(!(FSC_LAST[5]<=fsc && fsc<=FSC_CURRENT[5])) {
return 0x86;
}
block_number=LOG_FIRST +fsc;
result= disk_read(buffer,block_number);
- }
- else
- {
+ } else {
return 0x02;
}
if(result == 0)
@@ -384,7 +348,7 @@
int initialise_card()
{
// Set to 100kHz for initialisation, and clock card with cs_sd = 1
- spi.frequency(100000); // changed on 31 12 2015 to 1 MHz
+ spi.frequency(100000); // changed on 31 12 2015 to 1 MHz
cs_sd = 1;
for (int i = 0; i < 16; i++) {
spi.write(0xFF);
@@ -395,9 +359,8 @@
if (R1_response != R1_IDLE_STATE) {
debug("No disk, or could not put SD card in to spi idle state\r\n");
return SDCARD_FAIL;
- }
- else
- gPC.puts("SD Card is in IDLE state\n\r");
+ } else
+ gPC.puts("SD Card is in IDLE state\n\r");
// send CMD8 to determine whther it is ver 2.x
int r = cmd8();
@@ -643,13 +606,10 @@
if (cmd(24, block_number * cdv) != 0) {
return 1;
}
-
- uint64_t temp;
+
int r = write(buffer, 512);
- if(r == 0 ){
- temp = FCTN_CDMS_RD_RTC();
- TIME_LATEST_SD_RD = temp >> 7;
- }
+ if(r == 0 )
+ TIME_LATEST_SD_WR = FCTN_CDMS_RD_RTC() >> 7;
return r;
}
@@ -694,8 +654,7 @@
// receive the data
read(buffer, 512);
- uint64_t temp = FCTN_CDMS_RD_RTC();
- TIME_LATEST_SD_RD = temp >> 7;
+ TIME_LATEST_SD_RD = FCTN_CDMS_RD_RTC() >> 7;
return 0;
}
@@ -729,14 +688,14 @@
if (cmd(38,0) != 0) {
return 1;
}
-
+
return 0; //normal return
}
int disk_read_statusbits(uint8_t *buffer)
{
if (cmd(17, 0) != 0) {
- SD_RD_ERROR = 1;
+ SD_RD_ERROR = 1;
return -1;
}
