forked from xuos/xiuos
add softspi in xidatong-riscv64
This commit is contained in:
@@ -91,3 +91,19 @@ select RESOURCES_WDT
|
||||
if BSP_USING_WDT
|
||||
source "$BSP_DIR/third_party_driver/watchdog/Kconfig"
|
||||
endif
|
||||
|
||||
menuconfig BSP_USING_WDT
|
||||
bool "Using WATCHDOG device"
|
||||
default n
|
||||
select RESOURCES_WDT
|
||||
if BSP_USING_WDT
|
||||
source "$BSP_DIR/third_party_driver/watchdog/Kconfig"
|
||||
endif
|
||||
|
||||
menuconfig BSP_USING_SOFT_SPI
|
||||
bool "Using SOFT_SPI device"
|
||||
default n
|
||||
select RESOURCES_SOFT_SPI
|
||||
if BSP_USING_SOFT_SPI
|
||||
source "$BSP_DIR/third_party_driver/soft_spi/Kconfig"
|
||||
endif
|
||||
|
||||
@@ -48,4 +48,8 @@ ifeq ($(CONFIG_BSP_USING_WDT),y)
|
||||
SRC_DIR += watchdog
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_BSP_USING_SOFT_SPI),y)
|
||||
SRC_DIR += soft_spi
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
#ifndef CONNECT_TF_H
|
||||
#define CONNECT_TF_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int HwSoftSPIInit(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
if BSP_USING_SOFT_SPI
|
||||
config SOFT_SPI_BUS_NAME
|
||||
string "soft spi bus 1 name"
|
||||
default "soft_spi1_bus1"
|
||||
|
||||
config SOFT_SPI_DEVICE_NAME
|
||||
string "soft spi dev 1 name"
|
||||
default "soft_spi1_dev1"
|
||||
|
||||
config SOFT_SPI_DRV_NAME
|
||||
string "soft spi drv 1 name"
|
||||
default "soft_spi1_drv1"
|
||||
|
||||
config SOFT_SPI_SCK
|
||||
int "soft spi sck pin"
|
||||
default 26
|
||||
|
||||
config SOFT_SPI_MOSI
|
||||
int "soft spi mosi pin"
|
||||
default 27
|
||||
|
||||
config SOFT_SPI_MISO
|
||||
int "soft spi miso pin"
|
||||
default 25
|
||||
|
||||
config SOFT_SPI_CS0_PIN
|
||||
int "soft spi cs pin"
|
||||
default 28
|
||||
|
||||
config SOFT_SPI_DEVICE_SLAVE_ID
|
||||
int "soft spi slave id"
|
||||
default 0
|
||||
|
||||
config SOFT_SPI_CHIP_SELECT
|
||||
int "soft spi chip selected"
|
||||
default 0
|
||||
|
||||
config SOFT_SPI_CLK_DELAY
|
||||
int "clk in microsecond"
|
||||
default 0
|
||||
|
||||
endif
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
SRC_FILES := connect_soft_spi.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
+295
@@ -0,0 +1,295 @@
|
||||
#include <xizi.h>
|
||||
#include <device.h>
|
||||
#include <fpioa.h>
|
||||
#include <gpiohs.h>
|
||||
#include "drv_io_config.h"
|
||||
#include <plic.h>
|
||||
#include <utils.h>
|
||||
#include <connect_soft_spi.h>
|
||||
#include <sleep.h>
|
||||
|
||||
#include <sd_spi.h>
|
||||
#include <dev_spi.h>
|
||||
#include <bus_spi.h>
|
||||
|
||||
static x_err_t softSPIinit(struct SpiDriver *spi_drv, struct BusConfigureInfo *cfg)
|
||||
{
|
||||
NULL_PARAM_CHECK(spi_drv);
|
||||
NULL_PARAM_CHECK(cfg);
|
||||
|
||||
// mode CPOL = 0 CPHA = 0
|
||||
gpiohs_set_drive_mode(SOFT_SPI_CS0_PIN, GPIO_DM_OUTPUT);
|
||||
gpiohs_set_pin(SOFT_SPI_CS0_PIN, GPIO_PV_HIGH); // set the cs gpio high
|
||||
gpiohs_set_drive_mode(SOFT_SPI_SCK, GPIO_DM_OUTPUT);
|
||||
gpiohs_set_drive_mode(SOFT_SPI_MOSI, GPIO_DM_OUTPUT);
|
||||
gpiohs_set_drive_mode(SOFT_SPI_MISO, GPIO_DM_INPUT);
|
||||
gpiohs_set_pin(SOFT_SPI_SCK, GPIO_PV_LOW);
|
||||
KPrintf("%s init done\n", SOFT_SPI_BUS_NAME);
|
||||
|
||||
return EOK;
|
||||
}
|
||||
|
||||
static uint32 softSpiDrvConfigure(void *drv, struct BusConfigureInfo *configure_info)
|
||||
{
|
||||
NULL_PARAM_CHECK(drv);
|
||||
NULL_PARAM_CHECK(configure_info);
|
||||
|
||||
x_err_t ret = EOK;
|
||||
struct SpiDriver *spi_drv = (struct SpiDriver *)drv;
|
||||
struct SpiMasterParam *spi_param;
|
||||
|
||||
switch (configure_info->configure_cmd)
|
||||
{
|
||||
case OPE_INT:
|
||||
softSPIinit(spi_drv, configure_info);
|
||||
break;
|
||||
|
||||
case OPE_CFG:
|
||||
break;
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void soft_spi_writebyte(struct SpiHardwareDevice *spi_dev, uint8_t data)
|
||||
{
|
||||
int8_t i = 0;
|
||||
uint8_t temp = 0;
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
temp = ((data & 0x80) == 0x80) ? 1 : 0;
|
||||
data = data << 1;
|
||||
gpiohs_set_pin(SOFT_SPI_SCK, GPIO_PV_LOW);
|
||||
usleep(SOFT_SPI_CLK_DELAY);
|
||||
if (0 == temp)
|
||||
{
|
||||
gpiohs_set_pin(SOFT_SPI_MOSI, GPIO_PV_LOW);
|
||||
}
|
||||
else
|
||||
{
|
||||
gpiohs_set_pin(SOFT_SPI_MOSI, GPIO_PV_HIGH);
|
||||
}
|
||||
gpiohs_set_pin(SOFT_SPI_SCK, GPIO_PV_HIGH);
|
||||
usleep(SOFT_SPI_CLK_DELAY);
|
||||
}
|
||||
gpiohs_set_pin(SOFT_SPI_SCK, GPIO_PV_LOW);
|
||||
}
|
||||
|
||||
/* 读一个字节 */
|
||||
static uint8_t soft_spi_readbyte(struct SpiHardwareDevice *spi_dev)
|
||||
{
|
||||
uint8_t i = 0;
|
||||
uint8_t read_data = 0xFF;
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
read_data = read_data << 1;
|
||||
gpiohs_set_pin(SOFT_SPI_SCK, GPIO_PV_LOW);
|
||||
usleep(SOFT_SPI_CLK_DELAY);
|
||||
gpiohs_set_pin(SOFT_SPI_SCK, GPIO_PV_HIGH);
|
||||
usleep(SOFT_SPI_CLK_DELAY);
|
||||
if (1 == gpiohs_get_pin(SOFT_SPI_MISO))
|
||||
{
|
||||
read_data = read_data | 0x01;
|
||||
}
|
||||
}
|
||||
return read_data;
|
||||
}
|
||||
|
||||
/* 读写一个字节 */
|
||||
// this funcition is unverify until now!
|
||||
static uint8_t soft_spi_readwritebyte(struct SpiHardwareDevice *spi_dev, uint8_t data)
|
||||
{
|
||||
uint8_t i = 0;
|
||||
uint8_t temp = 0;
|
||||
uint8_t read_data = 0xFF;
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
temp = ((data & 0x80) == 0x80) ? 1 : 0;
|
||||
data = data << 1;
|
||||
read_data = read_data << 1;
|
||||
if (temp == 0)
|
||||
{
|
||||
gpiohs_set_pin(SOFT_SPI_MOSI, GPIO_PV_LOW);
|
||||
}
|
||||
else
|
||||
{
|
||||
gpiohs_set_pin(SOFT_SPI_MOSI, GPIO_PV_HIGH);
|
||||
}
|
||||
usleep(SOFT_SPI_CLK_DELAY);
|
||||
gpiohs_set_pin(SOFT_SPI_SCK, GPIO_PV_HIGH);
|
||||
usleep(SOFT_SPI_CLK_DELAY);
|
||||
if (gpiohs_get_pin(SOFT_SPI_MISO) == 1)
|
||||
{
|
||||
read_data = read_data + 1;
|
||||
}
|
||||
}
|
||||
return read_data;
|
||||
}
|
||||
|
||||
static uint32 softSpiWriteData(struct SpiHardwareDevice *spi_dev, struct SpiDataStandard *spi_datacfg)
|
||||
{
|
||||
SpiDeviceParam *dev_param = (SpiDeviceParam *)(spi_dev->haldev.private_data);
|
||||
|
||||
uint8 cs_gpio_pin = dev_param->spi_slave_param->spi_cs_gpio_pin;
|
||||
const uint8_t *data_buff = spi_datacfg->tx_buff;
|
||||
int data_length = spi_datacfg->length;
|
||||
if (NONE == spi_datacfg->tx_buff)
|
||||
{
|
||||
data_length = 0;
|
||||
}
|
||||
|
||||
if (spi_datacfg->spi_chip_select)
|
||||
{
|
||||
gpiohs_set_pin(cs_gpio_pin, GPIO_PV_LOW);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < data_length; i++)
|
||||
{
|
||||
soft_spi_writebyte(spi_dev, data_buff[i]);
|
||||
}
|
||||
|
||||
if (spi_datacfg->spi_cs_release)
|
||||
{
|
||||
gpiohs_set_pin(cs_gpio_pin, GPIO_PV_HIGH);
|
||||
}
|
||||
spi_datacfg = spi_datacfg->next;
|
||||
|
||||
return EOK;
|
||||
}
|
||||
|
||||
static uint32 softSpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiDataStandard *spi_datacfg)
|
||||
{
|
||||
SpiDeviceParam *dev_param = (SpiDeviceParam *)(spi_dev->haldev.private_data);
|
||||
uint8 cs_gpio_pin = dev_param->spi_slave_param->spi_cs_gpio_pin;
|
||||
uint8_t *recv_buff = spi_datacfg->rx_buff;
|
||||
int recv_length = spi_datacfg->length;
|
||||
|
||||
if (NONE == spi_datacfg->rx_buff)
|
||||
{
|
||||
recv_length = 0;
|
||||
}
|
||||
|
||||
if (spi_datacfg->spi_chip_select)
|
||||
{
|
||||
gpiohs_set_pin(cs_gpio_pin, GPIO_PV_LOW);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < recv_length; i++)
|
||||
{
|
||||
recv_buff[i] = soft_spi_readbyte(spi_dev);
|
||||
}
|
||||
|
||||
if (spi_datacfg->spi_cs_release)
|
||||
{
|
||||
gpiohs_set_pin(cs_gpio_pin, GPIO_PV_HIGH);
|
||||
}
|
||||
|
||||
spi_datacfg = spi_datacfg->next;
|
||||
|
||||
return spi_datacfg->length;
|
||||
}
|
||||
|
||||
const struct SpiDevDone soft_spi_dev_done = {
|
||||
.dev_close = NONE,
|
||||
.dev_open = NONE,
|
||||
.dev_read = softSpiReadData,
|
||||
.dev_write = softSpiWriteData};
|
||||
|
||||
static int BoardSoftSpiBusInit(struct SpiBus *spi_bus, struct SpiDriver *spi_driver)
|
||||
{
|
||||
x_err_t ret = EOK;
|
||||
|
||||
/*Init the spi bus */
|
||||
ret = SpiBusInit(spi_bus, SOFT_SPI_BUS_NAME);
|
||||
if (EOK != ret)
|
||||
{
|
||||
KPrintf("Board_Spi_init SpiBusInit error %d\n", ret);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/*Init the spi driver*/
|
||||
ret = SpiDriverInit(spi_driver, SOFT_SPI_DRV_NAME);
|
||||
if (EOK != ret)
|
||||
{
|
||||
KPrintf("Board_Spi_init SpiDriverInit error %d\n", ret);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/*Attach the spi driver to the spi bus*/
|
||||
ret = SpiDriverAttachToBus(SOFT_SPI_DRV_NAME, SOFT_SPI_BUS_NAME);
|
||||
if (EOK != ret)
|
||||
{
|
||||
KPrintf("Board_Spi_init SpiDriverAttachToBus error %d\n", ret);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int BoardSoftSpiDevBend(void)
|
||||
{
|
||||
x_err_t ret = EOK;
|
||||
|
||||
static struct SpiHardwareDevice spi_device0;
|
||||
memset(&spi_device0, 0, sizeof(struct SpiHardwareDevice));
|
||||
|
||||
static struct SpiSlaveParam spi_slaveparam0;
|
||||
memset(&spi_slaveparam0, 0, sizeof(struct SpiSlaveParam));
|
||||
|
||||
spi_slaveparam0.spi_slave_id = SOFT_SPI_DEVICE_SLAVE_ID;
|
||||
spi_slaveparam0.spi_cs_gpio_pin = SOFT_SPI_CS0_PIN;
|
||||
spi_slaveparam0.spi_cs_select_id = SOFT_SPI_CHIP_SELECT;
|
||||
|
||||
spi_device0.spi_param.spi_dma_param = NONE;
|
||||
spi_device0.spi_param.spi_slave_param = &spi_slaveparam0;
|
||||
|
||||
spi_device0.spi_dev_done = &(soft_spi_dev_done);
|
||||
|
||||
ret = SpiDeviceRegister(&spi_device0, (void *)(&spi_device0.spi_param), SOFT_SPI_DEVICE_NAME);
|
||||
if (EOK != ret)
|
||||
{
|
||||
KPrintf("Board_Spi_init SpiDeviceInit device %s error %d\n", SOFT_SPI_DEVICE_NAME, ret);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
ret = SpiDeviceAttachToBus(SOFT_SPI_DEVICE_NAME, SOFT_SPI_BUS_NAME);
|
||||
if (EOK != ret)
|
||||
{
|
||||
KPrintf("Board_Spi_init SpiDeviceAttachToBus device %s error %d\n", SOFT_SPI_DEVICE_NAME, ret);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int HwSoftSPIInit(void)
|
||||
{
|
||||
x_err_t ret = EOK;
|
||||
|
||||
static struct SpiBus spi_bus;
|
||||
memset(&spi_bus, 0, sizeof(struct SpiBus));
|
||||
|
||||
static struct SpiDriver spi_driver;
|
||||
memset(&spi_driver, 0, sizeof(struct SpiDriver));
|
||||
|
||||
spi_driver.configure = &(softSpiDrvConfigure);
|
||||
|
||||
ret = BoardSoftSpiBusInit(&spi_bus, &spi_driver);
|
||||
if (EOK != ret)
|
||||
{
|
||||
KPrintf("Board_Spi_Init error ret %u\n", ret);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
ret = BoardSoftSpiDevBend();
|
||||
if (EOK != ret)
|
||||
{
|
||||
KPrintf("Board_Spi_Init error ret %u\n", ret);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user