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:
Wed Nov 02 17:41:22 2016 +0000
Revision:
6:254b7e5820e7
Parent:
5:a979c36482fc
Parent:
3:2035a4a54d3f
Child:
8:40dcaa912883
Child:
9:45495ac12b89
Merged comments that had been omitted during previous merge.

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