cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F429 RTOS and SD card

IlConte
Associate III

I have a application with STM32F429 with RTOS and SPI µSD .

I have a singke task forr write in SD card and some time goes in HardFault_Handler ()

It is possible thei is caused by stack overflow ?

How can I check if the stack pointer goes in overflow ?

Thanks

#define configMINIMAL_STACK_SIZE                 ((uint16_t)128)
 
osThreadDef(sD_Task, SD_Task, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);
 
 
.........................
 
void SD_Task(void const * argument)
{
	 while(1) {
 
 
  		HAL_GPIO_WritePin(GPIOC, GPIO_PIN_12,GPIO_PIN_SET);    // debugga LED AUX2
   update_file_log("NEW LOG \r\n", 22);
		HAL_GPIO_WritePin(GPIOC, GPIO_PIN_12,GPIO_PIN_RESET);    // debugga LED AUX2
 
//		 HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);
		 osDelay(1000);
	 }
}
 
 
static uint8_t update_file_log(char * folder, char* s, int lunghezza)
{  uint8_t log_esiste=0;
 
 
   uint8_t return_value =1;
   BYTE writeBuf[256];
   uint16_t st;
 
 
 
   st = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_15);   // insert pin
   if ( st == 1) {
     fres = f_mount(&FatFs, "", 1); //1=mount now
     if (fres == FR_OK) {
	   DWORD free_clusters;
	   FATFS* getFreeFs;
	   fres = f_getfree("", &free_clusters, &getFreeFs);
	   if (fres == FR_OK) {
		 fres =  f_opendir (&dp, folder);
		 if (fres != FR_OK) {
			f_mkdir (folder);
		 }
		 f_chdir (folder);
         fres = f_open(&fil, FILE_LOG_NAME, FA_READ);
	     if (fres == FR_OK) {
	       log_esiste =1;
	       f_close(&fil);
	     }
 
	     if (log_esiste==0) {
	 	    fres = f_open(&fil, FILE_LOG_NAME, FA_WRITE | FA_OPEN_ALWAYS | FA_CREATE_ALWAYS);
	     } else {
	        fres = f_open(&fil, FILE_LOG_NAME, FA_WRITE | FA_OPEN_APPEND );
	     }
	     if (fres == FR_OK) {
	  	   strncpy((char*)writeBuf, s, lunghezza);
	   	   UINT bytesWrote;
	   	   fres = f_write(&fil, writeBuf, lunghezza, &bytesWrote);
 
	       f_close(&fil);
	       return_value = 0;
         }
	   }
     }
 
     f_mount(NULL, "", 0);
   }
   return return_value;
}

1 ACCEPTED SOLUTION

Accepted Solutions
KnarfB
Principal III

If your RTOS is FreeRTOS, it has some mechanisms for stack checking: https://www.freertos.org/Stacks-and-stack-overflow-checking.html

Anyway, you could increase the stack size temporarily.

Also avoid declaring arrays on the stack like BYTE writeBuf[256]; If possible, make the array a static variable.

hth

KnarfB

View solution in original post

2 REPLIES 2
KnarfB
Principal III

If your RTOS is FreeRTOS, it has some mechanisms for stack checking: https://www.freertos.org/Stacks-and-stack-overflow-checking.html

Anyway, you could increase the stack size temporarily.

Also avoid declaring arrays on the stack like BYTE writeBuf[256]; If possible, make the array a static variable.

hth

KnarfB

IlConte
Associate III

OK I check, thanks