Abstract: The remote intelligent heart detector based on STM32F103XX microcontroller uses Micro SD as the storage medium to save specific ECG signals. The data stored in the Micro SD card should be directly accessible on the computer, and the data to be stored should be written to the Micro SD card in FAT32 file format. The software method and hardware design of writing data to Micro SD using the STM32F103XX microprocessor and the SPI mode and Micro SD interface are studied. The FAT open source file system FATFS is specially designed for small embedded systems. It is easy to migrate and use, occupies relatively small hardware resources and has powerful functions.
Keywords: STM32F1103XX; Micro SD; SPI; FATFS
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 storage 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 4 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 active slave enable signal (cs). The biggest feature of SPI is that the communication between 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 Micro SD card can support two operation modes: SDIO mode and SPI mode. Designers can choose any of them. SDIO mode allows 4-wire high-speed data transmission with high transmission rate, but most microcontrollers do not have this interface, and it is complicated to use software to simulate the protocol. 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 compared with SDIO mode is the loss of transmission speed, but the processing speed of microprocessors is getting higher and higher. Most of the
engineering needs can be met by using SPI mode.
Micro SD card requires full-duplex, 8-bit SPI operation. Only 4 signal lines are needed between STM32F103XX microprocessor and Micro SD card to complete data reading/writing. When the CS chip select signal line is low, the microprocessor starts all bus transmission. Data is synchronously input from the MOSI pin of the microprocessor to the DI pin of the Micro SD card, and synchronously input from the DO line of the Micro SD card to the MISO pin of the microprocessor. 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 10-100 kΩ pull-up resistors on the microprocessor side. The hardware connection circuit is shown in Figure 1.
2 Software Design
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.
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.
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.
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 can be 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 item 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 item. 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 item, 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 item. 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 file can be read under Windows, which ensures high cost performance while facilitating further analysis and processing of the data. Writing a data block from the Micro SD card is similar to reading a data block. The host sends the CMD24 (MSD_WRITE_BLOCK) command to start the write operation process, and the Micro SD card will respond in the R1 command response format. If the command is responded, 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.
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 file system of the Micro SD card must be consistent with the file system of the computer. Currently, the commonly used file systems are mainly Microsoft's FATl2, FATl6, FAT32, NTFS, and EXT2 and EXT3 under the Linux system. Due to the widespread application of Microsoft Windows, the FAT file system is still the most used in current consumer electronic products, such as USB flash drives, MP3, MP4, digital cameras, etc., so it is very important for microcontroller system designers to find an easy-to-port and use FAT open source file system that occupies relatively small hardware resources and has powerful functions. Therefore, the FAT FS file system is ported to the Micro SD card storage mechanism. FAT FS is a completely free and open source FAT file system module designed specifically for small embedded systems. It is completely written in standard C language, so it has good hardware platform independence and can be ported to multiple platforms with only simple modifications. What users need to write porting 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.
The FAT FS Module was designed to be used on different microcontrollers, so it has a good hierarchical structure, as shown in Figure 4. The top layer is the application layer. Users do not need to pay attention to the internal structure of the FAT FS Module and the complex FAT protocol. They only need to call a series of application interface functions provided by the Module to the user, 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, the next free cluster is searched in a loop, and the current FAT item is modified 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 directory name of the new directory to the CreateDir() function. The process is the same as creating a file, except that no data-related information is required.
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) according to 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 the last sector. When applying for a new free FAT table entry 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 Deleting 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 the FDT for a file with the same name. 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 needs to provide the function DeleteDir() to delete the directory name. The program first searches the FDT for a directory entry with the same name. If there is a directory entry with the same name, the first byte content in the corresponding FDT table entry is changed to 0xE5H. Then read 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 file can be read under Windows, which ensures high cost performance and facilitates further analysis and processing of the data
.
Previous article:Design of high-precision and low-power ADC in touch screen controller chip
Next article:Design of Data Acquisition System Based on C8051F340
- Popular Resources
- Popular amplifiers
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- CGD and Qorvo to jointly revolutionize motor control solutions
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Nidec Intelligent Motion is the first to launch an electric clutch ECU for two-wheeled vehicles
- Bosch and Tsinghua University renew cooperation agreement on artificial intelligence research to jointly promote the development of artificial intelligence in the industrial field
- GigaDevice unveils new MCU products, deeply unlocking industrial application scenarios with diversified products and solutions
- Advantech: Investing in Edge AI Innovation to Drive an Intelligent Future
- CGD and QORVO will revolutionize motor control solutions
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- EEWORLD University - Multiphase Buck Regulator Design: Case Study
- I need help from the experts on writing MAX7219 dot matrix in microPython
- 【Top Micro Intelligent Display Module】IV: Serial port interaction and application of curve, drawing board and animation controls
- Last few hours: NXP i.MX answer questions and win AI-IoT series books, 10 books left, not all of them are mine~
- EEWORLD University Hall - Sharing of household heating and cooling inverter air conditioner application solutions based on Lingdong MM32SPIN series MCU
- Passband gain
- Steps and precautions for live load verification of relay protection devices
- The Pinouts Book: A Free eBook Every Electronics Engineer Should Have
- Please tell me the principle of this filter circuit
- Answer the questions to win prizes! ADI Application Tour - Battery Management and Smart Energy Storage