i2c device scan improved
This commit is contained in:
parent
0adedc3e35
commit
f77fdffba8
@ -2,6 +2,7 @@
|
|||||||
#define _I2C_H
|
#define _I2C_H
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <BitBang_I2C.h>
|
||||||
|
|
||||||
#define SSD1306_PRIMARY_ADDRESS (0x3D)
|
#define SSD1306_PRIMARY_ADDRESS (0x3D)
|
||||||
#define SSD1306_SECONDARY_ADDRESS (0x3C)
|
#define SSD1306_SECONDARY_ADDRESS (0x3C)
|
||||||
@ -22,7 +23,7 @@
|
|||||||
|
|
||||||
void i2c_init(void);
|
void i2c_init(void);
|
||||||
void i2c_deinit(void);
|
void i2c_deinit(void);
|
||||||
int i2c_scan(void);
|
void i2c_scan(void);
|
||||||
uint8_t i2c_readBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len);
|
uint8_t i2c_readBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len);
|
||||||
uint8_t i2c_writeBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len);
|
uint8_t i2c_writeBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len);
|
||||||
|
|
||||||
|
@ -62,6 +62,7 @@ lib_deps_lora =
|
|||||||
mcci-catena/MCCI LoRaWAN LMIC library @ ^3.2.0
|
mcci-catena/MCCI LoRaWAN LMIC library @ ^3.2.0
|
||||||
lib_deps_display =
|
lib_deps_display =
|
||||||
bitbank2/OneBitDisplay @ 1.7.2
|
bitbank2/OneBitDisplay @ 1.7.2
|
||||||
|
bitbank2/BitBang_I2C @ ^2.1.3
|
||||||
ricmoo/QRCode @ ^0.0.1
|
ricmoo/QRCode @ ^0.0.1
|
||||||
bodmer/TFT_eSPI @ ^2.2.20
|
bodmer/TFT_eSPI @ ^2.2.20
|
||||||
lib_deps_ledmatrix =
|
lib_deps_ledmatrix =
|
||||||
|
114
src/i2c.cpp
114
src/i2c.cpp
@ -5,7 +5,7 @@
|
|||||||
// Local logging tag
|
// Local logging tag
|
||||||
static const char TAG[] = __FILE__;
|
static const char TAG[] = __FILE__;
|
||||||
|
|
||||||
void i2c_init(void) { Wire.begin(MY_DISPLAY_SDA, MY_DISPLAY_SCL, 400000); }
|
void i2c_init(void) { Wire.begin(MY_DISPLAY_SDA, MY_DISPLAY_SCL, 100000); }
|
||||||
|
|
||||||
void i2c_deinit(void) {
|
void i2c_deinit(void) {
|
||||||
Wire.~TwoWire(); // shutdown/power off I2C hardware
|
Wire.~TwoWire(); // shutdown/power off I2C hardware
|
||||||
@ -14,73 +14,77 @@ void i2c_deinit(void) {
|
|||||||
pinMode(MY_DISPLAY_SCL, INPUT);
|
pinMode(MY_DISPLAY_SCL, INPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
int i2c_scan(void) {
|
void i2c_scan(void) {
|
||||||
|
|
||||||
int i2c_ret, addr;
|
// parts of the code in this function were taken from:
|
||||||
int devices = 0;
|
//
|
||||||
|
// Copyright (c) 2019 BitBank Software, Inc.
|
||||||
|
// Written by Larry Bank
|
||||||
|
// email: bitbank@pobox.com
|
||||||
|
// Project started 25/02/2019
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
BBI2C bbi2c;
|
||||||
|
|
||||||
|
const char *szNames[] = {
|
||||||
|
"Unknown", "SSD1306", "SH1106", "VL53L0X", "BMP180", "BMP280",
|
||||||
|
"BME280", "MPU-60x0", "MPU-9250", "MCP9808", "LSM6DS3", "ADXL345",
|
||||||
|
"ADS1115", "MAX44009", "MAG3110", "CCS811", "HTS221", "LPS25H",
|
||||||
|
"LSM9DS1", "LM8330", "DS3231", "LIS3DH", "LIS3DSH", "INA219",
|
||||||
|
"SHT3X", "HDC1080", "MPU6886", "BME680"};
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Starting I2C bus scan...");
|
ESP_LOGI(TAG, "Starting I2C bus scan...");
|
||||||
|
|
||||||
// block i2c bus access
|
// block i2c bus access
|
||||||
if (I2C_MUTEX_LOCK()) {
|
if (I2C_MUTEX_LOCK()) {
|
||||||
|
|
||||||
// Scan at 100KHz low speed
|
memset(&bbi2c, 0, sizeof(bbi2c));
|
||||||
Wire.setClock(100000);
|
bbi2c.bWire = 1; // use wire library, no bitbanging
|
||||||
|
bbi2c.iSDA = MY_DISPLAY_SDA;
|
||||||
|
bbi2c.iSCL = MY_DISPLAY_SCL;
|
||||||
|
I2CInit(&bbi2c, 100000L); // Scan at 100KHz low speed
|
||||||
|
delay(100); // allow devices to power up
|
||||||
|
|
||||||
for (addr = 8; addr <= 119; addr++) {
|
uint8_t map[16];
|
||||||
|
uint8_t i;
|
||||||
|
int iDevice, iCount;
|
||||||
|
|
||||||
Wire.beginTransmission(addr);
|
I2CScan(&bbi2c, map); // get bitmap of connected I2C devices
|
||||||
Wire.write(addr);
|
if (map[0] == 0xfe) // something is wrong with the I2C bus
|
||||||
i2c_ret = Wire.endTransmission();
|
{
|
||||||
|
ESP_LOGI(TAG, "I2C pins are not correct or the bus is being pulled low "
|
||||||
if (i2c_ret == 0) {
|
"by a bad device; unable to run scan");
|
||||||
devices++;
|
} else {
|
||||||
|
iCount = 0;
|
||||||
switch (addr) {
|
for (i = 1; i < 128; i++) // skip address 0 (general call address) since
|
||||||
|
// more than 1 device can respond
|
||||||
case SSD1306_PRIMARY_ADDRESS:
|
{
|
||||||
case SSD1306_SECONDARY_ADDRESS:
|
if (map[i >> 3] & (1 << (i & 7))) // device found
|
||||||
ESP_LOGI(TAG, "0x%X: SSD1306 Display controller", addr);
|
{
|
||||||
break;
|
iCount++;
|
||||||
|
iDevice = I2CDiscoverDevice(&bbi2c, i);
|
||||||
case BME_PRIMARY_ADDRESS:
|
ESP_LOGI(TAG, "Device found at 0x%X, type = %s", i,
|
||||||
case BME_SECONDARY_ADDRESS:
|
szNames[iDevice]); // show the device name as a string
|
||||||
ESP_LOGI(TAG, "0x%X: Bosch BME MEMS", addr);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case AXP192_PRIMARY_ADDRESS:
|
|
||||||
ESP_LOGI(TAG, "0x%X: AXP192 power management", addr);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case IP5306_PRIMARY_ADDRESS:
|
|
||||||
ESP_LOGI(TAG, "0x%X: IP5306 power management", addr);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case QUECTEL_GPS_PRIMARY_ADDRESS:
|
|
||||||
ESP_LOGI(TAG, "0x%X: Quectel GPS", addr);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MCP_24AA02E64_PRIMARY_ADDRESS:
|
|
||||||
ESP_LOGI(TAG, "0x%X: 24AA02E64 serial EEPROM", addr);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
ESP_LOGI(TAG, "0x%X: Unknown device", addr);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
} // switch
|
} // for i
|
||||||
} // for loop
|
ESP_LOGI(TAG, "%u I2C device(s) found", iCount);
|
||||||
|
}
|
||||||
ESP_LOGI(TAG, "I2C scan done, %u devices found.", devices);
|
|
||||||
|
|
||||||
// Set back to 400KHz
|
|
||||||
Wire.setClock(400000);
|
|
||||||
|
|
||||||
I2C_MUTEX_UNLOCK(); // release i2c bus access
|
I2C_MUTEX_UNLOCK(); // release i2c bus access
|
||||||
} else
|
} else
|
||||||
ESP_LOGE(TAG, "I2c bus busy - scan error");
|
ESP_LOGE(TAG, "I2C bus busy - scan error");
|
||||||
|
|
||||||
return devices;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// mutexed functions for i2c r/w access
|
// mutexed functions for i2c r/w access
|
||||||
|
Loading…
Reference in New Issue
Block a user