aboutsummaryrefslogtreecommitdiff
path: root/docs/porting/os.rst
blob: e713799fd50eec0b9dc6025d766f926e15ad6908 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
.. _os_interrupt:

===============================
Operating system and interrupts
===============================

LVGL is **not thread-safe** by default.

However, in the following conditions it's valid to call LVGL related
functions:

- In *events*. Learn more in :ref:`events`.
- In *lv_timer*. Learn more in :ref:`timer`.

Tasks and threads
-----------------

If you need to use real tasks or threads, you need a mutex which should
be invoked before the call of :cpp:func:`lv_timer_handler` and released after
it. Also, you have to use the same mutex in other tasks and threads
around every LVGL (``lv_...``) related function call and code. This way
you can use LVGL in a real multitasking environment. Just make use of a
mutex to avoid the concurrent calling of LVGL functions.

LVGL has a built-in mutex which can be used with:
- :cpp:func:`lv_lock()` and :cpp:func:`lv_lock_isr()`
- :cpp:func:`lv_unlock()`

These functions are called internally in :cpp:func:`lv_timer_handler`
and the users need to call them only from their own threads.

To enable ``lv_lock/lv_unlock`` ``LV_USE_OS`` needs to be set to other
than ``LV_OS_NONE``.


Here is some pseudocode to illustrate the concept:

.. code:: c

   void lvgl_thread(void)
   {
       while(1) {
           uint32_t time_till_next;
           time_till_next = lv_timer_handler(); /*lv_lock/lv_unlock is called internally*/
           thread_sleep(time_till_next); /* sleep for a while */
       }
   }

   void other_thread(void)
   {
       /* You must always hold the mutex while using LVGL APIs */
       lv_lock();
       lv_obj_t *img = lv_image_create(lv_screen_active());
       lv_unlock();

       while(1) {
           lv_lock();
           /* change to the next image */
           lv_image_set_src(img, next_image);
           lv_unlock();
           thread_sleep(2000);
       }
   }

Interrupts
----------

Try to avoid calling LVGL functions from interrupt handlers (except
:cpp:func:`lv_tick_inc` and :cpp:func:`lv_display_flush_ready`). But if you need to do
this you have to disable the interrupt which uses LVGL functions while
:cpp:func:`lv_timer_handler` is running.

It's a better approach to simply set a flag or some value in the
interrupt, and periodically check it in an LVGL timer (which is run by
:cpp:func:`lv_timer_handler`).