1

我正在使用基于微控制器 STM32F401RET6 的板 Nucleo F401RE。我将一个 Micro SD 插槽连接到板上,并有兴趣将数据写入 SD 卡并从中读取数据。我使用软件 STM32CubeX 来生成代码,特别是带有内置函数的 SD 库。我试图编写一个简单的代码,它将一个数组写入一个特定的数组并尝试在后面读取相同的数据。代码如下:

uint32_t to_send[512] = {1, 2, 3, 4, 5};
uint32_t to_receive[512];

int main(void)
{

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  MX_SDIO_SD_Init();

  char buffer[14] = "Hello, world\n";
  uint64_t address = 0x00; 
  HAL_SD_ErrorTypedef write_result = HAL_SD_WriteBlocks(&hsd, to_send, address, 512, 1);
  HAL_SD_ErrorTypedef read_result = HAL_SD_ReadBlocks(&hsd, to_receive, 0x00, 512, 1);
  HAL_UART_Transmit(&huart2, (uint8_t *) &write_result, 1, 1000);
  HAL_UART_Transmit(&huart2, (uint8_t *) &read_result, 1, 1000);


  while (1)
  {
      //HAL_UART_Transmit(&huart2, (uint8_t *)buffer, 14, 1000);
      HAL_UART_Transmit(&huart2, (uint8_t *)to_receive, 512, 1000);


}

虽然,我没有成功写入数据,但函数 HAL_SD_WriteBlocks() 返回值 SD_CMD_CRC_FAIL,这意味着:“收到命令响应(但 CRC 校验失败)”。我错过了什么?我多次检查硬件配置,micro SD 卡已正确连接到微控制器。如果需要,我可以添加 HAL 内置函数的实现。谢谢你。

4

2 回答 2

0
I didnt use usart for my project, i write the value to sd card and read the value ,
you must arrange the code for your expectations, my code is 


#include "main.h"
#include "stm32f4xx_hal.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/
SD_HandleTypeDef hsd;


/* USER CODE BEGIN PV */
/* Private variables --------------------------------------------------------*/
uint8_t to_send[512] = "sener suat sd card";
uint8_t to_receive[512];
uint8_t sener[3]={7,5,4};

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SDIO_SD_Init(void);
static void MX_USART2_UART_Init(void);

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

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();

  /* USER CODE BEGIN 2 */
   uint32_t address = 0x55;
  HAL_SD_MspInit(&hsd);
  HAL_SD_Init(&hsd);
  HAL_SD_WriteBlocks(&hsd,to_send,address,1,500);
  HAL_Delay(100);
  HAL_SD_ReadBlocks(&hsd,to_receive,address,1,500);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */
  /* USER CODE BEGIN 3 */

  }


}
于 2017-12-15T14:47:05.827 回答
0

确保您的 sdio 时钟在有效限制内(参见函数 SystemClock_Config):

/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
#define PLL_M      8
#define PLL_N      336

/* SYSCLK = PLL_VCO / PLL_P */
#define PLL_P      2

/* USB OTG FS, SDIO and RNG Clock =  PLL_VCO / PLLQ */
#define PLL_Q      7

在初始化时钟 (RCC_OscInitTypeDef) 时使用上述定义时,SDIO 时钟将为 336 / (7*2) = 25Mhz

(假设 PLL_M 与 HSE / HSI 相同)

如果频率太高(>50Mhz),那么您可能会在通信中遇到错误,这将解释您的症状。

于 2016-12-21T14:28:39.437 回答