实验步骤:
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 ()
{
/* USER CODE END WHILE */ /* USER CODE BEGIN 3 */
LED_RED_ON;
LED_GREEN_OFF;
LED_BLUE_OFF;
HAL_Delay();
LED_RED_OFF;
LED_GREEN_ON;
LED_BLUE_OFF;
HAL_Delay();
LED_RED_OFF;
LED_GREEN_OFF;
LED_BLUE_ON;
HAL_Delay();
}
/* 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 = ; if(BSP_SD_ReadBlocks((uint32_t*)buf, blk_addr, blk_len, timeout) == USBD_OK){
while(BSP_SD_GetCardState()!= MSD_OK)
{
if (timeout-- == )
{
return -;
}
}
}
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 = ; if(BSP_SD_WriteBlocks((uint32_t*)buf, blk_addr, blk_len, timeout) == USBD_OK){
while(BSP_SD_GetCardState()!= MSD_OK)
{
if (timeout-- == )
{
return -;
}
}
}
return (USBD_OK);
/* USER CODE END 14 */
}
源代码下载链接:
链接:http://pan.baidu.com/s/1dFvm5rj 密码:tys0
iCore4链接: