Micr0 SD card reading and writing based on STM32F103XX microprocessor

Publisher:huijiazi5210Latest update time:2012-08-07 Source: 现代电子技术 Keywords:STM32F1103XX  Micro  SPI  FATFS Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

O Introduction
Since the remote intelligent heart detection instrument processes and stores the collected ECG signals in real time. Therefore, based on the STM32F103XX microprocessor, this paper uses Micro SD memory card to realize the storage of key ECG signals.

1 Hardware Circuit Design
1.1 Introduction to STM32F103XX Microprocessor
STM32F103XX is based on the high-performance 32-bit RISC ARMCortex-M3 core with an operating frequency of 72 MHz. High-speed memory is integrated on the chip, and rich and enhanced peripherals and I/O are connected through the APB bus. All devices provide standard communication interfaces.
1.2 SPI Protocol
Since the SPI (setial peripheral interface) bus occupies fewer interface lines, has high communication efficiency, and supports most processor chips, it is an ideal choice. SPI is a serial interface protocol that uses 4 signal lines for communication, including master/slave modes. The four interface signals are: serial data input (MISO, master input, slave output), serial data output (MOSI, master output, slave input), shift clock (SCK), and low-level effective slave enable signal (cs). The biggest feature of SPI is that the communication between the master and slave devices is determined by the presence or absence of the master clock signal. Once the master clock signal is detected, data transmission begins.
1.3 Introduction to Micro SD Card
The interface of the Micro SD card can support two operating modes: SDIO mode and SPI mode. Designers can choose any of the modes. The SDIO mode allows 4-wire high-speed data transmission with high transmission rate, but most microcontrollers do not have this interface, and the software simulation protocol is complex. The SPI mode uses a simple and universal SPI channel interface to realize data transmission. At present, most microcontrollers provide SPI interface. The disadvantage of SPI mode relative to SDIO mode is the loss of transmission speed, but the processing speed of microprocessors is getting higher and higher, and the use of SPI mode can mostly meet
engineering needs.
Micro SD card requires full-duplex, 8-bit SPI operation. Only four signal lines are needed between the STM32F103XX microprocessor and the Micro SD card to complete data reading/writing. When the CS chip select signal line is low, the microprocessor starts all bus transmissions. Data is synchronously input from the microprocessor's MOSI pin to the Micro SD card's DI pin, and synchronously input from the Micro SD card's DO line to the microprocessor's MISO pin. Data is synchronously input and output on the rising edge of the CLK signal. At the end of each data transfer, 8 additional clocks must be provided to allow the Micro SD card to complete any unfinished operations. In addition, when using SPI mode, in order to prevent the bus from being suspended when no card is connected or the card driver is in a high-impedance state, according to the SD card specification, these signals need to use a 10-100 kΩ pull-up resistor on the microprocessor end. The hardware connection circuit is shown in Figure 1.

a.JPG [page]

2 Software Design
Part of the software design is based on the STM32 firmware library, which is targeted at the ARM-based 32-bit microprocessors STM32F101XX and STM32F103XX. It includes programs, data structures, and macro units covering all peripheral features, as well as device driver descriptions and examples for each peripheral module. Therefore, using this firmware library can save a lot of time and spend more time on programming, thereby reducing the overall overhead in application development.
2.1 Initialization of Micro SD card to SPI bus mode
The STM32F103XX microprocessor includes two serial peripheral interfaces (SPI), which can easily configure the Micro SD card interface. First, the SPI1 interface to be used is initialized using the command SPI_In-it (SPI1, & SPI_InitStructure), and SPI1 is enabled.
When the power is just turned on, the Micro SD card uses the proprietary SD bus protocol by default. To switch the Micro SD card to SPI mode, the host needs to issue the command CMDO (GO-IDLE-STATE). The Micro SD card will detect the SPI mode selection information. Since the card select (CS) pin remains at a low level during the transmission of this command and all other SPI commands, the Micro SD card responds with R1, and the idle state bit is set to a high level. At this time, the Micro SD card enters the idle state. The SPI clock frequency at this stage cannot exceed 400 kHz. After the Micro SD card enters the SPI mode, the host should first send an initialization command CMD1 (Activates the card process), at which time the SPI frequency can be set to high-speed mode. Then send the command CMD59 to decide whether to enable CRC check, set the read/write block data length, and finally return after a delay of 8 clocks. The Micro SD initialization process is shown in Figure 2.
b.JPG

2.2 Micro SD card reads and writes a single block of data
To read a data block from the Micro SD card, the host only needs to send the CMD17 (MSD_READ_SINGLE_BLOCK) command and use the starting address as a parameter (this address must be aligned with the starting position of a block on the medium). Then the Micro SD card will verify this byte address and respond with an R1 command. After completing the Micro SD card reading operation, first send a start data command, then send a fixed amount of data, and finally a 2-byte CRC check. Reading a data block is completed by the function u8 MSD_ReadBlock (u8*pBuffer, u32 ReadAddr, u16 NumByteToRead), pBuffer is a pointer that points to the address of the memory buffer that receives the Micro SD card data, ReadAddr is the address of the data to be read in the Micro SD card, and NumByteToRead is the number of bytes to be read, generally 512 B.

[page]

Writing a data block from a Micro SD card is similar to reading a data block. The host sends a CMD24 (MSD_WRITE_BLOCK) command to start the write operation process, and the Micro SD card will respond with an R1 command response format. If the command responds, the write operation is performed. The host sends a start token, then sends a fixed number of data bytes (512 B), and returns a data response token to indicate whether the data to be written is completed. Finally, there is a 2-byte CRC check. Writing a data block is completed by the function u8 MSD_WriteBuffer (u8*pBuffer, u32 WriteAddr, u32 Num-ByteToWrite). The parameters in the function of writing data to the Micro SD card are the same as those in the function of reading data from the Micro sD card. The process of reading/writing a single block of data is shown in Figure 3.
c.JPG

2.3 File storage of Micro SD card
Since the data on the Micro SD card needs to be directly read/written on the computer, the Micro SD card file system must be consistent with the computer's file system. At present, the commonly used file systems are mainly Microsoft's FATl2, FATl6, FAT32, NTFS, and EXT2 and EXT3 under Linux system. Due to the widespread application of Microsoft Windows, the most used FAT file system is still in current consumer electronic products, such as USB flash drives, MP3, MP4, digital cameras, etc. Therefore, it is very important for single-chip system designers to find a FAT open source file system that is easy to transplant and use, occupies relatively small hardware resources and has powerful functions. Therefore, the FAT FS file system is transplanted on the Micro SD card storage mechanism. FAT FS is a completely free and open source FAT file system module, which is specially designed for small embedded systems. It is completely written in standard C language, so it has good hardware platform independence and can be transplanted to multiple platforms with only simple modifications. What users need to write transplant code is the underlying interface provided by FAT FS, including the storage medium read/write interface DiskIO and the real-time clock RTC that provides file creation and modification time.
FAT FS Module was designed to be used on different single-chip microcomputers from the beginning, so it has a good hierarchical structure, as shown in Figure 4. The top layer is the application layer. Users do not need to understand the internal structure of the FAT FSModule and the complex FAT protocol. They only need to call a series of application interface functions provided by the module, such as f-open, f-read, f-write, f-Close, etc., which is as simple as reading/writing files on a PC.
2.3.1 File (or directory) creation
The process of creating a file (or directory) on a Micro SD card is the process of applying for a registration entry in the file directory table. First, the program must detect whether the file already exists in the file directory table (FDT), and then apply for a free FDT table entry. If a file with the same name exists, it returns and the creation of the file (or directory) fails. After successfully applying for the FDT table entry, the program will detect whether the remaining space on the disk meets the requirements of the data length of the newly created file, and then find the first free cluster number and modify the corresponding FDT table entry. According to the size of the file, it will continuously loop to find the next free cluster, and at the same time modify the current FAT item to the next cluster number, until the last FAT item is written to 0xFFFFH to indicate the end of the file. When creating a new directory, you only need to provide the name of the new directory to the CreateDir() function. The process is the same as creating a file, except that you do not need to provide data-related information.
2.3.2 Reading and writing files
All files on the Micro SD card are accessed in clusters. When reading files on the Micro SD card, you must first find the file plus directory registration item (F-DT) based on the file name. According to the starting cluster number in the directory registration item in the file, you can find the first cluster content of the file in the data DATA area, and you can find the second cluster number in the FAT table. According to the second cluster number, you can read the data of the second cluster and the third cluster number in the FAT, so that you can read all the file data. When writing files, you must ensure the consistency of the contents of FAT1 and FAT2, that is, you must perform the same write operation on the two FATs. When adding data to an existing file, the program must first analyze how many free bytes are left in the last sector of the original file so that the newly added data can merge with this last sector. When applying for a new free FAT table item for data exceeding the sector, the process is similar to creating a new file. Fill in 0xFFFFH in the last cluster of the file to indicate the end of the file.
2.3.3 Deletion of files (or directories)
The operation of deleting files requires the function DeleteFile() to delete the file name and extension. When deleting a file, the operation of the data area is not involved. It is only necessary to make a deletion mark on the directory registration item (FDT) of the file and mark the cluster occupied by the file in the FAT table as "empty cluster".
The program first searches for a file with the same name in the FDT. If there is a file with the same name, the first byte content in the corresponding FDT table item is changed to 0xE5H, indicating that the content of the FDT table item has been deleted and the new file can use the table item. Finally, it is necessary to change the content of all file-related cluster items in the FAT table to Ox0000 to release the corresponding Micro SD card disk space. If no file with the same name is found, an error value is returned. The operation of deleting a directory only requires the function DeleteDir() to delete the directory name. The program first searches for a directory item with the same name in the FDT. If there is a directory item with the same name, the first byte content in the corresponding FDT table item is changed to 0xE5H. Then read out the starting cluster number of the corresponding directory, and delete all files in the directory in the cluster number.

3 Conclusion
This solution has been successfully applied to remote intelligent heart detection instruments, providing a non-volatile memory solution for data acquisition of heart detection instruments. The data information collected by the remote intelligent heart detection instrument is saved in the Micro SD card in the format of FAT32 standard files. The data files can be read under Windows, which ensures high cost performance and facilitates further analysis and processing of the data.
Keywords:STM32F1103XX  Micro  SPI  FATFS Reference address:Micr0 SD card reading and writing based on STM32F103XX microprocessor

Previous article:Cortex-M3-based STM32 microcontroller handles advanced motor control methods
Next article:Temperature monitoring system based on STM32 and CAN bus

Recommended ReadingLatest update time:2024-11-16 19:58

SD card interface design
1 SD Card Standard The SD card standard is a standard designed and authorized by the SD Card Association for removable storage devices. It is mainly used to formulate the card's external dimensions, electrical interface and communication protocol. 1.1 SD Card Pin Function The external shape of the
[Industrial Control]
SD card interface design
Renesas Electronics joins hands with CG Power and Stars Micro to build packaging and testing factory in India
Renesas Electronics, India's CG Power and Industrial Solutions, and Thailand's Stars Microelectronics officially announced that they will cooperate to build an outsourced semiconductor packaging and test (OSAT) factory in India. This joint venture plan has been approved by the Indian government, marking a new stage of
[Semiconductor design/manufacturing]
Chenxian Optoelectronics launches the first self-developed glass-based Micro-LED splicing screen in mainland China
Recently, Micro-LED, one of Visionox's "one strength and two innovations" development strategies, has made a major breakthrough. Chenxian Optoelectronics has successfully launched the first independently developed glass-based Micro-LED splicing screen in mainland China - a 12.7-inch Micro-LED splicing display that can
[Mobile phone portable]
HLK-W806 (IV): Software SPI and hardware SPI driver for ST7735 LCD
Introduction to ST7735 ST7735 is a TFT driver chip for driving up to 162x132 pixels, 396 (128*3 colors) x162 lines of output, which can be directly connected to an external controller via SPI protocol, or 8-bit/9-bit/16-bit parallel. Display data can be stored in the on-chip 132 x 162 x 18 bits memory, and the display
[Microcontroller]
HLK-W806 (IV): Software SPI and hardware SPI driver for ST7735 LCD
Design of Timing Collection and Storage System Based on SD2300
With the rapid development of science and technology, many applications in the field of modern industrial measurement and control need to realize the timed acquisition and storage of a large amount of data. Taking the ocean current data acquisition and storage interface circuit designed for the current meter as an exam
[Microcontroller]
Design of Timing Collection and Storage System Based on SD2300
MCU SPI module + 74LS164 driver digital tube
The effect is as follows: Circularly shift right and then left       #include htc.h #ifndef _XTAL_FREQ  #define _XTAL_FREQ 4000000 #endif const unsigned char seg_comAnode ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,                         0x07,0x7f,0x6f};//Common anode const unsigned char seg_comCathode ={0x
[Microcontroller]
MCU SPI module + 74LS164 driver digital tube
Simulate SPI implementation and debugging process
Generally speaking, all SPI communication devices can be implemented using analog SPI, and the advantage of analog SPI is that there is no need to re-familiarize yourself with the configuration of the SPI controller for each MCU. You only need to simply configure the input and output of the four pins SPI_CLK, SPI_Cs,
[Microcontroller]
Embedded file system data storage module for MC9S12UF32
Introduction With the development of information technology, the embedded system's simple way of reading and writing storage media by address and byte can no longer meet the needs of practical applications. Using file systems to manage storage media has become a development direction for embedded systems. Althou
[Microcontroller]
Embedded file system data storage module for MC9S12UF32
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号