summaryrefslogtreecommitdiff
path: root/bsp/w60x/drivers/drv_spi.c
blob: ecec74927894772a2ad5804811be88de1440b8a4 (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
/*
 * Copyright (c) 2019 Winner Microelectronics Co., Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-12     fanwenl      1st version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include "board.h"
#include "wm_hostspi.h"
#include "wm_spi_hal.h"
#include "wm_io.h"
#include "wm_gpio_afsel.h"
#include "pin_map.h"
#include "drv_spi.h"

#ifdef BSP_USING_SPI

#define BSP_SPI_MAX_HZ (20* 1000 *1000)

struct wm_sw_spi_cs
{
    rt_int16_t pin;
};

struct wm_spi
{
    struct rt_spi_configuration *cfg;
};

static rt_err_t wm_hostspi_init(struct rt_spi_configuration *cfg)
{

    spi_clear_fifo();
    spi_set_endian(1);
    if (cfg->data_width == 8)
    {
        tls_spi_trans_type(SPI_BYTE_TRANSFER);
    }
    if (cfg->data_width == 16)
    {
        tls_spi_trans_type(SPI_WORD_TRANSFER);
    }
    spi_set_mode(cfg->mode);
    spi_set_chipselect_mode(SPI_CS_INACTIVE_MODE);
    spi_force_cs_out(1);
    if(cfg->max_hz > BSP_SPI_MAX_HZ)
    {
        cfg->max_hz = BSP_SPI_MAX_HZ;
    }
    spi_set_sclk(cfg->max_hz);

    spi_set_tx_trigger_level(0);
    spi_set_rx_trigger_level(7);

    spi_set_rx_channel(1);
    spi_set_tx_channel(1);

    return RT_EOK;
}

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

    /*将cfg参数放到device中*/
    struct wm_spi *hspi = (struct wm_spi *)device->bus->parent.user_data;
    hspi->cfg = cfg;

    wm_hostspi_init(cfg);

    return RT_EOK;
}

static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
{
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(device->bus != RT_NULL);
    RT_ASSERT(device->bus->parent.user_data != RT_NULL);

    struct wm_sw_spi_cs *cs = device->parent.user_data;

    struct tls_spi_transfer tls_transfer;

    tls_transfer.tx_buf = message->send_buf;
    tls_transfer.rx_buf = message->recv_buf;
    tls_transfer.len = message->length;

    rt_int32_t length = 0;
    rt_int32_t remain_length = message->length;
    rt_int32_t int_status;

    tls_irq_disable(SPI0_INT);

    length = spi_fill_txfifo(&tls_transfer, remain_length);
    spi_set_sclk_length(length * 8, 0);
    if (message->cs_take && cs)
    {
        tls_gpio_write((enum tls_io_name)cs->pin, 0);
    }
    spi_sclk_start();

    while (remain_length > 0)
    {
        while (spi_i2s_get_busy_status() == 1)
            ;
        length = spi_get_rxfifo(&tls_transfer, remain_length);
        remain_length -= length;

        if (remain_length == 0)
        {
            while (spi_i2s_get_busy_status() == 1)
                ;
            if (message->cs_release && cs)
            {
                tls_gpio_write((enum tls_io_name)cs->pin, 1);
            }
        }
        while (spi_i2s_get_busy_status() == 1)
            ;
        length = spi_fill_txfifo(&tls_transfer, remain_length);
        if (length)
        {
            spi_set_sclk_length(length * 8, 0);
            spi_sclk_start();
        }
    }

    while (spi_i2s_get_busy_status() == 1)
        ;
    if (message->cs_release && cs)
    {
        tls_gpio_write((enum tls_io_name)cs->pin, 1);
    }

    int_status = spi_get_int_status();
    spi_clear_int_status(int_status);
    tls_irq_enable(SPI0_INT);

    return message->length - remain_length;
}

rt_err_t wm_spi_bus_attach_device(const char *bus_name, const char *device_name, rt_uint32_t pin)
{
    rt_err_t ret;
    rt_int16_t gpio_pin;
    struct rt_spi_device *spi_device;
    struct wm_sw_spi_cs *cs_pin;

    gpio_pin = wm_get_pin(pin);
    if (gpio_pin == WM_PIN_DEFAULT)
    {
        return -RT_ERROR;
    }
    spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
    RT_ASSERT(spi_device != RT_NULL);

    cs_pin = (struct wm_sw_spi_cs *)rt_malloc(sizeof(struct wm_sw_spi_cs));
    RT_ASSERT(cs_pin != RT_NULL);

    cs_pin->pin = gpio_pin;
    tls_gpio_cfg((enum tls_io_name)gpio_pin, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_PULLHIGH);
    tls_gpio_write((enum tls_io_name)gpio_pin, 1);

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

    return ret;
}

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

struct wm_spi spi;
struct rt_spi_bus wm_spi_bus;

int wm_hw_spi_bus_init(void)
{
    rt_int16_t gpio_pin;

    gpio_pin = wm_get_pin(WM_SPI_CK_PIN);
    if (gpio_pin >= 0)
    {
        wm_spi_ck_config((enum tls_io_name)gpio_pin);
    }
    gpio_pin = wm_get_pin(WM_SPI_DI_PIN);
    if (gpio_pin >= 0)
    {
        wm_spi_di_config((enum tls_io_name)gpio_pin);
    }
    gpio_pin = wm_get_pin(WM_SPI_DO_PIN);
    if (gpio_pin >= 0)
    {
        wm_spi_do_config((enum tls_io_name)gpio_pin);
    }

    wm_spi_bus.parent.user_data = &spi;
#ifdef WM_SPI_BUS_NAME
    rt_spi_bus_register(&wm_spi_bus, WM_SPI_BUS_NAME, &wm_spi_ops);
#else
    rt_spi_bus_register(&wm_spi_bus, "spi0", &wm_spi_ops);
#endif

    return RT_EOK;
}
INIT_PREV_EXPORT(wm_hw_spi_bus_init);

#endif /* BSP_USING_SPI */