(MBED) implementation of the I2C interface

Dependents:   MCP23009tst AT30TSE752TST MCP4728setaddrProg mbedSerialInterface_talkback2 ... more

Committer:
wbeaumont
Date:
Tue Sep 10 11:16:24 2019 +0000
Revision:
3:06e9e208a2b0
Parent:
2:b7ebe53e8fac
changes to to use methods for  MBED OS5 ; Also changes to make it compatible to run it on other platforms ; see ; https://github.com/wimbeaumont/peripheral_dev_tst

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wbeaumont 0:c65947f64a8b 1 #ifndef __MBEDI2CINTERFACE_H
wbeaumont 0:c65947f64a8b 2 #define __MBEDI2CINTERFACE_H
wbeaumont 0:c65947f64a8b 3
wbeaumont 3:06e9e208a2b0 4 #include <mbed_version.h>
wbeaumont 2:b7ebe53e8fac 5
wbeaumont 3:06e9e208a2b0 6 /*
wbeaumont 3:06e9e208a2b0 7 * This is a MBED implementation of the I2CInterface class
wbeaumont 3:06e9e208a2b0 8 * This file make part of the PeriperalDevice package see repository
wbeaumont 3:06e9e208a2b0 9 * https://github.com/wimbeaumont/PeripheralDevices
wbeaumont 3:06e9e208a2b0 10 * For more info see the README.md in the top of repository
wbeaumont 3:06e9e208a2b0 11 * version 1.10 :added wait_for_ms
wbeaumont 3:06e9e208a2b0 12 * version 1.21 : followed changes of I2CInterface for start stop frequency
wbeaumont 3:06e9e208a2b0 13 * version 1.30 : upated to mbed os5 interface should still work for mbed os2
wbeaumont 3:06e9e208a2b0 14 * version 1.40 : implemented read_reg methode
wbeaumont 3:06e9e208a2b0 15 * version 1.41 : correction on implemented read_reg methode
wbeaumont 3:06e9e208a2b0 16 * version 1.50 : added comerr for I2C access
wbeaumont 3:06e9e208a2b0 17 * version 1.53 : correction on implemented read_reg methode
wbeaumont 3:06e9e208a2b0 18 * (C) Wim Beaumont Universiteit Antwerpen 2019
wbeaumont 3:06e9e208a2b0 19 *
wbeaumont 3:06e9e208a2b0 20 * License see
wbeaumont 3:06e9e208a2b0 21 * https://github.com/wimbeaumont/PeripheralDevices/blob/master/LICENSE
wbeaumont 2:b7ebe53e8fac 22 */
wbeaumont 3:06e9e208a2b0 23
wbeaumont 3:06e9e208a2b0 24 #include "I2CInterface.h"
wbeaumont 3:06e9e208a2b0 25
wbeaumont 3:06e9e208a2b0 26 #define VERSION_MBEDI2CInterface_HDR "1.53"
wbeaumont 0:c65947f64a8b 27
wbeaumont 0:c65947f64a8b 28
wbeaumont 0:c65947f64a8b 29 class MBEDI2CInterface :public I2CInterface {
wbeaumont 0:c65947f64a8b 30
wbeaumont 0:c65947f64a8b 31 I2C i2cdev;
wbeaumont 0:c65947f64a8b 32 public :
wbeaumont 0:c65947f64a8b 33
wbeaumont 1:80ebfbf95667 34 MBEDI2CInterface(PinName sda, PinName scl):
wbeaumont 1:80ebfbf95667 35 getVersion( VERSION_MBEDI2CInterface_HDR,VERSION_MBEDI2CInterface_HDR, __TIME__, __DATE__),i2cdev(sda,scl){
wbeaumont 0:c65947f64a8b 36 // no init code yet
wbeaumont 0:c65947f64a8b 37 } ;
wbeaumont 0:c65947f64a8b 38 // next could perhaps more efficient but not yet investigated
wbeaumont 3:06e9e208a2b0 39 virtual int frequency (int hz){ i2cdev.frequency(hz) ; return 0;};
wbeaumont 0:c65947f64a8b 40 virtual int read (int address, char *data, int length, bool repeated=false){
wbeaumont 3:06e9e208a2b0 41 return comerr=i2cdev.read ( address, data, length, repeated);
wbeaumont 3:06e9e208a2b0 42 };
wbeaumont 3:06e9e208a2b0 43 virtual int read (int& data, int ack){ data= i2cdev.read ( ack); return 0;};// Read a single byte from the I2C bus.
wbeaumont 3:06e9e208a2b0 44
wbeaumont 3:06e9e208a2b0 45
wbeaumont 3:06e9e208a2b0 46 // gererate a write (see write function_ with repeat is flase , writing the value reg to the device
wbeaumont 3:06e9e208a2b0 47 // the register size is in byte and has the be equal or smaller then the len (int)
wbeaumont 3:06e9e208a2b0 48 // it is assumed that the most significant byte off the regeister address is sent first
wbeaumont 3:06e9e208a2b0 49 virtual int read_reg( int address, char *data, int length, int reg, int regsize=1) {
wbeaumont 3:06e9e208a2b0 50 // first check the register size
wbeaumont 3:06e9e208a2b0 51 char regbytes[sizeof(int)];
wbeaumont 3:06e9e208a2b0 52 if ( (unsigned int) regsize > sizeof(int) ) return -11;
wbeaumont 3:06e9e208a2b0 53 for ( int lc = regsize-1 ; lc >= 0 ; lc--) {
wbeaumont 3:06e9e208a2b0 54 regbytes[lc] = (char )( reg & 0xFF);
wbeaumont 3:06e9e208a2b0 55 reg = reg >>8;
wbeaumont 3:06e9e208a2b0 56 }
wbeaumont 3:06e9e208a2b0 57 comerr= i2cdev.write ( address, regbytes, regsize,true ); // no stop !!
wbeaumont 3:06e9e208a2b0 58 if ( comerr == 0) {
wbeaumont 3:06e9e208a2b0 59 comerr = i2cdev.read ( address, data, length, false); // now stop I2C
wbeaumont 3:06e9e208a2b0 60 } else {
wbeaumont 3:06e9e208a2b0 61 comerr= 10*comerr;//so -10 to get the difference with read error
wbeaumont 0:c65947f64a8b 62 }
wbeaumont 3:06e9e208a2b0 63 return comerr;
wbeaumont 3:06e9e208a2b0 64 }
wbeaumont 3:06e9e208a2b0 65
wbeaumont 3:06e9e208a2b0 66
wbeaumont 3:06e9e208a2b0 67 virtual int write (int address, const char *data, int length, bool repeated=false){
wbeaumont 3:06e9e208a2b0 68 return comerr=i2cdev.write ( address, data, length, repeated);
wbeaumont 3:06e9e208a2b0 69 }
wbeaumont 3:06e9e208a2b0 70 virtual int write (int data){return i2cdev.write (data);};// Write single byte out on the I2C bus.
wbeaumont 3:06e9e208a2b0 71 virtual int start (void){i2cdev.start (); return 0; };// Creates a start condition on the I2C bus.
wbeaumont 3:06e9e208a2b0 72 virtual int stop (void){i2cdev.stop(); return 0; };// Creates a stop condition on the I2C bus.
wbeaumont 3:06e9e208a2b0 73
wbeaumont 3:06e9e208a2b0 74 #if DEVICE_I2C_ASYNCH
wbeaumont 3:06e9e208a2b0 75
wbeaumont 3:06e9e208a2b0 76 virtual int transfer (int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, void* callbackfunction, bool repeated=false){
wbeaumont 3:06e9e208a2b0 77 return comerr=i2cdev.transfer (address, tx_buffer, tx_length, rx_buffer, rx_length, callback, event, repeated);
wbeaumont 0:c65947f64a8b 78 };
wbeaumont 3:06e9e208a2b0 79 #else
wbeaumont 3:06e9e208a2b0 80
wbeaumont 3:06e9e208a2b0 81 virtual int transfer (int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, void* callbackfunction, bool repeated=false){
wbeaumont 3:06e9e208a2b0 82 return setnotsupported();
wbeaumont 3:06e9e208a2b0 83 }
wbeaumont 3:06e9e208a2b0 84
wbeaumont 3:06e9e208a2b0 85 #endif
wbeaumont 3:06e9e208a2b0 86 // new since mbed os 5
wbeaumont 3:06e9e208a2b0 87 #if (MBED_MAJOR_VERSION > 4 )
wbeaumont 3:06e9e208a2b0 88 #if DEVICE_I2C_ASYNCH
wbeaumont 3:06e9e208a2b0 89 virtual int abort_transfer(void) {i2cdev.abort_transfer();return 0;} // assumes it always works so return 0
wbeaumont 3:06e9e208a2b0 90 #else
wbeaumont 3:06e9e208a2b0 91 virtual int abort_transfer(void) {return setnotsupported();}
wbeaumont 3:06e9e208a2b0 92 #endif
wbeaumont 3:06e9e208a2b0 93
wbeaumont 3:06e9e208a2b0 94 virtual int lock(void) {i2cdev.lock(); return 0; }
wbeaumont 3:06e9e208a2b0 95 virtual int unlock(void) { i2cdev.unlock(); return 0; }
wbeaumont 3:06e9e208a2b0 96 #endif // mbed version
wbeaumont 3:06e9e208a2b0 97 // #else use the functions from the I2C interface
wbeaumont 3:06e9e208a2b0 98
wbeaumont 2:b7ebe53e8fac 99 virtual void wait_for_ms(int x) { wait_ms(x); }
wbeaumont 0:c65947f64a8b 100
wbeaumont 0:c65947f64a8b 101
wbeaumont 3:06e9e208a2b0 102 } ;
wbeaumont 0:c65947f64a8b 103
wbeaumont 0:c65947f64a8b 104
wbeaumont 3:06e9e208a2b0 105 #endif