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?