A library for the RFD77402 ToF distance-sensor. It's a port form the sparkfun-arduino-library.

RFD77402 ToF-sensor

I just ported the sourcecode provided by sparkfun (available on https://github.com/sparkfun/SparkFun_RFD77402_Arduino_Library) to the mbed-environment. So if you like it, please support them by e.g. buying a sparkfun-board, because in fact they did all the work :)

If you found any errors, please report them, I'm no professinal coder, so maybe there are some things I dindn't consider while porting the library.

The datasheet of the sensor is available at https://media.digikey.com/pdf/Data%20Sheets/RF%20Digital%20PDFs/RFD77402.pdf

The easiest way to use the sensor is e.g.:

RFD77402 MyDistance;                            //create an object-instance
MyDistance.begin(i2c);                          //initialize the sensor

MyDistance.takeMeasurement();                   //tell sensor to take a measurement

uint16_t distance = MyDistance.getDistance();   //get the last measurement result
Committer:
glx
Date:
Tue Nov 21 21:29:58 2017 +0000
Revision:
0:661414c07859
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
glx 0:661414c07859 1 /*
glx 0:661414c07859 2 This is a library written for the RFD77402 Time of Flight sensor.
glx 0:661414c07859 3 SparkFun sells these at its website: www.sparkfun.com
glx 0:661414c07859 4 Written by Nathan Seidle @ SparkFun Electronics, June 9th, 2017
glx 0:661414c07859 5 The VCSEL (vertical-cavity surface-emitting laser) Time of Flight sensor
glx 0:661414c07859 6 can accurately measure from 10cm to 200cm (2m) with milimeter precision.
glx 0:661414c07859 7 This library handles the initialization of the RFD77402 and bringing in of
glx 0:661414c07859 8 various readings.
glx 0:661414c07859 9 https://github.com/sparkfun/SparkFun_RFD77402_Arduino_Library
glx 0:661414c07859 10 Do you like this library? Help support SparkFun. Buy a board!
glx 0:661414c07859 11 Development environment specifics:
glx 0:661414c07859 12 Arduino IDE 1.8.1
glx 0:661414c07859 13 This program is distributed in the hope that it will be useful,
glx 0:661414c07859 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
glx 0:661414c07859 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
glx 0:661414c07859 16 GNU General Public License for more details.
glx 0:661414c07859 17 You should have received a copy of the GNU General Public License
glx 0:661414c07859 18 along with this program. If not, see <http://www.gnu.org/licenses/>.
glx 0:661414c07859 19
glx 0:661414c07859 20 I just ported the sourcecode provided by sparkfun
glx 0:661414c07859 21 (on https://github.com/sparkfun/SparkFun_RFD77402_Arduino_Library) to the
glx 0:661414c07859 22 mbed-environment. So if you like it, please support them by e.g. buying a
glx 0:661414c07859 23 sparkfun-board :)
glx 0:661414c07859 24 */
glx 0:661414c07859 25
glx 0:661414c07859 26 #include "mbed.h"
glx 0:661414c07859 27 #include "RFD77402.h"
glx 0:661414c07859 28
glx 0:661414c07859 29 //Sets up the sensor for constant read
glx 0:661414c07859 30 //Returns false if sensor does not respond
glx 0:661414c07859 31 bool RFD77402::begin(I2C &i2cPort)
glx 0:661414c07859 32 {
glx 0:661414c07859 33 //Bring in the user's choices
glx 0:661414c07859 34 _i2cPort = &i2cPort; //Grab which port the user wants us to use
glx 0:661414c07859 35
glx 0:661414c07859 36
glx 0:661414c07859 37 if (getChipID() < 0xAD00) return (false); //Chip ID failed. Should be 0xAD01 or 0xAD02
glx 0:661414c07859 38
glx 0:661414c07859 39 //Put chip into standby
glx 0:661414c07859 40 if (goToStandbyMode() == false) return (false); //Chip timed out before going to standby
glx 0:661414c07859 41
glx 0:661414c07859 42 //Drive INT_PAD high
glx 0:661414c07859 43 uint8_t setting = readRegister(RFD77402_ICSR);
glx 0:661414c07859 44 setting |= (1 << 2); //Set the bit
glx 0:661414c07859 45 writeRegister(RFD77402_ICSR, setting);
glx 0:661414c07859 46
glx 0:661414c07859 47 //Configure I2C Interface
glx 0:661414c07859 48 writeRegister(RFD77402_CONFIGURE_I2C, 0x65); //0b.0110.0101 = Address increment, auto increment, host debug, MCPU debug
glx 0:661414c07859 49
glx 0:661414c07859 50 //Set initialization - Magic from datasheet. Write 0x05 to 0x15 location.
glx 0:661414c07859 51 writeRegister16(RFD77402_CONFIGURE_PMU, 0x0500); //0b.0000.0101.0000.0000 //Patch_code_id_en, Patch_mem_en
glx 0:661414c07859 52
glx 0:661414c07859 53 if (goToOffMode() == false) return (false); //MCPU never turned off
glx 0:661414c07859 54
glx 0:661414c07859 55 //Read Module ID
glx 0:661414c07859 56 //Skipped
glx 0:661414c07859 57
glx 0:661414c07859 58 //Read Firmware ID
glx 0:661414c07859 59 //Skipped
glx 0:661414c07859 60
glx 0:661414c07859 61 //Set initialization - Magic from datasheet. Write 0x06 to 0x15 location.
glx 0:661414c07859 62 writeRegister16(RFD77402_CONFIGURE_PMU, 0x0600); //MCPU_Init_state, Patch_mem_en
glx 0:661414c07859 63
glx 0:661414c07859 64 if (goToOnMode() == false) return (false); //MCPU never turned on
glx 0:661414c07859 65
glx 0:661414c07859 66 //ToF Configuration
glx 0:661414c07859 67 //writeRegister16(RFD77402_CONFIGURE_A, 0xE100); //0b.1110.0001 = Peak is 0x0E, Threshold is 1.
glx 0:661414c07859 68 setPeak(0x0E); //Suggested values from page 20
glx 0:661414c07859 69 setThreshold(0x01);
glx 0:661414c07859 70
glx 0:661414c07859 71 writeRegister16(RFD77402_CONFIGURE_B, 0x10FF); //Set valid pixel. Set MSP430 default config.
glx 0:661414c07859 72 writeRegister16(RFD77402_CONFIGURE_HW_0, 0x07D0); //Set saturation threshold = 2,000.
glx 0:661414c07859 73 writeRegister16(RFD77402_CONFIGURE_HW_1, 0x5008); //Frequecy = 5. Low level threshold = 8.
glx 0:661414c07859 74 writeRegister16(RFD77402_CONFIGURE_HW_2, 0xA041); //Integration time = 10 * (6500-20)/15)+20 = 4.340ms. Plus reserved magic.
glx 0:661414c07859 75 writeRegister16(RFD77402_CONFIGURE_HW_3, 0x45D4); //Enable harmonic cancellation. Enable auto adjust of integration time. Plus reserved magic.
glx 0:661414c07859 76
glx 0:661414c07859 77 if (goToStandbyMode() == false) return (false); //Error - MCPU never went to standby
glx 0:661414c07859 78
glx 0:661414c07859 79 //Whew! We made it through power on configuration
glx 0:661414c07859 80
glx 0:661414c07859 81 //Get the calibration data via the 0x0006 mailbox command
glx 0:661414c07859 82 //getCalibrationData(); //Skipped
glx 0:661414c07859 83
glx 0:661414c07859 84 //Put device into Standby mode
glx 0:661414c07859 85 if (goToStandbyMode() == false) return (false); //Error - MCPU never went to standby
glx 0:661414c07859 86
glx 0:661414c07859 87 //Now assume user will want sensor in measurement mode
glx 0:661414c07859 88
glx 0:661414c07859 89 //Set initialization - Magic from datasheet. Write 0x05 to 0x15 location.
glx 0:661414c07859 90 writeRegister16(RFD77402_CONFIGURE_PMU, 0x0500); //Patch_code_id_en, Patch_mem_en
glx 0:661414c07859 91
glx 0:661414c07859 92 if (goToOffMode() == false) return (false); //Error - MCPU never turned off
glx 0:661414c07859 93
glx 0:661414c07859 94 //Write calibration data
glx 0:661414c07859 95 //Skipped
glx 0:661414c07859 96
glx 0:661414c07859 97 //Set initialization - Magic from datasheet. Write 0x06 to 0x15 location.
glx 0:661414c07859 98 writeRegister16(RFD77402_CONFIGURE_PMU, 0x0600); //MCPU_Init_state, Patch_mem_en
glx 0:661414c07859 99
glx 0:661414c07859 100 if (goToOnMode() == false) return (false); //Error - MCPU never turned on
glx 0:661414c07859 101
glx 0:661414c07859 102 return (true); //Success! Sensor is ready for measurements
glx 0:661414c07859 103 }
glx 0:661414c07859 104
glx 0:661414c07859 105 //Takes a single measurement and sets the global variables with new data
glx 0:661414c07859 106 //Returns zero if reading is good, otherwise return the errorCode from the result register.
glx 0:661414c07859 107 uint8_t RFD77402::takeMeasurement(void)
glx 0:661414c07859 108 {
glx 0:661414c07859 109 if (goToMeasurementMode() == false) return (CODE_FAILED_TIMEOUT); //Error - Timeout
glx 0:661414c07859 110 //New data is now available!
glx 0:661414c07859 111
glx 0:661414c07859 112 //Read result
glx 0:661414c07859 113 uint16_t resultRegister = readRegister16(RFD77402_RESULT);
glx 0:661414c07859 114
glx 0:661414c07859 115 if (resultRegister & 0x7FFF) //Reading is valid
glx 0:661414c07859 116 {
glx 0:661414c07859 117 uint8_t errorCode = (resultRegister >> 13) & 0x03;
glx 0:661414c07859 118
glx 0:661414c07859 119 if (errorCode == 0)
glx 0:661414c07859 120 {
glx 0:661414c07859 121 distance = (resultRegister >> 2) & 0x07FF; //Distance is good. Read it.
glx 0:661414c07859 122 //Serial.println("Distance field valid");
glx 0:661414c07859 123
glx 0:661414c07859 124 //Read confidence register
glx 0:661414c07859 125 uint16_t confidenceRegister = readRegister16(RFD77402_RESULT_CONFIDENCE);
glx 0:661414c07859 126 validPixels = confidenceRegister & 0x0F;
glx 0:661414c07859 127 confidenceValue = (confidenceRegister >> 4) & 0x07FF;
glx 0:661414c07859 128 }
glx 0:661414c07859 129
glx 0:661414c07859 130 return (errorCode);
glx 0:661414c07859 131
glx 0:661414c07859 132 }
glx 0:661414c07859 133 else
glx 0:661414c07859 134 {
glx 0:661414c07859 135 //Reading is not vald
glx 0:661414c07859 136 return (CODE_FAILED_NOT_NEW); //Error code for reading is not new
glx 0:661414c07859 137 }
glx 0:661414c07859 138
glx 0:661414c07859 139 }
glx 0:661414c07859 140
glx 0:661414c07859 141 //Returns the local variable to the caller
glx 0:661414c07859 142 uint16_t RFD77402::getDistance()
glx 0:661414c07859 143 {
glx 0:661414c07859 144 return (distance);
glx 0:661414c07859 145 }
glx 0:661414c07859 146
glx 0:661414c07859 147 //Returns the number of valid pixels found when taking measurement
glx 0:661414c07859 148 uint8_t RFD77402::getValidPixels()
glx 0:661414c07859 149 {
glx 0:661414c07859 150 return (validPixels);
glx 0:661414c07859 151 }
glx 0:661414c07859 152
glx 0:661414c07859 153 //Returns the qualitative value representing how confident the sensor is about its reported distance
glx 0:661414c07859 154 uint16_t RFD77402::getConfidenceValue()
glx 0:661414c07859 155 {
glx 0:661414c07859 156 return (confidenceValue);
glx 0:661414c07859 157 }
glx 0:661414c07859 158
glx 0:661414c07859 159 //Read the command opcode and covert to mode
glx 0:661414c07859 160 uint8_t RFD77402::getMode()
glx 0:661414c07859 161 {
glx 0:661414c07859 162 return (readRegister(RFD77402_COMMAND) & 0x3F);
glx 0:661414c07859 163 }
glx 0:661414c07859 164
glx 0:661414c07859 165 //Tell MCPU to go to standby mode
glx 0:661414c07859 166 //Return true if successful
glx 0:661414c07859 167 bool RFD77402::goToStandbyMode()
glx 0:661414c07859 168 {
glx 0:661414c07859 169 //Set Low Power Standby
glx 0:661414c07859 170 writeRegister(RFD77402_COMMAND, 0x90); //0b.1001.0000 = Go to standby mode. Set valid command.
glx 0:661414c07859 171
glx 0:661414c07859 172 //Check MCPU_ON Status
glx 0:661414c07859 173 for (uint8_t x = 0 ; x < 10 ; x++)
glx 0:661414c07859 174 {
glx 0:661414c07859 175 if ( (readRegister16(RFD77402_DEVICE_STATUS) & 0x001F) == 0x0000) return (true); //MCPU is now in standby
glx 0:661414c07859 176 wait_ms(10); //Suggested timeout for status checks from datasheet
glx 0:661414c07859 177 }
glx 0:661414c07859 178
glx 0:661414c07859 179 return (false); //Error - MCPU never went to standby
glx 0:661414c07859 180 }
glx 0:661414c07859 181
glx 0:661414c07859 182 //Tell MCPU to go to off state
glx 0:661414c07859 183 //Return true if successful
glx 0:661414c07859 184 bool RFD77402::goToOffMode()
glx 0:661414c07859 185 {
glx 0:661414c07859 186 //Set MCPU_OFF
glx 0:661414c07859 187 writeRegister(RFD77402_COMMAND, 0x91); //0b.1001.0001 = Go MCPU off state. Set valid command.
glx 0:661414c07859 188
glx 0:661414c07859 189 //Check MCPU_OFF Status
glx 0:661414c07859 190 for (uint8_t x = 0 ; x < 10 ; x++)
glx 0:661414c07859 191 {
glx 0:661414c07859 192 if ( (readRegister16(RFD77402_DEVICE_STATUS) & 0x001F) == 0x0010) return (true); //MCPU is now off
glx 0:661414c07859 193 wait_ms(10); //Suggested timeout for status checks from datasheet
glx 0:661414c07859 194 }
glx 0:661414c07859 195
glx 0:661414c07859 196 return (false); //Error - MCPU never turned off
glx 0:661414c07859 197 }
glx 0:661414c07859 198
glx 0:661414c07859 199 //Tell MCPU to go to on state
glx 0:661414c07859 200 //Return true if successful
glx 0:661414c07859 201 bool RFD77402::goToOnMode()
glx 0:661414c07859 202 {
glx 0:661414c07859 203 //Set MCPU_ON
glx 0:661414c07859 204 writeRegister(RFD77402_COMMAND, 0x92); //0b.1001.0010 = Wake up MCPU to ON mode. Set valid command.
glx 0:661414c07859 205
glx 0:661414c07859 206 //Check MCPU_ON Status
glx 0:661414c07859 207 for (uint8_t x = 0 ; x < 10 ; x++)
glx 0:661414c07859 208 {
glx 0:661414c07859 209 if ( (readRegister16(RFD77402_DEVICE_STATUS) & 0x001F) == 0x0018) return (true); //MCPU is now on
glx 0:661414c07859 210 wait_ms(10); //Suggested timeout for status checks from datasheet
glx 0:661414c07859 211 }
glx 0:661414c07859 212
glx 0:661414c07859 213 return (false); //Error - MCPU never turned on
glx 0:661414c07859 214 }
glx 0:661414c07859 215
glx 0:661414c07859 216 //Tell MCPU to go to measurement mode
glx 0:661414c07859 217 //Takes a measurement. If measurement data is ready, return true
glx 0:661414c07859 218 bool RFD77402::goToMeasurementMode()
glx 0:661414c07859 219 {
glx 0:661414c07859 220 //Single measure command
glx 0:661414c07859 221 writeRegister(RFD77402_COMMAND, 0x81); //0b.1000.0001 = Single measurement. Set valid command.
glx 0:661414c07859 222
glx 0:661414c07859 223 //Read ICSR Register - Check to see if measurement data is ready
glx 0:661414c07859 224 for (uint8_t x = 0 ; x < 10 ; x++)
glx 0:661414c07859 225 {
glx 0:661414c07859 226 if ( (readRegister(RFD77402_ICSR) & (1 << 4)) != 0) return (true); //Data is ready!
glx 0:661414c07859 227 wait_ms(10); //Suggested timeout for status checks from datasheet
glx 0:661414c07859 228 }
glx 0:661414c07859 229
glx 0:661414c07859 230 return (false); //Error - Timeout
glx 0:661414c07859 231 }
glx 0:661414c07859 232
glx 0:661414c07859 233 //Returns the VCSEL peak 4-bit value
glx 0:661414c07859 234 uint8_t RFD77402::getPeak(void)
glx 0:661414c07859 235 {
glx 0:661414c07859 236 uint16_t configValue = readRegister16(RFD77402_CONFIGURE_A);
glx 0:661414c07859 237 return ((configValue >> 12) & 0x0F);
glx 0:661414c07859 238 }
glx 0:661414c07859 239
glx 0:661414c07859 240 //Sets the VCSEL peak 4-bit value
glx 0:661414c07859 241 void RFD77402::setPeak(uint8_t peakValue)
glx 0:661414c07859 242 {
glx 0:661414c07859 243 uint16_t configValue = readRegister16(RFD77402_CONFIGURE_A); //Read
glx 0:661414c07859 244 configValue &= ~0xF000;// Zero out the peak configuration bits
glx 0:661414c07859 245 configValue |= (uint16_t)peakValue << 12; //Mask in user's settings
glx 0:661414c07859 246 writeRegister16(RFD77402_CONFIGURE_A, configValue); //Write in this new value
glx 0:661414c07859 247 }
glx 0:661414c07859 248
glx 0:661414c07859 249 //Returns the VCSEL Threshold 4-bit value
glx 0:661414c07859 250 uint8_t RFD77402::getThreshold(void)
glx 0:661414c07859 251 {
glx 0:661414c07859 252 uint16_t configValue = readRegister16(RFD77402_CONFIGURE_A);
glx 0:661414c07859 253 return ((configValue >> 8) & 0x0F);
glx 0:661414c07859 254 }
glx 0:661414c07859 255
glx 0:661414c07859 256 //Sets the VCSEL Threshold 4-bit value
glx 0:661414c07859 257 void RFD77402::setThreshold(uint8_t thresholdValue)
glx 0:661414c07859 258 {
glx 0:661414c07859 259 uint16_t configValue = readRegister16(RFD77402_CONFIGURE_A); //Read
glx 0:661414c07859 260 configValue &= ~0x0F00;// Zero out the threshold configuration bits
glx 0:661414c07859 261 configValue |= thresholdValue << 8; //Mask in user's settings
glx 0:661414c07859 262 writeRegister16(RFD77402_CONFIGURE_A, configValue); //Write in this new value
glx 0:661414c07859 263 }
glx 0:661414c07859 264
glx 0:661414c07859 265 //Returns the VCSEL Frequency 4-bit value
glx 0:661414c07859 266 uint8_t RFD77402::getFrequency(void)
glx 0:661414c07859 267 {
glx 0:661414c07859 268 uint16_t configValue = readRegister16(RFD77402_CONFIGURE_HW_1);
glx 0:661414c07859 269 return ((configValue >> 12) & 0x0F);
glx 0:661414c07859 270 }
glx 0:661414c07859 271
glx 0:661414c07859 272 //Sets the VCSEL Frequency 4-bit value
glx 0:661414c07859 273 void RFD77402::setFrequency(uint8_t thresholdValue)
glx 0:661414c07859 274 {
glx 0:661414c07859 275 uint16_t configValue = readRegister16(RFD77402_CONFIGURE_HW_1); //Read
glx 0:661414c07859 276 configValue &= ~0xF000;// Zero out the threshold configuration bits
glx 0:661414c07859 277 configValue |= thresholdValue << 12; //Mask in user's settings
glx 0:661414c07859 278 writeRegister16(RFD77402_CONFIGURE_HW_1, configValue); //Write in this new value
glx 0:661414c07859 279 }
glx 0:661414c07859 280
glx 0:661414c07859 281 //Gets whatever is in the 'MCPU to Host' mailbox
glx 0:661414c07859 282 //Check ICSR bit 5 before reading
glx 0:661414c07859 283 uint16_t RFD77402::getMailbox(void)
glx 0:661414c07859 284 {
glx 0:661414c07859 285 return (readRegister16(RFD77402_MCPU_TO_HOST_MAILBOX));
glx 0:661414c07859 286 }
glx 0:661414c07859 287
glx 0:661414c07859 288 //Software reset the device
glx 0:661414c07859 289 void RFD77402::reset(void)
glx 0:661414c07859 290 {
glx 0:661414c07859 291 writeRegister(RFD77402_COMMAND, 1<<6);
glx 0:661414c07859 292 wait_ms(100);
glx 0:661414c07859 293 }
glx 0:661414c07859 294
glx 0:661414c07859 295 //Returns the chip ID
glx 0:661414c07859 296 //Should be 0xAD01 or higher
glx 0:661414c07859 297 uint16_t RFD77402::getChipID()
glx 0:661414c07859 298 {
glx 0:661414c07859 299 return(readRegister16(RFD77402_MOD_CHIP_ID));
glx 0:661414c07859 300 }
glx 0:661414c07859 301
glx 0:661414c07859 302 //Retreive 2*27 bytes from MCPU for computation of calibration parameters
glx 0:661414c07859 303 //This is 9.2.2 from datasheet
glx 0:661414c07859 304 //Reads 54 bytes into the calibration[] array
glx 0:661414c07859 305 //Returns true if new cal data is loaded
glx 0:661414c07859 306 bool RFD77402::getCalibrationData(void)
glx 0:661414c07859 307 {
glx 0:661414c07859 308 if (goToOnMode() == false) return (false); //Error - sensor timed out before getting to On Mode
glx 0:661414c07859 309
glx 0:661414c07859 310 //Check ICSR Register and read Mailbox until it is empty
glx 0:661414c07859 311 uint8_t messages = 0;
glx 0:661414c07859 312 while (1)
glx 0:661414c07859 313 {
glx 0:661414c07859 314 if ( (readRegister(RFD77402_ICSR) & (1 << 5)) == 0) break; //Mailbox interrupt is cleared
glx 0:661414c07859 315
glx 0:661414c07859 316 //Mailbox interrupt (Bit 5) is set so read the M2H mailbox register
glx 0:661414c07859 317 getMailbox(); //Throw it out. Just read to clear the register.
glx 0:661414c07859 318
glx 0:661414c07859 319 if (messages++ > 27) return (false); //Error - Too many messages
glx 0:661414c07859 320
glx 0:661414c07859 321 wait_ms(10); //Suggested timeout for status checks from datasheet
glx 0:661414c07859 322 }
glx 0:661414c07859 323
glx 0:661414c07859 324 //Issue mailbox command
glx 0:661414c07859 325 writeRegister16(RFD77402_HOST_TO_MCPU_MAILBOX, 0x0006); //Send 0x0006 mailbox command
glx 0:661414c07859 326
glx 0:661414c07859 327 //Check to see if Mailbox can be read
glx 0:661414c07859 328 //Read 54 bytes of payload into the calibration[54] array
glx 0:661414c07859 329 for (uint8_t message = 0 ; message < 27 ; message++)
glx 0:661414c07859 330 {
glx 0:661414c07859 331 //Wait for bit to be set
glx 0:661414c07859 332 uint8_t x = 0;
glx 0:661414c07859 333 while (1)
glx 0:661414c07859 334 {
glx 0:661414c07859 335 uint8_t icsr = readRegister(RFD77402_ICSR);
glx 0:661414c07859 336 if ( (icsr & (1 << 5)) != 0) break; //New message in available
glx 0:661414c07859 337
glx 0:661414c07859 338 if (x++ > 10) return (false); //Error - Timeout
glx 0:661414c07859 339
glx 0:661414c07859 340 wait_ms(10); //Suggested timeout for status checks from datasheet
glx 0:661414c07859 341 }
glx 0:661414c07859 342
glx 0:661414c07859 343 uint16_t incoming = getMailbox(); //Get 16-bit message
glx 0:661414c07859 344
glx 0:661414c07859 345 //Put message into larger calibrationData array
glx 0:661414c07859 346 calibrationData[message * 2] = incoming >> 8;
glx 0:661414c07859 347 calibrationData[message * 2 + 1] = incoming & 0xFF;
glx 0:661414c07859 348 }
glx 0:661414c07859 349
glx 0:661414c07859 350 /*Serial.println("Calibration data:");
glx 0:661414c07859 351 for (int x = 0 ; x < 54 ; x++)
glx 0:661414c07859 352 {
glx 0:661414c07859 353 Serial.print("[");
glx 0:661414c07859 354 Serial.print(x);
glx 0:661414c07859 355 Serial.print("]=0x");
glx 0:661414c07859 356 if (calibrationData[x] < 0x10) Serial.print("0"); //Pretty print
glx 0:661414c07859 357 Serial.println(calibrationData[x], HEX);
glx 0:661414c07859 358 }*/
glx 0:661414c07859 359 }
glx 0:661414c07859 360
glx 0:661414c07859 361
glx 0:661414c07859 362
glx 0:661414c07859 363 //Reads two bytes from a given location from the RFD77402
glx 0:661414c07859 364 uint16_t RFD77402::readRegister16(char addr)
glx 0:661414c07859 365 {
glx 0:661414c07859 366 char cmd[2] = { 0 };
glx 0:661414c07859 367 cmd[0] = addr;
glx 0:661414c07859 368
glx 0:661414c07859 369 _i2cPort->write(RFD77402_ADDR, cmd, 1);
glx 0:661414c07859 370 _i2cPort->read(RFD77402_ADDR, cmd, 2);
glx 0:661414c07859 371
glx 0:661414c07859 372 //if (_i2cPort->available() != 2) return (0xFFFF); //Error
glx 0:661414c07859 373
glx 0:661414c07859 374 //uint8_t lower = _i2cPort->read();
glx 0:661414c07859 375 //uint8_t higher = _i2cPort->read();
glx 0:661414c07859 376
glx 0:661414c07859 377 return ((uint16_t)cmd[1] << 8 | cmd[0]);
glx 0:661414c07859 378 }
glx 0:661414c07859 379
glx 0:661414c07859 380 //Reads from a given location from the RFD77402
glx 0:661414c07859 381 uint8_t RFD77402::readRegister(char addr)
glx 0:661414c07859 382 {
glx 0:661414c07859 383 char cmd[1] = { 0 };
glx 0:661414c07859 384 cmd[0] = addr;
glx 0:661414c07859 385
glx 0:661414c07859 386 _i2cPort->write(RFD77402_ADDR, cmd, 1);
glx 0:661414c07859 387 _i2cPort->read(RFD77402_ADDR, cmd, 1);
glx 0:661414c07859 388 return cmd[0];
glx 0:661414c07859 389
glx 0:661414c07859 390 //return (0xFF); //Error
glx 0:661414c07859 391 }
glx 0:661414c07859 392
glx 0:661414c07859 393 //Write a 16 bit value to a spot in the RFD77402
glx 0:661414c07859 394 void RFD77402::writeRegister16(char addr, uint16_t val)
glx 0:661414c07859 395 {
glx 0:661414c07859 396 char cmd[3] = { 0 };
glx 0:661414c07859 397 cmd[0] = addr;
glx 0:661414c07859 398
glx 0:661414c07859 399 cmd[1] = (val & 0xFF); //Lower byte
glx 0:661414c07859 400 cmd[2] = (val >> 8); //Uper byte
glx 0:661414c07859 401 _i2cPort->write(RFD77402_ADDR, cmd, 3);
glx 0:661414c07859 402 }
glx 0:661414c07859 403
glx 0:661414c07859 404 //Write a value to a spot in the RFD77402
glx 0:661414c07859 405 void RFD77402::writeRegister(char addr, char val)
glx 0:661414c07859 406 {
glx 0:661414c07859 407 char cmd[2] = { 0 };
glx 0:661414c07859 408 cmd[0] = addr;
glx 0:661414c07859 409
glx 0:661414c07859 410 cmd[1] = val; //Lower byte
glx 0:661414c07859 411 _i2cPort->write(RFD77402_ADDR, cmd, 2);
glx 0:661414c07859 412 }