2021-09-21 14:56:40 +08:00
|
|
|
/*
|
2022-03-29 07:38:42 +08:00
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
2021-09-21 14:56:40 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2017-06-05 tanek first implementation.
|
2021-09-21 16:15:53 +08:00
|
|
|
* 2018-04-19 misonyo Porting for v85xxf30x
|
|
|
|
* 2019-03-31 xuzhuoyi Porting for v85xxe230
|
|
|
|
* 2021-09-21 zhuxw Porting for v85xx
|
2021-09-21 14:56:40 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "drv_spi.h"
|
|
|
|
#include "board.h"
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
|
|
|
#if defined(RT_USING_SPI) && defined(RT_USING_PIN)
|
|
|
|
#include <rtdevice.h>
|
|
|
|
|
2021-09-21 16:15:53 +08:00
|
|
|
#if !defined(RT_USING_SPI1) && !defined(RT_USING_SPI2)
|
2021-09-21 14:56:40 +08:00
|
|
|
#error "Please define at least one SPIx"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* private rt-thread spi ops function */
|
|
|
|
static rt_err_t configure(struct rt_spi_device* device, struct rt_spi_configuration* configuration);
|
|
|
|
static rt_uint32_t xfer(struct rt_spi_device* device, struct rt_spi_message* message);
|
|
|
|
|
2021-09-21 16:15:53 +08:00
|
|
|
static struct rt_spi_ops v85xx_spi_ops =
|
2021-09-21 14:56:40 +08:00
|
|
|
{
|
|
|
|
configure,
|
|
|
|
xfer
|
|
|
|
};
|
|
|
|
|
|
|
|
static rt_err_t configure(struct rt_spi_device* device, struct rt_spi_configuration* configuration)
|
|
|
|
{
|
2021-09-21 16:15:53 +08:00
|
|
|
SPI_InitType spi_init_struct;
|
2021-09-21 14:56:40 +08:00
|
|
|
|
|
|
|
rt_uint32_t spi_periph = (rt_uint32_t)device->bus->parent.user_data;
|
|
|
|
|
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
RT_ASSERT(configuration != RT_NULL);
|
|
|
|
|
2021-09-21 16:15:53 +08:00
|
|
|
if(configuration->data_width > 8)
|
2021-09-21 14:56:40 +08:00
|
|
|
{
|
2023-03-17 01:12:51 +08:00
|
|
|
return -RT_EIO;
|
2021-09-21 14:56:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
rt_uint32_t spi_apb_clock;
|
|
|
|
rt_uint32_t max_hz;
|
|
|
|
|
|
|
|
max_hz = configuration->max_hz;
|
|
|
|
|
2021-09-21 16:15:53 +08:00
|
|
|
spi_apb_clock = CLK_GetPCLKFreq();
|
2021-09-21 14:56:40 +08:00
|
|
|
|
|
|
|
if(max_hz >= spi_apb_clock/2)
|
|
|
|
{
|
2021-09-21 16:15:53 +08:00
|
|
|
spi_init_struct.ClockDivision = SPI_CLKDIV_2;
|
2021-09-21 14:56:40 +08:00
|
|
|
}
|
|
|
|
else if (max_hz >= spi_apb_clock/4)
|
|
|
|
{
|
2021-09-21 16:15:53 +08:00
|
|
|
spi_init_struct.ClockDivision = SPI_CLKDIV_4;
|
2021-09-21 14:56:40 +08:00
|
|
|
}
|
|
|
|
else if (max_hz >= spi_apb_clock/8)
|
|
|
|
{
|
2021-09-21 16:15:53 +08:00
|
|
|
spi_init_struct.ClockDivision = SPI_CLKDIV_8;
|
2021-09-21 14:56:40 +08:00
|
|
|
}
|
|
|
|
else if (max_hz >= spi_apb_clock/16)
|
|
|
|
{
|
2021-09-21 16:15:53 +08:00
|
|
|
spi_init_struct.ClockDivision = SPI_CLKDIV_16;
|
2021-09-21 14:56:40 +08:00
|
|
|
}
|
|
|
|
else if (max_hz >= spi_apb_clock/32)
|
|
|
|
{
|
2021-09-21 16:15:53 +08:00
|
|
|
spi_init_struct.ClockDivision = SPI_CLKDIV_32;
|
2021-09-21 14:56:40 +08:00
|
|
|
}
|
|
|
|
else if (max_hz >= spi_apb_clock/64)
|
|
|
|
{
|
2021-09-21 16:15:53 +08:00
|
|
|
spi_init_struct.ClockDivision = SPI_CLKDIV_64;
|
2021-09-21 14:56:40 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-21 16:15:53 +08:00
|
|
|
/* min prescaler 128 */
|
|
|
|
spi_init_struct.ClockDivision = SPI_CLKDIV_128;
|
2021-09-21 14:56:40 +08:00
|
|
|
}
|
|
|
|
} /* baudrate */
|
|
|
|
|
|
|
|
switch(configuration->mode & RT_SPI_MODE_3)
|
|
|
|
{
|
|
|
|
case RT_SPI_MODE_0:
|
2021-09-21 16:15:53 +08:00
|
|
|
spi_init_struct.SPH = SPI_SPH_0;
|
|
|
|
spi_init_struct.SPO = SPI_SPO_0;
|
2021-09-21 14:56:40 +08:00
|
|
|
break;
|
|
|
|
case RT_SPI_MODE_1:
|
2021-09-21 16:15:53 +08:00
|
|
|
spi_init_struct.SPH = SPI_SPH_1;
|
|
|
|
spi_init_struct.SPO = SPI_SPO_0;
|
2021-09-21 14:56:40 +08:00
|
|
|
break;
|
|
|
|
case RT_SPI_MODE_2:
|
2021-09-21 16:15:53 +08:00
|
|
|
spi_init_struct.SPH = SPI_SPH_0;
|
|
|
|
spi_init_struct.SPO = SPI_SPO_1;
|
2021-09-21 14:56:40 +08:00
|
|
|
break;
|
|
|
|
case RT_SPI_MODE_3:
|
2021-09-21 16:15:53 +08:00
|
|
|
spi_init_struct.SPH = SPI_SPH_1;
|
|
|
|
spi_init_struct.SPO = SPI_SPO_1;
|
2021-09-21 14:56:40 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-09-21 16:15:53 +08:00
|
|
|
if(!(configuration->mode & RT_SPI_MSB))
|
2021-09-21 14:56:40 +08:00
|
|
|
{
|
2023-03-17 01:12:51 +08:00
|
|
|
return -RT_EIO;
|
2021-09-21 14:56:40 +08:00
|
|
|
}
|
|
|
|
|
2021-09-21 16:15:53 +08:00
|
|
|
spi_init_struct.Mode = SPI_MODE_MASTER;
|
|
|
|
spi_init_struct.CSNSoft = SPI_CSNSOFT_ENABLE;
|
2021-09-21 14:56:40 +08:00
|
|
|
|
2021-09-21 16:15:53 +08:00
|
|
|
SPI_Init((SPI_TypeDef*)spi_periph, &spi_init_struct);
|
2021-09-21 14:56:40 +08:00
|
|
|
|
2021-09-21 16:15:53 +08:00
|
|
|
SPI_Cmd((SPI_TypeDef*)spi_periph, ENABLE);
|
2021-09-21 14:56:40 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
};
|
|
|
|
|
|
|
|
static rt_uint32_t xfer(struct rt_spi_device* device, struct rt_spi_message* message)
|
|
|
|
{
|
2021-09-21 16:15:53 +08:00
|
|
|
rt_base_t v85xx_cs_pin = (rt_base_t)device->parent.user_data;
|
2021-09-21 14:56:40 +08:00
|
|
|
rt_uint32_t spi_periph = (rt_uint32_t)device->bus->parent.user_data;
|
|
|
|
struct rt_spi_configuration * config = &device->config;
|
|
|
|
|
2021-09-26 14:13:38 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
2021-10-15 20:37:32 +08:00
|
|
|
RT_ASSERT(message != RT_NULL);
|
2021-09-21 14:56:40 +08:00
|
|
|
|
|
|
|
/* take CS */
|
|
|
|
if(message->cs_take)
|
|
|
|
{
|
2021-09-21 16:15:53 +08:00
|
|
|
rt_pin_write(v85xx_cs_pin, PIN_LOW);
|
2021-09-21 14:56:40 +08:00
|
|
|
DEBUG_PRINTF("spi take cs\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
if(config->data_width <= 8)
|
|
|
|
{
|
|
|
|
const rt_uint8_t * send_ptr = message->send_buf;
|
|
|
|
rt_uint8_t * recv_ptr = message->recv_buf;
|
|
|
|
rt_uint32_t size = message->length;
|
|
|
|
|
|
|
|
DEBUG_PRINTF("spi poll transfer start: %d\n", size);
|
|
|
|
|
|
|
|
while(size--)
|
|
|
|
{
|
|
|
|
rt_uint8_t data = 0xFF;
|
|
|
|
|
|
|
|
if(send_ptr != RT_NULL)
|
|
|
|
{
|
|
|
|
data = *send_ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Wait until the transmit buffer is empty
|
2021-09-21 16:15:53 +08:00
|
|
|
while(RESET == SPI_GetStatus((SPI_TypeDef*)spi_periph, SPI_STS_TXEMPTY));
|
2021-09-21 14:56:40 +08:00
|
|
|
// Send the byte
|
2021-09-21 16:15:53 +08:00
|
|
|
SPI_SendData((SPI_TypeDef*)spi_periph, data);
|
2021-09-21 14:56:40 +08:00
|
|
|
|
|
|
|
//Wait until a data is received
|
2021-09-21 16:15:53 +08:00
|
|
|
while(RESET == SPI_GetStatus((SPI_TypeDef*)spi_periph, SPI_STS_RNE));
|
2021-09-21 14:56:40 +08:00
|
|
|
// Get the received data
|
2021-09-21 16:15:53 +08:00
|
|
|
data = SPI_ReceiveData((SPI_TypeDef*)spi_periph);
|
2021-09-21 14:56:40 +08:00
|
|
|
|
|
|
|
if(recv_ptr != RT_NULL)
|
|
|
|
{
|
|
|
|
*recv_ptr++ = data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEBUG_PRINTF("spi poll transfer finsh\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* release CS */
|
|
|
|
if(message->cs_release)
|
|
|
|
{
|
2021-09-21 16:15:53 +08:00
|
|
|
rt_pin_write(v85xx_cs_pin, PIN_HIGH);
|
2021-09-21 14:56:40 +08:00
|
|
|
DEBUG_PRINTF("spi release cs\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return message->length;
|
|
|
|
};
|
|
|
|
|
2021-09-21 16:15:53 +08:00
|
|
|
int v85xx_hw_spi_init(void)
|
2021-09-21 14:56:40 +08:00
|
|
|
{
|
|
|
|
int result = 0;
|
2021-09-21 16:15:53 +08:00
|
|
|
#ifdef RT_USING_SPI1
|
2021-09-21 14:56:40 +08:00
|
|
|
static struct rt_spi_bus spi_bus0;
|
2021-09-21 16:15:53 +08:00
|
|
|
spi_bus0.parent.user_data = (void *)SPI1;
|
2021-09-21 14:56:40 +08:00
|
|
|
|
2021-09-21 16:15:53 +08:00
|
|
|
result = rt_spi_bus_register(&spi_bus0, "spi1", &v85xx_spi_ops);
|
2021-09-21 14:56:40 +08:00
|
|
|
|
2021-09-21 16:24:18 +08:00
|
|
|
#endif
|
2021-09-21 14:56:40 +08:00
|
|
|
|
2021-09-21 16:15:53 +08:00
|
|
|
#ifdef RT_USING_SPI2
|
2021-09-21 14:56:40 +08:00
|
|
|
static struct rt_spi_bus spi_bus1;
|
2021-09-21 16:15:53 +08:00
|
|
|
spi_bus1.parent.user_data = (void *)SPI2;
|
2021-09-21 14:56:40 +08:00
|
|
|
|
2021-09-21 16:15:53 +08:00
|
|
|
result = rt_spi_bus_register(&spi_bus1, "spi2", &v85xx_spi_ops);
|
2021-09-21 14:56:40 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
2021-09-21 16:15:53 +08:00
|
|
|
INIT_BOARD_EXPORT(v85xx_hw_spi_init);
|
2021-09-21 14:56:40 +08:00
|
|
|
#endif
|