Skip to content

Commit

Permalink
Merge pull request #138 from 4ms/Fix-screen-freeze
Browse files Browse the repository at this point in the history
Fix screen freezing issue
  • Loading branch information
danngreen authored Oct 26, 2023
2 parents e3470cb + 73a69e3 commit 4eeda30
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 29 deletions.
7 changes: 4 additions & 3 deletions firmware/src/gui/ui.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace MetaModule
{

using FrameBufferT = std::array<lv_color_t, ScreenBufferConf::width * ScreenBufferConf::height / 8>;
static inline __attribute__((section(".ddma"))) FrameBufferT framebuf1;
static inline __attribute__((section(".ddma"))) FrameBufferT framebuf2;
static inline __attribute__((section(".ddma"))) FrameBufferT framebuf1 alignas(64);
static inline __attribute__((section(".ddma"))) FrameBufferT framebuf2 alignas(64);

class Ui {
private:
Expand All @@ -31,7 +31,8 @@ private:
ParamDbgPrint print_dbg_params{params, metaparams};

static inline UartLog init_uart;
static inline LVGLDriver gui{MMDisplay::flush_to_screen, MMDisplay::read_input, framebuf1, framebuf2};
static inline LVGLDriver gui{
MMDisplay::flush_to_screen, MMDisplay::read_input, MMDisplay::wait_cb, framebuf1, framebuf2};

public:
Ui(PatchPlayLoader &patch_playloader,
Expand Down
4 changes: 2 additions & 2 deletions firmware/src/medium/conf/screen_conf.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ struct MMScreenConf : mdrivlib::DefaultSpiScreenConf {

struct DMAConf : mdrivlib::DefaultDMAConf {
static constexpr auto DMAx = 1;
static constexpr auto StreamNum = 3;
static constexpr auto StreamNum = 0;
static constexpr auto RequestNum = DMA_REQUEST_SPI4_TX;
static constexpr auto IRQn = DMA1_Stream3_IRQn;
static constexpr auto IRQn = DMA1_Stream0_IRQn;
static constexpr auto pri = 0;
static constexpr auto subpri = 0;
static constexpr auto dir = Mem2Periph;
Expand Down
74 changes: 51 additions & 23 deletions firmware/src/screen/lvgl_driver.hh
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include "conf/screen_buffer_conf.hh"
#include "conf/screen_conf.hh"
#include "drivers/screen_ST77XX.hh"
//#include "drivers/screen_ILI9341.hh"
//#include "drivers/screen_ltdc.hh"
//#include "drivers/screen_ltdc_setup.hh"
#include "drivers/timekeeper.hh"
#include "lvgl/lvgl.h"
#include "params/metaparams.hh"
Expand All @@ -12,6 +9,8 @@
#include "uart_log.hh"
#include <span>

// #define MONKEYROTARY

namespace MetaModule
{
class LVGLDriver {
Expand All @@ -25,21 +24,27 @@ class LVGLDriver {
// Callbacks
using flush_cb_t = void(lv_disp_drv_t *, const lv_area_t *, lv_color_t *);
using indev_cb_t = void(lv_indev_drv_t *indev, lv_indev_data_t *data);
using wait_cb_t = void(lv_disp_drv_t *);

// Display driver
lv_disp_drv_t disp_drv;
lv_disp_t *display;
// lv_theme_t *theme;

public:
LVGLDriver(flush_cb_t flush_cb, indev_cb_t indev_cb, std::span<lv_color_t> buffer1, std::span<lv_color_t> buffer2) {
LVGLDriver(flush_cb_t flush_cb,
indev_cb_t indev_cb,
wait_cb_t wait_cb,
std::span<lv_color_t> buffer1,
std::span<lv_color_t> buffer2) {
UartLog::log("\n\nLVGLDriver started\n");

lv_init();
lv_disp_draw_buf_init(&disp_buf, buffer1.data(), buffer2.data(), buffer1.size());
lv_disp_drv_init(&disp_drv);
disp_drv.draw_buf = &disp_buf;
disp_drv.flush_cb = flush_cb;
disp_drv.wait_cb = wait_cb;
disp_drv.hor_res = ScreenWidth;
disp_drv.ver_res = ScreenHeight;
display = lv_disp_drv_register(&disp_drv); // NOLINT
Expand All @@ -66,8 +71,6 @@ class MMDisplay {

private:
static inline ScreenFrameWriter _spi_driver;
// static inline ScreenParallelWriter<ScreenConf> _ltdc_driver;
// static inline mdrivlib::LTDCParallelSetup<ScreenControlConf> _screen_configure;

static inline std::array<lv_color_t, BufferSize> testbuf;

Expand All @@ -78,22 +81,12 @@ public:
_spi_driver.init();
_spi_driver.register_partial_frame_cb(end_flush);
_spi_driver.clear_overrun_on_interrupt();

// LTDC mode:
// _screen_configure.setup_driver_chip(mdrivlib::ST77XX::ST7789InitLTDC<ScreenConf>::cmds);
// _ltdc_driver.init(buf.data());

// for (int i = 0; i < 16; i++) {
// for (auto &px : testbuf)
// px.full = (1 << i);
// // _ltdc_driver.set_buffer(buf.data());
// _spi_driver.transfer_partial_frame(0, 0, 320, 240, reinterpret_cast<uint16_t *>(testbuf.data()));
// __BKPT();
// }
// for (auto &px : testbuf)
// px.full = (1 << 10);
}

static inline uint32_t last_transfer_start_time = 0;
static inline lv_area_t last_area{0, 0, 0, 0};
static inline uint16_t *last_pixbuf = nullptr;

static void end_flush() {
lv_disp_flush_ready(last_used_disp_drv);
}
Expand All @@ -102,9 +95,32 @@ public:
last_used_disp_drv = disp_drv;
auto pixbuf = reinterpret_cast<uint16_t *>(color_p);
_spi_driver.transfer_partial_frame(area->x1, area->y1, area->x2, area->y2, pixbuf);
// LTDC mode:
// _ltdc_driver.set_buffer((void *)color_p);
// lv_disp_flush_ready(disp_drv);

last_transfer_start_time = HAL_GetTick();
last_area = *area;
last_pixbuf = pixbuf;
}

static void wait_cb(lv_disp_drv_t *disp_drv) {
if (disp_drv->draw_buf->flushing) {
if (_spi_driver.had_transfer_error()) {
disp_drv->draw_buf->flushing = 0;
}

if (_spi_driver.had_fifo_error()) {
HAL_Delay(1);
}

//Timeout
if (HAL_GetTick() - last_transfer_start_time > 200) {
disp_drv->draw_buf->flushing = 0;
// Doesn't seem to matter:
// _spi_driver.reinit();
// HAL_Delay(1);
// Doesn't work at all (screen is still expecting data from previous command):
// _spi_driver.transfer_partial_frame(last_area.x1, last_area.y1, last_area.x2, last_area.y2, last_pixbuf);
}
}
}

static void read_input(lv_indev_drv_t *indev, lv_indev_data_t *data) {
Expand Down Expand Up @@ -134,7 +150,19 @@ public:
#else
data->state = m->rotary_button.is_pressed() ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
#endif

#ifdef MONKEYROTARY
static uint32_t last_tm = 0;
static uint32_t count = 100;
if (HAL_GetTick() - last_tm >= count) {
data->enc_diff = HAL_GetTick() & 0b01 ? 1 : -1;
last_tm = HAL_GetTick();
count = HAL_GetTick() % count + 100;
} else
data->enc_diff = m->rotary.use_motion();
#else
data->enc_diff = m->rotary.use_motion();
#endif
}
};
} // namespace MetaModule
4 changes: 4 additions & 0 deletions firmware/src/screen/screen_writer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public:
, _colstart{ScreenWriterConfT::colstart} {
}

void reinit() {
SpiDmaDataCmdDriver<ScreenWriterConfT, ScreenTransferDriverT>::reinit();
}

void init() {
SpiDmaDataCmdDriver<ScreenWriterConfT, ScreenTransferDriverT>::init();
_reset();
Expand Down

0 comments on commit 4eeda30

Please sign in to comment.