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
IQS62x.cpp@20:7eb08a52b954, 2018-03-21 (annotated)
- Committer:
- mventer
- Date:
- Wed Mar 21 05:56:22 2018 +0000
- Revision:
- 20:7eb08a52b954
- Parent:
- 19:f0675a3150c7
For use with IQS621_DualPIR_ALS_Temp. Added output line to toggle and corrected spelling in some comments.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzqDev | 0:ce75ae1e8fc7 | 1 | // A class library for Azoteq IQS62x devices |
AzqDev | 3:e26d7c502309 | 2 | |
AzqDev | 2:c16cb655d4a4 | 3 | // Copyright 2017 Azoteq. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
AzqDev | 2:c16cb655d4a4 | 4 | |
AzqDev | 17:2d46eabb129e | 5 | // More info on IQS62x sensor ICs: http://www.azoteq.com/products/proxfusion?mbed |
AzqDev | 3:e26d7c502309 | 6 | |
AzqDev | 18:b85048ec91d2 | 7 | // ProxFusion 5-minute YouTube video: http://bit.ly/proxfusion-video |
AzqDev | 5:653138b5cae9 | 8 | |
AzqDev | 0:ce75ae1e8fc7 | 9 | #include "IQS62x.h" |
mventer | 20:7eb08a52b954 | 10 | DigitalOut toggle(PB_3); |
AzqDev | 0:ce75ae1e8fc7 | 11 | |
AzqDev | 0:ce75ae1e8fc7 | 12 | // constructor |
AzqDev | 12:5a9bbbd6e312 | 13 | IQS62xIO::IQS62xIO() : |
AzqDev | 0:ce75ae1e8fc7 | 14 | i2c( IQS_I2C_DATA_PIN, IQS_I2C_CLOCK_PIN ), // first run the constructor for mbed class I2C |
AzqDev | 0:ce75ae1e8fc7 | 15 | IQSready( IQS_READY_PIN ) // first run the constructor for mbed class DigitalIn |
AzqDev | 12:5a9bbbd6e312 | 16 | { |
AzqDev | 0:ce75ae1e8fc7 | 17 | registers = I2CBuffer; // pointer to the receive buffer |
AzqDev | 0:ce75ae1e8fc7 | 18 | I2CErrorCount = 0; // reset I2C error count |
AzqDev | 14:2514595e2753 | 19 | IQSframes = 0; // reset number of reads |
AzqDev | 0:ce75ae1e8fc7 | 20 | i2c.frequency( I2Cspeed ); // I2C clock frequency |
AzqDev | 12:5a9bbbd6e312 | 21 | |
AzqDev | 12:5a9bbbd6e312 | 22 | // this uses memory but is very handy in diagnostics |
mventer | 20:7eb08a52b954 | 23 | memset(writeFlag, 0, I2CBufferSize); // a table to remember if we wrote to a register |
AzqDev | 14:2514595e2753 | 24 | memset(lastWrite, 0, I2CBufferSize); // a table to remember what we wrote to a register |
mventer | 20:7eb08a52b954 | 25 | memset(lastRead, 0, I2CBufferSize); // a table to remember what we read the previous read cycle |
AzqDev | 14:2514595e2753 | 26 | memset(readChanges, 0, I2CBufferSize); // a table to mark up any registers that changed since the previous read |
AzqDev | 14:2514595e2753 | 27 | memset(readChangesEver, 0, I2CBufferSize); // a table to mark up any registers that changed BUT never forget |
AzqDev | 19:f0675a3150c7 | 28 | memset(writeChanges, 0, I2CBufferSize); // a table to mark up any registers that differ from what was written to it |
AzqDev | 12:5a9bbbd6e312 | 29 | } |
AzqDev | 0:ce75ae1e8fc7 | 30 | |
AzqDev | 7:5e7ea07265d9 | 31 | // write a single byte to an IQS62x register |
AzqDev | 12:5a9bbbd6e312 | 32 | void IQS62xIO::writeRegister(int address, int data) |
AzqDev | 12:5a9bbbd6e312 | 33 | { |
mventer | 20:7eb08a52b954 | 34 | toggle = !toggle; |
AzqDev | 12:5a9bbbd6e312 | 35 | writeFlag[ address & 0xff ] = 1; // remember which registers we changed |
AzqDev | 12:5a9bbbd6e312 | 36 | lastWrite[ address & 0xff ] = data & 0xff; // remember what we wrote |
AzqDev | 7:5e7ea07265d9 | 37 | char twoBytes [2]; |
AzqDev | 7:5e7ea07265d9 | 38 | int numberOfBytes = 2; |
AzqDev | 7:5e7ea07265d9 | 39 | twoBytes[0] = address & 0xff; |
AzqDev | 7:5e7ea07265d9 | 40 | twoBytes[1] = data & 0xff; |
AzqDev | 12:5a9bbbd6e312 | 41 | waitForIqsReady(); |
AzqDev | 12:5a9bbbd6e312 | 42 | if(0!=i2c.write(I2C_ADR,twoBytes,numberOfBytes,false)) |
AzqDev | 0:ce75ae1e8fc7 | 43 | I2CErrorCount++; |
AzqDev | 0:ce75ae1e8fc7 | 44 | } |
AzqDev | 0:ce75ae1e8fc7 | 45 | |
AzqDev | 7:5e7ea07265d9 | 46 | // configure the IQS62x |
AzqDev | 8:f9c71b39c602 | 47 | #include "deviceType.h" // to check if we should override the configuration definition |
AzqDev | 8:f9c71b39c602 | 48 | #ifndef OVERRIDE_CONFIGURE |
AzqDev | 9:7c2666dfbc9a | 49 | // if there is no override for configure() then we use this simple version |
AzqDev | 12:5a9bbbd6e312 | 50 | void IQS62xIO::configure() |
AzqDev | 12:5a9bbbd6e312 | 51 | { |
AzqDev | 7:5e7ea07265d9 | 52 | writeRegister(0xd0,0x40); // simplest config : just acknowledge/clear the reset flag |
AzqDev | 7:5e7ea07265d9 | 53 | } |
AzqDev | 8:f9c71b39c602 | 54 | #endif |
AzqDev | 7:5e7ea07265d9 | 55 | |
AzqDev | 10:8fdac52881aa | 56 | // read N registers, starting at provided offset |
AzqDev | 12:5a9bbbd6e312 | 57 | void IQS62xIO::readIqsRegisters(int start, int count) |
AzqDev | 12:5a9bbbd6e312 | 58 | { |
AzqDev | 10:8fdac52881aa | 59 | memset(I2CBuffer,0x55,I2CBufferSize); // "clear" i2c receive buffer |
AzqDev | 14:2514595e2753 | 60 | IQSframes++; // count frames |
AzqDev | 12:5a9bbbd6e312 | 61 | waitForIqsReady(); |
AzqDev | 12:5a9bbbd6e312 | 62 | char i2c_start_address [1]; |
AzqDev | 10:8fdac52881aa | 63 | i2c_start_address[0] = start & 0xff; |
AzqDev | 10:8fdac52881aa | 64 | int numberOfBytes = 1; |
AzqDev | 10:8fdac52881aa | 65 | // write start address to the IQS62x address register |
AzqDev | 12:5a9bbbd6e312 | 66 | if(0!=i2c.write(I2C_ADR,i2c_start_address,numberOfBytes,false)) |
AzqDev | 0:ce75ae1e8fc7 | 67 | I2CErrorCount++; |
AzqDev | 12:5a9bbbd6e312 | 68 | waitForIqsReady(); |
AzqDev | 10:8fdac52881aa | 69 | numberOfBytes = count % I2CBufferSize; |
AzqDev | 0:ce75ae1e8fc7 | 70 | // read register values into a buffer |
AzqDev | 0:ce75ae1e8fc7 | 71 | if(0!=i2c.read(I2C_ADR,I2CBuffer,numberOfBytes,false)) |
AzqDev | 0:ce75ae1e8fc7 | 72 | I2CErrorCount++; |
AzqDev | 12:5a9bbbd6e312 | 73 | |
mventer | 20:7eb08a52b954 | 74 | #define DONT_CHECK_FOR_IQS_CHANGES 1 |
AzqDev | 12:5a9bbbd6e312 | 75 | #ifndef DONT_CHECK_FOR_IQS_CHANGES |
AzqDev | 12:5a9bbbd6e312 | 76 | // this is optional but handy diagnostics |
AzqDev | 19:f0675a3150c7 | 77 | // we build six tables: |
mventer | 20:7eb08a52b954 | 78 | //writeFlag - a table to remember if we wrote to a register |
AzqDev | 19:f0675a3150c7 | 79 | //lastWrite - a table to remember what we wrote to a register |
mventer | 20:7eb08a52b954 | 80 | //lastRead - a table to remember what we read the previous read cycle |
AzqDev | 19:f0675a3150c7 | 81 | //readChanges - a table to mark up any registers that changed since the previous read |
AzqDev | 19:f0675a3150c7 | 82 | //readChangesEver - a table to mark up any registers that changed BUT never forget |
AzqDev | 19:f0675a3150c7 | 83 | //writeChanges - a table to mark up any registers that differ from what was written to it |
AzqDev | 19:f0675a3150c7 | 84 | if (IQSframes == 1) memcpy(lastRead, I2CBuffer,I2CBufferSize); // if this is the first frame init the history buffer |
AzqDev | 14:2514595e2753 | 85 | if (IQSframes < 20) memset(readChangesEver,0,I2CBufferSize); // clear this until we have had 20 full reads |
AzqDev | 12:5a9bbbd6e312 | 86 | for(int i=0; i<I2CBufferSize; i++) { |
AzqDev | 12:5a9bbbd6e312 | 87 | if (writeFlag[i] == 1) { // only if we previously wrote to this register we check it |
AzqDev | 12:5a9bbbd6e312 | 88 | if( I2CBuffer[i] != lastWrite[i] ) |
AzqDev | 12:5a9bbbd6e312 | 89 | writeChanges[i]=1; // if register different from what we wrote, mark it |
AzqDev | 12:5a9bbbd6e312 | 90 | } |
AzqDev | 14:2514595e2753 | 91 | if ( I2CBuffer[i] != lastRead[i] ) { |
AzqDev | 13:71f8ee16a3a1 | 92 | readChanges[i]=20; // if register differs from previous read, highlight it for 20 read cycles |
AzqDev | 14:2514595e2753 | 93 | if (readChangesEver[i] < 127) readChangesEver[i]++; // count changes up to 127 and never forget |
AzqDev | 14:2514595e2753 | 94 | } else { |
AzqDev | 13:71f8ee16a3a1 | 95 | if (readChanges[i] > 0) readChanges[i]--; // bring it closer to zero |
AzqDev | 13:71f8ee16a3a1 | 96 | } |
AzqDev | 12:5a9bbbd6e312 | 97 | } |
AzqDev | 19:f0675a3150c7 | 98 | memcpy(lastRead, I2CBuffer, I2CBufferSize); // preserve history for next round |
AzqDev | 12:5a9bbbd6e312 | 99 | #endif |
AzqDev | 0:ce75ae1e8fc7 | 100 | } |
AzqDev | 0:ce75ae1e8fc7 | 101 | |
AzqDev | 0:ce75ae1e8fc7 | 102 | // wait for IQS62x to provide a ready signal (low) on IQS62x_ready pin |
AzqDev | 12:5a9bbbd6e312 | 103 | void IQS62xIO::waitForIqsReady() |
AzqDev | 12:5a9bbbd6e312 | 104 | { |
AzqDev | 12:5a9bbbd6e312 | 105 | int timeout=0; |
AzqDev | 0:ce75ae1e8fc7 | 106 | while (1) { |
AzqDev | 0:ce75ae1e8fc7 | 107 | if(IQSready==1) break; |
AzqDev | 12:5a9bbbd6e312 | 108 | if (timeout++ > 1000000) goto fatal_error; |
AzqDev | 0:ce75ae1e8fc7 | 109 | } |
AzqDev | 0:ce75ae1e8fc7 | 110 | timeout=0; |
AzqDev | 0:ce75ae1e8fc7 | 111 | while (1) { |
AzqDev | 0:ce75ae1e8fc7 | 112 | if(IQSready==0) break; |
AzqDev | 0:ce75ae1e8fc7 | 113 | if (timeout++ > 1000000) goto fatal_error; |
AzqDev | 0:ce75ae1e8fc7 | 114 | } |
AzqDev | 16:b77c819f6c6a | 115 | return;; |
AzqDev | 12:5a9bbbd6e312 | 116 | fatal_error: |
AzqDev | 12:5a9bbbd6e312 | 117 | error ("Fatal Error: IQS62x ready pin is not toggling"); |
AzqDev | 16:b77c819f6c6a | 118 | } |
AzqDev | 16:b77c819f6c6a | 119 | |
AzqDev | 16:b77c819f6c6a | 120 | // return one of the tables of changes that we maintain |
AzqDev | 16:b77c819f6c6a | 121 | char * IQS62xIO::getTable( int option ) |
AzqDev | 16:b77c819f6c6a | 122 | { |
AzqDev | 16:b77c819f6c6a | 123 | switch ( option ) { |
AzqDev | 16:b77c819f6c6a | 124 | case 1 : return readChangesEver; // a table to flag any register that ever changed |
AzqDev | 16:b77c819f6c6a | 125 | case 2 : return readChanges; // a table to flag any register that changed in the last 20 reads |
AzqDev | 16:b77c819f6c6a | 126 | case 3 : return writeFlag; // a table to flag any register we wrote to or initialized |
AzqDev | 16:b77c819f6c6a | 127 | case 4 : return writeChanges; // a table to flag any register we wrote to but then its value changed |
AzqDev | 16:b77c819f6c6a | 128 | default: return NULL;; |
AzqDev | 16:b77c819f6c6a | 129 | } |
AzqDev | 0:ce75ae1e8fc7 | 130 | } |