summaryrefslogtreecommitdiff
path: root/bsp/gd32303e-eval/drivers/drv_soft_spi.c
blob: 7fb053d0d388cec15b629a65b242e306105488ff (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
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-10-11     kyle         first implementation.
 */

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

#if defined(RT_USING_SPI) && defined(RT_USING_SPI_BITOPS) && defined(RT_USING_PIN)
#include <rtdevice.h>
#include "spi-bit-ops.h"

#define DBG_TAG               "drv.SPI"
#ifdef RT_SPI_DEBUG
#define DBG_LVL               DBG_LOG
#else
#define DBG_LVL               DBG_WARNING
#endif
#include <rtdbg.h>

#ifndef ITEM_NUM
#define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
#endif

struct gd32_spi_bit_data
{
    struct
    {
        rcu_periph_enum clk;
        rt_uint32_t port;
        rt_uint32_t pin;
    } sclk, mosi, miso;
};


rt_inline FlagStatus GPIO_OUTPUT_BIT_GET(uint32_t gpio_periph, uint32_t pin)
{
    if((uint32_t)RESET !=(GPIO_OCTL(gpio_periph)&(pin))){
        return SET;
    }else{
        return RESET;
    }
}

rt_inline void GPIO_BIT_RESET(uint32_t gpio_periph, uint32_t pin)
{
    GPIO_BC(gpio_periph) = (uint32_t)pin;
}

rt_inline void GPIO_BIT_SET(uint32_t gpio_periph, uint32_t pin)
{
    GPIO_BOP(gpio_periph) = (uint32_t)pin;
}

rt_inline FlagStatus GPIO_INPUT_BIT_GET(uint32_t gpio_periph,uint32_t pin)
{
    if((uint32_t)RESET != (GPIO_ISTAT(gpio_periph)&(pin))){
        return SET;
    }else{
        return RESET;
    }
}

rt_inline void GPIO_INIT(uint32_t gpio_periph, uint32_t mode, uint32_t speed, uint32_t pin)
{
    uint16_t i;
    uint32_t temp_mode = 0U;
    uint32_t reg = 0U;

    /* GPIO mode configuration */
    temp_mode = (uint32_t)(mode & ((uint32_t)0x0FU));

    /* GPIO speed configuration */
    if(((uint32_t)0x00U) != ((uint32_t)mode & ((uint32_t)0x10U))){
            /* output mode max speed:10MHz,2MHz,50MHz */
            temp_mode |= (uint32_t)speed;
    }

    /* configure the eight low port pins with GPIO_CTL0 */
    for(i = 0U;i < 8U;i++){
        if((1U << i) & pin){
            reg = GPIO_CTL0(gpio_periph);

            /* clear the specified pin mode bits */
            reg &= ~GPIO_MODE_MASK(i);
            /* set the specified pin mode bits */
            reg |= GPIO_MODE_SET(i, temp_mode);

            /* set IPD or IPU */
            if(GPIO_MODE_IPD == mode){
                /* reset the corresponding OCTL bit */
                GPIO_BC(gpio_periph) = (uint32_t)((1U << i) & pin);
            }else{
                /* set the corresponding OCTL bit */
                if(GPIO_MODE_IPU == mode){
                    GPIO_BOP(gpio_periph) = (uint32_t)((1U << i) & pin);
                }
            }
            /* set GPIO_CTL0 register */
            GPIO_CTL0(gpio_periph) = reg;
        }
    }
    /* configure the eight high port pins with GPIO_CTL1 */
    for(i = 8U;i < 16U;i++){
        if((1U << i) & pin){
            reg = GPIO_CTL1(gpio_periph);

            /* clear the specified pin mode bits */
            reg &= ~GPIO_MODE_MASK(i - 8U);
            /* set the specified pin mode bits */
            reg |= GPIO_MODE_SET(i - 8U, temp_mode);

            /* set IPD or IPU */
            if(GPIO_MODE_IPD == mode){
                /* reset the corresponding OCTL bit */
                GPIO_BC(gpio_periph) = (uint32_t)((1U << i) & pin);
            }else{
                /* set the corresponding OCTL bit */
                if(GPIO_MODE_IPU == mode){
                    GPIO_BOP(gpio_periph) = (uint32_t)((1U << i) & pin);
                }
            }
            /* set GPIO_CTL1 register */
            GPIO_CTL1(gpio_periph) = reg;
        }
    }
}

#define GPIO_SET_OUTPUT(port, pin)        GPIO_INIT(port, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, pin)
#define GPIO_SET_INPUT(port, pin)         GPIO_INIT(port, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, pin)


static void gpio_tog_sclk(void *data)
{
    struct gd32_spi_bit_data *bd = data;

    if (GPIO_OUTPUT_BIT_GET(bd->sclk.port, bd->sclk.pin) == SET)
    {
        GPIO_BIT_RESET(bd->sclk.port, bd->sclk.pin);
    }
    else
    {
        GPIO_BIT_SET(bd->sclk.port, bd->sclk.pin);
    }
}

static void gpio_set_sclk(void *data, rt_int32_t state)
{
    struct gd32_spi_bit_data *bd = data;

    if (state)
    {
        GPIO_BIT_SET(bd->sclk.port, bd->sclk.pin);
    }
    else
    {
        GPIO_BIT_RESET(bd->sclk.port, bd->sclk.pin);
    }
}

static void gpio_set_mosi(void *data, rt_int32_t state)
{
    struct gd32_spi_bit_data *bd = data;

    if (state)
    {
        GPIO_BIT_SET(bd->mosi.port, bd->mosi.pin);
    }
    else
    {
        GPIO_BIT_RESET(bd->mosi.port, bd->mosi.pin);
    }
}

static void gpio_set_miso(void *data, rt_int32_t state)
{
    struct gd32_spi_bit_data *bd = data;

    if (state)
    {
        GPIO_BIT_SET(bd->miso.port, bd->miso.pin);
    }
    else
    {
        GPIO_BIT_RESET(bd->miso.port, bd->miso.pin);
    }
}

static rt_int32_t gpio_get_sclk(void *data)
{
    struct gd32_spi_bit_data *bd = data;

    return GPIO_INPUT_BIT_GET(bd->sclk.port, bd->sclk.pin);
}

static rt_int32_t gpio_get_mosi(void *data)
{
    struct gd32_spi_bit_data *bd = data;

    return GPIO_INPUT_BIT_GET(bd->mosi.port, bd->mosi.pin);
}

static rt_int32_t gpio_get_miso(void *data)
{
    struct gd32_spi_bit_data *bd = data;

    return GPIO_INPUT_BIT_GET(bd->miso.port, bd->miso.pin);
}

static void gpio_dir_mosi(void *data, rt_int32_t state)
{
    struct gd32_spi_bit_data *bd = data;

    if (state)
    {
        GPIO_SET_INPUT(bd->mosi.port, bd->mosi.pin);
    }
    else
    {
        GPIO_SET_OUTPUT(bd->mosi.port, bd->mosi.pin);
    }
}

static void gpio_dir_miso(void *data, rt_int32_t state)
{
    struct gd32_spi_bit_data *bd = data;

    if (state)
    {
        GPIO_SET_INPUT(bd->miso.port, bd->miso.pin);
    }
    else
    {
        GPIO_SET_OUTPUT(bd->miso.port, bd->miso.pin);
    }
}

static void gpio_udelay(rt_uint32_t us)
{
    int i = ((rcu_clock_freq_get(CK_SYS) / 4000000) * us);

    while (i)
    {
        i--;
    }
}

static void soft_spi_gpio_init(const struct gd32_spi_bit_data *bd)
{
    rcu_periph_clock_enable(bd->sclk.clk);
    rcu_periph_clock_enable(bd->mosi.clk);
    rcu_periph_clock_enable(bd->miso.clk);

    gpio_init(bd->sclk.port, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, bd->sclk.pin);
    gpio_init(bd->mosi.port, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, bd->mosi.pin);
    gpio_init(bd->miso.port, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, bd->miso.pin);

    GPIO_BIT_SET(bd->sclk.port, bd->sclk.pin);
    GPIO_BIT_SET(bd->mosi.port, bd->mosi.pin);
    GPIO_BIT_SET(bd->miso.port, bd->miso.pin);
}

int rt_soft_spi_init(void)
{
    int result = 0;

    {
        static const struct gd32_spi_bit_data spi1_bdata =
        {
            .sclk = { RCU_GPIOB, GPIOB, GPIO_PIN_13},
            .mosi = { RCU_GPIOB, GPIOB, GPIO_PIN_15},
            .miso = { RCU_GPIOB, GPIOB, GPIO_PIN_14},
        };
        static struct rt_spi_bit_ops spi1_bops =
        {
            .data     = (void *)&spi1_bdata,
            .tog_sclk = gpio_tog_sclk,
            .set_sclk = gpio_set_sclk,
            .set_mosi = gpio_set_mosi,
            .set_miso = gpio_set_miso,
            .get_sclk = gpio_get_sclk,
            .get_mosi = gpio_get_mosi,
            .get_miso = gpio_get_miso,
            .dir_mosi = gpio_dir_mosi,
            .dir_miso = gpio_dir_miso,
            .udelay   = gpio_udelay,
        };
        struct rt_spi_bit_obj spi1_obj;

        soft_spi_gpio_init(&spi1_bdata);
        rt_spi_bit_add_bus(&spi1_obj, "spi3", &spi1_bops);
    }

    return result;
}
INIT_BOARD_EXPORT(rt_soft_spi_init);
#endif