随着RTT版本不断的更新,RTT已经支持越来越多的组件了,spi当然也不例外。spi子系统的核心部分已经实现好了,我们只需要添加和硬件相关的部分即可。如果不熟悉可以在其他bsp下查找相关的驱动,模仿即可。
由于我用的是GD25Q16,所以可以直接使用w25qxx的驱动。
需要在rtconfig中打开RT_USING_SPI。
从w25qxx_init中可以看出需要绑定一个spi device,所以我们需要注册一个spi device。
spi device需要绑定一个spi bus/总线,所以我们需要先注册一个spi 总线。spi总线的注册比较简单,spi_core.c里提供了一个注册接口rt_spi_bus_register。
rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus, const char *name, const struct rt_spi_ops *ops);
我们这里先看一下struct rt_spi_ops的结构:
struct rt_spi_ops
{
rt_err_t (*configure)(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
rt_uint32_t (*xfer)(struct rt_spi_device *device, struct rt_spi_message *message);
};
configure里的参数configuration的结构:
struct rt_spi_configuration
{
rt_uint8_t mode;
rt_uint8_t data_width;
rt_uint16_t reserved;
rt_uint32_t max_hz;
};
可以看出,configure是用来设备spi的模式和位宽、频率的。
另外一个函数xfer,从名字就可以看出来是spi的收发接口。xfer是transfer的缩写。
由于设置spi需要知道是哪一个spi,所以可以将这个保存在rt_spi_device的user_data中。在xfer中需要使能相应的spi设备,所以可以将片选Io也保存到user_data中。
struct stm32_spi {
SPI_TypeDef * spi;
int cs_pin;
};
我们完成configure和xfer就可以注册bus了,代码就贴在最后了。
总线注册完就可以注册具体的spi设备,注册spi设备主要的功能是将设备加入总线中,而具体的设备实际上就是代表哪个CS pin/片选脚。rtt中判定总线的接口是rt_spi_bus_attach_device,我们将具体的spi和cs pin保存在user_data中。
板子的64pin的,spi cs pin是PA4,所以cs_pin为20,不要选错了。
#include
#include "stm32f10x_spi_bus.h"
#include
#include
#include
#ifdef RT_USING_SPI
#define SPI1_DEV1_CS_PIN 20
struct stm32_spi {
SPI_TypeDef * spi;
int cs_pin;
};
static struct rt_spi_bus spi1_bus;
static struct stm32_spi spi1_1;
static struct rt_spi_device spi1_dev1;
rt_inline uint16_t get_spi_BaudRatePrescaler(rt_uint32_t max_hz)
{
uint16_t SPI_BaudRatePrescaler;
/* STM32F10x SPI MAX 18Mhz */
if(max_hz >= SystemCoreClock/2 && SystemCoreClock/2 <= 36000000)
{
SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
}
else if(max_hz >= SystemCoreClock/4)
{
SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
}
else if(max_hz >= SystemCoreClock/8)
{
SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
}
else if(max_hz >= SystemCoreClock/16)
{
SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
}
else if(max_hz >= SystemCoreClock/32)
{
SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
}
else if(max_hz >= SystemCoreClock/64)
{
SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
}
else if(max_hz >= SystemCoreClock/128)
{
SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
}
else
{
/* min prescaler 256 */
SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
}
return SPI_BaudRatePrescaler;
}
// rt_err_t (*configure)(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
static rt_err_t stm32f10x_spi_cfg(struct rt_spi_device *device, struct rt_spi_configuration *configuration)
{
struct stm32_spi *spi;
SPI_InitTypeDef SPI_InitStructure;
SPI_StructInit(&SPI_InitStructure);
spi = device->parent.user_data;
// rt_kprintf("spi : %x, cs : %d\n", spi->spi, spi->cs_pin);
/* data_width */
if(device->config.data_width <= 8)
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
else if(device->config.data_width <= 16)
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
else
return -RT_EIO;
/* baudrate */
SPI_InitStructure.SPI_BaudRatePrescaler = get_spi_BaudRatePrescaler(configuration->max_hz);
/* CPOL */
if(device->config.mode & RT_SPI_CPOL)
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
else
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
/* CPHA */
if(device->config.mode & RT_SPI_CPHA)
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
else
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
/* MSB or LSB */
if(device->config.mode & RT_SPI_MSB)
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
else
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
/* init SPI */
SPI_I2S_DeInit(spi->spi);
SPI_Init(spi->spi, &SPI_InitStructure);
/* Enable SPI_MASTER */
SPI_Cmd(spi->spi, ENABLE);
SPI_CalculateCRC(spi->spi, DISABLE);
return RT_EOK;
}
// rt_uint32_t (*xfer)(struct rt_spi_device *device, struct rt_spi_message *message);
static rt_uint32_t stm3210x_spi_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
{
struct stm32_spi *spi;
rt_uint32_t size = message->length;
spi = (struct stm32_spi *)device->parent.user_data;
//chip select
if (message->cs_take)
rt_pin_write(spi->cs_pin, 0);
if (device->config.data_width <= 8)
{
const rt_uint8_t * send_ptr = message->send_buf;
rt_uint8_t * recv_ptr = message->recv_buf;
while(size--) {
rt_uint8_t data = 0xFF;
if(send_ptr != RT_NULL)
data = *send_ptr++;
/*!< Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(spi->spi, SPI_I2S_FLAG_TXE) == RESET);
/*!< Send byte through the SPI1 peripheral */
SPI_I2S_SendData(spi->spi, data);
/*!< Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(spi->spi, SPI_I2S_FLAG_RXNE) == RESET);
/*!< Return the byte read from the SPI bus */
data = SPI_I2S_ReceiveData(spi->spi);
if(recv_ptr != RT_NULL)
*recv_ptr++ = data;
}
} else {
const rt_uint16_t * send_ptr = message->send_buf;
rt_uint16_t * recv_ptr = message->recv_buf;
while(size--) {
rt_uint16_t data = 0xFF;
if(send_ptr != RT_NULL)
data = *send_ptr++;
/*!< Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(spi->spi, SPI_I2S_FLAG_TXE) == RESET);
/*!< Send byte through the SPI1 peripheral */
SPI_I2S_SendData(spi->spi, data);
/*!< Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(spi->spi, SPI_I2S_FLAG_RXNE) == RESET);
/*!< Return the byte read from the SPI bus */
data = SPI_I2S_ReceiveData(spi->spi);
if(recv_ptr != RT_NULL)
*recv_ptr++ = data;
}
}
if (message->cs_release)
rt_pin_write(spi->cs_pin, 1);
return message->length;
}
static struct rt_spi_ops spi_ops = {
stm32f10x_spi_cfg,
stm3210x_spi_xfer
};
static rt_err_t stm32f10x_spi_bus_register(SPI_TypeDef * SPI, struct rt_spi_bus *bus,
const char *name, const struct rt_spi_ops *ops)
{
rt_err_t res = -RT_ERROR;
if (SPI == SPI1) {
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
spi1_1.spi = SPI1;
res = rt_spi_bus_register(bus, name, ops);
} else if (SPI == SPI2) {
} else {
}
return res;
}
static int stm32f10x_spi_dev_register(struct rt_spi_device *device,
const char *dev_name, const char *bus_name, int cs_pin)
{
rt_err_t res = -RT_ERROR;
spi1_1.cs_pin = cs_pin;
rt_pin_mode(cs_pin, PIN_MODE_OUTPUT);
rt_pin_write(cs_pin, 1);
res = rt_spi_bus_attach_device(device, dev_name, bus_name, &spi1_1);
return res;
}
#ifdef RT_USING_SPI1
int spi1_register(void)
{
rt_err_t res = -RT_ERROR;
if (stm32f10x_spi_bus_register(SPI1, &spi1_bus, "spi1", &spi_ops) == RT_EOK) {
res = stm32f10x_spi_dev_register(&spi1_dev1, "spi1_1", "spi1", SPI1_DEV1_CS_PIN);
}
return res;
}
INIT_DEVICE_EXPORT(spi1_register);
#endif //RT_USING_SPI1
#endif // RT_USING_SPI
最后初始化w25qxx_init,将设备名填写我们刚才注册的设备spi1_1即可。将w25qxx设置为文件系统的区分。
由于我spi flash是gd25q16的,ID和W25qxx不一样,会出现:
Manufacturers ID error!
只要把我们的厂商ID加入就行了。
到这里就搞定了,list_device看一下,可以看到我们注册的总线,总线设备和块设备。
发现文件系统初始化失败,找到打印这句话的地方。发现是块大小的问题。我们是4096,_MAX_SS默认是512,改一下就好了。
改完重新试一下,发现没有打印刚才的size问题了,但还是失败了。