RT-Thread nano 2.12 SPI驱动
2019-06-14 15:27:08 1 举报
AI智能生成
RT-Thread nano 2.12版SPI驱动的调用流程
作者其他创作
大纲/内容
SPI虚拟总线框架
rt_components_board_init()
stm32_hw_spi_init()
- stm32_spi_register_bus(SPI1, "spi1")
函数声明:int stm32_spi_register_bus(SPI_TypeDef *SPIx, const char *name)
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;
rt_spi_bus_register(spi_bus, name, &stm_spi_ops)
函数声明:rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
const char *name,
const struct rt_spi_ops *ops)
函数声明:rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
const char *name,
const struct rt_spi_ops *ops)
result = rt_spi_bus_device_init(bus, name);
函数声明:rt_err_t rt_spi_bus_device_init(struct rt_spi_bus *bus, const char *name)
函数声明:rt_err_t rt_spi_bus_device_init(struct rt_spi_bus *bus, const char *name)
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;
函数声明:static rt_size_t _spi_bus_device_read(rt_device_t dev,rt_off_t pos,void *buffer,rt_size_t size)
struct rt_spi_bus *bus;
bus = (struct rt_spi_bus *)dev;
rt_spi_transfer(bus->owner, RT_NULL, buffer, size);
device->write = _spi_bus_device_write;
与_spi_bus_device_read;操作类似
device->control = _spi_bus_device_control;
_spi_bus_device_control为空函数
rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);//将虚拟总线'spi1'注册进设备管理器中
rt_mutex_init(&(bus->lock), name, RT_IPC_FLAG_FIFO);//初始化spi总线互斥锁
bus->ops = ops;
ops为传入的stm_spi_ops
.configure = spi_configure
函数声明:rt_err_t spi_configure(struct rt_spi_device *device,struct rt_spi_configuration *configuration)
struct stm32_spi *hspi = (struct stm32_spi *)device->bus->parent.user_data;
hspi->cfg = configuration;
stm32_spi_init(hspi->Instance, configuration);
.xfer = spixfer
函数声明:rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
struct stm32_spi *hspi = (struct stm32_spi *)device->bus->parent.user_data;
struct stm32_hw_spi_cs *cs = device->parent.user_data;
rt_pin_write(cs->pin, 0);
res = spitxrx1b(hspi, rcvb, sndb);
SPISEND_1(&(hspi->Instance->DR), sndb, hspi->cfg->data_width);
SPIRECV_1(&(hspi->Instance->DR), rcvb, hspi->cfg->data_width);
bus->owner = RT_NULL;
SPI设备驱动
rt_components_init()
rt_ad5791_init()
stm32_spi_bus_attach_device(RT_AD5791_CS_PIN,"spi1", "spi10")
函数声明:rt_err_t stm32_spi_bus_attach_device(rt_uint32_t pin, const char *bus_name, const char *device_name)
函数声明:rt_err_t stm32_spi_bus_attach_device(rt_uint32_t pin, const char *bus_name, const char *device_name)
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;
rt_pin_mode(pin, PIN_MODE_OUTPUT);//设置CS脚为输出模式
rt_pin_write(pin, 1);
rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin)
函数声明:rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
const char *name,
const char *bus_name,
void *user_data)
函数声明:rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
const char *name,
const char *bus_name,
void *user_data)
bus = rt_device_find(bus_name);
device->bus = (struct rt_spi_bus *)bus;//将spi设备挂入spi总线
result = rt_spidev_device_init(device, name);
函数声明:rt_err_t rt_spidev_device_init(struct rt_spi_device *dev, const char *name)
函数声明:rt_err_t rt_spidev_device_init(struct rt_spi_device *dev, const char *name)
struct rt_device *device;
device = &(dev->parent);
device->type = RT_Device_Class_SPIDevice;
device->read = _spidev_device_read;
函数声明:static rt_size_t _spidev_device_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
struct rt_spi_device *device;
device = (struct rt_spi_device *)dev;
rt_spi_transfer(device, RT_NULL, buffer, size);
device->write = _spidev_device_write;
与_spidev_device_read;操作类似
device->control = _spidev_device_control;
_spidev_device_control;为空函数
rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);//注册spi设备
rt_memset(&device->config, 0, sizeof(device->config));
device->parent.user_data = user_data;//将cs pin信息保存至spi设备的用户信息区
ad5791_register("ad5791", "spi10");
函数声明:rt_err_t ad5791_register(const char * ad5791_device_name, const char * spi_device_name)
函数声明:rt_err_t ad5791_register(const char * ad5791_device_name, const char * spi_device_name)
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配置信息
cfg.data_width = 8;
cfg.mode = RT_SPI_MODE_1 | RT_SPI_MSB;
cfg.max_hz = 1 * 1000 * 1000;
//SPI配置信息
rt_spi_configure(spi_ad5791_device.rt_spi_device, &cfg);//配置spi
函数声明:rt_err_t rt_spi_configure(struct rt_spi_device *device,
struct rt_spi_configuration *cfg)
函数声明:rt_err_t rt_spi_configure(struct rt_spi_device *device,
struct rt_spi_configuration *cfg)
device->config.data_width = cfg->data_width;
device->config.mode = cfg->mode & RT_SPI_MODE_MASK ;
device->config.max_hz = cfg->max_hz ;
device->config.mode = cfg->mode & RT_SPI_MODE_MASK ;
device->config.max_hz = cfg->max_hz ;
result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
device->bus->ops->configure(device, &device->config);//配置spi寄存器
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)
rt_pin_mode(AD5791_RESET,PIN_MODE_OUTPUT);
rt_pin_write(AD5791_RESET,1);
rt_thread_delay(50);
rt_pin_write(AD5791_RESET,0);
rt_thread_delay(50);
rt_pin_write(AD5791_RESET,1);
rt_thread_delay(50);
rt_pin_write(AD5791_RESET,1);
rt_thread_delay(50);
rt_pin_write(AD5791_RESET,0);
rt_thread_delay(50);
rt_pin_write(AD5791_RESET,1);
rt_thread_delay(50);
ad5791_lock(&spi_ad5791_device);
status = ad5791_GetRegisterValue(AD5791_REG_CTRL);
rt_spi_send(spi_ad5791_device.rt_spi_device,registerWord,3);
rt_spi_transfer(device, send_buf, RT_NULL, length)
status = rt_spi_recv(spi_ad5791_device.rt_spi_device,receive_registerWord,3);
rt_spi_transfer(device, RT_NULL, recv_buf, length)
status = ad5791_SetRegisterValue(AD5791_REG_CTRL, newCtrl);
status = rt_spi_send(spi_ad5791_device.rt_spi_device, writeCommand, 3);
status = ad5791_GetRegisterValue(AD5791_REG_CTRL);
ad5791_unlock(&spi_ad5791_device);
spi_ad5791_device.da_device.open = ad5791_open;
rt_err_t ad5791_open(rt_device_t dev, rt_uint16_t oflag)//空函数
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);
ad5791_lock((struct spi_ad5791_device *)dev);
value = value << (MAX_RESOLUTION - ad5791_chip_info[act_device].resolution);
status = ad5791_SetRegisterValue(AD5791_REG_DAC, value);
ad5791_unlock((struct spi_ad5791_device *)dev);
spi_ad5791_device.da_device.control = ad5791_control;
ad5791_lock((struct spi_ad5791_device *)dev);
status = ad5791_GetRegisterValue(AD5791_REG_CTRL);
status = ad5791_SetRegisterValue(AD5791_REG_CTRL, newCtrl);
ad5791_unlock((struct spi_ad5791_device *)dev);
spi_ad5791_device.da_device.user_data = RT_NULL;
rt_device_register(&spi_ad5791_device.da_device, ad5791_device_name,RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
rt_size_t rt_spi_transfer(struct rt_spi_device *device,
const void *send_buf,
void *recv_buf,
rt_size_t length)//实际的传输函数
const void *send_buf,
void *recv_buf,
rt_size_t length)//实际的传输函数
struct rt_spi_message message;
result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
if (device->bus->owner != device)
result = device->bus->ops->configure(device, &device->config);
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;
message.recv_buf = recv_buf;
message.length = length;
message.cs_take = 1;
message.cs_release = 1;
message.next = RT_NULL;
result = device->bus->ops->xfer(device, &message);
rt_mutex_release(&(device->bus->lock));
数据类型: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 (*rx_indicate)(rt_device_t dev, rt_size_t size)
rt_err_t (*tx_complete)(rt_device_t dev, void *buffer)
rt_err_t (*init) (rt_device_t dev);
rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
rt_err_t (*close) (rt_device_t dev);
rt_size_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
rt_size_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
rt_err_t (*control)(rt_device_t dev, rt_uint8_t cmd, void *args);
void *user_data;
数据类型:struct rt_spi_device
struct rt_device parent;
struct rt_spi_bus *bus;
struct rt_spi_configuration config;
数据类型:struct rt_spi_bus
struct rt_device parent;
const struct rt_spi_ops *ops;
struct rt_mutex lock;
truct rt_spi_device *owner;
数据类型:struct spi_ad5791_device
struct rt_device da_device;
struct rt_device_blk_geometry geometry;
struct rt_spi_device * rt_spi_device;
struct rt_mutex lock;
void * user_data;
0 条评论
下一页