001package com.pi4j.wiringpi;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  I2C.java
009 *
010 * This file is part of the Pi4J project. More information about
011 * this project can be found here:  https://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2021 Pi4J
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Lesser General Public License as
018 * published by the Free Software Foundation, either version 3 of the
019 * License, or (at your option) any later version.
020 *
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Lesser Public License for more details.
025 *
026 * You should have received a copy of the GNU General Lesser Public
027 * License along with this program.  If not, see
028 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
029 * #L%
030 */
031
032
033import com.pi4j.util.NativeLibraryLoader;
034
035/**
036 * <p>
037 * WiringPi includes a library which can make it easier to use the Raspberry Pi’s on-board I2C interface.
038 * </p>
039 *
040 * <p>
041 * Before you can use the I2C interface, you may need to use the gpio utility to load the I2C drivers into the kernel:
042 *  > gpio load i2c
043 *
044 * If you need a baud rate other than the default 100Kbps, then you can supply this on the command-line:
045 *  > gpio load i2c 1000
046 *
047 * will set the baud rate to 1000Kbps – ie. 1,000,000 bps. (K here is times 1000) *
048 * </p>
049 *
050 * <p>
051 * <blockquote> This library depends on the wiringPi native system library.</br> (developed by
052 * Gordon Henderson @ <a href="http://wiringpi.com/">http://wiringpi.com/</a>)
053 * </blockquote>
054 * </p>
055 *
056 * @see <a href="https://www.pi4j.com/">https://www.pi4j.com</a>
057 * @see <a
058 *      href="http://wiringpi.com/reference/i2c-library/">http://wiringpi.com/reference/i2c-library</a>
059 * @author Robert Savage (<a
060 *         href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
061 */
062public class I2C {
063
064    public static int CHANNEL_0 = 0;
065    public static int CHANNEL_1 = 1;
066
067    // private constructor
068    private I2C()  {
069        // forbid object construction
070    }
071
072    static {
073        // Load the platform library
074        NativeLibraryLoader.load("libpi4j.so", "pi4j");
075    }
076
077    /**
078     * <p>wiringPiI2CSetup:</p>
079     *
080     * <p>
081     * This initialises the I2C system with your given device identifier. The ID is the I2C number of the device
082     * and you can use the i2cdetect program to find this out. wiringPiI2CSetup() will work out which revision
083     * Raspberry Pi you have and open the appropriate device in /dev.
084     * </p>
085     *
086     * <p>
087     * The return value is the standard Linux filehandle, or -1 if any error – in which case, you can consult
088     * errno as usual.
089     * </p>
090     *
091     * <p>
092     * E.g. the popular MCP23017 GPIO expander is usually device Id 0×20, so this is the number you would pass
093     * into wiringPiI2CSetup().
094     * </p>
095     *
096     * @see <a
097     *      href="http://wiringpi.com/reference/i2c-library/">http://wiringpi.com/reference/i2c-library</a>
098     *
099     * @param devid I2C device id
100     * @return return -1 on error; else returns Linux file handle
101     */
102    public static native int wiringPiI2CSetup(int devid);
103
104    /**
105     * <p>wiringPiI2CRead:</p>
106     *
107     * <p>
108     * Simple I2C device read. Some devices present data when you read them without having to do any register transactions.
109     * </p>
110     *
111     * @see <a
112     *      href="http://wiringpi.com/reference/i2c-library/">http://wiringpi.com/reference/i2c-library</a>
113     *
114     * @param fd Linux file handle obtained from call to wiringPiI2CSetup
115     * @return return -1 on error; else data read from I2C device
116     */
117    public static native int wiringPiI2CRead(int fd);
118
119    /**
120     * <p>wiringPiI2CWrite:</p>
121     *
122     * <p>
123     * Simple I2C device write. Some devices accept data this way without needing to access any internal registers.
124     * </p>
125     *
126     * @see <a
127     *      href="http://wiringpi.com/reference/i2c-library/">http://wiringpi.com/reference/i2c-library</a>
128     *
129     * @param fd Linux file handle obtained from call to wiringPiI2CSetup
130     * @param data data to write
131     * @return return -1 on error
132     */
133    public static native int wiringPiI2CWrite(int fd, int data);
134
135    /**
136     * <p>wiringPiI2CWriteReg8:</p>
137     *
138     * <p>
139     * I2C device write. Write an 8-bit data value into the device register indicated.
140     * </p>
141     *
142     * @see <a
143     *      href="http://wiringpi.com/reference/i2c-library/">http://wiringpi.com/reference/i2c-library</a>
144     *
145     * @param fd Linux file handle obtained from call to wiringPiI2CSetup
146     * @param reg I2C device register address
147     * @param data data byte to write (8 bits/1 byte)
148     * @return return -1 on error
149     */
150    public static native int wiringPiI2CWriteReg8(int fd, int reg, int data);
151
152    /**
153     * <p>wiringPiI2CWriteReg16:</p>
154     *
155     * <p>
156     * I2C device write. Write a 16-bit data value into the device register indicated.
157     * </p>
158     *
159     * @see <a
160     *      href="http://wiringpi.com/reference/i2c-library/">http://wiringpi.com/reference/i2c-library</a>
161     *
162     * @param fd Linux file handle obtained from call to wiringPiI2CSetup
163     * @param reg I2C device register address
164     * @param data data bytes to write (16 bits/2 bytes)
165     * @return return -1 on error
166     */
167    public static native int wiringPiI2CWriteReg16(int fd, int reg, int data);
168
169    /**
170     * <p>wiringPiI2CReadReg8:</p>
171     *
172     * <p>
173     * I2C device read. Read an 8-bit (1 byte) data value from the device register indicated.
174     * </p>
175     *
176     * @see <a
177     *      href="http://wiringpi.com/reference/i2c-library/">http://wiringpi.com/reference/i2c-library</a>
178     *
179     * @param fd Linux file handle obtained from call to wiringPiI2CSetup
180     * @param reg I2C device register address
181     * @return return -1 on error; else returns 8-bit value read from I2C device register
182     */
183    public static native int wiringPiI2CReadReg8(int fd, int reg);
184
185    /**
186     * <p>wiringPiI2CReadReg16:</p>
187     *
188     * <p>
189     * I2C device read. Read a 16-bit (2 bytes) data value from the device register indicated.
190     * </p>
191     *
192     * @see <a
193     *      href="http://wiringpi.com/reference/i2c-library/">http://wiringpi.com/reference/i2c-library</a>
194     *
195     * @param fd Linux file handle obtained from call to wiringPiI2CSetup
196     * @param reg I2C device register address
197     * @return return -1 on error; else returns 16-bit value read from I2C device register
198     */
199    public static native int wiringPiI2CReadReg16(int fd, int reg);
200}