123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #ifndef MBED_I2C_H
- #define MBED_I2C_H
- #include "platform/platform.h"
- #if defined (DEVICE_I2C) || defined(DOXYGEN_ONLY)
- #include "hal/i2c_api.h"
- #include "platform/SingletonPtr.h"
- #include "platform/PlatformMutex.h"
- #include "platform/NonCopyable.h"
- #if DEVICE_I2C_ASYNCH
- #include "platform/CThunk.h"
- #include "hal/dma_api.h"
- #include "platform/FunctionPointer.h"
- #endif
- namespace mbed {
- class I2C : private NonCopyable<I2C> {
- public:
- enum RxStatus {
- NoData,
- MasterGeneralCall,
- MasterWrite,
- MasterRead
- };
- enum Acknowledge {
- NoACK = 0,
- ACK = 1
- };
-
- I2C(PinName sda, PinName scl);
-
- void frequency(int hz);
-
- int read(int address, char *data, int length, bool repeated = false);
-
- int read(int ack);
-
- int write(int address, const char *data, int length, bool repeated = false);
-
- int write(int data);
-
- void start(void);
-
- void stop(void);
-
- virtual void lock(void);
-
- virtual void unlock(void);
- virtual ~I2C()
- {
-
- }
- #if DEVICE_I2C_ASYNCH
-
- int transfer(int address, const 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);
-
- void abort_transfer();
- protected:
-
- void lock_deep_sleep();
-
- void unlock_deep_sleep();
- void irq_handler_asynch(void);
- event_callback_t _callback;
- CThunk<I2C> _irq;
- DMAUsage _usage;
- bool _deep_sleep_locked;
- #endif
- protected:
- void aquire();
- i2c_t _i2c;
- static I2C *_owner;
- int _hz;
- static SingletonPtr<PlatformMutex> _mutex;
- };
- }
- #endif
- #endif
|