Unexpected Reboot while Updating Chart Series

Hi everyone,

I am expecting a strange behavior from my device while trying to update chart series.

I am working with ESP32, with ESP-IDF version 4.3.1.

Sometimes happens that the device reboots while updating the chart series, and when it happens, it happens on the #61 element of the array.

I am sure the array is consistent because it arrives from a queue and the same series is plotted on a web interface, which works without any problems.
Here is a part of my code:

static StreamPlot_t localPlotStruct;
memset(&localPlotStruct, 0, sizeof(StreamPlot_t));
---
        plotQueueRet = tOs_receiveUssWaveformData(&localPlotStruct, 0);

        series_upsWaveform->color = lv_palette_main(LV_PALETTE_RED);
        series_dnsWaveform->color = lv_palette_main(LV_PALETTE_GREEN);

        if (plotQueueRet != false) {
            uint16_t plotMaxLen = localPlotStruct.upsPlot.plotSize;

            uint16_t currChartLen = lv_chart_get_point_count(chartObj);

            if(currChartLen != plotMaxLen){
                lv_chart_set_point_count(chartObj, plotMaxLen);
                lv_chart_refresh(chartObj);
            }

            lv_chart_set_ext_y_array(chartObj, series_upsWaveform, &localPlotStruct.upsPlot.plotWaveform[0]);
            lv_chart_set_ext_y_array(chartObj, series_dnsWaveform, &localPlotStruct.dnsPlot.plotWaveform[0]);

            lv_chart_set_range(chartObj, LV_CHART_AXIS_PRIMARY_X, 0, plotMaxLen);

            lv_chart_refresh(chartObj);

The strange this is that it happens sometimes and only the first time i boot the device.
After that the device seems to work properly, whitout any unwanted reboots.

@kisvegabor

Thank you for the kind help!

Is there something interesting in the ESP crash log? Do you see what could be the reason of the reboot?

Note that LVGL is not thread safe by default. Please take a look at Operating system and interrupts — LVGL documentation

Thank you for you reply!

In the ESP log there are no interesting info, it tells me that the lv_chart function finds a non-consistent data, or something like that.
Right now I do not remember the exact message. When it will happen again I will update this message.

The reason, in my opinion, is quite hidden somewhere, because I am using all the FREERTOS features to prevent this kind of errors.

I have put the lv_task_handler between mutex functions as reported in documentation, but not before and after every lvgl function. My fault.

The LV_EVENT_REFRESH is called from a lv_timer which checks if the FreeRTOS event is arrived or not. If so, the array is then peeked by a 1-sized queue, which will surely contain data.
Again, the same queue is peeked from a webserver and it shows properly the data without any issue.

I will try to add the mutex before and after the lv_ functions and then I will let you know.

Thank you!

@kisvegabor just for clarification.

The mutex should be called also inside the lv_event callbacks?
I have a single task working with LVGL and I update every object through lv_events.

There is no need to use mutexes inside events, as they are called by lv_timer_handler, which should already have a mutex around it.

thank you @embeddedt !

That is what I was supposing.

My code snippet is inside an event, so the part relative to the thread safetiness won’t apply.

Any other suggestions?

I’m not really sure what inconsistent data in an array means, but maybe when you add data to the localPlotStruct.upsPlot.plotWaveform array it might be read by the chart at the same time (the write task is interrupted?) and ESP might see half written data?

So what if you add mutex around all localPlotStruct writes?