|
/* 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];
/*******************************************************************************
* 函數(shù)名:mStopIfError
* 輸 入: iError, 錯(cuò)誤代碼
* 輸 出:
* 功能說(shuō)明:檢查操作狀態(tài),如果錯(cuò)誤則顯示錯(cuò)誤代碼并停機(jī),
* 應(yīng)該替換為實(shí)際的處理措施,例如顯示錯(cuò)誤信息,等待用戶確認(rèn)后重試等
*******************************************************************************/
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 ) /* 查詢磁盤(pán)文件格式信息,扇區(qū)數(shù) */
{
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結(jié)構(gòu)的DiskQuery */
xWriteCH376Cmd( CMD01_RD_USB_DATA0 );
xReadCH376Data( ); /* 長(zhǎng)度總是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盤(pán)是否連接,等待U盤(pán)插入,對(duì)于SD卡,可以由單片機(jī)直接查詢SD卡座的插拔狀態(tài)引腳 */
{
printf("1212121:\r\n");
mDelaymS( 100 );
}
printf("8888888888\r\n");
mDelaymS( 200 ); /* 延時(shí),可選操作,有的USB存儲(chǔ)器需要幾十毫秒的延時(shí) */
/* 對(duì)于檢測(cè)到USB設(shè)備的,最多等待100*50mS,主要針對(duì)有些MP3太慢,對(duì)于檢測(cè)到USB設(shè)備并且連接DISK_MOUNTED的,最多等待5*50mS,主要針對(duì)DiskReady不過(guò)的 */
for ( i = 0; i < 100; i ++ )
{ /* 最長(zhǎng)等待時(shí)間,100*50mS */
mDelaymS( 50 );
s = CH376DiskMount( ); /* 初始化磁盤(pán)并測(cè)試磁盤(pán)是否就緒 */
if ( s == USB_INT_SUCCESS ) break; /* 準(zhǔn)備好 */
else if ( s == ERR_DISK_DISCON ) break; /* 檢測(cè)到斷開(kāi),重新檢測(cè)并計(jì)時(shí) */
if ( CH376GetDiskStatus( ) >= DEF_DISK_MOUNTED && i >= 5 ) break; /* 有的U盤(pán)總是返回未準(zhǔn)備好,不過(guò)可以忽略,只要其建立連接MOUNTED且嘗試5*50mS */
}
if ( CH376GetDiskStatus( ) < DEF_DISK_MOUNTED )
{ /* 未知USB設(shè)備,例如USB鍵盤(pán)、打印機(jī)等 */
// printf( "Unknown device\n" );
while ( CH376DiskConnect( ) == USB_INT_SUCCESS )
{ /* 檢查U盤(pán)是否連接,等待U盤(pán)拔出 */
mDelaymS( 100 );
}
}
// i = CH376ReadBlock( buf ); /* 如果需要,可以讀取數(shù)據(jù)塊CH376_CMD_DATA.DiskMountInq,返回長(zhǎng)度 */
// if ( i == sizeof( INQUIRY_DATA ) )
// { /* U盤(pán)的廠商和產(chǎn)品信息 */
// buf[ i ] = 0;
// printf( "SDdiskInfo: %s\n", ((P_INQUIRY_DATA)buf) -> VendorIdStr );
// }
strcpy( buf, "\\*" ); //列出根目錄下的文件
s = CH376FileOpenPath( buf ); /* 枚舉多級(jí)目錄下的文件或者目錄,輸入緩沖區(qū)必須在RAM中 */
while ( s == USB_INT_DISK_READ ) /* 枚舉到匹配的文件 */
{
CH376ReadBlock( buf ); /* 讀取枚舉到的文件的FAT_DIR_INFO結(jié)構(gòu),返回長(zhǎng)度總是sizeof( FAT_DIR_INFO ) */
pDir = (P_FAT_DIR_INFO)buf; /* 當(dāng)前文件目錄信息 */
if ( pDir -> DIR_Name[0] != '.' ) /* 不是本級(jí)或者上級(jí)目錄名則繼續(xù),否則必須丟棄不處理 */
{
if ( pDir -> DIR_Name[0] == 0x05 ) pDir -> DIR_Name[0] = 0xE5; /* 特殊字符替換 */
pDir -> DIR_Attr = 0; /* 強(qiáng)制文件名字符串結(jié)束以便打印輸出 */
printf( "*** EnumName: %s\n", pDir -> DIR_Name ); /* 打印名稱(chēng),原始8+3格式,未整理成含小數(shù)點(diǎn)分隔符 */
}
xWriteCH376Cmd( CMD0H_FILE_ENUM_GO ); /* 繼續(xù)枚舉文件和目錄 */
xEndCH376Cmd( );
s = Wait376Interrupt( );
}
if ( s != ERR_MISS_FILE ) mStopIfError( s ); /* 操作出錯(cuò) */
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
2020-9-17 18:26 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
218.23 KB, 下載次數(shù): 33, 下載積分: 黑幣 -5
|