An access library for the MMA8452 accelerometer
Controlled by I2C
Macros | Functions
mma8452.h File Reference

Header file for all defined constants and function prototypes. More...

#include <stdio.h>
#include <pigpiod_if2.h>
#include <stdint.h>
#include <stdbool.h>
Include dependency graph for mma8452.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define I2C_FLAGS   0
 I2C Flags are not used but must be given as a parameter to the pigio call i2c_open.
 
#define I2C_BUS   1
 I2C bus number.
 
#define MMA8452_ADDRESS   0x1c
 I2C device address.
 
#define MMA8452_SUCCESS   0
 error code for success
 
#define MMA8452_PIGPIO_INIT_FAILURE   -1
 error code when we cannot contact the pigpio daemon
 
#define MMA8452_I2C_INIT_FAILURE   -2
 error code when we cannot open the I2C bus
 
#define MMA8452_NOT_FOUND   -3
 error code if no mma8452 chip is connected to the I2C bus
 
#define MMA8452_INVALID_PAR   -4
 error code for an invalid parameter
 
#define MMA8452_NOT_STANDBY   -5
 error code if the mma8452 is not in standby when it should be
 
#define MMA8452_NOT_ACTIVE   -6
 error code if the mma8452 is not in active mode when it should be
 
#define MMA8452_LAST_ERROR_CODE   -6
 the last error code
 
#define MMA8452_ID   0x2a
 the mma8452 ID code
 
#define MMA8452_STATUS   0x00
 
#define MMA8452_OUT_X_MSB   0x01
 
#define MMA8452_OUT_X_LSB   0x02
 
#define MMA8452_OUT_Y_MSB   0x03
 
#define MMA8452_OUT_Y_LSB   0x04
 
#define MMA8452_OUT_Z_MSB   0x05
 
#define MMA8452_OUT_Z_LSB   0x06
 
#define MMA8452_SYSMOD   0x0b
 
#define MMA8452_INT_SOURCE   0x0c
 
#define MMA8452_WHO_AM_I   0x0d
 
#define MMA8452_XYZ_DATA_CFG   0x0e
 
#define MMA8452_HP_FILTER_CUTOFF   0x0f
 
#define MMA8452_PL_STATUS   0x10
 
#define MMA8452_PL_CFG   0x11
 
#define MMA8452_PL_COUNT   0x12
 
#define MMA8452_PL_BF_ZCOMP   0x13
 
#define MMA8452_P_L_THS_REG   0x14
 
#define MMA8452_FF_MT_CFG   0x15
 
#define MMA8452_FF_MT_SRC   0x16
 
#define MMA8452_FF_MT_THS   0x17
 
#define MMA8452_FF_MT_COUNT   0x18
 
#define MMA8452_TRANSIENT_CFG   0x1d
 
#define MMA8452_TRANSIENT_SRC   0x1e
 
#define MMA8452_TRANSIENT_THS   0x1f
 
#define MMA8452_TRANSIENT_COUNT   0x20
 
#define MMA8452_PULSE_CFG   0x21
 
#define MMA8452_PULSE_SRC   0x22
 
#define MMA8452_PULSE_THSX   0x23
 
#define MMA8452_PULSE_THSY   0x24
 
#define MMA8452_PULSE_THSZ   0x25
 
#define MMA8452_PULSE_TMLT   0x26
 
#define MMA8452_PULSE_LTCY   0x27
 
#define MMA8452_PULSE_WIND   0x28
 
#define MMA8452_ASLP_COUNT   0x29
 
#define MMA8452_CTLR_REG1   0x2a
 
#define MMA8452_CTLR_REG2   0x2b
 
#define MMA8452_CTLR_REG3   0x2c
 
#define MMA8452_CTLR_REG4   0x2d
 
#define MMA8452_CTLR_REG5   0x2e
 
#define MMA8452_OFF_X   0x2f
 
#define MMA8452_OFF_Y   0x30
 
#define MMA8452_OFF_Z   0x31
 
#define MMA8452_ZYXOW   (0x01 << 7)
 
#define MMA8452_ZOW   (0x01 << 6)
 
#define MMA8452_YOW   (0x01 << 5)
 
#define MMA8452_XOW   (0x01 << 4)
 
#define MMA8452_ZYXDR   (0x01 << 3)
 
#define MMA8452_ZDR   (0x01 << 2)
 
#define MMA8452_YDR   (0x01 << 1)
 
#define MMA8452_XDR   0x01
 
#define MMA8452_STANDBY   0x00
 
#define MMA8452_WAKE   0x01
 
#define MMA8452_SLEEP   0x02
 
#define MMA8452_SRC_DRDY   0x01
 
#define MMA8452_SRC_FF_MT   (0x01 << 2)
 
#define MMA8452_SRC_FF_PULSE   (0x01 << 3)
 
#define MMA8452_SRC_FF_LNDPRT   (0x01 << 4)
 
#define MMA8452_SRC_FF_TRANS   (0x01 << 5)
 
#define MMA8452_SRC_FF_ASLP   (0x01 << 7)
 
#define MMA8452_FS_MASK   0x03
 
#define MMA8452_FULL_SCALE_2G   0x00
 
#define MMA8452_FULL_SCALE_4G   0x01
 
#define MMA8452_FULL_SCALE_8G   0x02
 
#define MMA8452_HFP_OUT   (0x01 << 7)
 
#define MMA8452_SEL0   0x01
 
#define MMA8452_SEL1   (0x01 << 1)
 
#define MMA8452_PULSE_LPF_EN   (0x01 << 4)
 
#define MMA8452_PULSE_HPF_BYP   (0x01 << 5)
 
#define MMA8452_BAFRO   0x01
 
#define MMA8452_PORTRAIT_UP   0x00
 
#define MMA8452_PORTRAIT_DOWN   (0x01 << 1)
 
#define MMA8452_PORTRAIT_UP   0x00
 
#define MMA8452_LANDSCAPE_RIGHT   (0x02) << 1)
 
#define MMA8452_LANDSCAPE_LEFT   (0x03) << 1)
 
#define MMA8452_PL_EN   (0x01 << 6)
 
#define MMA8452_DBCNTM   (0x01 << 7)
 
#define MMA8452_ACTIVE   0x01
 
#define MMA8452_FREAD   (0x01 << 1)
 
#define MMA8452_LNOISE   (0x01 << 2)
 
#define MMA8452_DR_SHIFT   3
 
#define MMA8452_ASLP_RATE0   (0x01 << 6)
 
#define MMA8452_ASLP_RATE1   (0x01 << 7)
 
#define MMA8452_SYSMOD_MASK   3
 
#define MMA8542_LO_PA_MASK   0x06
 
#define MMA8542_MODE_ACTIVE   1
 
#define MMA8542_MODE_STANDBY   0
 

Functions

void mma8452SetDebug (bool)
 switching debug messages on or off More...
 
int mma8452Setup (void)
 Setup the accelerometer on the I2C bus of the RPi Opens the connection to the pigoio daemon. More...
 
int mma8452Init (void)
 Checks if the accelerometer's device WHO_AM_I register shows the right value of 0x2a. More...
 
int mma8452ReadXYZ (int16_t *)
 Function to read raw X,Y,Z acceleration values. The mma8452 is put into active mode. More...
 
int mma8452ReadXYZ_G (float *)
 Function to read X,Y,Z axes values in units of g. This calls mma8452ReadXYZ and calculates the corresponding floating point values in units if g. More...
 
int mma8452GetActiveStandby (void)
 Checks if the mma8452 is in standby or active mode. More...
 
int mma8452SetStandby ()
 Sets the mma8452 to standby mode. In this mode configuration registers can be programmed. More...
 
int mma8452SetActive ()
 Sets the mma8452 to active mode. In this mode acceleration data are aquired. More...
 
int mma8452GetRange (void)
 Gets the dynamic range The mma8452 has 3 dynamic ranges: 2g, 4g, 8g full scale. More...
 
int mma8452SetRange (int range)
 Sets the dynamic range The mma8452 has 3 dynamic ranges: 2g, 4g, 8g full scale. More...
 
bool mma8452GetFastRead (void)
 Checks if the mma8452 is in fast read mode. In fast read mode only the 8 most significant bits are read out. More...
 
int mma8452SetFastRead (bool onOff)
 Sets or removes mma8452 fast read mode. In fast read mode only the 8 most significant bits are read out. More...
 
int mma8452GetDataRate (void)
 Gets the data rate code. More...
 
int mma8452SetDataRate (int dataRate)
 Sets the data rate code. More...
 
int mma8452GetMode (void)
 Gets the running mode. More...
 
int mma8452EnablePL (bool)
 Enables or disables Portrait / Landscape mode. PL mode allows to find out if the device is horizontal or vertical and if is is upside down. More...
 
bool mma8452IsPLEnabled (void)
 Checks if Portrait / Landscape mode is enabled. PL mode allows to find out if the device is horizontal or vertical and if is is upside down. More...
 
int mma8452GetOrientation (void)
 Get a code for the orientation. More...
 
char * mma8452Orientation2String (int)
 Converts the orientation code into a humanly readable string. More...
 
char * mma8452DataRate2String (int dataRate)
 Converts the data rate code into a humanly readable text. More...
 
char * mma8452DataErrorCode2String (int errorCode)
 Converts the error code into a humanly readable text. More...
 
char * mma8452Mode2String (int mode)
 Converts the running mode code into a humanly readable string. More...
 
char * mma8452Range2String (int range)
 Converts the dynamic range code into a humanly readable text. More...
 

Detailed Description

Header file for all defined constants and function prototypes.

Function Documentation

◆ mma8452DataErrorCode2String()

char* mma8452DataErrorCode2String ( int  errorCode)

Converts the error code into a humanly readable text.

Parameters
datarate: error code
Return values
dynamic_range_stringa string describing the error

◆ mma8452DataRate2String()

char* mma8452DataRate2String ( int  dataRate)

Converts the data rate code into a humanly readable text.

Parameters
data_ratethe data rate code
Return values
dynamic_range_stringa string describing the dynamic range

◆ mma8452EnablePL()

int mma8452EnablePL ( bool  onOff)

Enables or disables Portrait / Landscape mode. PL mode allows to find out if the device is horizontal or vertical and if is is upside down.

Parameters

◆ mma8452GetActiveStandby()

int mma8452GetActiveStandby ( void  )

Checks if the mma8452 is in standby or active mode.

Return values
active_standbyMMA8452_ACTIVE if active, MMA8452_STANDBY if in standby mode or sleep

◆ mma8452GetDataRate()

int mma8452GetDataRate ( void  )

Gets the data rate code.

Return values
data_ratecode for the data rate

◆ mma8452GetFastRead()

bool mma8452GetFastRead ( void  )

Checks if the mma8452 is in fast read mode. In fast read mode only the 8 most significant bits are read out.

Return values
fast_modetrue if in fast mode, false otherwise

◆ mma8452GetMode()

int mma8452GetMode ( void  )

Gets the running mode.

Return values
running_modecode for the running mode, which can be standby, wake or sleep

◆ mma8452GetOrientation()

int mma8452GetOrientation ( void  )

Get a code for the orientation.

Return values
orientationcode for the orientation, returns -1 of PL mode is not enabled

◆ mma8452GetRange()

int mma8452GetRange ( void  )

Gets the dynamic range The mma8452 has 3 dynamic ranges: 2g, 4g, 8g full scale.

Return values
rangea code for the dynamic range: MMA8452_FULL_SCALE_2G, MMA8452_FULL_SCALE_4G, MMA8452_FULL_SCALE_8G

◆ mma8452Init()

int mma8452Init ( void  )

Checks if the accelerometer's device WHO_AM_I register shows the right value of 0x2a.

Return values
error_codeMMA8452_SUCCESS if the identification was found MMA8452_NOT_FOUND otherwise

◆ mma8452IsPLEnabled()

bool mma8452IsPLEnabled ( void  )

Checks if Portrait / Landscape mode is enabled. PL mode allows to find out if the device is horizontal or vertical and if is is upside down.

Parameters

◆ mma8452Mode2String()

char* mma8452Mode2String ( int  mode)

Converts the running mode code into a humanly readable string.

Parameters

◆ mma8452Orientation2String()

char* mma8452Orientation2String ( int  orientation)

Converts the orientation code into a humanly readable string.

Parameters

◆ mma8452Range2String()

char* mma8452Range2String ( int  range)

Converts the dynamic range code into a humanly readable text.

Parameters
datarate: the dynamic range code
Return values
dynamic_range_stringa string describing the dynamic range

◆ mma8452ReadXYZ()

int mma8452ReadXYZ ( int16_t *  buf)

Function to read raw X,Y,Z acceleration values. The mma8452 is put into active mode.

Parameters
pointerto a buffer of 3 int16_t values buf[0] = X, buf[1] = Y, buf[2] = z The acceleration values are signed 12 bit, left adjusted values
Return values
anerror_code: MMA8452_SUCCESS if successful

◆ mma8452ReadXYZ_G()

int mma8452ReadXYZ_G ( float *  buf)

Function to read X,Y,Z axes values in units of g. This calls mma8452ReadXYZ and calculates the corresponding floating point values in units if g.

Parameters
fbufpointer to a buffer of 3 float values buf[0] = X, buf[1] = Y, buf[2] = z The acceleration values are signed 12 bit, left adjusted values
Return values
error_codeMMA8452_SUCCESS if successful

◆ mma8452SetActive()

int mma8452SetActive ( )

Sets the mma8452 to active mode. In this mode acceleration data are aquired.

Return values
error_codeMMA8452_SUCCESS if successful an error code otherwise

◆ mma8452SetDataRate()

int mma8452SetDataRate ( int  dataRate)

Sets the data rate code.

Parameters

◆ mma8452SetDebug()

void mma8452SetDebug ( bool  onOff)

switching debug messages on or off

Parameters
onOffto switch debug mode on or off

◆ mma8452SetFastRead()

int mma8452SetFastRead ( bool  onOff)

Sets or removes mma8452 fast read mode. In fast read mode only the 8 most significant bits are read out.

Parameters

◆ mma8452SetRange()

int mma8452SetRange ( int  range)

Sets the dynamic range The mma8452 has 3 dynamic ranges: 2g, 4g, 8g full scale.

Parameters
rangecode for the dynamic range
Return values
error_codeMMA8452_SUCCESS if successful, a negative error code otherwise

◆ mma8452SetStandby()

int mma8452SetStandby ( )

Sets the mma8452 to standby mode. In this mode configuration registers can be programmed.

Return values
MMA8452_SUCCESSif successful an error code otherwise

◆ mma8452Setup()

int mma8452Setup ( void  )

Setup the accelerometer on the I2C bus of the RPi Opens the connection to the pigoio daemon.

Return values
error_codeMMA8452_SUCCESS if successful, MMA8452_PIGPIO_INIT_FAILURE otherwise