Read problem with basic SPI driver

Hi, I’m about to make a driver for a SPI device.

I’m using a pixracer board and as a test I tried to read the device id (whoami) of the mpu9250.
The read_registers() operation goes fine but the value read is wrong.
The value of the whoami register should be 0x71, but i get 0x00.

Can you tell me if there’s something wrong in the following code?
(I write it looking at AP_InertialSensor)

AP_HAL::OwnPtr<AP_HAL::SPIDevice> _dev;
uint8_t *_buffer;
_dev = hal.spi->get_device("mpu9250");
if (!_dev) {
    hal.console->printf("DEV NOT FOUND");


if (!_dev->get_semaphore()->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
    hal.console->printf("SEMAPHORE ERROR");

_dev->setup_checked_registers(7, 20);


// random value because it reads 0 and wanted to be sure that it was the read to set it to 0
uint8_t id = 42;

// 0x75 = whoami reg addr
if(_dev->read_registers(0x75, _buffer, 1)){
    id = *_buffer;
    hal.console->printf("REGISTER READ");
    hal.console->printf("READ ERROR");

if(id == 0x71){ // 0x71 = default whoami reg value
    hal.console->printf("ID CHECKED \n ID = %02x", id);
    hal.console->printf("ID ERROR \n ID = %02x", id);

Thank you!

1 Like


Instead of using a pointer in _dev->read_registers(0x75, _buffer, 1) it’s instead needed to pass a reference to a uint8_t.

uint8_t buffer = 0;
_dev->read_registers(0x75, &_buffer, 1))