drv_spi.c 9.3 KB
Newer Older
S
Sherman 已提交
1 2 3 4 5 6 7 8
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-08-23     Mr.Tiger     first version
S
Sherman 已提交
9
 * 2021-11-04     Sherman      ADD complete_event
10
 * 2022-12-7      Vandoul      ADD sci spi support
S
Sherman 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 */
/**< Note : Turn on any DMA mode and all SPIs will turn on DMA */

#include "drv_spi.h"

#ifdef RT_USING_SPI

//#define DRV_DEBUG
#define DBG_TAG              "drv.spi"
#ifdef DRV_DEBUG
    #define DBG_LVL               DBG_LOG
#else
    #define DBG_LVL               DBG_INFO
#endif /* DRV_DEBUG */
#include <rtdbg.h>

27
#if defined(BSP_USING_SPI0) || defined(BSP_USING_SPI1)
28 29
#define RA_SPI0_EVENT 0x01
#define RA_SPI1_EVENT 0x02
S
Sherman 已提交
30 31
static struct rt_event complete_event = {0};

S
Sherman 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44
static struct ra_spi_handle spi_handle[] =
{
#ifdef BSP_USING_SPI0
    {.bus_name = "spi0", .spi_ctrl_t = &g_spi0_ctrl, .spi_cfg_t = &g_spi0_cfg,},
#endif

#ifdef BSP_USING_SPI1
    {.bus_name = "spi1", .spi_ctrl_t = &g_spi1_ctrl, .spi_cfg_t = &g_spi1_cfg,},
#endif
};

static struct ra_spi spi_config[sizeof(spi_handle) / sizeof(spi_handle[0])] = {0};

45
void spi0_callback(spi_callback_args_t *p_args)
S
Sherman 已提交
46 47 48 49
{
    rt_interrupt_enter();
    if (SPI_EVENT_TRANSFER_COMPLETE == p_args->event)
    {
S
Sherman 已提交
50
        rt_event_send(&complete_event, RA_SPI0_EVENT);
S
Sherman 已提交
51 52 53 54
    }
    rt_interrupt_leave();
}

55
void spi1_callback(spi_callback_args_t *p_args)
S
Sherman 已提交
56 57 58 59
{
    rt_interrupt_enter();
    if (SPI_EVENT_TRANSFER_COMPLETE == p_args->event)
    {
S
Sherman 已提交
60
        rt_event_send(&complete_event, RA_SPI1_EVENT);
S
Sherman 已提交
61 62 63 64
    }
    rt_interrupt_leave();
}

S
Sherman 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
static rt_err_t ra_wait_complete(rt_event_t event, const char bus_name[RT_NAME_MAX])
{
    rt_uint32_t recved = 0x00;

    if (bus_name[3] == '0')
    {
        return rt_event_recv(event,
                             RA_SPI0_EVENT,
                             RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
                             RT_WAITING_FOREVER,
                             &recved);
    }
    else if (bus_name[3] == '1')
    {
        return rt_event_recv(event,
                             RA_SPI1_EVENT,
                             RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
                             RT_WAITING_FOREVER,
                             &recved);
    }
    return -RT_EINVAL;
}

S
Sherman 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100
static spi_bit_width_t ra_width_shift(rt_uint8_t data_width)
{
    spi_bit_width_t bit_width = SPI_BIT_WIDTH_8_BITS;
    if(data_width == 1)
        bit_width = SPI_BIT_WIDTH_8_BITS;
    else if(data_width == 2)
        bit_width = SPI_BIT_WIDTH_16_BITS;
    else if(data_width == 4)
        bit_width = SPI_BIT_WIDTH_32_BITS;

    return bit_width;
}

S
Sherman 已提交
101 102 103 104 105 106 107 108 109
static rt_err_t ra_write_message(struct rt_spi_device *device, const void *send_buf, const rt_size_t len)
{
    RT_ASSERT(device != NULL);
    RT_ASSERT(device->parent.user_data != NULL);
    RT_ASSERT(send_buf != NULL);
    RT_ASSERT(len > 0);
    rt_err_t err = RT_EOK;
    struct ra_spi *spi_dev =  rt_container_of(device->bus, struct ra_spi, bus);

S
Sherman 已提交
110
    spi_bit_width_t bit_width = ra_width_shift(spi_dev->rt_spi_cfg_t->data_width);
S
Sherman 已提交
111
    /**< send msessage */
S
Sherman 已提交
112
    err = R_SPI_Write((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, send_buf, len, bit_width);
S
Sherman 已提交
113 114 115 116 117
    if (RT_EOK != err)
    {
        LOG_E("%s write failed.", spi_dev->ra_spi_handle_t->bus_name);
        return -RT_ERROR;
    }
S
Sherman 已提交
118 119
    /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
    ra_wait_complete(&complete_event, spi_dev->ra_spi_handle_t->bus_name);
S
Sherman 已提交
120 121 122 123 124 125 126 127 128 129 130 131
    return len;
}

static rt_err_t ra_read_message(struct rt_spi_device *device, void *recv_buf, const rt_size_t len)
{
    RT_ASSERT(device != NULL);
    RT_ASSERT(device->parent.user_data != NULL);
    RT_ASSERT(recv_buf != NULL);
    RT_ASSERT(len > 0);
    rt_err_t err = RT_EOK;
    struct ra_spi *spi_dev =  rt_container_of(device->bus, struct ra_spi, bus);

S
Sherman 已提交
132
    spi_bit_width_t bit_width = ra_width_shift(spi_dev->rt_spi_cfg_t->data_width);
S
Sherman 已提交
133
    /**< receive message */
S
Sherman 已提交
134
    err = R_SPI_Read((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, recv_buf, len, bit_width);
S
Sherman 已提交
135 136 137 138 139
    if (RT_EOK != err)
    {
        LOG_E("\n%s write failed.\n", spi_dev->ra_spi_handle_t->bus_name);
        return -RT_ERROR;
    }
S
Sherman 已提交
140 141
    /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
    ra_wait_complete(&complete_event, spi_dev->ra_spi_handle_t->bus_name);
S
Sherman 已提交
142 143 144 145 146 147 148 149 150 151 152
    return len;
}

static rt_err_t ra_write_read_message(struct rt_spi_device *device, struct rt_spi_message *message)
{
    RT_ASSERT(device != NULL);
    RT_ASSERT(message != NULL);
    RT_ASSERT(message->length > 0);
    rt_err_t err = RT_EOK;
    struct ra_spi *spi_dev =  rt_container_of(device->bus, struct ra_spi, bus);

S
Sherman 已提交
153
    spi_bit_width_t bit_width = ra_width_shift(spi_dev->rt_spi_cfg_t->data_width);
S
Sherman 已提交
154
    /**< write and receive message */
S
Sherman 已提交
155
    err = R_SPI_WriteRead((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, message->send_buf, message->recv_buf, message->length, bit_width);
S
Sherman 已提交
156 157 158 159 160
    if (RT_EOK != err)
    {
        LOG_E("%s write and read failed.", spi_dev->ra_spi_handle_t->bus_name);
        return -RT_ERROR;
    }
S
Sherman 已提交
161 162
    /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
    ra_wait_complete(&complete_event, spi_dev->ra_spi_handle_t->bus_name);
S
Sherman 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    return message->length;
}

/**< init spi TODO : MSB does not support modification */
static rt_err_t ra_hw_spi_configure(struct rt_spi_device *device,
                                    struct rt_spi_configuration *configuration)
{
    RT_ASSERT(device != NULL);
    RT_ASSERT(configuration != NULL);
    rt_err_t err = RT_EOK;

    struct ra_spi *spi_dev =  rt_container_of(device->bus, struct ra_spi, bus);
    spi_dev->cs_pin = (rt_uint32_t)device->parent.user_data;

    /**< data_width : 1 -> 8 bits , 2 -> 16 bits, 4 -> 32 bits, default 32 bits*/
    rt_uint8_t data_width = configuration->data_width / 8;
    RT_ASSERT(data_width == 1 || data_width == 2 || data_width == 4);
    configuration->data_width = configuration->data_width / 8;
    spi_dev->rt_spi_cfg_t = configuration;

    spi_extended_cfg_t *spi_cfg = (spi_extended_cfg_t *)spi_dev->ra_spi_handle_t->spi_cfg_t->p_extend;

    /**< Configure Select Line */
S
Sherman 已提交
186
    rt_pin_write(spi_dev->cs_pin, PIN_HIGH);
S
Sherman 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

    /**< config bitrate */
    R_SPI_CalculateBitrate(spi_dev->rt_spi_cfg_t->max_hz, &spi_cfg->spck_div);

    /**< init */
    err = R_SPI_Open((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, (spi_cfg_t const * const)spi_dev->ra_spi_handle_t->spi_cfg_t);
    /* handle error */
    if (RT_EOK != err)
    {
        LOG_E("%s init failed.", spi_dev->ra_spi_handle_t->bus_name);
        return -RT_ERROR;
    }
    return RT_EOK;
}

static rt_uint32_t ra_spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
{
S
Sherman 已提交
204 205 206 207
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(device->bus != RT_NULL);
    RT_ASSERT(message != RT_NULL);

S
Sherman 已提交
208
    rt_err_t err = RT_EOK;
S
Sherman 已提交
209 210
    struct ra_spi *spi_dev =  rt_container_of(device->bus, struct ra_spi, bus);
    spi_dev->cs_pin = (rt_uint32_t)device->parent.user_data;
S
Sherman 已提交
211

S
Sherman 已提交
212
    if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS))
S
Sherman 已提交
213
    {
S
Sherman 已提交
214 215 216 217
        if (device->config.mode & RT_SPI_CS_HIGH)
            rt_pin_write(spi_dev->cs_pin, PIN_HIGH);
        else
            rt_pin_write(spi_dev->cs_pin, PIN_LOW);
S
Sherman 已提交
218
    }
S
Sherman 已提交
219 220

    if (message->length > 0)
S
Sherman 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    {
        if (message->send_buf == RT_NULL && message->recv_buf != RT_NULL)
        {
            /**< receive message */
            err = ra_read_message(device, (void *)message->recv_buf, (const rt_size_t)message->length);
        }
        else if (message->send_buf != RT_NULL && message->recv_buf == RT_NULL)
        {
            /**< send message */
            err = ra_write_message(device, (const void *)message->send_buf, (const rt_size_t)message->length);
        }
        else if (message->send_buf != RT_NULL && message->recv_buf != RT_NULL)
        {
            /**< send and receive message */
            err =  ra_write_read_message(device, message);
        }
    }
S
Sherman 已提交
238 239 240 241 242 243 244 245

    if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS))
    {
        if (device->config.mode & RT_SPI_CS_HIGH)
            rt_pin_write(spi_dev->cs_pin, PIN_LOW);
        else
            rt_pin_write(spi_dev->cs_pin, PIN_HIGH);
    }
S
Sherman 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    return err;
}

static const struct rt_spi_ops ra_spi_ops =
{
    .configure = ra_hw_spi_configure,
    .xfer = ra_spixfer,
};

int ra_hw_spi_init(void)
{
    for (rt_uint8_t spi_index = 0; spi_index < sizeof(spi_handle) / sizeof(spi_handle[0]); spi_index++)
    {
        spi_config[spi_index].ra_spi_handle_t = &spi_handle[spi_index];

        /**< register spi bus */
        rt_err_t err = rt_spi_bus_register(&spi_config[spi_index].bus, spi_handle[spi_index].bus_name, &ra_spi_ops);
        if (RT_EOK != err)
        {
            LOG_E("%s bus register failed.", spi_config[spi_index].ra_spi_handle_t->bus_name);
S
Sherman 已提交
266
            return -RT_ERROR;
S
Sherman 已提交
267 268 269
        }
    }

S
Sherman 已提交
270 271 272 273 274
    if (RT_EOK != rt_event_init(&complete_event, "ra_spi", RT_IPC_FLAG_PRIO))
    {
        LOG_E("SPI transfer event init fail!");
        return -RT_ERROR;
    }
S
Sherman 已提交
275 276 277
    return RT_EOK;
}
INIT_BOARD_EXPORT(ra_hw_spi_init);
278 279 280 281 282 283 284 285 286 287 288 289 290 291
#endif
void rt_hw_spi_device_attach(struct rt_spi_device *device, const char *device_name, const char *bus_name, void *user_data)
{
    RT_ASSERT(device != NULL);
    RT_ASSERT(device_name != NULL);
    RT_ASSERT(bus_name != NULL);
    RT_ASSERT(user_data != NULL);

    rt_err_t err = rt_spi_bus_attach_device(device, device_name, bus_name, user_data);
    if (RT_EOK != err)
    {
        LOG_E("%s attach failed.", bus_name);
    }
}
S
Sherman 已提交
292
#endif /* RT_USING_SPI */