A compilation of some hardware sensors and their shared programming interfaces.

Committer:
mgottscho
Date:
Sun Mar 16 01:48:59 2014 +0000
Revision:
0:8d34cc2ff388
A compilation of various hardware sensors and some shared programming interfaces.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mgottscho 0:8d34cc2ff388 1 /* I2CSensor.cpp
mgottscho 0:8d34cc2ff388 2 * Tested with mbed board: FRDM-KL46Z
mgottscho 0:8d34cc2ff388 3 * Author: Mark Gottscho
mgottscho 0:8d34cc2ff388 4 * mgottscho@ucla.edu
mgottscho 0:8d34cc2ff388 5 */
mgottscho 0:8d34cc2ff388 6
mgottscho 0:8d34cc2ff388 7 #include "mbed.h"
mgottscho 0:8d34cc2ff388 8 #include "I2CSensor.h"
mgottscho 0:8d34cc2ff388 9
mgottscho 0:8d34cc2ff388 10 using namespace std;
mgottscho 0:8d34cc2ff388 11
mgottscho 0:8d34cc2ff388 12 ///////////////////// Public methods /////////////////////////////
mgottscho 0:8d34cc2ff388 13
mgottscho 0:8d34cc2ff388 14 I2CSensor::I2CSensor(PinName sda, PinName scl, int i2c_addr) :
mgottscho 0:8d34cc2ff388 15 __sda_pin(sda),
mgottscho 0:8d34cc2ff388 16 __scl_pin(scl),
mgottscho 0:8d34cc2ff388 17 __i2c_addr(i2c_addr),
mgottscho 0:8d34cc2ff388 18 __who_am_i(0),
mgottscho 0:8d34cc2ff388 19 __i2c(sda, scl)
mgottscho 0:8d34cc2ff388 20 { }
mgottscho 0:8d34cc2ff388 21
mgottscho 0:8d34cc2ff388 22 I2CSensor::~I2CSensor() { }
mgottscho 0:8d34cc2ff388 23
mgottscho 0:8d34cc2ff388 24 PinName I2CSensor::getSDAPin() { return __sda_pin; }
mgottscho 0:8d34cc2ff388 25
mgottscho 0:8d34cc2ff388 26 PinName I2CSensor::getSCLPin() { return __scl_pin; }
mgottscho 0:8d34cc2ff388 27
mgottscho 0:8d34cc2ff388 28 uint8_t I2CSensor::getDeviceI2CAddress() { return __i2c_addr; }
mgottscho 0:8d34cc2ff388 29
mgottscho 0:8d34cc2ff388 30 uint8_t I2CSensor::getRegister(const uint8_t reg_addr) {
mgottscho 0:8d34cc2ff388 31 uint8_t data;
mgottscho 0:8d34cc2ff388 32 __readReg(reg_addr, &data, 1);
mgottscho 0:8d34cc2ff388 33 return data;
mgottscho 0:8d34cc2ff388 34 }
mgottscho 0:8d34cc2ff388 35
mgottscho 0:8d34cc2ff388 36 uint16_t I2CSensor::getRegister16b(const uint8_t reg_addr) {
mgottscho 0:8d34cc2ff388 37 uint8_t payload[2];
mgottscho 0:8d34cc2ff388 38 __readReg(reg_addr, payload, 2);
mgottscho 0:8d34cc2ff388 39 uint16_t data = (payload[0] << 8) | (payload[1]);
mgottscho 0:8d34cc2ff388 40 return data;
mgottscho 0:8d34cc2ff388 41 }
mgottscho 0:8d34cc2ff388 42
mgottscho 0:8d34cc2ff388 43 void I2CSensor::setRegister(const uint8_t reg_addr, const uint8_t data) {
mgottscho 0:8d34cc2ff388 44 uint8_t payload[2] = {reg_addr, data};
mgottscho 0:8d34cc2ff388 45 __writeReg(payload, 2);
mgottscho 0:8d34cc2ff388 46 }
mgottscho 0:8d34cc2ff388 47
mgottscho 0:8d34cc2ff388 48 void I2CSensor::setRegister16b(const uint8_t reg_addr, const uint16_t data) {
mgottscho 0:8d34cc2ff388 49 uint8_t dataMSB = (data >> 8) & 0x00FF;
mgottscho 0:8d34cc2ff388 50 uint8_t dataLSB = data & 0x00FF;
mgottscho 0:8d34cc2ff388 51 uint8_t payload[3] = {reg_addr, dataMSB, dataLSB};
mgottscho 0:8d34cc2ff388 52 __writeReg(payload, 3);
mgottscho 0:8d34cc2ff388 53 }
mgottscho 0:8d34cc2ff388 54
mgottscho 0:8d34cc2ff388 55 ///////////////////// Protected methods /////////////////////////////
mgottscho 0:8d34cc2ff388 56
mgottscho 0:8d34cc2ff388 57 int I2CSensor::__readReg(const uint8_t reg_addr, uint8_t *data, int len) {
mgottscho 0:8d34cc2ff388 58 int retval = 0;
mgottscho 0:8d34cc2ff388 59
mgottscho 0:8d34cc2ff388 60 __disable_irq();
mgottscho 0:8d34cc2ff388 61 retval = __i2c.write(__i2c_addr, (char *) &reg_addr, 1, true);
mgottscho 0:8d34cc2ff388 62 if (retval != 0) {
mgottscho 0:8d34cc2ff388 63 __enable_irq();
mgottscho 0:8d34cc2ff388 64 return retval;
mgottscho 0:8d34cc2ff388 65 }
mgottscho 0:8d34cc2ff388 66 retval = __i2c.read(__i2c_addr, (char *) data, len);
mgottscho 0:8d34cc2ff388 67 __enable_irq();
mgottscho 0:8d34cc2ff388 68
mgottscho 0:8d34cc2ff388 69 return retval;
mgottscho 0:8d34cc2ff388 70 }
mgottscho 0:8d34cc2ff388 71
mgottscho 0:8d34cc2ff388 72 int I2CSensor::__writeReg(const uint8_t *data, int total_len) {
mgottscho 0:8d34cc2ff388 73 int retval = 0;
mgottscho 0:8d34cc2ff388 74
mgottscho 0:8d34cc2ff388 75 __disable_irq();
mgottscho 0:8d34cc2ff388 76 retval = __i2c.write(__i2c_addr, (char *) data, total_len);
mgottscho 0:8d34cc2ff388 77 __enable_irq();
mgottscho 0:8d34cc2ff388 78
mgottscho 0:8d34cc2ff388 79 return retval;
mgottscho 0:8d34cc2ff388 80 }