通过 Raspberry Pi 的 SPI 读取/写入 LSM6DSOX

问题描述 投票:0回答:1

我在从运行 Ubuntu 20.04 的 Raspberry Pi 4 读取和写入 Adafruit LSM6DSOX IMU 时遇到问题。我需要通过 SPI 来完成此操作,因为我需要带宽,但我似乎只能成功读取 WHO_AM_I 寄存器。读/写任何其他寄存器仅返回 0x00。我已经验证我可以通过 SPI 从 Arduino 读取 IMU 数据,但如果我尝试读取 0x0F(IMU_ID)以外的寄存器,我会得到 0x0 作为响应。任何可能导致此问题的见解/想法将不胜感激!

编辑:事实证明我可以读取以下寄存器:

0x0f : 0x6c
0x13 : 0x1c
0x33 : 0x1c
0x53 : 0x1c
0x73 : 0x1c

然而,这些都是随机寄存器,并且值 0x1C 似乎与任何内容都不对应。

这是我的main.py:

import LSM6DSOX

def main():
    imu=LSM6DSOX.LSM6DSOX()
    imu.initSPI()
    whoamI=imu.read_reg(0x0F)
    while(whoamI != imu.LSM6DSOX_ID):
        imu.ms_sleep(200)
        print('searching for IMU')
        whoamI=imu.get_id()
        print(hex(whoamI))
    print('found lsm6dsox IMU')

    imu.spi.close()
    imu.spi = None

if __name__=="__main__":
    main()

这是我的 LSM6DSOX.py 的摘录:

    def initSPI(self):
        # Setup communication SPI
        self.spi = spidev.SpiDev()
        self.spi.open(0, 0)
        self.spi.mode=0b11 #mode 3, (mode 0 is also fine)
        self.spi.max_speed_hz = 500000
        return self.spi

    def read_reg(self, reg, len=1):
        # Set up message 
        buf = bytearray(len+1)
        buf[0] = 0b10000000 | reg # MSB bit must be 1 to indicate a read operation. this is OR'd with the register address you want to read

        resp =self.spi.xfer2(buf) #send (and recieve) data to the imu
        if len==1:
            return resp[1]
        else:
            return resp[1:] #display recieved data

    def write_reg(self, reg, data, len=1):
        # Set up message 
        buf = bytearray(len+1)
        buf[0] = 0b00000000 | reg # MSB bit must be 0 to indicate a read operation. this is OR'd with the register address you want to read
        buf[1:] =bytes(data)
        resp =self.spi.xfer2(buf) #send (and recieve) data to the imu
        return resp[1:] #display recieved data
raspberry-pi spi adafruit imu iio
1个回答
0
投票

您是否能够通过 Raspberry PI 使用 SPI 与 IMU 进行通信。我正在使用 spidev 并遇到了同样的问题,得到正确的 who_am_i 和零其他地方。如果您能帮助我,那将非常有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.