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:
Mon Nov 14 19:11:45 2016 +0000
Revision:
10:1cb542a62421
Parent:
9:45495ac12b89
Child:
11:e4a64ed3ed58
Removed debugging lines.  Removed prototype version accommodation.

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