Nut/OS  5.0.5
API Reference
mma745x.h
Go to the documentation of this file.
00001 #ifndef _DEV_MMA745X_H_
00002 #define _DEV_MMA745X_H_
00003 /*
00004  * Copyright (C) 2010 by Rittal GmbH & Co. KG,
00005  * Dawid Sadji <sadji.d@rittal.de> All rights reserved.
00006  * Ulrich Prinz <prinz.u@rittal.de> All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  *
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer.
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in the
00016  *    documentation and/or other materials provided with the distribution.
00017  * 3. Neither the name of the copyright holders nor the names of
00018  *    contributors may be used to endorse or promote products derived
00019  *    from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY EMBEDDED IT AND CONTRIBUTORS
00022  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EMBEDDED IT
00025  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00026  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00027  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00028  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00029  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00030  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00031  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032  *
00033  * For additional information see http://www.ethernut.de/
00034  *
00035  */
00036 
00037 /*
00038  * \file dev/mma745x.h
00039  * \brief Driver for Freescale MMA745x velocity sensor.
00040  *
00041  * \verbatim
00042  * $Id$
00043  * \endverbatim
00044  */
00045 
00046 #ifndef I2C_SLA_MMA745x
00047 #define I2C_SLA_MMA745x     0x1D
00048 #endif
00049 
00050 /*
00051  * MMA745x Register Map.
00052  */
00053 #define MMA745x_REG_XOUTL       0x00        
00054 #define MMA745x_REG_XOUTH       0x01        
00055 #define MMA745x_REG_YOUTL       0x02        
00056 #define MMA745x_REG_YOUTH       0x03        
00057 #define MMA745x_REG_ZOUTL       0x04        
00058 #define MMA745x_REG_ZOUTH       0x05        
00060 #define MMA745x_REG_XOUT8       0x06        
00061 #define MMA745x_REG_YOUT8       0x07        
00062 #define MMA745x_REG_ZOUT8       0x08        
00064 #define MMA745x_REG_STATUS      0x09        
00065 #define MMA745x_REG_DETSRC      0x0A        
00067 #define MMA745x_REG_TOUT        0x0B        
00068 #define MMA745x_REG_I2CAD       0x0D        
00069 #define MMA745x_REG_USRINF      0x0E        
00070 #define MMA745x_REG_WHOAMI      0x0E        
00072 #define MMA745x_REG_XOFFL       0x10        
00073 #define MMA745x_REG_XOFFH       0x11        
00074 #define MMA745x_REG_YOFFL       0x12        
00075 #define MMA745x_REG_YOFFH       0x13        
00076 #define MMA745x_REG_ZOFFL       0x14        
00077 #define MMA745x_REG_ZOFFH       0x15        
00079 #define MMA745x_REG_MCTL        0x16        
00080 #define MMA745x_REG_INTRST      0x17        
00081 #define MMA745x_REG_CTL1        0x18        
00082 #define MMA745x_REG_CTL2        0x19        
00083 #define MMA745x_REG_LDTH        0x1A        
00084 #define MMA745x_REG_PDTH        0x1B        
00085 #define MMA745x_REG_PW          0x1C        
00086 #define MMA745x_REG_LT          0x1D        
00087 #define MMA745x_REG_TW          0x1E        
00089 /*
00090  * MMA745x Status Register definition.
00091  * This register is read only.
00092  */
00093 #define MMA745X_STATUS_DRDY     (1 << 0)    
00094 #define MMA745X_STATUS_DOVR     (1 << 1)    
00095 #define MMA745X_STATUS_PERR     (1 << 2)    
00097 /*
00098  * MMA745x Detection Source Register definition.
00099  * This register is read only.
00100  */
00101 #define MMA745x_DETSRC_INT1     (1 << 0)    
00102 #define MMA745x_DETSRC_INT2     (1 << 1)    
00103 #define MMA745x_DETSRC_PDZ      (1 << 2)    
00104 #define MMA745x_DETSRC_PDY      (1 << 3)    
00105 #define MMA745x_DETSRC_PDX      (1 << 4)    
00106 #define MMA745x_DETSRC_LDZ      (1 << 5)    
00107 #define MMA745x_DETSRC_LDY      (1 << 6)    
00108 #define MMA745x_DETSRC_LDX      (1 << 7)    
00110 /*
00111  * MMA745x I2C Address Register definition.
00112  * This register is read only, bit 7 is read/write.
00113  */
00114 #define MMA745x_I2CAD_I2CDIS    (1 << 7)    
00115 #define MMA745x_I2CAD_I2CMSK    (0x7F)      
00117 /*
00118  * MMA745x Mode Register definitions.
00119  * This register is read/write.
00120  */
00121 #define MMA745X_MCTL_STBY       (0x00 << 0) 
00122 #define MMA745X_MCTL_MEAS       (0x01 << 0) 
00123 #define MMA745X_MCTL_LVL        (0x02 << 0) 
00124 #define MMA745X_MCTL_PLS        (0x03 << 0) 
00125 #define MMA745X_MCTL_MSK        (0x03 << 0) 
00127 #define MMA745X_MCTL_GLVL_8G    (0x00 << 2) 
00128 #define MMA745X_MCTL_GLVL_4G    (0x02 << 2) 
00129 #define MMA745X_MCTL_GLVL_2G    (0x01 << 2) 
00130 #define MMA745X_MCTL_GLVL_MSK   (0x03 << 2) 
00132 #define MMA745X_MCTL_STON       (1 << 4)    
00133 #define MMA745X_MCTL_SPI3W      (1 << 5)    
00134 #define MMA745X_MCTL_DRPD       (1 << 6)    
00136 /*
00137  * MMA745x Interrupt Reset Register definitions.
00138  * This register is read/write.
00139  */
00140 #define MMA745x_INTRST_CLRINT1  (1 << 0)    
00141 #define MMA745x_INTRST_CLRINT2  (1 << 1)    
00142 #define MMA745x_INTRST_MSK      (MMA745x_INTRST_CLRINT1 | MMA745x_INTRST_CLRINT2)
00143 /*
00144  * MMA745x Control Register 1 definitions.
00145  * This register is read/write.
00146  */
00147 #define MMA745x_CTL1_INTREV     (1 << 0)    
00149 #define MMA745x_CTL1_L1P2       (0x00 << 1) 
00150 #define MMA745x_CTL1_P1L2       (0x01 << 1) 
00151 #define MMA745x_CTL1_P1P2       (0x02 << 1) 
00152 #define MMA745x_CTL1_XDA        (1 << 3)    
00153 #define MMA745x_CTL1_YDA        (1 << 4)    
00154 #define MMA745x_CTL1_ZDA        (1 << 5)    
00155 #define MMA745x_CTL1_THOPT      (1 << 6)    
00156 #define MMA745x_CTL1_DFBW       (1 << 7)    
00158 /*
00159  * MMA745x Control Register 2 definitions.
00160  * This register is read/write.
00161  */
00162 #define MMA745x_CTL2_LDPL       (1 << 0)    
00164 #define MMA745x_CTL2_PDPL       (1 << 1)    
00166 #define MMA745x_CTL2_DRVO       (1 << 2)    
00168 /*
00169  * MMA745x Level Detection Threshold Limit Value.
00170  * This register is read/write.
00171  *
00172  * This register contains the threshold value for level detection.
00173  * If THOPT in CTL1 is 0 it is an unsigned 7 bit value and bit 7 should be 0.
00174  * If THOPT in CTL1 is 1 it is an signed 8 bit value.
00175  */
00176 #define MMA745x_LDTH_SMSK       0x7F        
00178 /*
00179  * MMA745x Pulse Detection Threshold Limit Value.
00180  * This register is read/write.
00181  *
00182  * This register contains the threshold value for pulse detection.
00183  * This is an unsigned 7 bit value and bit 7 should be 0.
00184  */
00185 #define MMA745x_PDTH_SMSK       0x7F        
00187 /*
00188  * MMA745x Pulse Duration Value.
00189  * This register is read/write.
00190  *
00191  * This register contains the pulse duration value.
00192  * This is an unsigned 8 bit value in 0.5ms steps.
00193  */
00194 #define MMA745x_PW_MSK          0xFF        
00196 /*
00197  * MMA745x Latency Time Value.
00198  * This register is read/write.
00199  *
00200  * This register contains the latency time for pulse detection.
00201  * This is an unsigned 8 bit value in 1ms steps.
00202  */
00203 #define MMA745x_LT_MSK          0xFF        
00205 /*
00206  * MMA745x Double Pulse Detection Time Window Value.
00207  * This register is read/write.
00208  *
00209  * This register contains time window for double pulse detection.
00210  * This is an unsigned 8 bit value in 1ms steps.
00211  */
00212 #define MMA745x_TW_MSK          0xFF        
00214 #include <cfg/mma745x.h>
00215 
00216 #ifndef MMA745X_RANGE
00217 #define MMA745X_RANGE   MMA745X_MCTL_GLVL_8G
00218 #endif
00219 
00220 #ifndef MMA74xx_MODE
00221 #define MMA74xx_MODE (MMA745X_MCTL_MEAS | MMA745X_RANGE)
00222 #endif
00223 
00226 typedef struct __attribute__ ((packed))
00227 {
00228     int16_t x;
00229     int16_t y;
00230     int16_t z;
00231 } mma10bit_t;
00232 
00235 typedef struct __attribute__ ((packed))
00236 {
00237     int8_t x;
00238     int8_t y;
00239     int8_t z;
00240 } mma8bit_t;
00241 
00244 typedef struct __attribute__ ((packed))
00245 {
00246     uint8_t rMODE;
00247     uint8_t rINTRST;
00248     uint8_t rCONTROL1;
00249     uint8_t rCONTROL2;
00250     uint8_t rLEVEL;
00251     uint8_t rPVALUE;
00252     uint8_t rPDUR;
00253     uint8_t rLATTV;
00254     uint8_t rTW;
00255 } mmaInit_t;
00256 
00259 typedef struct __attribute__ ((packed))
00260 {
00261     uint8_t state;
00262     uint8_t detsrc;
00263 } mmaState_t;
00264 
00265 /* brief MMA745x control function calls.
00266  */
00267 #define MMA_GET_STATE   0
00268 #define MMA_SET_MODE    1
00269 #define MMA_GET_IRQ     2
00270 #define MMA_SET_IRQ     3
00271 #define MMA_CLR_IRQ     4
00272 
00273 /* Low Level Access Functions */
00274 int Mma745xWrite( uint_fast8_t reg, void *val, size_t len);
00275 int Mma745xRead( uint_fast8_t reg, void *val, size_t len);
00276 
00277 /* Raw Value Access Functions */
00278 int Mma745xReadVal8( mma8bit_t *val);
00279 int Mma745xReadVal10( uint8_t ofs, mma10bit_t *val);
00280 
00281 /* g Value Access Functions */
00282 int Mma745xReadG( mma10bit_t *val);
00283 
00284 /* Calibration Value Access Functions */
00285 int Mma745xReadCal( mma10bit_t *cal);
00286 int Mma745xWriteCal( mma10bit_t *cal);
00287 
00288 /* Control Function */
00289 int Mma745xCtl( uint_fast8_t fkt, void *val);
00290 
00291 /* Startup Initialization */
00292 int Mma745xInit( uint_fast8_t selftest, mmaInit_t *init);
00293 
00294 #endif /* _DEV_MMA745X_H_ */