IOCTL 传输上的 EFAULT

问题描述

我一直在尝试与 BMP280 传感器通信,但无济于事,主要是由于 EFAULT。为此,我一直在使用 Bosch Sensortec's drivers,并且根据需要,我提供了自己的 SPI 读取例程实现。

但是,尝试从 reg_addr 规定的地址读取会导致 EFAULT/Bad Address 错误,因此结果数据大部分是垃圾。我一直在使用的代码,并评论了其他尝试如下:

/*!
 *  @brief Function for reading the sensor's registers through SPI bus.
 *
 *  @param[in] cs       : Chip select to enable the sensor.
 *  @param[in] reg_addr : Register address.
 *  @param[out] reg_data    : Pointer to the data buffer to store the read data.
 *  @param[in] length   : No of bytes to read.
 *
 *  @return Status of execution
 *  @retval 0 -> Success
 *  @retval >0 -> Failure Info
 *
 */
int8_t spi_reg_read(uint8_t cs,uint8_t reg_addr,uint8_t *reg_data,uint16_t length)
{
    pin_low(pin.header,pin.pin); //pin is CSB pin
    struct spi_ioc_transfer tr = {
                .tx_buf = (unsigned long)reg_addr,//also tried &reg_addr and casting it to char*
                .rx_buf = (unsigned long)reg_data,.len = length,.delay_usecs = 0,.speed_hz = 0,.bits_per_word = 0,};
    pin_high(pin.header,pin.pin);

    return ioctl(fd,SPI_IOC_MESSAGE(1),&tr);
}

SPI 总线的实例化和附加信息:

    fd = open("/dev/spidev0.0",O_RDWR);
    iolib_init();

    ioctl(fd,SPI_IOC_WR_MAX_SPEED_HZ,10000000); // also tested other frequencies
    ioctl(fd,SPI_IOC_RD_MAX_SPEED_HZ,10000000);
    ioctl(fd,SPI_IOC_RD_MODE,SPI_MODE_2); 
    ioctl(fd,SPI_IOC_WR_MODE,SPI_MODE_2); // also tried SPI_MODE_0,2 and 3

输出错误地址”(EFAULT)

读取值:0xb8 预期:第一个字节为 0x58

至于程序的其余部分,一切正常。使用类似的实现(IOCTL 和 SPI)与诺基亚 5110 显示器进行通信,第一次运行良好,唯一的区别是 tx_buf 使用了 char* 而不是我现在使用的解决方案。

我该如何修复这个特定错误

提前致谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...