|
/* 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
2020-9-17 18:26 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
218.23 KB, 下載次數: 33, 下載積分: 黑幣 -5
|