WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
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 "wpi/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 *
26 * @param[in] port The port to open.
27 * @param[out] status Error status variable. 0 on success.
28 */
29void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status);
30
31/**
32 * Generic I2C read/write transaction.
33 *
34 * This is a lower-level interface to the I2C hardware giving you more control
35 * over each transaction.
36 *
37 * @param port The I2C port.
38 * @param deviceAddress The address of the register on the device to be
39 * read/written.
40 * @param dataToSend Buffer of data to send as part of the transaction.
41 * @param sendSize Number of bytes to send as part of the transaction.
42 * @param dataReceived Buffer to read data into.
43 * @param receiveSize Number of bytes to read from the device.
44 * @return >= 0 on success or -1 on transfer abort.
45 */
46int32_t HAL_TransactionI2C(HAL_I2CPort port, int32_t deviceAddress,
47 const uint8_t* dataToSend, int32_t sendSize,
48 uint8_t* dataReceived, int32_t receiveSize);
49
50/**
51 * Executes a write transaction with the device.
52 *
53 * Writes a single byte to a register on a device and wait until the
54 * transaction is complete.
55 *
56 * @param port The I2C port.
57 * @param deviceAddress The address of the register on the device to be
58 * written.
59 * @param dataToSend The byte to write to the register on the device.
60 * @param sendSize Number of bytes to send.
61 * @return >= 0 on success or -1 on transfer abort.
62 */
63int32_t HAL_WriteI2C(HAL_I2CPort port, int32_t deviceAddress,
64 const uint8_t* dataToSend, int32_t sendSize);
65
66/**
67 * Executes a read transaction with the device.
68 *
69 * Reads bytes from a device.
70 * Most I2C devices will auto-increment the register pointer internally allowing
71 * you to read consecutive registers on a device in a single transaction.
72 *
73 * @param port The I2C port.
74 * @param deviceAddress The register to read first in the transaction.
75 * @param count The number of bytes to read in the transaction.
76 * @param buffer A pointer to the array of bytes to store the data read from the
77 * device.
78 * @return >= 0 on success or -1 on transfer abort.
79 */
80int32_t HAL_ReadI2C(HAL_I2CPort port, int32_t deviceAddress, uint8_t* buffer,
81 int32_t count);
82
83/**
84 * Closes an I2C port
85 *
86 * @param port The I2C port.
87 */
89#ifdef __cplusplus
90} // extern "C"
91#endif
92/** @} */
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.