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/

Files at this revision

API Documentation at this revision

Comitter:
wbeaumont
Date:
Tue Sep 10 11:20:07 2019 +0000
Parent:
6:93106e899445
Commit message:
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

Changed in this revision

DevErrorReporter.h Show annotated file Show diff for this revision Revisions of this file
I2CInterface.h Show annotated file Show diff for this revision Revisions of this file
dev_interface_def.h Show annotated file Show diff for this revision Revisions of this file
getVersion.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 93106e899445 -r b091a268b726 DevErrorReporter.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DevErrorReporter.h	Tue Sep 10 11:20:07 2019 +0000
@@ -0,0 +1,50 @@
+#ifndef __DEVERRORREPORTER__
+#define __DEVERRORREPORTER__
+
+//#include "getVersion.h"
+
+#define DEVERRORREPORTER_HDR_VER "2.00"
+
+
+/*
+ *  This is a the Error reporter it stores the last errors etc.  
+ *  No getVersion support for the moment. 
+ *  This file make part of the PeriperalDevice package see repository  
+ *  https://github.com/wimbeaumont/PeripheralDevices
+ *  For more info see   the README.md in the top of repository 
+ *
+ *  ver  0:10  initial 
+ *  ver  0.20  needed static before const
+ *  ver  0.30  device error int 
+ * (C) Wim Beaumont Universiteit Antwerpen 2019
+ *
+ * License see
+ * https://github.com/wimbeaumont/PeripheralDevices/blob/master/LICENSE
+*/ 
+
+
+class DevErrorReporter {
+
+protected :
+     static const int notsupportederrno = -1999;    
+     bool   ack; // last ack status 
+     bool   notsupported;
+     int    comerr; // reported Deverr 
+     bool   devinit; //  if the device is initialized 
+     bool setnotsupported(void) { comerr=notsupportederrno; notsupported=true;return comerr;}
+     int  deverr;
+     DevErrorReporter(void){
+      ack=false;comerr=0; devinit=false; notsupported=false;    
+     }  
+public:
+
+// status info 
+virtual bool getLastAckStatus(void) { return ack; }
+virtual bool getDeviceInitStatus(void) { return devinit; }
+virtual int getLastComError(void) {return comerr;}
+virtual bool getNotSupported(void) {return notsupported;}
+virtual int getDeviceError(void) { return deverr; }
+
+
+};
+#endif 
diff -r 93106e899445 -r b091a268b726 I2CInterface.h
--- a/I2CInterface.h	Mon Feb 19 15:25:23 2018 +0000
+++ b/I2CInterface.h	Tue Sep 10 11:20:07 2019 +0000
@@ -2,35 +2,71 @@
 #define __I2CINTERFACE__
 
 #include "getVersion.h"
+#include "DevErrorReporter.h"
+#define I2CINTERFACE_HDR_VER "2.10"
 
-#define I2CINTERFACE_HDR_VER "0.40"
+
 /*
+ *  This is a the I2CInterface "virtual"  class used in all I2C devices 
+ *  This file make part of the PeriperalDevice package see repository  
+ *  https://github.com/wimbeaumont/PeripheralDevices
+ *  For more info see   the README.md in the top of repository 
+ *
  *  ver  0:40  added wait_for_ms 
+ *  ver  1:00  changed the interface , all functions will have a retrun value was not correct 
+ *  ver  1.10  added lock and unlock, abort_transfer added more coments
+ *  ver  2.00  added error status info 
+ *  ver  2.10  added read_reg as this is often used. 
+ * (C) Wim Beaumont Universiteit Antwerpen 2016,2017,2018,2019
+ *
+ * License see
+ * https://github.com/wimbeaumont/PeripheralDevices/blob/master/LICENSE
 */ 
 
-class I2CInterface : public virtual getVersion{
+class I2CInterface : public virtual getVersion, public DevErrorReporter{
 private: 
 protected :
      void*  callback();
+     int    lockstatus; 
 
 public : 
-    I2CInterface():getVersion( I2CINTERFACE_HDR_VER ,I2CINTERFACE_HDR_VER , __TIME__, __DATE__){};   //Create an I2C Master interface
-virtual void    frequency (int hz){};//  Set the frequency of the I2C interface.
-virtual int     read (int address, char *data, int length, bool repeated=false){
-            return 0;};//Read from an I2C slave.
-virtual int     read (int ack){return 0;};// Read a single byte from the I2C bus.
-virtual int     write (int address, const char *data, int length, bool repeated=false){
-            return 0;
-        };// Write to an I2C slave.
+
+        I2CInterface():getVersion( I2CINTERFACE_HDR_VER ,I2CINTERFACE_HDR_VER , __TIME__, __DATE__){
+      lockstatus=0;
+    
+    };   //Create an I2C Master interface
+virtual int     frequency (int hz){ return 0;};//  Set the frequency of the I2C interface. returns 0 when ok,
+virtual int     read (int address, char *data, int length, bool repeated=false){return 0;};//Read from an I2C slave.
+                                               // if repeated is true no stop is generated
+
+// gererate a write (see write function_  with repeat is flase , writing the value reg to the device 
+//  the register size is in byte and has the be  equal or smaller then the size_off (int) 
+// it  is assumed that the most significant byte off the regeister address is sent first 
+virtual int   read_reg( int address, char *data, int length, int reg, int regsize=1) {
+        return 0;}
+
+virtual int     read (int &data, int ack){data=0; return 0;};
+                    // Read a single byte from the I2C bus. ack indicates if the ack has to be generated
+
+
+
+virtual int     write (int address, const char *data, int length, bool repeated=false){return 0;};// Write to an I2C slave.
+                                               // if repeated is true no stop is generated
 virtual int     write (int data){return 0;};//  Write single byte out on the I2C bus.
-virtual void    start (void){};// Creates a start condition on the I2C bus.
-virtual void    stop (void){};// Creates a stop condition on the I2C bus.
+virtual int     start (void){return 0;};// Creates a start condition on the I2C bus.
+virtual int     stop  (void){return 0;};// Creates a stop condition on the I2C bus.
 virtual int     transfer (int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, void*  callbackptr, bool repeated=false){
             return 0;
         };     //   Start non-blocking I2C transfer.       not yet clear how to deal with the callback
              // proposol here is for the implementation a spefic call back function ,that includes the event type  
             // wait function that is sometimes needed , not I2C hardware related but different implementation for platforms
 virtual void wait_for_ms(int x) { } ;
+virtual int  abort_transfer(void) {return 0;} 
+virtual int  lock(void) {if ( lockstatus) return -1;  lockstatus=1; return 0; } 
+virtual int  unlock(void) { lockstatus=0; return 0; }  
+
+
+
 }; 
 
 #endif
diff -r 93106e899445 -r b091a268b726 dev_interface_def.h
--- a/dev_interface_def.h	Mon Feb 19 15:25:23 2018 +0000
+++ b/dev_interface_def.h	Tue Sep 10 11:20:07 2019 +0000
@@ -1,6 +1,6 @@
 #ifndef device_interface_def_H
 #define device_interface_def_H
-
+#include <stdint.h>
 
 #define DEV_INTERFACE_DEF_VER "0.4"
 
@@ -8,13 +8,12 @@
     v 0.1  
     v 0.2   20170111 added int16_t  and uint16_t   added DEV_INTERFACE_DEF_VER
     v 0.4   20170111 added int8_t
+    v 0.5   20190909 added stdint.h , define the uxx  accordingly 
+                        removed uintx_t and uint8_t as these are definined in stdint.hardresume
 */
 
-typedef unsigned int  u32;
-typedef unsigned short int  u16;
-typedef unsigned short int  uint16_t;
-typedef signed char int8_t ;
-typedef short int  int16_t;
-typedef unsigned char u8;
-typedef unsigned char uint8_t;
+typedef uint32_t u32;
+typedef uint16_t u16;
+typedef uint8_t  u8;
+
 #endif
\ No newline at end of file
diff -r 93106e899445 -r b091a268b726 getVersion.cpp
--- a/getVersion.cpp	Mon Feb 19 15:25:23 2018 +0000
+++ b/getVersion.cpp	Tue Sep 10 11:20:07 2019 +0000
@@ -1,6 +1,16 @@
 #include "getVersion.h"
 
-#define GETVERSION_SRC_VER  "0.20"
+#define GETVERSION_SRC_VER  "0.22"
+
+
+/*
+ *  info see  getversion.h 
+ * (C) Wim Beaumont Universiteit Antwerpen 2017
+ *   ver 0.20
+ *   ver 0.21 removed redefining NULL
+ *   ver 0.22 removed some double spaces in output
+*/
+
 #ifdef MBED 
 #include "mbed.h"
 #else 
@@ -10,22 +20,21 @@
 
 #include "dev_interface_def.h"
 
-#define NULL 0
 
 
 getVersion::getVersion(const char* ver_h,const char* ver_s, const char* time,const char* date) {
         sver=ver_s; hver=ver_h; ctime=time;cdate=date;
-        sprintf(infostr,"HDR ver:  %s, SRC ver: %s",sver,hver);
+        sprintf(infostr,"HDR ver: %s, SRC ver: %s",sver,hver);
         
         } ;
 
 getVersion::getVersion(){sver=0; hver=0; ctime=0;cdate=0;
-            sprintf(infostr,"HDR ver:  %s, SRC ver: %s",GETVERSION_HDR_VER,GETVERSION_SRC_VER);
+            sprintf(infostr,"HDR ver: %s, SRC ver: %s",GETVERSION_HDR_VER,GETVERSION_SRC_VER);
 };     
 
 
 void getVersion::dev_interface_def_version( char* resultstring){
-    sprintf( resultstring,"dev_interface_def version : %s", DEV_INTERFACE_DEF_VER);
+    sprintf( resultstring,"dev_interface_def version: %s", DEV_INTERFACE_DEF_VER);
 }
 
 void getVersion::get_dec_version( unsigned short  hexversion , unsigned char & version, unsigned char& subversion) {