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:
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.
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
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- 8 output modes of MSP430 microcontroller
- Audio circuit delay
- Anti-interference issues of infrared sensors in obstacle detection applications
- Selected Technical Questions and Answers on National Semiconductor's Switching Voltage Regulators
- Synopsys Releases Latest Version of IC Compiler, Its Automated Layout Design Tool
- Problems with RS485 communication self-transceiver circuit
- Many netizens recommend: Yatli AT-START-F403A, you deserve it! The event will be online soon, so stay tuned~
- What is SAR and how to test the SAR of mobile phone antennas
- The mobile station adds a new board, the STM32F723 Discovery Kit
- [Teardown of the car wireless charger] - Disassembling the Deli wireless car charger