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

Arm*_*ron 4 c stm32 iar stm32f4discovery

我正在使用基于微控制器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);

}
Run Code Online (Sandbox Code Playgroud)

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

The stack pointer for stack 'CSTACK' (currently 0x1FFFFD30) is outside the stack range (0x20000008 to 0x20000408) 
Run Code Online (Sandbox Code Playgroud)

当我不使用函数HAL_SD_WriteBlocks()或HAL_SD_ReadBlocks()时,不会出现此消息.如果有人已经遇到这个问题而且知道如何修复它,那么一些帮助可以帮我省钱.如果需要,我可以添加其余的代码.

sve*_*ens 5

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

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

int main(void) {
  // ...
  uint32_t to_send[512];
  uint32_t to_receive[512];
  // ...
}
Run Code Online (Sandbox Code Playgroud)

相反,像这样分配你的缓冲区:

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

int main(void) {
  // ...
}
Run Code Online (Sandbox Code Playgroud)