![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
things are working
Dependencies: FreescaleIAP mbed-rtos mbed
Fork of CDMS_DEC_2016_jan by
Diff: cdms_sd.cpp
- Revision:
- 1:ad3b8a8032e2
- Parent:
- 0:bcbd76c86cde
--- a/cdms_sd.cpp Wed Dec 16 09:06:59 2015 +0000 +++ b/cdms_sd.cpp Thu Jan 21 14:46:28 2016 +0000 @@ -1,5 +1,7 @@ #include "cdms_sd.h" + + SPI spi_sd(PTE1, PTE3, PTE2); // MOSI,MISO, CLOCK microcontroller(in order) DigitalOut cs_sd(PTE22); @@ -9,22 +11,189 @@ uint64_t sd_sectors(); uint64_t sectors; -void FCTN_SD_MNGR() +void FCTN_CDMS_SD_INIT() +{ + + initialise_card(); + disk_initialize(); +} + + + +uint32_t FCTN_SD_MNGR(uint8_t sid) + +{ + + uint32_t SD_MNG_SECT=7000; + + uint32_t fsc; + uint8_t buffer[512]; + int b=disk_read(buffer, SD_MNG_SECT); + if(sid==0x0) + { + + fsc=(uint32_t)(buffer[0]<<24)+(uint32_t)(buffer[1]<<16)+(uint32_t)(buffer[2]<<8)+(uint32_t)buffer[3]; + uint32_t next_fsc=fsc+1; + buffer[0]=(uint8_t) (next_fsc>>24 & 0xFF); + buffer[1]=(uint8_t) (next_fsc>>16 & 0xFF); + buffer[2]=(uint8_t) (next_fsc>>8 & 0xFF); + buffer[3]=(uint8_t) (next_fsc & 0xFF); + buffer[511]+=2; + disk_write(buffer,SD_MNG_SECT); + } + if(sid==0x1) + { + fsc=(uint32_t)(buffer[4]<<24)+(uint32_t)(buffer[5]<<16)+(uint32_t)(buffer[6]<<8)+(uint32_t)buffer[7]; + uint32_t next_fsc=fsc+1; + buffer[4]=(uint8_t) (next_fsc>>24 & 0xFF); + buffer[5]=(uint8_t) (next_fsc>>16 & 0xFF); + buffer[6]=(uint8_t) (next_fsc>>8 & 0xFF); + buffer[7]=(uint8_t) (next_fsc & 0xFF); + buffer[511]+=2; + disk_write(buffer,SD_MNG_SECT); + } + if(sid==0x2) + { + fsc=(uint32_t)(buffer[8]<<24)+(uint32_t)(buffer[9]<<16)+(uint32_t)(buffer[10]<<8)+(uint32_t)buffer[11]; + uint32_t next_fsc=fsc+1; + buffer[8]=(uint8_t) (next_fsc>>24 & 0xFF); + buffer[9]=(uint8_t) (next_fsc>>16 & 0xFF); + buffer[10]=(uint8_t) (next_fsc>>8 & 0xFF); + buffer[11]=(uint8_t) (next_fsc & 0xFF); + buffer[511]+=2; + disk_write(buffer,SD_MNG_SECT); + } + if(sid==0x3) + { + fsc=(uint32_t)(buffer[12]<<24)+(uint32_t)(buffer[13]<<16)+(uint32_t)(buffer[14]<<8)+(uint32_t)buffer[15]; + uint32_t next_fsc=fsc+1; + buffer[12]=(uint8_t) (next_fsc>>24 & 0xFF); + buffer[13]=(uint8_t) (next_fsc>>16 & 0xFF); + buffer[14]=(uint8_t) (next_fsc>>8 & 0xFF); + buffer[15]=(uint8_t) (next_fsc & 0xFF); + buffer[511]+=2; + disk_write(buffer,SD_MNG_SECT); + } + if(sid==0x4) + { + fsc=(uint32_t)(buffer[16]<<24)+(uint32_t)(buffer[17]<<16)+(uint32_t)(buffer[18]<<8)+(uint32_t)buffer[19]; + uint32_t next_fsc=fsc+1; + buffer[16]=(uint8_t) (next_fsc>>24 & 0xFF); + buffer[17]=(uint8_t) (next_fsc>>16 & 0xFF); + buffer[18]=(uint8_t) (next_fsc>>8 & 0xFF); + buffer[19]=(uint8_t) (next_fsc & 0xFF); + buffer[511]+=2; + disk_write(buffer,SD_MNG_SECT); + } + return fsc; +} + + +int SD_WRITE(uint8_t* buffer,uint32_t fsc,uint8_t sid) { - /*Size of block of SD card for 2GB = 512B, 4 , 8 GB SD card. We will prefer 8 GB. - SD allocation. Assuming 8GB - SCP: 600 MB -122880 - SFF-AT: 2 GB -4194304 - SFF-BT: 5 GB -10485760 - HK-ARCH:100 MB -204800 - LOG: 50MB -102400 - SD card management: 50MB - 102400*/ - + + uint32_t SD_SCP_FIRST=1001; + uint32_t SD_SCP_LAST=2000; + uint32_t SD_SFF_AT_FIRST=2001; + uint32_t SD_SFF_AT_LAST = 3000; + uint32_t SD_SFF_BT_FIRST =3001; + uint32_t SD_SFF_BT_LAST=4000; + uint32_t SD_HK_ARCH_FIRST=4001; + uint32_t SD_HK_ARCH_LAST= 5000; + uint32_t LOG_FIRST =5001; + uint32_t LOG_LAST=6000; + uint32_t SD_MNG_SECT=7000; + uint32_t block_number; + int result; + if(sid==0x0) + { + block_number=SD_SCP_FIRST+fsc; + printf("write_block_number=%d\r\n",block_number); + result= disk_write(buffer,block_number); + return result; + } + if(sid==0x1) + { + block_number=SD_SFF_AT_FIRST + fsc; + result= disk_write(buffer,block_number); + return result; + } + if(sid==0x2) + { + block_number=SD_SFF_BT_FIRST + fsc; + result= disk_write(buffer,block_number); + return result; + } + if(sid==0x3) + { + block_number=SD_HK_ARCH_FIRST+fsc; + sd1.printf("Block number is %d \r\n",block_number); + result= disk_write(buffer,block_number); + return result; + } + if(sid==0x4) + { + block_number=LOG_FIRST +fsc; + result= disk_write(buffer,block_number); + return result; + } + return 1; } + +int SD_READ(uint8_t* buffer,uint32_t fsc,uint8_t sid) +{ + + uint32_t SD_SCP_FIRST=1001; + uint32_t SD_SCP_LAST=2000; + uint32_t SD_SFF_AT_FIRST=2001; + uint32_t SD_SFF_AT_LAST = 3000; + uint32_t SD_SFF_BT_FIRST =3001; + uint32_t SD_SFF_BT_LAST=4000; + uint32_t SD_HK_ARCH_FIRST=4001; + uint32_t SD_HK_ARCH_LAST= 5000; + uint32_t LOG_FIRST =5001; + uint32_t LOG_LAST=6000; + uint32_t SD_MNG_SECT=7000; + uint32_t block_number; + int result; + if(sid==0x0) + { + block_number=SD_SCP_FIRST + fsc; + sd1.printf("read_block_number=%d\r\n",block_number); + result= disk_read(buffer,block_number); + } + else if(sid==0x1) + { + block_number=SD_SFF_AT_FIRST + fsc; + result= disk_read(buffer,block_number); + } + else if(sid==0x2) + { + block_number=SD_SFF_BT_FIRST + fsc; + result= disk_read(buffer,block_number); + } + else if(sid==0x3) + { + block_number=SD_HK_ARCH_FIRST + fsc; + result= disk_read(buffer,block_number); + } + else if(sid==0x4) + { + block_number=LOG_FIRST +fsc; + result= disk_read(buffer,block_number); + } + else + { + return 1; + } + return result; +} + + int initialise_card() { // Set to 100kHz for initialisation, and clock card with cs_sd = 1 - spi_sd.frequency(100000); + spi_sd.frequency(100000); // changed on 31 12 2015 to 1 MHz cs_sd = 1; for (int i = 0; i < 16; i++) { spi_sd.write(0xFF);