Communication with SD Card with STM32 Processor - SDIO protocol

2.7k views Asked by At

I am using the board Nucleo F401Re based on micro-controller STM32F401RET6. I connected to the board a Micro SD slot, and interested in writing data to the SD Card and read data from it. I used the software STM32CubeX to generate code and in particular the SD library with built-in functions. 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:

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(). 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.

1

There are 1 answers

5
svens On BEST ANSWER

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. 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) {
  // ...
}