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@13:71f8ee16a3a1, 2017-05-13 (annotated)
- Committer:
- AzqDev
- Date:
- Sat May 13 09:26:47 2017 +0000
- Revision:
- 13:71f8ee16a3a1
- Parent:
- 12:5a9bbbd6e312
- Child:
- 14:2514595e2753
Highlight table
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 | 7:5e7ea07265d9 | 5 | // More info on IQS624 sensor IC: http://www.azoteq.com/products/proxfusion/iqs624?mbed |
AzqDev | 3:e26d7c502309 | 6 | |
AzqDev | 6:89bdb6f6a5c1 | 7 | // IQS624 1-minute youtube video: http://bit.ly/IQS624Video |
AzqDev | 5:653138b5cae9 | 8 | |
AzqDev | 0:ce75ae1e8fc7 | 9 | #include "IQS62x.h" |
AzqDev | 0:ce75ae1e8fc7 | 10 | |
AzqDev | 0:ce75ae1e8fc7 | 11 | // constructor |
AzqDev | 12:5a9bbbd6e312 | 12 | IQS62xIO::IQS62xIO() : |
AzqDev | 0:ce75ae1e8fc7 | 13 | i2c( IQS_I2C_DATA_PIN, IQS_I2C_CLOCK_PIN ), // first run the constructor for mbed class I2C |
AzqDev | 0:ce75ae1e8fc7 | 14 | IQSready( IQS_READY_PIN ) // first run the constructor for mbed class DigitalIn |
AzqDev | 12:5a9bbbd6e312 | 15 | { |
AzqDev | 0:ce75ae1e8fc7 | 16 | registers = I2CBuffer; // pointer to the receive buffer |
AzqDev | 0:ce75ae1e8fc7 | 17 | I2CErrorCount = 0; // reset I2C error count |
AzqDev | 0:ce75ae1e8fc7 | 18 | i2c.frequency( I2Cspeed ); // I2C clock frequency |
AzqDev | 12:5a9bbbd6e312 | 19 | |
AzqDev | 12:5a9bbbd6e312 | 20 | // this uses memory but is very handy in diagnostics |
AzqDev | 12:5a9bbbd6e312 | 21 | memset(writeFlag, 0, I2CBufferSize); // a table to remmber if we wrote to a register |
AzqDev | 12:5a9bbbd6e312 | 22 | memset(lastWrite, 0, I2CBufferSize); // a table to remember what we wrote to a register |
AzqDev | 12:5a9bbbd6e312 | 23 | memset(lastRead, 0, I2CBufferSize); // a table to remmber what we read the previous read cycle |
AzqDev | 12:5a9bbbd6e312 | 24 | memset(readChanges, 0, I2CBufferSize); // a table to mark up any registers that changed since the previous read |
AzqDev | 12:5a9bbbd6e312 | 25 | memset(writeChanges, 0, I2CBufferSize); // a table to mark up any registers that differs from what was written to it |
AzqDev | 12:5a9bbbd6e312 | 26 | } |
AzqDev | 0:ce75ae1e8fc7 | 27 | |
AzqDev | 7:5e7ea07265d9 | 28 | // write a single byte to an IQS62x register |
AzqDev | 12:5a9bbbd6e312 | 29 | void IQS62xIO::writeRegister(int address, int data) |
AzqDev | 12:5a9bbbd6e312 | 30 | { |
AzqDev | 12:5a9bbbd6e312 | 31 | writeFlag[ address & 0xff ] = 1; // remember which registers we changed |
AzqDev | 12:5a9bbbd6e312 | 32 | lastWrite[ address & 0xff ] = data & 0xff; // remember what we wrote |
AzqDev | 7:5e7ea07265d9 | 33 | char twoBytes [2]; |
AzqDev | 7:5e7ea07265d9 | 34 | int numberOfBytes = 2; |
AzqDev | 7:5e7ea07265d9 | 35 | twoBytes[0] = address & 0xff; |
AzqDev | 7:5e7ea07265d9 | 36 | twoBytes[1] = data & 0xff; |
AzqDev | 12:5a9bbbd6e312 | 37 | waitForIqsReady(); |
AzqDev | 12:5a9bbbd6e312 | 38 | if(0!=i2c.write(I2C_ADR,twoBytes,numberOfBytes,false)) |
AzqDev | 0:ce75ae1e8fc7 | 39 | I2CErrorCount++; |
AzqDev | 0:ce75ae1e8fc7 | 40 | } |
AzqDev | 0:ce75ae1e8fc7 | 41 | |
AzqDev | 7:5e7ea07265d9 | 42 | // configure the IQS62x |
AzqDev | 8:f9c71b39c602 | 43 | #include "deviceType.h" // to check if we should override the configuration definition |
AzqDev | 8:f9c71b39c602 | 44 | #ifndef OVERRIDE_CONFIGURE |
AzqDev | 9:7c2666dfbc9a | 45 | // if there is no override for configure() then we use this simple version |
AzqDev | 12:5a9bbbd6e312 | 46 | void IQS62xIO::configure() |
AzqDev | 12:5a9bbbd6e312 | 47 | { |
AzqDev | 7:5e7ea07265d9 | 48 | writeRegister(0xd0,0x40); // simplest config : just acknowledge/clear the reset flag |
AzqDev | 7:5e7ea07265d9 | 49 | } |
AzqDev | 8:f9c71b39c602 | 50 | #endif |
AzqDev | 7:5e7ea07265d9 | 51 | |
AzqDev | 10:8fdac52881aa | 52 | // read N registers, starting at provided offset |
AzqDev | 12:5a9bbbd6e312 | 53 | void IQS62xIO::readIqsRegisters(int start, int count) |
AzqDev | 12:5a9bbbd6e312 | 54 | { |
AzqDev | 10:8fdac52881aa | 55 | memset(I2CBuffer,0x55,I2CBufferSize); // "clear" i2c receive buffer |
AzqDev | 12:5a9bbbd6e312 | 56 | waitForIqsReady(); |
AzqDev | 12:5a9bbbd6e312 | 57 | char i2c_start_address [1]; |
AzqDev | 10:8fdac52881aa | 58 | i2c_start_address[0] = start & 0xff; |
AzqDev | 10:8fdac52881aa | 59 | int numberOfBytes = 1; |
AzqDev | 10:8fdac52881aa | 60 | // write start address to the IQS62x address register |
AzqDev | 12:5a9bbbd6e312 | 61 | if(0!=i2c.write(I2C_ADR,i2c_start_address,numberOfBytes,false)) |
AzqDev | 0:ce75ae1e8fc7 | 62 | I2CErrorCount++; |
AzqDev | 12:5a9bbbd6e312 | 63 | waitForIqsReady(); |
AzqDev | 10:8fdac52881aa | 64 | numberOfBytes = count % I2CBufferSize; |
AzqDev | 0:ce75ae1e8fc7 | 65 | // read register values into a buffer |
AzqDev | 0:ce75ae1e8fc7 | 66 | if(0!=i2c.read(I2C_ADR,I2CBuffer,numberOfBytes,false)) |
AzqDev | 0:ce75ae1e8fc7 | 67 | I2CErrorCount++; |
AzqDev | 12:5a9bbbd6e312 | 68 | |
AzqDev | 12:5a9bbbd6e312 | 69 | #define PLEASE_CHECK_FOR_IQS_CHANGES 1 |
AzqDev | 12:5a9bbbd6e312 | 70 | #ifndef DONT_CHECK_FOR_IQS_CHANGES |
AzqDev | 12:5a9bbbd6e312 | 71 | // this is optional but handy diagnostics |
AzqDev | 12:5a9bbbd6e312 | 72 | // we build two tables: |
AzqDev | 12:5a9bbbd6e312 | 73 | // writeChanges[i] is set to 1 if the register content differs from what we wrote to it |
AzqDev | 12:5a9bbbd6e312 | 74 | // readChanges[i] is set to 1 if the register value differs from the previously read value |
AzqDev | 12:5a9bbbd6e312 | 75 | for(int i=0; i<I2CBufferSize; i++) { |
AzqDev | 12:5a9bbbd6e312 | 76 | if (writeFlag[i] == 1) { // only if we previously wrote to this register we check it |
AzqDev | 12:5a9bbbd6e312 | 77 | if( I2CBuffer[i] != lastWrite[i] ) |
AzqDev | 12:5a9bbbd6e312 | 78 | writeChanges[i]=1; // if register different from what we wrote, mark it |
AzqDev | 12:5a9bbbd6e312 | 79 | } |
AzqDev | 12:5a9bbbd6e312 | 80 | if ( I2CBuffer[i] != lastRead[i] ) |
AzqDev | 13:71f8ee16a3a1 | 81 | readChanges[i]=20; // if register differs from previous read, highlight it for 20 read cycles |
AzqDev | 13:71f8ee16a3a1 | 82 | else { |
AzqDev | 13:71f8ee16a3a1 | 83 | if (readChanges[i] > 0) readChanges[i]--; // bring it closer to zero |
AzqDev | 13:71f8ee16a3a1 | 84 | } |
AzqDev | 12:5a9bbbd6e312 | 85 | } |
AzqDev | 12:5a9bbbd6e312 | 86 | memcpy(lastRead, I2CBuffer, I2CBufferSize); // preserve data for next round |
AzqDev | 12:5a9bbbd6e312 | 87 | #endif |
AzqDev | 0:ce75ae1e8fc7 | 88 | } |
AzqDev | 0:ce75ae1e8fc7 | 89 | |
AzqDev | 0:ce75ae1e8fc7 | 90 | // wait for IQS62x to provide a ready signal (low) on IQS62x_ready pin |
AzqDev | 12:5a9bbbd6e312 | 91 | void IQS62xIO::waitForIqsReady() |
AzqDev | 12:5a9bbbd6e312 | 92 | { |
AzqDev | 12:5a9bbbd6e312 | 93 | int timeout=0; |
AzqDev | 0:ce75ae1e8fc7 | 94 | while (1) { |
AzqDev | 0:ce75ae1e8fc7 | 95 | if(IQSready==1) break; |
AzqDev | 12:5a9bbbd6e312 | 96 | if (timeout++ > 1000000) goto fatal_error; |
AzqDev | 0:ce75ae1e8fc7 | 97 | } |
AzqDev | 0:ce75ae1e8fc7 | 98 | timeout=0; |
AzqDev | 0:ce75ae1e8fc7 | 99 | while (1) { |
AzqDev | 0:ce75ae1e8fc7 | 100 | if(IQSready==0) break; |
AzqDev | 0:ce75ae1e8fc7 | 101 | if (timeout++ > 1000000) goto fatal_error; |
AzqDev | 0:ce75ae1e8fc7 | 102 | } |
AzqDev | 0:ce75ae1e8fc7 | 103 | return; |
AzqDev | 12:5a9bbbd6e312 | 104 | fatal_error: |
AzqDev | 12:5a9bbbd6e312 | 105 | error ("Fatal Error: IQS62x ready pin is not toggling"); |
AzqDev | 0:ce75ae1e8fc7 | 106 | } |