RTEMS 7.0-rc1
Loading...
Searching...
No Matches
i2c.h
1/* SPDX-License-Identifier: GPL-2.0+-with-RTEMS-exception */
2
3/*
4 * Generic I2C bus interface for RTEMS
5 *
6 * Copyright (C) 2000 OKTET Ltd., St.-Petersburg, Russia
7 * Author: Victor V. Vengerov <vvv@oktet.ru>
8 *
9 * The license and distribution terms for this file may be
10 * found in the file LICENSE in this distribution or at
11 *
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#ifndef __RTEMS__I2C_H__
16#define __RTEMS__I2C_H__
17
18#include <rtems.h>
19#include <bsp.h>
20/* This header file define the generic interface to i2c buses available in
21 * system. This interface may be used by user applications or i2c-device
22 * drivers (like RTC, NVRAM, etc).
23 *
24 * Functions i2c_initialize and i2c_transfer declared in this header usually
25 * implemented in particular board support package. Usually this
26 * implementation is a simple wrapper or multiplexor to I2C controller
27 * driver which is available in system. It may be generic "software
28 * controller" I2C driver which control SDA and SCL signals directly (if SDA
29 * and SCL is general-purpose I/O pins), or driver for hardware I2C
30 * controller (standalone or integrated with processors: MBus controller in
31 * ColdFire processors, I2C controller in PowerQUICC and so on).
32 *
33 * i2c_transfer is a very generic low-level function. Higher-level function
34 * i2c_write, i2c_read, i2c_wrrd, i2c_wbrd is defined here too.
35 */
36
37/* I2C Bus Number type */
38typedef uint32_t i2c_bus_number;
39
40/* I2C device address */
41typedef uint16_t i2c_address;
42
43/* I2C error codes generated during message transfer */
44typedef enum i2c_message_status {
45 I2C_SUCCESSFUL = 0,
47 I2C_NO_DEVICE,
48 I2C_ARBITRATION_LOST,
49 I2C_NO_ACKNOWLEDGE,
50 I2C_NO_DATA,
51 I2C_RESOURCE_NOT_AVAILABLE
52} i2c_message_status;
53
54/* I2C Message */
55typedef struct i2c_message {
56 i2c_address addr; /* I2C slave device address */
57 uint16_t flags; /* message flags (see below) */
58 i2c_message_status status; /* message transfer status code */
59 uint16_t len; /* Number of bytes to read or write */
60 uint8_t *buf; /* pointer to data array */
62
63/* I2C message flag */
64#define I2C_MSG_ADDR_10 (0x01) /* 10-bit address */
65#define I2C_MSG_WR (0x02) /* transfer direction for this message
66 from master to slave */
67#define I2C_MSG_ERRSKIP (0x04) /* Skip message if last transfered message
68 is failed */
69/* Type for function which is called when transfer over I2C bus is finished */
70typedef void (*i2c_transfer_done) (void * arg);
71
72/* i2c_initialize --
73 * I2C driver initialization. This function usually called on device
74 * driver initialization state, before initialization task. All I2C
75 * buses are initialized; reasonable slow data transfer rate is
76 * selected for each bus.
77 *
78 * PARAMETERS:
79 * major - I2C device major number
80 * minor - I2C device minor number
81 * arg - RTEMS driver initialization argument
82 *
83 * RETURNS:
84 * RTEMS status code
85 */
87i2c_initialize(rtems_device_major_number major,
89 void *arg);
90
91/* i2c_select_clock_rate --
92 * select I2C bus clock rate for specified bus. Some bus controller do not
93 * allow to select arbitrary clock rate; in this case nearest possible
94 * slower clock rate is selected.
95 *
96 * PARAMETERS:
97 * bus - I2C bus number
98 * bps - data transfer rate for this bytes in bits per second
99 *
100 * RETURNS:
101 * RTEMS_SUCCESSFUL, if operation performed successfully,
102 * RTEMS_INVALID_NUMBER, if wrong bus number is specified,
103 * RTEMS_UNSATISFIED, if bus do not support data transfer rate selection
104 * or specified data transfer rate could not be used.
105 */
107i2c_select_clock_rate(i2c_bus_number bus, int bps);
108
109/* i2c_transfer --
110 * Initiate multiple-messages transfer over specified I2C bus or
111 * put request into queue if bus or some other resource is busy. (This
112 * is non-blocking function).
113 *
114 * PARAMETERS:
115 * bus - I2C bus number
116 * nmsg - number of messages
117 * msg - pointer to messages array
118 * done - function which is called when transfer is finished
119 * done_arg_ptr - arbitrary argument ptr passed to done funciton
120 *
121 * RETURNS:
122 * RTEMS_SUCCESSFUL if transfer initiated successfully, or error
123 * code if something failed.
124 */
126i2c_transfer(i2c_bus_number bus, int nmsg, i2c_message *msg,
127 i2c_transfer_done done, void *done_arg);
128
129/* i2c_transfer_wait --
130 * Initiate I2C bus transfer and block until this transfer will be
131 * finished. This function wait the semaphore if system in
132 * SYSTEM_STATE_UP state, or poll done flag in other states.
133 *
134 * PARAMETERS:
135 * bus - I2C bus number
136 * msg - pointer to transfer messages array
137 * nmsg - number of messages in transfer
138 *
139 * RETURNS:
140 * I2C_SUCCESSFUL, if tranfer finished successfully,
141 * I2C_RESOURCE_NOT_AVAILABLE, if semaphore operations has failed,
142 * value of status field of first error-finished message in transfer,
143 * if something wrong.
144 */
145i2c_message_status
146i2c_transfer_wait(i2c_bus_number bus, i2c_message *msg, int nmsg);
147
148/* i2c_poll --
149 * Poll I2C bus controller for events and hanle it. This function is
150 * used when I2C driver operates in poll-driven mode.
151 *
152 * PARAMETERS:
153 * bus - bus number to be polled
154 *
155 * RETURNS:
156 * none
157 */
158void
159i2c_poll(i2c_bus_number bus);
160
161/* i2c_write --
162 * Send single message over specified I2C bus to addressed device and
163 * wait while transfer is finished.
164 *
165 * PARAMETERS:
166 * bus - I2C bus number
167 * addr - address of I2C device
168 * buf - data to be sent to device
169 * size - data buffer size
170 *
171 * RETURNS:
172 * transfer status
173 */
174i2c_message_status
175i2c_write(i2c_bus_number bus, i2c_address addr, void *buf, int size);
176
177/* i2c_wrbyte --
178 * Send single one-byte long message over specified I2C bus to
179 * addressed device and wait while transfer is finished.
180 *
181 * PARAMETERS:
182 * bus - I2C bus number
183 * addr - address of I2C device
184 * cmd - byte message to be sent to device
185 *
186 * RETURNS:
187 * transfer status
188 */
189i2c_message_status
190i2c_wrbyte(i2c_bus_number bus, i2c_address addr, uint8_t cmd);
191
192/* i2c_read --
193 * receive single message over specified I2C bus from addressed device.
194 * This call will wait while transfer is finished.
195 *
196 * PARAMETERS:
197 * bus - I2C bus number
198 * addr - address of I2C device
199 * buf - buffer for received message
200 * size - receive buffer size
201 *
202 * RETURNS:
203 * transfer status
204 */
205i2c_message_status
206i2c_read(i2c_bus_number bus, i2c_address addr, void *buf, int size);
207
208/* i2c_wrrd --
209 * Send message over I2C bus to specified device and receive message
210 * from the same device during single transfer.
211 *
212 * PARAMETERS:
213 * bus - I2C bus number
214 * addr - address of I2C device
215 * bufw - data to be sent to device
216 * sizew - send data buffer size
217 * bufr - buffer for received message
218 * sizer - receive buffer size
219 *
220 * RETURNS:
221 * transfer status
222 */
223i2c_message_status
224i2c_wrrd(i2c_bus_number bus, i2c_address addr, void *bufw, int sizew,
225 void *bufr, int sizer);
226
227/* i2c_wbrd --
228 * Send one-byte message over I2C bus to specified device and receive
229 * message from the same device during single transfer.
230 *
231 * PARAMETERS:
232 * bus - I2C bus number
233 * addr - address of I2C device
234 * cmd - one-byte message to be sent over I2C bus
235 * bufr - buffer for received message
236 * sizer - receive buffer size
237 *
238 * RETURNS:
239 * transfer status
240 */
241i2c_message_status
242i2c_wbrd(i2c_bus_number bus, i2c_address addr, uint8_t cmd,
243 void *bufr, int sizer);
244
245#endif
#define I2C_TIMEOUT
Sets the transfer timeout in 10ms units.
Definition: i2c-dev.h:66
uint32_t rtems_device_major_number
This integer type represents the major number of devices.
Definition: io.h:103
uint32_t rtems_device_minor_number
This integer type represents the minor number of devices.
Definition: io.h:115
rtems_status_code
This enumeration provides status codes for directives of the Classic API.
Definition: status.h:85
This header file defines the RTEMS Classic API.
Definition: i2c.h:55
Definition: b1553brm.c:94