2024-03-31 20:28:24 +08:00
|
|
|
/*
|
2024-06-02 22:09:11 +08:00
|
|
|
* Copyright (c) 2006-2024, RT-Thread Development Team
|
2024-03-31 20:28:24 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2024-06-02 22:09:11 +08:00
|
|
|
* 2024-03-28 qiujingbao first version
|
|
|
|
* 2024/06/08 flyingcys fix transmission failure
|
2024-03-31 20:28:24 +08:00
|
|
|
*/
|
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
|
|
|
|
#include "board.h"
|
2024-03-31 20:28:24 +08:00
|
|
|
#include "drv_spi.h"
|
2024-06-02 22:09:11 +08:00
|
|
|
|
2024-05-28 18:45:53 +08:00
|
|
|
#include "drv_pinmux.h"
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
#define DBG_LEVEL DBG_LOG
|
2024-03-31 20:28:24 +08:00
|
|
|
#include <rtdbg.h>
|
2024-06-02 22:09:11 +08:00
|
|
|
#define LOG_TAG "drv.spi"
|
|
|
|
|
|
|
|
struct _device_spi
|
|
|
|
{
|
|
|
|
struct rt_spi_bus spi_bus;
|
|
|
|
struct dw_spi dws;
|
|
|
|
char *device_name;
|
|
|
|
};
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
static struct _device_spi _spi_obj[] =
|
2024-03-31 20:28:24 +08:00
|
|
|
{
|
2024-06-02 22:09:11 +08:00
|
|
|
#ifdef BSP_USING_SPI0
|
2024-03-31 20:28:24 +08:00
|
|
|
{
|
2024-06-02 22:09:11 +08:00
|
|
|
.dws.regs = (void *)DW_SPI0_BASE,
|
|
|
|
.dws.irq = DW_SPI0_IRQn,
|
|
|
|
.dws.index = 0,
|
|
|
|
.device_name = "spi0",
|
|
|
|
},
|
|
|
|
#endif /* BSP_USING_SPI0 */
|
|
|
|
#ifdef BSP_USING_SPI1
|
|
|
|
{
|
|
|
|
.dws.regs = (void *)DW_SPI1_BASE,
|
|
|
|
.dws.irq = DW_SPI1_IRQn,
|
|
|
|
.dws.index = 0,
|
|
|
|
.device_name = "spi1",
|
|
|
|
},
|
|
|
|
#endif /* BSP_USING_SPI1 */
|
|
|
|
#ifdef BSP_USING_SPI2
|
|
|
|
{
|
|
|
|
.dws.regs = (void *)DW_SPI2_BASE,
|
|
|
|
.dws.irq = DW_SPI2_IRQn,
|
|
|
|
.dws.index = 0,
|
2024-03-31 20:28:24 +08:00
|
|
|
.device_name = "spi2",
|
|
|
|
},
|
2024-06-02 22:09:11 +08:00
|
|
|
#endif /* BSP_USING_SPI2 */
|
|
|
|
#ifdef BSP_USING_SPI3
|
|
|
|
{
|
|
|
|
.dws.regs = (void *)DW_SPI3_BASE,
|
|
|
|
.dws.irq = DW_SPI3_IRQn,
|
|
|
|
.dws.index = 0,
|
|
|
|
.device_name = "spi3",
|
|
|
|
},
|
|
|
|
#endif /* BSP_USING_SPI3 */
|
2024-03-31 20:28:24 +08:00
|
|
|
};
|
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
|
2024-03-31 20:28:24 +08:00
|
|
|
{
|
2024-06-02 22:09:11 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
RT_ASSERT(device->bus != RT_NULL);
|
|
|
|
RT_ASSERT(device->bus->parent.user_data != RT_NULL);
|
|
|
|
RT_ASSERT(cfg != RT_NULL);
|
|
|
|
|
|
|
|
rt_err_t ret = RT_EOK;
|
|
|
|
struct _device_spi *spi = (struct _device_spi *)device->bus->parent.user_data;
|
|
|
|
struct dw_spi *dws = &spi->dws;
|
|
|
|
|
|
|
|
rt_uint8_t mode;
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
LOG_D("spi_configure input");
|
|
|
|
|
|
|
|
/* set cs low when spi idle */
|
|
|
|
writel(0, (void *)0x030001d0);
|
|
|
|
|
|
|
|
if (cfg->mode & RT_SPI_SLAVE)
|
2024-03-31 20:28:24 +08:00
|
|
|
{
|
2024-06-02 22:09:11 +08:00
|
|
|
LOG_E("invalid mode: %d", cfg->mode);
|
|
|
|
return -RT_EINVAL;
|
2024-03-31 20:28:24 +08:00
|
|
|
}
|
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
spi_reset_chip(dws);
|
|
|
|
spi_hw_init(dws);
|
|
|
|
spi_enable_chip(dws, 0);
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
LOG_D("cfg->max_hz: %d", cfg->max_hz);
|
|
|
|
dw_spi_set_clock(dws, SPI_REF_CLK, cfg->max_hz);
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
LOG_D("cfg->data_width: %d", cfg->data_width);
|
|
|
|
if (dw_spi_set_data_frame_len(dws, (uint32_t)cfg->data_width) < 0)
|
|
|
|
{
|
|
|
|
LOG_E("dw_spi_set_data_frame_len failed...\n");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
LOG_D("cfg->mode: %08x", cfg->mode);
|
|
|
|
switch (cfg->mode & RT_SPI_MODE_3)
|
|
|
|
{
|
|
|
|
case RT_SPI_MODE_0:
|
|
|
|
mode = SPI_FORMAT_CPOL0_CPHA0;
|
|
|
|
break;
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
case RT_SPI_MODE_1:
|
|
|
|
mode = SPI_FORMAT_CPOL0_CPHA1;
|
|
|
|
break;
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
case RT_SPI_MODE_2:
|
|
|
|
mode = SPI_FORMAT_CPOL1_CPHA0;
|
|
|
|
break;
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
case RT_SPI_MODE_3:
|
|
|
|
mode = SPI_FORMAT_CPOL1_CPHA1;
|
|
|
|
break;
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
default:
|
|
|
|
LOG_E("spi configure mode error %x\n", cfg->mode);
|
|
|
|
break;
|
|
|
|
}
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
dw_spi_set_polarity_and_phase(dws, mode);
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
dw_spi_set_cs(dws, 1, 0);
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
spi_enable_chip(dws, 1);
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
return RT_EOK;
|
2024-03-31 20:28:24 +08:00
|
|
|
}
|
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
static rt_err_t dw_spi_transfer_one(struct dw_spi *dws, const void *tx_buf, void *rx_buf, uint32_t len, enum transfer_type tran_type)
|
|
|
|
{
|
|
|
|
uint8_t imask = 0;
|
|
|
|
uint16_t txlevel = 0;
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
dws->tx = NULL;
|
|
|
|
dws->tx_end = NULL;
|
|
|
|
dws->rx = NULL;
|
|
|
|
dws->rx_end = NULL;
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
if (tx_buf != NULL) {
|
|
|
|
dws->tx = tx_buf;
|
|
|
|
dws->tx_end = dws->tx + len;
|
2024-03-31 20:28:24 +08:00
|
|
|
}
|
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
if (rx_buf != NULL) {
|
|
|
|
dws->rx = rx_buf;
|
|
|
|
dws->rx_end = dws->rx + len;
|
|
|
|
}
|
|
|
|
|
|
|
|
dws->rx_len = len / dws->n_bytes;
|
|
|
|
dws->tx_len = len / dws->n_bytes;
|
|
|
|
|
|
|
|
spi_enable_chip(dws, 0);
|
|
|
|
|
|
|
|
/* For poll mode just disable all interrupts */
|
|
|
|
spi_mask_intr(dws, 0xff);
|
|
|
|
|
|
|
|
/* set tran mode */
|
|
|
|
set_tran_mode(dws);
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
/* cs0 */
|
|
|
|
dw_spi_set_cs(dws, true, 0);
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
/* enable spi */
|
|
|
|
spi_enable_chip(dws, 1);
|
|
|
|
|
|
|
|
rt_hw_us_delay(10);
|
|
|
|
|
|
|
|
if (tran_type == POLL_TRAN)
|
|
|
|
{
|
|
|
|
if (poll_transfer(dws) < 0)
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
else
|
2024-03-31 20:28:24 +08:00
|
|
|
{
|
2024-06-02 22:09:11 +08:00
|
|
|
return -RT_ENOSYS;
|
2024-03-31 20:28:24 +08:00
|
|
|
}
|
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
return RT_EOK;
|
2024-03-31 20:28:24 +08:00
|
|
|
}
|
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
static rt_ssize_t spi_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
|
|
|
|
{
|
2024-03-31 20:28:24 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
RT_ASSERT(device->bus != RT_NULL);
|
2024-06-02 22:09:11 +08:00
|
|
|
RT_ASSERT(device->bus->parent.user_data != RT_NULL);
|
2024-03-31 20:28:24 +08:00
|
|
|
RT_ASSERT(message != RT_NULL);
|
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
struct _device_spi *spi = (struct _device_spi *)device->bus->parent.user_data;
|
|
|
|
struct dw_spi *dws = &spi->dws;
|
|
|
|
int32_t ret = 0;
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
if (message->send_buf && message->recv_buf)
|
2024-03-31 20:28:24 +08:00
|
|
|
{
|
2024-06-02 22:09:11 +08:00
|
|
|
ret = dw_spi_transfer_one(dws, message->send_buf, message->recv_buf, message->length, POLL_TRAN);
|
2024-03-31 20:28:24 +08:00
|
|
|
|
|
|
|
}
|
2024-06-02 22:09:11 +08:00
|
|
|
else if (message->send_buf)
|
2024-03-31 20:28:24 +08:00
|
|
|
{
|
2024-06-02 22:09:11 +08:00
|
|
|
ret = dw_spi_transfer_one(dws, message->send_buf, RT_NULL, message->length, POLL_TRAN);
|
2024-03-31 20:28:24 +08:00
|
|
|
|
|
|
|
}
|
2024-06-02 22:09:11 +08:00
|
|
|
else if (message->recv_buf)
|
2024-03-31 20:28:24 +08:00
|
|
|
{
|
2024-06-02 22:09:11 +08:00
|
|
|
ret = dw_spi_transfer_one(dws, RT_NULL, message->recv_buf, message->length, POLL_TRAN);
|
2024-03-31 20:28:24 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
return message->length;
|
2024-03-31 20:28:24 +08:00
|
|
|
}
|
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
static const struct rt_spi_ops _spi_ops =
|
2024-03-31 20:28:24 +08:00
|
|
|
{
|
2024-06-02 22:09:11 +08:00
|
|
|
.configure = spi_configure,
|
|
|
|
.xfer = spi_xfer,
|
2024-03-31 20:28:24 +08:00
|
|
|
};
|
|
|
|
|
2024-05-28 18:45:53 +08:00
|
|
|
#if defined(BOARD_TYPE_MILKV_DUO) || defined(BOARD_TYPE_MILKV_DUO_SPINOR) || defined(BOARD_TYPE_MILKV_DUO256M) || defined(BOARD_TYPE_MILKV_DUO256M_SPINOR)
|
2024-06-02 22:09:11 +08:00
|
|
|
// For Duo / Duo 256m, only SPI2 are exported on board.
|
2024-05-28 18:45:53 +08:00
|
|
|
#ifdef BSP_USING_SPI0
|
|
|
|
static const char *pinname_whitelist_spi0_sck[] = {
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
static const char *pinname_whitelist_spi0_sdo[] = {
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
static const char *pinname_whitelist_spi0_sdi[] = {
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
static const char *pinname_whitelist_spi0_cs[] = {
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSP_USING_SPI1
|
|
|
|
static const char *pinname_whitelist_spi1_sck[] = {
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
static const char *pinname_whitelist_spi1_sdo[] = {
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
static const char *pinname_whitelist_spi1_sdi[] = {
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
static const char *pinname_whitelist_spi1_cs[] = {
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSP_USING_SPI2
|
|
|
|
static const char *pinname_whitelist_spi2_sck[] = {
|
|
|
|
"SD1_CLK",
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
static const char *pinname_whitelist_spi2_sdo[] = {
|
|
|
|
"SD1_CMD",
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
static const char *pinname_whitelist_spi2_sdi[] = {
|
|
|
|
"SD1_D0",
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
static const char *pinname_whitelist_spi2_cs[] = {
|
|
|
|
"SD1_D3",
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSP_USING_SPI3
|
|
|
|
static const char *pinname_whitelist_spi3_sck[] = {
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
static const char *pinname_whitelist_spi3_sdo[] = {
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
static const char *pinname_whitelist_spi3_sdi[] = {
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
static const char *pinname_whitelist_spi3_cs[] = {
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
#error "Unsupported board type!"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void rt_hw_spi_pinmux_config()
|
|
|
|
{
|
|
|
|
#ifdef BSP_USING_SPI0
|
2024-06-02 22:09:11 +08:00
|
|
|
pinmux_config(BSP_SPI0_SCK_PINNAME, SPI0_SCK, pinname_whitelist_spi0_sck);
|
|
|
|
pinmux_config(BSP_SPI0_SDO_PINNAME, SPI0_SDO, pinname_whitelist_spi0_sdo);
|
|
|
|
pinmux_config(BSP_SPI0_SDI_PINNAME, SPI0_SDI, pinname_whitelist_spi0_sdi);
|
|
|
|
pinmux_config(BSP_SPI0_CS_PINNAME, SPI0_CS_X, pinname_whitelist_spi0_cs);
|
2024-05-28 18:45:53 +08:00
|
|
|
#endif /* BSP_USING_SPI0 */
|
|
|
|
|
|
|
|
#ifdef BSP_USING_SPI1
|
2024-06-02 22:09:11 +08:00
|
|
|
pinmux_config(BSP_SPI1_SCK_PINNAME, SPI1_SCK, pinname_whitelist_spi1_sck);
|
|
|
|
pinmux_config(BSP_SPI1_SDO_PINNAME, SPI1_SDO, pinname_whitelist_spi1_sdo);
|
|
|
|
pinmux_config(BSP_SPI1_SDI_PINNAME, SPI1_SDI, pinname_whitelist_spi1_sdi);
|
|
|
|
pinmux_config(BSP_SPI1_CS_PINNAME, SPI1_CS_X, pinname_whitelist_spi1_cs);
|
2024-05-28 18:45:53 +08:00
|
|
|
#endif /* BSP_USING_SPI1 */
|
|
|
|
|
|
|
|
#ifdef BSP_USING_SPI2
|
2024-06-02 22:09:11 +08:00
|
|
|
pinmux_config(BSP_SPI2_SCK_PINNAME, SPI2_SCK, pinname_whitelist_spi2_sck);
|
|
|
|
pinmux_config(BSP_SPI2_SDO_PINNAME, SPI2_SDO, pinname_whitelist_spi2_sdo);
|
|
|
|
pinmux_config(BSP_SPI2_SDI_PINNAME, SPI2_SDI, pinname_whitelist_spi2_sdi);
|
|
|
|
pinmux_config(BSP_SPI2_CS_PINNAME, SPI2_CS_X, pinname_whitelist_spi2_cs);
|
2024-05-28 18:45:53 +08:00
|
|
|
#endif /* BSP_USING_SPI2 */
|
|
|
|
|
|
|
|
#ifdef BSP_USING_SPI3
|
2024-06-02 22:09:11 +08:00
|
|
|
pinmux_config(BSP_SPI3_SCK_PINNAME, SPI3_SCK, pinname_whitelist_spi3_sck);
|
|
|
|
pinmux_config(BSP_SPI3_SDO_PINNAME, SPI3_SDO, pinname_whitelist_spi3_sdo);
|
|
|
|
pinmux_config(BSP_SPI3_SDI_PINNAME, SPI3_SDI, pinname_whitelist_spi3_sdi);
|
|
|
|
pinmux_config(BSP_SPI3_CS_PINNAME, SPI3_CS_X, pinname_whitelist_spi3_cs);
|
2024-05-28 18:45:53 +08:00
|
|
|
#endif /* BSP_USING_SPI3 */
|
|
|
|
}
|
|
|
|
|
2024-03-31 20:28:24 +08:00
|
|
|
int rt_hw_spi_init(void)
|
|
|
|
{
|
|
|
|
rt_err_t ret = RT_EOK;
|
2024-06-02 22:09:11 +08:00
|
|
|
struct dw_spi *dws;
|
2024-03-31 20:28:24 +08:00
|
|
|
|
2024-05-28 18:45:53 +08:00
|
|
|
rt_hw_spi_pinmux_config();
|
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
for (rt_size_t i = 0; i < sizeof(_spi_obj) / sizeof(struct _device_spi); i++)
|
|
|
|
{
|
|
|
|
_spi_obj[i].spi_bus.parent.user_data = (void *)&_spi_obj[i];
|
|
|
|
ret = rt_spi_bus_register(&_spi_obj[i].spi_bus, _spi_obj[i].device_name, &_spi_ops);
|
2024-03-31 20:28:24 +08:00
|
|
|
}
|
|
|
|
|
2024-06-02 22:09:11 +08:00
|
|
|
RT_ASSERT(ret == RT_EOK);
|
|
|
|
|
2024-03-31 20:28:24 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2024-07-04 10:07:14 +08:00
|
|
|
INIT_DEVICE_EXPORT(rt_hw_spi_init);
|