STM32 Note SD card reading and writing and FatFS file system

Publisher:王大雷Latest update time:2016-11-27 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Because I need to use it, I learned how to operate the SD card with SPI, and at the same time transplanted a free and open source FAT file system: FatFS. It feels good. I have realized the operation of reading and writing files on the microcontroller. Next, I can explain my G code!
  My SD card underlying operation refers to several common codes on the Internet, but I have also made some optimizations to its structure, at least it looks more convenient to use. It can be used as the diskio of the file system, or you can directly use the underlying function to read and write the SD card as a flash.
  The FatFs file system is quite small, 6-7K is enough, which is very suitable for the STM32 with 128K flash, and the cost is not high. At the same time, it is highly portable, and at least only 4 functions need to be modified to realize the transplantation of the file system. For an introduction to the relevant file system, please see here .
  Here is a relatively complete set of reference materials, including the original materials of the fatfs file system, several important manuals and codes downloaded from the Internet.
http://www.ouravr.com/bbs/bbs_content.jsp?bbs_sn=3210864&bbs_page_no=1&bbs_id=3020

  Below is my code:
rar
  The underlying SPI bus operation on the SD card is in SPI_SD_driver.c/h, and the disk operation function in the FATFS transplant file diskio.c will call the underlying operation function. Here are some low-level operation functions:
u8 SPI_ReadWriteByte(u8 TxData); //SPI bus reads and writes a byte
u8 SD_WaitReady(void); //Wait for SD card to be ready
u8 SD_SendCommand(u8 cmd, u32 arg, u8 crc); //SD card sends a command
u8 SD_SendCommand_NoDeassert(u8 cmd, u32 arg, u8 crc); //SD card sends a command, no disconnection
u8 SD_Init(void); //SD card initialization
                                  
u8 SD_ReceiveData(u8 *data, u16 len, u8 release); //SD card read data
u8 SD_GetCID(u8 *cid_data); //Read SD card CID
u8 SD_GetCSD(u8 *csd_data); //Read SD card CSD
u32 SD_GetCapacity(void); //Get SD card capacity

u8 SD_ReadSingleBlock(u32 sector, u8 *buffer); //Read a sector
u8 SD_WriteSingleBlock(u32 sector, const u8 *buffer); //Write a sector
u8 SD_ReadMultiBlock(u32 sector, u8 *buffer, u8 count); //Read multiple sectors
u8 SD_WriteMultiBlock(u32 sector, const u8 *data, u8 count); //Write multiple sectors

  This is a piece of code in diskio.c. In the disk initialization, we called the SD card initialization function in SPI_SD_driver.c.
DSTATUS disk_initialize (
    BYTE drv /* Physical drive nmuber (0..) */
)
{
    u8 state;

    if(drv)
    {
        return STA_NOINIT; //Only supports operations on disk 0
    }

    state = SD_Init();
    if(state == STA_NODISK)
    {
        return STA_NODISK;
    }
    else if(state != 0)
    {
        return STA_NOINIT; //Other errors: initialization failed
    }
    else
    {
        return 0; //Initialization successful
    }
}
  
   In short, the FATFS file system has high portability. After testing, under the 18MSPI clock of STM32, the file reading speed is more than 300K per second, and the file writing speed is also more than 100K. It should be said that it basically meets the speed requirements for disk reading and writing in embedded engineering applications. If the SD card reading and writing code is further optimized, the speed should be improved to a certain extent. At the same time, it should be noted that the reading and writing speed of FLASH itself is not as fast as RAM. By replacing the SD card, it is found that the reading and writing speed is directly related to the card itself, so you should try to choose a faster card.

Keywords:STM32 Reference address:STM32 Note SD card reading and writing and FatFS file system

Previous article:UCOS porting on S3C2410
Next article:S3C2440 reads and writes large capacity SD cards

Recommended ReadingLatest update time:2024-11-23 11:40

Characteristics of STM32 DMA
DMA features of STM32:    Each channel is directly connected to a dedicated hardware DMA request, and each channel also supports software triggering. These functions are configured through software.    Priority among the seven requests can be set programmatically by software (there are four levels: very high, high
[Microcontroller]
Characteristics of STM32 DMA
STM32RTC real-time clock
I use the STM32 library function: Two knowledge points:       1. RTC clock block diagram analysis (important)       2. How is the time displayed (brief analysis) 1. RTC clock block diagram analysis (important) First, let's get familiar with a few knowledge points:       1. The real-time clock (RTC) of STM32 is an inde
[Microcontroller]
STM32RTC real-time clock
Is the programming protocol of the SWD interface of STM32 public?
You need a good oscilloscope to capture it so that you have enough memory depth to ensure that you can filter out that damn 50clock. According to the Arm manual, each conversion on the sender side requires a TNR - but when I look at the JLINK waveform there is no damn TNR. The manual says that asynchronous SWD require
[Microcontroller]
stm32 memory structure, .icf file and .map file analysis in IAR development environment
Create a new IAR environment STM32 project and report an error: Error : section placement failed: unable to allocate space for sections/blocks with a total estimated  minimum size of 0x9ca8 bytes in (total uncommitted space 0x5000).  Reason: No .icf file was specified and space could not be allocated to the segment/
[Microcontroller]
stm32 memory structure, .icf file and .map file analysis in IAR development environment
How to merge two .bin files of STM32?
There are two processes in production. First write IAP, then write user program through IAP. There are two processes in production, and IAP may not be written to the chip through IAR+JLINK during production. It should be burned to the chip through a special burner+adapter. If it can be combined into one, it is best to
[Microcontroller]
Design of embedded binocular image acquisition system based on STM32
1 Introduction With the development of image processing technology and embedded systems, the use of embedded systems for image processing has made applications such as video surveillance, video telephony and video conferencing possible. Image acquisition on embedded systems is the prerequisite for realizing these ap
[Microcontroller]
Design of embedded binocular image acquisition system based on STM32
[STM32] STM32 port multiplexing and remapping (AFIO auxiliary function clock)
STM32F1xx official information: "STM32 Chinese Reference Manual V10" - Chapter 8 General and Multiplexed Function IO (GPIO and AFIO) Port multiplexing function Definition of port multiplexing STM32 has many built-in peripherals (such as serial port, ADC, DCA, etc.), and the external pins of these peripherals are
[Microcontroller]
[STM32] STM32 port multiplexing and remapping (AFIO auxiliary function clock)
stm32 usb enumeration process
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号