An access library for the MMA8452 accelerometer
Controlled by I2C
mma8452.h
Go to the documentation of this file.
1 
19 #ifndef _ACC_H_
20 #define _ACC_H_
21 
22 #include <stdio.h>
23 #include <pigpiod_if2.h>
24 #include <stdint.h>
25 #include <stdbool.h>
26 
31 #define I2C_FLAGS 0
32 
35 #define I2C_BUS 1
36 
40 #define MMA8452_ADDRESS 0x1c
41 
45 #define MMA8452_SUCCESS 0
46 
49 #define MMA8452_PIGPIO_INIT_FAILURE -1
50 
53 #define MMA8452_I2C_INIT_FAILURE -2
54 
57 #define MMA8452_NOT_FOUND -3
58 
61 #define MMA8452_INVALID_PAR -4
62 
65 #define MMA8452_NOT_STANDBY -5
66 
69 #define MMA8452_NOT_ACTIVE -6
70 
73 #define MMA8452_LAST_ERROR_CODE -6
74 
77 #define MMA8452_ID 0x2a
78 
79 #define MMA8452_STATUS 0x00
80 #define MMA8452_OUT_X_MSB 0x01
81 #define MMA8452_OUT_X_LSB 0x02
82 #define MMA8452_OUT_Y_MSB 0x03
83 #define MMA8452_OUT_Y_LSB 0x04
84 #define MMA8452_OUT_Z_MSB 0x05
85 #define MMA8452_OUT_Z_LSB 0x06
86 
87 #define MMA8452_SYSMOD 0x0b
88 #define MMA8452_INT_SOURCE 0x0c
89 #define MMA8452_WHO_AM_I 0x0d
90 #define MMA8452_XYZ_DATA_CFG 0x0e
91 #define MMA8452_HP_FILTER_CUTOFF 0x0f
92 #define MMA8452_PL_STATUS 0x10
93 #define MMA8452_PL_CFG 0x11
94 #define MMA8452_PL_COUNT 0x12
95 #define MMA8452_PL_BF_ZCOMP 0x13
96 #define MMA8452_P_L_THS_REG 0x14
97 #define MMA8452_FF_MT_CFG 0x15
98 #define MMA8452_FF_MT_SRC 0x16
99 #define MMA8452_FF_MT_THS 0x17
100 #define MMA8452_FF_MT_COUNT 0x18
101 
102 #define MMA8452_TRANSIENT_CFG 0x1d
103 #define MMA8452_TRANSIENT_SRC 0x1e
104 #define MMA8452_TRANSIENT_THS 0x1f
105 #define MMA8452_TRANSIENT_COUNT 0x20
106 #define MMA8452_PULSE_CFG 0x21
107 #define MMA8452_PULSE_SRC 0x22
108 #define MMA8452_PULSE_THSX 0x23
109 #define MMA8452_PULSE_THSY 0x24
110 #define MMA8452_PULSE_THSZ 0x25
111 #define MMA8452_PULSE_TMLT 0x26
112 #define MMA8452_PULSE_LTCY 0x27
113 #define MMA8452_PULSE_WIND 0x28
114 #define MMA8452_ASLP_COUNT 0x29
115 #define MMA8452_CTLR_REG1 0x2a
116 #define MMA8452_CTLR_REG2 0x2b
117 #define MMA8452_CTLR_REG3 0x2c
118 #define MMA8452_CTLR_REG4 0x2d
119 #define MMA8452_CTLR_REG5 0x2e
120 #define MMA8452_OFF_X 0x2f
121 #define MMA8452_OFF_Y 0x30
122 #define MMA8452_OFF_Z 0x31
123 
124 /* status register bits */
125 #define MMA8452_ZYXOW (0x01 << 7)
126 #define MMA8452_ZOW (0x01 << 6)
127 #define MMA8452_YOW (0x01 << 5)
128 #define MMA8452_XOW (0x01 << 4)
129 #define MMA8452_ZYXDR (0x01 << 3)
130 #define MMA8452_ZDR (0x01 << 2)
131 #define MMA8452_YDR (0x01 << 1)
132 #define MMA8452_XDR 0x01
133 
134 /* sysmod bits */
135 #define MMA8452_STANDBY 0x00
136 #define MMA8452_WAKE 0x01
137 #define MMA8452_SLEEP 0x02
138 
139 /* interrupt status register bits */
140 #define MMA8452_SRC_DRDY 0x01
141 #define MMA8452_SRC_FF_MT (0x01 << 2)
142 #define MMA8452_SRC_FF_PULSE (0x01 << 3)
143 #define MMA8452_SRC_FF_LNDPRT (0x01 << 4)
144 #define MMA8452_SRC_FF_TRANS (0x01 << 5)
145 #define MMA8452_SRC_FF_ASLP (0x01 << 7)
146 
147 /* XYZ data cfg register bits */
148 
149 #define MMA8452_FS_MASK 0x03
150 #define MMA8452_FULL_SCALE_2G 0x00
151 #define MMA8452_FULL_SCALE_4G 0x01
152 #define MMA8452_FULL_SCALE_8G 0x02
153 #define MMA8452_HFP_OUT (0x01 << 7)
154 
155 /* high pass filter cutoff bits */
156 #define MMA8452_SEL0 0x01
157 #define MMA8452_SEL1 (0x01 << 1)
158 #define MMA8452_PULSE_LPF_EN (0x01 << 4)
159 #define MMA8452_PULSE_HPF_BYP (0x01 << 5)
160 
161 /* portrait/landscape status register bits */
162 #define MMA8452_BAFRO 0x01
163 #define MMA8452_PORTRAIT_UP 0x00
164 #define MMA8452_PORTRAIT_DOWN (0x01 << 1)
165 #define MMA8452_PORTRAIT_UP 0x00
166 #define MMA8452_LANDSCAPE_RIGHT (0x02) << 1)
167 #define MMA8452_LANDSCAPE_LEFT (0x03) << 1)
168 
169 /* portrait/landscape configuration register bits */
170 #define MMA8452_PL_EN (0x01 << 6)
171 #define MMA8452_DBCNTM (0x01 << 7)
172 
173 /* ctrl reg 1 bits */
174 
175 #define MMA8452_ACTIVE 0x01
176 #define MMA8452_FREAD (0x01 << 1)
177 #define MMA8452_LNOISE (0x01 << 2)
178 #define MMA8452_DR_SHIFT 3
179 #define MMA8452_ASLP_RATE0 (0x01 << 6)
180 #define MMA8452_ASLP_RATE1 (0x01 << 7)
181 
182 /* sysmod reg bits */
183 #define MMA8452_SYSMOD_MASK 3
184 
185 /* pl status register bits */
186 #define MMA8542_LO_PA_MASK 0x06
187 
188 /* run mode */
189 #define MMA8542_MODE_ACTIVE 1
190 #define MMA8542_MODE_STANDBY 0
191 
192 void mma8452SetDebug(bool);
193 int mma8452Setup(void);
194 int mma8452Init(void);
195 int mma8452ReadXYZ(int16_t *);
196 int mma8452ReadXYZ_G(float *);
197 int mma8452GetActiveStandby(void);
198 int mma8452SetStandby();
199 int mma8452SetActive();
200 int mma8452GetRange(void);
201 int mma8452SetRange(int range);
202 bool mma8452GetFastRead(void);
203 int mma8452SetFastRead(bool onOff);
204 int mma8452GetDataRate(void);
205 int mma8452SetDataRate(int dataRate);
206 int mma8452GetMode(void);
207 int mma8452EnablePL(bool);
208 bool mma8452IsPLEnabled(void);
209 int mma8452GetOrientation(void);
210 char *mma8452Orientation2String(int);
211 char *mma8452DataRate2String(int dataRate);
212 char *mma8452DataErrorCode2String(int errorCode);
213 char *mma8452Mode2String(int mode);
214 char *mma8452Range2String(int range);
215 
216 #endif //_ACC_H_
217 
int mma8452SetFastRead(bool onOff)
Sets or removes mma8452 fast read mode. In fast read mode only the 8 most significant bits are read o...
Definition: mma8452.c:338
int mma8452GetRange(void)
Gets the dynamic range The mma8452 has 3 dynamic ranges: 2g, 4g, 8g full scale.
Definition: mma8452.c:360
int mma8452ReadXYZ_G(float *)
Function to read X,Y,Z axes values in units of g. This calls mma8452ReadXYZ and calculates the corres...
Definition: mma8452.c:234
int mma8452GetMode(void)
Gets the running mode.
Definition: mma8452.c:488
int mma8452SetDataRate(int dataRate)
Sets the data rate code.
Definition: mma8452.c:447
char * mma8452Range2String(int range)
Converts the dynamic range code into a humanly readable text.
Definition: mma8452.c:409
int mma8452SetStandby()
Sets the mma8452 to standby mode. In this mode configuration registers can be programmed.
Definition: mma8452.c:562
int mma8452GetOrientation(void)
Get a code for the orientation.
Definition: mma8452.c:610
void mma8452SetDebug(bool)
switching debug messages on or off
Definition: mma8452.c:61
int mma8452SetActive()
Sets the mma8452 to active mode. In this mode acceleration data are aquired.
Definition: mma8452.c:553
char * mma8452Mode2String(int mode)
Converts the running mode code into a humanly readable string.
Definition: mma8452.c:500
int mma8452SetRange(int range)
Sets the dynamic range The mma8452 has 3 dynamic ranges: 2g, 4g, 8g full scale.
Definition: mma8452.c:374
int mma8452GetActiveStandby(void)
Checks if the mma8452 is in standby or active mode.
Definition: mma8452.c:515
char * mma8452DataErrorCode2String(int errorCode)
Converts the error code into a humanly readable text.
Definition: mma8452.c:473
int mma8452Setup(void)
Setup the accelerometer on the I2C bus of the RPi Opens the connection to the pigoio daemon...
Definition: mma8452.c:70
int mma8452ReadXYZ(int16_t *)
Function to read raw X,Y,Z acceleration values. The mma8452 is put into active mode.
Definition: mma8452.c:144
char * mma8452Orientation2String(int)
Converts the orientation code into a humanly readable string.
Definition: mma8452.c:624
char * mma8452DataRate2String(int dataRate)
Converts the data rate code into a humanly readable text.
Definition: mma8452.c:422
int mma8452GetDataRate(void)
Gets the data rate code.
Definition: mma8452.c:434
int mma8452Init(void)
Checks if the accelerometer&#39;s device WHO_AM_I register shows the right value of 0x2a.
Definition: mma8452.c:116
bool mma8452GetFastRead(void)
Checks if the mma8452 is in fast read mode. In fast read mode only the 8 most significant bits are re...
Definition: mma8452.c:321
int mma8452EnablePL(bool)
Enables or disables Portrait / Landscape mode. PL mode allows to find out if the device is horizontal...
Definition: mma8452.c:572
bool mma8452IsPLEnabled(void)
Checks if Portrait / Landscape mode is enabled. PL mode allows to find out if the device is horizonta...
Definition: mma8452.c:594