From ad36f838578412fbe59a030e6b26338e7a9fcf6d Mon Sep 17 00:00:00 2001 From: Samuli Rissanen Date: Fri, 23 Feb 2018 13:17:37 +0200 Subject: [PATCH] kx122: Moved definitions, documentation tweaks Signed-off-by: Samuli Rissanen Signed-off-by: Noel Eck --- src/kx122/kx122.c | 58 +++++++++++++++++++++++++++++++-- src/kx122/kx122.cxx | 6 ++-- src/kx122/kx122.h | 79 ++++++++++++++++----------------------------- src/kx122/kx122.hpp | 28 +++++++++++----- 4 files changed, 105 insertions(+), 66 deletions(-) diff --git a/src/kx122/kx122.c b/src/kx122/kx122.c index 387ef506..d8141f21 100644 --- a/src/kx122/kx122.c +++ b/src/kx122/kx122.c @@ -24,7 +24,51 @@ #include "kx122.h" +//Used to set the bit required for SPI reading +#define SPI_READ 0x80 +//Used to mask the read bit required for SPI writing +#define SPI_WRITE 0x7F + +//Used to determine amount of samples in the buffer. +#define LOW_RES_SAMPLE_MODIFIER 3 +#define HIGH_RES_SAMPLE_MODIFIER 6 + +//Mask for software reset +#define KX122_CNTL2_SRST_MASK 0x80 + +//Masks for interrupt control registers +#define KX122_INC1_MASK 0xFB +#define KX122_INC4_MASK 0xF7 +#define KX122_INC5_MASK 0xFB +#define KX122_INC6_MASK 0xF7 + +//Acceleration data buffer length +#define BUFFER_LEN 6 + +//Acceleration per decimal value for each acceleration ranges +#define RANGE_2G_G 0.00006f +#define RANGE_4G_G 0.00012f +#define RANGE_8G_G 0.00024f + +//Acceleration scaling between high and low resolution modes +#define RANGE_RES_SCALE 260 + +//Maximum loop iterations, used in software reset and self testing +#define MAX_LOOP_COUNT 100 + +//Sensor self-test defines +#define SELF_TEST_LOOP_WAIT_TIME 10000 +#define SELF_TEST_MAX_DIFFERENCE 0.75f +#define SELF_TEST_MIN_XY_DIFFERENCE 0.25f +#define SELF_TEST_MIN_Z_DIFFERENCE 0.20f + +//Maximum amount of samples in the buffer for high and low resolutions +#define MAX_BUFFER_SAMPLES_LOW_RES 681 +#define MAX_BUFFER_SAMPLES_HIGH_RES 340 + +//Earth gravity constant (m/s^2) +#define GRAVITY 9.81f /** Enables the chip select on the chip_select GPIO pin. Chip select is active low. @@ -820,10 +864,20 @@ upm_result_t kx122_clear_interrupt(const kx122_context dev) return kx122_read_register(dev,KX122_INT_REL,®_val); } -upm_result_t kx122_get_interrupt_status(const kx122_context dev, uint8_t *data) +bool kx122_get_interrupt_status(const kx122_context dev) { assert(dev != NULL); - return kx122_read_register(dev,KX122_STATUS_REG,data); + uint8_t reg_val; + + if(kx122_read_register(dev,KX122_STATUS_REG,®_val) != UPM_SUCCESS){ + printf("%s: kx122_read_register() failed.\n", __FUNCTION__); + return false; + } + + if(reg_val != 0x00){ + return true; + } + return false; } upm_result_t kx122_enable_buffer(const kx122_context dev) diff --git a/src/kx122/kx122.cxx b/src/kx122/kx122.cxx index 57f61e33..669a61ae 100644 --- a/src/kx122/kx122.cxx +++ b/src/kx122/kx122.cxx @@ -201,11 +201,9 @@ void KX122::routeInterrupt2(uint8_t bits) } } -void KX122::getInterruptStatus(uint8_t *data) +bool KX122::getInterruptStatus() { - if(kx122_get_interrupt_status(m_kx122,data)){ - throw std::runtime_error(std::string(__FUNCTION__) + "kx122_get_interrupt_status failed"); - } + return kx122_get_interrupt_status(m_kx122); } void KX122::getInterruptSource(uint8_t *data) diff --git a/src/kx122/kx122.h b/src/kx122/kx122.h index 1a5fdb8e..235bc540 100644 --- a/src/kx122/kx122.h +++ b/src/kx122/kx122.h @@ -22,6 +22,10 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#ifdef __cplusplus +extern "C"{ +#endif + #include #include #include @@ -30,7 +34,7 @@ #include #include -#include +#include #include "kx122_registers.h" @@ -42,12 +46,6 @@ * @include kx122.c */ -//Used to set the bit required for SPI reading -#define SPI_READ 0x80 - -//Used to mask the read bit required for SPI writing -#define SPI_WRITE 0x7F - //Frequency of the SPI connection #define SPI_FREQUENCY 10000 @@ -55,46 +53,6 @@ #define KX122_DEFAULT_SLAVE_ADDR_1 0x1F #define KX122_DEFAULT_SLAVE_ADDR_2 0x1E -//Masks -#define KX122_CNTL2_SRST_MASK 0x80 - -//Used to determine amount of samples in the buffer. -#define LOW_RES_SAMPLE_MODIFIER 3 -#define HIGH_RES_SAMPLE_MODIFIER 6 - -//Masks for interrupt control registers -#define KX122_INC1_MASK 0xFB -#define KX122_INC4_MASK 0xF7 -#define KX122_INC5_MASK 0xFB -#define KX122_INC6_MASK 0xF7 - -//Acceleration data buffer length -#define BUFFER_LEN 6 - -//Acceleration per decimal value for each acceleration ranges -#define RANGE_2G_G 0.00006f -#define RANGE_4G_G 0.00012f -#define RANGE_8G_G 0.00024f - -//Acceleration scaling between high and low resolution modes -#define RANGE_RES_SCALE 260 - -//Maximum loop iterations -#define MAX_LOOP_COUNT 100 - -//Sensor self-test defines -#define SELF_TEST_LOOP_WAIT_TIME 10000 -#define SELF_TEST_MAX_DIFFERENCE 0.75f -#define SELF_TEST_MIN_XY_DIFFERENCE 0.25f -#define SELF_TEST_MIN_Z_DIFFERENCE 0.20f - -//Maximum amount of samples in the buffer for high and low resolutions -#define MAX_BUFFER_SAMPLES_LOW_RES 681 -#define MAX_BUFFER_SAMPLES_HIGH_RES 340 - -//Earth gravity constant (m/s^2) -#define GRAVITY 9.81f - //Microseconds in a second #define MICRO_S 1000000 @@ -207,6 +165,8 @@ KX122 initialization Set addr to -1 if using SPI. When using I2C, set chip_select_pin to -1; +If no errors occur, the device gets initialized with default values and gets set to active state. + @param bus I2C or SPI bus to use. @param addr I2C address of the sensor. @param chip_select Chip select pin for SPI. @@ -317,6 +277,9 @@ simulating input acceleration. The test compares samples from all axis before an applying the electrostatic force to the sensor. If the amount of acceleration increases according to the values defined in TABLE 1 of the datasheet, the test passes. +The function prints out the minimum, maximum and values calculated during the test +for each axis, and the result of the test for each axis. + See the datasheet for more information. @param dev The device context. @@ -341,9 +304,9 @@ Sets the sensor to active mode. upm_result_t kx122_set_sensor_active(const kx122_context dev); /** -Sets the data sampling rate of the sensor. +Sets the ODR of the sensor. -Sensor needs to be in standby mode when setting the data sampling rate. +Sensor needs to be in standby mode when setting the ODR. @param dev The device context. @param odr One of the KX122_ODR_T values. @@ -367,6 +330,9 @@ Sets the resolution of the sensor. High resolution (16 bits) or low resolution ( Sensor needs to be in standby mode when setting the sensor resolution. +When sensor is set to low resolution mode, the sensor runs in low power mode, which in turn +enables features such as averaging.(kx122_set_average()). + @param dev The device context. @param res One of the KX122_RES_T values. @return UPM result. @@ -500,13 +466,18 @@ Gets the status of the interrupts. (Has an interrupt occured) See datasheet for more details. @param dev The device context. -@param data Pointer to a uint8_t variable to store the value. -@return UPM result. +@return Return true if an interrupt event has occured, returns false if no interrupts have occured. */ -upm_result_t kx122_get_interrupt_status(const kx122_context dev, uint8_t *data); +bool kx122_get_interrupt_status(const kx122_context dev); /** Gets the source of the interrupt. +The value stored is one or more of the KX122_INTERRUPT_T values, depending on the interrupts +that have occured. + +If multiple interrupts have occured, and you need to determine a specific interrupt, +you can use masking to get the state of the specific interrupt: +(int_source & KX122_DATA_READY_INT) == KX122_DATA_READY_INT) See datasheet for more details. @@ -694,3 +665,7 @@ Clears the buffer, removing all existing samples from the buffer. @return UPM result. */ upm_result_t kx122_clear_buffer(const kx122_context dev); + +#ifdef __cplusplus +} +#endif diff --git a/src/kx122/kx122.hpp b/src/kx122/kx122.hpp index 9bacf3bd..d61c12e5 100644 --- a/src/kx122/kx122.hpp +++ b/src/kx122/kx122.hpp @@ -24,9 +24,7 @@ #include -extern "C"{ - #include "kx122.h" -} +#include "kx122.h" /** * @file kx122.hpp @@ -46,6 +44,8 @@ namespace upm{ Set addr to -1 if using SPI. When using I2C, set chip_select_pin to -1 + If no errors occur, the device gets initialized with default values and gets set to active state. + @param bus I2C or SPI bus to use. @param addr I2C address of the sensor. @param chip_select Chip select pin for SPI. @@ -141,6 +141,9 @@ namespace upm{ applying the electrostatic force to the sensor. If the amount of acceleration increases according to the values defined in TABLE 1 of the datasheet, the test passes. + The function prints out the minimum, maximum and values calculated during the test + for each axis, and the result of the test for each axis. + See the datasheet for more information. @throws std::runtime_error on failure. @@ -162,9 +165,9 @@ namespace upm{ void setSensorActive(); /** - Sets the data sampling rate of the sensor. + Sets the ODR of the sensor. - Sensor needs to be in standby mode when setting the data sampling rate. + Sensor needs to be in standby mode when setting the ODR. @param odr One of the KX122_ODR_T values. @throws std::runtime_error on failure. @@ -186,6 +189,9 @@ namespace upm{ Sensor needs to be in standby mode when setting the sensor resolution. + When sensor is set to low resolution mode, the sensor runs in low power mode, which in turn + enables features such as averaging.(setAverage()). + @param res One of the KX122_RES_T values. @throws std::runtime_error on failure. */ @@ -308,14 +314,20 @@ namespace upm{ See datasheet for more details. - @param data Pointer to a uint8_t variable to store the value. - @throws std::runtime_error on failure. + @return Return true if an interrupt event has occured, returns false if no interrupts have occured. */ - void getInterruptStatus(uint8_t *data); + bool getInterruptStatus(); /** Gets the source of the interrupt. + The value stored is one or more of the KX122_INTERRUPT_T values, depending on the interrupts + that have occured. + + If multiple interrupts have occured, and you need to determine a specific interrupt, + you can use masking to get the state of the specific interrupt: + (int_source & KX122_DATA_READY_INT) == KX122_DATA_READY_INT) + See datasheet for more details. @param data Pointer to a uint8_t variable to store the value.