This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

I2C internally using SPI to acquire mpu9250 geomagnetism

Hello

I am currently trying to get sensor values via SPI communication with mpu9250, a 9-axis sensor.

Although the following settings were made,acceleration,gyro,and temperature could be measured , but geomagnetics can not be measured.

Is the setting for acquiring geomagnetism with mpu9250 SPI correct?

#define mpu_6axis_sleve_address 0x68
#define read_six_axis 0xBB//read data
#define PWR_MGMT_1 0x6B
#define USER_CTRL 0x6A
#define AK8963_SLAVE_ADDR 0x03//mag
#define I2C_MST_CTRL 0x24
#define I2C_SLV0_ADDR 0x25
#define I2C_SLV0_REG 0x26
#define I2C_SLV0_CTRL 0x27
#define I2C_SLV0_DO 0x63
#define I2C_SLV4_ADDR 0x31
#define I2C_SLV4_REG 0x32
#define I2C_SLV4_DO 0x33
#define I2C_SLV4_CTRL 0x34
#define ACCEL_XOUT_H 0x3B
#define SMPLRT_DIV 0x19
#define GYRO_CONFIG 0x1B
#define ACCEL_CONFIG 0x1C
#define ACCEL_CONFIG2 0x1D


#define SPI_INSTANCE 1;
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);//SPI_INSTANCE     (spi1)
static volatile bool spi_xfer_done;  /**< Flag used to indicate that SPI instance completed the transfer. */

volatile static bool spi_tx_done = false;
void nrf_drv_mpu_spi_event_handler(const nrf_drv_spi_evt_t *evt, void * p_context)

{

    if(evt->type == NRF_DRV_SPI_EVENT_DONE)

    {

        spi_tx_done = true;

    }

    else

    {

        // Something is wrong

    }

}




void SPI_INIT()
{
    uint32_t err_code;
    
    nrf_drv_spi_config_t config = {
        .sck_pin = sensor_SCK_PIN,
        .mosi_pin = sensor_MOSI_PIN,
        .miso_pin = sensor_MISO_PIN,
        .ss_pin = sensor_CS_PIN,
        .frequency = NRF_DRV_SPI_FREQ_4M,
        .mode = NRF_DRV_SPI_MODE_0,
        .bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST
    };

    err_code = nrf_drv_spi_init(&spi,&config,nrf_drv_mpu_spi_event_handler,NULL);///////////////

}


void MPU9250_setMODE()
{
    uint8_t reg_chipreset[2] = {PWR_MGMT_1,0x80};
    nrf_drv_spi_transfer(&spi,reg_chipreset,sizeof(reg_chipreset),NULL,0);//chip reset
    nrf_delay_ms(100);

    uint8_t reg_wakeup[2] = {PWR_MGMT_1,0x01};
    nrf_drv_spi_transfer(&spi,reg_wakeup,sizeof(reg_wakeup),NULL,0);// wake up device

    uint8_t reg_userctrl[2] = {USER_CTRL,0x30};
    nrf_drv_spi_transfer(&spi,reg_userctrl,sizeof(reg_userctrl),NULL,0);//Activate function

    uint8_t reg_accel[2] = {ACCEL_CONFIG,0x10};
    nrf_drv_spi_transfer(&spi,reg_accel,sizeof(reg_accel),NULL,0);//±8g(accel)

    uint8_t reg_accel2[2] = {ACCEL_CONFIG2,0x08};
    nrf_drv_spi_transfer(&spi,reg_accel2,sizeof(reg_accel2),NULL,0);//4kHz sampling

    uint8_t reg_gyro[2] = {GYRO_CONFIG,0x10};
    nrf_drv_spi_transfer(&spi,reg_gyro,sizeof(reg_gyro),NULL,0);//±1000dps (gyro)

    uint8_t reg_mag100hz[2] = {SMPLRT_DIV,0x09};
    nrf_drv_spi_transfer(&spi,reg_mag100hz,sizeof(reg_mag100hz),NULL,0);//100Hz sampling for mag



    nrf_drv_spi_transfer(&spi,reg_i2cclk,sizeof(reg_i2cclk),NULL,0);//I2C clock to 400kHz


    uint8_t reg_addr[2] = {I2C_SLV4_ADDR,0x0C};
    nrf_drv_spi_transfer(&spi,reg_addr,sizeof(reg_addr),NULL,0);//AK8963 slave address

    uint8_t reg_ctrl2[2] = {I2C_SLV4_REG,0x0B};
    nrf_drv_spi_transfer(&spi,reg_ctrl2,sizeof(reg_ctrl2),NULL,0);//CTRL2

    uint8_t reg_reset[2] = {I2C_SLV4_DO,0x01};
    nrf_drv_spi_transfer(&spi,reg_reset,sizeof(reg_reset),NULL,0);//reset enabled


    uint8_t reg_data[2] = {I2C_SLV4_CTRL,0x80};
    nrf_drv_spi_transfer(&spi,reg_data,sizeof(reg_data),NULL,0);//send data


    nrf_delay_ms(20);


    uint8_t reg_ctrl1[2] = {I2C_SLV4_REG,0x0A};
    nrf_drv_spi_transfer(&spi,reg_ctrl1,sizeof(reg_ctrl1),NULL,0);//ctrl1

    uint8_t reg_100hz[2] = {I2C_SLV4_DO,0x16};
    nrf_drv_spi_transfer(&spi,reg_100hz,sizeof(reg_100hz),NULL,0);//Continuous measurement mode 100Hz

    nrf_drv_spi_transfer(&spi,reg_data,sizeof(reg_data),NULL,0);//send data
    nrf_delay_ms(20);

    uint8_t reg_ak8963[2] = {I2C_SLV0_ADDR,0x8C};
    nrf_drv_spi_transfer(&spi,reg_ak8963,sizeof(reg_ak8963),NULL,0);//AK8963 data read

    uint8_t reg_hxl[2] = {I2C_SLV0_REG,0x03};
    nrf_drv_spi_transfer(&spi,reg_hxl,sizeof(reg_hxl),NULL,0);//hxl

    uint8_t reg_receive[2] = {I2C_SLV0_CTRL,0x87};
    nrf_drv_spi_transfer(&spi,reg_receive,sizeof(reg_receive),NULL,0);//7byte data receive -> ext_sens_data_00

}
uint8_t mag_axis[7] = {0};

nrf_drv_spi_xfer_desc_t xfer_spi = NRF_DRV_SPI_XFER_TRX(&reg_six,1,six_axis,21);

uint32_t flags = NRF_DRV_SPI_FLAG_HOLD_XFER       |
                  NRF_DRV_SPI_FLAG_RX_POSTINC      |
                  NRF_DRV_SPI_FLAG_NO_XFER_EVT_HANDLER |
                  NRF_DRV_SPI_FLAG_REPEATED_XFER;

ret_code_t ret = nrf_drv_spi_xfer(&spi,&xfer_spi,flags);
uint32_t start_tsk_addr = nrf_drv_spi_start_task_get(&spi);

Thank you.

Related