在使用SPI设备时,通常的几个步骤,已经用过多次了,这次使用SPI设备比较简单,仅需要一个SPI写功能,加上几个GPIO输出功能就OK了,然后下面流程走:
1,绑定SPI总线,注册SPI设备,这里使用的是总线是spi3,SPI设备spi30,然后按下面方式绑定
ret = rt_hw_spi_device_attach("spi3", "spi30", SPI_HFPA_PORT, SPI_HFPA_CS_PIN);
2,在线程中调用操作
hd->spidev->parent.write(dev, 0, buf, 1);
3,然后编译没有问题,下载运行,出现错误:
(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex) assertion failed at function:rt_mutex_take, line number:680
4,按错误提示,进行mutex的相关初始化操作:
rt_mutex_init(&_hfpa_dev.spidev->bus->lock, "spi30_lock", RT_IPC_FLAG_FIFO);
然后这问题不出现了,但是又出现下面问题
(obj != object) assertion failed at function:rt_object_init, line number:328
这两个问题,困扰我一天,今天终于想明白了。原因是spidev->parent.write函数根本就没有和rt_spi_send关联上,调用时去对象列表里找,结果找到错误对象,根据上面断言,产生错误提示
5,问题的原因找到了,就好办了,在初始化时,在头文件里给出下面定义:
typedef struct rt_hfpa_device
{
struct rt_device device;/* The standard device */
struct rt_spi_device *spidev;
const struct rt_hfpa_ops *ops; /* The hfpa ops */
} *rt_hfpa_device_t;
struct rt_hfpa_ops
{
int (* hfpa_write)(rt_hfpa_device_t hdt, const uint8_t *buf, uint8_t len);
};
然后在.c执行文件给出实现方法并关联上:
static int hfpa_write(rt_hfpa_device_t hdt, const uint8_t *buf, uint8_t len)
{
return rt_spi_send(hdt->spidev, buf, len);
}
const static struct rt_hfpa_ops g_ops = {
.hfpa_write = hfpa_write,
};
6,在初始化函数中,加入进去,注意下面最后一行:
_hfpa_dev.device.type = RT_Device_Class_Miscellaneous;
_hfpa_dev.device.rx_indicate = RT_NULL;
_hfpa_dev.device.tx_complete = RT_NULL;
_hfpa_dev.device.user_data = RT_NULL;
_hfpa_dev.ops = &g_ops;
7,如无必要,可以把前面第4步,rt_mutex_init()注释掉,这里我关联的是定义的操作,没有关联到parent层的write操作,因为我将这个SPI总线设备和其他操作GPIO管脚包装成了一个设备,然后在测试线程中加入下面代码
hd->ops->hfpa_write(hd, buf, 1);
然后重新编译下载
OK,问题解决。