mbed-drivers
I2C.h
1 /*
2  * Copyright (c) 2006-2016, ARM Limited, All Rights Reserved
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may
6  * not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef MBED_I2C_H
18 #define MBED_I2C_H
19 
20 #include "platform.h"
21 
22 #if DEVICE_I2C
23 
24 #include "i2c_api.h"
25 
26 #if DEVICE_I2C_ASYNCH
27 #include "CThunk.h"
28 #include "dma_api.h"
29 #include "core-util/FunctionPointer.h"
30 #include "Transaction.h"
31 #endif
32 
33 namespace mbed {
34 
52 class I2C {
53 
54 public:
55  enum RxStatus {
56  NoData,
57  MasterGeneralCall,
58  MasterWrite,
59  MasterRead
60  };
61 
62  enum Acknowledge {
63  NoACK = 0,
64  ACK = 1
65  };
66 
72  I2C(PinName sda, PinName scl);
73 
78  void frequency(int hz);
79 
94  int read(int address, char *data, int length, bool repeated = false);
95 
103  int read(int ack);
104 
119  int write(int address, const char *data, int length, bool repeated = false);
120 
129  int write(int data);
130 
134  void start(void);
135 
138  void stop(void);
139 
140 #if DEVICE_I2C_ASYNCH
141 
146  typedef mbed::util::FunctionPointer3<void, Buffer, Buffer, int> event_callback_t;
147 
160  int transfer(int address, char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t& callback, int event = I2C_EVENT_TRANSFER_COMPLETE, bool repeated = false);
161 
172  int transfer(int address, const Buffer& tx_buffer, const Buffer& rx_buffer, const event_callback_t& callback, int event = I2C_EVENT_TRANSFER_COMPLETE, bool repeated = false);
173 
176  void abort_transfer();
177 protected:
180 
181  void irq_handler_asynch(void);
182  transaction_data_t _current_transaction;
183  CThunk<I2C> _irq;
184  DMAUsage _usage;
185 #endif
186 
187 protected:
188  void aquire();
189 
190  i2c_t _i2c;
191  static I2C *_owner;
192  int _hz;
193 };
194 
195 } // namespace mbed
196 
197 #endif
198 
199 #endif
Definition: Transaction.h:48
int transfer(int address, char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t &callback, int event=I2C_EVENT_TRANSFER_COMPLETE, bool repeated=false)
Definition: I2C.cpp:97
void start(void)
Definition: I2C.cpp:87
int read(int address, char *data, int length, bool repeated=false)
Definition: I2C.cpp:70
Definition: I2C.h:52
I2C(PinName sda, PinName scl)
Definition: I2C.cpp:26
void abort_transfer()
Definition: I2C.cpp:116
mbed::util::FunctionPointer3< void, Buffer, Buffer, int > event_callback_t
Definition: I2C.h:146
void stop(void)
Definition: I2C.cpp:91
void frequency(int hz)
Definition: I2C.cpp:38
Definition: CThunk.h:70
Definition: Buffer.h:30
Definition: BusIn.cpp:19
int write(int address, const char *data, int length, bool repeated=false)
Definition: I2C.cpp:56