1 |
|
---|
2 | /** \addtogroup hal */
|
---|
3 | /** @{*/
|
---|
4 | /* mbed Microcontroller Library
|
---|
5 | * Copyright (c) 2006-2015 ARM Limited
|
---|
6 | *
|
---|
7 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
8 | * you may not use this file except in compliance with the License.
|
---|
9 | * You may obtain a copy of the License at
|
---|
10 | *
|
---|
11 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
12 | *
|
---|
13 | * Unless required by applicable law or agreed to in writing, software
|
---|
14 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
16 | * See the License for the specific language governing permissions and
|
---|
17 | * limitations under the License.
|
---|
18 | */
|
---|
19 | #ifndef MBED_I2C_API_H
|
---|
20 | #define MBED_I2C_API_H
|
---|
21 |
|
---|
22 | #include "device.h"
|
---|
23 | #include "hal/buffer.h"
|
---|
24 |
|
---|
25 | #if DEVICE_I2C_ASYNCH
|
---|
26 | #include "hal/dma_api.h"
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | #if DEVICE_I2C
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * @defgroup hal_I2CEvents I2C Events Macros
|
---|
33 | *
|
---|
34 | * @{
|
---|
35 | */
|
---|
36 | #define I2C_EVENT_ERROR (1 << 1)
|
---|
37 | #define I2C_EVENT_ERROR_NO_SLAVE (1 << 2)
|
---|
38 | #define I2C_EVENT_TRANSFER_COMPLETE (1 << 3)
|
---|
39 | #define I2C_EVENT_TRANSFER_EARLY_NACK (1 << 4)
|
---|
40 | #define I2C_EVENT_ALL (I2C_EVENT_ERROR | I2C_EVENT_TRANSFER_COMPLETE | I2C_EVENT_ERROR_NO_SLAVE | I2C_EVENT_TRANSFER_EARLY_NACK)
|
---|
41 |
|
---|
42 | /**@}*/
|
---|
43 |
|
---|
44 | #if DEVICE_I2C_ASYNCH
|
---|
45 | /** Asynch I2C HAL structure
|
---|
46 | */
|
---|
47 | typedef struct {
|
---|
48 | struct i2c_s i2c; /**< Target specific I2C structure */
|
---|
49 | struct buffer_s tx_buff; /**< Tx buffer */
|
---|
50 | struct buffer_s rx_buff; /**< Rx buffer */
|
---|
51 | } i2c_t;
|
---|
52 |
|
---|
53 | #else
|
---|
54 | /** Non-asynch I2C HAL structure
|
---|
55 | */
|
---|
56 | typedef struct i2c_s i2c_t;
|
---|
57 |
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | enum {
|
---|
61 | I2C_ERROR_NO_SLAVE = -1,
|
---|
62 | I2C_ERROR_BUS_BUSY = -2
|
---|
63 | };
|
---|
64 |
|
---|
65 | #ifdef __cplusplus
|
---|
66 | extern "C" {
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * \defgroup hal_GeneralI2C I2C Configuration Functions
|
---|
71 | * @{
|
---|
72 | */
|
---|
73 |
|
---|
74 | /** Initialize the I2C peripheral. It sets the default parameters for I2C
|
---|
75 | * peripheral, and configures its specifieds pins.
|
---|
76 | *
|
---|
77 | * @param obj The I2C object
|
---|
78 | * @param sda The sda pin
|
---|
79 | * @param scl The scl pin
|
---|
80 | */
|
---|
81 | void i2c_init(i2c_t *obj, PinName sda, PinName scl);
|
---|
82 |
|
---|
83 | /** Configure the I2C frequency
|
---|
84 | *
|
---|
85 | * @param obj The I2C object
|
---|
86 | * @param hz Frequency in Hz
|
---|
87 | */
|
---|
88 | void i2c_frequency(i2c_t *obj, int hz);
|
---|
89 |
|
---|
90 | /** Send START command
|
---|
91 | *
|
---|
92 | * @param obj The I2C object
|
---|
93 | */
|
---|
94 | int i2c_start(i2c_t *obj);
|
---|
95 |
|
---|
96 | /** Send STOP command
|
---|
97 | *
|
---|
98 | * @param obj The I2C object
|
---|
99 | */
|
---|
100 | int i2c_stop(i2c_t *obj);
|
---|
101 |
|
---|
102 | /** Blocking reading data
|
---|
103 | *
|
---|
104 | * @param obj The I2C object
|
---|
105 | * @param address 7-bit address (last bit is 1)
|
---|
106 | * @param data The buffer for receiving
|
---|
107 | * @param length Number of bytes to read
|
---|
108 | * @param stop Stop to be generated after the transfer is done
|
---|
109 | * @return Number of read bytes
|
---|
110 | */
|
---|
111 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop);
|
---|
112 |
|
---|
113 | /** Blocking sending data
|
---|
114 | *
|
---|
115 | * @param obj The I2C object
|
---|
116 | * @param address 7-bit address (last bit is 0)
|
---|
117 | * @param data The buffer for sending
|
---|
118 | * @param length Number of bytes to write
|
---|
119 | * @param stop Stop to be generated after the transfer is done
|
---|
120 | * @return
|
---|
121 | * zero or non-zero - Number of written bytes
|
---|
122 | * negative - I2C_ERROR_XXX status
|
---|
123 | */
|
---|
124 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop);
|
---|
125 |
|
---|
126 | /** Reset I2C peripheral. TODO: The action here. Most of the implementation sends stop()
|
---|
127 | *
|
---|
128 | * @param obj The I2C object
|
---|
129 | */
|
---|
130 | void i2c_reset(i2c_t *obj);
|
---|
131 |
|
---|
132 | /** Read one byte
|
---|
133 | *
|
---|
134 | * @param obj The I2C object
|
---|
135 | * @param last Acknoledge
|
---|
136 | * @return The read byte
|
---|
137 | */
|
---|
138 | int i2c_byte_read(i2c_t *obj, int last);
|
---|
139 |
|
---|
140 | /** Write one byte
|
---|
141 | *
|
---|
142 | * @param obj The I2C object
|
---|
143 | * @param data Byte to be written
|
---|
144 | * @return 0 if NAK was received, 1 if ACK was received, 2 for timeout.
|
---|
145 | */
|
---|
146 | int i2c_byte_write(i2c_t *obj, int data);
|
---|
147 |
|
---|
148 | /**@}*/
|
---|
149 |
|
---|
150 | #if DEVICE_I2CSLAVE
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * \defgroup SynchI2C Synchronous I2C Hardware Abstraction Layer for slave
|
---|
154 | * @{
|
---|
155 | */
|
---|
156 |
|
---|
157 | /** Configure I2C as slave or master.
|
---|
158 | * @param obj The I2C object
|
---|
159 | * @return non-zero if a value is available
|
---|
160 | */
|
---|
161 | void i2c_slave_mode(i2c_t *obj, int enable_slave);
|
---|
162 |
|
---|
163 | /** Check to see if the I2C slave has been addressed.
|
---|
164 | * @param obj The I2C object
|
---|
165 | * @return The status - 1 - read addresses, 2 - write to all slaves,
|
---|
166 | * 3 write addressed, 0 - the slave has not been addressed
|
---|
167 | */
|
---|
168 | int i2c_slave_receive(i2c_t *obj);
|
---|
169 |
|
---|
170 | /** Configure I2C as slave or master.
|
---|
171 | * @param obj The I2C object
|
---|
172 | * @return non-zero if a value is available
|
---|
173 | */
|
---|
174 | int i2c_slave_read(i2c_t *obj, char *data, int length);
|
---|
175 |
|
---|
176 | /** Configure I2C as slave or master.
|
---|
177 | * @param obj The I2C object
|
---|
178 | * @return non-zero if a value is available
|
---|
179 | */
|
---|
180 | int i2c_slave_write(i2c_t *obj, const char *data, int length);
|
---|
181 |
|
---|
182 | /** Configure I2C address.
|
---|
183 | * @param obj The I2C object
|
---|
184 | * @param idx Currently not used
|
---|
185 | * @param address The address to be set
|
---|
186 | * @param mask Currently not used
|
---|
187 | */
|
---|
188 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask);
|
---|
189 |
|
---|
190 | #endif
|
---|
191 |
|
---|
192 | /**@}*/
|
---|
193 |
|
---|
194 | #if DEVICE_I2C_ASYNCH
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * \defgroup hal_AsynchI2C Asynchronous I2C Hardware Abstraction Layer
|
---|
198 | * @{
|
---|
199 | */
|
---|
200 |
|
---|
201 | /** Start I2C asynchronous transfer
|
---|
202 | *
|
---|
203 | * @param obj The I2C object
|
---|
204 | * @param tx The transmit buffer
|
---|
205 | * @param tx_length The number of bytes to transmit
|
---|
206 | * @param rx The receive buffer
|
---|
207 | * @param rx_length The number of bytes to receive
|
---|
208 | * @param address The address to be set - 7bit or 9bit
|
---|
209 | * @param stop If true, stop will be generated after the transfer is done
|
---|
210 | * @param handler The I2C IRQ handler to be set
|
---|
211 | * @param hint DMA hint usage
|
---|
212 | */
|
---|
213 | void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint32_t address, uint32_t stop, uint32_t handler, uint32_t event, DMAUsage hint);
|
---|
214 |
|
---|
215 | /** The asynchronous IRQ handler
|
---|
216 | *
|
---|
217 | * @param obj The I2C object which holds the transfer information
|
---|
218 | * @return Event flags if a transfer termination condition was met, otherwise return 0.
|
---|
219 | */
|
---|
220 | uint32_t i2c_irq_handler_asynch(i2c_t *obj);
|
---|
221 |
|
---|
222 | /** Attempts to determine if the I2C peripheral is already in use
|
---|
223 | *
|
---|
224 | * @param obj The I2C object
|
---|
225 | * @return Non-zero if the I2C module is active or zero if it is not
|
---|
226 | */
|
---|
227 | uint8_t i2c_active(i2c_t *obj);
|
---|
228 |
|
---|
229 | /** Abort asynchronous transfer
|
---|
230 | *
|
---|
231 | * This function does not perform any check - that should happen in upper layers.
|
---|
232 | * @param obj The I2C object
|
---|
233 | */
|
---|
234 | void i2c_abort_asynch(i2c_t *obj);
|
---|
235 |
|
---|
236 | #endif
|
---|
237 |
|
---|
238 | /**@}*/
|
---|
239 |
|
---|
240 | #ifdef __cplusplus
|
---|
241 | }
|
---|
242 | #endif
|
---|
243 |
|
---|
244 | #endif
|
---|
245 |
|
---|
246 | #endif
|
---|
247 |
|
---|
248 | /** @}*/
|
---|