summaryrefslogtreecommitdiff
path: root/bsp/Vango/v85xx/drivers/drv_spi.c
blob: 7fa11d445574365b4635bee5e0854342f310f14f (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
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2017-06-05     tanek        first implementation.
 * 2018-04-19     misonyo      Porting for v85xxf30x
 * 2019-03-31     xuzhuoyi     Porting for v85xxe230
 * 2021-09-21     zhuxw        Porting for v85xx
 */

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

#if defined(RT_USING_SPI) && defined(RT_USING_PIN)
#include <rtdevice.h>

#if !defined(RT_USING_SPI1) && !defined(RT_USING_SPI2)
#error "Please define at least one SPIx"
#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 v85xx_spi_ops =
{
    configure,
    xfer
};

static rt_err_t configure(struct rt_spi_device* device, struct rt_spi_configuration* configuration)
{
    SPI_InitType spi_init_struct;

    rt_uint32_t spi_periph = (rt_uint32_t)device->bus->parent.user_data;

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

    if(configuration->data_width > 8)
    {
        return -RT_EIO;
    }

    {
        rt_uint32_t spi_apb_clock;
        rt_uint32_t max_hz;

        max_hz = configuration->max_hz;

        spi_apb_clock = CLK_GetPCLKFreq();

        if(max_hz >= spi_apb_clock/2)
        {
            spi_init_struct.ClockDivision = SPI_CLKDIV_2;
        }
        else if (max_hz >= spi_apb_clock/4)
        {
            spi_init_struct.ClockDivision = SPI_CLKDIV_4;
        }
        else if (max_hz >= spi_apb_clock/8)
        {
            spi_init_struct.ClockDivision = SPI_CLKDIV_8;
        }
        else if (max_hz >= spi_apb_clock/16)
        {
            spi_init_struct.ClockDivision = SPI_CLKDIV_16;
        }
        else if (max_hz >= spi_apb_clock/32)
        {
            spi_init_struct.ClockDivision = SPI_CLKDIV_32;
        }
        else if (max_hz >= spi_apb_clock/64)
        {
            spi_init_struct.ClockDivision = SPI_CLKDIV_64;
        }
        else
        {
            /*  min prescaler 128 */
            spi_init_struct.ClockDivision = SPI_CLKDIV_128;
        }
    } /* baudrate */

    switch(configuration->mode & RT_SPI_MODE_3)
    {
    case RT_SPI_MODE_0:
        spi_init_struct.SPH = SPI_SPH_0;
        spi_init_struct.SPO = SPI_SPO_0;
        break;
    case RT_SPI_MODE_1:
        spi_init_struct.SPH = SPI_SPH_1;
        spi_init_struct.SPO = SPI_SPO_0;
        break;
    case RT_SPI_MODE_2:
        spi_init_struct.SPH = SPI_SPH_0;
        spi_init_struct.SPO = SPI_SPO_1;
        break;
    case RT_SPI_MODE_3:
        spi_init_struct.SPH = SPI_SPH_1;
        spi_init_struct.SPO = SPI_SPO_1;
        break;
    }

    if(!(configuration->mode & RT_SPI_MSB))
    {
        return -RT_EIO;
    }

    spi_init_struct.Mode = SPI_MODE_MASTER;
    spi_init_struct.CSNSoft = SPI_CSNSOFT_ENABLE;

    SPI_Init((SPI_TypeDef*)spi_periph, &spi_init_struct);

    SPI_Cmd((SPI_TypeDef*)spi_periph, ENABLE);

    return RT_EOK;
};

static rt_uint32_t xfer(struct rt_spi_device* device, struct rt_spi_message* message)
{
    rt_base_t v85xx_cs_pin = (rt_base_t)device->parent.user_data;
    rt_uint32_t spi_periph = (rt_uint32_t)device->bus->parent.user_data;
    struct rt_spi_configuration * config = &device->config;

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

    /* take CS */
    if(message->cs_take)
    {
        rt_pin_write(v85xx_cs_pin, PIN_LOW);
        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++;
                }

                //Wait until the transmit buffer is empty
                while(RESET == SPI_GetStatus((SPI_TypeDef*)spi_periph, SPI_STS_TXEMPTY));
                // Send the byte
                SPI_SendData((SPI_TypeDef*)spi_periph, data);

                //Wait until a data is received
                while(RESET == SPI_GetStatus((SPI_TypeDef*)spi_periph, SPI_STS_RNE));
                // Get the received data
                data = SPI_ReceiveData((SPI_TypeDef*)spi_periph);

                if(recv_ptr != RT_NULL)
                {
                    *recv_ptr++ = data;
                }
            }
            DEBUG_PRINTF("spi poll transfer finsh\n");
        }
    }

    /* release CS */
    if(message->cs_release)
    {
        rt_pin_write(v85xx_cs_pin, PIN_HIGH);
        DEBUG_PRINTF("spi release cs\n");
    }

    return message->length;
};

int v85xx_hw_spi_init(void)
{
    int result = 0;
#ifdef RT_USING_SPI1
    static struct rt_spi_bus spi_bus0;
    spi_bus0.parent.user_data = (void *)SPI1;

    result = rt_spi_bus_register(&spi_bus0, "spi1", &v85xx_spi_ops);

#endif

#ifdef RT_USING_SPI2
    static struct rt_spi_bus spi_bus1;
    spi_bus1.parent.user_data = (void *)SPI2;

    result = rt_spi_bus_register(&spi_bus1, "spi2", &v85xx_spi_ops);

#endif
    return result;
}
INIT_BOARD_EXPORT(v85xx_hw_spi_init);
#endif