Description
I have SD card open, seek, read, close functions working outside LV, but when I register a drive and use the LV call backs, it hard crashes on lv_fs_read.
lv_fs_open and close work OK.
What MCU/Processor/Board and compiler are you using?
Teensy 4.1
Platformio
arduinoteensy 1.155.0 (1.55)
What LVGL version are you using?
lvgl@^8.0.1
lv_drivers@^8.0.1
What do you want to achieve?
Eventually load BMP and other files from SD Card
What have you tried so far?
I have dual screens, encoder and SD Card working, showing tabs and charts, etc.
I’m using the Teensy4.1 built in SD Card holder and functions and this can read a file manually.
I could not get lv to read a BMP file from SD card so tried the simple example of using lv SD card functions to read a text file
I’ve used the prototype callback functions and tried to implement them for Teensy
I’ve tried to trace how this works through my code and lv code and look at other examples on Google.
I’ve found the line in lv_fs.c where it is crashing, where it asks the callback function to read. But it seems this line is where is crashes, even if there is nothing in the called back function (completely empty function) if that makes sense? Doesn’t seem to matter what is in the read callback function it always crashes here. If I comment out that line in lv_fs.c it does not crash, but obviously this is useless.
I’m now stuck and would really appreciate help. I must be missing something in my implementation somewhere.
Thanks very much for your help.
Code to reproduce
This works:
//Direct file open and read:
File myManualFile;
if (SD.exists("folder/file.txt"))
{
Serial.println("file.txt exists.");
myManualFile = SD.open("folder/file.txt", O_RDONLY);
if (myManualFile)
{
Serial.print("file.txt Opened! : ");
}
myManualFile.seek(3);
Serial.print(" Contents from Position ");
Serial.print(myManualFile.position());
Serial.print(" = ");
while (myManualFile.available())
{
Serial.write(myManualFile.read());
}
// close the file:
myManualFile.close();
Serial.println("");
Serial.println("File Closed");
}
//End direct file open and read
This does not:
//Now try LV method:
lv_fs_file_t fileSD;
lv_fs_res_t res;
res = lv_fs_open(&fileSD, "S:folder/file.txt", LV_FS_MODE_RD);
if (res != LV_FS_RES_OK)
{
Serial.println("LV Failed to open SD File!");
}
else
{
Serial.println("LV Opened SD File!");
}
uint32_t read_num;
uint32_t charTOread = 8;
uint8_t buf[8];
Serial.print("LV File Pointer: ");
Serial.println((int)&fileSD);
Serial.println("LV Just about to read SD File!");
res = lv_fs_read(&fileSD, buf, charTOread, &read_num);
Serial.println("LV did a SD File Read!");
res = LV_FS_RES_OK;
if (res != LV_FS_RES_OK)
{
Serial.println("LV Couldn't Read!");
}
else
{
Serial.println("LV GOT DATA!");
}
lv_fs_close(&fileSD);
Serial.println("closed file");
It opens OK but crashes when it gets to lv_fs_read.
Looking at where the code gets to, it seems to crash when it gets to this line in lv_fs.c:
lv_fs_res_t res = file_p->drv->read_cb(file_p->drv, file_p->file_d, buf, btr, &br_tmp);
lv_fs_res_t lv_fs_read(lv_fs_file_t * file_p, void * buf, uint32_t btr, uint32_t * br)
{
if(br != NULL) *br = 0;
if(file_p->drv == NULL) return LV_FS_RES_INV_PARAM;
if(file_p->drv->read_cb == NULL) return LV_FS_RES_NOT_IMP;
uint32_t br_tmp = 0;
LV_LOG_WARN("In lv_fs.c - just about to read!");
lv_fs_res_t res = file_p->drv->read_cb(file_p->drv, file_p->file_d, buf, btr, &br_tmp);
LV_LOG_WARN("I did the lv_fs.c read bit!");
if(br != NULL) *br = br_tmp;
return res;
}
So, I’m assuming there is something wrong with my read callback?. But, when I have a completely empty read callback function it still hard crashes on this line.
This is my read callback function:
static lv_fs_res_t sd_read_cb(lv_fs_drv_t *drv, void *file_p, void *buf, uint32_t btr, uint32_t *br)
{
(void)drv; /*Unused*/
Serial.println("Call Back Asked to read file");
//FsFile *fp = (FsFile *)file_p; /*Just avoid the confusing casings*/
File *fp = (File *)file_p; /*Just avoid the confusing casings*/
File fileSD = *fp;
Serial.println((int)fileSD.available());
*br = fileSD.read(buf, btr);
lv_fs_res_t res = LV_FS_RES_OK;
return res;
}
My file system setup is like this after registering my two displays:
//setup LVGL File System:
static lv_fs_drv_t drv; /*Needs to be static or global*/
lv_fs_drv_init(&drv); /*Basic initialization*/
drv.letter = 'S'; /*An uppercase letter to identify the drive */
drv.ready_cb = sd_ready_cb; /*Callback to tell if the drive is ready to use */
drv.open_cb = sd_open_cb; /*Callback to open a file */
drv.close_cb = sd_close_cb; /*Callback to close a file */
drv.read_cb = sd_read_cb; /*Callback to read a file */
drv.write_cb = NULL; /*Callback to write a file */
drv.seek_cb = sd_seek_cb; /*Callback to seek in a file (Move cursor) */
drv.tell_cb = sd_tell_cb; /*Callback to tell the cursor position */
drv.dir_open_cb = NULL; /*Callback to open directory to read its content */
drv.dir_read_cb = NULL; /*Callback to read a directory's content */
drv.dir_close_cb = NULL; /*Callback to close a directory */
//drv.user_data = my_user_data; /*Any custom data if required*/
Serial.println("I got to just before registered drv!");
lv_fs_drv_register(&drv); /*Finally register the drive*/
Serial.println("I registered drv!");
Screenshot and/or video
If possible, add screenshots and/or videos about the current state.