cancel
Showing results for 
Search instead for 
Did you mean: 

Hard fault after first JPG with 746D

LMI2
Lead
Posted on June 06, 2018 at 21:40

I am building code to show all jpeg images in SD card and print their names. I use the STM32F746 Discovery. The jpeg decoder crashes after the first image. The Jpg decoder stays at hard fault loop. If I comment the jpeg_decode(&viili, IMAGE_WIDTH, _aucLine, Jpeg_CallbackFunction); line, code works but of course no images are shown.

Main.c looks like this

/* Includes ------------------------------------------------------------------*/ #include 'main.h'  FATFS SDFatFs; /* File system object for SD card logical drive */ FIL viili; /* File object */ FRESULT res; DIR dir; static FILINFO fno;  char SDPath[4]; /* SD card logical drive path */  RGB_typedef *RGB_matrix;  uint8_t _aucLine[2048]; uint32_t offset = 0; uint32_t line_counter = 239;  /* Private function prototypes -----------------------------------------------*/ static void SystemClock_Config(void); static void LCD_Config(void); static uint8_t Jpeg_CallbackFunction(uint8_t* Row, uint32_t DataLength); static void CPU_CACHE_Enable(void);  /* Private functions ---------------------------------------------------------*/  /**  * @brief Main program  * @param None  * @retval None  */ int main(void) {  /* Enable the CPU Cache */  CPU_CACHE_Enable();   HAL_Init();    /* Configure the system clock to 200 MHz */  SystemClock_Config();    /*##-1- LCD Configuration ##################################################*/   LCD_Config();   /*##-2- Link the micro SD disk I/O driver ##################################*/  if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)  {  /*##-3- Register the file system object to the FatFs module ##############*/  if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) == FR_OK)  {  res = f_findfirst(&dir, &fno, '', 'ima*.jpg'); /* Start to search for photo files */ res = f_open(&viili,&fno.fname[0] , FA_READ);  while (res == FR_OK && fno.fname[0])  { /* Repeat while an item is found */  /*##-5- Decode the jpg image file ##########################################*/ jpeg_decode(&viili, IMAGE_WIDTH, _aucLine, Jpeg_CallbackFunction);  /*##-4- Close the JPG image ################################################*/  f_close(&viili);  BSP_LCD_DisplayStringAtLine(2, (uint8_t*)fno.fname); HAL_Delay(2000);  res = f_findnext(&dir, &fno); /* Search for next item */ }  f_close(&viili);  }  }  /* Infinite loop */  while (1)  {  } } �?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

Note: this post was migrated and contained many threaded conversations, some content may be missing.
10 REPLIES 10
AvaTar
Lead
Posted on June 07, 2018 at 07:44

I think you will need to look into the jpeg_decode() function and the SCB fault registers, not much to see on the top-level code.

But my guess would be a stack overflow.

200806CGIL2
Posted on June 07, 2018 at 13:01

You mean I should increase stack space. How do you do it with Keil?

I don't know if it's a stack overflow. But the jpeg format is based upon spectral data, and decoding involves a conversion to pixel data. You bet this process involves some memory for intermediary buffers.

I suggest to investigate/debug it first, and confirm the cause.

For how to set a project's stack size, please consult the Keil help/tutorials. I don't use uVision.

For the jpeg_decode(), I would check the documentation, and examples provided.

Does the call ever enter your provided callback function ?

I am not sure if my f_open is in the correct place or if I should open each image separately.

I strongly suspect the latter. Otherwise the file object pointer is dangling for following image files.

Posted on June 07, 2018 at 16:42

When I changed my code to the following file names are now increasing but no other images are shown. And the program loops here. cinfo.err = jpeg_std_error(&jerr);

There is something wrong with JPG handling. With code below the card would open 29 files and stop. When I commented both jpeg_decode(&viili, IMAGE_WIDTH, _aucLine, Jpeg_CallbackFunction); lines, the CPU opened all of the files, 125.

I changed my code so it makes sense, to me that is.

 if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)

  {

    /*♯♯-3- Register the file system object to the FatFs module ♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

    if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) == FR_OK)

    {

    res = f_findfirst(&dir, &fno, '', 'ima*.jpg');  /* Start to search for photo files */

    res = f_open(&viili,&fno.fname[0] , FA_READ);

    /*♯♯-5- Decode the jpg image file ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

    jpeg_decode(&viili, IMAGE_WIDTH, _aucLine, Jpeg_CallbackFunction);

    /*♯♯-4- Close the JPG image ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/            

    f_close(&viili);                

    while (res == FR_OK && fno.fname[0])

            {         /* Repeat while an item is found */    

                res = f_open(&viili,&fno.fname[0] , FA_READ);

     /*♯♯-5- Decode the jpg image file ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

                jpeg_decode(&viili, IMAGE_WIDTH, _aucLine, Jpeg_CallbackFunction);

   /*♯♯-4- Close the JPG image ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

               f_close(&viili);    

        BSP_LCD_DisplayStringAtLine(2, (uint8_t*)fno.fname);

                HAL_Delay(1000);

        res = f_findnext(&dir, &fno);               /* Search for next item */

    }

Edit: edited text to make it clearer

Posted on June 07, 2018 at 19:32

With the code from my previous mail I see part of an other picture below the first. Maybe the Jpeg decoding has to initialized somehow between pictures.

Posted on June 08, 2018 at 09:00

Or, some static buffer is not cleared / re-initialized for the next image.

Posted on June 08, 2018 at 09:47

'Or, some static buffer is not cleared / re-initialized for the next image.'

That is what I think, too. The same problem appears without f_findfirst and f_findnext.

That is why I opened a new discussion with a better title, I hope.

https://community.st.com/0D50X00009XkfjCSAR

Posted on June 08, 2018 at 10:17

Don't you have the sources, to debug into the decode function ?

Posted on June 10, 2018 at 13:18

I did some debugging or tracing and I think that the problem is not in SDfat code or JPEG library either. But the reason is that only first image starts at zero position. The rest keep on increasing the position of new data. I don't know how to reset that. I have reset the LCD and JPG library which didn't help, no surprise because reset functions were already called by original example code.

Especially I don't understand the line jpeg_decode(&MyFile, IMAGE_WIDTH, _aucLine, Jpeg_CallbackFunction); The Jpeg_CallbackFunction parameters don't seem to match.