|
| 1 | +// |
| 2 | +// I2C Detector - scan and identify devices on an I2C bus using the BitBang_I2C library |
| 3 | +// |
| 4 | +// The purpose of this code is to provide a sample sketch which can serve |
| 5 | +// to detect not only the addresses of I2C devices, but what type of device each one is. |
| 6 | +// So far, I've added the 25 devices I've personally used or found to be reliably detected |
| 7 | +// based on their register contents. I encourage people to do pull requests to add support |
| 8 | +// for more devices to make this code have wider appeal. |
| 9 | + |
| 10 | +// There are plenty of I2C devices which appear at fixed addresses, yet don't have unique |
| 11 | +// "Who_Am_I" registers or other data to reliably identify them. It's certainly possible to |
| 12 | +// write code which initializes these devices and tries to verify their identity. This can |
| 13 | +// potentially damage them and would necessarily increase the code size. I would like to keep |
| 14 | +// the size of this code small enough so that it can be included in many microcontroller |
| 15 | +// projects where code space is scarce. |
| 16 | + |
| 17 | +// Copyright (c) 2019 BitBank Software, Inc. |
| 18 | +// Written by Larry Bank |
| 19 | + |
| 20 | +// Project started 25/02/2019 |
| 21 | +// |
| 22 | +// This program is free software: you can redistribute it and/or modify |
| 23 | +// it under the terms of the GNU General Public License as published by |
| 24 | +// the Free Software Foundation, either version 3 of the License, or |
| 25 | +// (at your option) any later version. |
| 26 | +// |
| 27 | +// This program is distributed in the hope that it will be useful, |
| 28 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 29 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 30 | +// GNU General Public License for more details. |
| 31 | +// |
| 32 | +// You should have received a copy of the GNU General Public License |
| 33 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 34 | +// |
| 35 | +// Uses my Bit Bang I2C library. You can find it here: |
| 36 | +// https://github.com/bitbank2/BitBang_I2C |
| 37 | +#include <stdlib.h> |
| 38 | +#include <string.h> |
| 39 | +#include <stdio.h> |
| 40 | +#include <stdint.h> |
| 41 | +#include <inttypes.h> |
| 42 | +#include <BitBang_I2C.h> |
| 43 | +#include <pigpio.h> |
| 44 | + |
| 45 | +#define SHOW_NAME |
| 46 | +#ifdef SHOW_NAME |
| 47 | +const char *szNames[] = {"Unknown","SSD1306","SH1106","VL53L0X","BMP180", "BMP280","BME280", |
| 48 | + "MPU-60x0", "MPU-9250", "MCP9808","LSM6DS3", "ADXL345", "ADS1115","MAX44009", |
| 49 | + "MAG3110", "CCS811", "HTS221", "LPS25H", "LSM9DS1","LM8330", "DS3231", "LIS3DH", |
| 50 | + "LIS3DSH","INA219","SHT3X","HDC1080","MPU6886","BME680", "AXP202", "AXP192", "24AA02XEXX", |
| 51 | + "DS1307", "MPU688X", "FT6236G", "FT6336G", "FT6336U", "FT6436", "BM8563","BNO055"}; |
| 52 | +#endif |
| 53 | + |
| 54 | +BBI2C bbi2c; |
| 55 | + |
| 56 | +int main(int argc, char **argv) |
| 57 | +{ |
| 58 | +int i, iSDA, iSCL; |
| 59 | +uint8_t map[16]; |
| 60 | +int iDevice, iCount; |
| 61 | + |
| 62 | + if (argc != 3) { |
| 63 | + printf("Bit Bang I2C detector\nUsage: i2c_detect <SDA_PIN> <SCL_PIN>\n"); |
| 64 | + return 0; |
| 65 | + } |
| 66 | + |
| 67 | + iSDA = atoi(argv[1]); |
| 68 | + iSCL = atoi(argv[2]); |
| 69 | + memset(&bbi2c, 0, sizeof(bbi2c)); |
| 70 | + bbi2c.bWire = 0; // use bit bang, not wire library |
| 71 | + bbi2c.iSDA = iSDA; |
| 72 | + bbi2c.iSCL = iSCL; |
| 73 | + I2CInit(&bbi2c, 100000L); |
| 74 | + |
| 75 | + printf("Starting I2C Scan\n"); |
| 76 | + I2CScan(&bbi2c, map); // get bitmap of connected I2C devices |
| 77 | + if (map[0] == 0xfe) // something is wrong with the I2C bus |
| 78 | + { |
| 79 | + printf("I2C pins are not correct or the bus is being pulled low by a bad device; unable to run scan\n"); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + iCount = 0; |
| 84 | + for (i=1; i<128; i++) // skip address 0 (general call address) since more than 1 device can respond |
| 85 | + { |
| 86 | + if (map[i>>3] & (1 << (i & 7))) // device found |
| 87 | + { |
| 88 | + iCount++; |
| 89 | + iDevice = I2CDiscoverDevice(&bbi2c, i); |
| 90 | + printf("Device found at 0x%02x, type = %s\n", i, szNames[iDevice]); |
| 91 | + } |
| 92 | + } // for i |
| 93 | + printf("%d device(s) found\n", iCount); |
| 94 | + } |
| 95 | + return 0; |
| 96 | +} |
0 commit comments