高级会员

- 积分
- 570
- 金钱
- 570
- 注册时间
- 2016-3-1
- 在线时间
- 84 小时
|
50金钱
本帖最后由 feiyinglala 于 2025-5-25 10:01 编辑
背景:STM32F429,使用SDIO端口驱动SD卡问题和现象:
1.在main函数中SD卡初始化、往里边写TXT都是正常的
2.在我自己的数据记录函数初始化中,同样的语句,就运行不下去了,用仿真跟踪发现在f_open()函数跑飞的,仿真进去参数对比如下图
主函数代码如下
- while(SD_Init()) //检测不到SD卡
- {//SD_Init 中后续若需要,添加DMA代码
- printf("SD Card Error!\r\n");
- }
- show_sdcard_info(); //打印SD卡相关信息
- usmart_dev.init(84); //初始化USMART【FAT用】初始化串口控制器84MHz
- exfuns_init(); //【要】为fatfs相关变量申请内存(为每个磁盘、FILE/FTEMP/FATBUF各申请一块内存)
- f_mount(fs[0],"0:",1); //【要】挂载SD卡 来自ff.c
- FAT_res=f_mount(fs[1],"1:",1); //挂载FLASH. 来自ff.c
- if(FAT_res==0X0D)//FLASH磁盘,FAT文件系统错误,重新格式化FLASH
- {
- FAT_res=f_mkfs("1:",1,4096);//格式化FLASH,1,盘符;1,不需要引导区,8个扇区为1个簇
- if(FAT_res==0)
- {
- f_setlabel((const TCHAR *)"1:ALIENTEK"); //设置Flash磁盘的名字为:ALIENTEK
- }else
- Delay_ms(1000);
- }
- <blockquote>res=f_open(&fil,"0:/message20250524.txt",FA_CREATE_ALWAYS|FA_WRITE);
复制代码 数据记录函数初始化
- // 初始化SD卡日志系统
- void SDLogger_Init(void) {
- FRESULT res;
- FIL fil_CSV;
- char filename[32];
- uint32_t counter = 0;
-
- res=f_open(&fil_CSV,"0:/message20250526.txt",FA_CREATE_ALWAYS|FA_WRITE);
- f_write(&fil_CSV,"Hello Fatfs!Welcome to your UAV trip! CSV\r\n",41,&bw); //文件指针、写入数据指针、字符数、
- f_close(&fil_CSV);
- }
复制代码
fopen函数代码如下
- FRESULT f_open (
- FIL* fp, /* Pointer to the blank file object */
- const TCHAR* path, /* Pointer to the file name */
- BYTE mode /* Access mode and file open mode flags */
- )
- {
- FRESULT res;
- DIR dj;
- BYTE *dir;
- DEF_NAMEBUF;
- if (!fp) return FR_INVALID_OBJECT;
- fp->fs = 0; /* Clear file object */
- /* Get logical drive number */
- #if !_FS_READONLY
- mode &= FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW;
- res = find_volume(&dj.fs, &path, (BYTE)(mode & ~FA_READ));
- #else
- mode &= FA_READ;
- res = find_volume(&dj.fs, &path, 0);
- #endif
- if (res == FR_OK) {
- INIT_BUF(dj);
- res = follow_path(&dj, path); /* Follow the file path */
- dir = dj.dir;
- #if !_FS_READONLY /* R/W configuration */
- if (res == FR_OK) {
- if (!dir) /* Default directory itself */
- res = FR_INVALID_NAME;
- #if _FS_LOCK
- else
- res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
- #endif
- }
- /* Create or Open a file */
- if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {
- DWORD dw, cl;
- if (res != FR_OK) { /* No file, create new */
- if (res == FR_NO_FILE) /* There is no file to open, create a new entry */
- #if _FS_LOCK
- res = enq_lock() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;
- #else
- res = dir_register(&dj);
- #endif
- mode |= FA_CREATE_ALWAYS; /* File is created */
- dir = dj.dir; /* New entry */
- }
- else { /* Any object is already existing */
- if (dir[DIR_Attr] & (AM_RDO | AM_DIR)) { /* Cannot overwrite it (R/O or DIR) */
- res = FR_DENIED;
- } else {
- if (mode & FA_CREATE_NEW) /* Cannot create as new file */
- res = FR_EXIST;
- }
- }
- if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) { /* Truncate it if overwrite mode */
- dw = get_fattime(); /* Created time */
- ST_DWORD(dir+DIR_CrtTime, dw);
- dir[DIR_Attr] = 0; /* Reset attribute */
- ST_DWORD(dir+DIR_FileSize, 0); /* size = 0 */
- cl = ld_clust(dj.fs, dir); /* Get start cluster */
- st_clust(dir, 0); /* cluster = 0 */
- dj.fs->wflag = 1;
- if (cl) { /* Remove the cluster chain if exist */
- dw = dj.fs->winsect;
- res = remove_chain(dj.fs, cl);
- if (res == FR_OK) {
- dj.fs->last_clust = cl - 1; /* Reuse the cluster hole */
- res = move_window(dj.fs, dw);
- }
- }
- }
- }
- else { /* Open an existing file */
- if (res == FR_OK) { /* Follow succeeded */
- if (dir[DIR_Attr] & AM_DIR) { /* It is a directory */
- res = FR_NO_FILE;
- } else {
- if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */
- res = FR_DENIED;
- }
- }
- }
- if (res == FR_OK) {
- if (mode & FA_CREATE_ALWAYS) /* Set file change flag if created or overwritten */
- mode |= FA__WRITTEN;
- fp->dir_sect = dj.fs->winsect; /* Pointer to the directory entry */
- fp->dir_ptr = dir;
- #if _FS_LOCK
- fp->lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
- if (!fp->lockid) res = FR_INT_ERR;
- #endif
- }
- #else /* R/O configuration */
- if (res == FR_OK) { /* Follow succeeded */
- dir = dj.dir;
- if (!dir) { /* Current directory itself */
- res = FR_INVALID_NAME;
- } else {
- if (dir[DIR_Attr] & AM_DIR) /* It is a directory */
- res = FR_NO_FILE;
- }
- }
- #endif
- FREE_BUF();
- if (res == FR_OK) {
- fp->flag = mode; /* File access mode */
- fp->err = 0; /* Clear error flag */
- fp->sclust = ld_clust(dj.fs, dir); /* File start cluster */
- fp->fsize = LD_DWORD(dir+DIR_FileSize); /* File size */
- fp->fptr = 0; /* File pointer */
- fp->dsect = 0;
- #if _USE_FASTSEEK
- fp->cltbl = 0; /* Normal seek mode */
- #endif
- fp->fs = dj.fs; /* Validate file object */
- fp->id = fp->fs->id;
- }
- }
- LEAVE_FF(dj.fs, res);
- }
复制代码
|
|