aboutsummaryrefslogtreecommitdiff
path: root/include/peripheralmanager/i2c_device.h
blob: ca14477c27ff2f1ed1b615570b7006ea3209ba78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SYSTEM_PERIPHERALMANAGER_I2C_DEVICE_H_
#define SYSTEM_PERIPHERALMANAGER_I2C_DEVICE_H_

#include <sys/cdefs.h>
#include <sys/types.h>

__BEGIN_DECLS

/// @defgroup I2c I2c device interface
/// @brief Functions to control an I2C device.
///
/// These functions can be used to control an I2C device.
/// @{

typedef struct BI2cDevice BI2cDevice;

/// Reads from the device.
/// @param device Pointer to the BI2cDevice struct.
/// @param data Output buffer to write the data to.
/// @param len Number of bytes to read.
/// @param bytes_read Output pointer to the number of bytes actually read.
/// @return 0 on success, errno on error
int BI2cDevice_read(const BI2cDevice* device,
                    void* data,
                    uint32_t len,
                    uint32_t* bytes_read);

/// Reads a byte from an I2C register.
/// @param device Pointer to the BI2cDevice struct.
/// @param reg Register to read from.
/// @param val Output pointer to value to read.
/// @return 0 on success, errno on error
int BI2cDevice_readRegByte(const BI2cDevice* device, uint8_t reg, uint8_t* val);

/// Reads a word from an I2C register.
/// @param device Pointer to the BI2cDevice struct.
/// @param reg Register to read from.
/// @param val Output pointer to value to read.
/// @return 0 on success, errno on error
int BI2cDevice_readRegWord(const BI2cDevice* device,
                           uint8_t reg,
                           uint16_t* val);

/// Reads from an I2C register.
/// @param device Pointer to the BI2cDevice struct.
/// @param reg Register to read from.
/// @param data Output buffer to write the data to.
/// @param len Number of bytes to read.
/// @param bytes_read Output pointer to the number of bytes read.
/// @return 0 on success, errno on error
int BI2cDevice_readRegBuffer(const BI2cDevice* device,
                             uint8_t reg,
                             void* data,
                             uint32_t len,
                             uint32_t* bytes_read);

/// Writes to the device.
/// @param device Pointer to the BI2cDevice struct.
/// @param data Buffer to write.
/// @param len Number of bytes to write.
/// @param bytes_written Output pointer to the number of bytes written.
/// @return 0 on success, errno on error
int BI2cDevice_write(const BI2cDevice* device,
                     const void* data,
                     uint32_t len,
                     uint32_t* bytes_written);

/// Writes a byte to an I2C register.
/// @param device Pointer to the BI2cDevice struct.
/// @param reg Register to write to.
/// @param val Value to write.
/// @return 0 on success, errno on error
int BI2cDevice_writeRegByte(const BI2cDevice* device, uint8_t reg, uint8_t val);

/// Writes a word to an I2C register.
/// @param device Pointer to the BI2cDevice struct.
/// @param reg Register to write to.
/// @param val Value to write.
/// @return 0 on success, errno on error
int BI2cDevice_writeRegWord(const BI2cDevice* device,
                            uint8_t reg,
                            uint16_t val);

/// Writes to an I2C register.
/// @param device Pointer to the BI2cDevice struct.
/// @param reg Register to write to.
/// @param data Data to write.
/// @param len Number of bytes to write.
/// @param bytes_written Output pointer to the number of bytes written.
/// @return 0 on success, errno on error
int BI2cDevice_writeRegBuffer(const BI2cDevice* device,
                              uint8_t reg,
                              const void* data,
                              uint32_t len,
                              uint32_t* bytes_written);

/// Destroys a BI2cDevice struct.
/// @param device Pointer to the BI2cDevice struct.
void BI2cDevice_delete(BI2cDevice* device);

/// @}

__END_DECLS

#endif  // SYSTEM_PERIPHERALMANAGER_I2C_DEVICE_H_