【iCore4 雙核心板_ARM】例程十九:USBD_MSC實驗——虛擬U盤

XiaomaGee發表於2017-09-12

實驗步驟:

1、將SD卡插在SD卡槽中。

2、將跳線冒跳至USB_OTG,將USB_OTG通過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_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_HS
* Description    : 
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
int8_t STORAGE_Read_HS (uint8_t lun, 
                        uint8_t *buf, 
                        uint32_t blk_addr,                       
                        uint16_t blk_len)
{
  /* USER CODE BEGIN 13 */ 
    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 13 */ 
}

/*******************************************************************************
* Function Name  : STORAGE_Write_HS
* Description    :
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
int8_t STORAGE_Write_HS (uint8_t lun, 
                         uint8_t *buf, 
                         uint32_t blk_addr,
                         uint16_t blk_len)
{
  /* USER CODE BEGIN 14 */ 
    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 14 */ 
}

原始碼下載連結:

連結:http://pan.baidu.com/s/1dFvm5rj 密碼:tys0

iCore4連結:

相關文章