Driver for the AKM AK9752 IR sensor device.

Dependents:   AKDP-RevD7_014

Library for the AK9752 Ultra-Small IR Sensor with I2C Interface. Includes integrated temperature sensor (0 - 50C) and 16-bit ADC.

Committer:
tkstreet
Date:
Tue Nov 01 17:03:31 2016 +0000
Revision:
3:2035a4a54d3f
Parent:
0:51fa46d39a3e
Child:
6:254b7e5820e7
Added comments and documentation to library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
masahikofukasawa 0:51fa46d39a3e 1 #include "AK9752.h"
masahikofukasawa 0:51fa46d39a3e 2 #include "AK9752_reg.h"
masahikofukasawa 0:51fa46d39a3e 3
tkstreet 3:2035a4a54d3f 4 /**
tkstreet 3:2035a4a54d3f 5 * Combine low and high int8_t bytes of Temp/IR data and store in int16_t variable
tkstreet 3:2035a4a54d3f 6 */
masahikofukasawa 0:51fa46d39a3e 7 #define CONV16I(high,low) ((int16_t)(((high) << 8) | (low)))
masahikofukasawa 0:51fa46d39a3e 8
masahikofukasawa 0:51fa46d39a3e 9 #define LEN_ONE_BYTE 1 /**<! Data length of 1 byte data. */
masahikofukasawa 0:51fa46d39a3e 10 #define LEN_BUF_THRESHOLD 8 /**<! Data length of Threshold settings. From THIRH to THTMPL */
tkstreet 3:2035a4a54d3f 11 #define LEN_BUF_IR_DATA 7 /**<! Data length of IR sensor data: ST1,INTCAUSE,IR(16),TMP(16),ST2. */
tkstreet 3:2035a4a54d3f 12 #define VAL_SOFTWARE_RESET 0x01 /**<! Software reset value. */
tkstreet 3:2035a4a54d3f 13 #define ST1_STATUS_FLAG_DRDY 0x01 /**<! Data Ready*/
tkstreet 3:2035a4a54d3f 14 #define INT_STATUS_FLAG_DR 0x01 /**<! Data Read */
tkstreet 3:2035a4a54d3f 15 #define INT_STATUS_FLAG_TMPL 0x02 /**<! Temp low threshold reached */
tkstreet 3:2035a4a54d3f 16 #define INT_STATUS_FLAG_TMPH 0x04 /**<! Temp high threshold reached */
tkstreet 3:2035a4a54d3f 17 #define INT_STATUS_FLAG_IRL 0x08 /**<! IR low threshold reached */
tkstreet 3:2035a4a54d3f 18 #define INT_STATUS_FLAG_IRH 0x10 /**<! IR high threshold reached */
tkstreet 3:2035a4a54d3f 19 #define INT_STATUS_MASK 0x1F /**<! Mask highest 3 bits of status */
tkstreet 3:2035a4a54d3f 20 #define ST2_STATUS_FLAG_DOR 0x01 /**<! Data overrun (data read is required) */
masahikofukasawa 0:51fa46d39a3e 21
masahikofukasawa 0:51fa46d39a3e 22
masahikofukasawa 0:51fa46d39a3e 23 AK9752::AK9752() {
masahikofukasawa 0:51fa46d39a3e 24 }
masahikofukasawa 0:51fa46d39a3e 25
masahikofukasawa 0:51fa46d39a3e 26 void AK9752::init(I2C *conn, SlaveAddress addr) {
masahikofukasawa 0:51fa46d39a3e 27 slaveAddress = addr;
masahikofukasawa 0:51fa46d39a3e 28 connection = conn;
masahikofukasawa 0:51fa46d39a3e 29 }
masahikofukasawa 0:51fa46d39a3e 30
masahikofukasawa 0:51fa46d39a3e 31 AK9752::Status AK9752::checkConnection() {
masahikofukasawa 0:51fa46d39a3e 32 AK9752::Status status = AK9752::SUCCESS;
masahikofukasawa 0:51fa46d39a3e 33
masahikofukasawa 0:51fa46d39a3e 34 // Gets the WIA register value.
masahikofukasawa 0:51fa46d39a3e 35 char buf[2];
masahikofukasawa 0:51fa46d39a3e 36 if ((status=AK9752::read(AK9752_REG_ADDR_WIA1, buf, 2)) != SUCCESS) {
masahikofukasawa 0:51fa46d39a3e 37 return status;
masahikofukasawa 0:51fa46d39a3e 38 }
masahikofukasawa 0:51fa46d39a3e 39
masahikofukasawa 0:51fa46d39a3e 40 // Checks the obtained value equals to the supposed value.
masahikofukasawa 0:51fa46d39a3e 41 if ( (buf[0] != AK9752_REG_VALUE_WIA1) || (buf[1] != AK9752_REG_VALUE_WIA2) ) {
masahikofukasawa 0:51fa46d39a3e 42 return AK9752::ERROR;
masahikofukasawa 0:51fa46d39a3e 43 }
masahikofukasawa 0:51fa46d39a3e 44
masahikofukasawa 0:51fa46d39a3e 45 return status;
masahikofukasawa 0:51fa46d39a3e 46 }
masahikofukasawa 0:51fa46d39a3e 47
masahikofukasawa 0:51fa46d39a3e 48 AK9752::Status AK9752::read(char registerAddress, char *buf, int length) {
tkstreet 3:2035a4a54d3f 49 // Tell slave address of where to read data
masahikofukasawa 0:51fa46d39a3e 50 if (connection->write((slaveAddress << 1), &registerAddress, LEN_ONE_BYTE) != 0) {
masahikofukasawa 0:51fa46d39a3e 51 // I2C write failed.
masahikofukasawa 0:51fa46d39a3e 52 return AK9752::ERROR_I2C_WRITE;
masahikofukasawa 0:51fa46d39a3e 53 }
masahikofukasawa 0:51fa46d39a3e 54
tkstreet 3:2035a4a54d3f 55 // Read register data (converts 7-bit address to 8-bit)
masahikofukasawa 0:51fa46d39a3e 56 if (connection->read((slaveAddress << 1), buf, length) != 0) {
masahikofukasawa 0:51fa46d39a3e 57 // I2C read failed.
masahikofukasawa 0:51fa46d39a3e 58 return AK9752::ERROR_I2C_READ;
masahikofukasawa 0:51fa46d39a3e 59 }
masahikofukasawa 0:51fa46d39a3e 60
masahikofukasawa 0:51fa46d39a3e 61 return AK9752::SUCCESS;
masahikofukasawa 0:51fa46d39a3e 62 }
masahikofukasawa 0:51fa46d39a3e 63
masahikofukasawa 0:51fa46d39a3e 64 AK9752::Status AK9752::write(char registerAddress, const char *buf, int length) {
tkstreet 3:2035a4a54d3f 65 int bufLength = length + 1; // Increase size to account for address byte
masahikofukasawa 0:51fa46d39a3e 66 char data[bufLength];
masahikofukasawa 0:51fa46d39a3e 67
masahikofukasawa 0:51fa46d39a3e 68 // Creates data to be sent.
tkstreet 3:2035a4a54d3f 69 data[0] = registerAddress; // Place register address in first byte
masahikofukasawa 0:51fa46d39a3e 70 for (int i=0; i < length; i++) {
tkstreet 3:2035a4a54d3f 71 data[1+i] = buf[i]; // Load write data starting at second byte (i.e. data[1])
masahikofukasawa 0:51fa46d39a3e 72 }
masahikofukasawa 0:51fa46d39a3e 73
tkstreet 3:2035a4a54d3f 74 // Initiate I2C write command
masahikofukasawa 0:51fa46d39a3e 75 if (connection->write((slaveAddress << 1), data, bufLength) != 0) {
masahikofukasawa 0:51fa46d39a3e 76 // I2C write failed.
masahikofukasawa 0:51fa46d39a3e 77 return AK9752::ERROR_I2C_WRITE;
masahikofukasawa 0:51fa46d39a3e 78 }
masahikofukasawa 0:51fa46d39a3e 79
masahikofukasawa 0:51fa46d39a3e 80 return AK9752::SUCCESS;
masahikofukasawa 0:51fa46d39a3e 81 }
masahikofukasawa 0:51fa46d39a3e 82
masahikofukasawa 0:51fa46d39a3e 83 AK9752::Status AK9752::getInterruptEnable(InterruptStatus *intStatus){
masahikofukasawa 0:51fa46d39a3e 84 Status status;
masahikofukasawa 0:51fa46d39a3e 85
masahikofukasawa 0:51fa46d39a3e 86 char buf = 0;
masahikofukasawa 0:51fa46d39a3e 87 if((status=read(AK9752_REG_ADDR_INTEN, &buf, LEN_ONE_BYTE)) != SUCCESS) {
tkstreet 3:2035a4a54d3f 88 return status; // Read failed
masahikofukasawa 0:51fa46d39a3e 89 }
masahikofukasawa 0:51fa46d39a3e 90
tkstreet 3:2035a4a54d3f 91 // Set interrupt status flags individually
masahikofukasawa 0:51fa46d39a3e 92 intStatus->irh = ((buf & INT_STATUS_FLAG_IRH) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 93 intStatus->irl = ((buf & INT_STATUS_FLAG_IRL) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 94 intStatus->tmph = ((buf & INT_STATUS_FLAG_TMPH) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 95 intStatus->tmpl = ((buf & INT_STATUS_FLAG_TMPL) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 96 intStatus->dr = ((buf & INT_STATUS_FLAG_DR) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 97
tkstreet 3:2035a4a54d3f 98 return SUCCESS; // Read succeeded
masahikofukasawa 0:51fa46d39a3e 99 }
masahikofukasawa 0:51fa46d39a3e 100
tkstreet 3:2035a4a54d3f 101 AK9752::Status AK9752::setInterruptEnable(const InterruptStatus *intStatus) {
masahikofukasawa 0:51fa46d39a3e 102 char buf = 0;
masahikofukasawa 0:51fa46d39a3e 103
tkstreet 3:2035a4a54d3f 104 // If interrupt bit is 1, set corresponding buf bit to 1, otherwise set to 0
masahikofukasawa 0:51fa46d39a3e 105 buf += intStatus->irh ? INT_STATUS_FLAG_IRH : 0;
masahikofukasawa 0:51fa46d39a3e 106 buf += intStatus->irl ? INT_STATUS_FLAG_IRL : 0;
masahikofukasawa 0:51fa46d39a3e 107 buf += intStatus->tmph ? INT_STATUS_FLAG_TMPH : 0;
masahikofukasawa 0:51fa46d39a3e 108 buf += intStatus->tmpl ? INT_STATUS_FLAG_TMPL : 0;
masahikofukasawa 0:51fa46d39a3e 109 buf += intStatus->dr ? INT_STATUS_FLAG_DR : 0;
masahikofukasawa 0:51fa46d39a3e 110
tkstreet 3:2035a4a54d3f 111 // Perform interrupt status write operation
masahikofukasawa 0:51fa46d39a3e 112 Status status;
masahikofukasawa 0:51fa46d39a3e 113 if ((status=write(AK9752_REG_ADDR_INTEN, &buf, LEN_ONE_BYTE)) != SUCCESS) {
masahikofukasawa 0:51fa46d39a3e 114 return status;
masahikofukasawa 0:51fa46d39a3e 115 }
masahikofukasawa 0:51fa46d39a3e 116
tkstreet 3:2035a4a54d3f 117 // Read back the interrupt status
masahikofukasawa 0:51fa46d39a3e 118 char readback = 0;
masahikofukasawa 0:51fa46d39a3e 119 if ((status=read(AK9752_REG_ADDR_INTEN, &readback, LEN_ONE_BYTE)) != SUCCESS) {
tkstreet 3:2035a4a54d3f 120 return status; // Read back operation failed
masahikofukasawa 0:51fa46d39a3e 121 }
masahikofukasawa 0:51fa46d39a3e 122
tkstreet 3:2035a4a54d3f 123 if ((readback & INT_STATUS_MASK) != buf) {
tkstreet 3:2035a4a54d3f 124 return ERROR; // Read back succeeded, but values incorrect
masahikofukasawa 0:51fa46d39a3e 125 }
tkstreet 3:2035a4a54d3f 126
masahikofukasawa 0:51fa46d39a3e 127 return SUCCESS;
masahikofukasawa 0:51fa46d39a3e 128 }
masahikofukasawa 0:51fa46d39a3e 129
masahikofukasawa 0:51fa46d39a3e 130 AK9752::Status AK9752::setThreshold(const Threshold *th) {
masahikofukasawa 0:51fa46d39a3e 131 Status status;
masahikofukasawa 0:51fa46d39a3e 132 char buf[LEN_BUF_THRESHOLD];
masahikofukasawa 0:51fa46d39a3e 133
tkstreet 3:2035a4a54d3f 134 // Mask off irrelevant byte, cast 8-bit values to 16-bit
tkstreet 3:2035a4a54d3f 135 //
tkstreet 3:2035a4a54d3f 136 buf[0] = (char)(((uint16_t)th->thirh & 0x00FF)); // THIRHL Register
tkstreet 3:2035a4a54d3f 137 buf[1] = (char)(((uint16_t)th->thirh & 0xFF00) >> 8); // THIRHH Register
tkstreet 3:2035a4a54d3f 138 buf[2] = (char)(((uint16_t)th->thirl & 0x00FF)); // THIRLL Register
tkstreet 3:2035a4a54d3f 139 buf[3] = (char)(((uint16_t)th->thirl & 0xFF00) >> 8); // THIRLH Register
tkstreet 3:2035a4a54d3f 140 buf[4] = (char)(((uint16_t)th->thtmph & 0x00FF)); // THTMPHL Register
tkstreet 3:2035a4a54d3f 141 buf[5] = (char)(((uint16_t)th->thtmph & 0xFF00) >> 8); // THTMPHH Register
tkstreet 3:2035a4a54d3f 142 buf[6] = (char)(((uint16_t)th->thtmpl & 0x00FF)); // THTMPLL Register
tkstreet 3:2035a4a54d3f 143 buf[7] = (char)(((uint16_t)th->thtmpl & 0xFF00) >> 8); // THTMPLH Register
masahikofukasawa 0:51fa46d39a3e 144
tkstreet 3:2035a4a54d3f 145 // Perform threshold register write operaton
masahikofukasawa 0:51fa46d39a3e 146 if ((status=write(AK9752_REG_ADDR_THIRHL, buf, LEN_BUF_THRESHOLD)) != SUCCESS) {
tkstreet 3:2035a4a54d3f 147 return status; // Write operation failed
masahikofukasawa 0:51fa46d39a3e 148 }
masahikofukasawa 0:51fa46d39a3e 149
tkstreet 3:2035a4a54d3f 150 return SUCCESS; // Write operation succeeded
masahikofukasawa 0:51fa46d39a3e 151 }
masahikofukasawa 0:51fa46d39a3e 152
masahikofukasawa 0:51fa46d39a3e 153 AK9752::Status AK9752::getThreshold(Threshold *th) {
masahikofukasawa 0:51fa46d39a3e 154 Status status;
masahikofukasawa 0:51fa46d39a3e 155 char buf[LEN_BUF_THRESHOLD];
masahikofukasawa 0:51fa46d39a3e 156
tkstreet 3:2035a4a54d3f 157 // Perform threshold register read operation
masahikofukasawa 0:51fa46d39a3e 158 if ((status=read(AK9752_REG_ADDR_THIRHL, buf, LEN_BUF_THRESHOLD)) != SUCCESS) {
tkstreet 3:2035a4a54d3f 159 return status; // Read operation failed
masahikofukasawa 0:51fa46d39a3e 160 }
masahikofukasawa 0:51fa46d39a3e 161
tkstreet 3:2035a4a54d3f 162 // Combine low and high bytes of data
masahikofukasawa 0:51fa46d39a3e 163 th->thirh = CONV16I(buf[1], buf[0]);
masahikofukasawa 0:51fa46d39a3e 164 th->thirl = CONV16I(buf[3], buf[2]);
masahikofukasawa 0:51fa46d39a3e 165 th->thtmph = CONV16I(buf[5], buf[4]);
masahikofukasawa 0:51fa46d39a3e 166 th->thtmpl = CONV16I(buf[7], buf[6]);
masahikofukasawa 0:51fa46d39a3e 167
tkstreet 3:2035a4a54d3f 168 return SUCCESS; // Read operation succeeded
masahikofukasawa 0:51fa46d39a3e 169 }
masahikofukasawa 0:51fa46d39a3e 170
masahikofukasawa 0:51fa46d39a3e 171 AK9752::Status AK9752::getOperationMode(OperationMode *mode, FcTmp *fc_tmp, FcIr *fc_ir){
tkstreet 3:2035a4a54d3f 172 Status status;
masahikofukasawa 0:51fa46d39a3e 173
masahikofukasawa 0:51fa46d39a3e 174 char buf[2];
masahikofukasawa 0:51fa46d39a3e 175 if ((status=read(AK9752_REG_ADDR_CNTL1, buf, 2)) != SUCCESS) {
tkstreet 3:2035a4a54d3f 176 return status; // Read operation failed
masahikofukasawa 0:51fa46d39a3e 177 }
masahikofukasawa 0:51fa46d39a3e 178
tkstreet 3:2035a4a54d3f 179 *fc_tmp = AK9752::FcTmp((buf[0] & 0x1C)>>2); // Read CNTL1[2]-CNTL1[4]
tkstreet 3:2035a4a54d3f 180 *fc_ir = AK9752::FcIr(buf[0] & 0x03); // Read CNTL1[0]-CNTL1[1]
tkstreet 3:2035a4a54d3f 181 *mode = AK9752::OperationMode(buf[1] & 0x03); // Read CNTL2[0]-CNTL2[1]
tkstreet 3:2035a4a54d3f 182
tkstreet 3:2035a4a54d3f 183 return SUCCESS; // Read operation succeeded
masahikofukasawa 0:51fa46d39a3e 184 }
masahikofukasawa 0:51fa46d39a3e 185
masahikofukasawa 0:51fa46d39a3e 186 AK9752::Status AK9752::setOperationMode(OperationMode mode, FcTmp fc_tmp, FcIr fc_ir){
masahikofukasawa 0:51fa46d39a3e 187 Status status;
masahikofukasawa 0:51fa46d39a3e 188
masahikofukasawa 0:51fa46d39a3e 189 char buf[2];
tkstreet 3:2035a4a54d3f 190 buf[0] = (fc_tmp<<2 | fc_ir&0x03); // Combine FCTMP & FCIR in 1 byte
masahikofukasawa 0:51fa46d39a3e 191 buf[1] = mode;
tkstreet 3:2035a4a54d3f 192
tkstreet 3:2035a4a54d3f 193 // Write 2 bytes starting from CNTL1 -> write CNTL1 and CNTL2
masahikofukasawa 0:51fa46d39a3e 194 if ((status=write(AK9752_REG_ADDR_CNTL1, buf, 2)) != SUCCESS) {
tkstreet 3:2035a4a54d3f 195 return status; // Write operation failed
masahikofukasawa 0:51fa46d39a3e 196 }
tkstreet 3:2035a4a54d3f 197 return SUCCESS; // Write operation succeeded
masahikofukasawa 0:51fa46d39a3e 198 }
masahikofukasawa 0:51fa46d39a3e 199
masahikofukasawa 0:51fa46d39a3e 200 AK9752::Status AK9752::reset() {
masahikofukasawa 0:51fa46d39a3e 201 Status status;
tkstreet 3:2035a4a54d3f 202
tkstreet 3:2035a4a54d3f 203 char val = VAL_SOFTWARE_RESET; // char instance required for write()
masahikofukasawa 0:51fa46d39a3e 204 if ((status=write(AK9752_REG_ADDR_CNTL3, &val, LEN_ONE_BYTE)) != SUCCESS) {
tkstreet 3:2035a4a54d3f 205 return status; // Write operation failed
masahikofukasawa 0:51fa46d39a3e 206 }
tkstreet 3:2035a4a54d3f 207 return SUCCESS; // Write operation succeeded
masahikofukasawa 0:51fa46d39a3e 208 }
masahikofukasawa 0:51fa46d39a3e 209
tkstreet 3:2035a4a54d3f 210 AK9752::Status AK9752::getSensorData(SensorData *data) {
tkstreet 3:2035a4a54d3f 211
masahikofukasawa 0:51fa46d39a3e 212 char buf[LEN_BUF_IR_DATA];
masahikofukasawa 0:51fa46d39a3e 213
tkstreet 3:2035a4a54d3f 214 // Read 7 bytes starting at ST1: reads ST1, INTCAUSE, data and ST2 at once
tkstreet 3:2035a4a54d3f 215 if ((read(AK9752_REG_ADDR_ST1, buf, LEN_BUF_IR_DATA)) != SUCCESS) {
tkstreet 3:2035a4a54d3f 216 return ERROR; // Read operation failed
masahikofukasawa 0:51fa46d39a3e 217 }
masahikofukasawa 0:51fa46d39a3e 218
tkstreet 3:2035a4a54d3f 219 // Check Data ReaDY
tkstreet 3:2035a4a54d3f 220 if( (buf[0] & ST1_STATUS_FLAG_DRDY) == 0 ){ // DRDY=0, data not ready
masahikofukasawa 0:51fa46d39a3e 221 return ERROR;
masahikofukasawa 0:51fa46d39a3e 222 }
masahikofukasawa 0:51fa46d39a3e 223
tkstreet 3:2035a4a54d3f 224 // Read IR threshold, temp threshold and data ready flags
masahikofukasawa 0:51fa46d39a3e 225 data->intStatus.irh = ((buf[1] & INT_STATUS_FLAG_IRH) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 226 data->intStatus.irl = ((buf[1] & INT_STATUS_FLAG_IRL) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 227 data->intStatus.tmph = ((buf[1] & INT_STATUS_FLAG_TMPH) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 228 data->intStatus.tmpl = ((buf[1] & INT_STATUS_FLAG_TMPL) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 229 data->intStatus.dr = ((buf[1] & INT_STATUS_FLAG_DR) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 230
tkstreet 3:2035a4a54d3f 231 // Extract IR sensor data
tkstreet 3:2035a4a54d3f 232 data->ir = (int16_t)((buf[3] << 8) | buf[2]); // Combine IRL & IRH
masahikofukasawa 0:51fa46d39a3e 233
tkstreet 3:2035a4a54d3f 234 // Extract Temperature sensor data
tkstreet 3:2035a4a54d3f 235 data->temperature = (int16_t)((buf[5] << 8) | buf[4]); // Combine TMPL & TMPH
masahikofukasawa 0:51fa46d39a3e 236
tkstreet 3:2035a4a54d3f 237 // Extract Data Overrun status
masahikofukasawa 0:51fa46d39a3e 238 data->dor = ((buf[1] & ST2_STATUS_FLAG_DOR) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 239
masahikofukasawa 0:51fa46d39a3e 240 return SUCCESS;
masahikofukasawa 0:51fa46d39a3e 241 }