STM32处理器与SD卡通信-SDIO协议

Communication with SD Card with STM32 Processor - SDIO protocol

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

  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";
  uint32_t to_send[512] ; // Te
  uint32_t to_receive[512];
  uint64_t address = 150; 
  HAL_SD_WriteBlocks(&hsd, to_send, address, 512, 1);
  HAL_SD_ReadBlocks(&hsd, to_receive, address, 512, 1);


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

}

代码在函数 HAL_Init() 中间停止,我收到以下消息:

The stack pointer for stack 'CSTACK' (currently 0x1FFFFD30) is outside the stack range (0x20000008 to 0x20000408) 

当我不使用函数 HAL_SD_WriteBlocks() 或 HAL_SD_ReadBlocks() 时,不会出现此消息。如果有人已经遇到这个问题并且知道如何解决它,一些帮助可以拯救我。如果需要,我可以添加其余代码。

您使用的堆栈过多 space。您可以在链接描述文件中调整分配的堆栈 space 并在需要时增加它。

但是,您可以通过以不同方式编写代码来避免这种情况。在上面的示例中,您在堆栈上分配了大缓冲区 (4kB)。除非绝对必要,否则不要这样做。我指的是这个:

int main(void) {
  // ...
  uint32_t to_send[512];
  uint32_t to_receive[512];
  // ...
}

而是像这样分配缓冲区:

uint32_t to_send[512];
uint32_t to_receive[512];

int main(void) {
  // ...
}