1 /** mbed Microcontroller Library
2   ******************************************************************************
3   * @file    i2c_api.h
4   * @author
5   * @version V1.0.0
6   * @brief   This file provides following mbed I2C API
7   ******************************************************************************
8   * @attention
9   *
10   * Copyright (c) 2006-2013 ARM Limited
11   *
12   * Licensed under the Apache License, Version 2.0 (the "License");
13   * you may not use this file except in compliance with the License.
14   * You may obtain a copy of the License at
15   *
16   *     http://www.apache.org/licenses/LICENSE-2.0
17   *
18   * Unless required by applicable law or agreed to in writing, software
19   * distributed under the License is distributed on an "AS IS" BASIS,
20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   * See the License for the specific language governing permissions and
22   * limitations under the License.
23   ******************************************************************************
24   */
25 #ifndef MBED_I2C_API_H
26 #define MBED_I2C_API_H
27 
28 #include "device.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /** @addtogroup i2c       I2C
35  *  @ingroup    hal
36  *  @brief      I2C functions
37  *  @{
38  */
39 
40 ///@name Ameba Common
41 ///@{
42 typedef struct i2c_s i2c_t;
43 
44 enum {
45   I2C_ERROR_NO_SLAVE = -1,
46   I2C_ERROR_BUS_BUSY = -2
47 };
48 
49 /**
50   * @brief  Initializes the I2C device, include clock/function/I2C registers.
51   * @param  obj: i2c object define in application software.
52   * @param  sda: SDA PinName according to pinmux spec.
53   * @param  scl: SCL PinName according to pinmux spec.
54   * @retval none
55   */
56 void i2c_init(i2c_t *obj, PinName sda, PinName scl);
57 
58 /**
59   * @brief  Set i2c frequency.
60   * @param  obj: i2c object define in application software.
61   * @param  hz: i2c clock(unit is Hz).
62   * @retval none
63   */
64 void i2c_frequency(i2c_t *obj, int hz);
65 
66 /**
67   * @brief  Start i2c device.
68   * @param  obj: i2c object define in application software.
69   * @retval 0
70   */
71 int i2c_start(i2c_t *obj);
72 
73 /**
74   * @brief  Stop i2c device.
75   * @param  obj: i2c object define in application software.
76   * @retval 0
77   */
78 int i2c_stop(i2c_t *obj);
79 
80 /**
81   * @brief  Deinitializes the I2C device
82   * @param  obj: i2c object define in application software.
83   * @retval none
84   */
85 void i2c_reset(i2c_t *obj);
86 
87 /**
88   * @brief  I2C master receive single byte.
89   * @param  obj: i2c object define in application software.
90   * @param  last: hold the received data.
91   * @retval : the received data.
92   */
93 int i2c_byte_read(i2c_t *obj, int last);
94 
95 /**
96   * @brief  I2C master send single byte.
97   * @param  obj: i2c object define in application software.
98   * @param  data: the data to be sent.
99   * @retval : result.
100   */
101 int i2c_byte_write(i2c_t *obj, int data);
102 
103 /**
104   * @brief  Set i2c device to be slave.
105   * @param  obj: i2c object define in application software.
106   * @param  enable_slave: enable slave function, this parameter can be one of the following values:
107   *		@arg 0 disable
108   *		@arg 1 enable
109   * @retval none
110   */
111 void i2c_slave_mode(i2c_t *obj, int enable_slave);
112 
113 /**
114   * @brief  Get i2c slave state.
115   * @param  obj: i2c object define in application software.
116   * @retval : the state of i2c slave.
117   */
118 int i2c_slave_receive(i2c_t *obj);
119 
120 /**
121   * @brief  Set i2c slave address.
122   * @param  obj: i2c object define in application software.
123   * @param  idx: i2c index, this parameter can be one of the following values:
124   *		@arg 0 I2C0 Device
125   *		@arg 1 I2C1 Device
126   * @param  address: slave address.
127   * @param  mask: the mask of address
128   * @retval none
129   */
130 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask);
131 /**
132   * @brief  I2C master read in poll mode.
133   * @param  obj: i2c object define in application software.
134   * @param  address: slave address which will be transmitted.
135   * @param  data: point to the buffer to hold the received data.
136   * @param  length: the length of data that to be received.
137   * @param  stop: specifies whether a STOP is issued after all the bytes are received.
138   * @retval : the length of data received.
139   */
140 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop);
141 
142 /**
143   * @brief  I2C master write in poll mode.
144   * @param  obj: i2c object define in application software.
145   * @param  address: slave address which will be transmitted.
146   * @param  data: point to the data to be sent.
147   * @param  length: the length of data that to be sent.
148   * @param  stop: specifies whether a STOP is issued after all the bytes are sent.
149   * @retval : the length of data send.
150   */
151 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop);
152 
153 /**
154   * @brief  I2C slave read in poll mode.
155   * @param  obj: i2c object define in application software.
156   * @param  data: point to the buffer to hold the received data.
157   * @param  length: the length of data that to be received.
158   * @retval : the length of data received.
159   */
160 int i2c_slave_read(i2c_t *obj, char *data, int length);
161 
162 /**
163   * @brief  I2C slave write in poll mode.
164   * @param  obj: i2c object define in application software.
165   * @param  data: point to the data to be sent.
166   * @param  length: the length of data that to be sent.
167   * @retval 0: FAIL
168   * @retval 1: SUCCESS
169   */
170 int i2c_slave_write(i2c_t *obj, const char *data, int length);
171 
172 /**
173   * @brief  Set/clear i2c slave RD_REQ interrupt mask.
174   * @param  obj: i2c object define in application software.
175   * @param  set: set or clear for read request.
176   * @retval 1: SUCCESS
177   */
178 int i2c_slave_set_for_rd_req(i2c_t *obj, int set);
179 
180 /**
181   * @brief  Set/clear i2c slave NAK or ACK data part in transfer.
182   * @param  obj: i2c object define in application software.
183   * @param  set_nak: set or clear for data NAK.
184   * @retval 1: SUCCESS
185   */
186 int i2c_slave_set_for_data_nak(i2c_t *obj, int set_nak);
187 ///@}
188 
189 #if ((defined(CONFIG_PLATFORM_8711B) && (CONFIG_PLATFORM_8711B == 1)) || (defined (CONFIG_PLATFORM_8721D) && (CONFIG_PLATFORM_8721D == 1)))
190 ///@name AmebaZ and AmebaD
191 ///@{
192 /**
193   * @brief  I2C master send data and read data in poll mode.
194   * @param  obj: i2c object define in application software.
195   * @param  address: slave address which will be transmitted.
196   * @param  pWriteBuf: point to the data to be sent.
197   * @param  Writelen: the length of data that to be sent.
198   * @param  pReadBuf: point to the buffer to hold the received data.
199   * @param  Readlen: the length of data that to be received.
200   * @retval the length of data received.
201   */
202 int i2c_repeatread(i2c_t *obj, int address, u8 *pWriteBuf, int Writelen, u8 *pReadBuf, int Readlen) ;
203 ///@}
204 #endif //(CONFIG_PLATFORM_8711B||CONFIG_PLATFORM_8721D)
205 
206 /*\@}*/
207 
208 #ifdef __cplusplus
209 }
210 #endif
211 
212 #endif/* MBED_I2C_API_H */
213