|
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <stdio.h>
#include "FILE_SYS.H"
#include "usart.H"
#include "string.h"
void Delay(__IO uint32_t nCount);
void GPIOInit(void);
void mStopIfError(uint8_t iError);
UINT8 CH376DiskFat( void );
unsigned char buf[64];
/*******************************************************************************
* 函数名:mStopIfError
* 输 入: iError, 错误代码
* 输 出:
* 功能说明:检查操作状态,如果错误则显示错误代码并停机,
* 应该替换为实际的处理措施,例如显示错误信息,等待用户确认后重试等
*******************************************************************************/
void mStopIfError(uint8_t iError)
{
if (iError == USB_INT_SUCCESS)
{
return; /* 操作成功 */
}
while(1)
{
// printf("212123\r\n");
/* LED闪烁 */
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
mDelaymS( 200 );
GPIO_SetBits(GPIOB, GPIO_Pin_12);
mDelaymS( 200 );
}
}
UINT8 CH376DiskFat( void ) /* 查询磁盘文件格式信息,扇区数 */
{
UINT8 s, i;
#ifndef DEF_IC_V43_U
xWriteCH376Cmd( CMD01_GET_IC_VER );
if ( xReadCH376Data( ) < 0x43 ) {
if ( CH376ReadVar8( VAR_DISK_STATUS ) >= DEF_DISK_READY ) CH376WriteVar8( VAR_DISK_STATUS, DEF_DISK_MOUNTED );
}
#endif
s = CH376SendCmdWaitInt( CMD0H_DISK_QUERY );
if ( s == USB_INT_SUCCESS ) { /* 参考CH376INC.H文件中CH376_CMD_DATA结构的DiskQuery */
xWriteCH376Cmd( CMD01_RD_USB_DATA0 );
xReadCH376Data( ); /* 长度总是sizeof(CH376_CMD_DATA.DiskQuery) */
xReadCH376Data( ); /* CH376_CMD_DATA.DiskQuery.mTotalSector */
xReadCH376Data( );
xReadCH376Data( );
xReadCH376Data( );
xReadCH376Data( ); /* CH376_CMD_DATA.DiskQuery.mFreeSector */
xReadCH376Data( );
xReadCH376Data( );
xReadCH376Data( );
i = xReadCH376Data( ); /* CH376_CMD_DATA.DiskQuery.mDiskFat */
xEndCH376Cmd( );
}
else i = 5;
return( i );
}
int main(void)
{
unsigned char i,s;
P_FAT_DIR_INFO pDir;
GPIOInit();
USART_init();
//printf("\r\n TEST \r\n");
s = mInitCH376Host(); /* 初始化CH376 */
mStopIfError( s );
while ( CH376DiskConnect( ) != USB_INT_SUCCESS ) /* 检查U盘是否连接,等待U盘插入,对于SD卡,可以由单片机直接查询SD卡座的插拔状态引脚 */
{
printf("1212121:\r\n");
mDelaymS( 100 );
}
printf("8888888888\r\n");
mDelaymS( 200 ); /* 延时,可选操作,有的USB存储器需要几十毫秒的延时 */
/* 对于检测到USB设备的,最多等待100*50mS,主要针对有些MP3太慢,对于检测到USB设备并且连接DISK_MOUNTED的,最多等待5*50mS,主要针对DiskReady不过的 */
for ( i = 0; i < 100; i ++ )
{ /* 最长等待时间,100*50mS */
mDelaymS( 50 );
s = CH376DiskMount( ); /* 初始化磁盘并测试磁盘是否就绪 */
if ( s == USB_INT_SUCCESS ) break; /* 准备好 */
else if ( s == ERR_DISK_DISCON ) break; /* 检测到断开,重新检测并计时 */
if ( CH376GetDiskStatus( ) >= DEF_DISK_MOUNTED && i >= 5 ) break; /* 有的U盘总是返回未准备好,不过可以忽略,只要其建立连接MOUNTED且尝试5*50mS */
}
if ( CH376GetDiskStatus( ) < DEF_DISK_MOUNTED )
{ /* 未知USB设备,例如USB键盘、打印机等 */
// printf( "Unknown device\n" );
while ( CH376DiskConnect( ) == USB_INT_SUCCESS )
{ /* 检查U盘是否连接,等待U盘拔出 */
mDelaymS( 100 );
}
}
// i = CH376ReadBlock( buf ); /* 如果需要,可以读取数据块CH376_CMD_DATA.DiskMountInq,返回长度 */
// if ( i == sizeof( INQUIRY_DATA ) )
// { /* U盘的厂商和产品信息 */
// buf[ i ] = 0;
// printf( "SDdiskInfo: %s\n", ((P_INQUIRY_DATA)buf) -> VendorIdStr );
// }
strcpy( buf, "\\*" ); //列出根目录下的文件
s = CH376FileOpenPath( buf ); /* 枚举多级目录下的文件或者目录,输入缓冲区必须在RAM中 */
while ( s == USB_INT_DISK_READ ) /* 枚举到匹配的文件 */
{
CH376ReadBlock( buf ); /* 读取枚举到的文件的FAT_DIR_INFO结构,返回长度总是sizeof( FAT_DIR_INFO ) */
pDir = (P_FAT_DIR_INFO)buf; /* 当前文件目录信息 */
if ( pDir -> DIR_Name[0] != '.' ) /* 不是本级或者上级目录名则继续,否则必须丢弃不处理 */
{
if ( pDir -> DIR_Name[0] == 0x05 ) pDir -> DIR_Name[0] = 0xE5; /* 特殊字符替换 */
pDir -> DIR_Attr = 0; /* 强制文件名字符串结束以便打印输出 */
printf( "*** EnumName: %s\n", pDir -> DIR_Name ); /* 打印名称,原始8+3格式,未整理成含小数点分隔符 */
}
xWriteCH376Cmd( CMD0H_FILE_ENUM_GO ); /* 继续枚举文件和目录 */
xEndCH376Cmd( );
s = Wait376Interrupt( );
}
if ( s != ERR_MISS_FILE ) mStopIfError( s ); /* 操作出错 */
while (1)
{
printf("212123\r\n");
GPIO_SetBits(GPIOB, GPIO_Pin_12);
/* Insert delay */
Delay(0xAFFFF);
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
/* Insert delay */
Delay(0xAFFFF);
}
}
void GPIOInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOB Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PB12 and PB13 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void Delay(__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
/*
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
|
-
-
GPIO.7z
218.23 KB, 下载次数: 32, 下载积分: 黑币 -5
|