summaryrefslogtreecommitdiff
path: root/bsp/Infineon/libraries/HAL_Drivers/drv_spi.c
blob: eee1c01d21aa3f7f9a346d18a40219f192453973 (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
/*
 * Copyright (c) 2006-2024 RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-07-18     Rbb666       first version
 * 2023-03-30     Rbb666       update spi driver
 * 2025-04-27     Hydevcode    update spi driver
 */

#include <drv_spi.h>

#ifdef RT_USING_SPI

/*#define DRV_DEBUG*/
#define DBG_TAG              "drv.spi"
#ifdef DRV_DEBUG
    #define DBG_LVL               DBG_LOG
#else
    #define DBG_LVL               DBG_INFO
#endif /* DRV_DEBUG */
#include <rtdbg.h>

#ifdef BSP_USING_SPI0
    static struct rt_spi_bus spi_bus0;
#endif
#ifdef BSP_USING_SPI3
    static struct rt_spi_bus spi_bus3;
#endif
#ifdef BSP_USING_SPI5
    static struct rt_spi_bus spi_bus5;
#endif
#ifdef BSP_USING_SPI6
    static struct rt_spi_bus spi_bus6;
#endif

static struct ifx_spi_handle spi_bus_obj[] =
{
#if defined(BSP_USING_SPI0)
#if defined(SOC_XMC7100D_F144K4160AA)
    {
        .bus_name = "spi0",
        .sck_pin = GET_PIN(0, 2),
        .miso_pin = GET_PIN(1, 0),
        .mosi_pin = GET_PIN(1, 1),
    },
#else
    {
        .bus_name = "spi0",
        .sck_pin = GET_PIN(0, 4),
        .miso_pin = GET_PIN(0, 3),
        .mosi_pin = GET_PIN(0, 2),
    },
#endif
#endif
#if defined(BSP_USING_SPI3)
    {
        .bus_name = "spi3",
        .sck_pin = GET_PIN(6, 2),
        .miso_pin = GET_PIN(6, 1),
        .mosi_pin = GET_PIN(6, 0),
    },
#endif
#if defined(BSP_USING_SPI5)
    {
        .bus_name = "spi5",
        .sck_pin = GET_PIN(7, 2),
        .miso_pin = GET_PIN(7, 0),
        .mosi_pin = GET_PIN(7, 1),
    },
#endif
#if defined(BSP_USING_SPI6)
    {
        .bus_name = "spi6",
        .sck_pin = GET_PIN(12, 2),
        .miso_pin = GET_PIN(12, 1),
        .mosi_pin = GET_PIN(12, 0),
    },
#endif
};

static struct ifx_spi spi_config[sizeof(spi_bus_obj) / sizeof(spi_bus_obj[0])] =
{0};

/* private rt-thread spi ops function */
static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message);

static struct rt_spi_ops ifx_spi_ops =
{
    .configure = spi_configure,
    .xfer = spixfer,
};

static void spi_interrupt_callback(void *arg, cyhal_spi_event_t event)
{
    struct ifx_spi *spi_drv = (struct ifx_spi *)arg;

    rt_interrupt_enter();

    if ((event & CYHAL_SPI_IRQ_DONE) != 0u)
    {
        /* Transmission is complete. Handle Event */
        rt_completion_done(&spi_drv->cpt);
    }

    rt_interrupt_leave();
}

static void ifx_spi_init(struct ifx_spi *spi_device)
{
    RT_ASSERT(spi_device != RT_NULL);

    rt_err_t result = RT_EOK;

    static uint8_t init_flag = 1;

    if (init_flag)
    {
        result = cyhal_spi_init(spi_device->spi_handle_t->spi_obj, spi_device->spi_handle_t->mosi_pin, spi_device->spi_handle_t->miso_pin,
                                spi_device->spi_handle_t->sck_pin, NC, NULL, spi_device->spi_handle_t->spi_obj->data_bits,
                                spi_device->spi_handle_t->spi_obj->mode, false);
        if (result != RT_EOK)
        {
            LOG_E("spi%s init fail", spi_device->spi_handle_t->bus_name);
            return;
        }

        result = cyhal_spi_set_frequency(spi_device->spi_handle_t->spi_obj, spi_device->spi_handle_t->freq);
        if (result == CYHAL_SPI_RSLT_CLOCK_ERROR)
        {
            LOG_E("%s set frequency fail", spi_device->spi_handle_t->bus_name);
            return;
        }
        LOG_I("[%s] freq:[%d]HZ\n", spi_device->spi_handle_t->bus_name, spi_device->spi_handle_t->freq);

        /* Register a callback function to be called when the interrupt fires */
        cyhal_spi_register_callback(spi_device->spi_handle_t->spi_obj, spi_interrupt_callback, spi_device);

        /* Enable the events that will trigger the call back function */
        cyhal_spi_enable_event(spi_device->spi_handle_t->spi_obj, CYHAL_SPI_IRQ_DONE, 4, true);
    }

    init_flag = 0;
}

static rt_err_t spi_configure(struct rt_spi_device *device,
                              struct rt_spi_configuration *configuration)
{
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(configuration != RT_NULL);

    struct ifx_spi *spi_device = rt_container_of(device->bus, struct ifx_spi, spi_bus);

    /* data_width */
    if (configuration->data_width <= 8)
    {
        spi_device->spi_handle_t->spi_obj->data_bits = 8;
    }
    else if (configuration->data_width <= 16)
    {
        spi_device->spi_handle_t->spi_obj->data_bits = 16;
    }
    else
    {
        return -RT_EIO;
    }

    uint32_t max_hz;
    max_hz = configuration->max_hz;
    spi_device->spi_handle_t->freq = max_hz;

    /* MSB or LSB */
    switch (configuration->mode & RT_SPI_MODE_3)
    {
    case RT_SPI_MODE_0:
        spi_device->spi_handle_t->spi_obj->mode = CYHAL_SPI_MODE_00_MSB;
        break;

    case RT_SPI_MODE_1:
        spi_device->spi_handle_t->spi_obj->mode = CYHAL_SPI_MODE_01_MSB;
        break;

    case RT_SPI_MODE_2:
        spi_device->spi_handle_t->spi_obj->mode = CYHAL_SPI_MODE_10_MSB;
        break;

    case RT_SPI_MODE_3:
        spi_device->spi_handle_t->spi_obj->mode = CYHAL_SPI_MODE_11_MSB;
        break;
    }

    ifx_spi_init(spi_device);

    return RT_EOK;
}

static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
{
    RT_ASSERT(device != NULL);
    RT_ASSERT(message != NULL);

    struct ifx_spi *spi_device = rt_container_of(device->bus, struct ifx_spi, spi_bus);

    /* take CS */
    if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
    {
        if (device->config.mode & RT_SPI_CS_HIGH)
        {
            cyhal_gpio_write(device->cs_pin, PIN_HIGH);
        }
        else
        {
            cyhal_gpio_write(device->cs_pin, PIN_LOW);
        }
        LOG_D("spi take cs\n");
    }

    int result = RT_EOK;

    if (message->length > 0)
    {
        if (message->send_buf == RT_NULL && message->recv_buf != RT_NULL)
        {
            /**< receive message */
            result = cyhal_spi_transfer(spi_device->spi_handle_t->spi_obj, RT_NULL, 0x00, message->recv_buf, message->length, 0x00);
        }
        else if (message->send_buf != RT_NULL && message->recv_buf == RT_NULL)
        {
            /**< send message */
            result = cyhal_spi_transfer(spi_device->spi_handle_t->spi_obj, message->send_buf, message->length, RT_NULL, 0x00, 0x00);
        }
        else if (message->send_buf != RT_NULL && message->recv_buf != RT_NULL)
        {
            /**< send and receive message */
            result = cyhal_spi_transfer(spi_device->spi_handle_t->spi_obj, message->send_buf, message->length, message->recv_buf, message->length, 0x00);
        }

        /* blocking the thread,and the other tasks can run */
        rt_completion_wait(&spi_device->cpt, RT_WAITING_FOREVER);
    }

    if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
    {
        if (device->config.mode & RT_SPI_CS_HIGH)
            cyhal_gpio_write(device->cs_pin, PIN_LOW);
        else
            cyhal_gpio_write(device->cs_pin, PIN_HIGH);
    }

    return message->length;
}

/**
  * Attach the spi device to SPI bus, this function must be used after initialization.
  */
rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin)
{
    RT_ASSERT(bus_name != RT_NULL);
    RT_ASSERT(device_name != RT_NULL);

    rt_err_t result;
    struct rt_spi_device *spi_device;

    /* attach the device to spi bus*/
    spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
    RT_ASSERT(spi_device != RT_NULL);

    result = rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, cs_pin, RT_NULL);
    if (result != RT_EOK)
    {
        LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
    }

    RT_ASSERT(result == RT_EOK);

    LOG_D("%s attach to %s done", device_name, bus_name);

    return result;
}

int rt_hw_spi_init(void)
{
    int result = RT_EOK;

    for (int spi_index = 0; spi_index < sizeof(spi_bus_obj) / sizeof(spi_bus_obj[0]); spi_index++)
    {
        spi_bus_obj[spi_index].spi_obj = rt_malloc(sizeof(cyhal_spi_t));
        RT_ASSERT(spi_bus_obj[spi_index].spi_obj != RT_NULL);

        spi_config[spi_index].spi_handle_t = &spi_bus_obj[spi_index];

        rt_err_t err = rt_spi_bus_register(&spi_config[spi_index].spi_bus, spi_bus_obj[spi_index].bus_name, &ifx_spi_ops);
        if (RT_EOK != err)
        {
            LOG_E("%s bus register failed.", spi_config[spi_index].spi_handle_t->bus_name);
            return -RT_ERROR;
        }

        LOG_D("MOSI PIN:[%d], MISO PIN[%d], CLK PIN[%d]\n",
              spi_bus_obj[spi_index].mosi_pin, spi_bus_obj[spi_index].miso_pin,
              spi_bus_obj[spi_index].sck_pin);

        /* initialize completion object */
        rt_completion_init(&spi_config[spi_index].cpt);
    }

    return result;
}
INIT_BOARD_EXPORT(rt_hw_spi_init);
#endif /* RT_USING_SPI */