adc121c021: Initial implementation

Tested on the Grove I2C ADC.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Sarah Knepper <sarah.knepper@intel.com>
This commit is contained in:
Jon Trulson 2014-12-15 17:17:19 -07:00 committed by Sarah Knepper
parent eca9ed95c8
commit 878ca55385
8 changed files with 709 additions and 0 deletions

View File

@ -53,6 +53,7 @@ add_executable (ldt0028-example ldt0028.cxx)
add_executable (am2315-example am2315.cxx)
add_executable (itg3200-example itg3200.cxx)
add_executable (enc03r-example enc03r.cxx)
add_executable (adc121c021-example adc121c021.cxx)
include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@ -93,6 +94,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/ldt0028)
include_directories (${PROJECT_SOURCE_DIR}/src/am2315)
include_directories (${PROJECT_SOURCE_DIR}/src/itg3200)
include_directories (${PROJECT_SOURCE_DIR}/src/enc03r)
include_directories (${PROJECT_SOURCE_DIR}/src/adc121c021)
target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@ -149,3 +151,4 @@ target_link_libraries (ldt0028-example ldt0028 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (am2315-example am2315 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (itg3200-example itg3200 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (enc03r-example enc03r ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (adc121c021-example adc121c021 ${CMAKE_THREAD_LIBS_INIT})

66
examples/adc121c021.cxx Normal file
View File

@ -0,0 +1,66 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <signal.h>
#include <iostream>
#include "adc121c021.h"
using namespace std;
int shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main(int argc, char **argv)
{
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate an ADC121C021 on I2C bus 0
upm::ADC121C021 *adc = new upm::ADC121C021(ADC121C021_I2C_BUS,
ADC121C021_DEFAULT_I2C_ADDR);
// An analog sensor, such as a Grove light sensor,
// must be attached to the adc
// Prints the value and corresponding voltage every 50 milliseconds
while (shouldRun)
{
uint16_t val = adc->value();
cout << "ADC value: " << val << " Volts = "
<< adc->valueToVolts(val) << endl;
usleep(50000);
}
//! [Interesting]
cout << "Exiting..." << endl;
delete adc;
return 0;
}

View File

@ -0,0 +1,68 @@
/*jslint node:true, vars:true, bitwise:true, unparam:true */
/*jshint unused:true */
/*global */
/*
* Author: Zion Orent <zorent@ics.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Load ADC121C021 module
var I2C_ADC = require('jsupm_adc121c021');
// Instantiate an ADC121C021 on I2C bus 0
var myI2C_ADC = new I2C_ADC.ADC121C021(I2C_ADC.ADC121C021_I2C_BUS,
I2C_ADC.ADC121C021_DEFAULT_I2C_ADDR);
var g_sum, g_iteration, g_intervalObj;
var g_total_iteration_count = 30;
// An analog sensor, such as a Grove light sensor,
// must be attached to the adc
main();
// this function will be called by itself
function main()
{
g_sum = 0;
g_iteration = 0;
// Sum the value every 50 milliseconds for g_total_iteration_count times
// Then print the average value and corresponding voltage
g_intervalObj = setInterval(function(I2CADC_val)
{
g_sum += I2CADC_val;
if (g_iteration >= g_total_iteration_count)
{
g_iteration = 0;
var val = (g_sum / g_total_iteration_count);
console.log("ADC value: " + val + " Volts = " +
myI2C_ADC.valueToVolts(val));
clearInterval(g_intervalObj);
// Now that we're done with this iteration, go to the next iteration
main();
}
g_iteration++;
}, 50, myI2C_ADC.value());
}
// Print message when exiting
process.on('SIGINT', function()
{
console.log("Exiting...");
process.exit(0);
});

View File

@ -0,0 +1,5 @@
set (libname "adc121c021")
set (libdescription "upm adc121c021 I2C ADC converter module")
set (module_src ${libname}.cxx)
set (module_h ${libname}.h)
upm_module_init()

View File

@ -0,0 +1,250 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <iostream>
#include <string>
#include "adc121c021.h"
using namespace upm;
using namespace std;
ADC121C021::ADC121C021(int bus, uint8_t address, float vref)
{
// setup our i2c link
m_i2c = mraa_i2c_init(bus);
m_addr = address;
mraa_result_t ret = mraa_i2c_address(m_i2c, m_addr);
if (ret != MRAA_SUCCESS)
cerr << "ADC121C021: Could not initialize i2c bus. " << endl;
m_vref = vref;
}
ADC121C021::~ADC121C021()
{
mraa_i2c_stop(m_i2c);
}
mraa_result_t ADC121C021::writeByte(uint8_t reg, uint8_t byte)
{
return mraa_i2c_write_byte_data(m_i2c, byte, reg);
}
mraa_result_t ADC121C021::writeWord(uint8_t reg, uint16_t word)
{
// We need to swap the bytes
uint8_t b1 = (word & 0xff00) >> 8;
word <<= 8;
word |= b1;
return mraa_i2c_write_word_data(m_i2c, word, reg);
}
uint8_t ADC121C021::readByte(uint8_t reg)
{
return mraa_i2c_read_byte_data(m_i2c, reg);
}
uint16_t ADC121C021::readWord(uint8_t reg)
{
uint16_t val = mraa_i2c_read_word_data(m_i2c, reg);
uint8_t b1;
// The value returned is in the wrong byte order, so we need to swap them
b1 = (val & 0xff00) >> 8;
val <<= 8;
val |= b1;
return val;
}
uint16_t ADC121C021::value()
{
// mask off alert flag and reserved bits
return (readWord(ADC121C021_REG_RESULT) & 0x0fff);
}
float ADC121C021::valueToVolts(uint16_t val)
{
// The arduino example multiplies this by 2, which seems wrong. If
// the reference voltage is 3.0, then you should never get a voltage
// value higher than that.
//
// val * m_vref * 2.0 / ADC121C021_RESOLUTION
return (val * m_vref / ADC121C021_RESOLUTION);
}
bool ADC121C021::getAlertStatus()
{
// high order bit is the alert flag, mask off the rest
bool rv = (readWord(ADC121C021_REG_RESULT) & 0x8000) ? true : false;
if (rv)
{
// read the alert low and high values and set the appropriate
// member variables
uint8_t astatus = readByte(ADC121C021_REG_ALERT_STATUS);
if (astatus & 0x01)
m_alertLow = true;
else
m_alertLow = false;
if (astatus & 0x02)
m_alertHigh = true;
else
m_alertHigh = false;
}
return rv;
}
void ADC121C021::clearAlertStatus()
{
// zero out both the low and high alert flags
writeByte(ADC121C021_REG_ALERT_STATUS, 0x03);
m_alertHigh = false;
m_alertLow = false;
}
void ADC121C021::enableAlertFlag(bool enable)
{
// read the current config register
uint8_t val = readByte(ADC121C021_REG_CONFIG);
if (enable)
val |= 0x08;
else
val &= ~0x08;
// write the register back
writeByte(ADC121C021_REG_CONFIG, val);
}
void ADC121C021::enableAlertPin(bool enable)
{
// read the current config register
uint8_t val = readByte(ADC121C021_REG_CONFIG);
if (enable)
val |= 0x04;
else
val &= ~0x04;
// write the register back
writeByte(ADC121C021_REG_CONFIG, val);
}
void ADC121C021::enableAlertHold(bool enable)
{
// read the current config register
uint8_t val = readByte(ADC121C021_REG_CONFIG);
if (enable)
val |= 0x10;
else
val &= ~0x10;
// write the register back
writeByte(ADC121C021_REG_CONFIG, val);
}
void ADC121C021::enableAlertPinPolarityHigh(bool enable)
{
// read the current config register
uint8_t val = readByte(ADC121C021_REG_CONFIG);
if (enable)
val |= 0x01;
else
val &= ~0x01;
// write the register back
writeByte(ADC121C021_REG_CONFIG, val);
}
void ADC121C021::setAutomaticConversion(ADC121C021_CYCLE_TIME_T cycleTime)
{
// first we
// read the current config register, masking off the cycle time bits
uint8_t val = readByte(ADC121C021_REG_CONFIG) & 0x1f;
val |= ((uint8_t)cycleTime << 5);
// write the register back
writeByte(ADC121C021_REG_CONFIG, val);
}
mraa_result_t ADC121C021::setAlertLowLimit(uint16_t limit)
{
// mask off the invalid bits in case they were set
limit &= 0x0fff;
// write it
return writeWord(ADC121C021_REG_ALERT_LIM_UNDER, limit);
}
mraa_result_t ADC121C021::setAlertHighLimit(uint16_t limit)
{
// mask off the invalid bits in case they were set
limit &= 0x0fff;
// write it
return writeWord(ADC121C021_REG_ALERT_LIM_OVER, limit);
}
mraa_result_t ADC121C021::setHysteresis(uint16_t limit)
{
// mask off the invalid bits in case they were set
limit &= 0x0fff;
// write it
return writeWord(ADC121C021_REG_ALERT_HYS, limit);
}
uint16_t ADC121C021::getHighestConversion()
{
return readWord(ADC121C021_REG_HIGHEST_CONV);
}
uint16_t ADC121C021::getLowestConversion()
{
return readWord(ADC121C021_REG_LOWEST_CONV);
}
mraa_result_t ADC121C021::clearHighestConversion()
{
return writeWord(ADC121C021_REG_HIGHEST_CONV, 0x0000);
}
mraa_result_t ADC121C021::clearLowestConversion()
{
return writeWord(ADC121C021_REG_LOWEST_CONV, 0x0fff);
}

295
src/adc121c021/adc121c021.h Normal file
View File

@ -0,0 +1,295 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <string>
#include <mraa/i2c.h>
#define ADC121C021_I2C_BUS 0
#define ADC121C021_DEFAULT_I2C_ADDR 0x55
#define ADC121C021_RESOLUTION 4096 // 12 bits
/**
* ADC121C021 registers
*/
#define ADC121C021_REG_RESULT 0x00
#define ADC121C021_REG_ALERT_STATUS 0x01
#define ADC121C021_REG_CONFIG 0x02
#define ADC121C021_REG_ALERT_LIM_UNDER 0x03
#define ADC121C021_REG_ALERT_LIM_OVER 0x04
#define ADC121C021_REG_ALERT_HYS 0x05
#define ADC121C021_REG_LOWEST_CONV 0x06
#define ADC121C021_REG_HIGHEST_CONV 0x07
// For the Grove I2C ADC
#define ADC121C021_DEFAULT_VREF 3.0
namespace upm {
/**
* valid cycle times for automatic conversion mode
*/
typedef enum { ADC121C021_CYCLE_NONE = 0, // disabled
ADC121C021_CYCLE_32 = 1, // 27 ksps
ADC121C021_CYCLE_64 = 2, // 13.5
ADC121C021_CYCLE_128 = 3, // 6.7
ADC121C021_CYCLE_256 = 4, // 3.4
ADC121C021_CYCLE_512 = 5, // 1.7
ADC121C021_CYCLE_1024 = 6, // 0.9
ADC121C021_CYCLE_2048 = 7 // 0.4
} ADC121C021_CYCLE_TIME_T;
/**
* @brief C++ API for the ADC121C021 I2C ADC
*
* UPM module for the ADC121C021 12 bit Analog to Digital (ADC) converter.
* By providing a constant reference voltage, this sensor helps
* to increase the accuracy of a value collected from an analog sensor.
*
* @ingroup i2c adc121c021
* @snippet adc121c021.cxx Interesting
*/
class ADC121C021 {
public:
/**
* adc121c021 ADC constructor
*
* @param bus i2c bus to use
* @param address the address for this sensor; default is 0x55
* @param vref reference voltage for this sensor; default is 3.0
*/
ADC121C021(int bus, uint8_t address = ADC121C021_DEFAULT_I2C_ADDR,
float vref = ADC121C021_DEFAULT_VREF);
/**
* ADC121C021 Destructor
*/
~ADC121C021();
/**
* Write byte value into register
*
* @param reg register location to write into
* @param byte byte to write
* @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
*/
mraa_result_t writeByte(uint8_t reg, uint8_t byte);
/**
* Write word value into register
*
* @param reg register location to write into
* @param word word to write
* @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
*/
mraa_result_t writeWord(uint8_t reg, uint16_t word);
/**
* Read byte value from register
*
* @param reg register location to read from
* @return value at specified register
*/
uint8_t readByte(uint8_t reg);
/**
* Read word value from register
*
* @param reg register location to read from
* @return value at specified register
*/
uint16_t readWord(uint8_t reg);
/**
* Read current value of conversion
*
* @return current conversion value
*/
uint16_t value();
/**
* Convert a supplied value to voltage based on set VREF
*
* @param val value of conversion (from value())
* @return conversion value in volts
*/
float valueToVolts(uint16_t val);
/**
* Read current status of the alert flag. If the flag is set, the
* lower or upper alert indicators will be set as appropriate, and
* you can access these values with alertLowTriggered() or
* alertHighTriggered().
*
* @return true if the alert flag is set
*/
bool getAlertStatus();
/**
* Return the current value of m_alertLow. You must call
* getAlertStatus() to update this value.
*
* @return current alert low status
*/
bool alertLowTriggered() { return m_alertLow; };
/**
* Return the current value of m_alertHigh. You must call
* getAlertStatus() to update this value.
*
* @return current alert high status
*/
bool alertHighTriggered() { return m_alertHigh; };
/**
* Clear the alert low and high flags. This will also clear the
* last stored alert values.
*/
void clearAlertStatus();
/**
* Enable or disable the Alert Flag functionality. If enabled,
* then when the measured value exceeds the low or high limits
* configured, the alert flag will be set. Use getAlertStatus()
* to access these values.
*
* @param enable if true, enables Alert Flag; otherwise, disables Alert Flag
*/
void enableAlertFlag(bool enable);
/**
* Enable or disable the Alert Pin functionality.
*
* @param enable if true, enables Alert Pin; otherwise, disables Alert Pin
*/
void enableAlertPin(bool enable);
/**
* Enable or disable the Alert Hold functionality. When Alert
* Hold is enabled, the alert status remains until manually
* cleared via clearAlertStatus(). Otherwise, the alert will self
* clear when the value moves into the defined limits if alerts
* are enabled via enableAlertFlag().
*
* @param enable if true, enables Alert Hold; otherwise, disables Alert Hold
*/
void enableAlertHold(bool enable);
/**
* If the Alert Pin fnctionality is enabled, define the active
* polarity of the pin in an alert condition. Enabling this sets
* the pin to active high in an alert condition, otherwise an
* active low is used.
*
* @param enable if true, Alert Pin is active high, else active low
*/
void enableAlertPinPolarityHigh(bool enable);
/**
* Enable or disable Automatic Conversion mode. When enabled, the
* ADC will sample and update the conversion value independently.
* This is disabled by default, and a conversion is only done by
* calling value().
*
* @param cycleTime set the Cycle Time for automatic conversion
*/
void setAutomaticConversion(ADC121C021_CYCLE_TIME_T cycleTime);
/**
* Set the Alert Low Limit. If Alerts are enabled and the
* measured conversion value is lower than this, an alert will be
* triggered.
*
* @param limit the Low Alert Limit
* @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
*/
mraa_result_t setAlertLowLimit(uint16_t limit);
/**
* Set the Alert High Limit. If Alerts are enabled and the
* measured conversion value is higher than this, an alert will be
* triggered.
*
* @param limit the High Alert Limit
* @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
*/
mraa_result_t setAlertHighLimit(uint16_t limit);
/**
* Set the Hysteresis value. If a high or low alert condition is
* triggered, the conversion result must move within the high or
* low limit by more than this value to clear the alert condition.
* If the Alert Hold bit is set, then the alert will not self
* clear regardless of this value.
*
* @param limit Hysteresis Limit
* @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
*/
mraa_result_t setHysteresis(uint16_t limit);
/**
* Return the Highest Conversion value sampled so far. This value
* is only updated by the converter when automatic conversion mode
* is enabled.
*
* @return the highest conversion value recorded
*/
uint16_t getHighestConversion();
/**
* Return the Lowest Conversion value sampled so far. This value
* is only updated by the converter when automatic conversion mode
* is enabled.
*
* @return the lowest conversion value recorded
*/
uint16_t getLowestConversion();
/**
* Clear the Highest Conversion value sampled so far.
*
* @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
*/
mraa_result_t clearHighestConversion();
/**
* Clear the Lowest Conversion value sampled so far.
*
* @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
*/
mraa_result_t clearLowestConversion();
private:
mraa_i2c_context m_i2c;
uint8_t m_addr;
float m_vref;
bool m_alertLow;
bool m_alertHigh;
};
}

View File

@ -0,0 +1,9 @@
%module jsupm_adc121c021
%include "../upm.i"
%include "../carrays_uint16_t.i"
%{
#include "adc121c021.h"
%}
%include "adc121c021.h"

View File

@ -0,0 +1,13 @@
%module pyupm_adc121c021
%include "../upm.i"
%feature("autodoc", "3");
#ifdef DOXYGEN
%include "adc121c021_doc.i"
#endif
%include "adc121c021.h"
%{
#include "adc121c021.h"
%}