RT-Thread nano 2.12 SPI驱动
2019-06-14 15:27:08 1 举报
AI智能生成
RT-Thread nano 2.12版SPI驱动的调用流程
作者其他创作
大纲/内容
RT-Thread nano 2.12版 SPI驱动
SPI虚拟总线框架
rt_components_board_init()
stm32_hw_spi_init()
spi_bus = &_spi_bus1;//spi bus句柄
struct rt_spi_bus *spi_bus;
spi = &_spi1;
数据类型:struct stm32_spi *spi;
SPI_TypeDef *Instance;
struct rt_spi_configuration *cfg;
spi->Instance = SPIx;
spi_bus->parent.user_data = spi;
struct rt_device *device;
device = &bus->parent;
device->type = RT_Device_Class_SPIBUS;
device->init = RT_NULL;
device->open = RT_NULL;
device->close = RT_NULL;
device->read = _spi_bus_device_read;
struct rt_spi_bus *bus;
bus = (struct rt_spi_bus *)dev;
device->write = _spi_bus_device_write;
与_spi_bus_device_read;操作类似
device->control = _spi_bus_device_control;
_spi_bus_device_control为空函数
bus->ops = ops;\t
ops为传入的stm_spi_ops
.configure = spi_configure
struct stm32_spi *hspi = (struct stm32_spi *)device->bus->parent.user_data;
hspi->cfg = configuration;
.xfer = spixfer
struct stm32_hw_spi_cs *cs = device->parent.user_data;
bus->owner = RT_NULL;
SPI设备驱动
rt_components_init()
rt_ad5791_init()
struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));//创建spi设备实例
struct rt_spi_device *spi_device
struct stm32_hw_spi_cs *cs_pin = (struct stm32_hw_spi_cs *)rt_malloc(sizeof(struct stm32_hw_spi_cs));//创建spi设备cs脚实例
cs_pin->pin = pin;
bus = rt_device_find(bus_name);
device->bus = (struct rt_spi_bus *)bus;//将spi设备挂入spi总线
device = &(dev->parent);
device->type = RT_Device_Class_SPIDevice;
device->read = _spidev_device_read;
struct rt_spi_device *device;
device = (struct rt_spi_device *)dev;
device->write = _spidev_device_write;
与_spidev_device_read;操作类似
device->control = _spidev_device_control;
_spidev_device_control;为空函数
device->parent.user_data = user_data;//将cs pin信息保存至spi设备的用户信息区
ad5791_register(\"ad5791\
struct rt_spi_device * rt_spi_device;
rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name);
spi_ad5791_device.rt_spi_device = rt_spi_device;//为da设备绑定spi设备,注意不是spi总线
struct spi_ad5791_device spi_ad5791_device
struct rt_spi_configuration cfg;cfg.data_width = 8;cfg.mode = RT_SPI_MODE_1 | RT_SPI_MSB; cfg.max_hz = 1 * 1000 * 1000; //SPI配置信息
device->config.data_width = cfg->data_width; device->config.mode = cfg->mode & RT_SPI_MODE_MASK ; device->config.max_hz = cfg->max_hz ;
rt_mutex_release(&(device->bus->lock));
spi_ad5791_device.da_device.type = RT_Device_Class_Char;
spi_ad5791_device.da_device.init = ad5791_init;
函数声明:rt_err_t ad5791_init(rt_device_t dev)
ad5791_lock(&spi_ad5791_device);
status = ad5791_GetRegisterValue(AD5791_REG_CTRL);
status = ad5791_GetRegisterValue(AD5791_REG_CTRL);
ad5791_unlock(&spi_ad5791_device);
spi_ad5791_device.da_device.open = ad5791_open;
spi_ad5791_device.da_device.close = ad5791_close;
static rt_err_t ad5791_close(rt_device_t dev)//空函数
spi_ad5791_device.da_device.read = ad5791_read;
ad5791_lock((struct spi_ad5791_device *)dev);
*((int*)buffer) = ad5791_GetRegisterValue(*((unsigned char*)pos));
ad5791_unlock((struct spi_ad5791_device *)dev);
spi_ad5791_device.da_device.write = ad5791_write;
uint32_t value = *((uint32_t*)buffer);
value = value << (MAX_RESOLUTION - ad5791_chip_info[act_device].resolution);
spi_ad5791_device.da_device.control = ad5791_control;
ad5791_lock((struct spi_ad5791_device *)dev);\t
\tad5791_unlock((struct spi_ad5791_device *)dev);
spi_ad5791_device.da_device.user_data = RT_NULL;
struct rt_spi_message message;
if (device->bus->owner != device)
device->bus->owner = device;
message.send_buf = send_buf; message.recv_buf = recv_buf; message.length = length; message.cs_take = 1; message.cs_release = 1; message.next = RT_NULL;
数据类型:struct rt_device
struct rt_object parent;
enum rt_device_class_type type;
rt_uint16_t flag;
rt_uint16_t open_flag;
rt_uint8_t ref_count;
rt_uint8_t device_id;
rt_err_t (*init) (rt_device_t dev);
rt_err_t (*close) (rt_device_t dev);
void *user_data;
数据类型:struct rt_spi_device
struct rt_device parent;
struct rt_spi_configuration config;
数据类型:struct rt_spi_bus
const struct rt_spi_ops *ops;
struct rt_mutex lock;\t
truct rt_spi_device *owner;
数据类型:struct spi_ad5791_device
struct rt_device da_device;
struct rt_device_blk_geometry geometry;
struct rt_mutex lock;
void * user_data;
0 条评论
回复 删除
下一页