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

发布于 2025-02-10 14:28:57 字数 2002 浏览 3 评论 0原文

我很难从覆盆子Pi 4中阅读并写信给我的Adafruit LSM6DSOX IMU,运行Ubuntu 20.04。我需要通过SPI进行操作,因为我需要带宽,但是我似乎只能成功阅读WHO_AM_I注册。读取/写入任何其他寄存器仅返回0x00。我已经验证了我可以通过SPI从Arduino中读取IMU的数据,但是如果我尝试读取以外的0x0F(IMU_ID)以外的寄存器,我会得到0x0作为响应。任何洞察力/想法可能会引起这一点,将不胜感激!

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

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

这些都是随机寄存器,但是值0x1c似乎与任何内容都不相对。

这是我的主要.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

I'm having trouble reading and writing to my Adafruit LSM6DSOX IMU from my Raspberry Pi 4 running Ubuntu 20.04. I need to do it via SPI since I require the bandwidth, but I can only seem to read the WHO_AM_I register successfully. Reading/writing to any other register only returns 0x00. I have verified that I can read data off the IMU from an Arduino via SPI, but if I try to read a register other than 0x0F (the IMU_ID) I get 0x0 as a response. Any insight/ideas what could be causing this would be greatly appreciated!

EDIT: It turns out I can read the following registers:

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

These are all random registers however, and the value 0x1C doesn't seem to correspond with anything.

This is my 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()

This is an excerpt from my 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文