Hello, I am facing an issue in nrf52dk (nrf52832dk). Whenever I access i2c0 bus for the controller it works fine but whenever i switched on the i2c1, it starts giving me device not ready error. Here is the main.c I'm using:
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/sys/printk.h>
#include <stdint.h>
/* BMA400 I2C address */
#define BMA400_I2C_ADDR 0x14
/* BMA400 register map (subset we need) */
#define BMA400_CHIP_ID 0x00
#define BMA400_STATUS 0x03
#define BMA400_ACC_X_LSB 0x04
#define BMA400_ACC_CONFIG0 0x19
#define BMA400_ACC_CONFIG1 0x1A
#define BMA400_ACC_CONFIG2 0x1B
/* Expected values */
#define BMA400_EXPECTED_CHIPID 0x90
/* Scale factors depending on g-range */
#define BMA400_SCALE_2G (1.0f / 1024.0f) /* LSB/g */
#define BMA400_SCALE_4G (1.0f / 512.0f)
#define BMA400_SCALE_8G (1.0f / 256.0f)
#define BMA400_SCALE_16G (1.0f / 128.0f)
/* Global device descriptor */
static const struct i2c_dt_spec bma400 = I2C_DT_SPEC_GET(DT_NODELABEL(mysensor));
/* Helper: read chip ID */
static int bma400_check_chipid(void)
{
uint8_t id;
if (i2c_reg_read_byte_dt(&bma400, BMA400_CHIP_ID, &id) != 0) {
printk("I2C read failed\n");
return -EIO;
}
printk("BMA400 Chip ID = 0x%02X\n", id);
return (id == BMA400_EXPECTED_CHIPID) ? 0 : -ENODEV;
}
/* Helper: configure accelerometer (Normal mode, ±2g, 100 Hz ODR, osr=3) */
static int bma400_config(void)
{
int ret;
/* ACC_CONFIG0: power_mode=normal (0x02) */
ret = i2c_reg_write_byte_dt(&bma400, BMA400_ACC_CONFIG0, 0x02);
if (ret)
return ret;
k_msleep(2);
/* ACC_CONFIG1: ±2g (00), osr=3 (11), odr=100Hz (0x8) => 0x38 */
ret = i2c_reg_write_byte_dt(&bma400, BMA400_ACC_CONFIG1, 0x38);
if (ret) return ret;
k_msleep(5);
/* ACC_CONFIG2: select acc_filt1 as data source (0x00) */
ret = i2c_reg_write_byte_dt(&bma400, BMA400_ACC_CONFIG2, 0x00);
if (ret) return ret;
return 0;
}
/* Helper: read and assemble 12-bit signed accel data */
static int bma400_read_xyz(int16_t *x, int16_t *y, int16_t *z)
{
uint8_t data[6];
if (i2c_burst_read_dt(&bma400, BMA400_ACC_X_LSB, data, 6) != 0) {
printk("Failed to read accel data\n");
return -EIO;
}
/* Assemble 12-bit signed values */
int16_t rx = ((data[1] & 0x0F) << 8) | data[0];
int16_t ry = ((data[3] & 0x0F) << 8) | data[2];
int16_t rz = ((data[5] & 0x0F) << 8) | data[4];
/* Sign-extend */
if (rx & 0x0800) rx |= 0xF000;
if (ry & 0x0800) ry |= 0xF000;
if (rz & 0x0800) rz |= 0xF000;
*x = rx;
*y = ry;
*z = rz;
return 0;
}
/* Main entry */
void main(void)
{
printk("Starting BMA400 test...\n");
if (!device_is_ready(bma400.bus)) {
printk("I2C device not ready!\n");
return;
}
if (bma400_check_chipid() != 0) {
printk("BMA400 not found!\n");
return;
}
if (bma400_config() != 0) {
printk("BMA400 config failed!\n");
return;
}
printk("BMA400 configured.\n");
/* scale for ±2g (since we set acc_range=00) */
const float scale = BMA400_SCALE_2G;
while (1) {
int16_t x, y, z;
if (bma400_read_xyz(&x, &y, &z) == 0) {
float gx = x * scale;
float gy = y * scale;
float gz = z * scale;
printk("RAW: X=%d Y=%d Z=%d | Accel: X=%.3f g, Y=%.3f g, Z=%.3f g\n",
x, y, z, gx, gy, gz);
}
k_msleep(200);
}
}
Here is the proj.conf:
# STEP 2 - Enable the I2C driver CONFIG_I2C=y # STEP 4.2 - Enable floating point format specifiers CONFIG_CBPRINTF_FP_SUPPORT=y CONFIG_LOG=y # CONFIG_LOG_DEFAULT_LEVEL=3 CONFIG_PRINTK=y
here is the device overlay file:
// To get started, press Ctrl+Space to bring up the completion menu and view the available nodes.
// For more help, browse the DeviceTree documentation at https://docs.zephyrproject.org/latest/guides/dts/index.html
&i2c1 {
status = "okay";
pinctrl-0 = <&i2c1_default>;
pinctrl-1 = <&i2c1_sleep>;
pinctrl-names = "default", "sleep";
mysensor: bma4xx@14 {
compatible = "bosch,bma4xx";
reg = <0x14>;
status = "okay";
};
};
&pinctrl {
/omit-if-no-ref/ i2c1_default: i2c1_default {
group1 {
psels = <NRF_PSEL(TWIM_SCL, 0, 25)>,
<NRF_PSEL(TWIM_SDA, 0, 24)>;
};
};
/omit-if-no-ref/ i2c1_sleep: i2c1_sleep {
group1 {
psels = <NRF_PSEL(TWIM_SCL, 0, 25)>,
<NRF_PSEL(TWIM_SDA, 0, 24)>;
low-power-enable;
};
};
};
&i2c0 {
status = "disabled";
};
&spi2 {
status = "disabled";
};
&spi1 {
status = "disabled";
};
Error log i'm facing right now is:
*** Booting nRF Connect SDK v3.0.2-89ba1294ac9b ***
*** Using Zephyr OS v4.0.99-f791c49f492c ***
Starting BMA400 test...
I2C device not ready!
Connection and wirings are correct because as soon as i change i2c1 to i2c0: it stars giving me:
*** Booting nRF Connect SDK v3.0.2-89ba1294ac9b ***
*** Using Zephyr OS v4.0.99-f791c49f492c ***
Starting BMA400 test...
BMA400 Chip ID = 0x90
BMA400 configured.
RAW: X=0 Y=0 Z=0 | Accel: X=0.000 g, Y=0.000 g, Z=0.000 g
RAW: X=800 Y=-250 Z=-518 | Accel: X=0.781 g, Y=-0.244 g, Z=-0.506 g
RAW: X=802 Y=-247 Z=-519 | Accel: X=0.783 g, Y=-0.241 g, Z=-0.507 g
RAW: X=800 Y=-247 Z=-517 | Accel: X=0.781 g, Y=-0.241 g, Z=-0.505 g
My real question:
Is there any specific method or configurations needed to enable i2c1 for nrf52dk? and if i want to configure my one sensor on i2c1 and two sensors on i2c0 simultaneously, can I do that? Is the methods same for nrf9151dk?
Regards,
Waleed