U盤數(shù)據(jù)采集系統(tǒng)軟件篇二(U盤模塊)
本篇主要介紹U盤模塊,必須是有USB-HOST功能的芯片才可以進(jìn)行U盤數(shù)據(jù)的讀寫,在硬件設(shè)計(jì)時(shí)已經(jīng)提到了,本例用到的芯片屬于STM32F105系列是帶有OTG功能的,103系列的芯片只能作為從設(shè)備。STM32F105 USB接口主要特點(diǎn):
USB2.0協(xié)議,OTG1.3協(xié)議。
可作為USB主機(jī)、USB設(shè)備、
OTG設(shè)備(A類/B類)使用
可使用內(nèi)部FS PHY做FS通信
從官方下載USB例程,Library庫如下圖
我們需要做U盤數(shù)據(jù)的讀寫,USB做主機(jī)即HOST功能。如上圖在官方例程的Libraries目錄中。其中的USB OTG是USB Device和USB Host的基礎(chǔ)。在實(shí)際使用中,USB OTG是USB Device和USB Host的底層驅(qū)動(dòng)。本例需要移植OTG+HOST。(現(xiàn)在可以不需要移植了,通過STM32CubeMX直接由圖形界面簡(jiǎn)單配置下,生成初始化代碼,并對(duì)外設(shè)做了進(jìn)一步的抽象,讓開發(fā)人員更只專注應(yīng)用的開發(fā)。搞嵌入式還是要了解底層驅(qū)動(dòng),對(duì)吧?)
01
驅(qū)動(dòng)文件移植
移植文件如下圖:
1.1 OTG和HOST驅(qū)動(dòng)文件簡(jiǎn)要說明
本例中需要實(shí)現(xiàn)U盤讀寫需要MSC驅(qū)動(dòng)程序,MSC:(Mass storage class)大容量存儲(chǔ)類驅(qū)動(dòng)程序用于支持通用USB閃存驅(qū)動(dòng)程序,使用BOT“Bulk-Only Transport”協(xié)議和透明SCSI命令集。
1.2 MSC驅(qū)動(dòng)文件介紹
1.3 用戶文件介紹
通常所有的用戶代碼均在以上文件做修改即可,只有少數(shù)特殊處理會(huì)設(shè)計(jì)到驅(qū)動(dòng)程序源碼的修改。在配置好后,一般來說,所有修改均在usbh_usr.h/.c中。
1.4 配置文件介紹
usb_conf.h
//#define USE_USB_OTG_FS //#define USE_USB_OTG_HS //#define USE_ULPI_PHY //#define USE_EMBEDDED_PHY /****************** USB OTG HS CONFIGURATION **********************************/ // #define USB_OTG_INTERNAL_VBUS_ENABLED/****************** USB OTG FS CONFIGURATION **********************************/ /****************** USB OTG MODE CONFIGURATION ********************************///#define USE_DEVICE_MODE//#define USE_OTG_MODE
usbh_config.h
02
USB處理狀態(tài)機(jī)
下面介紹USB處理的核心狀態(tài)機(jī)
核心狀態(tài)機(jī)過程由USBH_Process函數(shù)實(shí)現(xiàn)。應(yīng)該從應(yīng)用程序主循環(huán)周期性地調(diào)用該函數(shù)。
/*** @brief USBH_Process* USB Host core main state machine process* @param None * @retval None*/void USBH_Process(USB_OTG_CORE_HANDLE *pdev , USBH_HOST *phost){ volatile USBH_Status status = USBH_FAIL; /* check for Host port events */ if ((HCD_IsDeviceConnected(pdev) == 0)&& (phost->gState != HOST_IDLE)) { if(phost->gState != HOST_DEV_DISCONNECTED) { phost->gState = HOST_DEV_DISCONNECTED; } } switch (phost->gState) { case HOST_IDLE ://在主機(jī)初始化之后,內(nèi)核在這種狀態(tài)下開始輪詢USB設(shè)備連接。 當(dāng)檢測(cè)到設(shè)備斷開連接事件時(shí)以及未發(fā)生錯(cuò)誤時(shí)也會(huì)進(jìn)入此狀態(tài)。 if (HCD_IsDeviceConnected(pdev)) { phost->gState = HOST_DEV_ATTACHED; USB_OTG_BSP_mDelay(100); } break; case HOST_DEV_ATTACHED ://當(dāng)一個(gè)設(shè)備連接時(shí),核心進(jìn)入這個(gè)狀態(tài)。 當(dāng)一個(gè)設(shè)備被檢測(cè)到時(shí),狀態(tài)機(jī)轉(zhuǎn)到HOST_ENUMERATION狀態(tài)。 phost->usr_cb->DeviceAttached(); phost->Control.hc_num_out = USBH_Alloc_Channel(pdev, 0x00); phost->Control.hc_num_in = USBH_Alloc_Channel(pdev, 0x80); /* Reset USB Device */ if ( HCD_ResetPort(pdev) == 0) { phost->usr_cb->ResetDevice(); /* Wait for USB USBH_ISR_PrtEnDisableChange() Host is Now ready to start the Enumeration */ phost->device_prop.speed = HCD_GetCurrentSpeed(pdev); phost->gState = HOST_ENUMERATION; phost->usr_cb->DeviceSpeedDetected(phost->device_prop.speed); /* Open Control pipes */ USBH_Open_Channel (pdev, phost->Control.hc_num_in, phost->device_prop.address, phost->device_prop.speed, EP_TYPE_CTRL, phost->Control.ep0size); /* Open Control pipes */ USBH_Open_Channel (pdev, phost->Control.hc_num_out, phost->device_prop.address, phost->device_prop.speed, EP_TYPE_CTRL, phost->Control.ep0size); } break; case HOST_ENUMERATION: //在這種狀態(tài)下,核心進(jìn)行USB設(shè)備的基本枚舉。 在枚舉過程結(jié)束時(shí),選擇默認(rèn)設(shè)備配置(配置0)。 /* Check for enumeration status */ if ( USBH_HandleEnum(pdev , phost) == USBH_OK) { /* The function shall return USBH_OK when full enumeration is complete */ /* user callback for end of device basic enumeration */ phost->usr_cb->EnumerationDone(); phost->gState = HOST_USR_INPUT; } break; case HOST_USR_INPUT: //這是一個(gè)中間狀態(tài),它遵循枚舉并包括等待用戶輸入以啟動(dòng)USB類操作。 /*The function should return user response true to move to class state */ if ( phost->usr_cb->UserInput() == USBH_USR_RESP_OK) { if((phost->class_cb->Init(pdev, phost))\ == USBH_OK) { phost->gState = HOST_CLASS_REQUEST; } } break; case HOST_CLASS_REQUEST: //從此狀態(tài)開始,類驅(qū)動(dòng)程序接管,并調(diào)用類請(qǐng)求狀態(tài)機(jī)以處理所有初始類控制請(qǐng)求。 完成所需的類請(qǐng)求后,內(nèi)核將移至HOST_CLASS狀態(tài)。 /* process class standard contol requests state machine */ status = phost->class_cb->Requests(pdev, phost); if(status == USBH_OK) { phost->gState = HOST_CLASS; } else { USBH_ErrorHandle(phost, status); } break; case HOST_CLASS: // 在這種狀態(tài)下,類狀態(tài)機(jī)被稱為類相關(guān)操作(非控制和控制操作) /* process class state machine */ status = phost->class_cb->Machine(pdev, phost); USBH_ErrorHandle(phost, status); break; case HOST_CTRL_XFER: //每當(dāng)需要控制轉(zhuǎn)移時(shí)就會(huì)進(jìn)入該狀態(tài) /* process control transfer state machine */ USBH_HandleControl(pdev, phost); break; case HOST_SUSPENDED: break; case HOST_ERROR_STATE: //只要有任何庫狀態(tài)機(jī)發(fā)生未恢復(fù)的錯(cuò)誤,就會(huì)進(jìn)入此狀態(tài)。 在這種情況下,調(diào)用用戶回調(diào)函數(shù)(例如,顯示未恢復(fù)的錯(cuò)誤消息)。 然后主機(jī)庫被重新初始化。 /* Re-Initilaize Host for new Enumeration */ USBH_DeInit(pdev, phost); phost->usr_cb->DeInit(); phost->class_cb->DeInit(pdev, &phost->device_prop); break; case HOST_DEV_DISCONNECTED ://設(shè)備斷開連接進(jìn)入此狀態(tài) /* Manage User disconnect operations*/ phost->usr_cb->DeviceDisconnected(); /* Re-Initilaize Host for new Enumeration */ USBH_DeInit(pdev, phost); phost->usr_cb->DeInit(); phost->class_cb->DeInit(pdev, &phost->device_prop); USBH_DeAllocate_AllChannel(pdev); phost->gState = HOST_IDLE; break; default : break; }}
MSC核心狀態(tài)機(jī),U盤讀寫操作的上層實(shí)現(xiàn),代碼如下:
int USBH_USR_MSC_Application(void){ uint16_t bytesWritten, bytesToWrite; FILINFO fno; DIR dir; /**********狀態(tài)機(jī)******************/ switch(USBH_USR_ApplicationState) { case USH_USR_FS_INIT: USBH_USR_ApplicationState = USH_USR_IDLE; /* Initialises the File System*/ if (f_mount( 0, &fatfs ) != FR_OK ) //掛載失敗 { return(-1); } if(USBH_MSC_Param.MSWriteProtect == DISK_WRITE_PROTECTED)//寫保護(hù) { return(-1); } if(f_open(&file,"TEST.txt", FA_WRITE | FA_CREATE_ALWAYS) == FR_OK)//U盤有效 { bytesToWrite = sizeof(writeTextBuff); res= f_write (&file, writeTextBuff, bytesToWrite, (void *)&bytesWritten); //寫一個(gè)測(cè)試文件 f_close(&file); if(f_open(&file,"upgrade.txt", FA_OPEN_EXISTING |FA_READ) == FR_OK)//assert { if(FR_OK == f_read(&file, read_txt_file_buff, 8, (void *)&numOfOperationBytes)) { if(read_txt_file_buff[0] == 'z' &&read_txt_file_buff[1] == 'q' &&read_txt_file_buff[2] == '7' &&read_txt_file_buff[3] == '7' &&read_txt_file_buff[4] == '7' &&read_txt_file_buff[5] == '7') { //gb_udiskupgradeflag = 1; } } f_close(&file); } f_mount(0, NULL); } else//U盤無效 { return(-1); } f_mount( 0, &fatfs ); res = f_open(&file, "main.bin", FA_OPEN_EXISTING|FA_READ); res = f_read(&file, read_txt_file_buff, 2048, (void *)&numOfOperationBytes); f_close(&file); f_mount(0, NULL); gb_udiskIsOnLine = 1;//U盤在線 gb_udiskStateChanged = 1; break; case USH_USR_FS_READ_FILE_SIZE: USBH_USR_ApplicationState = USH_USR_IDLE; if (f_mount( 0, &fatfs ) != FR_OK ) { udiskErrCode = MOUNT_ERROR; gb_udiskOperationFinished = 1; return(-1); } res = f_open(&file, pFilePath, FA_OPEN_EXISTING | FA_READ); if(res != FR_OK) { f_close(&file); f_mount(0, NULL); udiskErrCode = OPEN_FILE_ERROR; gb_udiskOperationFinished = 1; return(-1); } fileSize = file.fsize; f_close(&file); f_mount(0, NULL); udiskErrCode = UDISK_OPERATION_OK; gb_udiskOperationFinished = 1; break; case USH_USR_FS_READFILE_PART: USBH_USR_ApplicationState = USH_USR_IDLE; if ( f_mount( 0, &fatfs ) != FR_OK ) { udiskErrCode = MOUNT_ERROR; gb_udiskOperationFinished = 1; return(-1); } res = f_open(&file, pFilePath, FA_OPEN_EXISTING | FA_READ); if(res != FR_OK) { f_close(&file); f_mount(0, NULL); udiskErrCode = OPEN_FILE_ERROR; gb_udiskOperationFinished = 1; return(-1); } res = f_lseek(&file,readOffset); if(res != FR_OK) { f_close(&file); f_mount(0, NULL); udiskErrCode = SEEK_FILE_ERROR; gb_udiskOperationFinished = 1; return(-1); } res = f_read(&file, pFileData, operationLen, (void *)&numOfOperationBytes); //res = f_read(&file, read_txt_file_buff, 1024, (void *)&numOfOperationBytes); if(res != FR_OK) { f_close(&file); f_mount(0, NULL); udiskErrCode = READ_FILE_ERROR; gb_udiskOperationFinished = 1; return(-1); } if(operationLen != numOfOperationBytes) { f_close(&file); f_mount(0, NULL); udiskErrCode = OPERATION_NOT_COMPLETE; gb_udiskOperationFinished = 1; return(-1); } f_close(&file); f_mount(0, NULL); udiskErrCode = UDISK_OPERATION_OK; gb_udiskOperationFinished = 1; break; case USH_USR_FS_WRITEFILE_PART: USBH_USR_ApplicationState = USH_USR_IDLE; if ( f_mount( 0, &fatfs ) != FR_OK ) { udiskErrCode = MOUNT_ERROR; gb_udiskOperationFinished = 1; return(-1); } res = f_open(&file, pFilePath, FA_OPEN_EXISTING | FA_WRITE); if(res != FR_OK) { udiskErrCode = OPEN_FILE_ERROR; gb_udiskOperationFinished = 1; return(-1); } res = f_lseek(&file,file.fsize);//寫在尾部 if(res != FR_OK) { udiskErrCode = SEEK_FILE_ERROR; gb_udiskOperationFinished = 1; f_close(&file); f_mount(0, NULL); return(-1); } res = f_write(&file, pFileData, operationLen, (void *)&numOfOperationBytes); if(res != FR_OK) { f_close(&file); f_mount(0, NULL); udiskErrCode = WRITE_FILE_ERROR; gb_udiskOperationFinished = 1; return(-1); } if(operationLen != numOfOperationBytes) { f_close(&file); f_mount(0, NULL); udiskErrCode = OPERATION_NOT_COMPLETE; gb_udiskOperationFinished = 1; return(-1); } f_close(&file); f_mount(0, NULL); udiskErrCode = UDISK_OPERATION_OK; gb_udiskOperationFinished = 1; break; case USH_USR_FS_CREATE_FILE: USBH_USR_ApplicationState = USH_USR_IDLE; if ( f_mount( 0, &fatfs ) != FR_OK ) { udiskErrCode = MOUNT_ERROR; gb_udiskOperationFinished = 1; return(-1); } res = f_open(&file, pFilePath, FA_CREATE_ALWAYS); if(res != FR_OK) { f_close(&file); f_mount(0, NULL); udiskErrCode = OPEN_FILE_ERROR; gb_udiskOperationFinished = 1; return(-1); } f_close(&file); f_mount(0, NULL); udiskErrCode = UDISK_OPERATION_OK; gb_udiskOperationFinished = 1; break; default: break; } return(0);}
03
初始化USB HOST及主程序調(diào)用
調(diào)用如下函數(shù)初始化USB Host:
/* Init Host Library */ USBH_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, USB_OTG_HS_CORE_ID, &USB_Host, &USBH_MSC_cb, &USR_cb);
主程序循環(huán)調(diào)用USBH_Process
/* Host Task handler */ USBH_Process(&USB_OTG_Core, &USB_Host);
/ The End /
本文主要介紹了在STM32F105平臺(tái)上USB HOST MSC驅(qū)動(dòng)移植和介紹。后續(xù)還會(huì)對(duì)程序中用到的文件系統(tǒng)FATFS部分進(jìn)行講解。
本文由【嵌入式案例Show】原創(chuàng)出品,未經(jīng)許可,請(qǐng)勿轉(zhuǎn)載
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺(tái)僅提供信息存儲(chǔ)服務(wù)。文章僅代表作者個(gè)人觀點(diǎn),不代表本平臺(tái)立場(chǎng),如有問題,請(qǐng)聯(lián)系我們,謝謝!