asdca

Dependencies:   mbed

Committer:
baumamer
Date:
Sun Apr 29 12:13:41 2018 +0000
Revision:
0:6d03232fcb64
rfd777402

Who changed what in which revision?

UserRevisionLine numberNew contents of line
baumamer 0:6d03232fcb64 1 /*
baumamer 0:6d03232fcb64 2 This is a library written for the RFD77402 Time of Flight sensor.
baumamer 0:6d03232fcb64 3 SparkFun sells these at its website: www.sparkfun.com
baumamer 0:6d03232fcb64 4 Written by Nathan Seidle @ SparkFun Electronics, June 9th, 2017
baumamer 0:6d03232fcb64 5 The VCSEL (vertical-cavity surface-emitting laser) Time of Flight sensor
baumamer 0:6d03232fcb64 6 can accurately measure from 10cm to 200cm (2m) with milimeter precision.
baumamer 0:6d03232fcb64 7 This library handles the initialization of the RFD77402 and bringing in of
baumamer 0:6d03232fcb64 8 various readings.
baumamer 0:6d03232fcb64 9 https://github.com/sparkfun/SparkFun_RFD77402_Arduino_Library
baumamer 0:6d03232fcb64 10 Do you like this library? Help support SparkFun. Buy a board!
baumamer 0:6d03232fcb64 11 Development environment specifics:
baumamer 0:6d03232fcb64 12 Arduino IDE 1.8.1
baumamer 0:6d03232fcb64 13 This program is distributed in the hope that it will be useful,
baumamer 0:6d03232fcb64 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
baumamer 0:6d03232fcb64 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
baumamer 0:6d03232fcb64 16 GNU General Public License for more details.
baumamer 0:6d03232fcb64 17 You should have received a copy of the GNU General Public License
baumamer 0:6d03232fcb64 18 along with this program. If not, see <http://www.gnu.org/licenses/>.
baumamer 0:6d03232fcb64 19 */
baumamer 0:6d03232fcb64 20 #ifndef RFD77402_h
baumamer 0:6d03232fcb64 21 #define RFD77402_h
baumamer 0:6d03232fcb64 22
baumamer 0:6d03232fcb64 23 #include <mbed.h>
baumamer 0:6d03232fcb64 24 /*
baumamer 0:6d03232fcb64 25 //#define RFD77402_ADDR 0x4C //7-bit unshifted default I2C Address
baumamer 0:6d03232fcb64 26 #define RFD77402_ADDR 0x4C //7-bit unshifted default I2C Address
baumamer 0:6d03232fcb64 27
baumamer 0:6d03232fcb64 28 //Register addresses
baumamer 0:6d03232fcb64 29 #define RFD77402_ICSR 0x00
baumamer 0:6d03232fcb64 30 #define RFD77402_INTERRUPTS 0x02
baumamer 0:6d03232fcb64 31 #define RFD77402_COMMAND 0x04
baumamer 0:6d03232fcb64 32 #define RFD77402_DEVICE_STATUS 0x06
baumamer 0:6d03232fcb64 33 #define RFD77402_RESULT 0x08
baumamer 0:6d03232fcb64 34 #define RFD77402_RESULT_CONFIDENCE 0x0A
baumamer 0:6d03232fcb64 35 #define RFD77402_CONFIGURE_A 0x0C
baumamer 0:6d03232fcb64 36 #define RFD77402_CONFIGURE_B 0x0E
baumamer 0:6d03232fcb64 37 #define RFD77402_HOST_TO_MCPU_MAILBOX 0x10
baumamer 0:6d03232fcb64 38 #define RFD77402_MCPU_TO_HOST_MAILBOX 0x12
baumamer 0:6d03232fcb64 39 #define RFD77402_CONFIGURE_PMU 0x14
baumamer 0:6d03232fcb64 40 #define RFD77402_CONFIGURE_I2C 0x1C
baumamer 0:6d03232fcb64 41 #define RFD77402_CONFIGURE_HW_0 0x20
baumamer 0:6d03232fcb64 42 #define RFD77402_CONFIGURE_HW_1 0x22
baumamer 0:6d03232fcb64 43 #define RFD77402_CONFIGURE_HW_2 0x24
baumamer 0:6d03232fcb64 44 #define RFD77402_CONFIGURE_HW_3 0x26
baumamer 0:6d03232fcb64 45 #define RFD77402_MOD_CHIP_ID 0x28
baumamer 0:6d03232fcb64 46
baumamer 0:6d03232fcb64 47 #define RFD77402_MODE_MEASUREMENT 0x01
baumamer 0:6d03232fcb64 48 #define RFD77402_MODE_STANDBY 0x10
baumamer 0:6d03232fcb64 49 #define RFD77402_MODE_OFF 0x11
baumamer 0:6d03232fcb64 50 #define RFD77402_MODE_ON 0x12
baumamer 0:6d03232fcb64 51
baumamer 0:6d03232fcb64 52 #define CODE_VALID_DATA 0x00
baumamer 0:6d03232fcb64 53 #define CODE_FAILED_PIXELS 0x01
baumamer 0:6d03232fcb64 54 #define CODE_FAILED_SIGNAL 0x02
baumamer 0:6d03232fcb64 55 #define CODE_FAILED_SATURATED 0x03
baumamer 0:6d03232fcb64 56 #define CODE_FAILED_NOT_NEW 0x04
baumamer 0:6d03232fcb64 57 #define CODE_FAILED_TIMEOUT 0x05
baumamer 0:6d03232fcb64 58
baumamer 0:6d03232fcb64 59 #define I2C_SPEED_STANDARD 100000
baumamer 0:6d03232fcb64 60 #define I2C_SPEED_FAST 400000
baumamer 0:6d03232fcb64 61
baumamer 0:6d03232fcb64 62 #define INT_CLR_REG 1 //tells which register read clears the interrupt (Default: 1, Result Register)
baumamer 0:6d03232fcb64 63 #define INT_CLR 0 << 1 //tells whether or not to clear when register is read (Default: 0, cleared upon register read)
baumamer 0:6d03232fcb64 64 #define INT_PIN_TYPE 1 << 2 //tells whether int is push-pull or open drain (Default: 1, push-pull)
baumamer 0:6d03232fcb64 65 #define INT_LOHI 0 << 3 //tells whether the interrupt is active low or high (Default: 0, active low)
baumamer 0:6d03232fcb64 66
baumamer 0:6d03232fcb64 67 //Setting any of the following bits to 1 enables an interrupt when that event occurs
baumamer 0:6d03232fcb64 68 #define INTSRC_DATA 1 //Interrupt fires with newly available data
baumamer 0:6d03232fcb64 69 #define INTSRC_M2H 0 << 1//Interrupt fires with newly available data in M2H mailbox register
baumamer 0:6d03232fcb64 70 #define INTSRC_H2M 0 << 2//Interrupt fires when H2M register is read
baumamer 0:6d03232fcb64 71 #define INTSRC_RST 0 << 3 //Interrupt fires when HW reset occurs
baumamer 0:6d03232fcb64 72 */
baumamer 0:6d03232fcb64 73
baumamer 0:6d03232fcb64 74 class RFD77402
baumamer 0:6d03232fcb64 75 {
baumamer 0:6d03232fcb64 76 public:
baumamer 0:6d03232fcb64 77
baumamer 0:6d03232fcb64 78 // register addresses from API vl53l0x_device.h (ordered as listed there)
baumamer 0:6d03232fcb64 79 enum regAddr {
baumamer 0:6d03232fcb64 80
baumamer 0:6d03232fcb64 81 RFD77402_ADDR = 0x4C<<1, //7-bit unshifted default I2C Address
baumamer 0:6d03232fcb64 82
baumamer 0:6d03232fcb64 83 //Register addresses
baumamer 0:6d03232fcb64 84 RFD77402_ICSR = 0x00,
baumamer 0:6d03232fcb64 85 RFD77402_INTERRUPTS = 0x02,
baumamer 0:6d03232fcb64 86 RFD77402_COMMAND = 0x04,
baumamer 0:6d03232fcb64 87 RFD77402_DEVICE_STATUS = 0x06,
baumamer 0:6d03232fcb64 88 RFD77402_RESULT = 0x08,
baumamer 0:6d03232fcb64 89 RFD77402_RESULT_CONFIDENCE = 0x0A,
baumamer 0:6d03232fcb64 90 RFD77402_CONFIGURE_A = 0x0C,
baumamer 0:6d03232fcb64 91 RFD77402_CONFIGURE_B = 0x0E,
baumamer 0:6d03232fcb64 92 RFD77402_HOST_TO_MCPU_MAILBOX = 0x10,
baumamer 0:6d03232fcb64 93 RFD77402_MCPU_TO_HOST_MAILBOX = 0x12,
baumamer 0:6d03232fcb64 94 RFD77402_CONFIGURE_PMU = 0x14,
baumamer 0:6d03232fcb64 95 RFD77402_CONFIGURE_I2C = 0x1C,
baumamer 0:6d03232fcb64 96 RFD77402_CONFIGURE_HW_0 = 0x20,
baumamer 0:6d03232fcb64 97 RFD77402_CONFIGURE_HW_1 = 0x22,
baumamer 0:6d03232fcb64 98 RFD77402_CONFIGURE_HW_2 = 0x24,
baumamer 0:6d03232fcb64 99 RFD77402_CONFIGURE_HW_3 = 0x26,
baumamer 0:6d03232fcb64 100 RFD77402_MOD_CHIP_ID = 0x28,
baumamer 0:6d03232fcb64 101
baumamer 0:6d03232fcb64 102 RFD77402_MODE_MEASUREMENT = 0x01,
baumamer 0:6d03232fcb64 103 RFD77402_MODE_STANDBY = 0x10,
baumamer 0:6d03232fcb64 104 RFD77402_MODE_OFF = 0x11,
baumamer 0:6d03232fcb64 105 RFD77402_MODE_ON = 0x12,
baumamer 0:6d03232fcb64 106
baumamer 0:6d03232fcb64 107 CODE_VALID_DATA = 0x00,
baumamer 0:6d03232fcb64 108 CODE_FAILED_PIXELS = 0x01,
baumamer 0:6d03232fcb64 109 CODE_FAILED_SIGNAL = 0x02,
baumamer 0:6d03232fcb64 110 CODE_FAILED_SATURATED = 0x03,
baumamer 0:6d03232fcb64 111 CODE_FAILED_NOT_NEW = 0x04,
baumamer 0:6d03232fcb64 112 CODE_FAILED_TIMEOUT = 0x05,
baumamer 0:6d03232fcb64 113
baumamer 0:6d03232fcb64 114 I2C_SPEED_STANDARD = 100000,
baumamer 0:6d03232fcb64 115 I2C_SPEED_FAST = 400000,
baumamer 0:6d03232fcb64 116
baumamer 0:6d03232fcb64 117 INT_CLR_REG = 1, //tells which register read clears the interrupt (Default: 1, Result Register)
baumamer 0:6d03232fcb64 118 INT_CLR = 0<<1, //tells whether or not to clear when register is read (Default: 0, cleared upon register read)
baumamer 0:6d03232fcb64 119 INT_PIN_TYPE = 1 << 2, //tells whether int is push-pull or open drain (Default: 1, push-pull)
baumamer 0:6d03232fcb64 120 INT_LOHI = 0 << 3, //tells whether the interrupt is active low or high (Default: 0, active low)
baumamer 0:6d03232fcb64 121
baumamer 0:6d03232fcb64 122 //Setting any of the following bits to 1 enables an interrupt when that event occurs
baumamer 0:6d03232fcb64 123 INTSRC_DATA = 1, //Interrupt fires with newly available data
baumamer 0:6d03232fcb64 124 INTSRC_M2H = 0 << 1, //Interrupt fires with newly available data in M2H mailbox register
baumamer 0:6d03232fcb64 125 INTSRC_H2M = 0 << 2, //Interrupt fires when H2M register is read
baumamer 0:6d03232fcb64 126 INTSRC_RST = 0 << 3, //Interrupt fires when HW reset occurs
baumamer 0:6d03232fcb64 127
baumamer 0:6d03232fcb64 128 };
baumamer 0:6d03232fcb64 129
baumamer 0:6d03232fcb64 130 // Constructor
baumamer 0:6d03232fcb64 131 RFD77402(I2C*);
baumamer 0:6d03232fcb64 132 virtual ~RFD77402(); //Destroyer
baumamer 0:6d03232fcb64 133
baumamer 0:6d03232fcb64 134 //By default use Wire, standard I2C speed, and the defaul AK9750 address
baumamer 0:6d03232fcb64 135 bool init();
baumamer 0:6d03232fcb64 136
baumamer 0:6d03232fcb64 137 uint8_t takeMeasurement(); //Takes a single measurement and sets the global variables with new data
baumamer 0:6d03232fcb64 138 uint16_t getDistance(); //Returns the local variable to the caller
baumamer 0:6d03232fcb64 139 uint8_t getValidPixels(); //Returns the number of valid pixels found when taking measurement
baumamer 0:6d03232fcb64 140 uint16_t getConfidenceValue(); //Returns the qualitative value representing how confident the sensor is about its reported distance
baumamer 0:6d03232fcb64 141 uint8_t getMode(); //Read the command opcode and covert to mode
baumamer 0:6d03232fcb64 142
baumamer 0:6d03232fcb64 143 bool goToStandbyMode(); //Tell MCPU to go to standby mode. Return true if successful
baumamer 0:6d03232fcb64 144 bool goToOffMode(); //Tell MCPU to go to off state. Return true if successful
baumamer 0:6d03232fcb64 145 bool goToOnMode(); //Tell MCPU to go to on state. Return true if successful
baumamer 0:6d03232fcb64 146 bool goToMeasurementMode(); //Tell MCPU to go to measurement mode. Takes a measurement. If measurement data is ready, return true
baumamer 0:6d03232fcb64 147
baumamer 0:6d03232fcb64 148 uint8_t getPeak(); //Returns the VCSEL peak 4-bit value
baumamer 0:6d03232fcb64 149 void setPeak(uint8_t peakValue); //Sets the VCSEL peak 4-bit value
baumamer 0:6d03232fcb64 150 uint8_t getThreshold(); //Returns the VCSEL Threshold 4-bit value
baumamer 0:6d03232fcb64 151 void setThreshold(uint8_t threshold); //Sets the VCSEL Threshold 4-bit value
baumamer 0:6d03232fcb64 152 uint8_t getFrequency(); //Returns the VCSEL Frequency 4-bit value
baumamer 0:6d03232fcb64 153 void setFrequency(uint8_t threshold); //Sets the VCSEL Frequency 4-bit value
baumamer 0:6d03232fcb64 154
baumamer 0:6d03232fcb64 155 uint16_t getMailbox(); //Gets whatever is in the 'MCPU to Host' mailbox. Check ICSR bit 5 before reading.
baumamer 0:6d03232fcb64 156 void reset(); //Software reset the device
baumamer 0:6d03232fcb64 157 uint16_t getChipID(); //Returns the chip ID. Should be 0xAD01 or higher.
baumamer 0:6d03232fcb64 158
baumamer 0:6d03232fcb64 159 //Retreive 2*27 bytes from MCPU for computation of calibration parameters
baumamer 0:6d03232fcb64 160 //Reads 54 bytes into the calibration[] array
baumamer 0:6d03232fcb64 161 //Returns true if new cal data is loaded
baumamer 0:6d03232fcb64 162 bool getCalibrationData();
baumamer 0:6d03232fcb64 163
baumamer 0:6d03232fcb64 164 uint16_t readRegister16(uint8_t addr); //Reads two bytes from a given location from the RFD77402
baumamer 0:6d03232fcb64 165 uint8_t readRegister(uint8_t addr); //Reads from a given location from the RFD77402
baumamer 0:6d03232fcb64 166 void writeRegister16(uint8_t addr, uint16_t val); //Write a 16 bit value to a spot in the RFD77402
baumamer 0:6d03232fcb64 167 void writeRegister(uint8_t addr, uint8_t val); //Write a value to a spot in the RFD77402
baumamer 0:6d03232fcb64 168
baumamer 0:6d03232fcb64 169 //Variables
baumamer 0:6d03232fcb64 170 uint16_t distance;
baumamer 0:6d03232fcb64 171 uint8_t validPixels;
baumamer 0:6d03232fcb64 172 uint16_t confidenceValue;
baumamer 0:6d03232fcb64 173 uint8_t calibrationData[54]; //Loaded by the 0x006 mailbox command
baumamer 0:6d03232fcb64 174
baumamer 0:6d03232fcb64 175 private:
baumamer 0:6d03232fcb64 176 /*
baumamer 0:6d03232fcb64 177 //Variables
baumamer 0:6d03232fcb64 178 TwoWire *_i2cPort; //The generic connection to user's chosen I2C hardware
baumamer 0:6d03232fcb64 179 */
baumamer 0:6d03232fcb64 180 // mbed members
baumamer 0:6d03232fcb64 181 I2C* i2c;
baumamer 0:6d03232fcb64 182 Timer* timer;
baumamer 0:6d03232fcb64 183 };
baumamer 0:6d03232fcb64 184
baumamer 0:6d03232fcb64 185 #endif /* RFD77402_h */