Hi swapnilsayan, sorry for the late response. While working on this project, I used Nordic nRF52840 as the master chip. Accordingly, the SPI and I2C functions are somewhat connected to the Nordic SDK. SPI: void delay(uint32_t period) {
nrf_delay_ms(period);
}
int8_t spi_read_transfer(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t length)
{
ret_code_t ret;
uint8_t read_temp[length + 1];
uint8_t counter = 0;
while (counter < 3) {
ret = nrf_drv_spi_transfer(&spi, ®_addr, 1, read_temp, length + 1);
if (ret == NRF_SUCCESS) {
break;
} else {
++counter;
}
}
if (counter >= 3) {
APP_ERROR_CHECK(ret);
}
nrf_delay_us(500);
for (int i = 1; i < length + 1; ++i) {
reg_data[i - 1] = read_temp[i];
}
return (int8_t)ret;
}
int8_t spi_write_transfer(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t length)
{
ret_code_t ret;
uint8_t write_temp[length + 1];
write_temp[0] = reg_addr;
uint8_t counter = 0;
for (int i = 1; i < length + 1; ++i) {
write_temp[i] = reg_data[i-1];
}
while (counter < 3) {
ret = nrf_drv_spi_transfer(&spi, write_temp, length + 1, NULL, 0);
if (ret == NRF_SUCCESS) {
break;
} else {
++counter;
}
}
if (counter >= 3) {
APP_ERROR_CHECK(ret);
}
nrf_delay_us(500) ;
return (int8_t)ret;
}
void setupSPI() {
nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
spi_config.frequency = NRF_DRV_SPI_FREQ_8M;
spi_config.ss_pin = SPI_SS_PIN;
spi_config.miso_pin = SPI_MISO_PIN;
spi_config.mosi_pin = SPI_MOSI_PIN;
spi_config.sck_pin = SPI_SCK_PIN;
APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, NULL, NULL));
} I didn't actually use I2C, but it was used internally by the BMI160 and BMM150 drivers, I guess. I used BMX160, which can be controlled with BMI160+BMM150 drivers. Sensors are initialized as follows: struct bmi160_dev bmi;
struct bmm150_dev bmm;
void initializeSensors() {
bmi.id = 0;
bmi.interface = BMI160_SPI_INTF;
bmi.read = spi_read_transfer;
bmi.write = spi_write_transfer;
bmi.delay_ms = delay;
bmm.dev_id = BMM150_DEFAULT_I2C_ADDRESS;
bmm.intf = BMM150_I2C_INTF;
bmm.read = bmm150_aux_read;
bmm.write = bmm150_aux_write;
bmm.delay_ms = delay;
APP_ERROR_CHECK(bmi160_init(&bmi));
bmi.aux_cfg.aux_sensor_enable = BMI160_ENABLE;
bmi.aux_cfg.aux_i2c_addr = bmm.dev_id;
bmi.aux_cfg.manual_enable = BMI160_ENABLE;
APP_ERROR_CHECK(bmi160_aux_init(&bmi));
APP_ERROR_CHECK(bmm150_init(&bmm));
} More details about this can be found on the official Bosch Github repos (https://github.com/BoschSensortec/BMI160_driver, https://github.com/BoschSensortec/BMM150-Sensor-API). Kind regards, Marko Njirjak
... View more