OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 5171|回复: 2

stm32103移植fat中遇到问题,新手求大神!!!急急

[复制链接]

22

主题

49

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
214
金钱
214
注册时间
2014-4-30
在线时间
26 小时
发表于 2014-4-30 10:36:15 | 显示全部楼层 |阅读模式




程序能读出扇区(不知道读的对不对),但是移植后就是不行,返回错误res=12.

程序已经上传,请大家帮帮忙看看呀!
*********************main*********************
#include "stm32f10x.h"
#include "MMC_SD.h"
#include "delay.h"
#include <stdio.h>
#include "integer.h"
#include "diskio.h"
#include "ff.h"

USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

FATFS fs;            // Work area (file system object) for logical drive
FIL fsrc, fdst;         // file objects
BYTE buffer[512];       // file copy buffer
FRESULT res;            // FatFs function common result code
UINT br, bw;            // File R/W count  
uint8_t textFileBuffer[] = "sd card fat...\r\n";

u8 pbuf[512];



void Delay(vu32 nCount)
{
  for(; nCount != 0; nCount--);
}

void STM32_Shenzhou_COMInit(USART_InitTypeDef* USART_InitStruct)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

  /* Enable UART clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); 

  /* Configure USART Tx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART Rx as input floating */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 
  USART_Init(USART1, USART_InitStruct); /* USART configuration */
  USART_Cmd(USART1, ENABLE);  /* Enable USART */
}

int main(void)
{
   u32 sd_size;
   u8 t=0;
   u8 *buf;
   delay_init(72);
   
  /*LED管脚初始化*/
  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE); /*使能LED灯使用的GPIO时钟*/

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);  /*LED灯相关的GPIO口初始化*/
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_3);   /*关闭所有的LED指示灯*/
  GPIO_SetBits(GPIOB,GPIO_Pin_2);      

  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  STM32_Shenzhou_COMInit(&USART_InitStructure);

  printf("\n\r---------------------------------------------\n ");
  printf("\n\rMicro SD卡测试程序\n");
   
   while(SD_Init()!=0)//检测不到SD卡
{
printf("SD Card Failed!");
delay_ms(50);
printf("Please Check!      ");
delay_ms(50);
}

//检测SD卡成功
GPIO_ResetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_3);   /*打开所有的LED指示灯*/
    GPIO_ResetBits(GPIOB,GPIO_Pin_2);
delay_ms(50);
printf("\n\rSD Card Checked OK ");    
sd_size=SD_GetCapacity()/1024;
printf("\n\rSD Card Size: %ld  b\n\r",sd_size);
GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_3);   /*关闭所有的LED指示灯*/
    GPIO_SetBits(GPIOB,GPIO_Pin_2);
    if(disk_initialize(MMC)==0);
printf("\n\r SD init ok \n\r");

while(1)
{
if(SD_ReadSingleBlock(0,buf)==0)//读取MBR扇区
{
printf("USART1 Sending Data...");
delay_ms(50);
printf("SECTOR 0 DATA:\n");
for(sd_size=0;sd_size<512;sd_size++)//打印MBR扇区数据
{
printf("%x ",buf[sd_size]);
}
printf("\n\rUSART1 Send Data Over!\n");
}
GPIO_ResetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_3);   /*打开所有的LED指示灯*/
        GPIO_ResetBits(GPIOB,GPIO_Pin_2); 

delay_ms(50);
GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_3);   /*关闭所有的LED指示灯*/
        GPIO_SetBits(GPIOB,GPIO_Pin_2);


//写文件测试
printf("write file test......\n\r");
res = f_open(&fdst, "0:/test.txt", FA_CREATE_ALWAYS | FA_WRITE);
if(res != FR_OK){
       printf("open file error : %d\n\r",res);
}else{
       res = f_write(&fdst, textFileBuffer, sizeof(textFileBuffer), &bw);               /* Write it to the dst file */
       if(res == FR_OK){
               printf("write data ok! %d\n\r",bw);
       }else{
               printf("write data error : %d\n\r",res);
       }
       /*close file */
       
f_close(&fdst);
f_mount(0, NULL); 
}
}
}

PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (uint8_t) ch);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {
  }

  return ch;
}



SD20140429.zip

3.08 MB, 下载次数: 45

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

120

主题

7878

帖子

13

精华

资深版主

Rank: 8Rank: 8

积分
12012
金钱
12012
注册时间
2013-9-10
在线时间
427 小时
发表于 2014-4-30 13:08:05 | 显示全部楼层
读取一个block是有数据的,你这读取的数据不对
现在,程序把烂铜烂铁变得智能化了,人呢,一旦离开了这烂铜烂铁就不知道干啥了
回复 支持 反对

使用道具 举报

22

主题

49

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
214
金钱
214
注册时间
2014-4-30
在线时间
26 小时
 楼主| 发表于 2014-5-7 17:50:03 | 显示全部楼层
原子哥v5  移植成功了 困扰多久的问题解决了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-7-7 22:42

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表