将数据写入微控制器STM32F401RET6的micro SD卡

时间:2016-12-21 09:19:32

标签: c sd-card stm32 stm32f4discovery stm32f4

我正在使用基于微控制器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内置函数的实现。谢谢。

2 个答案:

答案 0 :(得分: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),那么您可能会在沟通中出现错误,这将解释您的症状。

答案 1 :(得分: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 */

  }


}