Hi:
Use ncs:v2.1.1
#include <logging/log.h>
#include <zephyr.h>
#include <drivers/i2c.h>
#include <stdio.h>
#include "user_twi.h"
static const struct device *slm_twi_dev[] = {
//DEVICE_DT_GET_OR_NULL(DT_NODELABEL(i2c0)),
//DEVICE_DT_GET_OR_NULL(DT_NODELABEL(i2c1)),
DEVICE_DT_GET_OR_NULL(DT_NODELABEL(i2c2)),
//DEVICE_DT_GET_OR_NULL(DT_NODELABEL(i2c3)),
};
#define TWI_DATA_LEN 128
int do_twi_write(uint16_t index, uint16_t dev_addr, uint8_t *data, uint16_t len)
{
int ret = -EINVAL;
ret = i2c_write(slm_twi_dev[index], data, len, dev_addr);
if (ret < 0) {
printk("Fail to write twi data at address: %hx", dev_addr);
}
return ret;
}
int do_twi_read(uint16_t index, uint16_t dev_addr, uint8_t *data, uint32_t num_read)
{
int ret = -EINVAL;
if (!slm_twi_dev[index]) {
printk("TWI device is not opened");
return ret;
}
if (num_read > TWI_DATA_LEN) {
printk("Not enough buffer. Increase TWI_DATA_LEN");
return -ENOBUFS;
}
ret = i2c_read(slm_twi_dev[index], data, num_read, dev_addr);
if (ret < 0) {
printk("Fail to read twi data");
return ret;
}
return ret;
}
int twi_init(void)
{
for (size_t i = 0U; i < ARRAY_SIZE(slm_twi_dev); i++) {
if (slm_twi_dev[i] != NULL && !device_is_ready(slm_twi_dev[i])) {
return -ENODEV;
}
}
return 0;
}
#include <zephyr/drivers/gpio.h>
#define CO2_ADDR 0x62
#define CO2_START_REG 0x21B1
#define CO2_READ_REG 0xEC05
#define CO2_STOP_REG 0x3F86
//#define CO2_PWR_NODE DT_ALIAS(co2Pwr)
//static const struct gpio_dt_spec co2_pwr_pin = GPIO_DT_SPEC_GET(CO2_PWR_NODE, gpios);
int twi_write_bytes(uint8_t addr, uint8_t *data, uint32_t num_bytes)
{
do_twi_write( 0, addr, data, num_bytes );
return 0;
}
int twi_read_bytes(uint8_t addr, uint8_t *data, uint32_t num_bytes)
{
do_twi_read( 0, addr, data, num_bytes );
return 0;
}
static void twi_example_wrtie( void )
{
uint8_t buf[2];
buf[0] = (uint8_t)(CO2_START_REG>>8);
buf[1] = (uint8_t)(CO2_START_REG);
twi_write_bytes( CO2_ADDR, buf, 2 );
}
static void twi_example_read( uint8_t *data )
{
uint8_t buf[2];
buf[0] = (uint8_t)(CO2_READ_REG>>8);
buf[1] = (uint8_t)(CO2_READ_REG);
twi_write_bytes( CO2_ADDR, buf, 2 );
twi_read_bytes( CO2_ADDR, data, 9 );
}
void twi_example(void)
{
twi_example_wrtie();
}
void twi_read(void)
{
uint8_t buf[9];
uint16_t atmos;
uint16_t data;
float temp,co2;
twi_example_read(buf);
atmos = (uint16_t)(buf[0]<<8)+(uint16_t)(buf[1]);
temp = ((float)((uint16_t)(buf[3]<<8)+(uint16_t)(buf[4]))*175)/65535-45;
co2 = ((float)((uint16_t)(buf[6]<<8)+(uint16_t)(buf[7]))*100)/65535;
printk("%d ppm, %f C, %f %%",atmos,temp,co2);
}
void main(void)
{
twi_init();
while (1)
{
k_msleep(3000);
twi_read();
}
}
This is LOG:
<inf> mqtt_simple: twi ret=0
Fail to read twi data58856 ppm, %f C, %f %
<err> i2c_nrfx_twim: Error 0x0BAE0001 occurred for message 0
717 ppm, %f C, %f %
Fail to read twi data58856 ppm, %f C, %f %
<err> i2c_nrfx_twim: Error 0x0BAE0001 occurred for message 0
According to the LOG, I2C was successfully written, but the read failed.
And the hardware can be confirmed as OK.
And using NCS V1.9.1 is successful.