Skip to content

fix(freertos) sync signal from isr fixed - #6793

Merged
kisvegabor merged 5 commits into
lvgl:masterfrom
nxp-mcuxpresso:bugfix/freertos-signal-from-isr
Sep 24, 2024
Merged

kisvegabor merged 5 commits into
lvgl:masterfrom
nxp-mcuxpresso:bugfix/freertos-signal-from-isr

Conversation

@cosmindanielradu19

Copy link
Copy Markdown
Contributor

Description of the feature or fix

Fixes #6692
lv_thread_sync_signal_isr needed to call _enter_critical_isr() instead of _enter_critical() because it was hanging

Notes

kisvegabor
kisvegabor previously approved these changes Sep 4, 2024

@kisvegabor kisvegabor left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@nicusorcitu

Copy link
Copy Markdown
Contributor

@liamHowatt

@liamHowatt liamHowatt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Comment thread src/osal/lv_freertos.c Outdated
Comment on lines +77 to +78
#define _enter_critical_isr() _enter_critical()
#define _exit_critical_isr() _exit_critical()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#define _enter_critical_isr() _enter_critical()
#define _exit_critical_isr() _exit_critical()
#define _enter_critical_isr() taskENTER_CRITICAL_FROM_ISR()
#define _exit_critical_isr(x) taskEXIT_CRITICAL_FROM_ISR(x)

I tested on ESP32 and that API is available. I think it would be good to use it here too.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to be this or FreeRTOS will assert in vPortExitCritical - at least on the CM4/7 port

@kschwan

kschwan commented Sep 11, 2024

Copy link
Copy Markdown

I am seeing this issue after upgrading from v9.1 to v9.2. Let me know if I can help testing. Currently working with an i.MX RT1170 CM7

@liamHowatt

Copy link
Copy Markdown
Contributor

Thanks @kschwan.

Currently working with an i.MX RT1170 CM7

So it is working now with this fix?

@nicusorcitu

Copy link
Copy Markdown
Contributor

hi @liamHowatt
I reverted the PR #6692 and let the notification sync as it was before. More details are on the PR #6692.

We can work together to fix any issue on your side.

@kschwan

kschwan commented Sep 11, 2024

Copy link
Copy Markdown

Thanks @kschwan.

Currently working with an i.MX RT1170 CM7

So it is working now with this fix?

I'll check tomorrow! I believe it will because I made a very similar change myself before coming here

@kschwan

kschwan commented Sep 12, 2024

Copy link
Copy Markdown

OK, so I checked out your branch, updated my lv_conf.h accordingly. Something's not working though. The display isn't refreshing.

I use PXP to rotate image buffers.

I attached a debugger and it looks like every thread is blocked in lv_thread_sync_wait() (ulTaskNotifyTake() @ lv_freertos.c:200)
image

@kschwan

kschwan commented Sep 12, 2024 •

Copy link
Copy Markdown

Note: I had to patch lv_draw_pxp.h to #include "../../sw/lv_draw_sw_private.h"

@kschwan

kschwan commented Sep 12, 2024 •

Copy link
Copy Markdown

If I switch to master (651f69f) and apply the following patch, it runs. At a glance it looks like the refresh period is fluctuating more than previously (9.1) though I can't say for sure

diff --git a/src/draw/nxp/pxp/lv_draw_pxp.h b/src/draw/nxp/pxp/lv_draw_pxp.h
index d5e98d791..eb0f69e17 100644
--- a/src/draw/nxp/pxp/lv_draw_pxp.h
+++ b/src/draw/nxp/pxp/lv_draw_pxp.h
@@ -24,6 +24,7 @@ extern "C" {

 #if LV_USE_DRAW_PXP
 #include "../../sw/lv_draw_sw.h"
+#include "../../sw/lv_draw_sw_private.h"

 /*********************
  *      DEFINES
diff --git a/src/osal/lv_freertos.c b/src/osal/lv_freertos.c
index 1c81a8e05..86eac9a82 100644
--- a/src/osal/lv_freertos.c
+++ b/src/osal/lv_freertos.c
@@ -349,7 +349,7 @@ lv_result_t lv_thread_sync_signal_isr(lv_thread_sync_t * pxCond)
     prvCheckCondInit(pxCond);

 #if USE_FREERTOS_TASK_NOTIFY
-    _enter_critical();
+    UBaseType_t interrupt_status = taskENTER_CRITICAL_FROM_ISR();
     TaskHandle_t task_to_notify = pxCond->xTaskToNotify;
     pxCond->xTaskToNotify = NULL;
     if(task_to_notify == NULL) {
@@ -357,7 +357,7 @@ lv_result_t lv_thread_sync_signal_isr(lv_thread_sync_t * pxCond)
         pxCond->xSyncSignal = pdTRUE;
     }
     /* If a task is already waiting, there is no need to set the sync signal */
-    _exit_critical();
+    taskEXIT_CRITICAL_FROM_ISR(interrupt_status);

     if(task_to_notify != NULL) {
         /* There is a task waiting. Send a notification to it */

@nicusorcitu

Copy link
Copy Markdown
Contributor

I also see this blocking status with PXP draw + SW draw enabled.
The commit that caused it is that PXP interrupt now uses sync due to a PR #6298

So now, the PXP interrupt and LVGL main thread both uses the same thread handle to be signaled. So after the PXP IRQ comes, a different thread is being woke up.
The solution would be to initialize the PXP interrupt from the PXP thread and not from LVGL thread. I will get back with a fix for this.

@nicusorcitu

Copy link
Copy Markdown
Contributor

Have to solve that problem too on PXP.
PXP wait can be called from two threads now: from PXP draw thread (for drawing) and from LVGL main thread (for screen rotation).
Need to find a solution to make it work with thread synchronization because it is a single task handle that can be signaled.
The PR with changes on interrupt from semaphores to task sync should have not been merged either #6298

@kisvegabor

Copy link
Copy Markdown
Member

I'm reading #6692 and these comments and trying to understand the root of the issue, but I'm not quite there yet.

The original issue was to solve calling lv_init and lv_timer_handler from different threads. If they are called from the same thread does it work well?


There is some thing which might be an issue in the current logic. In lv_draw_dispatch() we have this:

        if(!task_dispatched) {
            lv_draw_wait_for_finish();
            lv_draw_dispatch_request();
        }

Note that

  • task_dispatched==true means that at least on draw unit is working on something. Maybe it was dispatched earlier, maybe now, doesn't matter.
  • if(!task_dispatched) will be true when all the draw units are idle, they don't do anything.
  • Then why should we wait_for_finish? By definition there is nothing to finish.

@nicusorcitu

Copy link
Copy Markdown
Contributor

task_dispatched is true only if dispatcher dispatched something to draw units.
So it can be false in those cases: unit does not support the task or no more tasks in the queue. If task_dispatched is false it does not necessarily mean the units are idle. Some units can be still busy processing.
Even if it is the idle case, e.g. all tasks are completed, it should work. There will be just one single wait for finish triggered which is harmless.
I put here an example on how lv_draw_wait_for_finish() works:

@nicusorcitu

nicusorcitu commented Sep 16, 2024 •

Copy link
Copy Markdown
Contributor

@liamHowatt @kisvegabor Let me do some more work and I will get back with another fix, hopefully keeping the code that @liamHowatt implemented here

pxCond->xTaskToNotify = current_task_handle;

Because we will need that code while the PXP interrupt have to signal two different tasks: draw task (for drawing) and LVGL main task (for screen rotation). In the past while using semaphores it was no issue, but once with PR #6298 we need to handle it with task sync too.

Signed-off-by: Nicușor Cîțu <[email protected]>
Signed-off-by: Cosmin-Daniel Radu <[email protected]>
@nicusorcitu
nicusorcitu force-pushed the bugfix/freertos-signal-from-isr branch from a5891f4 to b43de47 Compare September 16, 2024 10:35
@nicusorcitu

nicusorcitu commented Sep 16, 2024 •

Copy link
Copy Markdown
Contributor

Hi @liamHowatt @kschwan please let me know if this solution works for you. It works for me on all combinations:
SW + VGLite + PXP
SW + PXP
SW + VGLite
VGLite + PXP

VGLite still needs an extra patch but I will send it separately on a different PR. I intend to do it soon to fixup the remaining issues on lvgl 9.2.0

@nicusorcitu
nicusorcitu force-pushed the bugfix/freertos-signal-from-isr branch from b43de47 to bbb1a90 Compare September 16, 2024 13:21
@kisvegabor

Copy link
Copy Markdown
Member

So it can be false in those cases: unit does not support the task or no more tasks in the queue. If task_dispatched is false it does not necessarily mean the units are idle.

If there is no supported draw task for a unit or the list is empty, the draw unit will be idle. Just to be sure we understand it the same way: if the draw unit it busy and cannot take new tasks, the dispatcher should return 0 (meaning 0 tasks were taken)

Some units can be still busy processing.

The return value of all draw unit dispatches are combined into a single result here.

There will be just one single wait for finish triggered which is harmless.

If it should work without this wait too, let's remove it to see if we understand what's happening correctly.


I'm not familiar with the FreeRTOS part, so @liamHowatt will comment on it.

@nicusorcitu

Copy link
Copy Markdown
Contributor

@kisvegabor

Some units can be still busy processing.

The return value of all draw unit dispatches are combined into a single result here.

VGLite dispatcher will never return:
0 - busy to handle the previous task (e.g. not yet queued to the GPU).
But will not return busy to signal the GPU HW is busy. That is different.
1 - for task dispatched
-1 - otherwise (task not supported or task list empty)
LV_DRAW_UNIT_IDLE define need to be renamed or at least let's agree on a return code convention and make the changes appropriate on all draw units.

So, if task is not supported or task list is empty, the GPU HW might be busy, so we can call a wait for finish which will make sure the unit is done processing all tasks.
I have done a separate patch that actually does wait for completion, and it works.
I will push the patch once with these freertos changes even my intend was to send it separately together with other vglite fixes.

@kisvegabor

Copy link
Copy Markdown
Member

Why can't VGLite return 0 when:

  1. Some tasks are already queued
  2. There is no available draw task at this moment

0 would indicate that I couldn't take a new task, but I'm doing something.

I think the missing information for me is the new flow of VGLite. When will VGLite render the queued tasks? Only on lv_draw_wait_for_finish or are there any other cases?

@kisvegabor

Copy link
Copy Markdown
Member

Okay, thank you! Let's keep logic as it is 🙂

@nicusorcitu

nicusorcitu commented Sep 17, 2024 •

Copy link
Copy Markdown
Contributor

I have not changed any logic in the main dispatcher.
The only callback was this lv_draw_wait_for_finish() here can be NULL on the other units. So any other unit is not affected.

@kschwan

kschwan commented Sep 17, 2024

Copy link
Copy Markdown

Hi @liamHowatt @kschwan please let me know if this solution works for you. It works for me on all combinations: SW + VGLite + PXP SW + PXP SW + VGLite VGLite + PXP

VGLite still needs an extra patch but I will send it separately on a different PR. I intend to do it soon to fixup the remaining issues on lvgl 9.2.0

Thanks! I'll give it a spin tomorrow.

@kschwan

kschwan commented Sep 18, 2024 •

Copy link
Copy Markdown

I'm seeing a weird issue: The display is not refreshing if I disable LV_USE_PERF_MONITOR. With LV_USE_PERF_MONITOR enabled, it seems to be working correctly.

I having same issue on release/v9.1, too. I just didn't notice for a while because I had perf monitor enabled.

lv_timer_handler() appears to be returning 0xffffffff.

@nicusorcitu

Copy link
Copy Markdown
Contributor

Please open a separate thread for that issue.

@kschwan

kschwan commented Sep 18, 2024

Copy link
Copy Markdown

Please open a separate thread for that issue.

Thanks, will do

@kschwan

kschwan commented Sep 18, 2024 •

Copy link
Copy Markdown

Looks like it's working correctly on my end (with PXP/FreeRTOS) :)

@nicusorcitu

Copy link
Copy Markdown
Contributor

Hi @liamHowatt would you please review this PR? I let your implementation, just fixed the ISR routines and handled the loop case (we discussed) inside the _vglite_wait_for_finish(). Thanks.

kisvegabor
kisvegabor previously approved these changes Sep 19, 2024

@kisvegabor kisvegabor left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay for me in general, but @liamHowatt can add more meaningful comments.

@liamHowatt liamHowatt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works great on ESP IDF. There are a few nice catches you made.

Just two things.

Comment thread Kconfig Resolved
Comment thread src/osal/lv_freertos.c Resolved
@nicusorcitu
nicusorcitu force-pushed the bugfix/freertos-signal-from-isr branch from fb975da to bfb6cec Compare September 23, 2024 06:02

@liamHowatt liamHowatt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@kisvegabor
kisvegabor merged commit 9db0ee3 into lvgl:master Sep 24, 2024
@nicusorcitu
nicusorcitu deleted the bugfix/freertos-signal-from-isr branch September 26, 2024 05:50
kisvegabor pushed a commit to kisvegabor/lvgl_fork that referenced this pull request Sep 26, 2024
Signed-off-by: Nicușor Cîțu <[email protected]>
Signed-off-by: Cosmin-Daniel Radu <[email protected]>
Co-authored-by: Nicușor Cîțu <[email protected]>
@kdschlosser kdschlosser mentioned this pull request Sep 28, 2024
kisvegabor pushed a commit to kisvegabor/lvgl_fork that referenced this pull request Oct 1, 2024
Signed-off-by: Nicușor Cîțu <[email protected]>
Signed-off-by: Cosmin-Daniel Radu <[email protected]>
Co-authored-by: Nicușor Cîțu <[email protected]>
kisvegabor pushed a commit that referenced this pull request Oct 9, 2024
Signed-off-by: Nicușor Cîțu <[email protected]>
Signed-off-by: Cosmin-Daniel Radu <[email protected]>
Co-authored-by: Nicușor Cîțu <[email protected]>
rodb70 pushed a commit to rodb70/lvgl that referenced this pull request Nov 8, 2024
Signed-off-by: Nicușor Cîțu <[email protected]>
Signed-off-by: Cosmin-Daniel Radu <[email protected]>
Co-authored-by: Nicușor Cîțu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants