RAK811 I2C using new compiler

I am moving some of the sample code I originally created over to the new compiler, using a WisNode RAK811 for convenience as it has pins I can access & a serial port.

I have the LED flashing (aka Hello World of embedded) working just fine.

I’ve hit a road block for I2C. I have a BME280 temp/humidity/pressure sensor that I have working on an Arduino and at present I would be happy to just read the chip id.

#define ADDR 0x76
#include <Wire.h>

void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println("\nI2C Tester");
}

void loop() {
byte result = 0;

Wire.beginTransmission(ADDR);
result = Wire.endTransmission();
if (result == 0) {
Serial.println(“I2C device found”);
} else {
Serial.println(“Unknown error”);
}

delay(100);

Wire.beginTransmission(ADDR);
Wire.write(0xD0);
result = Wire.endTransmission();
Serial.print("Write result = ");
Serial.println(result, HEX);

Wire.requestFrom(ADDR, 1);
while ( Wire.available() ) {
result = Wire.read();
Serial.print("Response = ");
Serial.println(result, HEX);
}

delay(3000);
}

These are the I2C parts from the RUI RAK811 template from the v2 Git repository:

#define I2C_SCL 18
#define I2C_SDA 19
RUI_I2C_ST appI2C;

#define BME280_ADDR 0x76

appI2C.INSTANCE_ID = 1;
appI2C.PIN_SDA = I2C_SDA;
appI2C.PIN_SCL = I2C_SCL;
appI2C.FREQUENCY = RUI_I2C_FREQ_100K;

if(rui_i2c_init(&appI2C) != RUI_STATUS_OK)
RUI_LOG_PRINTF(“I2C init error.\r\n”);

uint8_t t = 0;
RUI_RETURN_STATUS result = 0;

// Chip ID as this is JUST a read
result = rui_i2c_rw(&appI2C, RUI_IF_READ, BME280_ADDR, 0xD0, &t, 1);
RUI_LOG_PRINTF(“BME280read = %d\r\n”, result);
RUI_LOG_PRINTF(“BME280_ChipId = %d\r\n”, t);

I have also tried this as a write before read to mirror the Arduino style:

result = rui_i2c_rw(&appI2C, RUI_IF_WRITE, BME280_ADDR, 0xD0, &t, 1);
RUI_LOG_PRINTF(“BME280write = %d\r\n”, result);
result = rui_i2c_rw(&appI2C, RUI_IF_READ, BME280_ADDR, 0xD0, &t, 1);
RUI_LOG_PRINTF(“BME280read = %d\r\n”, result);
RUI_LOG_PRINTF(“BME280_ChipId = %d\r\n”, t);

The intern who said they’d got this working can’t find the code now (and they are in isolation at university, so I can’t go and talk to them).

What I don’t know is if the RUI read sends the reg address.

Either way, both versions return error code 6.

A simple I2C sample that is NOT a whole library of Bosch commands would be much appreciated.

Is @ZhuQI the RUI expert?

Hi @nmcc, The devaddr parameter in the rui_i2c_rw() interface is an 8-bit address, and the read/write bit needs to be considered.

I’m not clear how to act on this information.

I’ve reviewed the RUI code for the RAK5205 & RAK7204 that have an I2C interface and I see the devaddr being used as the one published, and I’m using the published one on a module that has been tested using the Arduino code that I’ve shown above. In the forum post I2c, initialization @Nicholas says that the address is already shifted.

Is the Read/Write bit not handled by the RUI_IF_READ/WRITE?

I can “just” go and do this in STM so I’m not a beginner but as I’m trying to get setup as a reseller that supports the product, it would be useful if I could get to grips with RUI.

Mindful of time zones and that it is now 5pm or thereabouts over in the RAK main office, it would appear that I’ve lost Monday to work on this now. Can I ask that someone applies themselves to this thread by taking ten minutes to produce a working example so that I can get on with things Tuesday.

I need to ship a prototype, preferably based on RAK4200, later this week or this project will die like the WisNode one did and my wife, project manager, will be upset.

@Sucre, @Nicholas, @ZhuQI, @Fomi, @makahernandez, @velev, @nero, @Hobo

Hi @nmcc,Sorry to reply you so late. I have a DS3231 module in my hand. Below is the driver example of the module in RAK4200. The time register is read and written normally.

For some other reasons, using the rui_i2c_rw() interface on RAK811 requires the use of 8-bit addresses, namely DA3231_ADDR_WRITE and DS3231_ADDR_READ.

#define I2C_SCL 9
#define I2C_SDA 10
RUI_I2C_ST appI2C;

RUI_RETURN_STATUS ret_code;

uint8_t data[16];
uint16_t data_len = 16;

#define DS3231_ADDR 0x68
#define DS3231_ADDR_WRITE (DS3231_ADDR<<1)
#define DS3231_ADDR_READ (DS3231_ADDR<<1 | 0x01)

void setup(void)
{
appI2C.INSTANCE_ID = 1;
appI2C.PIN_SDA = I2C_SDA;
appI2C.PIN_SCL = I2C_SCL;
appI2C.FREQUENCY = RUI_I2C_FREQ_100K;

ret_code = rui_i2c_init(&appI2C);
if (ret_code != RUI_STATUS_OK)
    RUI_LOG_PRINTF("I2C init error.\r\n");

ret_code = rui_i2c_rw(&appI2C, RUI_IF_READ, DS3231_ADDR, 0x00, data, data_len);
if (ret_code != RUI_STATUS_OK)
    RUI_LOG_PRINTF("I2C read error.\r\n");
else
    RUI_LOG_PRINTF("I2C read success.\r\n");

memset(data, 0x00, 3);
ret_code = rui_i2c_rw(&appI2C, RUI_IF_WRITE, DS3231_ADDR, 0x00, data, 3);
if (ret_code != RUI_STATUS_OK)
    RUI_LOG_PRINTF("I2C write error.\r\n");
else
    RUI_LOG_PRINTF("I2C write success.\r\n");

}

void loop(void)
{
ret_code = rui_i2c_rw(&appI2C, RUI_IF_READ, DS3231_ADDR, 0x00, data, data_len);
if (ret_code != RUI_STATUS_OK)
RUI_LOG_PRINTF(“I2C read error.\r\n”);
else
RUI_LOG_PRINTF("%02X:%02X:%02X\r\n", data[2], data[1], data[0]);

rui_delay_ms(1000);

}

OK, thanks, I’ll try this out this afternoon - I’ve a DS3231 in the bits box so I can try on both modules.

Perhaps useful to document the address shift required on RAK811!