SPI communication

I am trying to implement a program that reads data from the onboard BMI160 accelerometer using the SPI communication interface. I used the test_spi.py example and the information provided here. The program runs and I can see some data, but it is not the sequence I expect (0x07, 0x30, 0x81, 0x0b, 0xc0). I receive a different sequence every time I run the program. The code is shown below and I am not sure if I am setting up correctly the parameters (MISO, MOSI, CS and SCKL), I just copied them from the examples. Any help will be appreciated.

def data_handler_imu(self, data):
    global g_data
    g_data = data
g_data = []
callback_imu = FnVoid_VoidP_DataP(data_handler_imu)
device = MetaWear(sys.argv[1])
#Read 5 bytes from the BMI160 starting at register 0x5A (INT_LOWHIGH), for reading SPI: 0x5A | 0x80 = 0xDA
extra_data= (c_ubyte * 1)(0xDA)
parameters= SpiParameters(mode = SpiMode._3, frequency = SpiFrequency._8MHz, data = extra_data, data_length = len(extra_data), slave_select_pin = 9, clock_pin = 0, mosi_pin = 11, miso_pin = 7, lsb_first = 0, use_nrf_pins = 1)
signal= libmetawear.mbl_mw_spi_get_data_signal(device.board, 5, 0xe)
libmetawear.mbl_mw_datasignal_subscribe(signal, None, callback_imu)
libmetawear.mbl_mw_datasignal_read_with_parameters(signal, byref(parameters))
sleep(1) # Wait for data


  • @ivoc

    The MetaWear firmware and APIs already have a fully implemented and featured driver for the BMI160. You should not ever need to access it via the SPI interface. If your goal is to work with the BMI160 you should absolutely use the APIs.

    As to why the example doesn't work -- my first guess is that the pins in the example are different than the pins for the MetaWear model you are using. All of the necessary information is detailed in the datasheet. My second guess is that the native driver is interfering, or there are some steps required before the register can be read from. You would have to consult the BMI160 datasheet to be certain.


Sign In or Register to comment.