lcd_ssd1963.c 11.9 KB
Newer Older
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
/**************************************************************************//**
*
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date            Author       Notes
* 2022-2-23       Wayne        First version
*
******************************************************************************/

#include <rtconfig.h>

#if defined(NU_PKG_USING_SSD1963)

#include <rtdevice.h>
#include <lcd_ssd1963.h>

#if defined(NU_PKG_SSD1963_WITH_OFFSCREEN_FRAMEBUFFER)
    #if !defined(NU_PKG_SSD1963_LINE_BUFFER_NUMBER)
        #define NU_PKG_SSD1963_LINE_BUFFER_NUMBER   YSIZE_PHYS
    #endif
#endif

#define ssd1963_delay_ms(ms)    rt_thread_mdelay(ms)
27
static void ssd1963_fillscreen(rt_uint16_t color);
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

static struct rt_device_graphic_info g_SSD1963Info =
{
    .bits_per_pixel = 16,
    .pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565,
    .framebuffer = RT_NULL,
    .width = XSIZE_PHYS,
    .pitch = XSIZE_PHYS * 2,
    .height = YSIZE_PHYS
};

static rt_err_t ssd1963_pin_init(void)
{
    rt_pin_mode(BOARD_USING_SSD1963_PIN_DC, PIN_MODE_OUTPUT);
    rt_pin_mode(BOARD_USING_SSD1963_PIN_RESET, PIN_MODE_OUTPUT);
    rt_pin_mode(BOARD_USING_SSD1963_PIN_BACKLIGHT, PIN_MODE_OUTPUT);
    rt_pin_mode(BOARD_USING_SSD1963_PIN_DISPLAY, PIN_MODE_OUTPUT);

    CLR_RS;
    CLR_RST;
    SET_BACKLIGHT_OFF;
    SET_DISP_OFF;

    return RT_EOK;
}

static rt_err_t ssd1963_lcd_init(rt_device_t dev)
{
    /* Hardware reset */
    SET_RST;
    ssd1963_delay_ms(5);     // Delay 5ms

    CLR_RST;
    ssd1963_delay_ms(20);    // Delay 20ms

    SET_RST;
    ssd1963_delay_ms(40);    // Delay 40ms

    /* Initial control registers */
    ssd1963_send_cmd(0x01);    //Software reset
    ssd1963_delay_ms(10);

    ssd1963_send_cmd(0xe0);    //Start PLL. Before the start, the system was operated with the crystal oscillator or clock input.
    ssd1963_send_cmd_parameter(0x01);   //0: Disable PLL, 1:Enable PLL.

    ssd1963_delay_ms(50);

    ssd1963_send_cmd(0xe0);
    ssd1963_send_cmd_parameter(0x03);   //0: Disable PLL, 1:Enable PLL. 3:Enable+LOCK PLL
    ssd1963_delay_ms(5);

    ssd1963_send_cmd(0xb0);    //SET LCD MODE  SET TFT MODE
    ssd1963_send_cmd_parameter(0x20);   //SET 24Bit, Disable TFT FRC & dithering, DCLK in falling edge, LLINE Active low, LFRAME Active low
    ssd1963_send_cmd_parameter(0x00);   //SET TFT mode
    ssd1963_send_cmd_parameter(0x01);   //SET horizontal size=480-1 HightByte
    ssd1963_send_cmd_parameter(0xdf);   //SET horizontal size=480-1 LowByte
    ssd1963_send_cmd_parameter(0x01);   //SET vertical size=272-1 HightByte
    ssd1963_send_cmd_parameter(0x0f);   //SET vertical size=272-1 LowByte
    ssd1963_send_cmd_parameter(0x00);   //SET even/odd line RGB seq.=RGB

    ssd1963_send_cmd(0xf0);    //SET pixel data I/F format=16bit(565 format)
    ssd1963_send_cmd_parameter(0x03);

    //ssd1963_send_cmd(0xf0);    //SET pixel data I/F format=8bit (666 format)
    //ssd1963_send_cmd_parameter(0x00);

    ssd1963_send_cmd(0x36);    // SET read from frame buffer to the display is RGB
    ssd1963_send_cmd_parameter(0x00);

    ssd1963_send_cmd(0xe2);
    ssd1963_send_cmd_parameter(0x1d);
    ssd1963_send_cmd_parameter(0x02);
    ssd1963_send_cmd_parameter(0x54);

    ssd1963_send_cmd(0xe6);    //SET pixel clock frequency
    ssd1963_send_cmd_parameter(0x01);
    ssd1963_send_cmd_parameter(0x99);
    ssd1963_send_cmd_parameter(0x9a);

    ssd1963_send_cmd(0xb4);    //SET HBP,
    ssd1963_send_cmd_parameter(0x02);   //SET HSYNC Tatol = 525
    ssd1963_send_cmd_parameter(0x0d);
    ssd1963_send_cmd_parameter(0x00);   //SET HBP = 20
    ssd1963_send_cmd_parameter(0x14);
    ssd1963_send_cmd_parameter(0x05);    //SET VBP 5
    ssd1963_send_cmd_parameter(0x00);    //SET Hsync pulse start position
    ssd1963_send_cmd_parameter(0x00);
    ssd1963_send_cmd_parameter(0x00);    //SET Hsync pulse subpixel start position

    ssd1963_send_cmd(0xb6);     //SET VBP,
    ssd1963_send_cmd_parameter(0x01);    //SET Vsync total 292
    ssd1963_send_cmd_parameter(0x24);
    ssd1963_send_cmd_parameter(0x00);    //SET VBP = 10
    ssd1963_send_cmd_parameter(0x0a);
    ssd1963_send_cmd_parameter(0x05);    //SET Vsync pulse 5
    ssd1963_send_cmd_parameter(0x00);    //SET Vsync pulse start position
    ssd1963_send_cmd_parameter(0x00);

    ssd1963_send_cmd(0x29);    //SET display on
    ssd1963_delay_ms(5);

129 130
    ssd1963_fillscreen(0);

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
    SET_DISP_ON;

    SET_BACKLIGHT_ON;

    return RT_EOK;
}

#if defined(NU_PKG_SSD1963_WITH_OFFSCREEN_FRAMEBUFFER)
static void ssd1963_fillrect(uint16_t *pixels, struct rt_device_rect_info *pRectInfo)
{
    ssd1963_set_column(pRectInfo->x, pRectInfo->x + pRectInfo->width - 1);
    ssd1963_set_page(pRectInfo->y, pRectInfo->y + pRectInfo->height - 1);
    ssd1963_send_cmd(0x2c);

    ssd1963_send_pixels(pixels, pRectInfo->height * pRectInfo->width * 2);
}
#endif

static void ssd1963_fillscreen(rt_uint16_t color)
{
#if defined(NU_PKG_SSD1963_WITH_OFFSCREEN_FRAMEBUFFER)
    struct rt_device_rect_info rectinfo;
    int filled_line_num = 0;

    while (filled_line_num < YSIZE_PHYS)
    {
        int pixel_count;
        rectinfo.x = 0;
        rectinfo.y = filled_line_num;
        rectinfo.width = XSIZE_PHYS;
        rectinfo.height = (NU_PKG_SSD1963_LINE_BUFFER_NUMBER < YSIZE_PHYS) ? NU_PKG_SSD1963_LINE_BUFFER_NUMBER : YSIZE_PHYS;

        pixel_count = XSIZE_PHYS * NU_PKG_SSD1963_LINE_BUFFER_NUMBER;
        rt_uint16_t *pu16ShadowBuf = (rt_uint16_t *)g_SSD1963Info.framebuffer;

        while (pixel_count > 0)
        {
            *pu16ShadowBuf++ = color;
            pixel_count--;
        }
        ssd1963_fillrect((uint16_t *)g_SSD1963Info.framebuffer, &rectinfo);
        filled_line_num += NU_PKG_SSD1963_LINE_BUFFER_NUMBER;
    }
#else
    ssd1963_set_column(0, (XSIZE_PHYS - 1));
    ssd1963_set_page(0, (YSIZE_PHYS - 1));
    ssd1963_send_cmd(0x2c);

    for (int i = 0; i < (XSIZE_PHYS * YSIZE_PHYS); i++)
        ssd1963_send_pixel_data(color);
#endif
}

static void ssd1963_lcd_set_pixel(const char *color, int x, int y)
{
    ssd1963_set_column(x, x);
    ssd1963_set_page(y, y);
    ssd1963_send_cmd(0x2c);
    ssd1963_send_pixel_data(*(uint16_t *)color);
}

static void ssd1963_lcd_draw_hline(const char *pixel, int x1, int x2, int y)
{
    ssd1963_set_column(x1, x2);
    ssd1963_set_page(y, y);
    ssd1963_send_cmd(0x2c);

    for (; x1 < x2; x1++)
        ssd1963_send_pixel_data(*(uint16_t *)pixel);
}

static void ssd1963_lcd_draw_vline(const char *pixel, int x, int y1, int y2)
{
    ssd1963_set_column(x, x);
    ssd1963_set_page(y1, y2);
    ssd1963_send_cmd(0x2c);

    for (; y1 < y2; y1++)
        ssd1963_send_pixel_data(*(uint16_t *)pixel);
}

static void ssd1963_lcd_blit_line(const char *pixels, int x, int y, rt_size_t size)
{
    rt_uint16_t *ptr = (rt_uint16_t *)pixels;

    ssd1963_set_column(x, x + size);
    ssd1963_set_page(y, y);
    ssd1963_send_cmd(0x2c);

    while (size--)
        ssd1963_send_pixel_data(*ptr++);
}

static rt_err_t ssd1963_lcd_open(rt_device_t dev, rt_uint16_t oflag)
{
    return RT_EOK;
}

static rt_err_t ssd1963_lcd_close(rt_device_t dev)
{
    return RT_EOK;
}

static rt_err_t ssd1963_lcd_control(rt_device_t dev, int cmd, void *args)
{
    switch (cmd)
    {
    case RTGRAPHIC_CTRL_GET_INFO:
    {
        struct rt_device_graphic_info *info;

        info = (struct rt_device_graphic_info *) args;
        RT_ASSERT(info != RT_NULL);
        rt_memcpy(args, (void *)&g_SSD1963Info, sizeof(struct rt_device_graphic_info));
    }
    break;

    case RTGRAPHIC_CTRL_RECT_UPDATE:
    {
#if defined(NU_PKG_SSD1963_WITH_OFFSCREEN_FRAMEBUFFER)
        struct rt_device_rect_info *psRectInfo = (struct rt_device_rect_info *)args;
        rt_uint16_t *pixels  = (rt_uint16_t *)g_SSD1963Info.framebuffer;
        RT_ASSERT(args);

        ssd1963_fillrect(pixels, psRectInfo);
#else
        /* nothong to be done */
#endif
    }
    break;
    default:
        break;
    }

    return RT_EOK;
}

static struct rt_device lcd_device;
static struct rt_device_graphic_ops ssd1963_ops =
{
    ssd1963_lcd_set_pixel,
    ssd1963_lcd_get_pixel,
    ssd1963_lcd_draw_hline,
    ssd1963_lcd_draw_vline,
    ssd1963_lcd_blit_line
};

int rt_hw_lcd_ssd1963_init(void)
{
    ssd1963_pin_init();

    /* register lcd device */
    lcd_device.type = RT_Device_Class_Graphic;
    lcd_device.init = ssd1963_lcd_init;
    lcd_device.open = ssd1963_lcd_open;
    lcd_device.close = ssd1963_lcd_close;
    lcd_device.control = ssd1963_lcd_control;
    lcd_device.read = RT_NULL;
    lcd_device.write = RT_NULL;

    lcd_device.user_data = &ssd1963_ops;

#if defined(NU_PKG_SSD1963_WITH_OFFSCREEN_FRAMEBUFFER)
    g_SSD1963Info.framebuffer = rt_malloc_align((g_SSD1963Info.pitch * NU_PKG_SSD1963_LINE_BUFFER_NUMBER) + 32, 32);
    RT_ASSERT(g_SSD1963Info.framebuffer != RT_NULL);
    g_SSD1963Info.smem_len = g_SSD1963Info.pitch * NU_PKG_SSD1963_LINE_BUFFER_NUMBER;
#endif

    /* register graphic device driver */
    rt_device_register(&lcd_device, "lcd", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);

    return 0;
}

#ifdef RT_USING_FINSH
#define LINE_LEN 32
static void lcd_test(int argc, char *argv[])
{
    uint16_t pixels[LINE_LEN];
    uint16_t color;
    int x, y, i;
    x = y = 100;

    ssd1963_lcd_init(NULL);

    color = 0x0; //Black, RGB
    rt_kprintf("Brush 0x%X on screen.\n", color);
    ssd1963_fillscreen(color);
    ssd1963_lcd_get_pixel((char *)&color, x, y);
    rt_kprintf("lcd get pixel, pixel: 0x%X, x: %d, y: %d\n", color, x, y);

    color = 0xffff; //White, RGB
    rt_kprintf("Brush 0x%X on screen.\n", color);
    ssd1963_fillscreen(color);
    ssd1963_lcd_get_pixel((char *)&color, x, y);
    rt_kprintf("lcd get pixel, pixel: 0x%X, x: %d, y: %d\n", color, x, y);

    color = 0x1f; //Blue, RGB
    rt_kprintf("Brush 0x%X on screen.\n", color);
    ssd1963_fillscreen(color);
    ssd1963_lcd_get_pixel((char *)&color, x, y);
    rt_kprintf("lcd get pixel, pixel: 0x%X, x: %d, y: %d\n", color, x, y);

    color = 0x07e0; //Green, RGB
    rt_kprintf("Brush 0x%X on screen.\n", color);
    ssd1963_fillscreen(color);
    ssd1963_lcd_get_pixel((char *)&color, x, y);
    rt_kprintf("lcd get pixel, pixel: 0x%X, x: %d, y: %d\n", color, x, y);

    color = 0xf800; //Red, RGB
    rt_kprintf("Brush 0x%X on screen.\n", color);
    ssd1963_fillscreen(color);
    ssd1963_lcd_get_pixel((char *)&color, x, y);
    rt_kprintf("lcd get pixel, pixel: 0x%X, x: %d, y: %d\n", color, x, y);

    color = 0xffff; //White, RGB
    rt_kprintf("lcd draw hline, pixel: 0x%X, x1: %d, x2: %d, y: %d\n", color, x, x + 20, y);
    ssd1963_lcd_draw_hline((const char *)&color, x, x + 20, y);

    color = 0xffff; //White, RGB
    rt_kprintf("lcd draw vline, pixel: 0x%X, x: %d, y: %d\n", color, y, y + 20);
    ssd1963_lcd_draw_vline((const char *)&color, x, y, y + 20);

    for (i = 0; i < LINE_LEN; i++)
        pixels[i] = 20 + i * 5;

    x = y = 50;
    rt_kprintf("lcd blit line, start: x: %d, y: %d\n", x, y);
    ssd1963_lcd_blit_line((const char *)&pixels[0], x, y, LINE_LEN);

    x = y = 200;
    color = 0x07E0; //Green, RGB
    rt_kprintf("lcd set pixel, pixel: 0x%X, x: %d, y: %d\n", color, x, y);
    ssd1963_lcd_set_pixel((const char *)&color, x, y);
    color = 0x0;
    ssd1963_lcd_get_pixel((char *)&color, x, y);
    rt_kprintf("lcd get pixel, pixel: 0x%X, x: %d, y: %d\n", color, x, y);

    x = y = 200;
    color = 0x1f; //Blue, RGB
    rt_kprintf("lcd set pixel, pixel: 0x%X, x: %d, y: %d\n", color, x, y);
    ssd1963_lcd_set_pixel((const char *)&color, x, y);
    color = 0x0;
    ssd1963_lcd_get_pixel((char *)&color, x, y);
    rt_kprintf("lcd get pixel, pixel: 0x%X, x: %d, y: %d\n", color, x, y);

    x = y = 200;
    color = 0xf800; //Red, RGB
    rt_kprintf("lcd set pixel, pixel: 0x%X, x: %d, y: %d\n", color, x, y);
    ssd1963_lcd_set_pixel((const char *)&color, x, y);
    color = 0x0;
    ssd1963_lcd_get_pixel((char *)&color, x, y);
    rt_kprintf("lcd get pixel, pixel: 0x%X, x: %d, y: %d\n", color, x, y);
}
MSH_CMD_EXPORT(lcd_test, test lcd display);
#endif

#endif /* if defined(NU_PKG_USING_SSD1963) */