How to add a library and a display driver to lv_micropython

Hi everyone,


Currently, I am working with LittlevGL with Micropython. As I tried to did some research on the lv_micropython repo from Github, my display module is not supported. Therefore, I had to write my own display driver in C and ran it with STM32CubeIDE. After a long time of failing, our team could run lvgl objects like buttons on the screen. the driver we wrote mostly based on Discovery library.

What MCU/Processor/Board and compiler are you using?

STM32L4R9I Discovery

What do you want to achieve?

I want to add the display driver we wrote into lv_micropython. And also, because the driver based on Discovery library, I also need to add that library to lv_micropython too.

What have you tried so far?

I don’t know where to start.

The driver is here:

Tagging @amirgon on this conversation.

First step - try to build lv_micropython with your driver.

lv_micropython now supports stm32, see this PR:

Add the relevant files (your display.c file as well as the necessary BSP files) to the stm32 makefile and try to build and deploy it.

Another option, if you don’t want to change stm32 Makefile, is to add your code an en extenral module as explained here:

You should be able to get the micropython prompt (through UART).
Try to import lvstm32 and lvgl.

Next step, if everything builds and works fine, you need a way to call your driver functions from Micropython. This is done by creating a micropython module that wraps your display driver.

From your code it looks like your driver gets no parameters. How do you configure it? set its IO pins etc? In Micropython all these are usually set on runtime.
Once you decide how your module should look like (which parameters it should receive), please have a look at That’s a guide that explains how to add micropython user module and how to build it.

My suggestion is to have an init function which receives the parameters to configure your driver, and a deinit function to remove the driver.
You can call lv_disp_drv_register in your init function (like you do in your display.c), but another option is to call lv.disp_drv_register in micropython and let the driver provide a pointer to flush_cb, like is done on ili9341 driver on ESP32.

Hi @embeddedt,

Thank you for tagging @amirgon into this conversation.

Hi @amirgon,

Thank you for your detail response in this thread. I will carefully read it and initiate your instruction.
I am a bit concern about what i wrote (the display.c). As I read some already-supported display driver in lv_micropython/lib/lv_bindings/driver/esp32 (from lv_micropython Github repository: I saw quite a different style of C file than what i wrote. So Was what i did so far in correct path and if so, What you instructed me would allow me to change from display.c to what i read in the link above?

Thank you, Huy.

if everything builds and works fine, you need a way to call your driver functions from Micropython. This is done by creating a micropython module that wraps your display driver.

Hi @amirgon could you please tell me how should I do if I want to use existing micropython modules to call the drivers instead of creating my own modules. This should be easier than creating new modules but I couldn’t find yet a clear explanation of the method.

You know many STM32 Nucleo boards don’t have ethernet, wi-fi, sd etc. however they accept arduino shields. For instance I want to use this arduino ethernet sd shield on my Nucleo F401RE and Nucleo F103RB boards however the shield has Wiznet 5100 ethernet controller while mainline micropython kernel only supports w5500 and w5200 controllers. The driver is available here . I want to put this driver next to w5500 and w5200 drivers here and call it using existing micropython socket module (mount for the sd card). How can I do this? I know this question is not direct lvgl related but I appreciate your answer.

Hi @ta1db

If I understand correctly, you have some ethernet driver which is not supported by Micropython today, and you would like to use it with Micropython (through socket module).

First of all, the best place to ask is Micropython forum. I’ll do my best to answer you, but the true experts are there and could give you better advices.

I’m not sure there is a simple way to “switch” some micropython modules such that they would call different drivers, but it’s best to ask this in Micropython forum.

As far as I know, the “classical” way to add support for a new driver is to create a native Micropython module for it.

You can, if you want, take an existing module and change it to use your driver. You would still need to edit C code, build and deploy firmware etc. I’m not familiar with the socket module and its drivers so I’m not sure what is the effort.

In case of lvgl, since the API is very large and changes all the time, we are generating the native Micropython module by a script instead of creating it manually.
The same script can be used to generate other modules. For example, I’ve used it for lodepng (PNG decoding library) and for ESP-IDF (ESP32 development framework API).

Hi @amirgon
Thanks for your kind answer, you gave me a good advise, I will try to modify the existing socket module.