I2C device discovery and printout of known device names

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers i2cDevices.h Source File

i2cDevices.h

00001 #ifndef I2CDEVICES_H
00002 #define I2CDEVICES_H
00003 
00004 #include "DevI2C.h"
00005 
00006 //if the use of low-level I2C routines is required, uncomment out the next two lines 
00007 //#define I2CLOWLEVEL     1   
00008 //#warning "using low level I2C routines"
00009 
00010 /** Software routines that show the I2C devices connected to the I2C bus. The
00011  *  routine also sets flags that indicate certain I2C devices have been 
00012  *  detected.  Those flags are within a "typedef" structure.
00013  *
00014  * NOTE: Requires the external definition of the user console: "RawSerial pc;"
00015  *
00016  * @code
00017  * #include "mbed.h"
00018  * #include "i2cDevices.h"
00019  *
00020  * RawSerial pc(USBTX, USBRX);
00021  * DigitalOut led1(LED1);
00022  *
00023  * #define SDA         PTE0     //i2c bus for KL25Z
00024  * #define SCL         PTE1
00025  *
00026  * I2C i2c1(SDA, SCL);
00027  * i2c_discovery i2c_d(SDA, SCL, "i2c_d");
00028  * i2c_discovery::i2cDeviceFlags i2c_flags = {};
00029  *
00030  * int main() {
00031  *     pc.baud(230400);
00032  *     i2c1.frequency(400000);
00033  *     pc.print("\r\n\r\nbegin....\r\n");
00034  *     i2c_d.find_i2c(i2c_flags);
00035  *     if(i2c_flags.PCA9955_0 == true) {
00036  *         pc.printf("Device detected!\r\n");
00037  *     } else {
00038  *         pc.printf("Device not found!!!\r\n");
00039  *     }
00040  *     while(1) {
00041  *         led1 = !led1;
00042  *         wait(1.0);
00043  *     }
00044  * }
00045  *
00046  * @endcode
00047  */
00048 
00049 //used to define terminal text output color
00050 #define COLOR_TERM
00051 #ifdef COLOR_TERM
00052     #define DOBLUE      "\033[0;34;2m"
00053     #define DOMAGENTA   "\033[0;35;2m"
00054     #define DONONE      "\033[0m"
00055 #else
00056     #define DOBLUE      ""
00057     #define DOMAGENTA   ""
00058     #define DONONE      ""
00059 #endif
00060 
00061 //list of i2c device possibilities
00062 #define I2C_0x02    "PCA9955-GGG"
00063 #define I2C_0x06    "SHT1x"
00064 #define I2C_0x0C    "AK8975"
00065 
00066 #define I2C_0x1C    "MAG3110"
00067 
00068 #define I2C_0x20    "VEML6040, VEML6075"
00069 
00070 #define I2C_0x38    "MMA8451-0, FXOS8700CQ-2"
00071 #define I2C_0x3A    "MMA8451-1, FXOS8700CQ-1"
00072 #define I2C_0x3C    "FXOS8700CQ-0, LIS3MDL"
00073 #define I2C_0x3E    "FXOS8700CQ-3"
00074 
00075 #define I2C_0x42    "MCP23017-b"
00076 #define I2C_0x44    "MCP23017-c"
00077 #define I2C_0x46    "MCP23017-d"
00078 
00079 #define I2C_0x50    "MAX5387-ggg"
00080 #define I2C_0x52    "MAX5387-ggv, VL53L0X"
00081 #define I2C_0x54    "MAX5387-gvg"
00082 #define I2C_0x56    "MAX5387-gvv"
00083 #define I2C_0x58    "MAX5387-vgg"
00084 #define I2C_0x5A    "MAX5387-vgv"
00085 #define I2C_0x5C    "MAX5387-vvg"
00086 #define I2C_0x5E    "MAX5387-vvv"
00087 
00088 #define I2C_0x70    "VEML6070-ld/c"
00089 #define I2C_0x72    "VEML6070-hd"
00090 
00091 #define I2C_0x80    "Si7005, Si7021, HTU21D, HDC1080, INA2xx/MAX7300-gg"
00092 #define I2C_0x82    "INA2xx/MAX7300-gv"
00093 #define I2C_0x84    "INA2xx/MAX7300-gd"
00094 #define I2C_0x86    "INA2xx/MAX7300-gc"
00095 #define I2C_0x88    "INA2xx/MAX7300-vg"
00096 #define I2C_0x8A    "INA2xx/MAX7300-vv"
00097 #define I2C_0x8C    "INA2xx/MAX7300-vd"
00098 #define I2C_0x8E    "INA2xx/MAX7300-vc"
00099 
00100 #define I2C_0x90    "AD7416-a, LM75, INA2xx/MAX7300-dg"
00101 #define I2C_0x92    "MAX9768, INA2xx/MAX7300-dv"
00102 #define I2C_0x94    "INA2xx/MAX7300-dd"
00103 #define I2C_0x96    "INA2xx/MAX7300-dc"
00104 #define I2C_0x98    "INA2xx/MAX7300-cg"
00105 #define I2C_0x9A    "INA2xx/MAX7300-cv"
00106 #define I2C_0x9C    "INA2xx/MAX7300-cd"
00107 #define I2C_0x9E    "INA2xx/MAX7300-cc"
00108 
00109 #define I2C_0xA0    "24FC512-000, 24AA02-b0"
00110 #define I2C_0xA2    "24AA02-b1"
00111 #define I2C_0xA4    "24AA02-b2"
00112 #define I2C_0xA6    "24AA02-b3"
00113 #define I2C_0xA8    "24AA02-b4"
00114 #define I2C_0xAA    "MMA8491, 24AA02-b5, BQ27xx0, BQ34Z100-G1"
00115 #define I2C_0xAC    "24AA02-b6, M24SR64-Y"
00116 #define I2C_0xAE    "24AA02-b7"
00117 
00118 #define I2C_0xB4    "CCS811-0"
00119 #define I2C_0xB6    "CCS811-1"
00120 #define I2C_0xBA    "LPS22HB"
00121 #define I2C_0xBE    "HTS221"
00122 
00123 #define I2C_0xC0    "MPL3115A2, MLX90620-ram, PCA9955-0"
00124 #define I2C_0xC2    "SCD30"
00125 
00126 #define I2C_0xD0    "M41T11, DS1307, DS3232M, MCU9150-0"
00127 #define I2C_0xD2    "MCU9150-1"
00128 #define I2C_0xD4    "LSM6DSL"
00129 #define I2C_0xDE    "MCP7941x-rtc"
00130 
00131 #define I2C_0xE0    "PCA9955-AllCall"
00132 #define I2C_0xEC    "MS5611-cs1, BME280-cs0, PCA9955-SubCall"
00133 #define I2C_0xEE    "MS5611-cs0, BME280-cs1, BMP-05"
00134 
00135 /** Create an i2c controller class
00136  *
00137  * @param i2c class class
00138  *
00139  */
00140 class i2c_discovery
00141 {
00142     public:
00143     /** Structure which is used to exchange indicate certain
00144      *  devices of interest is detected or not by this routine.
00145      */    
00146     typedef struct {
00147         bool rtcDS_ok ;          /*!< i2c address 0xd0 */
00148         bool ms5611_ok ;         /*!< i2c address 0xee */
00149         bool bme280_cs0_ok ;     /*!< i2c address 0xec */
00150         bool bme280_cs1_ok ;     /*!< i2c address 0xee */
00151         bool mma8451cs1_ok ;     /*!< i2c address 0x3a */
00152         bool HTU21D_ok ;         /*!< i2c address 0x80 */
00153         bool INA219_MAX7300_gg_ok ; /*!< i2c address 0x80 */
00154         bool INA230_MAX7300_gg_ok ; /*!< i2c address 0x80 */
00155         bool PCA9955_GGG_ok ;    /*!< i2c address 0xc0 */
00156         bool VEML60xx_ok ;       /*!< i2c address 0x20 */
00157         bool VEML6070_ok ;       /*!< i2c address 0x70 */
00158         bool eeprom000_ok ;      /*!< i2c address 0xa0 */
00159         bool mlx90620_ok ;       /*!< i2c address 0xc0 */
00160         bool SCD30_ok ;          /*!< i2c address 0xc2 */
00161         bool bq27210_ok ;        /*!< i2c address 0xaa */
00162         bool ccs811_0_ok ;       /*!< i2c address 0xb4 */
00163         bool ccs811_1_ok ;       /*!< i2c address 0xb6 */
00164         bool mcp7941x_ok ;       /*!< i2c address 0xde */
00165     } i2cDeviceFlags;
00166 
00167     /**
00168      * Constructor, uses default I2C clock speed
00169      * - Fixed at I2C address 0x80
00170      * - I2C speed set to 400KHz
00171      *
00172      * @param sda - mbed I2C interface pin
00173      * @param scl - mbed I2C interface pin
00174      * @param name - name of i2c_discovery
00175      */
00176     i2c_discovery(PinName sda, PinName scl, const char* name);
00177     
00178     /**
00179      * Clear I2C device Flags
00180      * 
00181      * @param i2cDeviceFlags pointer
00182      *
00183      * @return --none--
00184     */    
00185     void clearFlags(i2cDeviceFlags& i2cFlags);
00186     
00187     /**
00188      * Go and search for I2C devices
00189      * 
00190      * @param i2cDeviceFlags pointer
00191      *
00192      * @return count - number of i2c devices detected
00193     */     
00194     int find_i2c(i2cDeviceFlags& i2cFlags);
00195     
00196     private:
00197 
00198     int showI2C(i2cDeviceFlags& i2cFlags, char addr);
00199     int dispI2C(i2cDeviceFlags& i2cFlags);
00200     
00201     protected:
00202     
00203     DevI2C _i2c;
00204 
00205 };
00206 
00207 #endif