[STM32L5] 【STM32L562 DK试用】移植SD卡驱动

[复制链接]
 楼主| lulugl 发表于 2025-5-17 17:08 | 显示全部楼层 |阅读模式
【前言】
开发板上板载了SD卡插座,这一篇分享如何驱动SD卡。
1、打开stm32cubeMX打开SD,但是不让生成初始化代码,初始化,由BSP工程的代码来实现。
44829682837d4577b0.png
19372682837e26b8da.png
2、添加bsp的sd卡驱动文件到工程中:
917106828384aa76b4.png
3、添加sd的中断到stm32l5xxit.c中
  1. void EXTI2_IRQHandler(void)
  2. {
  3.   BSP_SD_DETECT_IRQHandler(0);
  4. }

  5. void SDMMC1_IRQHandler(void)
  6. {
  7.   BSP_SD_IRQHandler(0);
  8. }
890706828391e87bb9.png
4、添加sd测试函数:
  1. #include "main.h"
  2. #include "stdio.h"

  3. #define SD_CARD_PRESENCE_POLLING_MODE        0
  4. #define SD_CARD_PRESENCE_INTERRUPT_MODE      1

  5. #define SD_CARD_PRESENCE_VALIDATION_MODE     SD_CARD_PRESENCE_INTERRUPT_MODE      

  6. #define BLOCK_START_ADDR         0     /* Block start address      */
  7. #define NUM_OF_BLOCKS            5     /* Total number of blocks   */
  8. #define BUFFER_WORDS_SIZE        ((BLOCKSIZE * NUM_OF_BLOCKS) >> 2) /* Total data size in bytes */

  9. __IO uint32_t SDWriteStatus = 0, SDReadStatus = 0, SDDetectStatus = 0, SDDetectIT = 0;

  10. void SD_demo(void)
  11. {
  12.         int32_t SD_state = BSP_ERROR_NONE;
  13.         SD_state = BSP_SD_Init(0);
  14.        
  15. #if (SD_CARD_PRESENCE_VALIDATION_MODE == SD_CARD_PRESENCE_INTERRUPT_MODE)
  16.   if (SD_state == BSP_ERROR_NONE)
  17.   {
  18.     SD_state = BSP_SD_DetectITConfig(0);
  19.   }
  20. #endif
  21.        
  22.         if (SD_state != BSP_ERROR_NONE)
  23.   {
  24.                 if (SD_state == BSP_ERROR_UNKNOWN_COMPONENT)
  25.     {
  26.       printf("SD shall be inserted before");
  27.       printf("running test.\r\n");
  28.       printf("SD Test Aborted.\r\n");
  29.     }
  30.     else
  31.     {
  32.       printf("SD Initialization : FAIL.\r\n");
  33.       printf("SD Test Aborted.\r\n");
  34.     }
  35.         }
  36.         else
  37.   {
  38.     printf("SD Initialization : OK.\r\n");
  39.         }
  40.        
  41. }


  42. /**
  43.   * [url=home.php?mod=space&uid=247401]@brief[/url] Tx Transfer completed callback
  44.   * @param  Instance     SD Instance
  45.   * @retval None
  46.   */
  47. void BSP_SD_WriteCpltCallback(uint32_t Instance)
  48. {
  49.   if (Instance == 0)
  50.   {
  51.     SDWriteStatus = 1;
  52.   }
  53. }

  54. /**
  55.   * @brief Rx Transfer completed callback
  56.   * @param  Instance     SD Instance
  57.   * @retval None
  58.   */
  59. void BSP_SD_ReadCpltCallback(uint32_t Instance)
  60. {
  61.   if (Instance == 0)
  62.   {
  63.     SDReadStatus = 1;
  64.   }
  65. }

  66. /**
  67.   * @brief  BSP SD Callback.
  68.   * @param  Instance SD Instance
  69.   * @param  Status   Pin status
  70.   * @retval None.
  71.   */
  72. void BSP_SD_DetectCallback(uint32_t Instance, uint32_t Status)
  73. {
  74.   if (Instance == 0)
  75.   {
  76.     SDDetectIT = 1;
  77.     SDDetectStatus = Status;
  78.   }
  79. }

  80. /**
  81.   * @brief SDMMC error callback
  82.   * @param None
  83.   * @retval None
  84.   */
  85. void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
  86. {
  87.   Error_Handler();
  88. }

在main.c中添加sd_demo
61507682851ea27484.png
下载到开发板上,可以看到SD初始化成功:
787416828520e51695.png
慢醇 发表于 2025-5-31 18:30 | 显示全部楼层
是否可以在仿真中查看?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

180

主题

830

帖子

12

粉丝
快速回复 在线客服 返回列表 返回顶部