summaryrefslogtreecommitdiff
path: root/bsp/n32g452xx/Libraries/rt_drivers/drv_spi.c
blob: 8a345fd90eca5e798bc5b6803b6b9e823dbdf265 (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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author            Notes
 * 2022-03-06     BalanceTWK        first version
 * 2022-04-16     wolfJane          fix spixfer, add time out check
 */

#include <board.h>
#include <rtthread.h>
#include <rtdevice.h>

#ifdef RT_USING_SPI
#ifdef BSP_USING_SPI

#if defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2) || defined(BSP_USING_SPI3) || defined(BSP_USING_SPI4) || defined(BSP_USING_SPI5) || defined(BSP_USING_SPI6)

#include "drv_spi.h"
#define DRV_DEBUG
#define LOG_TAG              "drv.spi"
#include <drv_log.h>

#define SPI_TIME_OUT    (1000)

enum
{
#ifdef BSP_USING_SPI1
    SPI1_INDEX,
#endif
#ifdef BSP_USING_SPI2
    SPI2_INDEX,
#endif
#ifdef BSP_USING_SPI3
    SPI3_INDEX,
#endif
#ifdef BSP_USING_SPI4
    SPI4_INDEX,
#endif
#ifdef BSP_USING_SPI5
    SPI5_INDEX,
#endif
#ifdef BSP_USING_SPI6
    SPI6_INDEX,
#endif
};

struct n32_spi_config
{
    SPI_Module *module;
    char *bus_name;
};
/* n32 spi dirver class */
struct n32_spi
{
    SPI_InitType SPI_InitStructure;
    struct n32_spi_config *config;
    struct rt_spi_configuration *cfg;

    struct rt_spi_bus spi_bus;
};

static struct n32_spi_config spi_config[] =
{
#ifdef BSP_USING_SPI1
    {
        .module = SPI1,
        .bus_name = "spi1",
    },
#endif

#ifdef BSP_USING_SPI2
    {
        .module = SPI2,
        .bus_name = "spi2",
    },
#endif

#ifdef BSP_USING_SPI3
    {
        .module = SPI3,
        .bus_name = "spi3",
    },
#endif

};

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

static rt_err_t n32_spi_init(struct n32_spi *spi_drv, struct rt_spi_configuration *cfg)
{
    RT_ASSERT(spi_drv != RT_NULL);
    RT_ASSERT(cfg != RT_NULL);

    SPI_InitType *SPI_InitStructure = &spi_drv->SPI_InitStructure;
    SPI_Module *spi_handle = spi_drv->config->module;

    /* GPIO configuration ------------------------------------------------------*/
    n32_msp_spi_init(spi_drv->config->module);

    if (cfg->mode & RT_SPI_SLAVE)
    {
        /* SPI_InitStructure->SpiMode = SPI_MODE_SLAVE; */
        return -RT_ERROR;
    }
    else
    {
        SPI_InitStructure->SpiMode = SPI_MODE_MASTER;
    }

    if (cfg->mode & RT_SPI_3WIRE)
    {
        SPI_InitStructure->DataDirection = SPI_DIR_SINGLELINE_TX;
    }
    else
    {
        SPI_InitStructure->DataDirection = SPI_DIR_DOUBLELINE_FULLDUPLEX;
    }

    if (cfg->data_width == 8)
    {
        SPI_InitStructure->DataLen = SPI_DATA_SIZE_8BITS;
    }
    else if (cfg->data_width == 16)
    {
        SPI_InitStructure->DataLen = SPI_DATA_SIZE_16BITS;
    }
    else
    {
        return -RT_EIO;
    }

    if (cfg->mode & RT_SPI_CPHA)
    {
        SPI_InitStructure->CLKPHA = SPI_CLKPHA_SECOND_EDGE;
    }
    else
    {
        SPI_InitStructure->CLKPHA = SPI_CLKPHA_FIRST_EDGE;
    }

    if (cfg->mode & RT_SPI_CPOL)
    {
        SPI_InitStructure->CLKPOL = SPI_CLKPOL_HIGH;
    }
    else
    {
        SPI_InitStructure->CLKPOL = SPI_CLKPOL_LOW;
    }

    if (cfg->mode & RT_SPI_NO_CS)
    {
        SPI_InitStructure->NSS = SPI_NSS_HARD;
    }
    else
    {
        SPI_InitStructure->NSS = SPI_NSS_SOFT;
    }

    RCC_ClocksType RCC_Clock;
    RCC_GetClocksFreqValue(&RCC_Clock);
    rt_uint64_t SPI_APB_CLOCK;

    if (SPI1 == spi_handle)
    {
        SPI_APB_CLOCK = RCC_Clock.Pclk1Freq;
    }
    else if (SPI2 == spi_handle ||  SPI3 == spi_handle)
    {
        SPI_APB_CLOCK = RCC_Clock.Pclk2Freq;
    }

    if (cfg->max_hz >= SPI_APB_CLOCK / 2)
    {
        SPI_InitStructure->BaudRatePres = SPI_BR_PRESCALER_2;
    }
    else if (cfg->max_hz >= SPI_APB_CLOCK / 4)
    {
        SPI_InitStructure->BaudRatePres = SPI_BR_PRESCALER_4;
    }
    else if (cfg->max_hz >= SPI_APB_CLOCK / 8)
    {
        SPI_InitStructure->BaudRatePres = SPI_BR_PRESCALER_8;
    }
    else if (cfg->max_hz >= SPI_APB_CLOCK / 16)
    {
        SPI_InitStructure->BaudRatePres = SPI_BR_PRESCALER_16;
    }
    else if (cfg->max_hz >= SPI_APB_CLOCK / 32)
    {
        SPI_InitStructure->BaudRatePres = SPI_BR_PRESCALER_32;
    }
    else if (cfg->max_hz >= SPI_APB_CLOCK / 64)
    {
        SPI_InitStructure->BaudRatePres = SPI_BR_PRESCALER_64;
    }
    else if (cfg->max_hz >= SPI_APB_CLOCK / 128)
    {
        SPI_InitStructure->BaudRatePres = SPI_BR_PRESCALER_128;
    }
    else
    {
        SPI_InitStructure->BaudRatePres = SPI_BR_PRESCALER_256;
    }

    if (cfg->mode & RT_SPI_MSB)
    {
        SPI_InitStructure->FirstBit = SPI_FB_MSB;
    }
    else
    {
        SPI_InitStructure->FirstBit = SPI_FB_LSB;
    }

    SPI_InitStructure->CRCPoly       = 7;

    SPI_Init(spi_handle, SPI_InitStructure);
    /* Enable SPI_MASTER TXE interrupt */
    SPI_I2S_EnableInt(spi_handle, SPI_I2S_INT_TE, ENABLE);

    /* Enable SPI_MASTER */
    SPI_Enable(spi_handle, ENABLE);

    return RT_EOK;
}

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 n32_spi *spi_drv =  rt_container_of(device->bus, struct n32_spi, spi_bus);
    spi_drv->cfg = configuration;

    return n32_spi_init(spi_drv, configuration);
}

static rt_ssize_t _spi_recv(SPI_Module *hspi,
        uint8_t *tx_buff,
        uint8_t *rx_buff,
        uint32_t length,
        uint32_t timeout)
{
    /* Init tickstart for timeout management*/
    uint32_t tickstart = rt_tick_get();
    uint8_t dat = 0;

    if ((tx_buff == RT_NULL) && (rx_buff == RT_NULL) || (length == 0))
    {
        return -RT_EIO;
    }

    while (length--)
    {
        while (SPI_I2S_GetStatus(hspi, SPI_I2S_TE_FLAG) == RESET)
        {
            if ((rt_tick_get() - tickstart) > timeout)
            {
                return -RT_ETIMEOUT;
            }
        }
        SPI_I2S_TransmitData(hspi, *tx_buff++);

        while (SPI_I2S_GetStatus(hspi, SPI_I2S_RNE_FLAG) == RESET)
        {
            if ((rt_tick_get() - tickstart) > timeout)
            {
                return -RT_ETIMEOUT;
            }
        }
        dat = SPI_I2S_ReceiveData(hspi);

        if (rx_buff)
        {
            *rx_buff++ = dat;
        }
    }
    return RT_EOK;
}

static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
{
    rt_size_t send_length;
    rt_uint8_t *recv_buf;
    const rt_uint8_t *send_buf;
    rt_ssize_t stat = RT_EOK;

    /* Check Direction parameter */
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(device->bus != RT_NULL);
    RT_ASSERT(device->bus->parent.user_data != RT_NULL);
    RT_ASSERT(message != RT_NULL);

    struct n32_spi *spi_drv =  rt_container_of(device->bus, struct n32_spi, spi_bus);
    struct n32_hw_spi_cs *cs = device->parent.user_data;
    SPI_Module *spi_handle = spi_drv->config->module;

    if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS))
    {
        if (device->config.mode & RT_SPI_CS_HIGH)
        {
            GPIO_SetBits(cs->module, cs->pin);
        }
        else
        {
            GPIO_ResetBits(cs->module, cs->pin);
        }
    }

    send_length = message->length;
    recv_buf = message->recv_buf;
    send_buf = message->send_buf;

    /* start once data exchange in DMA mode */
    if (message->send_buf && message->recv_buf)
    {
        LOG_D("%s:%d", __FUNCTION__, __LINE__);
        stat = -RT_EIO;
    }
    else if (message->send_buf)
    {
        stat = _spi_recv(spi_handle,
                         (uint8_t *)send_buf,
                         RT_NULL,
                         send_length,
                         SPI_TIME_OUT);
    }
    else
    {
        rt_memset(recv_buf, 0xff, send_length);
        stat = _spi_recv(spi_handle,
                         (uint8_t *)recv_buf,
                         (uint8_t *)recv_buf,
                         send_length,
                         SPI_TIME_OUT);
    }

    if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS))
    {
        if (device->config.mode & RT_SPI_CS_HIGH)
        {
            GPIO_ResetBits(cs->module, cs->pin);
        }
        else
        {
            GPIO_SetBits(cs->module, cs->pin);
        }
    }

    if (stat != RT_EOK)
    {
        send_length = 0;
    }

    return send_length;
}

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

static int rt_hw_spi_bus_init(void)
{
    rt_err_t result;
    for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
    {
        spi_bus_obj[i].config = &spi_config[i];
        spi_bus_obj[i].spi_bus.parent.user_data = &spi_config[i];
        result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].bus_name, &n32_spi_ops);
        RT_ASSERT(result == RT_EOK);

        LOG_D("%s bus init done", spi_config[i].bus_name);
    }

    return result;
}
int rt_hw_spi_init(void)
{
    /* TODO: n32_get_dma_info(); */
    return rt_hw_spi_bus_init();
}
INIT_BOARD_EXPORT(rt_hw_spi_init);


/**
  * 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, GPIO_Module *cs_gpiox, uint32_t cs_gpio_pin)
{
    rt_err_t result;
    struct rt_spi_device *spi_device;
    struct n32_hw_spi_cs *cs_pin;
    GPIO_InitType GPIO_InitStructure;

    RT_ASSERT(bus_name != RT_NULL);
    RT_ASSERT(device_name != RT_NULL);


    /* Enable the GPIO Clock */
    if (cs_gpiox == GPIOA)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
    }
    else if (cs_gpiox == GPIOB)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
    }
    else if (cs_gpiox == GPIOC)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
    }
    else if (cs_gpiox == GPIOD)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOD, ENABLE);
    }
    else if (cs_gpiox == GPIOE)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOE, ENABLE);
    }
    else if (cs_gpiox == GPIOF)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOF, ENABLE);
    }
    else if (cs_gpiox == GPIOG)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOG, ENABLE);
    }

    /* Configure the GPIO pin */
    if (cs_gpio_pin <= GPIO_PIN_ALL)
    {
        GPIO_InitStructure.Pin        = cs_gpio_pin;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitPeripheral(cs_gpiox, &GPIO_InitStructure);
    }

    /* 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);
    cs_pin = (struct n32_hw_spi_cs *)rt_malloc(sizeof(struct n32_hw_spi_cs));
    RT_ASSERT(cs_pin != RT_NULL);
    cs_pin->module = cs_gpiox;
    cs_pin->pin = cs_gpio_pin;

    result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);

    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;
}

#endif /* BSP_USING_SPIx */

#endif /* BSP_USING_SPI */
#endif /* RT_USING_SPI */