From 1dd8626044914e24613808368fc35e7994e4bbce Mon Sep 17 00:00:00 2001 From: Kiveisha Yevgeniy Date: Wed, 11 Jun 2014 13:21:58 +0000 Subject: [PATCH] mma7455: added new accelerometer module Signed-off-by: Kiveisha Yevgeniy --- examples/CMakeLists.txt | 3 + examples/mma7455.cxx | 64 +++++++++++++ src/mma7455/CMakeLists.txt | 4 + src/mma7455/jsupm_mma7455.i | 7 ++ src/mma7455/mma7455.cxx | 176 ++++++++++++++++++++++++++++++++++ src/mma7455/mma7455.h | 223 ++++++++++++++++++++++++++++++++++++++++++++ src/mma7455/pyupm_mma7455.i | 8 ++ 7 files changed, 485 insertions(+) create mode 100644 examples/mma7455.cxx create mode 100644 src/mma7455/CMakeLists.txt create mode 100644 src/mma7455/jsupm_mma7455.i create mode 100644 src/mma7455/mma7455.cxx create mode 100644 src/mma7455/mma7455.h create mode 100644 src/mma7455/pyupm_mma7455.i diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 0b36feb..a483260 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -13,6 +13,7 @@ add_executable (son-hcsr04 hcsr04.cxx) add_executable (oled-1308 oled-1308.cxx) add_executable (oled-1327 oled-1327.cxx) add_executable (proximity max44000.cxx) +add_executable (accelerometer mma7455.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) @@ -24,6 +25,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/nrf24l01) include_directories (${PROJECT_SOURCE_DIR}/src/servo) include_directories (${PROJECT_SOURCE_DIR}/src/hcsr04) include_directories (${PROJECT_SOURCE_DIR}/src/max44000) +include_directories (${PROJECT_SOURCE_DIR}/src/mma7455) target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT}) @@ -40,3 +42,4 @@ target_link_libraries (son-hcsr04 hcsr04 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (oled-1308 i2clcd ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (oled-1327 i2clcd ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (proximity max44000 ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (accelerometer mma7455 ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/mma7455.cxx b/examples/mma7455.cxx new file mode 100644 index 0000000..052919f --- /dev/null +++ b/examples/mma7455.cxx @@ -0,0 +1,64 @@ +/* + * Author: Yevgeniy Kiveisha + * 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 +#include +#include "mma7455.h" +#include +#include + +int doWork = 0; +upm::MMA7455 *sensor = NULL; + +void +sig_handler(int signo) +{ + printf("got signal\n"); + if (signo == SIGINT) { + printf("exiting application\n"); + doWork = 1; + } +} + +int +main(int argc, char **argv) +{ + //! [Interesting] + sensor = new upm::MMA7455(0, ADDR); + + short x, y, z; + while (!doWork) { + sensor->readData(&x, &y, &z); + std::cout << "Accelerometer X(" << x << ") Y(" << y << ") Z(" << z << ")" << std::endl; + usleep (100000); + } + + //! [Interesting] + + std::cout << "exiting application" << std::endl; + + delete sensor; + + return 0; +} diff --git a/src/mma7455/CMakeLists.txt b/src/mma7455/CMakeLists.txt new file mode 100644 index 0000000..5c766ab --- /dev/null +++ b/src/mma7455/CMakeLists.txt @@ -0,0 +1,4 @@ +set (libname "mma7455") +set (libdecription "upm MMA7455") +add_library (mma7455 SHARED mma7455.cxx) +upm_module_init() diff --git a/src/mma7455/jsupm_mma7455.i b/src/mma7455/jsupm_mma7455.i new file mode 100644 index 0000000..bfa234b --- /dev/null +++ b/src/mma7455/jsupm_mma7455.i @@ -0,0 +1,7 @@ +%module jsupm_mma7455 + +%{ + #include "mma7455.h" +%} + +%include "mma7455.h" diff --git a/src/mma7455/mma7455.cxx b/src/mma7455/mma7455.cxx new file mode 100644 index 0000000..0471ec7 --- /dev/null +++ b/src/mma7455/mma7455.cxx @@ -0,0 +1,176 @@ +/* + * Author: Yevgeniy Kiveisha + * 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 +#include +#include +#include +#include +#include + +#include "mma7455.h" + +using namespace upm; + +MMA7455::MMA7455 (int bus, int devAddr) { + unsigned char data = 0; + int nBytes = 0; + + m_name = "MMA7455"; + + m_controlAddr = devAddr; + m_bus = bus; + + m_i2ControlCtx = maa_i2c_init(m_bus); + + maa_result_t error = maa_i2c_address(m_i2ControlCtx, m_controlAddr); + if (error != MAA_SUCCESS) { + fprintf(stderr, "Messed up i2c bus\n"); + return; + } + + // setting GLVL 0x1 (64LSB/g) and MODE 0x1 (Measurement Mode) + data = (BIT (MMA7455_GLVL0) | BIT (MMA7455_MODE0)); + error = ic2WriteReg (MMA7455_MCTL, &data, 0x1); + if (error != MAA_SUCCESS) { + std::cout << "ERROR :: MMA7455 instance wan not created (Mode)" << std::endl; + return; + } + + if (MAA_SUCCESS != calibrate ()) { + std::cout << "ERROR :: MMA7455 instance wan not created (Calibrate)" << std::endl; + return; + } +} + +MMA7455::~MMA7455() { + maa_i2c_stop(m_i2ControlCtx); +} + +maa_result_t +MMA7455::calibrate () { + maa_result_t error = MAA_SUCCESS; + int i = 0; + + accelData xyz; + xyz.value.x = xyz.value.y = xyz.value.z = 0; + + do { + error = readData (&xyz.value.x, &xyz.value.y, &xyz.value.z); + if (MAA_SUCCESS != error) { + return error; + } + + xyz.value.x += 2 * -xyz.value.x; + xyz.value.y += 2 * -xyz.value.y; + xyz.value.z += 2 * -(xyz.value.z - 64); + + error = ic2WriteReg (MMA7455_XOFFL, (unsigned char *) &xyz, 0x6); + if (error != MAA_SUCCESS) { + return error; + } + + } while ( ++i < 3 ); + + return error; +} + +maa_result_t +MMA7455::readData (short * ptrX, short * ptrY, short * ptrZ) { + accelData xyz; + unsigned char data = 0; + int nBytes = 0; + + /*do { + nBytes = ic2ReadReg (MMA7455_STATUS, &data, 0x1); + } while ( !(data & MMA7455_DRDY) && nBytes == MAA_SUCCESS); + + if (nBytes == MAA_SUCCESS) { + std::cout << "NO_GDB :: 1" << std::endl; + return MAA_SUCCESS; + }*/ + + nBytes = ic2ReadReg (MMA7455_XOUTL, (unsigned char *) &xyz, 0x6); + if (nBytes == 0) { + std::cout << "NO_GDB :: 2" << std::endl; + return MAA_ERROR_UNSPECIFIED; + } + + if (xyz.reg.x_msb & 0x02) { + xyz.reg.x_msb |= 0xFC; + } + + if (xyz.reg.y_msb & 0x02) { + xyz.reg.y_msb |= 0xFC; + } + + if (xyz.reg.z_msb & 0x02) { + xyz.reg.z_msb |= 0xFC; + } + + // The result is the g-force in units of 64 per 'g'. + *ptrX = xyz.value.x; + *ptrY = xyz.value.y; + *ptrZ = xyz.value.z; + + return MAA_SUCCESS; +} + +int +MMA7455::ic2ReadReg (unsigned char reg, unsigned char * buf, unsigned char size) { + if (MAA_SUCCESS != maa_i2c_address(m_i2ControlCtx, m_controlAddr)) { + return 0; + } + + if (MAA_SUCCESS != maa_i2c_write_byte(m_i2ControlCtx, reg)) { + return 0; + } + + if (MAA_SUCCESS != maa_i2c_address(m_i2ControlCtx, m_controlAddr)) { + return 0; + } + + return (int) maa_i2c_read(m_i2ControlCtx, buf, size); +} + +maa_result_t +MMA7455::ic2WriteReg (unsigned char reg, unsigned char * buf, unsigned char size) { + maa_result_t error = MAA_SUCCESS; + + uint8_t data[size + 1]; + data[0] = reg; + memcpy(&data[1], buf, size); + + error = maa_i2c_address (m_i2ControlCtx, m_controlAddr); + if (error != MAA_SUCCESS) { + return error; + } + error = maa_i2c_write (m_i2ControlCtx, data, size + 1); + if (error != MAA_SUCCESS) { + return error; + } + + return error; +} + diff --git a/src/mma7455/mma7455.h b/src/mma7455/mma7455.h new file mode 100644 index 0000000..ea9d255 --- /dev/null +++ b/src/mma7455/mma7455.h @@ -0,0 +1,223 @@ +/* + * Author: Yevgeniy Kiveisha + * 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 +#include + +#define ADDR 0x1D // device address + +// Register names according to the datasheet. +// Register 0x1C is sometimes called 'PW', and sometimes 'PD'. +// The two reserved registers can not be used. +#define MMA7455_XOUTL 0x00 // Read only, Output Value X LSB +#define MMA7455_XOUTH 0x01 // Read only, Output Value X MSB +#define MMA7455_YOUTL 0x02 // Read only, Output Value Y LSB +#define MMA7455_YOUTH 0x03 // Read only, Output Value Y MSB +#define MMA7455_ZOUTL 0x04 // Read only, Output Value Z LSB +#define MMA7455_ZOUTH 0x05 // Read only, Output Value Z MSB +#define MMA7455_XOUT8 0x06 // Read only, Output Value X 8 bits +#define MMA7455_YOUT8 0x07 // Read only, Output Value Y 8 bits +#define MMA7455_ZOUT8 0x08 // Read only, Output Value Z 8 bits +#define MMA7455_STATUS 0x09 // Read only, Status Register +#define MMA7455_DETSRC 0x0A // Read only, Detection Source Register +#define MMA7455_TOUT 0x0B // Temperature Output Value (Optional) +#define MMA7455_RESERVED1 0x0C // Reserved +#define MMA7455_I2CAD 0x0D // Read/Write, I2C Device Address +#define MMA7455_USRINF 0x0E // Read only, User Information (Optional) +#define MMA7455_WHOAMI 0x0F // Read only, "Who am I" value (Optional) +#define MMA7455_XOFFL 0x10 // Read/Write, Offset Drift X LSB +#define MMA7455_XOFFH 0x11 // Read/Write, Offset Drift X MSB +#define MMA7455_YOFFL 0x12 // Read/Write, Offset Drift Y LSB +#define MMA7455_YOFFH 0x13 // Read/Write, Offset Drift Y MSB +#define MMA7455_ZOFFL 0x14 // Read/Write, Offset Drift Z LSB +#define MMA7455_ZOFFH 0x15 // Read/Write, Offset Drift Z MSB +#define MMA7455_MCTL 0x16 // Read/Write, Mode Control Register +#define MMA7455_INTRST 0x17 // Read/Write, Interrupt Latch Reset +#define MMA7455_CTL1 0x18 // Read/Write, Control 1 Register +#define MMA7455_CTL2 0x19 // Read/Write, Control 2 Register +#define MMA7455_LDTH 0x1A // Read/Write, Level Detection Threshold Limit Value +#define MMA7455_PDTH 0x1B // Read/Write, Pulse Detection Threshold Limit Value +#define MMA7455_PD 0x1C // Read/Write, Pulse Duration Value +#define MMA7455_LT 0x1D // Read/Write, Latency Time Value (between pulses) +#define MMA7455_TW 0x1E // Read/Write, Time Window for Second Pulse Value +#define MMA7455_RESERVED2 0x1F // Reserved + +// Defines for the bits, to be able to change +// between bit number and binary definition. +// By using the bit number, programming the MMA7455 +// is like programming an AVR microcontroller. +// But instead of using "(1<