lv_lib_freetype icon indicating copy to clipboard operation
lv_lib_freetype copied to clipboard

freetype for MCU

Open CalvinML opened this issue 4 years ago • 21 comments

The "freeType" library tested in LVGL emulator is normal, can "LVGL + FreeType" add support for MCU?

CalvinML avatar Aug 23 '21 02:08 CalvinML

It'a certainly possible to build FreeType on MCUs but it requires some adjustments on FreeTypes build system.


I moved this issue to the FreeType repo.

kisvegabor avatar Aug 24 '21 10:08 kisvegabor

As I understand it, this library is not compatible with MCUs, but it is compatible with simulators, right?

Miguel0101 avatar Sep 22 '21 11:09 Miguel0101

FreeType can run on sufficiently powerful MCUs as well. I have used it on STM32 before, though not with LVGL.

embeddedt avatar Sep 22 '21 12:09 embeddedt

FreeType can run on sufficiently powerful MCUs as well. I have used it on STM32 before, though not with LVGL.

Some MCUs do not support the C File API(fopen, etc). EDIT: Does lv_lib_freetype supports ESP32?

Miguel0101 avatar Sep 22 '21 12:09 Miguel0101

I saw some implementations on ESP32, but it's unrelated to LVGL so I can't comment much on it.

kisvegabor avatar Sep 23 '21 15:09 kisvegabor

I added freetype support to lvgl in Tasmota on Esp32. It runs fine but you need to be cautious about memory and stack size. The default 8kb stack is insufficient for freetype. I suppose similar MCUs will work with the same constraints.

s-hadinger avatar Sep 23 '21 16:09 s-hadinger

I added freetype support to lvgl in Tasmota on Esp32.

That's great! Can share some details about how did you port FreeType to ESP32?

kisvegabor avatar Sep 23 '21 19:09 kisvegabor

I agree with @kisvegabor. It would be great to support ESP32.

Miguel0101 avatar Sep 29 '21 14:09 Miguel0101

I added freetype support to lvgl in Tasmota on Esp32.

That's great! Can share some details about how did you port FreeType to ESP32?

Sure, and sorry for the late response.

First FreeType compiles out of the box on ESP32, so it does not require specific work here. The main problem is that calling get_glyph_dsc_cb() consumes more than 16KB of stack whereas the default ESP32 stack is 8KB. This yields to a crash.

Currently I run get_glyph_dsc_cb() in a separate FreeRTOS task with 24KB of stack allocated and pass parameters through a queue. I'm not satisfied with the solution and discovered in between that esp-idf has a function to pass a temporary stack bigger than the default one.

Here is the patched code: https://github.com/arendst/Tasmota/blob/development/lib/libesp32_lvgl/lv_lib_freetype/lv_freetype.c

s-hadinger avatar Oct 07 '21 07:10 s-hadinger

That's great, thank you for the explanation!

kisvegabor avatar Oct 07 '21 16:10 kisvegabor

I'm also looking into adding this functionality to my ESP32 project. Thanks @s-hadinger for sharing your solution!

I discovered in between that esp-idf has a function to pass a temporary stack bigger than the default one.

Are you refering too Call function with external stack?

fvanroie avatar Nov 11 '21 03:11 fvanroie

Yes. That's the one. I will switch to this method which is simpler

s-hadinger avatar Nov 11 '21 09:11 s-hadinger

I've also made a wrapper here for someone who needs it: https://github.com/huming2207/esp-ft2

Currently:

  1. It may cause crashing due to the stack overflow issue (stack is too small)
  2. May need this patch to allow LVGL to know my wrapper's existence: https://github.com/lvgl/lvgl/pull/3240

huming2207 avatar Apr 04 '22 02:04 huming2207

It may cause crashing due to the stack overflow issue (stack is too small)

Which stack do you mean?

kisvegabor avatar Apr 04 '22 11:04 kisvegabor

Which stack do you mean?

As per @s-hadinger mentioned, the task that eventually does get_glyph_dsc_cb() needs to be big enough, otherwise it will crash due to stack overflow.

huming2207 avatar Apr 04 '22 12:04 huming2207

In my experience you need 24KB of CPU native stack to have Freetype run correctly. This gives 3 options:

  • Resize your main stack's to 24KB
  • Run get_glyph_dsc_cb() in a separate RTOS stack with a stack sized at 24KB
  • Use esp-idf esp_execute_shared_stack_function() to swap the stack with a 24KB memory chunk for this call

s-hadinger avatar Apr 04 '22 12:04 s-hadinger

In my experience you need 24KB of CPU native stack to have Freetype run correctly. This gives 3 options:

Yep. For our scenario, we can just increase the main thread's stack to be 32KB as we also have a lot of other stuff that needs the stack memory anyway.

huming2207 avatar Apr 05 '22 01:04 huming2207

I think the right way of doing this is probably to spin up a FreeRTOS task with 24KB stack in the init function, that wait and deal with FT2, and pass the data over a FreeRTOS queue to the real get_glyph_dsc_cb() and pass the result back to LVGL. Otherwise I don't know how to pass the arguments when using esp_execute_shared_stack_function() - unless if we use global variables (which I'd prefer avoid doing that).

huming2207 avatar Apr 05 '22 06:04 huming2207

This is what I did the first time, because I couldn't increase the main stack. But it's a little more complicated to do.

s-hadinger avatar Apr 05 '22 06:04 s-hadinger

Glad to use add this(LVGL+Freetype) into my ESP32 project. But my concern is the resource constraint because my project also use so much space and cpu. Can you share some info on resources usage of this Freetype?

Currently, we are using ESp32-WROOM but I can use ESP32-Wrover if it's suitable.

Thanks;

garudaonekh avatar May 02 '22 09:05 garudaonekh

Everything is explained above. You need at least 24KB of stack when calling Freetype. It's really stretched if you don't have a PSRAM.

s-hadinger avatar May 02 '22 18:05 s-hadinger