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:
masahikofukasawa
Date:
Fri Jul 08 22:27:55 2016 +0000
Revision:
0:51fa46d39a3e
Child:
1:0b59cfeee8ee
Child:
3:2035a4a54d3f
New for RevD7. Release to Japan.

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
masahikofukasawa 0:51fa46d39a3e 4 #define CONV16I(high,low) ((int16_t)(((high) << 8) | (low)))
masahikofukasawa 0:51fa46d39a3e 5
masahikofukasawa 0:51fa46d39a3e 6 #define LEN_ONE_BYTE 1 /**<! Data length of 1 byte data. */
masahikofukasawa 0:51fa46d39a3e 7 #define LEN_BUF_THRESHOLD 8 /**<! Data length of Threshold settings. From THIRH to THTMPL */
masahikofukasawa 0:51fa46d39a3e 8 #define LEN_BUF_IR_DATA 7 /**<! Data length of IR sensor data. From ST1 to ST2. */
masahikofukasawa 0:51fa46d39a3e 9 #define VAL_SOFTWARE_RESET 0x01
masahikofukasawa 0:51fa46d39a3e 10 #define ST1_STATUS_FLAG_DRDY 0x01
masahikofukasawa 0:51fa46d39a3e 11 #define INT_STATUS_FLAG_DR 0x01
masahikofukasawa 0:51fa46d39a3e 12 #define INT_STATUS_FLAG_TMPL 0x02
masahikofukasawa 0:51fa46d39a3e 13 #define INT_STATUS_FLAG_TMPH 0x04
masahikofukasawa 0:51fa46d39a3e 14 #define INT_STATUS_FLAG_IRL 0x08
masahikofukasawa 0:51fa46d39a3e 15 #define INT_STATUS_FLAG_IRH 0x10
masahikofukasawa 0:51fa46d39a3e 16 #define INT_STATUS_MASK 0x1F
masahikofukasawa 0:51fa46d39a3e 17 #define ST2_STATUS_FLAG_DOR 0x01
masahikofukasawa 0:51fa46d39a3e 18
masahikofukasawa 0:51fa46d39a3e 19
masahikofukasawa 0:51fa46d39a3e 20 AK9752::AK9752() {
masahikofukasawa 0:51fa46d39a3e 21 }
masahikofukasawa 0:51fa46d39a3e 22
masahikofukasawa 0:51fa46d39a3e 23 void AK9752::init(I2C *conn, SlaveAddress addr) {
masahikofukasawa 0:51fa46d39a3e 24 slaveAddress = addr;
masahikofukasawa 0:51fa46d39a3e 25 connection = conn;
masahikofukasawa 0:51fa46d39a3e 26 }
masahikofukasawa 0:51fa46d39a3e 27
masahikofukasawa 0:51fa46d39a3e 28 AK9752::Status AK9752::checkConnection() {
masahikofukasawa 0:51fa46d39a3e 29 AK9752::Status status = AK9752::SUCCESS;
masahikofukasawa 0:51fa46d39a3e 30
masahikofukasawa 0:51fa46d39a3e 31 // Gets the WIA register value.
masahikofukasawa 0:51fa46d39a3e 32 char buf[2];
masahikofukasawa 0:51fa46d39a3e 33 if ((status=AK9752::read(AK9752_REG_ADDR_WIA1, buf, 2)) != SUCCESS) {
masahikofukasawa 0:51fa46d39a3e 34 return status;
masahikofukasawa 0:51fa46d39a3e 35 }
masahikofukasawa 0:51fa46d39a3e 36
masahikofukasawa 0:51fa46d39a3e 37 // Checks the obtained value equals to the supposed value.
masahikofukasawa 0:51fa46d39a3e 38 if ( (buf[0] != AK9752_REG_VALUE_WIA1) || (buf[1] != AK9752_REG_VALUE_WIA2) ) {
masahikofukasawa 0:51fa46d39a3e 39 return AK9752::ERROR;
masahikofukasawa 0:51fa46d39a3e 40 }
masahikofukasawa 0:51fa46d39a3e 41
masahikofukasawa 0:51fa46d39a3e 42 return status;
masahikofukasawa 0:51fa46d39a3e 43 }
masahikofukasawa 0:51fa46d39a3e 44
masahikofukasawa 0:51fa46d39a3e 45 AK9752::Status AK9752::read(char registerAddress, char *buf, int length) {
masahikofukasawa 0:51fa46d39a3e 46 // Writes a start address
masahikofukasawa 0:51fa46d39a3e 47 if (connection->write((slaveAddress << 1), &registerAddress, LEN_ONE_BYTE) != 0) {
masahikofukasawa 0:51fa46d39a3e 48 // I2C write failed.
masahikofukasawa 0:51fa46d39a3e 49 return AK9752::ERROR_I2C_WRITE;
masahikofukasawa 0:51fa46d39a3e 50 }
masahikofukasawa 0:51fa46d39a3e 51
masahikofukasawa 0:51fa46d39a3e 52 // Reads register data
masahikofukasawa 0:51fa46d39a3e 53 if (connection->read((slaveAddress << 1), buf, length) != 0) {
masahikofukasawa 0:51fa46d39a3e 54 // I2C read failed.
masahikofukasawa 0:51fa46d39a3e 55 return AK9752::ERROR_I2C_READ;
masahikofukasawa 0:51fa46d39a3e 56 }
masahikofukasawa 0:51fa46d39a3e 57
masahikofukasawa 0:51fa46d39a3e 58 return AK9752::SUCCESS;
masahikofukasawa 0:51fa46d39a3e 59 }
masahikofukasawa 0:51fa46d39a3e 60
masahikofukasawa 0:51fa46d39a3e 61 AK9752::Status AK9752::write(char registerAddress, const char *buf, int length) {
masahikofukasawa 0:51fa46d39a3e 62 int bufLength = length + 1;
masahikofukasawa 0:51fa46d39a3e 63 char data[bufLength];
masahikofukasawa 0:51fa46d39a3e 64
masahikofukasawa 0:51fa46d39a3e 65 // Creates data to be sent.
masahikofukasawa 0:51fa46d39a3e 66 data[0] = registerAddress;
masahikofukasawa 0:51fa46d39a3e 67 for (int i=0; i < length; i++) {
masahikofukasawa 0:51fa46d39a3e 68 data[1+i] = buf[i];
masahikofukasawa 0:51fa46d39a3e 69 }
masahikofukasawa 0:51fa46d39a3e 70
masahikofukasawa 0:51fa46d39a3e 71 // Writes a start address.
masahikofukasawa 0:51fa46d39a3e 72 if (connection->write((slaveAddress << 1), data, bufLength) != 0) {
masahikofukasawa 0:51fa46d39a3e 73 // I2C write failed.
masahikofukasawa 0:51fa46d39a3e 74 return AK9752::ERROR_I2C_WRITE;
masahikofukasawa 0:51fa46d39a3e 75 }
masahikofukasawa 0:51fa46d39a3e 76
masahikofukasawa 0:51fa46d39a3e 77 return AK9752::SUCCESS;
masahikofukasawa 0:51fa46d39a3e 78 }
masahikofukasawa 0:51fa46d39a3e 79
masahikofukasawa 0:51fa46d39a3e 80 AK9752::Status AK9752::getInterruptEnable(InterruptStatus *intStatus){
masahikofukasawa 0:51fa46d39a3e 81 Status status;
masahikofukasawa 0:51fa46d39a3e 82
masahikofukasawa 0:51fa46d39a3e 83 char buf = 0;
masahikofukasawa 0:51fa46d39a3e 84 if((status=read(AK9752_REG_ADDR_INTEN, &buf, LEN_ONE_BYTE)) != SUCCESS) {
masahikofukasawa 0:51fa46d39a3e 85 return status;
masahikofukasawa 0:51fa46d39a3e 86 }
masahikofukasawa 0:51fa46d39a3e 87
masahikofukasawa 0:51fa46d39a3e 88 intStatus->irh = ((buf & INT_STATUS_FLAG_IRH) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 89 intStatus->irl = ((buf & INT_STATUS_FLAG_IRL) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 90 intStatus->tmph = ((buf & INT_STATUS_FLAG_TMPH) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 91 intStatus->tmpl = ((buf & INT_STATUS_FLAG_TMPL) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 92 intStatus->dr = ((buf & INT_STATUS_FLAG_DR) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 93
masahikofukasawa 0:51fa46d39a3e 94 return SUCCESS;
masahikofukasawa 0:51fa46d39a3e 95 }
masahikofukasawa 0:51fa46d39a3e 96
masahikofukasawa 0:51fa46d39a3e 97 AK9752::Status AK9752::setInterruptEnable(const AK9752::InterruptStatus *intStatus) {
masahikofukasawa 0:51fa46d39a3e 98 char buf = 0;
masahikofukasawa 0:51fa46d39a3e 99
masahikofukasawa 0:51fa46d39a3e 100 buf += intStatus->irh ? INT_STATUS_FLAG_IRH : 0;
masahikofukasawa 0:51fa46d39a3e 101 buf += intStatus->irl ? INT_STATUS_FLAG_IRL : 0;
masahikofukasawa 0:51fa46d39a3e 102 buf += intStatus->tmph ? INT_STATUS_FLAG_TMPH : 0;
masahikofukasawa 0:51fa46d39a3e 103 buf += intStatus->tmpl ? INT_STATUS_FLAG_TMPL : 0;
masahikofukasawa 0:51fa46d39a3e 104 buf += intStatus->dr ? INT_STATUS_FLAG_DR : 0;
masahikofukasawa 0:51fa46d39a3e 105
masahikofukasawa 0:51fa46d39a3e 106 Status status;
masahikofukasawa 0:51fa46d39a3e 107 if ((status=write(AK9752_REG_ADDR_INTEN, &buf, LEN_ONE_BYTE)) != SUCCESS) {
masahikofukasawa 0:51fa46d39a3e 108 return status;
masahikofukasawa 0:51fa46d39a3e 109 }
masahikofukasawa 0:51fa46d39a3e 110
masahikofukasawa 0:51fa46d39a3e 111 char readback = 0;
masahikofukasawa 0:51fa46d39a3e 112 if ((status=read(AK9752_REG_ADDR_INTEN, &readback, LEN_ONE_BYTE)) != SUCCESS) {
masahikofukasawa 0:51fa46d39a3e 113 return status;
masahikofukasawa 0:51fa46d39a3e 114 }
masahikofukasawa 0:51fa46d39a3e 115 if ((readback & INT_STATUS_MASK) != buf) {
masahikofukasawa 0:51fa46d39a3e 116 return ERROR;
masahikofukasawa 0:51fa46d39a3e 117 }
masahikofukasawa 0:51fa46d39a3e 118
masahikofukasawa 0:51fa46d39a3e 119 return SUCCESS;
masahikofukasawa 0:51fa46d39a3e 120 }
masahikofukasawa 0:51fa46d39a3e 121
masahikofukasawa 0:51fa46d39a3e 122 AK9752::Status AK9752::getData(char *buf) {
masahikofukasawa 0:51fa46d39a3e 123 Status status;
masahikofukasawa 0:51fa46d39a3e 124 if ((status=read(AK9752_REG_ADDR_ST1, buf, LEN_BUF_IR_DATA)) != SUCCESS) {
masahikofukasawa 0:51fa46d39a3e 125 return status;
masahikofukasawa 0:51fa46d39a3e 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
masahikofukasawa 0:51fa46d39a3e 134 buf[0] = (char)(((uint16_t)th->thirh & 0x00FF)); // THIRHL
masahikofukasawa 0:51fa46d39a3e 135 buf[1] = (char)(((uint16_t)th->thirh & 0xFF00) >> 8); // THIRHH
masahikofukasawa 0:51fa46d39a3e 136 buf[2] = (char)(((uint16_t)th->thirl & 0x00FF)); // THIRLL
masahikofukasawa 0:51fa46d39a3e 137 buf[3] = (char)(((uint16_t)th->thirl & 0xFF00) >> 8); // THIRLH
masahikofukasawa 0:51fa46d39a3e 138 buf[4] = (char)(((uint16_t)th->thtmph & 0x00FF)); // THTMPHL
masahikofukasawa 0:51fa46d39a3e 139 buf[5] = (char)(((uint16_t)th->thtmph & 0xFF00) >> 8); // THTMPHH
masahikofukasawa 0:51fa46d39a3e 140 buf[6] = (char)(((uint16_t)th->thtmpl & 0x00FF)); // THTMPLL
masahikofukasawa 0:51fa46d39a3e 141 buf[7] = (char)(((uint16_t)th->thtmpl & 0xFF00) >> 8); // THTMPLH
masahikofukasawa 0:51fa46d39a3e 142
masahikofukasawa 0:51fa46d39a3e 143 if ((status=write(AK9752_REG_ADDR_THIRHL, buf, LEN_BUF_THRESHOLD)) != SUCCESS) {
masahikofukasawa 0:51fa46d39a3e 144 return status;
masahikofukasawa 0:51fa46d39a3e 145 }
masahikofukasawa 0:51fa46d39a3e 146
masahikofukasawa 0:51fa46d39a3e 147 return SUCCESS;
masahikofukasawa 0:51fa46d39a3e 148 }
masahikofukasawa 0:51fa46d39a3e 149
masahikofukasawa 0:51fa46d39a3e 150 AK9752::Status AK9752::getThreshold(Threshold *th) {
masahikofukasawa 0:51fa46d39a3e 151 Status status;
masahikofukasawa 0:51fa46d39a3e 152 char buf[LEN_BUF_THRESHOLD];
masahikofukasawa 0:51fa46d39a3e 153
masahikofukasawa 0:51fa46d39a3e 154 if ((status=read(AK9752_REG_ADDR_THIRHL, buf, LEN_BUF_THRESHOLD)) != SUCCESS) {
masahikofukasawa 0:51fa46d39a3e 155 return status;
masahikofukasawa 0:51fa46d39a3e 156 }
masahikofukasawa 0:51fa46d39a3e 157
masahikofukasawa 0:51fa46d39a3e 158 th->thirh = CONV16I(buf[1], buf[0]);
masahikofukasawa 0:51fa46d39a3e 159 th->thirl = CONV16I(buf[3], buf[2]);
masahikofukasawa 0:51fa46d39a3e 160 th->thtmph = CONV16I(buf[5], buf[4]);
masahikofukasawa 0:51fa46d39a3e 161 th->thtmpl = CONV16I(buf[7], buf[6]);
masahikofukasawa 0:51fa46d39a3e 162
masahikofukasawa 0:51fa46d39a3e 163 return SUCCESS;
masahikofukasawa 0:51fa46d39a3e 164 }
masahikofukasawa 0:51fa46d39a3e 165
masahikofukasawa 0:51fa46d39a3e 166 AK9752::Status AK9752::getOperationMode(OperationMode *mode, FcTmp *fc_tmp, FcIr *fc_ir){
masahikofukasawa 0:51fa46d39a3e 167 Status status;
masahikofukasawa 0:51fa46d39a3e 168
masahikofukasawa 0:51fa46d39a3e 169 char buf[2];
masahikofukasawa 0:51fa46d39a3e 170 if ((status=read(AK9752_REG_ADDR_CNTL1, buf, 2)) != SUCCESS) {
masahikofukasawa 0:51fa46d39a3e 171 return status;
masahikofukasawa 0:51fa46d39a3e 172 }
masahikofukasawa 0:51fa46d39a3e 173 *fc_tmp = AK9752::FcTmp((buf[0] & 0x1C)>>2);
masahikofukasawa 0:51fa46d39a3e 174 *fc_ir = AK9752::FcIr(buf[0] & 0x03);
masahikofukasawa 0:51fa46d39a3e 175 *mode = AK9752::OperationMode(buf[1] & 0x03);
masahikofukasawa 0:51fa46d39a3e 176
masahikofukasawa 0:51fa46d39a3e 177 return SUCCESS;
masahikofukasawa 0:51fa46d39a3e 178 }
masahikofukasawa 0:51fa46d39a3e 179
masahikofukasawa 0:51fa46d39a3e 180 AK9752::Status AK9752::setOperationMode(OperationMode mode, FcTmp fc_tmp, FcIr fc_ir){
masahikofukasawa 0:51fa46d39a3e 181 Status status;
masahikofukasawa 0:51fa46d39a3e 182
masahikofukasawa 0:51fa46d39a3e 183 char buf[2];
masahikofukasawa 0:51fa46d39a3e 184 buf[0] = (fc_tmp<<2 | fc_ir&0x03);
masahikofukasawa 0:51fa46d39a3e 185 buf[1] = mode;
masahikofukasawa 0:51fa46d39a3e 186 if ((status=write(AK9752_REG_ADDR_CNTL1, buf, 2)) != SUCCESS) {
masahikofukasawa 0:51fa46d39a3e 187 return status;
masahikofukasawa 0:51fa46d39a3e 188 }
masahikofukasawa 0:51fa46d39a3e 189 return SUCCESS;
masahikofukasawa 0:51fa46d39a3e 190 }
masahikofukasawa 0:51fa46d39a3e 191
masahikofukasawa 0:51fa46d39a3e 192 AK9752::Status AK9752::reset() {
masahikofukasawa 0:51fa46d39a3e 193 Status status;
masahikofukasawa 0:51fa46d39a3e 194 char val = VAL_SOFTWARE_RESET;
masahikofukasawa 0:51fa46d39a3e 195 if ((status=write(AK9752_REG_ADDR_CNTL3, &val, LEN_ONE_BYTE)) != SUCCESS) {
masahikofukasawa 0:51fa46d39a3e 196 return status;
masahikofukasawa 0:51fa46d39a3e 197 }
masahikofukasawa 0:51fa46d39a3e 198 return SUCCESS;
masahikofukasawa 0:51fa46d39a3e 199 }
masahikofukasawa 0:51fa46d39a3e 200
masahikofukasawa 0:51fa46d39a3e 201 AK9752::Status AK9752::getSensorData(AK9752::SensorData *data) {
masahikofukasawa 0:51fa46d39a3e 202 Status status;
masahikofukasawa 0:51fa46d39a3e 203 char buf[LEN_BUF_IR_DATA];
masahikofukasawa 0:51fa46d39a3e 204
masahikofukasawa 0:51fa46d39a3e 205 if ((status=getData(buf)) != SUCCESS) {
masahikofukasawa 0:51fa46d39a3e 206 return status;
masahikofukasawa 0:51fa46d39a3e 207 }
masahikofukasawa 0:51fa46d39a3e 208
masahikofukasawa 0:51fa46d39a3e 209 // check DRDY
masahikofukasawa 0:51fa46d39a3e 210 if( (buf[0] & ST1_STATUS_FLAG_DRDY) == 0 ){
masahikofukasawa 0:51fa46d39a3e 211 // DRDY=0, data not ready
masahikofukasawa 0:51fa46d39a3e 212 return ERROR;
masahikofukasawa 0:51fa46d39a3e 213 }
masahikofukasawa 0:51fa46d39a3e 214
masahikofukasawa 0:51fa46d39a3e 215 // Interrupt Status
masahikofukasawa 0:51fa46d39a3e 216 data->intStatus.irh = ((buf[1] & INT_STATUS_FLAG_IRH) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 217 data->intStatus.irl = ((buf[1] & INT_STATUS_FLAG_IRL) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 218 data->intStatus.tmph = ((buf[1] & INT_STATUS_FLAG_TMPH) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 219 data->intStatus.tmpl = ((buf[1] & INT_STATUS_FLAG_TMPL) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 220 data->intStatus.dr = ((buf[1] & INT_STATUS_FLAG_DR) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 221
masahikofukasawa 0:51fa46d39a3e 222 // IR sensor data
masahikofukasawa 0:51fa46d39a3e 223 data->ir = (int16_t)((buf[3] << 8) | buf[2]);
masahikofukasawa 0:51fa46d39a3e 224
masahikofukasawa 0:51fa46d39a3e 225 // Temperature sensor data
masahikofukasawa 0:51fa46d39a3e 226 data->temperature = (int16_t)((buf[5] << 8) | buf[4]);
masahikofukasawa 0:51fa46d39a3e 227
masahikofukasawa 0:51fa46d39a3e 228 // DOR Status
masahikofukasawa 0:51fa46d39a3e 229 data->dor = ((buf[1] & ST2_STATUS_FLAG_DOR) > 0) ? true : false;
masahikofukasawa 0:51fa46d39a3e 230
masahikofukasawa 0:51fa46d39a3e 231 return SUCCESS;
masahikofukasawa 0:51fa46d39a3e 232 }