How to read a file using C language

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
heiseiggg
Posts: 3
Joined: Mon Oct 12, 2020 12:58 pm

How to read a file using C language

Post by heiseiggg » Tue Nov 10, 2020 11:21 am

I'm currently using MicroPython to develop esp32 board. Now I need to read a file in flash in main.c. How could I do this? Could anybody show me an example? Thanks

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to read a file using C language

Post by jimmo » Wed Nov 11, 2020 12:12 am

heiseiggg wrote:
Tue Nov 10, 2020 11:21 am
I'm currently using MicroPython to develop esp32 board. Now I need to read a file in flash in main.c. How could I do this? Could anybody show me an example? Thanks
You can use mp_vfs_open (see extmod/vfs.h) to open the file which will return a stream object, that you can use mp_stream_rw (or mp_stream_read_exactly) to read bytes from (see py/stream.h). You can see an example of this vfs_reader.c (which is what is used by the lexer to load Python files from flash).

This is how builtin "open" works (see esp32/mpconfigport.h where it maps mp_builtin_open to mp_vfs_open).

heiseiggg
Posts: 3
Joined: Mon Oct 12, 2020 12:58 pm

Re: How to read a file using C language

Post by heiseiggg » Wed Nov 11, 2020 3:13 am

jimmo wrote:
Wed Nov 11, 2020 12:12 am
heiseiggg wrote:
Tue Nov 10, 2020 11:21 am
I'm currently using MicroPython to develop esp32 board. Now I need to read a file in flash in main.c. How could I do this? Could anybody show me an example? Thanks
You can use mp_vfs_open (see extmod/vfs.h) to open the file which will return a stream object, that you can use mp_stream_rw (or mp_stream_read_exactly) to read bytes from (see py/stream.h). You can see an example of this vfs_reader.c (which is what is used by the lexer to load Python files from flash).

This is how builtin "open" works (see esp32/mpconfigport.h where it maps mp_builtin_open to mp_vfs_open).
Thanks Jimmo, Your advice is quite helpful.
What if I want to modify the file from flash with C? Which file could be useful for me?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to read a file using C language

Post by jimmo » Wed Nov 11, 2020 9:31 am

heiseiggg wrote:
Wed Nov 11, 2020 3:13 am
What if I want to modify the file from flash with C? Which file could be useful for me?
Exactly the same -- mp_stream_rw

heiseiggg
Posts: 3
Joined: Mon Oct 12, 2020 12:58 pm

Re: How to read a file using C language

Post by heiseiggg » Thu Nov 12, 2020 6:40 am

jimmo wrote:
Wed Nov 11, 2020 9:31 am
heiseiggg wrote:
Wed Nov 11, 2020 3:13 am
What if I want to modify the file from flash with C? Which file could be useful for me?
Exactly the same -- mp_stream_rw
Hi Jimmo, I've got another problem. When I call mp_reader_new_file function, it works well. But if I directly call mp_vfs_open, the "NLR jump failed error" occurs. Could you please help me figure out what might cause this problem? Thanks.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to read a file using C language

Post by jimmo » Thu Nov 12, 2020 11:54 am

heiseiggg wrote:
Thu Nov 12, 2020 6:40 am
But if I directly call mp_vfs_open, the "NLR jump failed error" occurs. Could you please help me figure out what might cause this problem? Thanks.
NLR is the way exceptions are handled in MicroPython -- this means that mp_vfs_open is raising an exception which your code is not handling. Here's an example of writing an exception handler in C:

Code: Select all

    nlr_buf_t nlr;
    if (nlr_push(&nlr) == 0) {
        // do stuff
        nlr_pop();
    } else {
        mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
        return MP_OBJ_NULL;
    }
I'm not sure why mp_vfs_open is failing, but perhaps something to point out is that until _boot.py runs on ESP32, there is no filesystem mounted.

Pytri
Posts: 2
Joined: Fri Nov 13, 2020 7:50 pm

Re: How to read a file using C language

Post by Pytri » Fri Nov 13, 2020 7:53 pm

jimmo wrote:
Wed Nov 11, 2020 12:12 am
heiseiggg wrote:
Tue Nov 10, 2020 11:21 am
I'm currently using MicroPython to develop esp32 board. Now I need to read a file in flash in main.c. How could I do this? Could anybody show me an example? Thanks
You can use mp_vfs_open (see extmod/vfs.h) to open the file which will return a stream object, that you can use mp_stream_rw (or mp_stream_read_exactly) to read bytes from (see py/stream.h). You can see an example of this vfs_reader.c (which is what is used by the lexer to load Python files from flash).

This is how builtin "open" works (see esp32/mpconfigport.h where it maps mp_builtin_open to mp_vfs_open).
Thanks, helped me as well.

Post Reply