/
i2c.h
168 lines (152 loc) · 4.83 KB
/
i2c.h
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* @file drivers/i2c.h
* @version 7.0.0
*
* @section License
* Copyright (C) 2014-2016, Erik Moqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* This file is part of the Simba project.
*/
#ifndef __DRIVERS_I2C_H__
#define __DRIVERS_I2C_H__
#include "simba.h"
#include "i2c_port.h"
/* Predefined baudrates. */
#define I2C_BAUDRATE_3_2MBPS I2C_PORT_BAUDRATE_3_2MBPS
#define I2C_BAUDRATE_1MBPS I2C_PORT_BAUDRATE_1MBPS
#define I2C_BAUDRATE_400KBPS I2C_PORT_BAUDRATE_400KBPS
#define I2C_BAUDRATE_100KBPS I2C_PORT_BAUDRATE_100KBPS
extern struct i2c_device_t i2c_device[I2C_DEVICE_MAX];
/**
* Initialize the i2c module. This function must be called before
* calling any other function in this module.
*
* The module will only be initialized once even if this function is
* called multiple times.
*
* @return zero(0) or negative error code.
*/
int i2c_module_init();
/**
* Initialize given driver object. The same driver object is used for
* both master and slave modes. Use `i2c_start()` to start the device
* as a master, and `i2c_slave_start()` to start it as a slave.
*
* @param[out] self_p Driver object to initialize.
* @param[in] dev_p I2C device to use.
* @param[in] baudrates Bus baudrate when in master mode. Unused in
* slave mode.
* @param[in] address Slave address when in slave mode. Unused in
* master mode.
*
* @return zero(0) or negative error code.
*/
int i2c_init(struct i2c_driver_t *self_p,
struct i2c_device_t *dev_p,
int baudrate,
int address);
/**
* Start given driver object in master mode. Enables data reception
* and transmission, but does not start any transmission. Data
* tarnsfers are started by calling the `i2c_read()` and
* `i2c_write()`.
*
* @param[in] self_p Driver object to initialize.
*
* @return zero(0) or negative error code.
*/
int i2c_start(struct i2c_driver_t *self_p);
/**
* Stop given driver object. Disables data reception and transmission
* in master mode.
*
* @param[in] self_p Driver object to initialize.
*
* @return zero(0) or negative error code.
*/
int i2c_stop(struct i2c_driver_t *self_p);
/**
* Read into given buffer to given slave address.
*
* @param[in] self_p Driver object.
* @param[in] address Slave address to read from.
* @param[out] buf_p Buffer to read into.
* @param[in] size Number of bytes to read.
*
* @return Number of bytes read or negative error code.
*/
ssize_t i2c_read(struct i2c_driver_t *self_p,
int address,
void *buf_p,
size_t size);
/**
* Write given buffer to given slave address.
*
* @param[in] self_p Driver object.
* @param[in] address Slave address to write to.
* @param[in] buf_p Buffer to write.
* @param[in] size Number of bytes to write.
*
* @return Number of bytes written or negative error code.
*/
ssize_t i2c_write(struct i2c_driver_t *self_p,
int address,
const void *buf_p,
size_t size);
/**
* Start given driver object in slave mode. Enables data reception and
* transmission, but does not start any transmission. Data transfers
* are started by calling the `i2c_slave_read()` and
* `i2c_slave_write()`.
*
* @param[in] self_p Driver object to initialize.
*
* @return zero(0) or negative error code.
*/
int i2c_slave_start(struct i2c_driver_t *self_p);
/**
* Stop given driver object. Disables data reception and transmission
* in slave mode.
*
* @param[in] self_p Driver object to initialize.
*
* @return zero(0) or negative error code.
*/
int i2c_slave_stop(struct i2c_driver_t *self_p);
/**
* Read into given buffer from the next master that addresses this
* slave.
*
* @param[in] self_p Driver object.
* @param[out] buf_p Buffer to read into.
* @param[in] size Number of bytes to read.
*
* @return Number of bytes read or negative error code.
*/
ssize_t i2c_slave_read(struct i2c_driver_t *self_p,
void *buf_p,
size_t size);
/**
* Write given buffer to the next master that addresses this
* slave.
*
* @param[in] self_p Driver object.
* @param[in] buf_p Buffer to write.
* @param[in] size Number of bytes to write.
*
* @return Number of bytes written or negative error code.
*/
ssize_t i2c_slave_write(struct i2c_driver_t *self_p,
const void *buf_p,
size_t size);
#endif