f_lseek); //Continue to write the previous data that will not erase                                                                                                                                                           

2025/07/2021:20:34 hotcomm 1253

f_lseek); //Continue to write the previous data that will not erase                                                                                                                                                            - DayDayNews. Open the CubeMX new file, select the microcontroller chip

2. Set the system frequency: See the figure below

f_lseek); //Continue to write the previous data that will not erase                                                                                                                                                            - DayDayNews

f_lseek); //Continue to write the previous data that will not erase                                                                                                                                                            - DayDayNews

3. Set SPI3, select the settings as host mode, and the others are fine.

f_lseek); //Continue to write the previous data that will not erase                                                                                                                                                            - DayDayNews

4. Select FATFS and check User-defined. All other default settings are fine (note that the maximum and minimum values of the partition are 512).

f_lseek); //Continue to write the previous data that will not erase                                                                                                                                                            - DayDayNews

5. The above configuration is basically completed. Now the final configuration of the generated project is:

f_lseek); //Continue to write the previous data that will not erase                                                                                                                                                            - DayDayNews

f_lseek); //Continue to write the previous data that will not erase                                                                                                                                                            - DayDayNews

Complete the above settings and click "GENERATE COOD" to generate the project.

6. First set the SD card driver and add two functions to the spi.c file: (Just name the function name yourself as needed)

Note: Remember to declare the function in the spi.h file. Spi configuration and pin initialization have been completed automatically without having to configure again.

f_lseek); //Continue to write the previous data that will not erase                                                                                                                                                            - DayDayNews

7. Create a new SD_Driver.c and SD_Driver.h file (implementing the driver for the SD card).

mainly includes:

void SD_CS(uint8_t p) //SPI3 pin control uint8_t SD_GETCID(uint8_t *cid_data) //Read CID information of the SD card uint8_t SD_GETCSD(uint8_t *cid_data) //Read CID information of the SD card uint32_t SD_GetSectorCount(void) //Read the number of sectors of the SD card uint8_t SD_init(void) //SD card initialization function uint8_t SD_ReadDisk(uint8_t*buf,uint32_t sector,uint8_t cnt) //Read data from the SD card uint8_t SD_ReceiveData(uint8_t *data, uint16_t len) //Read data of the specified length in the SD card uint8_t SD_SendBlock(uint8_t*buf,uint8_t cmd) //Write a 512-byte data to the SD card int SD_sendcmd(uint8_t cmd,uint32_t arg,uint8_t crc) //Send commands to the SD card through SPI uint8_t SD_WriteDisk(uint8_t*buf,uint32_t sector,uint8_t cnt) //Write data to the SD card 

8. Fill in the "user_diskio.c" file with the following five functions to realize the association between the file system and SD.

USER_initialize(); USER_ioctl(); USER_read(); USER_status(); USER_write();

If necessary, fill in fatfs.c with get_fattime(); function set date

DWORD get_fattime (void) { return ((2010UL-1980) 25) /* Year = 2010 */ | (11UL 21) /* Month = 11 */ | (2UL 16) /* Day = 2 */ | (15U 11) /* Hour = 15 */ | (0U 5) /* Min = 0 */ | (0U 1) /* Sec = 0 */ ; }

9. This is basically the configuration! The following is to implement the reading and writing of the SD card in the main function;

//Define some variables FATFS fs; FIL file; uint8_t res=0; UINT Br,Bw; char path[4]="0:"; uint8_t testBuffer[]="SD¿¨Ð´ëÖÐÓ¢ÎIJâÊÔ,SD card Chinese and English reading and writing test!! \r\n"; uint8_t ReadBuffer[512]; char success[]="Data writing Ok!\r\n"; char error[]="error!\r\n"; char mount[]="File system mount successfully! \r\n"; char open[]="ÎFile is open! \r\n"; int main(){ /***No more writing in various initializations***/ res=f_mount(&fs,"0:",0); //mount the file system if(res!=FR_OK){ HAL_UART_Transmit(&huaart2,(uint8_t *) &error,sizeof(error),100); }else{ HAL_UART_Transmit(&huaart2,(uint8_t *) &mount,sizeof(mount),100); } while(1) { if(f_open(&file,"hello.txt",FA_OPEN_ALWAYS|FA_WRITE)==FR_OK){ HAL_UART_Transmit(&huaart2,(uint8_t *) &open,sizeof(open),100); f_lseek(&file, f_size(&file)); //Then write the previous data that will not be erased if(f_write(&file,testBuffer,sizeof(testBuffer),&Bw)==FR_OK){ HAL_UART_Transmit(&huaart2,(uint8_t *) &success,sizeof(success),100); f_close(&file); //Be sure to close the file }}} }

hotcomm Category Latest News