summaryrefslogtreecommitdiff
path: root/bsp/phytium/libraries/drivers/drv_spi.c
blob: 2b1d2130b838c97d5cad8e766efce12ec86cfc60 (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
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
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Email: [email protected]
 *
 * Change Logs:
 * Date        Author       Notes
 * 2022-11-10  liqiaozhong  first commit
 * 2023-03-08  liqiaozhong  support 4 spis and qspi working together
 *
 */
#include <rtthread.h>
#include <rtdevice.h>
#include "interrupt.h"

#include <string.h>
#include "fdebug.h"

#if defined(TARGET_E2000)
#include "fparameters.h"
#endif

#include "fcpu_info.h"
#include "fkernel.h"
#include "ftypes.h"
#include "fsleep.h"

#ifdef RT_USING_SPI

#include <dfs_file.h>

#include "fspim.h"
#include "fspim_hw.h" /* include low-level header file for internal probe */
#include "drv_spi.h"
/************************** Constant Definitions *****************************/
/**************************** Type Definitions *******************************/
/************************** Variable Definitions *****************************/
#ifdef RT_USING_SPIM0
    static struct drv_spi _RTSpim0;
#endif

#ifdef RT_USING_SPIM1
    static struct drv_spi _RTSpim1;
#endif

#ifdef RT_USING_SPIM2
    static struct drv_spi _RTSpim2;
#endif

#ifdef RT_USING_SPIM3
    static struct drv_spi _RTSpim3;
#endif

static struct rt_spi_device *spi_device = RT_NULL;
static struct rt_event rx_done_event;
/***************** Macros (Inline Functions) Definitions *********************/
#define FSPIM_DEBUG_TAG "SPIM"
#define FSPIM_ERROR(format, ...)   FT_DEBUG_PRINT_E(FSPIM_DEBUG_TAG, format, ##__VA_ARGS__)
#define FSPIM_WARN(format, ...)   FT_DEBUG_PRINT_W(FSPIM_DEBUG_TAG, format, ##__VA_ARGS__)
#define FSPIM_INFO(format, ...)    FT_DEBUG_PRINT_I(FSPIM_DEBUG_TAG, format, ##__VA_ARGS__)
#define FSPIM_DEBUG(format, ...)   FT_DEBUG_PRINT_D(FSPIM_DEBUG_TAG, format, ##__VA_ARGS__)

#define EVENT_RX_DONE (1 << 1)
/*******************************Api Functions*********************************/
static rt_err_t spim_configure(struct rt_spi_device* device, struct rt_spi_configuration* configuration);
static rt_uint32_t spim_xfer(struct rt_spi_device* device, struct rt_spi_message* message);

static FError FSpimSetupInterrupt(FSpim *instance_p)
{
    FASSERT(instance_p);
    FSpimConfig *config_p = &instance_p->config;
    uintptr base_addr = config_p->base_addr;
    u32 cpu_id = 0;

    GetCpuId(&cpu_id);
    FSPIM_DEBUG("cpu_id is %d, irq_num is %d\n", cpu_id, config_p->irq_num);
    config_p->irq_prority = 0xd0;
    rt_hw_interrupt_set_target_cpus(config_p->irq_num, cpu_id);
    rt_hw_interrupt_set_priority(config_p->irq_num, config_p->irq_prority);

    /* register intr callback */
    rt_hw_interrupt_install(config_p->irq_num,
                    FSpimInterruptHandler,
                    instance_p,
                    NULL);

    /* enable tx fifo overflow / rx overflow / rx full */
    FSpimMaskIrq(base_addr, FSPIM_IMR_ALL_BITS);

    /* enable irq */
    rt_hw_interrupt_umask(config_p->irq_num);

    return FSPIM_SUCCESS;
}

static void rt_ft_send_event_done(void *instance_p, void *param)
{
    FASSERT(instance_p);
    rt_event_send(&rx_done_event, EVENT_RX_DONE);
    return;
}

static const struct rt_spi_ops spim_ops =
{
    .configure = spim_configure,
    .xfer = spim_xfer
};

static rt_err_t spim_configure(struct rt_spi_device *device,
                              struct rt_spi_configuration *configuration)
{
    FError ret = FSPIM_SUCCESS;
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(configuration != RT_NULL);
    struct drv_spi *user_data_cfg = device->parent.user_data;
    FSpimConfig input_cfg = *FSpimLookupConfig(user_data_cfg->spi_id);
#ifdef RT_USING_SMART
    input_cfg.base_addr = (uintptr)rt_ioremap((void*)input_cfg.base_addr, 0x1000);
#endif
    FSpimConfig *set_input_cfg = &input_cfg;

    /* set fspim device according to configuration */
    if (configuration->mode & RT_SPI_CPOL)
    {
        set_input_cfg->cpol = FSPIM_CPOL_HIGH;
    }
    else
    {
        set_input_cfg->cpol = FSPIM_CPOL_LOW;
    }
    if (configuration->mode & RT_SPI_CPHA)
    {
        set_input_cfg->cpha = FSPIM_CPHA_2_EDGE;
    }
    else
    {
        set_input_cfg->cpha = FSPIM_CPHA_1_EDGE;
    }

    if (configuration->data_width == 8)
    {
        set_input_cfg->n_bytes = FSPIM_1_BYTE;
    }
    else if (configuration->data_width == 16)
    {
        set_input_cfg->n_bytes = FSPIM_2_BYTE;
    }

    /* send spi_cfg to RT-Thread sys */
    ret = FSpimCfgInitialize(&user_data_cfg->spim_instance, &input_cfg);
    if (FSPIM_SUCCESS != ret)
        return RT_ERROR;

    /* irq setting */
    ret = FSpimSetupInterrupt(&user_data_cfg->spim_instance);
    if (FSPIM_SUCCESS != ret)
        return RT_ERROR;
    FSpimRegisterIntrruptHandler(&user_data_cfg->spim_instance, FSPIM_INTR_EVT_RX_DONE, rt_ft_send_event_done, NULL);

    return ret;
}

static rt_uint32_t spim_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
{
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(device->parent.user_data != RT_NULL);
    RT_ASSERT(message != RT_NULL);
    rt_size_t message_length;
    rt_uint8_t *recv_buf;
    const rt_uint8_t *send_buf;

    /* recv spi_cfg from RT-Thread sys */
    struct drv_spi *user_data_xfer = device->parent.user_data;
    FSpim *xfer_spim_instance = &user_data_xfer->spim_instance;

    FError tx_rx_result = FSPIM_SUCCESS;
    message_length = message->length;
    recv_buf = message->recv_buf;
    send_buf = message->send_buf;

    if (message->cs_take)
    {
        FSpimSetChipSelection(xfer_spim_instance, TRUE);
    }

    if (message_length > 0)
    {
        if (send_buf == RT_NULL && recv_buf != RT_NULL)
        {
            /* receive message */
            tx_rx_result = FSpimTransferByInterrupt(xfer_spim_instance, RT_NULL, recv_buf, message_length);
        }
        else if (send_buf != RT_NULL && recv_buf == RT_NULL)
        {
            /* send message */
            tx_rx_result = FSpimTransferByInterrupt(xfer_spim_instance, send_buf, RT_NULL, message_length);
        }
        else if (send_buf != RT_NULL && recv_buf != RT_NULL)
        {
            /* not supported yet */
            rt_kprintf("Do not support the situation that send_buf and recv_buf both not equal to 0.");
        }
    }

    if (FSPIM_SUCCESS != tx_rx_result)
    {
        rt_kprintf("FSpimTransferByInterrupt() fail!!!");
        message_length = 0;
    }

    if (rt_event_recv(&rx_done_event, (EVENT_RX_DONE),
                      (RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR),
                      RT_WAITING_FOREVER, RT_NULL) != RT_EOK)
    {
        rt_kprintf("Wait rx timeout!!!\n");
        message_length = 0;
    }

    if (message->cs_release)
    {
        FSpimSetChipSelection(xfer_spim_instance, FALSE);
    }

    return message_length;
}

int ft_spi_init(void)
{
    rt_err_t result;
    static struct rt_spi_bus spim_bus;

    /* event creat */
    if (RT_EOK != rt_event_init(&rx_done_event, "rx_done_event", RT_IPC_FLAG_FIFO))
    {
        rt_kprintf("Create event failed.\n");
        return RT_ERROR;
    }

    /* spi bus init */
    result = rt_spi_bus_register(&spim_bus, "spi0", &spim_ops);
    RT_ASSERT((struct rt_spi_device *)rt_device_find("spi0"));
    rt_kprintf("Spi bus spi0 init\n");

    /* spi device init and attach to bus */
    #ifdef RT_USING_SPIM0
        _RTSpim0.spi_id = FSPI0_ID;
        result = rt_spi_bus_attach_device(&_RTSpim0.device, "spi00", "spi0", &_RTSpim0);
        spi_device = (struct rt_spi_device *)rt_device_find("spi00");
        if (RT_NULL == spi_device)
        {
            rt_kprintf("Spi init failed -> can't find spi00 device!\n");
            return RT_ERROR;
        }
        rt_kprintf("Spi master device spi00 init.\n");
    #endif

    #ifdef RT_USING_SPIM1
        _RTSpim1.spi_id = FSPI1_ID;
        result = rt_spi_bus_attach_device(&_RTSpim1.device, "spi01", "spi0", &_RTSpim1);
        spi_device = (struct rt_spi_device *)rt_device_find("spi01");
        if (RT_NULL == spi_device)
        {
            rt_kprintf("Spi init failed -> can't find spi01 device!\n");
            return RT_ERROR;
        }
        rt_kprintf("Spi master device spi01 init.\n");
    #endif

    #ifdef RT_USING_SPIM2
        _RTSpim2.spi_id = FSPI2_ID;
        result = rt_spi_bus_attach_device(&_RTSpim2.device, "spi02", "spi0", &_RTSpim2);
        spi_device = (struct rt_spi_device *)rt_device_find("spi02");
        if (RT_NULL == spi_device)
        {
            rt_kprintf("Spi init failed -> can't find spi02 device!\n");
            return RT_ERROR;
        }
        rt_kprintf("Spi master device spi02 init.\n");
    #endif

    #ifdef RT_USING_SPIM3
        _RTSpim3.spi_id = FSPI3_ID;
        result = rt_spi_bus_attach_device(&_RTSpim3.device, "spi03", "spi0", &_RTSpim3);
        spi_device = (struct rt_spi_device *)rt_device_find("spi03");
        if (RT_NULL == spi_device)
        {
            rt_kprintf("Spi init failed -> can't find spi03 device!\n");
            return RT_ERROR;
        }
        rt_kprintf("Spi master device spi03 init.\n");
    #endif


    return result;
}
INIT_DEVICE_EXPORT(ft_spi_init);

/* spi test example */
static void fspim_test_sample(int argc, char *argv[])
{
    rt_uint8_t send_to_flash_id = 0x9f; /* Flash cmd */
    rt_uint8_t recv_from_falsh_id1[5] = {0};
    rt_uint8_t recv_from_falsh_id2[5] = {0};

    /* find the spi device to get the device handle */
    spi_device = (struct rt_spi_device *)rt_device_find("spi02");
    if (!spi_device)
    {
        rt_kprintf("fspim_test_sample run failed! can't find spi02 device!\n");
    }
    else
    {
        static struct rt_spi_message msg1, msg2;

        msg1.send_buf   = &send_to_flash_id;
        msg1.recv_buf   = RT_NULL;
        msg1.length     = 1;
        msg1.cs_take    = 1;
        msg1.cs_release = 0;
        msg1.next       = &msg2;

        msg2.send_buf   = RT_NULL;
        msg2.recv_buf   = recv_from_falsh_id2;
        msg2.length     = 5;
        msg2.cs_take    = 0;
        msg2.cs_release = 1;
        msg2.next       = RT_NULL;

        /* send the command to read the ID using rt_spi_send_then_recv() */
        rt_spi_send_then_recv(spi_device, &send_to_flash_id, 1, recv_from_falsh_id1, 5);
        rt_kprintf("use rt_spi_send_then_recv() read flash ID is:0x%x 0x%x 0x%x 0x%x 0x%x\n", recv_from_falsh_id1[0], recv_from_falsh_id1[1], recv_from_falsh_id1[2], recv_from_falsh_id1[3], recv_from_falsh_id1[4]);

        /* send the command to read the ID using rt_spi_transfer_message() */
        rt_spi_transfer_message(spi_device, &msg1);
        rt_kprintf("use rt_spi_transfer_message() read flash ID is:0x%x 0x%x 0x%x 0x%x 0x%x\n", recv_from_falsh_id2[0], recv_from_falsh_id2[1], recv_from_falsh_id2[2], recv_from_falsh_id2[3], recv_from_falsh_id2[4]);
    }
}
MSH_CMD_EXPORT(fspim_test_sample, "fspim test sample");
#endif