本程序編寫基於秉火霸道STM32F103ZET6運行環境。
跑這個實驗之前吃了一些虧,讓我一一道來!
1、軟件寫好了,卻發現插入USB線連接到電腦後USB居然沒有枚舉
解決方法:
野火的這款開發板上做了一個USB上電使能IO,也就是說,當PD3爲低電平時,USB才能正常工作,如果不去使能這個管腳的話,USB自然就不工作了。
2、HAL庫讀寫SD卡API版本問題
解決方法:
我用的是1.8.0的HAL庫,這個庫和老版本的HAL庫在API上有重大的變更,接口的參數也不一樣,含義也有區別。
老版本HAL庫讀寫SD卡的接口
我們來看下HAL庫舊版本的讀寫API:
/**
* @brief Reads block(s) from a specified address in a card. The Data transfer
* is managed by polling mode.
* @param hsd: SD handle
* @param pReadBuffer: pointer to the buffer that will contain the received data
* @param ReadAddr: Address from where data is to be read
* @param BlockSize: SD card Data block size (in bytes)
* This parameter should be 512
* @param NumberOfBlocks: Number of SD blocks to read
* @retval SD Card error state
*/
HAL_SD_ErrorTypedef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
我們看到,這裏的BlockSize是以字節爲單位進行讀的,最小應當是512,因爲SD卡一個塊的大小是512,所以,NumberOfBlocks表示塊數,讀一塊,那麼就是BlockSize512,讀N塊,那麼就是BlockSize512*N。
再來看看老版本HAL庫的寫SD卡接口
/**
* @brief Allows to write block(s) to a specified address in a card. The Data
* transfer is managed by polling mode.
* @param hsd: SD handle
* @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
* @param WriteAddr: Address from where data is to be written
* @param BlockSize: SD card Data block size (in bytes)
* This parameter should be 512.
* @param NumberOfBlocks: Number of SD blocks to write
* @retval SD Card error state
*/
HAL_SD_ErrorTypedef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
寫也是一樣的,這裏的BlockSize是以字節爲單位進行讀的,最小應當是512,因爲SD卡一個塊的大小是512,所以,NumberOfBlocks表示塊數,寫一塊,那麼就是BlockSize512,寫N塊,那麼就是BlockSize512*N。
新版本HAL庫讀寫SD卡的接口
然而在最新的HAL庫上,是不用乘以512的,我們來看一下1.8.0版本HAL庫關於這兩個函數的描述:
/**
* @brief Reads block(s) from a specified address in a card. The Data transfer
* is managed by polling mode.
* @note This API should be followed by a check on the card state through
* HAL_SD_GetCardState().
* @param hsd: Pointer to SD handle
* @param pData: pointer to the buffer that will contain the received data
* @param BlockAdd: Block Address from where data is to be read
* @param NumberOfBlocks: Number of SD blocks to read
* @param Timeout: Specify timeout value
* @retval HAL status
*/
HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
我們來看下讀SD卡的接口,這裏傳入的BlockAdd,也就是塊的起始地址,NumberOfBlocks表示的是多少塊,所以本來就是以塊爲單位進行讀的,所以也就不用去乘512。
/**
* @brief Allows to write block(s) to a specified address in a card. The Data
* transfer is managed by polling mode.
* @note This API should be followed by a check on the card state through
* HAL_SD_GetCardState().
* @param hsd: Pointer to SD handle
* @param pData: pointer to the buffer that will contain the data to transmit
* @param BlockAdd: Block Address where data will be written
* @param NumberOfBlocks: Number of SD blocks to write
* @param Timeout: Specify timeout value
* @retval HAL status
*/
HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
寫SD卡的接口也是一樣的,這裏傳入的BlockAdd,也就是塊的起始地址,NumberOfBlocks表示的是多少塊,所以本來就是以塊爲單位進行寫的,所以也就不用去乘512。
所以,在實現USB大容量存儲設備接口的時候,我應該這麼來實現:
/**
* @brief .
* @param lun: .
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
/* USER CODE BEGIN 6 */
if(HAL_OK != HAL_SD_ReadBlocks(&hsd,(uint8_t *)buf, blk_addr , blk_len, 1000))
return USBD_FAIL ;
return (USBD_OK);
/* USER CODE END 6 */
}
/**
* @brief .
* @param lun: .
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
/* USER CODE BEGIN 7 */
if(HAL_OK != HAL_SD_WriteBlocks(&hsd, (uint8_t *)buf, blk_addr , blk_len, 1000))
return USBD_FAIL ;
return (USBD_OK);
/* USER CODE END 7 */
}
好,這兩個問題解決了,一下來看看這麼做的。
一、USB使能管腳配置
PD3管腳,默認輸出電平爲低電平,也就是D+管腳爲使能狀態,USB供上了電。
PB1是我拿來做調試的燈。
二、RCC時鐘
這裏要切記一點,因爲這裏我們用到了USB,USB的時鐘要配置爲48MHz,具體看手冊。
而SDIO是時鐘是HCLK的二分頻。
三、調試接口
這裏選擇串行調試。
四、SDIO配置
這裏,我們配置模式爲4位寬總線的SD卡模式,時鐘分頻因子之前在步驟二中我們已經知道了,SDIO的時鐘頻率是HCLK的二分頻,所以SDIOCLK clock divide factor這個選項我們設置爲2。
開啓SDIO全局中斷。
五、USB配置
這裏將USB設備配置爲大容量存儲,其餘默認即可。
六、生成並添加代碼邏輯
這裏我們把棧的大小稍微調大一點,以便我們後期在代碼裏進行測試。
在usbd_storage_if.c中實現如下接口:
STORAGE_GetCapacity_FS 獲取U盤容量信息
STORAGE_IsReady_FS 獲取U盤狀態
STORAGE_Read_FS 讀U盤
STORAGE_Write_FS 寫U盤
通過接口獲取有多少塊以及塊的大小。
/**
* @brief .
* @param lun: .
* @param block_num: .
* @param block_size: .
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
/* USER CODE BEGIN 3 */
*block_num = hsd.SdCard.BlockNbr ;
*block_size = hsd.SdCard.BlockSize ;
return (USBD_OK);
/* USER CODE END 3 */
}
判斷SD卡的狀態是否已經準備好了,狀態的描述如下:
typedef enum
{
HAL_SD_STATE_RESET = ((uint32_t)0x00000000U), /*!< SD not yet initialized or disabled */
HAL_SD_STATE_READY = ((uint32_t)0x00000001U), /*!< SD initialized and ready for use */
HAL_SD_STATE_TIMEOUT = ((uint32_t)0x00000002U), /*!< SD Timeout state */
HAL_SD_STATE_BUSY = ((uint32_t)0x00000003U), /*!< SD process ongoing */
HAL_SD_STATE_PROGRAMMING = ((uint32_t)0x00000004U), /*!< SD Programming State */
HAL_SD_STATE_RECEIVING = ((uint32_t)0x00000005U), /*!< SD Receiving State */
HAL_SD_STATE_TRANSFER = ((uint32_t)0x00000006U), /*!< SD Transfert State */
HAL_SD_STATE_ERROR = ((uint32_t)0x0000000FU) /*!< SD is in error state */
}HAL_SD_StateTypeDef;
/**
* @brief .
* @param lun: .
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_IsReady_FS(uint8_t lun)
{
/* USER CODE BEGIN 4 */
uint8_t state = 0;
state = HAL_SD_GetState(&hsd) ;
if(HAL_SD_STATE_READY != state)
return USBD_FAIL ;
return (USBD_OK);
/* USER CODE END 4 */
}
實現USB讀寫SD卡
/**
* @brief .
* @param lun: .
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
/* USER CODE BEGIN 6 */
if(HAL_OK != HAL_SD_ReadBlocks(&hsd,(uint8_t *)buf, blk_addr , blk_len, 1000))
return USBD_FAIL ;
return (USBD_OK);
/* USER CODE END 6 */
}
/**
* @brief .
* @param lun: .
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
/* USER CODE BEGIN 7 */
if(HAL_OK != HAL_SD_WriteBlocks(&hsd, (uint8_t *)buf, blk_addr , blk_len, 1000))
return USBD_FAIL ;
return (USBD_OK);
/* USER CODE END 7 */
}
在主函數的while循環裏添加調試閃爍燈。
/**
* @brief The application entry point.
* @retval int
*/
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_USART2_UART_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調試燈以500ms的頻率進行翻轉
HAL_GPIO_TogglePin(LED0_GPIO_Port,LED0_Pin);
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
七、運行結果
將USB線插入USB DEVICE的接口,並連接到PC端的USB口,在PC端彈出可移動磁盤,實驗成功。
我用的是4GB的內存卡,PC端顯示3.68GB,爲什麼呢?度娘一下:
金士頓4G SD卡只有3.68G?
你電腦的算法是1024MB=1GB
U盤廠家的算法是1000MB=1GB
還要加上法律允許的產品誤差,一般廠家會取最小值,不會多給你空間的。
爲什麼是4G的手機內存卡在手機裏顯示只有3.68G?
硬件廠商爲了計算方便採用的是十進制,也就是滿1000字節算1K,滿1000K算1M,以此類推。
軟件設計上由於計算機採用的是二進制所以是滿1024字節即2的10次方算1K,1024K算1M。
所以你的卡越大差距就會越大,這點在電腦硬盤上感覺會更明顯一些。