/*********************************************************** * Function: * Description: * Input: * Input: * Output: * Return: * Others: ***********************************************************/ static void rt_thread_entry_sd_test( void* parameter ) { FRESULT res; char tempbuf[64]; rt_sem_take( &sem_dataflash, RT_TICK_PER_SECOND * FLASH_SEM_DELAY ); SPI_Configuration(); res = SD_Init(); if(0 == res) { rt_kprintf("\r\n SD CARD INIT OK!"); memset(tempbuf,0,sizeof(tempbuf)); SD_GetCID(tempbuf); rt_kprintf("\r\n CID="); printf_hex_data(tempbuf, 16); SD_GetCSD(tempbuf); rt_kprintf("\r\n SID="); printf_hex_data(tempbuf, 16); rt_kprintf("\r\n SD 容量=%d",SD_GetCapacity()); } else { rt_kprintf("\r\n SD CARD INIT ERR = %d",res); } if(0 == f_mount(MMC, &fs)) { rt_kprintf("\r\n f_mount SD OK!"); } rt_sem_release(&sem_dataflash); while( 1 ) { rt_thread_delay( RT_TICK_PER_SECOND / 20 ); } }
//获取SD卡的容量(字节) //返回值:0: 取容量出错 // 其他:SD卡的容量(字节) u32 SD_GetCapacity(void) { u8 csd[16]; u32 Capacity; u8 r1; u16 i; u16 temp; //取CSD信息,如果期间出错,返回0 if(SD_GetCSD(csd)!=0) return 0; //如果为SDHC卡,按照下面方式计算 if((csd[0]&0xC0)==0x40) { Capacity=((u32)csd[8])<<8; Capacity+=(u32)csd[9]+1; Capacity = (Capacity)*1024;//得到扇区数 Capacity*=512;//得到字节数 } else { i = csd[6]&0x03; i<<=8; i += csd[7]; i<<=2; i += ((csd[8]&0xc0)>>6); //C_SIZE_MULT r1 = csd[9]&0x03; r1<<=1; r1 += ((csd[10]&0x80)>>7); r1+=2;//BLOCKNR temp = 1; while(r1) { temp*=2; r1--; } Capacity = ((u32)(i+1))*((u32)temp); // READ_BL_LEN i = csd[5]&0x0f; //BLOCK_LEN temp = 1; while(i) { temp*=2; i--; } //The final result Capacity *= (u32)temp;//字节为单位 } return (u32)Capacity; }