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.
[page]
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.
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.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.
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
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- [Silicon Labs Development Kit Review] +PDM Stereo Microphone SPK0641HT4H-1
- Embedded Qt-Realize switching between two windows
- Some unused boards
- 【2022 Digi-Key Innovation Design Competition-Smart Study Room Based on Raspberry Pi
- 【NXP Rapid IoT Review】+ Trial Computer Programming & Mobile Phone Control
- TMS320C6747 Fixed-point/Floating-point Digital Signal Processor
- Chip Manufacturing 7-Chip Manufacturing
- FPGA water lamp design vhdl source program
- USB packet capture module protocol
- The PWM wave output of TL494 in the boost circuit is abnormal. Please help me