This are general defs and base class interfaces. ( DACInterface, I2CInterface) to be used with I2C devices

Dependents:   MCP23009tst AT30TSE752TST MCP4728setaddrProg mbedSerialInterface_talkback2 ... more

The DevInterfaces class is used for a more general support of a number of I2C devices.

For the moment this is MCP4728 Quad DAC.

The idea is to write a libs for the I2C devices (and perhaps later SPI , ... devices) not restricted to a certain hardware platform.

So there is the I2CInterface. This is a ( not pure) virtual class. A pointer to this class is used for the I2C device lib (like the MCP4728 class https://developer.mbed.org/users/wbeaumont/code/MCP4728/ ) to communicate with the I2C interface. The I2C interface is kept simple as possible. So it has to be applied for "tested environments". I2C Bus timeouts etc. are hard to debug with such a simple interface. This kind of interface is also not suitable for optimal performance. The user has to take care of issues of blocking devices, parallel processes etc. This can be dealt with in the implementation of the I2CInterface class

The aim of this project is to generate code for these devices on different platforms without doing the painstaking work of looking up all the register details.

There is an implementation of the I2CInterface for the MBED , tested with the FRDM-KL05Z platform. https://developer.mbed.org/users/wbeaumont/code/I2Cinterfaces/

A very simple application on a MBED can be found in https://developer.mbed.org/users/wbeaumont/code/MCP4728test/

Committer:
wbeaumont
Date:
Tue Sep 10 11:20:07 2019 +0000
Revision:
7:b091a268b726
Parent:
5:b5c9eb2330dc
to make it compatible for other platforms see; https://github.com/wimbeaumont/peripheral_dev_tst; ( mbed os5  based)  but this still works for mbed os2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wbeaumont 0:da1fb7dd363f 1 #ifndef __I2CINTERFACE__
wbeaumont 0:da1fb7dd363f 2 #define __I2CINTERFACE__
wbeaumont 0:da1fb7dd363f 3
wbeaumont 0:da1fb7dd363f 4 #include "getVersion.h"
wbeaumont 7:b091a268b726 5 #include "DevErrorReporter.h"
wbeaumont 7:b091a268b726 6 #define I2CINTERFACE_HDR_VER "2.10"
wbeaumont 0:da1fb7dd363f 7
wbeaumont 7:b091a268b726 8
wbeaumont 5:b5c9eb2330dc 9 /*
wbeaumont 7:b091a268b726 10 * This is a the I2CInterface "virtual" class used in all I2C devices
wbeaumont 7:b091a268b726 11 * This file make part of the PeriperalDevice package see repository
wbeaumont 7:b091a268b726 12 * https://github.com/wimbeaumont/PeripheralDevices
wbeaumont 7:b091a268b726 13 * For more info see the README.md in the top of repository
wbeaumont 7:b091a268b726 14 *
wbeaumont 5:b5c9eb2330dc 15 * ver 0:40 added wait_for_ms
wbeaumont 7:b091a268b726 16 * ver 1:00 changed the interface , all functions will have a retrun value was not correct
wbeaumont 7:b091a268b726 17 * ver 1.10 added lock and unlock, abort_transfer added more coments
wbeaumont 7:b091a268b726 18 * ver 2.00 added error status info
wbeaumont 7:b091a268b726 19 * ver 2.10 added read_reg as this is often used.
wbeaumont 7:b091a268b726 20 * (C) Wim Beaumont Universiteit Antwerpen 2016,2017,2018,2019
wbeaumont 7:b091a268b726 21 *
wbeaumont 7:b091a268b726 22 * License see
wbeaumont 7:b091a268b726 23 * https://github.com/wimbeaumont/PeripheralDevices/blob/master/LICENSE
wbeaumont 5:b5c9eb2330dc 24 */
wbeaumont 0:da1fb7dd363f 25
wbeaumont 7:b091a268b726 26 class I2CInterface : public virtual getVersion, public DevErrorReporter{
wbeaumont 0:da1fb7dd363f 27 private:
wbeaumont 0:da1fb7dd363f 28 protected :
wbeaumont 0:da1fb7dd363f 29 void* callback();
wbeaumont 7:b091a268b726 30 int lockstatus;
wbeaumont 0:da1fb7dd363f 31
wbeaumont 0:da1fb7dd363f 32 public :
wbeaumont 7:b091a268b726 33
wbeaumont 7:b091a268b726 34 I2CInterface():getVersion( I2CINTERFACE_HDR_VER ,I2CINTERFACE_HDR_VER , __TIME__, __DATE__){
wbeaumont 7:b091a268b726 35 lockstatus=0;
wbeaumont 7:b091a268b726 36
wbeaumont 7:b091a268b726 37 }; //Create an I2C Master interface
wbeaumont 7:b091a268b726 38 virtual int frequency (int hz){ return 0;};// Set the frequency of the I2C interface. returns 0 when ok,
wbeaumont 7:b091a268b726 39 virtual int read (int address, char *data, int length, bool repeated=false){return 0;};//Read from an I2C slave.
wbeaumont 7:b091a268b726 40 // if repeated is true no stop is generated
wbeaumont 7:b091a268b726 41
wbeaumont 7:b091a268b726 42 // gererate a write (see write function_ with repeat is flase , writing the value reg to the device
wbeaumont 7:b091a268b726 43 // the register size is in byte and has the be equal or smaller then the size_off (int)
wbeaumont 7:b091a268b726 44 // it is assumed that the most significant byte off the regeister address is sent first
wbeaumont 7:b091a268b726 45 virtual int read_reg( int address, char *data, int length, int reg, int regsize=1) {
wbeaumont 7:b091a268b726 46 return 0;}
wbeaumont 7:b091a268b726 47
wbeaumont 7:b091a268b726 48 virtual int read (int &data, int ack){data=0; return 0;};
wbeaumont 7:b091a268b726 49 // Read a single byte from the I2C bus. ack indicates if the ack has to be generated
wbeaumont 7:b091a268b726 50
wbeaumont 7:b091a268b726 51
wbeaumont 7:b091a268b726 52
wbeaumont 7:b091a268b726 53 virtual int write (int address, const char *data, int length, bool repeated=false){return 0;};// Write to an I2C slave.
wbeaumont 7:b091a268b726 54 // if repeated is true no stop is generated
wbeaumont 0:da1fb7dd363f 55 virtual int write (int data){return 0;};// Write single byte out on the I2C bus.
wbeaumont 7:b091a268b726 56 virtual int start (void){return 0;};// Creates a start condition on the I2C bus.
wbeaumont 7:b091a268b726 57 virtual int stop (void){return 0;};// Creates a stop condition on the I2C bus.
wbeaumont 0:da1fb7dd363f 58 virtual int transfer (int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, void* callbackptr, bool repeated=false){
wbeaumont 0:da1fb7dd363f 59 return 0;
wbeaumont 0:da1fb7dd363f 60 }; // Start non-blocking I2C transfer. not yet clear how to deal with the callback
wbeaumont 0:da1fb7dd363f 61 // proposol here is for the implementation a spefic call back function ,that includes the event type
wbeaumont 5:b5c9eb2330dc 62 // wait function that is sometimes needed , not I2C hardware related but different implementation for platforms
wbeaumont 5:b5c9eb2330dc 63 virtual void wait_for_ms(int x) { } ;
wbeaumont 7:b091a268b726 64 virtual int abort_transfer(void) {return 0;}
wbeaumont 7:b091a268b726 65 virtual int lock(void) {if ( lockstatus) return -1; lockstatus=1; return 0; }
wbeaumont 7:b091a268b726 66 virtual int unlock(void) { lockstatus=0; return 0; }
wbeaumont 7:b091a268b726 67
wbeaumont 7:b091a268b726 68
wbeaumont 7:b091a268b726 69
wbeaumont 0:da1fb7dd363f 70 };
wbeaumont 0:da1fb7dd363f 71
wbeaumont 0:da1fb7dd363f 72 #endif