summaryrefslogtreecommitdiff
path: root/bsp/renesas/libraries/HAL_Drivers/drv_lcd.c
blob: 944fb66c0d70967b8287c8dda9c9e24073a2204f (plain)
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
27
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
129
130
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
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-11-24     Rbb666       the first version
 */

#include <rtthread.h>

#if (defined(BSP_USING_LCD)) || (defined(SOC_SERIES_R7FA6M3))

#include <lcd_port.h>

#include "r_display_api.h"
#include "hal_data.h"

//#define DRV_DEBUG
//#define LOG_TAG             "drv.lcd"
//#include <drv_log.h>

struct drv_lcd_device
{
    struct rt_device parent;
    struct rt_device_graphic_info lcd_info;

    void *framebuffer;
};

struct drv_lcd_device _lcd;

uint16_t screen_rotation;
uint16_t *lcd_current_working_buffer = (uint16_t *)&fb_background[0];

void turn_on_lcd_backlight(void)
{
    rt_pin_mode(LCD_BL_PIN, PIN_MODE_OUTPUT);   /* LCD_BL */
    rt_pin_write(LCD_BL_PIN, PIN_HIGH);
}

void ra_bsp_lcd_init(void)
{
    fsp_err_t error;
    // Set screen rotation to default view
    screen_rotation = ROTATION_ZERO;

    /**  Display driver open */
    error = R_GLCDC_Open(&g_display0_ctrl, &g_display0_cfg);
    if (FSP_SUCCESS == error)
    {
        /**  Display driver start */
        error = R_GLCDC_Start(&g_display0_ctrl);
    }
}

void ra_bsp_lcd_set_display_buffer(uint8_t index)
{
    R_GLCDC_BufferChange(&g_display0_ctrl, fb_background[index - 1], DISPLAY_FRAME_LAYER_1);
}

void ra_bsp_lcd_set_working_buffer(uint8_t index)
{
    if (index >= 1 && index <= 2)
    {
        lcd_current_working_buffer = (uint16_t *)fb_background[index - 1];
    }
}

void ra_bsp_lcd_enable_double_buffer(void)
{
    ra_bsp_lcd_set_display_buffer(1);
    ra_bsp_lcd_set_working_buffer(2);
}

void ra_bsp_lcd_clear(uint16_t color)
{
    for (uint32_t i = 0; i < (LCD_WIDTH * LCD_HEIGHT); i++)
    {
        lcd_current_working_buffer[i] = color;
    }
}

void ra_bsp_lcd_swap_buffer(void)
{
    if (lcd_current_working_buffer == (uint16_t *)fb_background[0])
    {
        ra_bsp_lcd_set_display_buffer(1);
        ra_bsp_lcd_set_working_buffer(2);
    }
    else
    {
        ra_bsp_lcd_set_display_buffer(2);
        ra_bsp_lcd_set_working_buffer(1);
    }
}

void bsp_lcd_draw_pixel(uint32_t x, uint32_t y, uint16_t color)
{
    // Verify pixel is within LCD range
    if ((x < LCD_WIDTH) && (y < LCD_HEIGHT))
    {
        switch (screen_rotation)
        {
        case ROTATION_ZERO:
        {
            lcd_current_working_buffer[(y * LCD_WIDTH) + x] = color;
            break;
        }
        case ROTATION_180:
        {
            lcd_current_working_buffer[((LCD_HEIGHT - y) * LCD_WIDTH) + (LCD_WIDTH - x)] = color;
            break;
        }
        default:
        {
            lcd_current_working_buffer[(y * LCD_WIDTH) + x] = color;
            break;
        }
        }
    }
    else
    {
        rt_kprintf("draw pixel outof range:%d,%d\n", x, y);
    }
}

void lcd_fill_array(uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end, void *pcolor)
{
    uint16_t *pixel = RT_NULL;
    uint16_t cycle_y, x_offset = 0;

    pixel = (uint16_t *)pcolor;

    for (cycle_y = y_start; cycle_y <= y_end;)
    {
        for (x_offset = 0; x_start + x_offset <= x_end; x_offset++)
        {
            bsp_lcd_draw_pixel(x_start + x_offset, cycle_y, *pixel++);
        }
        cycle_y++;
    }
}

static rt_err_t ra_lcd_init(rt_device_t device)
{
    RT_ASSERT(device != RT_NULL);

    ra_bsp_lcd_init();

    return RT_EOK;
}

static rt_err_t ra_lcd_control(rt_device_t device, int cmd, void *args)
{
    struct drv_lcd_device *lcd = (struct drv_lcd_device *)device;

    switch (cmd)
    {
    case RTGRAPHIC_CTRL_RECT_UPDATE:
    {
        struct rt_device_rect_info *info = (struct rt_device_rect_info *)args;

        (void)info; /* nothing, right now */
        rt_kprintf("update screen...\n");
    }
    break;

    case RTGRAPHIC_CTRL_POWERON:
        rt_pin_write(LCD_BL_PIN, PIN_HIGH);
        break;

    case RTGRAPHIC_CTRL_POWEROFF:
        rt_pin_write(LCD_BL_PIN, PIN_LOW);
        break;

    case RTGRAPHIC_CTRL_GET_INFO:
    {
        struct rt_device_graphic_info *info = (struct rt_device_graphic_info *)args;

        RT_ASSERT(info != RT_NULL);
        info->pixel_format  = lcd->lcd_info.pixel_format;
        info->bits_per_pixel = 16;
        info->width         = lcd->lcd_info.width;
        info->height        = lcd->lcd_info.height;
        info->framebuffer   = lcd->lcd_info.framebuffer;
    }
    break;

    case RTGRAPHIC_CTRL_SET_MODE:
        break;
    }

    return RT_EOK;
}

int rt_hw_lcd_init(void)
{
    rt_err_t result = RT_EOK;
    struct rt_device *device = &_lcd.parent;

    /* memset _lcd to zero */
    memset(&_lcd, 0x00, sizeof(_lcd));

    /* config LCD dev info */
    _lcd.lcd_info.height = LCD_HEIGHT;
    _lcd.lcd_info.width = LCD_WIDTH;
    _lcd.lcd_info.bits_per_pixel = LCD_BITS_PER_PIXEL;
    _lcd.lcd_info.pixel_format = LCD_PIXEL_FORMAT;

    _lcd.lcd_info.framebuffer = (uint8_t *)&fb_background[0];

    device->type    = RT_Device_Class_Graphic;
#ifdef RT_USING_DEVICE_OPS
    device->ops     = &lcd_ops;
#else
    device->init    = ra_lcd_init;
    device->control = ra_lcd_control;
#endif

    /* register lcd device */
    rt_device_register(device, "lcd", RT_DEVICE_FLAG_RDWR);

    turn_on_lcd_backlight();

    // Initialize lcd controller
    ra_lcd_init(device);

    ra_bsp_lcd_set_display_buffer(1);

    screen_rotation = ROTATION_ZERO;

    return result;
}

INIT_DEVICE_EXPORT(rt_hw_lcd_init);

int lcd_test()
{
    struct drv_lcd_device *lcd;
    lcd = (struct drv_lcd_device *)rt_device_find("lcd");

    while (1)
    {
        /* red */
        for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
        {
            lcd->lcd_info.framebuffer[2 * i] = 0x00;
            lcd->lcd_info.framebuffer[2 * i + 1] = 0xF8;
        }
        lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL);
        rt_thread_mdelay(1000);
        /* green */
        for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
        {
            lcd->lcd_info.framebuffer[2 * i] = 0xE0;
            lcd->lcd_info.framebuffer[2 * i + 1] = 0x07;
        }
        lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL);
        rt_thread_mdelay(1000);
        /* blue */
        for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
        {
            lcd->lcd_info.framebuffer[2 * i] = 0x1F;
            lcd->lcd_info.framebuffer[2 * i + 1] = 0x00;
        }
        lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL);
        rt_thread_mdelay(1000);
    }
}
MSH_CMD_EXPORT(lcd_test, lcd_test);

#endif /* BSP_USING_LCD */