U盘数据采集系统软件篇二(U盘模块)
扫描二维码
随时随地手机看文章
本篇主要介绍U盘模块,必须是有USB-HOST功能的芯片才可以进行U盘数据的读写,在硬件设计时已经提到了,本例用到的芯片属于STM32F105系列是带有OTG功能的,103系列的芯片只能作为从设备。STM32F105 USB接口主要特点:
USB2.0协议,OTG1.3协议。
可作为USB主机、USB设备、
OTG设备(A类/B类)使用
可使用内部FS PHY做FS通信
从官方下载USB例程,Library库如下图
我们需要做U盘数据的读写,USB做主机即HOST功能。如上图在官方例程的Libraries目录中。其中的USB OTG是USB Device和USB Host的基础。在实际使用中,USB OTG是USB Device和USB Host的底层驱动。本例需要移植OTG+HOST。(现在可以不需要移植了,通过STM32CubeMX直接由图形界面简单配置下,生成初始化代码,并对外设做了进一步的抽象,让开发人员更只专注应用的开发。搞嵌入式还是要了解底层驱动,对吧?)
01
驱动文件移植
移植文件如下图:
1.1 OTG和HOST驱动文件简要说明
本例中需要实现U盘读写需要MSC驱动程序,MSC:(Mass storage class)大容量存储类驱动程序用于支持通用USB闪存驱动程序,使用BOT“Bulk-Only Transport”协议和透明SCSI命令集。
1.2 MSC驱动文件介绍
1.3 用户文件介绍
通常所有的用户代码均在以上文件做修改即可,只有少数特殊处理会设计到驱动程序源码的修改。在配置好后,一般来说,所有修改均在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处理状态机
下面介绍USB处理的核心状态机
核心状态机过程由USBH_Process函数实现。应该从应用程序主循环周期性地调用该函数。
/*** @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 ://在主机初始化之后,内核在这种状态下开始轮询USB设备连接。 当检测到设备断开连接事件时以及未发生错误时也会进入此状态。 if (HCD_IsDeviceConnected(pdev)) { phost->gState = HOST_DEV_ATTACHED; USB_OTG_BSP_mDelay(100); } break; case HOST_DEV_ATTACHED ://当一个设备连接时,核心进入这个状态。 当一个设备被检测到时,状态机转到HOST_ENUMERATION状态。 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: //在这种状态下,核心进行USB设备的基本枚举。 在枚举过程结束时,选择默认设备配置(配置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: //这是一个中间状态,它遵循枚举并包括等待用户输入以启动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: //从此状态开始,类驱动程序接管,并调用类请求状态机以处理所有初始类控制请求。 完成所需的类请求后,内核将移至HOST_CLASS状态。 /* 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: // 在这种状态下,类状态机被称为类相关操作(非控制和控制操作) /* process class state machine */ status = phost->class_cb->Machine(pdev, phost); USBH_ErrorHandle(phost, status); break; case HOST_CTRL_XFER: //每当需要控制转移时就会进入该状态 /* process control transfer state machine */ USBH_HandleControl(pdev, phost); break; case HOST_SUSPENDED: break; case HOST_ERROR_STATE: //只要有任何库状态机发生未恢复的错误,就会进入此状态。 在这种情况下,调用用户回调函数(例如,显示未恢复的错误消息)。 然后主机库被重新初始化。 /* 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 ://设备断开连接进入此状态 /* 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核心状态机,U盘读写操作的上层实现,代码如下:
int USBH_USR_MSC_Application(void){ uint16_t bytesWritten, bytesToWrite; FILINFO fno; DIR dir; /**********状态机******************/ 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)//写保护 { 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); //写一个测试文件 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及主程序调用
调用如下函数初始化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);
主程序循环调用USBH_Process
/* Host Task handler */ USBH_Process(&USB_OTG_Core, &USB_Host);
/ The End /
本文主要介绍了在STM32F105平台上USB HOST MSC驱动移植和介绍。后续还会对程序中用到的文件系统FATFS部分进行讲解。
本文由【嵌入式案例Show】原创出品,未经许可,请勿转载
免责声明:本文内容由21ic获得授权后发布,版权归原作者所有,本平台仅提供信息存储服务。文章仅代表作者个人观点,不代表本平台立场,如有问题,请联系我们,谢谢!