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 May 01 21:26:36 2018 +0000
Revision:
14:c7692d514168
Parent:
11:e4a64ed3ed58
Trivial changes regarding readability and data protection.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
masahikofukasawa 0:51fa46d39a3e 1 #ifndef __AK9752_H__
masahikofukasawa 0:51fa46d39a3e 2 #define __AK9752_H__
masahikofukasawa 0:51fa46d39a3e 3
masahikofukasawa 0:51fa46d39a3e 4 #include "mbed.h"
masahikofukasawa 0:51fa46d39a3e 5
tkstreet 3:2035a4a54d3f 6 /**
tkstreet 6:254b7e5820e7 7 * Device driver for AK9752 by AKM Semiconductors, Inc.
tkstreet 3:2035a4a54d3f 8 * @note AK9752 is an IR sensor with I2C interface.
tkstreet 3:2035a4a54d3f 9 * Example:
tkstreet 3:2035a4a54d3f 10 * @code
tkstreet 3:2035a4a54d3f 11 * #include "mbed.h"
tkstreet 3:2035a4a54d3f 12 * #include "ak9752_reg.h"
tkstreet 3:2035a4a54d3f 13 * #include "ak9752.h"
tkstreet 3:2035a4a54d3f 14 *
masahikofukasawa 2:5b437caef51f 15 * #define I2C_SPEED_100KHZ 100000
masahikofukasawa 2:5b437caef51f 16 * #define I2C_SPEED_400KHZ 400000
masahikofukasawa 2:5b437caef51f 17 *
masahikofukasawa 2:5b437caef51f 18 * int main() {
masahikofukasawa 2:5b437caef51f 19 * // Creates an instance of I2C
masahikofukasawa 2:5b437caef51f 20 * I2C connection(I2C_SDA0, I2C_SCL0);
masahikofukasawa 2:5b437caef51f 21 * connection.frequency(I2C_SPEED_100KHZ);
masahikofukasawa 2:5b437caef51f 22 *
masahikofukasawa 2:5b437caef51f 23 * // TBD
masahikofukasawa 2:5b437caef51f 24 *
masahikofukasawa 2:5b437caef51f 25 * while(true) {
masahikofukasawa 2:5b437caef51f 26 * // TBD
masahikofukasawa 2:5b437caef51f 27 * }
masahikofukasawa 2:5b437caef51f 28 * }
tkstreet 3:2035a4a54d3f 29 * @endcode
tkstreet 3:2035a4a54d3f 30 */
masahikofukasawa 0:51fa46d39a3e 31 class AK9752
masahikofukasawa 0:51fa46d39a3e 32 {
masahikofukasawa 0:51fa46d39a3e 33 public:
masahikofukasawa 0:51fa46d39a3e 34 /**
tkstreet 7:fb2afd6d095c 35 * Return status.
masahikofukasawa 0:51fa46d39a3e 36 */
masahikofukasawa 0:51fa46d39a3e 37 typedef enum {
masahikofukasawa 0:51fa46d39a3e 38 SUCCESS, /**< Success */
masahikofukasawa 0:51fa46d39a3e 39 ERROR, /**< Error */
masahikofukasawa 0:51fa46d39a3e 40 ERROR_I2C_WRITE, /**< I2C write error */
masahikofukasawa 0:51fa46d39a3e 41 ERROR_I2C_READ, /**< I2C read error */
masahikofukasawa 0:51fa46d39a3e 42 ERROR_ARG_OUT_OF_BOUNDS, /**< An argument is out of bounds */
masahikofukasawa 11:e4a64ed3ed58 43 DATA_READY, /**< Data ready */
masahikofukasawa 11:e4a64ed3ed58 44 NOT_DATA_READY, /**< Data ready is not asserted. */
masahikofukasawa 0:51fa46d39a3e 45 } Status;
masahikofukasawa 0:51fa46d39a3e 46
masahikofukasawa 0:51fa46d39a3e 47 /**
tkstreet 7:fb2afd6d095c 48 * Slave address of AK9752.
masahikofukasawa 0:51fa46d39a3e 49 */
masahikofukasawa 0:51fa46d39a3e 50 typedef enum {
masahikofukasawa 0:51fa46d39a3e 51 SLAVE_ADDR_1 = 0x64, /**< CAD1=0, CAD0=0 */
masahikofukasawa 0:51fa46d39a3e 52 } SlaveAddress;
masahikofukasawa 0:51fa46d39a3e 53
masahikofukasawa 0:51fa46d39a3e 54 /**
tkstreet 7:fb2afd6d095c 55 * Data sampling operation modes.
masahikofukasawa 0:51fa46d39a3e 56 */
masahikofukasawa 0:51fa46d39a3e 57 typedef enum {
tkstreet 3:2035a4a54d3f 58 MODE_STANDBY = 0x00, /**< MODE_STANDBY: 0x00 */
tkstreet 3:2035a4a54d3f 59 MODE_CONTINUOUS = 0x01, /**< MODE_CONTINUOUS: 0x01 */
tkstreet 3:2035a4a54d3f 60 MODE_SINGLE_SHOT = 0x02, /**< MODE_SINGLE_SHOT: 0x02 */
masahikofukasawa 0:51fa46d39a3e 61 } OperationMode;
masahikofukasawa 0:51fa46d39a3e 62
masahikofukasawa 0:51fa46d39a3e 63 /**
tkstreet 7:fb2afd6d095c 64 * Cut-off frequency setting for the temperature sensor filter.
masahikofukasawa 0:51fa46d39a3e 65 */
masahikofukasawa 0:51fa46d39a3e 66 typedef enum {
masahikofukasawa 0:51fa46d39a3e 67 FCTMP_NOFILTER = 0x00, /**< No Filter */
masahikofukasawa 0:51fa46d39a3e 68 FCTMP_2P5HZ = 0x01, /**< Fc = 2.5 Hz */
masahikofukasawa 0:51fa46d39a3e 69 FCTMP_0P9HZ = 0x02, /**< Fc = 0.9 Hz */
masahikofukasawa 0:51fa46d39a3e 70 FCTMP_0P45HZ = 0x03, /**< Fc = 0.45 Hz */
masahikofukasawa 0:51fa46d39a3e 71 FCTMP_0P22HZ = 0x04, /**< Fc = 0.22 Hz */
masahikofukasawa 0:51fa46d39a3e 72 } FcTmp;
masahikofukasawa 0:51fa46d39a3e 73
masahikofukasawa 0:51fa46d39a3e 74 /**
tkstreet 7:fb2afd6d095c 75 * Cut-off frequency setting for the IR sensor filter.
masahikofukasawa 0:51fa46d39a3e 76 */
masahikofukasawa 0:51fa46d39a3e 77 typedef enum {
masahikofukasawa 0:51fa46d39a3e 78 FCIR_NOFILTER = 0x00, /**< No Filter */
masahikofukasawa 0:51fa46d39a3e 79 FCIR_2P5HZ = 0x01, /**< Fc = 2.5 Hz */
masahikofukasawa 0:51fa46d39a3e 80 FCIR_0P9HZ = 0x02, /**< Fc = 0.9 Hz */
masahikofukasawa 0:51fa46d39a3e 81 FCIR_0P45HZ = 0x03, /**< Fc = 0.45 Hz */
masahikofukasawa 0:51fa46d39a3e 82 } FcIr;
masahikofukasawa 0:51fa46d39a3e 83
masahikofukasawa 0:51fa46d39a3e 84 /**
tkstreet 7:fb2afd6d095c 85 * Status of the IR and temperature sensor threshold interrupts.
masahikofukasawa 0:51fa46d39a3e 86 */
masahikofukasawa 0:51fa46d39a3e 87 typedef struct {
tkstreet 3:2035a4a54d3f 88 bool irh; /**< IR Sensor crossed high threshold. */
tkstreet 3:2035a4a54d3f 89 bool irl; /**< IR Sensor crossed low threshold. */
tkstreet 3:2035a4a54d3f 90 bool tmph; /**< Tempearture Sensor crossed high threshold. */
tkstreet 3:2035a4a54d3f 91 bool tmpl; /**< Temperature Sensor crossed low threshold. */
tkstreet 3:2035a4a54d3f 92 bool dr; /**< Data Ready. */
masahikofukasawa 0:51fa46d39a3e 93 } InterruptStatus;
masahikofukasawa 0:51fa46d39a3e 94
masahikofukasawa 0:51fa46d39a3e 95 /**
tkstreet 7:fb2afd6d095c 96 * Threshold level interrupt settings for the IR and temperature sensor.
masahikofukasawa 0:51fa46d39a3e 97 */
masahikofukasawa 0:51fa46d39a3e 98 typedef struct {
tkstreet 3:2035a4a54d3f 99 int16_t thirh; /**< High Threshold level for IR sensor. */
tkstreet 3:2035a4a54d3f 100 int16_t thirl; /**< Low Threshold level of IR sensor. */
masahikofukasawa 0:51fa46d39a3e 101 int16_t thtmph; /**< High Threshold level of temperature sensor. */
masahikofukasawa 0:51fa46d39a3e 102 int16_t thtmpl; /**< Low Threshold level of temperature sensor. */
masahikofukasawa 0:51fa46d39a3e 103 } Threshold;
masahikofukasawa 0:51fa46d39a3e 104
masahikofukasawa 0:51fa46d39a3e 105 /**
tkstreet 7:fb2afd6d095c 106 * Sensor data: IR, temperature, and status of their interrupts.
masahikofukasawa 0:51fa46d39a3e 107 */
masahikofukasawa 0:51fa46d39a3e 108 typedef struct {
tkstreet 3:2035a4a54d3f 109 InterruptStatus intStatus; /**< Interrupt status object */
tkstreet 3:2035a4a54d3f 110 int16_t ir; /**< IR Sensor Data (pA) */
tkstreet 3:2035a4a54d3f 111 int16_t temperature; /**< Temperature Data (deg C) */
tkstreet 3:2035a4a54d3f 112 bool dor; /**< Data Overrun: 1:data skip, 0:after ST2 read*/
masahikofukasawa 0:51fa46d39a3e 113 } SensorData;
tkstreet 3:2035a4a54d3f 114
masahikofukasawa 0:51fa46d39a3e 115 /**
masahikofukasawa 0:51fa46d39a3e 116 * Constructor.
masahikofukasawa 0:51fa46d39a3e 117 *
tkstreet 7:fb2afd6d095c 118 * @param conn Instance of I2C connection
tkstreet 3:2035a4a54d3f 119 * @param addr Slave address of the device
masahikofukasawa 0:51fa46d39a3e 120 */
masahikofukasawa 0:51fa46d39a3e 121 AK9752();
masahikofukasawa 0:51fa46d39a3e 122
tkstreet 3:2035a4a54d3f 123 /**
tkstreet 3:2035a4a54d3f 124 * Initialize AK9752 connection.
tkstreet 3:2035a4a54d3f 125 *
tkstreet 7:fb2afd6d095c 126 * @param conn Instance of I2C connection
tkstreet 7:fb2afd6d095c 127 * @param addr Slave address of I2C device
tkstreet 3:2035a4a54d3f 128 */
masahikofukasawa 0:51fa46d39a3e 129 void init(I2C *conn, SlaveAddress addr);
masahikofukasawa 0:51fa46d39a3e 130
masahikofukasawa 0:51fa46d39a3e 131 /**
masahikofukasawa 0:51fa46d39a3e 132 * Checks AK9752 connection.
tkstreet 3:2035a4a54d3f 133 *
tkstreet 7:fb2afd6d095c 134 * @return SUCCESS if connection is established, error otherwise.
masahikofukasawa 0:51fa46d39a3e 135 */
masahikofukasawa 0:51fa46d39a3e 136 Status checkConnection();
masahikofukasawa 0:51fa46d39a3e 137
masahikofukasawa 0:51fa46d39a3e 138 /**
tkstreet 7:fb2afd6d095c 139 * Retrieve the interrupt enable/disable status.
tkstreet 3:2035a4a54d3f 140 *
tkstreet 3:2035a4a54d3f 141 * @param intStatus Interrupt enable status
tkstreet 3:2035a4a54d3f 142 *
tkstreet 7:fb2afd6d095c 143 * @return SUCCESS if the interrupt status is obtained successfully, error otherwise.
masahikofukasawa 0:51fa46d39a3e 144 */
masahikofukasawa 0:51fa46d39a3e 145 Status getInterruptEnable(InterruptStatus *intStatus);
masahikofukasawa 0:51fa46d39a3e 146
masahikofukasawa 0:51fa46d39a3e 147 /**
tkstreet 7:fb2afd6d095c 148 * Enable or disable interrupts.
tkstreet 3:2035a4a54d3f 149 *
masahikofukasawa 0:51fa46d39a3e 150 * @param intStatus interrupt status
tkstreet 3:2035a4a54d3f 151 *
tkstreet 7:fb2afd6d095c 152 * @return SUCCESS if the interrupt status is set successfully, error otherwise.
masahikofukasawa 0:51fa46d39a3e 153 */
masahikofukasawa 0:51fa46d39a3e 154 Status setInterruptEnable(const InterruptStatus *intStatus);
masahikofukasawa 0:51fa46d39a3e 155
masahikofukasawa 0:51fa46d39a3e 156 /**
tkstreet 3:2035a4a54d3f 157 * Gets sensor operation mode: Standby, Continuous or Single-Shot.
tkstreet 3:2035a4a54d3f 158 *
masahikofukasawa 0:51fa46d39a3e 159 * @param mode Pointer to the operation mode.
masahikofukasawa 0:51fa46d39a3e 160 * @param fc_tmp Pointer to the tempearture sensor filter setting.
masahikofukasawa 0:51fa46d39a3e 161 * @param fc_ir Pointer to the IR sensor filter setting.
tkstreet 3:2035a4a54d3f 162 *
tkstreet 7:fb2afd6d095c 163 * @return SUCCESS if operation mode is set successfully, error otherwise.
masahikofukasawa 0:51fa46d39a3e 164 */
masahikofukasawa 0:51fa46d39a3e 165 Status getOperationMode(OperationMode *mode, FcTmp *fc_tmp, FcIr *fc_ir);
masahikofukasawa 0:51fa46d39a3e 166
masahikofukasawa 0:51fa46d39a3e 167 /**
tkstreet 3:2035a4a54d3f 168 * Sets sensor Operation mode: Standby, Continuous or Single-Shot.
tkstreet 3:2035a4a54d3f 169 *
tkstreet 3:2035a4a54d3f 170 * @param mode Operation mode to be set.
tkstreet 3:2035a4a54d3f 171 * @param fc_tmp Filter cut-off frequency setting for temperature sensor.
tkstreet 3:2035a4a54d3f 172 * @param fc_ir Filter cut-off frequency setting for IR sensor.
tkstreet 3:2035a4a54d3f 173 *
tkstreet 7:fb2afd6d095c 174 * @return SUCCESS if operation mode is set successfully, error otherwise.
masahikofukasawa 0:51fa46d39a3e 175 */
masahikofukasawa 0:51fa46d39a3e 176 Status setOperationMode(OperationMode mode, FcTmp fc_tmp = FCTMP_NOFILTER, FcIr fc_ir = FCIR_NOFILTER);
masahikofukasawa 0:51fa46d39a3e 177
masahikofukasawa 0:51fa46d39a3e 178 /**
tkstreet 3:2035a4a54d3f 179 * Sets threshold of IR/temperature sensor.
tkstreet 3:2035a4a54d3f 180 *
tkstreet 7:fb2afd6d095c 181 * @param th Variable of Threshold struct containing thresholds to be set.
tkstreet 3:2035a4a54d3f 182 *
tkstreet 7:fb2afd6d095c 183 * @return SUCCESS if thresholds are set successfully, error otherwise.
masahikofukasawa 0:51fa46d39a3e 184 */
masahikofukasawa 0:51fa46d39a3e 185 Status setThreshold(const Threshold *th);
masahikofukasawa 0:51fa46d39a3e 186
masahikofukasawa 0:51fa46d39a3e 187 /**
tkstreet 7:fb2afd6d095c 188 * Gets thresholds of IR/temperature sensors.
tkstreet 3:2035a4a54d3f 189 *
tkstreet 7:fb2afd6d095c 190 * @param th Instance of Threshold struct to store thresholds.
tkstreet 3:2035a4a54d3f 191 *
tkstreet 7:fb2afd6d095c 192 * @return SUCCESS if threshold is read successfully, error otherwise.
masahikofukasawa 0:51fa46d39a3e 193 */
masahikofukasawa 0:51fa46d39a3e 194 Status getThreshold(Threshold *th);
masahikofukasawa 0:51fa46d39a3e 195
masahikofukasawa 0:51fa46d39a3e 196 /**
tkstreet 3:2035a4a54d3f 197 * Resets the AK9752.
tkstreet 3:2035a4a54d3f 198 *
masahikofukasawa 0:51fa46d39a3e 199 * @return SUCCESS if the device is reset successfully, otherwise returns other value.
masahikofukasawa 0:51fa46d39a3e 200 */
masahikofukasawa 0:51fa46d39a3e 201 Status reset();
masahikofukasawa 0:51fa46d39a3e 202
masahikofukasawa 0:51fa46d39a3e 203 /**
tkstreet 3:2035a4a54d3f 204 * Retrieves all sensor data: IR & Temperature data, and ST1, ST2, & INTCAUSE
tkstreet 3:2035a4a54d3f 205 * registers.
tkstreet 3:2035a4a54d3f 206 *
masahikofukasawa 0:51fa46d39a3e 207 * @param data Pointer to the SensorData structure object to store the read data.
tkstreet 3:2035a4a54d3f 208 *
masahikofukasawa 0:51fa46d39a3e 209 * @return SUCCESS if data is obtained successfully, otherwise returns other value.
masahikofukasawa 0:51fa46d39a3e 210 */
masahikofukasawa 0:51fa46d39a3e 211 Status getSensorData(SensorData *data);
masahikofukasawa 0:51fa46d39a3e 212
masahikofukasawa 0:51fa46d39a3e 213 /**
masahikofukasawa 11:e4a64ed3ed58 214 * Check if data is ready, i.e. measurement is finished.
masahikofukasawa 11:e4a64ed3ed58 215 *
masahikofukasawa 11:e4a64ed3ed58 216 * @return Returns DATA_READY if data is ready or NOT_DATA_READY if data is not ready. If error happens, returns another code.
masahikofukasawa 11:e4a64ed3ed58 217 */
masahikofukasawa 11:e4a64ed3ed58 218 Status isDataReady();
masahikofukasawa 11:e4a64ed3ed58 219
masahikofukasawa 11:e4a64ed3ed58 220 /**
tkstreet 3:2035a4a54d3f 221 * Reads indicated AK9752 register(s).
tkstreet 3:2035a4a54d3f 222 *
masahikofukasawa 0:51fa46d39a3e 223 * @param registerAddress Register address to be read.
tkstreet 3:2035a4a54d3f 224 * @param buf Buffer to store the register data.
masahikofukasawa 0:51fa46d39a3e 225 * @param length Length in bytes to be read.
tkstreet 3:2035a4a54d3f 226 *
masahikofukasawa 0:51fa46d39a3e 227 * @return SUCCESS if data is read successfully, otherwise returns other value.
masahikofukasawa 0:51fa46d39a3e 228 */
masahikofukasawa 0:51fa46d39a3e 229 Status read(char registerAddress, char *buf, int length);
masahikofukasawa 0:51fa46d39a3e 230
masahikofukasawa 0:51fa46d39a3e 231 /**
tkstreet 3:2035a4a54d3f 232 * Writes data into AK9752 register(s).
tkstreet 3:2035a4a54d3f 233 *
masahikofukasawa 0:51fa46d39a3e 234 * @param registerAddress Register address to be written.
masahikofukasawa 0:51fa46d39a3e 235 * @param buf Data to be written.
masahikofukasawa 0:51fa46d39a3e 236 * @param length Length in bytes to be written.
tkstreet 3:2035a4a54d3f 237 *
masahikofukasawa 0:51fa46d39a3e 238 * @return SUCCESS if data is written successfully, otherwise returns other value.
masahikofukasawa 0:51fa46d39a3e 239 */
masahikofukasawa 0:51fa46d39a3e 240 Status write(char registerAddress, const char *buf, int length);
masahikofukasawa 0:51fa46d39a3e 241
masahikofukasawa 0:51fa46d39a3e 242 private:
tkstreet 14:c7692d514168 243
masahikofukasawa 0:51fa46d39a3e 244 I2C *connection;
masahikofukasawa 0:51fa46d39a3e 245 SlaveAddress slaveAddress;
masahikofukasawa 0:51fa46d39a3e 246
masahikofukasawa 0:51fa46d39a3e 247 // const static uint8_t IR_DATA_LEN = 7; /**<! Data length of IR sensor. From ST1 to ST2. */
masahikofukasawa 0:51fa46d39a3e 248
masahikofukasawa 0:51fa46d39a3e 249 };
masahikofukasawa 0:51fa46d39a3e 250
masahikofukasawa 0:51fa46d39a3e 251 #endif // __AK9752_H__