Refactor I2C driver detection phase 2

This commit is contained in:
Theo Arends 2019-11-07 16:56:05 +01:00
parent e8b9823e6a
commit f32b64d5de
16 changed files with 173 additions and 100 deletions

66
API.md
View File

@ -68,3 +68,69 @@ FUNC_DISPLAY_FONT_SIZE | | 6.1.1.7 |
FUNC_DISPLAY_ROTATION | | 6.1.1.7 |
FUNC_DISPLAY_DRAW_STRING | | 6.1.1.7 |
FUNC_DISPLAY_ONOFF | | 6.1.1.7 |
## Init sequence
The following list contains the callback init sequence
CFG: Loaded from flash at FB, Count 1581
xdrv - FUNC_SETTINGS_OVERRIDE
xdrv - FUNC_PIN_STATE
xsns - FUNC_PIN_STATE
xdrv - FUNC_MODULE_INIT
xlgt - FUNC_MODULE_INIT
xdrv - FUNC_PRE_INIT
xnrg - FUNC_PRE_INIT
SRC: Restart
xdrv - FUNC_SET_POWER
xlgt - FUNC_SET_CHANNELS
xdrv - FUNC_SET_DEVICE_POWER
Project tasmota Wemos 2 Version 7.0.0.3(tasmota)-STAGE
xdrv - FUNC_INIT
xsns - FUNC_INIT
I2C: ADS1115 found at 0x48
xdrv - FUNC_LOOP
xsns - FUNC_LOOP
xdrv - FUNC_EVERY_50_MSECOND
xlgt - FUNC_SET_CHANNELS
xsns - FUNC_EVERY_50_MSECOND
xdrv - FUNC_EVERY_100_MSECOND
xsns - FUNC_EVERY_100_MSECOND
xdrv - FUNC_EVERY_250_MSECOND
xsns - FUNC_EVERY_250_MSECOND
xdrv - FUNC_EVERY_SECOND
xsns - FUNC_EVERY_SECOND
WIF: Attempting connection...
WIF: Network (re)scan started...
WIF: Attempting connection...
WIF: Attempting connection...
WIF: Attempting connection...
WIF: Network 0, AP1, SSId indebuurt1, Channel 1, BSSId 24:D3:F2:97:C0:A1, RSSI -86, Encryption 1
WIF: Network 1, AP2, SSId indebuurt2, Channel 5, BSSId A0:AB:1B:7D:42:AC, RSSI -42, Encryption 1
WIF: Network 2, AP-, SSId indebuurt3, Channel 12, BSSId 60:E3:27:58:77:E6, RSSI -84, Encryption 1
WIF: Connecting to AP2 indebuurt2 in mode 11N as wemos2...
WIF: Attempting connection...
WIF: Attempting connection...
WIF: Attempting connection...
WIF: Connected
xdrv - FUNC_WEB_ADD_HANDLER
xsns - FUNC_WEB_ADD_HANDLER
HTP: Web server active on wemos2 with IP address 192.168.2.191
NTP: Drift 0, (UTC) Wed Nov 06 13:57:08 2019, (DST) Sun Mar 31 02:00:00 2019, (STD) Sun Oct 27 03:00:00 2019
APP: Boot Count 500
MQT: Attempting connection...
MQT: Connected
MQT: tele/wemos2/LWT = Online (retained)
MQT: cmnd/wemos2/POWER =
MQT: Subscribe to cmnd/wemos2/#
MQT: Subscribe to cmnd/sonoffs/#
MQT: Subscribe to cmnd/DVES_15568C_fb/#
xdrv - FUNC_MQTT_SUBSCRIBE
MQT: tele/wemos2/INFO1 = {"Module":"Generic","Version":"7.0.0.3(tasmota)","FallbackTopic":"cmnd/DVES_15568C_fb/","GroupTopic":"cmnd/sonoffs/"}
MQT: tele/wemos2/INFO2 = {"WebServerMode":"Admin","Hostname":"wemos2","IPAddress":"192.168.2.191"}
MQT: tele/wemos2/INFO3 = {"RestartReason":"Software/System restart"}
MQT: stat/wemos2/RESULT = {"POWER1":"OFF"}
MQT: stat/wemos2/POWER1 = OFF
MQT: stat/wemos2/RESULT = {"POWER2":"ON"}
MQT: stat/wemos2/POWER2 = ON
xdrv - FUNC_MQTT_INIT
CFG: Saved to flash at FA, Count 1582, Bytes 4096

View File

@ -60,3 +60,6 @@ Index | Define | Driver | Device | Address(es) | Description
34 | USE_PAJ7620 | xsns_50 | PAJ7620 | 0x73 | Gesture sensor
35 | USE_INA226 | xsns_54 | INA226 | 0x40 - 0x41, 0x44 - 0x45 | Low voltage current sensor
36 | USE_HIH6 | xsns_55 | HIH6130 | 0x27 | Temperature and Humidity sensor
37 | USE_24C256 | xdrv_10 | 24C256 | 0x50 | Scripter EEPROM storage
38 | USE_DISPLAY_ILI9488 | xdsp_08 | FT6236 | 0x38 | Touch panel controller
39 | USE_DISPLAY_RA8876 | xdsp_10 | FT5316 | 0x38 | Touch panel controller

View File

@ -3,6 +3,7 @@
* Initial support for I2C driver runtime control using command I2CDriver and document I2CDEVICES.md
* Fix random crash caused by UPNP flood
* Add support for Honeywell HPMA115S0 particle concentration sensor by David Hunt (#6843)
* Remove driver xsns_12_ads1115_i2cdev replaced by xsns_12_ads1115
*
* 7.0.0.2 20191102
* Add command WebColor19 to control color of Module and Name (#6811)

View File

@ -1486,6 +1486,17 @@ void I2cScan(char *devs, unsigned int devs_len)
}
}
void I2cResetActive(uint32_t addr, uint32_t count = 1)
{
addr &= 0x7F; // Max I2C address is 127
count &= 0x7F; // Max 4 x 32 bits available
while (count-- && (addr < 128)) {
i2c_active[addr / 32] &= ~(1 << (addr % 32));
addr++;
}
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("I2C: Active %08X,%08X,%08X,%08X"), i2c_active[0], i2c_active[1], i2c_active[2], i2c_active[3]);
}
void I2cSetActive(uint32_t addr, uint32_t count = 1)
{
addr &= 0x7F; // Max I2C address is 127
@ -1506,19 +1517,14 @@ bool I2cActive(uint32_t addr)
return false;
}
bool I2cDevice(uint32_t addr)
bool I2cSetDevice(uint32_t addr)
{
addr &= 0x7F; // Max I2C address is 127
if (I2cActive(addr)) {
return false; // If already active report as not present;
}
Wire.beginTransmission((uint8_t)addr);
return (0 == Wire.endTransmission());
}
bool I2cSetDevice(uint32_t addr)
{
bool result = I2cDevice(addr);
bool result = (0 == Wire.endTransmission());
if (result) {
I2cSetActive(addr, 1);
}

View File

@ -38,6 +38,7 @@ keywords if then else endif, or, and are better readable for beginners (others m
\*********************************************************************************************/
#define XDRV_10 10
#define XI2C_37 37 // See I2CDEVICES.md
#define SCRIPT_DEBUG 0
@ -4729,8 +4730,8 @@ bool Xdrv10(uint8_t function)
#ifdef USE_24C256
#ifndef USE_SCRIPT_FATFS
if (i2c_flg) {
if (I2cDevice(EEPROM_ADDRESS)) {
if (I2cEnabled(XI2C_37)) {
if (I2cSetDevice(EEPROM_ADDRESS)) {
// found 32kb eeprom
char *script;
script=(char*)calloc(EEP_SCRIPT_SIZE+4,1);

View File

@ -22,6 +22,7 @@
#ifdef USE_DISPLAY_ILI9488
#define XDSP_08 8
#define XI2C_38 38 // See I2CDEVICES.md
#define COLORED 1
#define UNCOLORED 0
@ -112,7 +113,7 @@ void ILI9488_InitDriver()
color_type = COLOR_COLOR;
// start digitizer with fixed adress
if (i2c_flg && I2cDevice(FT6236_address)) {
if (I2cEnabled(XI2C_38) && I2cSetDevice(FT6236_address)) {
FT6236begin(FT6236_address);
FT6236_found=1;
} else {

View File

@ -22,6 +22,7 @@
#ifdef USE_DISPLAY_RA8876
#define XDSP_10 10
#define XI2C_39 39 // See I2CDEVICES.md
#define COLORED 1
#define UNCOLORED 0
@ -97,7 +98,7 @@ void RA8876_InitDriver()
#endif
color_type = COLOR_COLOR;
if (i2c_flg && I2cDevice(FT5316_address)) {
if (I2cEnabled(XI2C_39) && I2cSetDevice(FT5316_address)) {
FT6236begin(FT5316_address);
FT5316_found=1;
} else {

View File

@ -226,10 +226,6 @@ bool Xsns07(uint8_t function)
bool result = false;
switch (function) {
// case FUNC_PREP_BEFORE_TELEPERIOD: // As this is not a real I2C device it may interfere with other sensors
case FUNC_INIT: // Move detection to restart only removing interference
ShtDetect();
break;
case FUNC_EVERY_SECOND:
ShtEverySecond();
break;
@ -241,6 +237,9 @@ bool Xsns07(uint8_t function)
ShtShow(0);
break;
#endif // USE_WEBSERVER
case FUNC_INIT: // Move detection to restart only removing interference
ShtDetect();
break;
}
return result;
}

View File

@ -198,11 +198,12 @@ bool Ina219CommandSensor(void)
void Ina219Detect(void)
{
for (int i=0; i<sizeof(ina219_type); i++) {
if (ina219_type[i])
continue;
for (uint32_t i = 0; i < sizeof(ina219_type); i++) {
if (ina219_type[i]) { continue; }
uint16_t addr = ina219_addresses[i];
if (I2cActive(addr)) { continue; }
if (Ina219SetCalibration(Settings.ina219_mode, addr)) {
I2cSetActive(addr);
ina219_type[i] = 1;
AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, ina219_types, addr);
}
@ -286,9 +287,6 @@ bool Xsns13(uint8_t function)
result = Ina219CommandSensor();
}
break;
case FUNC_INIT:
Ina219Detect();
break;
case FUNC_EVERY_SECOND:
Ina219EverySecond();
break;
@ -300,6 +298,9 @@ bool Xsns13(uint8_t function)
Ina219Show(0);
break;
#endif // USE_WEBSERVER
case FUNC_INIT:
Ina219Detect();
break;
}
return result;
}

View File

@ -84,7 +84,9 @@ void Sht3xDetect(void)
float t;
float h;
for (uint32_t i = 0; i < SHT3X_MAX_SENSORS; i++) {
if (I2cActive(sht3x_addresses[i])) { continue; }
if (Sht3xRead(t, h, sht3x_addresses[i])) {
I2cSetActive(sht3x_addresses[i]);
sht3x_sensors[sht3x_count].address = sht3x_addresses[i];
GetTextIndexed(sht3x_sensors[sht3x_count].types, sizeof(sht3x_sensors[sht3x_count].types), i, kShtTypes);
AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, sht3x_sensors[sht3x_count].types, sht3x_sensors[sht3x_count].address);
@ -144,9 +146,6 @@ bool Xsns14(uint8_t function)
bool result = false;
switch (function) {
case FUNC_INIT:
Sht3xDetect();
break;
case FUNC_JSON_APPEND:
Sht3xShow(1);
break;
@ -155,6 +154,9 @@ bool Xsns14(uint8_t function)
Sht3xShow(0);
break;
#endif // USE_WEBSERVER
case FUNC_INIT:
Sht3xDetect();
break;
}
return result;
}

View File

@ -67,12 +67,12 @@ void Tsl2561Detect(void)
if (tsl2561_type) { return; }
uint8_t id;
if (I2cDevice(0x29) || I2cDevice(0x39) || I2cDevice(0x49)) {
if (I2cSetDevice(0x29) || I2cSetDevice(0x39) || I2cSetDevice(0x49)) {
Tsl.begin();
if (!Tsl.id(id)) return;
if (Tsl.on()) {
tsl2561_type = 1;
AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, tsl2561_types, Tsl.address(), id);
AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, tsl2561_types, Tsl.address());
}
}
}
@ -127,9 +127,6 @@ bool Xsns16(uint8_t function)
bool result = false;
switch (function) {
case FUNC_INIT:
Tsl2561Detect();
break;
case FUNC_EVERY_SECOND:
Tsl2561EverySecond();
break;
@ -141,6 +138,9 @@ bool Xsns16(uint8_t function)
Tsl2561Show(0);
break;
#endif // USE_WEBSERVER
case FUNC_INIT:
Tsl2561Detect();
break;
}
return result;
}

View File

@ -116,18 +116,12 @@ void MPU_6050SetAccelOffsets(int x, int y, int z)
void MPU_6050Detect(void)
{
if (MPU_6050_found)
{
return;
}
if (MPU_6050_found) { return; }
for (uint32_t i = 0; i < sizeof(MPU_6050_addresses); i++)
{
if(!I2cDevice(MPU_6050_addresses[i]))
{
break;
}
MPU_6050_address = MPU_6050_addresses[i];
if (!I2cSetDevice(MPU_6050_address)) { break; }
mpu6050.setAddr(MPU_6050_addresses[i]);
#ifdef USE_MPU6050_DMP
@ -231,9 +225,6 @@ bool Xsns32(uint8_t function)
bool result = false;
switch (function) {
case FUNC_PREP_BEFORE_TELEPERIOD:
MPU_6050Detect();
break;
case FUNC_EVERY_SECOND:
if (tele_period == Settings.tele_period -3) {
MPU_6050PerformReading();
@ -248,6 +239,9 @@ bool Xsns32(uint8_t function)
MPU_6050PerformReading();
break;
#endif // USE_WEBSERVER
case FUNC_INIT:
MPU_6050Detect();
break;
}
return result;
}

View File

@ -127,11 +127,10 @@ unsigned char cmdb[6];
Wire.endTransmission();
}
void SPS30_Detect() {
void SPS30_Detect(void)
{
if (!I2cSetDevice(SPS30_ADDR)) { return; }
if (!I2cDevice(SPS30_ADDR)) {
return;
}
uint8_t dcode[32];
sps30_get_data(SPS_CMD_GET_SERIAL,dcode,sizeof(dcode));
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("sps30 found with serial: %s"),dcode);
@ -289,25 +288,25 @@ bool Xsns44(byte function)
bool result = false;
switch (function) {
case FUNC_INIT:
SPS30_Detect();
break;
case FUNC_EVERY_SECOND:
SPS30_Every_Second();
break;
case FUNC_JSON_APPEND:
SPS30_Show(1);
break;
case FUNC_COMMAND_SENSOR:
if (XSNS_44 == XdrvMailbox.index) {
result = SPS30_cmd();
}
break;
#ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
SPS30_Show(0);
break;
#endif // USE_WEBSERVER
case FUNC_COMMAND_SENSOR:
if (XSNS_44 == XdrvMailbox.index) {
result = SPS30_cmd();
}
break;
case FUNC_INIT:
SPS30_Detect();
break;
}
return result;
}

View File

@ -32,27 +32,17 @@ uint16_t vl53l0x_distance;
uint16_t Vl53l0_buffer[5];
uint8_t Vl53l0_index;
/********************************************************************************************/
void Vl53l0Detect()
void Vl53l0Detect(void)
{
if (vl53l0x_ready) { return; }
if (!I2cDevice(0x29)) {
return;
}
if (!I2cSetDevice(0x29)) { return; }
if (vl53l0x_ready) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR("VL53L1X is ready"));
return;
}
if (!sensor.init()) { return; }
if (sensor.init()==true) {
snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "VL53L0X", sensor.getAddress());
AddLog(LOG_LEVEL_DEBUG);
} else {
return;
}
AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "VL53L0X", sensor.getAddress());
sensor.setTimeout(500);
@ -64,11 +54,8 @@ void Vl53l0Detect()
vl53l0x_ready = 1;
Vl53l0_index=0;
}
#define D_UNIT_MILLIMETER "mm"
#ifdef USE_WEBSERVER
const char HTTP_SNS_VL53L0X[] PROGMEM =
"{s}VL53L0X " D_DISTANCE "{m}%d" D_UNIT_MILLIMETER "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
@ -76,12 +63,13 @@ const char HTTP_SNS_VL53L0X[] PROGMEM =
#define USE_VL_MEDIAN
void Vl53l0Every_250MSecond() {
void Vl53l0Every_250MSecond(void)
{
if (!vl53l0x_ready) { return; }
uint16_t tbuff[5],tmp;
uint8_t flag;
if (!vl53l0x_ready) return;
// every 200 ms
uint16_t dist = sensor.readRangeContinuousMillimeters();
if (dist==0 || dist>2000) {
@ -116,9 +104,7 @@ void Vl53l0Every_250MSecond() {
void Vl53l0Show(boolean json)
{
if (!vl53l0x_ready) {
return;
}
if (!vl53l0x_ready) { return; }
if (json) {
ResponseAppend_P(PSTR(",\"VL53L0X\":{\"" D_JSON_DISTANCE "\":%d}"), vl53l0x_distance);
@ -127,7 +113,6 @@ void Vl53l0Show(boolean json)
WSContentSend_PD(HTTP_SNS_VL53L0X, vl53l0x_distance);
#endif
}
}
/*********************************************************************************************\
@ -141,9 +126,6 @@ bool Xsns45(byte function)
bool result = false;
switch (function) {
case FUNC_INIT:
Vl53l0Detect();
break;
case FUNC_EVERY_250_MSECOND:
Vl53l0Every_250MSecond();
break;
@ -155,6 +137,9 @@ bool Xsns45(byte function)
Vl53l0Show(0);
break;
#endif // USE_WEBSERVER
case FUNC_INIT:
Vl53l0Detect();
break;
}
return result;
}

View File

@ -29,13 +29,12 @@ uint8_t mlx_ready;
float obj_temp;
float amb_temp;
void MLX90614_Init() {
if (!I2cDevice(I2_ADR_IRT)) {
return;
}
void MLX90614_Init(void)
{
if (!I2cSetDevice(I2_ADR_IRT)) { return; }
mlx_ready=1;
AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "MLX90614", I2_ADR_IRT);
// not needed on tasmota
//Wire.begin();
@ -122,9 +121,6 @@ bool Xsns46(byte function)
bool result = false;
switch (function) {
case FUNC_INIT:
MLX90614_Init();
break;
case FUNC_EVERY_SECOND:
MLX90614_Every_Second();
break;
@ -136,6 +132,9 @@ bool Xsns46(byte function)
MLX90614_Show(0);
break;
#endif // USE_WEBSERVER
case FUNC_INIT:
MLX90614_Init();
break;
}
return result;
}

View File

@ -176,6 +176,19 @@ bool Ina226TestPresence(uint8_t device)
}
void Ina226ResetActive(void)
{
Ina226SlaveInfo_t *p = slaveInfo;
for (uint32_t i = 0; i < INA226_MAX_ADDRESSES; i++) {
p = &slaveInfo[i];
// Address
uint8_t addr = p->address;
if (addr) {
I2cResetActive(addr);
}
}
}
/*
* Initialize INA226 devices
@ -183,20 +196,17 @@ bool Ina226TestPresence(uint8_t device)
void Ina226Init()
{
uint32_t i;
slavesFound = 0;
Ina226SlaveInfo_t *p = slaveInfo;
//AddLog_P2( LOG_LEVEL_NONE, "Ina226Init");
AddLog_P2( LOG_LEVEL_NONE, "Size of Settings: %d bytes", sizeof(Settings));
// AddLog_P2( LOG_LEVEL_NONE, "Size of Settings: %d bytes", sizeof(Settings));
if (!i2c_flg)
AddLog_P2(LOG_LEVEL_DEBUG, "INA226: Initialization failed: No I2C support");
// if (!i2c_flg)
// AddLog_P2(LOG_LEVEL_DEBUG, "INA226: Initialization failed: No I2C support");
// Clear slave info data
@ -209,9 +219,11 @@ void Ina226Init()
// Detect devices
for (i = 0; (i < INA226_MAX_ADDRESSES); i++){
for (i = 0; i < INA226_MAX_ADDRESSES; i++){
uint8_t addr = pgm_read_byte(probeAddresses + i);
if (I2cActive(addr)) { continue; }
// Skip device probing if the full scale current is zero
//AddLog_P2( LOG_LEVEL_NONE, "fs_i[%d]: %d", i, Settings.ina226_i_fs[i]);
@ -246,6 +258,8 @@ void Ina226Init()
// store data in slave info struct.
I2cSetActive(addr);
p = &slaveInfo[i];
// Address
p->address = addr;
@ -399,6 +413,7 @@ bool Ina226CommandSensor()
// Device-less commands
switch (p1){
case 1: // Rerun init
Ina226ResetActive();
Ina226Init();
Response_P(PSTR("{\"Sensor54-Command-Result\":{\"SlavesFound\":%d}}"),slavesFound);
break;
@ -542,9 +557,6 @@ bool Xsns54(byte callback_id)
case FUNC_EVERY_SECOND:
Ina226EverySecond();
break;
case FUNC_INIT:
Ina226Init();
break;
case FUNC_JSON_APPEND:
Ina226Show(1);
break;
@ -558,6 +570,9 @@ bool Xsns54(byte callback_id)
result = Ina226CommandSensor();
}
break;
case FUNC_INIT:
Ina226Init();
break;
}
// Return boolean result
return result;