summaryrefslogtreecommitdiff
path: root/bsp/nuvoton/libraries/nu_packages/ILI9341/ili9341_spi.c
blob: 08c48a2a8ea2e70bc1ae28219ed9079ac6c2f7b6 (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
/**************************************************************************//**
*
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date            Author       Notes
* 2020-1-16       Wayne        First version
*
******************************************************************************/

#include <rtconfig.h>

#if defined(NU_PKG_USING_ILI9341_SPI)

#include <rtdevice.h>
#include <lcd_ili9341.h>

#if !defined(NU_PKG_USING_ILI9341_SPI_CLK_FREQ)
    #define NU_PKG_USING_ILI9341_SPI_CLK_FREQ   48000000
#endif

static struct rt_spi_device ili9341_spi_device;
static struct rt_spi_configuration ili9341_cfg =
{
    .mode = RT_SPI_MODE_0 | RT_SPI_MSB,
    .data_width = 8,
    .max_hz = NU_PKG_USING_ILI9341_SPI_CLK_FREQ,
};

static void ili9341_change_datawidth(int data_width)
{
    if (ili9341_cfg.data_width != data_width)
    {
        ili9341_cfg.data_width = data_width;
        rt_spi_configure(&ili9341_spi_device, &ili9341_cfg);
    }
}

void ili9341_send_cmd(rt_uint8_t cmd)
{
    ili9341_change_datawidth(8);
    CLR_RS;
    rt_spi_transfer(&ili9341_spi_device, (const void *)&cmd, NULL, 1);
    SET_RS;
}

void ili9341_send_cmd_parameter(uint8_t data)
{
    ili9341_change_datawidth(8);
    rt_spi_transfer(&ili9341_spi_device, (const void *)&data, NULL, 1);
}

static void ili9341_write_data_16bit(uint16_t data)
{
    ili9341_change_datawidth(16);
    rt_spi_transfer(&ili9341_spi_device, (const void *)&data, NULL, 2);
}

void ili9341_send_pixel_data(rt_uint16_t pixel)
{
    ili9341_write_data_16bit(pixel);
}

void ili9341_send_pixels(rt_uint16_t *pixels, int len)
{
    ili9341_change_datawidth(16);
    rt_spi_transfer(&ili9341_spi_device, (const void *)pixels, NULL, len);
}

static rt_err_t ili9341_spi_send_then_recv(struct rt_spi_device *device,
        const void           *send_buf,
        rt_size_t             send_length,
        void                 *recv_buf,
        rt_size_t             recv_length)
{
    rt_err_t result;
    struct rt_spi_message message;

    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(device->bus != RT_NULL);

    ili9341_change_datawidth(8);
    result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
    if (result == RT_EOK)
    {
        if (device->bus->owner != device)
        {
            /* not the same owner as current, re-configure SPI bus */
            result = device->bus->ops->configure(device, &device->config);
            if (result == RT_EOK)
            {
                /* set SPI bus owner */
                device->bus->owner = device;
            }
            else
            {
                /* configure SPI bus failed */
                result = -RT_EIO;
                goto __exit;
            }
        }

        /* send data */
        message.send_buf   = send_buf;
        message.recv_buf   = RT_NULL;
        message.length     = send_length;
        message.cs_take    = 1;
        message.cs_release = 0;
        message.next       = RT_NULL;

        CLR_RS;
        result = device->bus->ops->xfer(device, &message);
        SET_RS;
        if (result == 0)
        {
            result = -RT_EIO;
            goto __exit;
        }

        /* recv data */
        message.send_buf   = RT_NULL;
        message.recv_buf   = recv_buf;
        message.length     = recv_length;
        message.cs_take    = 0;
        message.cs_release = 1;
        message.next       = RT_NULL;

        result = device->bus->ops->xfer(device, &message);
        if (result == 0)
        {
            result = -RT_EIO;
            goto __exit;
        }

        result = RT_EOK;
    }
    else
    {
        return -RT_EIO;
    }

__exit:

    rt_mutex_release(&(device->bus->lock));
    return result;
}

void ili9341_set_column(uint16_t StartCol, uint16_t EndCol)
{
    ili9341_send_cmd(0x2A);
    ili9341_write_data_16bit(StartCol);
    ili9341_write_data_16bit(EndCol);
}

void ili9341_set_page(uint16_t StartPage, uint16_t EndPage)
{
    ili9341_send_cmd(0x2B);
    ili9341_write_data_16bit(StartPage);
    ili9341_write_data_16bit(EndPage);
}

void ili9341_lcd_get_pixel(char *color, int x, int y)
{
    uint8_t cmd;
    typedef union
    {
        rt_uint32_t rgbx;
        struct
        {
            rt_uint8_t x;
            rt_uint8_t r;
            rt_uint8_t g;
            rt_uint8_t b;
        } S;
    } ili9341_pixel;
    ili9341_pixel bgrx;

    if (x >= XSIZE_PHYS || y >= YSIZE_PHYS)
    {
        *(rt_uint16_t *)color = 0;
        return;
    }

    ili9341_set_column(x, x);
    ili9341_set_page(y, y);

    cmd  = 0x2E;
    ili9341_spi_send_then_recv(&ili9341_spi_device, &cmd, 1, &bgrx, 4);
    //rt_kprintf("%08x.\n", bgrx);

    // To RGB565
    *(rt_uint16_t *)color = ((bgrx.S.r >> 3) << 11) | ((bgrx.S.g >> 2) << 5) | (bgrx.S.b >> 3);
}

rt_err_t rt_hw_lcd_ili9341_spi_init(const char *spibusname, void *pvUserData)
{
    if (rt_spi_bus_attach_device(&ili9341_spi_device, "lcd_ili9341", spibusname, pvUserData) != RT_EOK)
        return -RT_ERROR;

    if (pvUserData != RT_NULL)
    {
        // GPIO CS pin mode to output */
        rt_pin_mode(*((rt_base_t *)pvUserData), PIN_MODE_OUTPUT);
    }

    rt_kprintf("Preferred ili9341 spi clock frequency is %d Hz.\n", NU_PKG_USING_ILI9341_SPI_CLK_FREQ);

    return rt_spi_configure(&ili9341_spi_device, &ili9341_cfg);
}

#endif /* #if defined(NU_PKG_USING_ILI9341_SPI) */