SD card file read/write

Source: Internet
Author: User
Tags printf
SD card File read/write

Chess Boy 1048272975

SD card (Secure Digital Memory card) has the advantages of small size, large capacity, fast data transmission, pluggable, security, etc., which are widely used in portable devices. For example, as a memory card for digital camera, as a mobile phone, tablet multimedia expansion card with the TF card (micro SD) and so on. 1. SD Card Overview

SD card technology is developed on the basis of the MMC card, the size of the same as the MMC card, just 0.7mm thicker than the MMC card, so the SD host can recognize and access the MMC card. The SD card interface, in addition to retaining the 7-pin MMC card, also adds 2 pins on both sides, as a data line, in order to improve the transmission rate by turning the transmission mode from serial to parallel. At this time the specification is SD1.0 version, the maximum capacity only to 4GB. In order to follow up on the replacement of products, the SD association released the next generation SD card specification SD2.0 with larger capacity and faster storage in 06. The specification redefined the speed level of the SD card, divided into three gears: Class 2, 4, 6, corresponding to the write speed 2mb/s, 4mb/s, 6mb/s. According to the card capacity is divided into standard cards (less than 2GB) and high-capacity cards (2GB~32GB), the current application of the SD card on the market is mostly SD2.0 version of the card. In order to make the memory card more mini, through the SD card specification standard, but also derived the MINISD card and micro SD card, these cards are smaller than the standard SD card size, through the SD adapter sleeve can be used as a general SD card. In particular, micro SD card, can be regarded as the smallest memory card, ultra-small volume can greatly save the interior design of consumer electronics space, the basic current Android phones are selected micro SD card as a multimedia expansion memory card. With the progress of science and technology, SD2.0 standard SD card is also gradually unable to meet the needs of the application, in 10 SD Association released a new SD3.0 specification, the specification defines SDXC and UHS, and increased Class10, capacity range of 32GB~2TB. While the SDXC card still needs to wait for its price to fall further, the SD4.0 specification has begun to develop in tension, which is beyond the scope of this article. 2. SD Card driver

The SD card supports a total of three transmission modes: SPI mode, 1-bit SD mode, and 4-bit SD mode. All SD cards must support the older SPI/MMC mode, which supports a slow four-wire SPI interface, enabling many microcontrollers to read and write SD cards via the SPI or analog SPI interface. The lpc5411x Development Board of Wanli expands a TF card slot through the SPI interface and can read and write TF cards with the SPI interface.

The SD2.0 standard defines the physical layer correlation specification and the host controller specification, the SD card driver's writing must refer to these two specifications, follows the standard SD card to realize the data access with the Unified software drive. NXP provides SDMMC library middleware for its full range of chips to support SD/MMC card reading and writing, and can download the corresponding BSP on the website, which contains SDMMC library, Fsl_sdspi.h/fsl_ The SDSPI.C is the standard driver for the SPI Mode access SD card and can be applied directly to the LPC5411X development environment. SD Card Driver is the main implementation of three interfaces, respectively, the SD card recognition initialization, SD card block read, SD card block write, the implementation can refer to FSL_SDSPI.H/FSL_SDSPI.C driver files. 3. Fatfs

Data are often stored in the form of files in the storage device, for SD card, the general use of the Fat32 file system, fatfs because of its open source free, support Fat32, has been widely used.

Fatfs is a FAT file system module, written by Japanese engineer Chan, who has never stopped maintenance and updates since the first FATFS version was released in 06. Fatfs's writing follows ANSI C and is completely separate from the disk I/O layer. It does not rely on the hardware architecture, the Code and workspace space is small, so that it can be embedded in the various low-cost microcontrollers, such as AVR, 8051, PIC, ARM, Z80, 68K and so on. As the SD card is generally used Fat32 file system, in the use of SD card system to transplant fatfs, will be a good implementation of the SD card file management.

The Fatfs module is completely independent of the disk I/O layer, so the underlying disk I/O access is not part of the Fatfs module, and the user must implement this part to access the storage device itself. These functions are usually implemented in DISKIO.C disk_initialize (), Disk_status (), Disk_read (), Disk_wirte (), Disk_ioctl (), if OS-related features are enabled, Additional process/memory functions are required. where Disk_initialize () corresponding to the SD card driver card recognition initialization interface, Disk_read () corresponding to the SD card block read interface, Disk_wirte () corresponding to the SD card block write interface. NXP provides FATFS middleware support for its full range of chips, and fatfs the specific implementation of the SD card driver interface can refer to the ported FATFS middleware in BSP. 4. Reading and writing test

After porting the SD card driver and fatfs the underlying interface, you can use the FATFS application programming interface to read and write the files inside the SD card. The average read and write speed of a 10MB size file is tested with 2KB size as read-write unit.

uint8_t testbuffer[2048];

int main ()

{

uint32_t i;

FATFS FS;

FIL file;

Fresult Res;

uint32_t Timecount;

uint32_t Bytewrite, Byteread;

/* Board pin, clock, debug console init */

/*attach MHz clock to FLEXCOMM0 (Debug console) */

CLOCK_ATTACHCLK (Board_debug_uart_clk_attach);

/* Enable clock for gpio*/

Clock_enableclock (Kclock_gpio0);

Clock_enableclock (KCLOCK_GPIO1);

Board_initpins ();

Board_bootclockfrohf96m ();

Board_initdebugconsole ();

Gpio_init ();

F_mount (&fs, "4:", 0);

/*

Res = F_mkfs ("", 0, 4096);

if (Res! = RES_OK) {

PRINTF ("F_mkfs error%d\r\n", Res);

while (1);

}

*/

PRINTF ("Writing test.bin, File sise10mb\r\n");

Res= F_open (&file, "4:test.bin", Fa_write | Fa_create_always);

if (Res! = RES_OK) {

PRINTF ("Createfile failed\r\n");

while (1) {

Gpio_togglepinsoutput (gpio,0, 1u << 15);

Delay_ms (300);

}

}

for (i=0; i<sizeof (testbuffer); i++) {

testbuffer[i]= i;

}

Timecount =timer_get_current_milliseconds ();

For (i=0;i<10*1024*1024/sizeof (testbuffer); i++) {

Res= F_write (&file, &testbuffer, sizeof (Testbuffer), &bytewrite);

if (Res! = RES_OK) {

F_close (&file);

PRINTF ("WriteFile error\r\n");

while (1) {

Gpio_togglepinsoutput (gpio,0, 1u << 15);

Delay_ms (300);

}

}

}

F_close (&file);

PRINTF ("Sd write Speed%dkb/s\r\n", 10*1024*1024/(Timer_get_current_milliseconds ()-timecount));

PRINTF ("Reading test.bin, File sise10mb\r\n");

Res = F_open (&file, "4:test.bin", Fa_read | fa_open_existing);

if (Res! = RES_OK) {

PRINTF ("Openfile failed\r\n");

while (1) {

Gpio_togglepinsoutput (gpio,0, 1u << 15);

Delay_ms (300);

}

}

Timecount =timer_get_current_milliseconds ();

For (i=0;i<10*1024*1024/sizeof (testbuffer); i++) {

Res= F_read (&file, (unsigned char *) &testbuffer, sizeof (Testbuffer), &byteread);

if (Res! = RES_OK) {

F_close (&file);

PRINTF ("Readfile error\r\n");

while (1) {

Gpio_togglepinsoutput (gpio,0, 1u << 15);

Delay_ms (300);

}

}

}

F_close (&file);

PRINTF ("Sd read speed%dkb/s\r\n", 10*1024*1024/(Timer_get_current_milliseconds ()-timecount));

while (1) {

Gpio_togglepinsoutput (gpio,0, 1u << 15);

Delay_ms (1000);

}

}

The reading and writing speed test results are as follows:

The SD card write speed is 872 kb/s, reading speed is 1169 KB/S,SD card read and write through the SPI interface, SPI clock with fro 12M clock, so this read and write speed is appropriate. SD card read and write speed with the card speed level, data transmission rate, read and write more than one block read and write faster, here test one-time reading 8 blocks (512 bytes/block), 2048 bytes, read and write speed is mainly limited to the SPI transmission rate, lpc5411x SPI interface up to 48M clock , the SPI can further improve the transmission rate of the SPI by adopting the PLL clock and the internal high-speed clock, thus further improving the read and write speed of the SD card. 5. Appendix

MDK project, including the SPI mode SD driver, fatfs file system module, SD card reading and writing speed test application routines.

Https://pan.baidu.com/s/1cMz1G6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.