2015-10-26 01:51 AM
I checked the code, it works when picture is in memory. But I can't make it work with MicroCD.
I rewrote the code for work with FatFs, but STM32 freezes on f_lseek() function. There is my code, I purposely deleted all unnecessary for easy reading.FATFS MicroSD_FAT;
char
MicroSD_Path[4];
BYTE
jdwork[3100];
typedef
struct
{
void
*pData;
} IODEV;
UINT
input_func(JDEC *jd,
BYTE
*buff,
UINT
ndata){
IODEV *dev = (IODEV *)jd->device;
UINT
rb;
FIL *fil;
if
(buff){
fil = (FIL *)dev->pData;
f_read(fil, buff, ndata, &rb);
return
rb;
}
return
(f_lseek(fil, f_tell(fil) + ndata) == FR_OK) ? ndata : 0;
// Here freezes
}
// It works
UINT
output_func(JDEC *jd,
void
*bitmap, JRECT *rect){
uint16_t *bmp = (uint16_t *)bitmap;
uint16_t x, y, i = 0;
for
(y = rect->top; y <= rect->bottom; y++) {
for
(x = rect->left; x <= rect->right; x++) {
*(__IO uint16_t *)(LCD_FRAME_BUFFER + (x * 2) + (y * IMAGE_WIDTH * 2)) = (uint16_t)(bmp[i++]);
}
}
return
1;
}
int
main(
void
){
CPU_CACHE_Enable();
HAL_Init();
SystemClock_Config();
LCD_Config();
JDEC jd;
IODEV iodev;
JRESULT rc;
FIL JpegEncFile;
BSP_LCD_Clear(LCD_COLOR_WHITE);
// Start
if
(FATFS_LinkDriver(&SD_Driver, MicroSD_Path) != 0){
while
(1);
}
if
(f_mount(&MicroSD_FAT, (
TCHAR
const
*)MicroSD_Path, 0) != FR_OK){
while
(1);
}
if
(f_open(&JpegEncFile, IMAGE_NAME, FA_READ) != FR_OK){
while
(1);
}
iodev.pData = (
void
*)&JpegEncFile;
rc = jd_prepare(&jd, input_func, jdwork,
sizeof
(jdwork), &iodev);
if
(rc){
BSP_LCD_Clear(LCD_COLOR_LIGHTBLUE);
// jd_prepare() error
while
(1);
}
rc = jd_decomp(&jd, output_func, 2);
if
(rc){
BSP_LCD_Clear(LCD_COLOR_RED);
// jd_decomp() error
while
(1);
}
while
(1);
}
2015-10-26 05:31 AM
Make sure you assign fil on both execution paths.
2015-10-26 06:56 AM
You can
explain it
more in detail
?
2015-10-26 07:04 AM
I think Clive suggests you to move the ''fil = (FIL *)dev->pData;'' line up, so it comes before the if statement (in ''input_func'' function).
2015-10-26 07:44 AM
In
input_func
() Where buff == NULL, fil has no value assigned to it (random stack junk), and then you attempt to f_lseek() against garbage.
Move the assignment up earlier as it's needed for both execution/exit paths.
2015-10-27 02:15 AM
I changed my
code, but
there is no result
typedef
struct
{
void
*pData;
uint32_t joffset;
uint32_t jsize;
} IODEV;
UINT
input_func(JDEC *jd,
BYTE
*buff,
UINT
ndata){
IODEV *dev = (IODEV *)jd->device;
UINT
rb;
FIL *fil;
ndata = dev->jsize - dev->joffset > ndata ? ndata : dev->jsize - dev->joffset;
if
(buff){
fil = (FIL *)dev->pData;
f_read(fil, buff, ndata, &rb);
f_lseek(fil, dev->joffset);
}
dev->joffset += ndata;
return
ndata;
}
int
main(
void
){
// ...
iodev.pData = (
void
*)&JpegEncFile;
iodev.jsize = f_size(&JpegEncFile);
iodev.joffset = 0;
// ...
}
2015-10-28 01:17 AM
I rewrote
the code from
a working
example.
Here's the
code:
UINT
input_func (JDEC * jd,
BYTE
* buff,
UINT
ndata) {
IODEV * dev = (IODEV *) jd->device;
ndata = dev->jsize - dev->joffset > ndata ? ndata : dev->jsize - dev->joffset;
if
(buff)
memcpy
(buff, dev->jpic + dev->joffset, ndata);
dev->joffset += ndata;
return
ndata;
}
2015-10-28 04:27 AM
Need to work on logic and sequence
I'd expect these have a better chance of workingUINT input_func(JDEC *jd, BYTE *buff, UINT ndata){
IODEV *dev = (IODEV *)jd->device;
UINT rb;
FIL *fil = (FIL *)dev->pData;
if(buff){
f_read(fil, buff, ndata, &rb);
return rb;
}
return (f_lseek(fil, f_tell(fil) + ndata) == FR_OK) ? ndata : 0; // Here freezes
}
or
UINT input_func(JDEC *jd, BYTE *buff, UINT ndata){
IODEV *dev = (IODEV *)jd->device;
ndata = dev->jsize - dev->joffset > ndata ? ndata : dev->jsize - dev->joffset;
if(buff){
UINT rb;
FIL *fil = (FIL *)dev->pData;
f_lseek(fil, dev->joffset);
f_read(fil, buff, ndata, &rb);
}
dev->joffset += ndata;
return ndata;
}
2015-10-29 12:29 AM
Thank you!
It works