简体   繁体   English

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

[英]Communication with SD Card with STM32 Processor - SDIO protocol

I am using the board Nucleo F401Re based on micro-controller STM32F401RET6. 我正在使用基于微控制器STM32F401RET6的Nucleo F401Re板。 I connected to the board a Micro SD slot, and interested in writing data to the SD Card and read data from it. 我连接到主板上的Micro SD插槽,并有兴趣将数据写入SD卡并从中读取数据。 I used the software STM32CubeX to generate code and in particular the SD library with built-in functions. 我使用软件STM32CubeX生成代码,特别是带有内置函数的SD库。 I tried to write a simple code which writes an array to a specific array and tries to read the same data afterwords. 我试着编写一个简单的代码,将一个数组写入一个特定的数组,然后尝试读取相同的数据。 The code is as follows: 代码如下:

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

}

The code stopps in the middle of the function HAL_Init() and I'm getting the following message: 代码在函数HAL_Init()的中间停止,我收到以下消息:

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

This message doesn't appear when I don't use the functions HAL_SD_WriteBlocks(), or HAL_SD_ReadBlocks(). 当我不使用函数HAL_SD_WriteBlocks()或HAL_SD_ReadBlocks()时,不会出现此消息。 If someone already had this problem and knows how to fix it, some help would save me. 如果有人已经遇到这个问题而且知道如何修复它,那么一些帮助可以帮我省钱。 If needed I can add the rest of the code. 如果需要,我可以添加其余的代码。

You're using too much stack space. 你使用了太多的堆栈空间。 You can adjust the allocated stack space in the linker script and increase it if needed. 您可以在链接描述文件中调整分配的堆栈空间,并在需要时增加它。

However you can probably avoid that by writing your code differently. 但是,您可以通过不同地编写代码来避免这种情况。 In your example above, you're allocating large buffers (4kB) on the stack. 在上面的示例中,您将在堆栈上分配大缓冲区(4kB)。 Don't do that unless absolutely necessary. 除非绝对必要,否则不要这样做。 I'm referring to this: 我是指这个:

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

Instead, allocate your buffers like this: 相反,像这样分配你的缓冲区:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM