在使用LSM6DSV16XTR时硬件i2c接口被占用,不想要共用其他设备所以就把芯片的接口从i2c改为spi接口。硬件接线如下:
spi配置如下:
在驱动代码中做如下更改:
int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len)
{
uint8_t address = reg & 0x7F;
// HAL_I2C_Mem_Write(handle, LSM6DSV16X_I2C_ADD_L, reg, I2C_MEMADD_SIZE_8BIT, (uint8_t*) bufp, len, 0xFF);
HAL_GPIO_WritePin(GYRO_CS_GPIO_Port, GYRO_CS_Pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(handle, &address, 1, 0xffff);
HAL_SPI_Transmit(handle, bufp, len, 0xffff);
HAL_GPIO_WritePin(GYRO_CS_GPIO_Port, GYRO_CS_Pin, GPIO_PIN_SET);
return 0;
}
int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
{
uint8_t address = reg | 0x80;
// HAL_I2C_Mem_Read(handle, LSM6DSV16X_I2C_ADD_L, reg, I2C_MEMADD_SIZE_8BIT, bufp, len, 0xFF);
HAL_GPIO_WritePin(GYRO_CS_GPIO_Port, GYRO_CS_Pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(handle, &address, 1, 0xffff);
HAL_SPI_Receive(handle, bufp, len, 0xffff);
HAL_GPIO_WritePin(GYRO_CS_GPIO_Port, GYRO_CS_Pin, GPIO_PIN_SET);
return 0;
}
测试ID读取成功。