001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package org.wpilib.hardware.hal;
006
007import java.nio.ByteBuffer;
008
009/**
010 * I2C HAL JNI functions.
011 *
012 * @see "wpi/hal/I2C.h"
013 */
014public class I2CJNI extends JNIWrapper {
015  /**
016   * Initializes the I2C port.
017   *
018   * <p>Opens the port if necessary and saves the handle.
019   *
020   * @param port The port to open.
021   * @see "HAL_InitializeI2C"
022   */
023  public static native void i2CInitialize(int port);
024
025  /**
026   * Generic I2C read/write transaction.
027   *
028   * <p>This is a lower-level interface to the I2C hardware giving you more control over each
029   * transaction.
030   *
031   * @param port The I2C port.
032   * @param address The address of the register on the device to be read/written.
033   * @param dataToSend Buffer of data to send as part of the transaction.
034   * @param sendSize Number of bytes to send as part of the transaction.
035   * @param dataReceived Buffer to read data into.
036   * @param receiveSize Number of bytes to read from the device.
037   * @return &gt;= 0 on success or -1 on transfer abort.
038   * @see "HAL_TransactionI2C"
039   */
040  public static native int i2CTransaction(
041      int port,
042      byte address,
043      ByteBuffer dataToSend,
044      byte sendSize,
045      ByteBuffer dataReceived,
046      byte receiveSize);
047
048  /**
049   * Generic I2C read/write transaction.
050   *
051   * <p>This is a lower-level interface to the I2C hardware giving you more control over each
052   * transaction.
053   *
054   * @param port The I2C port.
055   * @param address The address of the register on the device to be read/written.
056   * @param dataToSend Buffer of data to send as part of the transaction.
057   * @param sendSize Number of bytes to send as part of the transaction.
058   * @param dataReceived Buffer to read data into.
059   * @param receiveSize Number of bytes to read from the device.
060   * @return &gt;= 0 on success or -1 on transfer abort.
061   * @see "HAL_TransactionI2C"
062   */
063  public static native int i2CTransactionB(
064      int port,
065      byte address,
066      byte[] dataToSend,
067      byte sendSize,
068      byte[] dataReceived,
069      byte receiveSize);
070
071  /**
072   * Executes a write transaction with the device.
073   *
074   * <p>Writes a single byte to a register on a device and wait until the transaction is complete.
075   *
076   * @param port The I2C port.
077   * @param address The address of the register on the device to be written.
078   * @param dataToSend The byte to write to the register on the device.
079   * @param sendSize Number of bytes to send.
080   * @return &gt;= 0 on success or -1 on transfer abort.
081   * @see "HAL_WriteI2C"
082   */
083  public static native int i2CWrite(int port, byte address, ByteBuffer dataToSend, byte sendSize);
084
085  /**
086   * Executes a write transaction with the device.
087   *
088   * <p>Writes a single byte to a register on a device and wait until the transaction is complete.
089   *
090   * @param port The I2C port.
091   * @param address The address of the register on the device to be written.
092   * @param dataToSend The byte to write to the register on the device.
093   * @param sendSize Number of bytes to send.
094   * @return &gt;= 0 on success or -1 on transfer abort.
095   * @see "HAL_WriteI2C"
096   */
097  public static native int i2CWriteB(int port, byte address, byte[] dataToSend, byte sendSize);
098
099  /**
100   * Executes a read transaction with the device.
101   *
102   * <p>Reads bytes from a device. Most I2C devices will auto-increment the register pointer
103   * internally allowing you to read consecutive registers on a device in a single transaction.
104   *
105   * @param port The I2C port.
106   * @param address The register to read first in the transaction.
107   * @param dataReceived A ByteBuffer to store the data read from the device.
108   * @param receiveSize The number of bytes to read in the transaction.
109   * @return &gt;= 0 on success or -1 on transfer abort.
110   * @see "HAL_ReadI2C"
111   */
112  public static native int i2CRead(
113      int port, byte address, ByteBuffer dataReceived, byte receiveSize);
114
115  /**
116   * Executes a read transaction with the device.
117   *
118   * <p>Reads bytes from a device. Most I2C devices will auto-increment the register pointer
119   * internally allowing you to read consecutive registers on a device in a single transaction.
120   *
121   * @param port The I2C port.
122   * @param address The register to read first in the transaction.
123   * @param dataReceived A byte array to store the data read from the device.
124   * @param receiveSize The number of bytes to read in the transaction.
125   * @return &gt;= 0 on success or -1 on transfer abort.
126   * @see "HAL_ReadI2C"
127   */
128  public static native int i2CReadB(int port, byte address, byte[] dataReceived, byte receiveSize);
129
130  /**
131   * Closes an I2C port.
132   *
133   * @param port The I2C port.
134   * @see "HAL_CloseI2C"
135   */
136  public static native void i2CClose(int port);
137
138  /** Utility class. */
139  private I2CJNI() {}
140}