lv_port_disp.c 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-10-18     Meco Man     The first version
 */
#include <lvgl.h>
#include <lcd_port.h>

L
liukangcc 已提交
13 14 15 16
//#define DRV_DEBUG
#define LOG_TAG             "lvgl.disp"
#include <drv_log.h>

17 18 19 20 21 22 23 24
/*A static or global variable to store the buffers*/
static lv_disp_draw_buf_t disp_buf;

rt_device_t lcd_device = 0;
static struct rt_device_graphic_info info;

static lv_disp_drv_t disp_drv;  /*Descriptor of a display driver*/

L
liukangcc 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
static DMA_HandleTypeDef         DmaHandle;
#define DMA_STREAM               DMA2_Stream0
#define DMA_CHANNEL              DMA_CHANNEL_0
#define DMA_STREAM_IRQ           DMA2_Stream0_IRQn
#define DMA_STREAM_IRQHANDLER    DMA2_Stream0_IRQHandler

static int32_t x1_flush;
static int32_t y1_flush;
static int32_t x2_flush;
static int32_t y2_fill;
static int32_t y_fill_act;
static const lv_color_t * buf_to_flush;
/**
  * @brief  DMA conversion complete callback
  * @note   This function is executed when the transfer complete interrupt
  *         is generated
  * @retval None
  */
static void DMA_TransferComplete(DMA_HandleTypeDef *han)
44
{
L
liukangcc 已提交
45
    y_fill_act ++;
46

L
liukangcc 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    if(y_fill_act > y2_fill)
    {
          lv_disp_flush_ready(&disp_drv);
    }
    else
    {
        buf_to_flush += (x2_flush - x1_flush + 1);
        /*##-7- Start the DMA transfer using the interrupt mode ####################*/
        /* Configure the source, destination and buffer size DMA fields and Start DMA Stream transfer */
        /* Enable All the DMA interrupts */
        if (HAL_DMA_Start_IT(han,(uint32_t)buf_to_flush,
            (uint32_t)&((uint32_t *)info.framebuffer)[y_fill_act * info.width + x1_flush],
            (x2_flush - x1_flush + 1)) != HAL_OK)
        {
            LOG_E("HAL_DMA_Start_IT error");
            while(1);	/*Halt on error*/
        }
    }
65 66
}

L
liukangcc 已提交
67 68 69 70 71 72 73
/**
  * @brief  DMA conversion error callback
  * @note   This function is executed when the transfer error interrupt
  *         is generated during DMA transfer
  * @retval None
  */
static void DMA_TransferError(DMA_HandleTypeDef *han)
74
{
L
liukangcc 已提交
75 76
    LOG_E("dma transfer error");
    while(1);
77 78
}

L
liukangcc 已提交
79 80 81 82 83 84
/**
  * @brief  This function handles DMA Stream interrupt request.
  * @param  None
  * @retval None
  */
void DMA_STREAM_IRQHANDLER(void)
85
{
L
liukangcc 已提交
86 87 88 89
    rt_interrupt_enter();
    HAL_DMA_IRQHandler(&DmaHandle);
    rt_interrupt_leave();
}
90

L
liukangcc 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
static void DMA_Config(void)
{
    /*## -1- Enable DMA2 clock #################################################*/
    __HAL_RCC_DMA2_CLK_ENABLE();

    /*##-2- Select the DMA functional Parameters ###############################*/
    DmaHandle.Init.Channel = DMA_CHANNEL;                     /* DMA_CHANNEL_0                    */
    DmaHandle.Init.Direction = DMA_MEMORY_TO_MEMORY;          /* M2M transfer mode                */
    DmaHandle.Init.PeriphInc = DMA_PINC_ENABLE;               /* Peripheral increment mode Enable */
    DmaHandle.Init.MemInc = DMA_MINC_ENABLE;                  /* Memory increment mode Enable     */
    DmaHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; /* Peripheral data alignment : 32bit */
    DmaHandle.Init.MemDataAlignment = DMA_PDATAALIGN_WORD;    /* memory data alignment : 32bit     */
    DmaHandle.Init.Mode = DMA_NORMAL;                         /* Normal DMA mode                  */
    DmaHandle.Init.Priority = DMA_PRIORITY_HIGH;              /* priority level : high            */
    DmaHandle.Init.FIFOMode = DMA_FIFOMODE_ENABLE;            /* FIFO mode enabled                */
    DmaHandle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_1QUARTERFULL;   /* FIFO threshold: 1/4 full   */
    DmaHandle.Init.MemBurst = DMA_MBURST_SINGLE;              /* Memory burst                     */
    DmaHandle.Init.PeriphBurst = DMA_PBURST_SINGLE;           /* Peripheral burst                 */

    DmaHandle.Instance = DMA_STREAM;

    if (HAL_DMA_Init(&DmaHandle) != HAL_OK)
113
    {
L
liukangcc 已提交
114
        while(1);
115 116
    }

L
liukangcc 已提交
117 118
    HAL_DMA_RegisterCallback(&DmaHandle, HAL_DMA_XFER_CPLT_CB_ID, DMA_TransferComplete);
    HAL_DMA_RegisterCallback(&DmaHandle, HAL_DMA_XFER_ERROR_CB_ID, DMA_TransferError);
119

L
liukangcc 已提交
120 121 122
    HAL_NVIC_SetPriority(DMA_STREAM_IRQ, 0, 0);
    HAL_NVIC_EnableIRQ(DMA_STREAM_IRQ);
}
123

L
liukangcc 已提交
124 125 126 127 128 129 130
static void lcd_fb_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
{
    /*Return if the area is out the screen*/
    if (area->x2 < 0) return;
    if (area->y2 < 0) return;
    if (area->x1 > info.width - 1) return;
    if (area->y1 > info.height - 1) return;
131

L
liukangcc 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    /*Truncate the area to the screen*/
    int32_t act_x1 = area->x1 < 0 ? 0 : area->x1;
    int32_t act_y1 = area->y1 < 0 ? 0 : area->y1;
    int32_t act_x2 = area->x2 > info.width - 1 ? info.width - 1 : area->x2;
    int32_t act_y2 = area->y2 > info.height - 1 ? info.height - 1 : area->y2;

    x1_flush = act_x1;
    y1_flush = act_y1;
    x2_flush = act_x2;
    y2_fill = act_y2;
    y_fill_act = act_y1;
    buf_to_flush = color_p;

    if (HAL_DMA_Start_IT(&DmaHandle,(uint32_t)buf_to_flush,
        (uint32_t)&((uint32_t *)info.framebuffer)[y_fill_act * info.width + x1_flush],
        (x2_flush - x1_flush + 1)) != HAL_OK)
148
    {
L
liukangcc 已提交
149 150
        LOG_E("dma start it error");
        while(1);
151 152 153 154 155 156
    }
}

void lv_port_disp_init(void)
{
    rt_err_t result;
L
liukangcc 已提交
157 158
    void* buf_1 = RT_NULL;
    void* buf_2 = RT_NULL;
159 160 161 162

    lcd_device = rt_device_find("lcd");
    if (lcd_device == 0)
    {
L
liukangcc 已提交
163
        LOG_E("error!");
164 165 166 167 168
        return;
    }
    result = rt_device_open(lcd_device, 0);
    if (result != RT_EOK)
    {
L
liukangcc 已提交
169
        LOG_E("error!");
170 171
        return;
    }
L
liukangcc 已提交
172

173 174 175 176
    /* get framebuffer address */
    result = rt_device_control(lcd_device, RTGRAPHIC_CTRL_GET_INFO, &info);
    if (result != RT_EOK)
    {
L
liukangcc 已提交
177
        LOG_E("error!");
178 179 180 181
        /* get device information failed */
        return;
    }

L
liukangcc 已提交
182
    RT_ASSERT (info.bits_per_pixel == 8 || info.bits_per_pixel == 16 ||
183 184
              info.bits_per_pixel == 24 || info.bits_per_pixel == 32);

L
liukangcc 已提交
185 186 187 188 189 190 191 192 193 194 195
    DMA_Config();

    buf_1 = rt_malloc(info.width * 40 * sizeof(lv_color_t));
    if (buf_1 == RT_NULL)
    {
        LOG_E("malloc memory failed");
        return;
    }

    buf_2 = rt_malloc(info.width * 40 * sizeof(lv_color_t));
    if (buf_2 == RT_NULL)
196
    {
L
liukangcc 已提交
197
        LOG_E("malloc memory failed");
198 199 200
        return;
    }

L
liukangcc 已提交
201 202
    /*Initialize `disp_buf` with the buffer(s).*/
    lv_disp_draw_buf_init(&disp_buf, buf_1, buf_2, info.width * 40);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

    lv_disp_drv_init(&disp_drv); /*Basic initialization*/

    /*Set the resolution of the display*/
    disp_drv.hor_res = info.width;
    disp_drv.ver_res = info.height;

    /*Set a display buffer*/
    disp_drv.draw_buf = &disp_buf;

    /*Used to copy the buffer's content to the display*/
    disp_drv.flush_cb = lcd_fb_flush;

    /*Finally register the driver*/
    lv_disp_drv_register(&disp_drv);
}