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