我在SPI模式下读写SD卡上的值。512字节块的写入和读取频率设置为10Mhz。写一个块大约需要5毫秒,两个块之间的时间大约是10毫秒。
有没有办法提高写作速度?
void sd_card_write_block(uint16 blockNumber, uint8* buffer)
{
uint16 blockLow = 0;
uint16 blockHigh = 0;
uint8 dummy = 0;
uint8 result = 0;
uint8 data_block_start_byte = 0;
uint8 write_command[SD_CMD_SIZE] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
uint8 dummy_buffer[DUMMY_BUFFER_LENGTH] = {0xFF, 0xFF, 0xFF, 0xFF,0xFF, 0xFF, 0xFF, 0xFF};
uint8 i = 0;
uint8 check_response[CHECK_RESPONSE_SIZE] = {0x00, 0x00, 0x00};
uint8 check_response1[CHECK_RESPONSE_SIZE] = {0x00, 0x00, 0x00};
uint8 r1 = 0;
uint16 retry = 0;
uint8 response1 = 0;
dummy = 0xFF;
//initialize the dummy buffer to keep MOSI pin High
for(i = 0; i < DUMMY_BUFFER_LENGTH; i++)
{
dummy_buffer[i] = 0xFF;
}
//set CS pin low
spi_select_slave( &spi_master_instance, &slave, true);
//send three clock cycles with MOSI HIGH (Ncs)
spi_write_buffer_wait( &spi_master_instance, dummy_buffer, NCS_LENGTH);
//block size was set in sd_init
blockLow = ((blockNumber & 0x003F) << 9);
blockHigh = ((blockNumber & 0xFFC0) >> 7);
//send SD CMD24(WRITE_SINGLE_BLOCK) to write the data to SD card
write_command[0] = 0x58;
//high block address bits, blockHigh HIGH and LOW
write_command[1] = (blockHigh >> 0x08);
write_command[2] = (blockHigh & 0xFF);
//low block address bits, blockLow HIGH and LOW
write_command[3] = (blockLow >> 0x08);
write_command[4] = (blockLow & 0xFF);
//checksum is no longer required but send 0xFF
write_command[5] = 0xFF;
spi_write_buffer_wait( &spi_master_instance, write_command, SD_CMD_SIZE);
spi_transceive_buffer_wait( &spi_master_instance, dummy_buffer, check_response, CHECK_RESPONSE_SIZE);
//send three clock cycles with MOSI High
spi_write_buffer_wait( &spi_master_instance, dummy_buffer, DUMMY_BUFFER_LENGTH);
//set bit 0 to 0 which indicates the beginning of the data block
data_block_start_byte = DATA_BLOCK_START_TOKEN;
spi_transceive_buffer_wait( &spi_master_instance, &data_block_start_byte, &result, SD_RESPONSE_SIZE);
/*takes so long because its similar to transreceivea and it discards the rx*/
spi_write_buffer_wait( &spi_master_instance, buffer, SD_BLOCK_LENGTH);
//read the microSD card response
spi_transceive_buffer_wait( &spi_master_instance, dummy_buffer, check_response1, CHECK_RESPONSE_SIZE);
do
{
// write dummy byte
spi_transceive_buffer_wait( &spi_master_instance, &dummy, &response1, SD_RESPONSE_SIZE);
r1 = response1;
// do retry counter
retry++;
if(retry > MAX_TIMEOUT)
{
spi_select_slave( &spi_master_instance, &slave, false);
break;
}
}
while(r1 == END_OF_BLOCK_RESPONSE);
//set the CS High
spi_select_slave( &spi_master_instance, &slave, false);
}
最佳答案
你不能,至少不能太多。原因是SPI模式本身就是瓶颈。所以,你可以使用一些技巧来提高速度,但你真的不会从中受益太多。如果你真的需要那么快的速度,我建议你使用SDIO(并不像你想象的那么复杂)。如果你用的是AVR,试试xmega的阵容(不要引用我的话,因为我用的是ARMs,所以我在AVR里不太了解),或者完全换成另一个阵容。