summaryrefslogtreecommitdiff
path: root/bsp/hk32/libraries/rt_drivers/drv_spi.c
blob: 41a693d98062d550dd4f17a2597ef1c4b461625c (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
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-08-15     Jonas        first version
 */

#include <board.h>
#include "drv_spi.h"

#ifdef RT_USING_SPI
#if !defined(BSP_USING_SPI1) && !defined(BSP_USING_SPI2) && \
    !defined(BSP_USING_SPI3) && !defined(BSP_USING_SPI4)
    #error "Please define at least one SPIx"
#endif

//#define DEBUG

#define ARR_LEN(__N)      (sizeof(__N) / sizeof(__N[0]))

#ifdef DEBUG
    #define DEBUG_PRINTF(...)   rt_kprintf(__VA_ARGS__)
#else
    #define DEBUG_PRINTF(...)
#endif

/* private rt-thread spi ops function */
static rt_err_t configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
static rt_uint32_t xfer(struct rt_spi_device *device, struct rt_spi_message *message);

static struct rt_spi_ops hk32_spi_ops =
{
    configure,
    xfer
};

/**
  * 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_TypeDef *cs_gpiox, uint16_t cs_gpio_pin)
{
    RT_ASSERT(bus_name != RT_NULL);
    RT_ASSERT(device_name != RT_NULL);

    rt_err_t result;
    struct rt_spi_device *spi_device;
    struct hk32_spi_cs *cs_pin;

    /* initialize the cs pin && select the slave*/
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Pin = cs_gpio_pin;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(cs_gpiox, &GPIO_InitStruct);
    GPIO_SetBits(cs_gpiox, cs_gpio_pin);

    /* 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 hk32_spi_cs *)rt_malloc(sizeof(struct hk32_spi_cs));
    RT_ASSERT(cs_pin != RT_NULL);
    cs_pin->GPIOx = cs_gpiox;
    cs_pin->GPIO_Pin = cs_gpio_pin;
    result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);

    if (result != RT_EOK)
    {
        DEBUG_PRINTF("%s attach to %s faild, %d\n", device_name, bus_name, result);
    }

    RT_ASSERT(result == RT_EOK);

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

    return result;
}

static rt_err_t configure(struct rt_spi_device *device,
                          struct rt_spi_configuration *configuration)
{
    struct rt_spi_bus *spi_bus = (struct rt_spi_bus *)device->bus;
    struct hk32_spi *spi_instance = (struct hk32_spi *)spi_bus->parent.user_data;

    SPI_InitTypeDef SPI_InitStruct;

    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(configuration != RT_NULL);

    hk32_msp_spi_init(spi_instance->config->spix);

    /* data_width */
    if (configuration->data_width <= 8)
    {
        SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
    }
    else if (configuration->data_width <= 16)
    {
        SPI_InitStruct.SPI_DataSize = SPI_DataSize_16b;
    }
    else
    {
        return -RT_EIO;
    }

    /* baudrate */
    {
        uint32_t spi_apb_clock;
        uint32_t max_hz;
        RCC_ClocksTypeDef  RCC_Clocks;

        max_hz = configuration->max_hz;

        RCC_GetClocksFreq(&RCC_Clocks);
        DEBUG_PRINTF("sys   freq: %d\n", RCC_Clocks.SYSCLK_Freq);
        DEBUG_PRINTF("max   freq: %d\n", max_hz);

        if (spi_instance->config->spix == SPI1)
        {
            spi_apb_clock = RCC_Clocks.PCLK_Frequency;
            DEBUG_PRINTF("pclk freq: %d\n", RCC_Clocks.PCLK_Frequency);
        }
        else
        {
            spi_apb_clock = RCC_Clocks.PCLK_Frequency;
            DEBUG_PRINTF("pclk1 freq: %d\n", RCC_Clocks.PCLK_Frequency);
        }

        if (max_hz >= spi_apb_clock / 2)
        {
            SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
        }
        else if (max_hz >= spi_apb_clock / 4)
        {
            SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
        }
        else if (max_hz >= spi_apb_clock / 8)
        {
            SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
        }
        else if (max_hz >= spi_apb_clock / 16)
        {
            SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
        }
        else if (max_hz >= spi_apb_clock / 32)
        {
            SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
        }
        else if (max_hz >= spi_apb_clock / 64)
        {
            SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
        }
        else if (max_hz >= spi_apb_clock / 128)
        {
            SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
        }
        else
        {
            /*  min prescaler 256 */
            SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
        }
    } /* baudrate */

    switch (configuration->mode & RT_SPI_MODE_3)
    {
    case RT_SPI_MODE_0:
        SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
        SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
        break;
    case RT_SPI_MODE_1:
        SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
        SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
        break;
    case RT_SPI_MODE_2:
        SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
        SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
        break;
    case RT_SPI_MODE_3:
        SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
        SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
        break;
    }

    /* MSB or LSB */
    if (configuration->mode & RT_SPI_MSB)
    {
        SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
    }
    else
    {
        SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_LSB;
    }

    SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
    SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;

    /* init SPI */
    SPI_Init(spi_instance->config->spix, &SPI_InitStruct);
    /* Enable SPI_MASTER */
    SPI_Cmd(spi_instance->config->spix, ENABLE);
    SPI_CalculateCRC(spi_instance->config->spix, DISABLE);

    return RT_EOK;
};

static rt_uint32_t xfer(struct rt_spi_device *device, struct rt_spi_message *message)
{
    struct rt_spi_bus *hk32_spi_bus = (struct rt_spi_bus *)device->bus;
    struct hk32_spi *spi_instance = (struct hk32_spi *)hk32_spi_bus->parent.user_data;
    struct rt_spi_configuration *config = &device->config;
    struct hk32_spi_cs *hk32_spi_cs = device->parent.user_data;

    RT_ASSERT(device != NULL);
    RT_ASSERT(message != NULL);

    /* take CS */
    if (message->cs_take)
    {
        GPIO_ResetBits(hk32_spi_cs->GPIOx, hk32_spi_cs->GPIO_Pin);
        DEBUG_PRINTF("spi take cs\n");
    }

    {
        if (config->data_width <= 8)
        {
            const rt_uint8_t *send_ptr = message->send_buf;
            rt_uint8_t *recv_ptr = message->recv_buf;
            rt_uint32_t size = message->length;

            DEBUG_PRINTF("spi poll transfer start: %d\n", size);

            while (size--)
            {
                rt_uint8_t data = 0xFF;

                if (send_ptr != RT_NULL)
                {
                    data = *send_ptr++;
                }

                /* Todo: replace register read/write by hk32 lib */
                /* Wait until the transmit buffer is empty */
                while (RESET == SPI_I2S_GetFlagStatus(spi_instance->config->spix, SPI_I2S_FLAG_TXE));
                /*  Send the byte */
                SPI_SendData8(spi_instance->config->spix, data);

                /* Wait until a data is received */
                while (RESET == SPI_I2S_GetFlagStatus(spi_instance->config->spix, SPI_I2S_FLAG_RXNE));
                /* Get the received data */
                data = SPI_ReceiveData8(spi_instance->config->spix);

                if (recv_ptr != RT_NULL)
                {
                    *recv_ptr++ = data;
                }
            }
            DEBUG_PRINTF("spi poll transfer finsh\n");
        }
        else if (config->data_width <= 16)
        {
            const rt_uint16_t *send_ptr = message->send_buf;
            rt_uint16_t *recv_ptr = message->recv_buf;
            rt_uint32_t size = message->length;

            while (size--)
            {
                rt_uint16_t data = 0xFF;

                if (send_ptr != RT_NULL)
                {
                    data = *send_ptr++;
                }

                /* Wait until the transmit buffer is empty */
                while (RESET == SPI_I2S_GetFlagStatus(spi_instance->config->spix, SPI_I2S_FLAG_TXE));
                /* Send the byte */
                SPI_I2S_SendData16(spi_instance->config->spix, data);

                /* Wait until a data is received */
                while (RESET == SPI_I2S_GetFlagStatus(spi_instance->config->spix, SPI_I2S_FLAG_RXNE));
                /*  Get the received data */
                data = SPI_I2S_ReceiveData16(spi_instance->config->spix);

                if (recv_ptr != RT_NULL)
                {
                    *recv_ptr++ = data;
                }
            }
        }
    }

    /* release CS */
    if (message->cs_release)
    {
        GPIO_SetBits(hk32_spi_cs->GPIOx, hk32_spi_cs->GPIO_Pin);
        DEBUG_PRINTF("spi release cs\n");
    }

    return message->length;
};

static struct hk32_spi_config configs[] =
{
#ifdef BSP_USING_SPI1
    {SPI1, "spi1"},
#endif

#ifdef BSP_USING_SPI2
    {SPI2, "spi2"},
#endif

#ifdef BSP_USING_SPI3
    {SPI3, "spi3"},
#endif

#ifdef BSP_USING_SPI4
    {SPI4, "spi4"},
#endif
};

static struct hk32_spi spis[sizeof(configs) / sizeof(configs[0])] = {0};

int rt_hw_spi_init(void)
{
    int i;
    rt_err_t result;
    rt_size_t obj_num = sizeof(spis) / sizeof(struct hk32_spi);

    for (i = 0; i < obj_num; i++)
    {
        spis[i].config = &configs[i];
        spis[i].spi_bus.parent.user_data = (void *)&spis[i];
        result = rt_spi_bus_register(&(spis[i].spi_bus), spis[i].config->spi_name, &hk32_spi_ops);
    }

    return result;
}

INIT_BOARD_EXPORT(rt_hw_spi_init);

#endif