中级会员
 
- 积分
- 214
- 金钱
- 214
- 注册时间
- 2014-4-30
- 在线时间
- 26 小时
|
程序能读出扇区(不知道读的对不对),但是移植后就是不行,返回错误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;
}
|
|