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:
- 209:63e9c8f8b5d2
- Parent:
- 203:424308159a56
- Child:
- 214:6848a51af734
- Child:
- 248:407ab7e337cd
diff -r d2ac4580960d -r 63e9c8f8b5d2 cdms_sd.h
--- a/cdms_sd.h Sat Jul 02 14:25:11 2016 +0000
+++ b/cdms_sd.h Sat Jul 02 14:54:52 2016 +0000
@@ -1,6 +1,3 @@
-
-
-
//SPI spi(PTE1, PTE3, PTE2); // MOSI,MISO, CLOCK microcontroller(in order)
//DigitalOut cs_sd(PTE22);
@@ -34,6 +31,7 @@
uint32_t LOG_FIRST =5001;
uint32_t LOG_LAST=6000;
uint32_t SD_MNG_SECT=7000;
+uint16_t SD_LIB_WRITES = 0;
extern uint8_t SD_INIT_FLAGS;
@@ -43,8 +41,9 @@
int disk_write(const uint8_t *, uint64_t);
int disk_read(uint8_t *, uint64_t);
int disk_erase(int,int);
+int disk_read_statusbits(uint8_t *);
-uint32_t FCTN_SD_MNG(uint8_t);
+void FCTN_SD_MNG();
int INCREMENT_SD_LIB(uint8_t);
@@ -63,7 +62,6 @@
#define SDCARD_V2 2
#define SDCARD_V2HC 3
-
int cdv;
uint64_t sd_sectors();
uint64_t sectors;
@@ -86,9 +84,7 @@
return 0;
}
-
-
-void FCTN_SD_MNGR(void)
+void FCTN_SD_MNGR()
{
uint32_t fsc;
uint32_t start_fsc;
@@ -99,7 +95,7 @@
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;
FSC_LAST[1] = start_fsc;
-
+
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_CURRENT[2] = fsc;
@@ -109,12 +105,12 @@
start_fsc=(uint32_t)(buffer[20]<<24)+(uint32_t)(buffer[21]<<16)+(uint32_t)(buffer[22]<<8)+(uint32_t)buffer[23];
FSC_CURRENT[3] = fsc;
FSC_LAST[3] = start_fsc;
-
+
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_CURRENT[4] = fsc;
FSC_LAST[4] = start_fsc;
-
+
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_CURRENT[5] = fsc;
@@ -125,8 +121,10 @@
{
uint32_t fsc;
uint32_t start_fsc;
+ int i;
uint8_t buffer[512];
- int i;
+ 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)
{
@@ -145,9 +143,14 @@
buffer[6]=(uint8_t) (start_fsc>>8 & 0xFF);
buffer[7]=(uint8_t) (start_fsc & 0xFF);
}
+
i = disk_write(buffer,SD_MNG_SECT);
- FCTN_SD_MNGR();
- return i;
+ if(i == 0)
+ {
+ FSC_CURRENT[1] = fsc;
+ FSC_LAST[1] = start_fsc;
+ return i;
+ }
}
if(sid==0x02)
{
@@ -167,8 +170,12 @@
buffer[15]=(uint8_t) (start_fsc & 0xFF);
}
i = disk_write(buffer,SD_MNG_SECT);
- FCTN_SD_MNGR();
- return i;
+ if(i == 0)
+ {
+ FSC_CURRENT[2] = fsc;
+ FSC_LAST[2] = start_fsc;
+ return i;
+ }
}
if(sid==0x03)
{
@@ -188,8 +195,12 @@
buffer[23]=(uint8_t) (start_fsc & 0xFF);
}
i = disk_write(buffer,SD_MNG_SECT);
- FCTN_SD_MNGR();
- return i;
+ if(i == 0)
+ {
+ FSC_CURRENT[3] = fsc;
+ FSC_LAST[3] = start_fsc;
+ return i;
+ }
}
if(sid==0x04)
{
@@ -209,8 +220,12 @@
buffer[31]=(uint8_t) (start_fsc & 0xFF);
}
i = disk_write(buffer,SD_MNG_SECT);
- FCTN_SD_MNGR();
- return i;
+ if(i == 0)
+ {
+ FSC_CURRENT[4] = fsc;
+ FSC_LAST[4] = start_fsc;
+ return i;
+ }
}
if(sid==0x05)
{
@@ -230,8 +245,12 @@
buffer[39]=(uint8_t) (start_fsc & 0xFF);
}
i = disk_write(buffer,SD_MNG_SECT);
- FCTN_SD_MNGR();
- return i;
+ if(i == 0)
+ {
+ FSC_CURRENT[5] = fsc;
+ FSC_LAST[5] = start_fsc;
+ return i;
+ }
}
return -1;
}
@@ -247,7 +266,10 @@
block_number=SD_SCP_FIRST+fsc;
result= disk_write(buffer,block_number);
if(result == 0)
- INCREMENT_SD_LIB(sid);
+ {
+ if(INCREMENT_SD_LIB(sid) == 0)
+ SD_LIB_WRITES++;
+ }
return result;
}
if(sid==0x02)
@@ -256,7 +278,10 @@
block_number= SD_SFF_AT_FIRST+fsc;
result= disk_write(buffer,block_number);
if(result == 0)
- INCREMENT_SD_LIB(sid);
+ {
+ if(INCREMENT_SD_LIB(sid) == 0)
+ SD_LIB_WRITES++;
+ }
return result;
}
if(sid==0x03)
@@ -265,7 +290,10 @@
block_number= SD_SFF_BT_FIRST +fsc;
result= disk_write(buffer,block_number);
if(result == 0)
- INCREMENT_SD_LIB(sid);
+ {
+ if(INCREMENT_SD_LIB(sid) == 0)
+ SD_LIB_WRITES++;
+ }
return result;
}
if(sid==0x04)
@@ -274,7 +302,10 @@
block_number=SD_HK_ARCH_FIRST +fsc;
result= disk_write(buffer,block_number);
if(result == 0)
- INCREMENT_SD_LIB(sid);
+ {
+ if(INCREMENT_SD_LIB(sid) == 0)
+ SD_LIB_WRITES++;
+ }
return result;
}
if(sid==0x05)
@@ -283,7 +314,10 @@
block_number= LOG_FIRST +fsc;
result= disk_write(buffer,block_number);
if(result == 0)
- INCREMENT_SD_LIB(sid);
+ {
+ if(INCREMENT_SD_LIB(sid) == 0)
+ SD_LIB_WRITES++;
+ }
return result;
}
return 1;
@@ -292,10 +326,10 @@
uint8_t SD_READ(uint8_t* buffer,uint32_t fsc,uint8_t sid)
{
FCTN_SD_MNGR();
+ uint32_t block_number;
+ int result;
if(SD_SW_EN_DS == 1)
return 0x89;
- uint32_t block_number;
- int result;
if(sid==0x01)
{
if(!(FSC_LAST[1]<=fsc && fsc<=FSC_CURRENT[1])){
@@ -609,11 +643,14 @@
if (cmd(24, block_number * cdv) != 0) {
return 1;
}
-
- // send the data block
- write(buffer, 512);
- //gPC.printf("Written Successfully bro \n");
- return 0;
+
+ uint64_t temp;
+ int r = write(buffer, 512);
+ if(r == 0 ){
+ temp = FCTN_CDMS_RD_RTC();
+ TIME_LATEST_SD_RD = temp >> 7;
+ }
+ return r;
}
int write(const uint8_t*buffer, uint32_t length)
@@ -651,11 +688,14 @@
{
// set read address for single block (CMD17)
if (cmd(17, block_number * cdv) != 0) {
+ SD_RD_ERROR = 1;
return 1;
}
// receive the data
read(buffer, 512);
+ uint64_t temp = FCTN_CDMS_RD_RTC();
+ TIME_LATEST_SD_RD = temp >> 7;
return 0;
}
@@ -691,4 +731,15 @@
}
return 0; //normal return
+}
+
+int disk_read_statusbits(uint8_t *buffer)
+{
+ if (cmd(17, 0) != 0) {
+ SD_RD_ERROR = 1;
+ return -1;
+ }
+
+ // receive the data
+ return read(buffer,64);
}
\ No newline at end of file
