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

标签 c stm32 iar stm32f4discovery

我正在使用基于微 Controller 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() 时,不会出现此消息。如果有人已经遇到这个问题并且知道如何解决它,一些帮助可以拯救我。如果需要,我可以添加其余代码。

最佳答案

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

但是,您可以通过以不同方式编写代码来避免这种情况。在上面的示例中,您在堆栈上分配了大缓冲区 (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) {
  // ...
}

关于STM32处理器与SD卡通信-SDIO协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41245625/

相关文章:

c - 即使 GCC 优化关闭,是否也有必要使用 "volatile"限定符?

c++ - Visual Studio C++ header

c - "#pragma calls"和条件编译

arm - 使用 Eclipse CDT 和 IAR 插件进行 headless 构建

c - 如何修复 linux 内核部分不匹配?

将结构复制到 char 缓冲区

c - STM32 中 PWM 的预分频器和周期值

c - 结构数组 : Structure members are Enum Variables

c - libevent 和非阻塞套接字

我可以/应该通过单个指针访问多个设备寄存器吗?