Library to read and write Azoteq IQS6xx device registers via I2C.
Dependents: IQS620_HelloWorld IQS622_HelloWorld IQS624_HelloWorld IQS621_HelloWorld ... more
Library: IQS62x
Library to read and write Azoteq IQS6xx device registers via I2C.
Supported Devices
Handy Table of ProxFusion Device Features
ALS = Ambient Light Sensor PIR = Passive Infrared
Revision 12:5a9bbbd6e312, committed 2017-05-12
- Comitter:
- AzqDev
- Date:
- Fri May 12 23:14:44 2017 +0000
- Parent:
- 11:4d6d1da5bd31
- Child:
- 13:71f8ee16a3a1
- Commit message:
- Track read & write changes to registers
Changed in this revision
IQS62x.cpp | Show annotated file Show diff for this revision Revisions of this file |
IQS62x.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/IQS62x.cpp Fri May 12 14:55:20 2017 +0000 +++ b/IQS62x.cpp Fri May 12 23:14:44 2017 +0000 @@ -9,23 +9,33 @@ #include "IQS62x.h" // constructor -IQS62xIO::IQS62xIO() : +IQS62xIO::IQS62xIO() : i2c( IQS_I2C_DATA_PIN, IQS_I2C_CLOCK_PIN ), // first run the constructor for mbed class I2C IQSready( IQS_READY_PIN ) // first run the constructor for mbed class DigitalIn -{ +{ registers = I2CBuffer; // pointer to the receive buffer I2CErrorCount = 0; // reset I2C error count i2c.frequency( I2Cspeed ); // I2C clock frequency -} + + // this uses memory but is very handy in diagnostics + memset(writeFlag, 0, I2CBufferSize); // a table to remmber if we wrote to a register + memset(lastWrite, 0, I2CBufferSize); // a table to remember what we wrote to a register + memset(lastRead, 0, I2CBufferSize); // a table to remmber what we read the previous read cycle + memset(readChanges, 0, I2CBufferSize); // a table to mark up any registers that changed since the previous read + memset(writeChanges, 0, I2CBufferSize); // a table to mark up any registers that differs from what was written to it +} // write a single byte to an IQS62x register -void IQS62xIO::writeRegister(int address, int data) { +void IQS62xIO::writeRegister(int address, int data) +{ + writeFlag[ address & 0xff ] = 1; // remember which registers we changed + lastWrite[ address & 0xff ] = data & 0xff; // remember what we wrote char twoBytes [2]; int numberOfBytes = 2; twoBytes[0] = address & 0xff; twoBytes[1] = data & 0xff; - waitForReady(); - if(0!=i2c.write(I2C_ADR,twoBytes,numberOfBytes,false)) + waitForIqsReady(); + if(0!=i2c.write(I2C_ADR,twoBytes,numberOfBytes,false)) I2CErrorCount++; } @@ -33,34 +43,56 @@ #include "deviceType.h" // to check if we should override the configuration definition #ifndef OVERRIDE_CONFIGURE // if there is no override for configure() then we use this simple version -void IQS62xIO::configure() { +void IQS62xIO::configure() +{ writeRegister(0xd0,0x40); // simplest config : just acknowledge/clear the reset flag } #endif // read N registers, starting at provided offset -void IQS62xIO::readIqsRegisters(int start, int count){ +void IQS62xIO::readIqsRegisters(int start, int count) +{ memset(I2CBuffer,0x55,I2CBufferSize); // "clear" i2c receive buffer - waitForReady(); - char i2c_start_address [1]; + waitForIqsReady(); + char i2c_start_address [1]; i2c_start_address[0] = start & 0xff; int numberOfBytes = 1; // write start address to the IQS62x address register - if(0!=i2c.write(I2C_ADR,i2c_start_address,numberOfBytes,false)) + if(0!=i2c.write(I2C_ADR,i2c_start_address,numberOfBytes,false)) I2CErrorCount++; - waitForReady(); + waitForIqsReady(); numberOfBytes = count % I2CBufferSize; // read register values into a buffer if(0!=i2c.read(I2C_ADR,I2CBuffer,numberOfBytes,false)) I2CErrorCount++; + +#define PLEASE_CHECK_FOR_IQS_CHANGES 1 +#ifndef DONT_CHECK_FOR_IQS_CHANGES + // this is optional but handy diagnostics + // we build two tables: + // writeChanges[i] is set to 1 if the register content differs from what we wrote to it + // readChanges[i] is set to 1 if the register value differs from the previously read value + for(int i=0; i<I2CBufferSize; i++) { + if (writeFlag[i] == 1) { // only if we previously wrote to this register we check it + if( I2CBuffer[i] != lastWrite[i] ) + writeChanges[i]=1; // if register different from what we wrote, mark it + } + if ( I2CBuffer[i] != lastRead[i] ) + readChanges[i]=1; // if register differs from previous read, mark it + else + readChanges[i]=0; // no change, no flag + } + memcpy(lastRead, I2CBuffer, I2CBufferSize); // preserve data for next round +#endif } // wait for IQS62x to provide a ready signal (low) on IQS62x_ready pin -void IQS62xIO::waitForReady() { - int timeout=0; +void IQS62xIO::waitForIqsReady() +{ + int timeout=0; while (1) { if(IQSready==1) break; - if (timeout++ > 1000000) goto fatal_error; + if (timeout++ > 1000000) goto fatal_error; } timeout=0; while (1) { @@ -68,6 +100,6 @@ if (timeout++ > 1000000) goto fatal_error; } return; - fatal_error: - error ("Fatal Error: IQS62x ready pin is not toggling"); +fatal_error: + error ("Fatal Error: IQS62x ready pin is not toggling"); } \ No newline at end of file
--- a/IQS62x.h Fri May 12 14:55:20 2017 +0000 +++ b/IQS62x.h Fri May 12 23:14:44 2017 +0000 @@ -31,7 +31,14 @@ DigitalIn IQSready; // ready pin const static int I2Cspeed = I2CSPEED; // I2C clock rate int I2CErrorCount; // number of I2C errors - char I2CBuffer [I2CBufferSize]; // I2C read buffer + char I2CBuffer [I2CBufferSize]; // I2C read buffer + + char writeFlag [I2CBufferSize]; // to remember our if we wrote to this register + char lastWrite [I2CBufferSize]; // to remember what we wrote to this register + char lastRead [I2CBufferSize]; // to remember what we read from this register + char readChanges [I2CBufferSize]; // to log changes from previous read + char writeChanges [I2CBufferSize]; // to log changes from what we wrote + char * registers; // pointer to I2C read buffer IQS62xIO(); // constructor @@ -39,5 +46,5 @@ void configure(); // write configuration registers of the IQS62x void readAll(); // read all registers from the IQS62x void readIqsRegisters(int start, int count); // starting at "start" read count registers - void waitForReady(); // wait for IQS62x ready signal + void waitForIqsReady(); // wait for IQS62x ready signal }; \ No newline at end of file