银杏科技有限公司旗下技术文档发布平台 | |||
技术支持电话 | 0379-69926675-801 | ||
技术支持邮件 | Gingko@vip.163.com | ||
版本 | 日期 | 作者 | 修改内容 |
---|---|---|---|
V1.0 | 2020-07-10 | gingko | 初次建立 |
===== 实验三十:U_DISK_IAP_FPGA实验——更新升级FPGA =====e
int main(void) { /* MCU 配置*/ /* 重置所有外设, 初始化Flash接口和Systick. */ HAL_Init(); /* 配置系统时钟*/ SystemClock_Config(); /* 初始化所有已配置的外设 */ MX_GPIO_Init(); MX_USB_HOST_Init(); MX_FATFS_Init(); while (1) { MX_USB_HOST_Process(); } }
void MX_USB_HOST_Init(void) { /* 初始化主机库,添加支持的类并启动该库*/ USBH_Init(&hUsbHostHS, USBH_UserProcess, HOST_HS); USBH_RegisterClass(&hUsbHostHS, USBH_MSC_CLASS); USBH_Start(&hUsbHostHS); } /*初始化HOST 内核 */ USBH_StatusTypeDef USBH_Init(USBH_HandleTypeDef *phost, void (*pUsrFunc)(USBH_HandleTypeDef *phost, uint8_t ), uint8_t id) { /* 检查USB主机句柄是否有效*/ if(phost == NULL) { USBH_ErrLog("Invalid Host handle"); return USBH_FAIL; } /* 设置DRiver ID */ phost->id = id; /* 取消连结类别*/ phost->pActiveClass = NULL; phost->ClassNumber = 0; /* 恢复默认状态并准备EP0 */ DeInitStateMachine(phost); /* 分配用户进程 */ if(pUsrFunc != NULL) { phost->pUser = pUsrFunc; } #if (USBH_USE_OS == 1) /* 创建USB主机队列 */ osMessageQDef(USBH_Queue, 10, uint16_t); phost->os_event = osMessageCreate (osMessageQ(USBH_Queue), NULL); /*创建USB主机任务 */ #if defined (USBH_PROCESS_STACK_SIZE) osThreadDef(USBH_Thread, USBH_Process_OS, USBH_PROCESS_PRIO, 0, USBH_PROCESS_STACK_SIZE); #else osThreadDef(USBH_Thread, USBH_Process_OS, USBH_PROCESS_PRIO, 0, 8 * configMINIMAL_STACK_SIZE); #endif phost->thread = osThreadCreate (osThread(USBH_Thread), phost); #endif /*初始化底层驱动 */ USBH_LL_Init(phost); return USBH_OK; } /*启动USB Host内核*/ USBH_StatusTypeDef USBH_Start (USBH_HandleTypeDef *phost) { /* 启动底层驱动 */ USBH_LL_Start(phost); /* 在端口上激活VBUS */ USBH_LL_DriverVBUS (phost, TRUE); return USBH_OK; }
/*启动USB Host内核*/ USBH_StatusTypeDef USBH_Start (USBH_HandleTypeDef *phost) { /* 启动底层驱动 */ USBH_LL_Start(phost); /* 在端口上激活VBUS */ USBH_LL_DriverVBUS (phost, TRUE); return USBH_OK; } uint8_t FATFS_LinkDriverEx(Diskio_drvTypeDef *drv, char *path, uint8_t lun) { uint8_t ret = 1; uint8_t DiskNum = 0; if(disk.nbr <= _VOLUMES) { disk.is_initialized[disk.nbr] = 0; disk.drv[disk.nbr] = drv; disk.lun[disk.nbr] = lun; DiskNum = disk.nbr++; path[0] = DiskNum + '0'; path[1] = ':'; path[2] = '/'; path[3] = 0; ret = 0; } return ret; }
void MX_USB_HOST_Process(void) { /* USB主机后台任务 */ USBH_Process(&hUsbHostHS); } /*用户回调定义 */ static void USBH_UserProcess (USBH_HandleTypeDef *phost, uint8_t id) { int i,k; unsigned char buffer[1024]; unsigned long int ncounter = 0; unsigned int counter = 0; FIL file; FRESULT res; switch(id) { case HOST_USER_SELECT_CONFIGURATION: break; case HOST_USER_DISCONNECTION: Appli_state = APPLICATION_DISCONNECT; break; case HOST_USER_CLASS_ACTIVE: Appli_state = APPLICATION_READY; //判断f_mount是否成功 res = f_mount(&fatfs,"0:",1); if(res != RES_OK){ while(1){ //绿灯快闪 LED_GREEN_ON; for(i = 0;i < 1000000;i++); LED_GREEN_OFF; for(i = 0;i < 1000000;i++); } } res = f_open(&file,"0:/system/sram.rbf",FA_READ); //只读打开文件 if(res != FR_OK){ while(1){ //蓝灯快闪 LED_BLUE_ON; for(i = 0;i < 1000000;i++); LED_BLUE_OFF; for(i = 0;i < 1000000;i++); } } res = f_lseek(&file,0); //移动写指针到文件首 if(res != FR_OK){ //白色 LED_RED_ON; LED_BLUE_ON; LED_GREEN_ON; while(1){ } } //开始升级FPGA程序 NCONFIG_OFF; DCLK_OFF; for(i = 0; i < 5000; i++); if(NSTATUS == 1) { LED_RED_ON; } for(i = 0;i < 40;i++); NCONFIG_ON; for(i = 0; i < 40; i++); while(ncounter < file.fsize) { res = f_read(&file,buffer,1024,&counter); //读取数据 if(res != RES_OK){ //读取失败,红灯慢闪 while(1){ LED_RED_ON; for(i = 0;i < 10000000;i++); LED_RED_OFF; for(i = 0;i < 10000000;i++); } } for(k = 0; k < counter; k++) { for(i = 0; i < 8; i++) { if(buffer[k]&0x01)DATA0_ON; else DATA0_OFF; DCLK_ON; buffer[k] >>= 1; DCLK_OFF; } ncounter++; } } if(CONFIG_DONE == 1){ LED_GREEN_ON; }else { LED_BLUE_ON; } for(i = 0; i < 40; i++) { DCLK_ON; for(i = 0; i < 800; i++); //延时 100us DCLK_OFF; for(i = 0; i < 800; i++); //延时 100us } break; case HOST_USER_CONNECTION: Appli_state = APPLICATION_START; break; default: break; } }