WPILibC++ 2024.3.2
I2C.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include "hal/I2CTypes.h"
10
11/**
12 * @defgroup hal_i2c I2C Functions
13 * @ingroup hal_capi
14 * @{
15 */
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21/**
22 * Initializes the I2C port.
23 *
24 * Opens the port if necessary and saves the handle.
25 * If opening the MXP port, also sets up the channel functions appropriately.
26 *
27 * @param[in] port The port to open, 0 for the on-board, 1 for the MXP.
28 * @param[out] status Error status variable. 0 on success.
29 */
30void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status);
31
32/**
33 * Generic I2C read/write transaction.
34 *
35 * This is a lower-level interface to the I2C hardware giving you more control
36 * over each transaction.
37 *
38 * @param port The I2C port, 0 for the on-board, 1 for the MXP.
39 * @param deviceAddress The address of the register on the device to be
40 * read/written.
41 * @param dataToSend Buffer of data to send as part of the transaction.
42 * @param sendSize Number of bytes to send as part of the transaction.
43 * @param dataReceived Buffer to read data into.
44 * @param receiveSize Number of bytes to read from the device.
45 * @return >= 0 on success or -1 on transfer abort.
46 */
47int32_t HAL_TransactionI2C(HAL_I2CPort port, int32_t deviceAddress,
48 const uint8_t* dataToSend, int32_t sendSize,
49 uint8_t* dataReceived, int32_t receiveSize);
50
51/**
52 * Executes a write transaction with the device.
53 *
54 * Writes a single byte to a register on a device and wait until the
55 * transaction is complete.
56 *
57 * @param port The I2C port, 0 for the on-board, 1 for the MXP.
58 * @param deviceAddress The address of the register on the device to be
59 * written.
60 * @param dataToSend The byte to write to the register on the device.
61 * @param sendSize Number of bytes to send.
62 * @return >= 0 on success or -1 on transfer abort.
63 */
64int32_t HAL_WriteI2C(HAL_I2CPort port, int32_t deviceAddress,
65 const uint8_t* dataToSend, int32_t sendSize);
66
67/**
68 * Executes a read transaction with the device.
69 *
70 * Reads bytes from a device.
71 * Most I2C devices will auto-increment the register pointer internally allowing
72 * you to read consecutive registers on a device in a single transaction.
73 *
74 * @param port The I2C port, 0 for the on-board, 1 for the MXP.
75 * @param deviceAddress The register to read first in the transaction.
76 * @param count The number of bytes to read in the transaction.
77 * @param buffer A pointer to the array of bytes to store the data read from the
78 * device.
79 * @return >= 0 on success or -1 on transfer abort.
80 */
81int32_t HAL_ReadI2C(HAL_I2CPort port, int32_t deviceAddress, uint8_t* buffer,
82 int32_t count);
83
84/**
85 * Closes an I2C port
86 *
87 * @param port The I2C port, 0 for the on-board, 1 for the MXP.
88 */
90#ifdef __cplusplus
91} // extern "C"
92#endif
93/** @} */
HAL_I2CPort
Definition: I2CTypes.h:17
void HAL_InitializeI2C(HAL_I2CPort port, int32_t *status)
Initializes the I2C port.
int32_t HAL_ReadI2C(HAL_I2CPort port, int32_t deviceAddress, uint8_t *buffer, int32_t count)
Executes a read transaction with the device.
void HAL_CloseI2C(HAL_I2CPort port)
Closes an I2C port.
int32_t HAL_WriteI2C(HAL_I2CPort port, int32_t deviceAddress, const uint8_t *dataToSend, int32_t sendSize)
Executes a write transaction with the device.
int32_t HAL_TransactionI2C(HAL_I2CPort port, int32_t deviceAddress, const uint8_t *dataToSend, int32_t sendSize, uint8_t *dataReceived, int32_t receiveSize)
Generic I2C read/write transaction.
constexpr auto count() -> size_t
Definition: core.h:1203