source: EcnlProtoTool/trunk/asp3_dcre/mbed/hal/i2c_api.h@ 439

Last change on this file since 439 was 429, checked in by coas-nagasima, 4 years ago

ASP3, TINET, mbed を更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr;charset=UTF-8
File size: 6.5 KB
Line 
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 */
47typedef 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 */
56typedef struct i2c_s i2c_t;
57
58#endif
59
60enum {
61 I2C_ERROR_NO_SLAVE = -1,
62 I2C_ERROR_BUS_BUSY = -2
63};
64
65#ifdef __cplusplus
66extern "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 */
81void 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 */
88void i2c_frequency(i2c_t *obj, int hz);
89
90/** Send START command
91 *
92 * @param obj The I2C object
93 */
94int i2c_start(i2c_t *obj);
95
96/** Send STOP command
97 *
98 * @param obj The I2C object
99 */
100int 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 */
111int 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 */
124int 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 */
130void 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 */
138int 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 */
146int 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 * @param enable_slave Enable i2c hardware so you can receive events with ::i2c_slave_receive
160 * @return non-zero if a value is available
161 */
162void i2c_slave_mode(i2c_t *obj, int enable_slave);
163
164/** Check to see if the I2C slave has been addressed.
165 * @param obj The I2C object
166 * @return The status - 1 - read addresses, 2 - write to all slaves,
167 * 3 write addressed, 0 - the slave has not been addressed
168 */
169int i2c_slave_receive(i2c_t *obj);
170
171/** Configure I2C as slave or master.
172 * @param obj The I2C object
173 * @param data The buffer for receiving
174 * @param length Number of bytes to read
175 * @return non-zero if a value is available
176 */
177int i2c_slave_read(i2c_t *obj, char *data, int length);
178
179/** Configure I2C as slave or master.
180 * @param obj The I2C object
181 * @param data The buffer for sending
182 * @param length Number of bytes to write
183 * @return non-zero if a value is available
184 */
185int i2c_slave_write(i2c_t *obj, const char *data, int length);
186
187/** Configure I2C address.
188 * @param obj The I2C object
189 * @param idx Currently not used
190 * @param address The address to be set
191 * @param mask Currently not used
192 */
193void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask);
194
195#endif
196
197/**@}*/
198
199#if DEVICE_I2C_ASYNCH
200
201/**
202 * \defgroup hal_AsynchI2C Asynchronous I2C Hardware Abstraction Layer
203 * @{
204 */
205
206/** Start I2C asynchronous transfer
207 *
208 * @param obj The I2C object
209 * @param tx The transmit buffer
210 * @param tx_length The number of bytes to transmit
211 * @param rx The receive buffer
212 * @param rx_length The number of bytes to receive
213 * @param address The address to be set - 7bit or 9bit
214 * @param stop If true, stop will be generated after the transfer is done
215 * @param handler The I2C IRQ handler to be set
216 * @param event Event mask for the transfer. See \ref hal_I2CEvents
217 * @param hint DMA hint usage
218 */
219void 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);
220
221/** The asynchronous IRQ handler
222 *
223 * @param obj The I2C object which holds the transfer information
224 * @return Event flags if a transfer termination condition was met, otherwise return 0.
225 */
226uint32_t i2c_irq_handler_asynch(i2c_t *obj);
227
228/** Attempts to determine if the I2C peripheral is already in use
229 *
230 * @param obj The I2C object
231 * @return Non-zero if the I2C module is active or zero if it is not
232 */
233uint8_t i2c_active(i2c_t *obj);
234
235/** Abort asynchronous transfer
236 *
237 * This function does not perform any check - that should happen in upper layers.
238 * @param obj The I2C object
239 */
240void i2c_abort_asynch(i2c_t *obj);
241
242#endif
243
244/**@}*/
245
246#ifdef __cplusplus
247}
248#endif
249
250#endif
251
252#endif
253
254/** @}*/
Note: See TracBrowser for help on using the repository browser.