【iCore1S 双核心板_ARM】例程十六:USB_MSC实验——虚拟U盘
实验步骤:
1、将SD卡插在SD卡槽中。
2、将跳线冒跳至USB_Device,将USB_Device通过Micor USB线与USB主机(电脑)相连。
3、烧写程序,我的电脑中将出现一个磁盘。
实验现象:
核心代码:
int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_SDIO_SD_Init(); MX_USB_DEVICE_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ LED_RED_ON; LED_GREEN_OFF; LED_BLUE_OFF; HAL_Delay(500); LED_RED_OFF; LED_GREEN_ON; LED_BLUE_OFF; HAL_Delay(500); LED_RED_OFF; LED_GREEN_OFF; LED_BLUE_ON; HAL_Delay(500); } /* USER CODE END 3 */ }
/******************************************************************************* * Function Name : STORAGE_Read_FS * Description : * Input : None. * Output : None. * Return : None. *******************************************************************************/ int8_t STORAGE_Read_FS (uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) { /* USER CODE BEGIN 6 */ uint32_t timeout = 10000; if(BSP_SD_ReadBlocks((uint32_t*)buf, blk_addr, blk_len, timeout) == USBD_OK){ while(BSP_SD_GetCardState()!= MSD_OK) { if (timeout-- == 0) { return -1; } } } return (USBD_OK); /* USER CODE END 6 */ } /******************************************************************************* * Function Name : STORAGE_Write_FS * Description : * Input : None. * Output : None. * Return : None. *******************************************************************************/ int8_t STORAGE_Write_FS (uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) { /* USER CODE BEGIN 7 */ uint32_t timeout = 10000; if(BSP_SD_WriteBlocks((uint32_t*)buf, blk_addr, blk_len, timeout) == USBD_OK){ while(BSP_SD_GetCardState()!= MSD_OK) { if (timeout-- == 0) { return -1; } } } return (USBD_OK); /* USER CODE END 7 */ }
实验方法及指导书:
链接:http://pan.baidu.com/s/1eRRzMs6 密码:59vk