Contiki-Inga 3.x
Macros | Functions
ADXL345 Accelerometer Interface

Macros

#define ADXL345_CS   2
 
#define ADXL345_DEVICE_ID_REG   0x00
 Device ID Register.
 
#define ADXL345_OUTX_LOW_REG   0x32
 x Acceleration Data register (high)
 
#define ADXL345_OUTX_HIGH_REG   0x33
 x Acceleration Data register (low)
 
#define ADXL345_OUTY_LOW_REG   0x34
 y Acceleration Data register (high)
 
#define ADXL345_OUTY_HIGH_REG   0x35
 y Acceleration Data register (low)
 
#define ADXL345_OUTZ_LOW_REG   0x36
 z Acceleration Data register (high)
 
#define ADXL345_OUTZ_HIGH_REG   0x37
 z Acceleration Data register (low)
 
#define adxl345_raw_to_mg(raw)   (raw * 62) / 16;
 Convert raw value to mg value.
 

Functions

int8_t adxl345_available (void)
 Checks if ADXL345 is available. More...
 
int8_t adxl345_init (void)
 Initialize the ADXL345 accelerometer. More...
 
void adxl345_deinit (void)
 Deinitilizes the ADX345 accelerometer. More...
 
void adxl345_set_g_range (uint8_t range)
 
void adxl345_set_data_rate (uint8_t rate)
 
void adxl345_set_fifomode (uint8_t mode)
 
void adxl345_set_powermode (uint8_t mode)
 
uint8_t adxl345_get_fifo_level ()
 Returns the current fill level of the internal FIFO (when operating in FIFO/Stream mode). More...
 
int16_t adxl345_get_x (void)
 This function returns the current measured acceleration at the x-axis of the adxl345. More...
 
int16_t adxl345_get_y (void)
 This function returns the current measured acceleration at the y-axis of the adxl345. More...
 
int16_t adxl345_get_z (void)
 This function returns the current measured acceleration at the z-axis of the adxl345. More...
 
acc_data_t adxl345_get (void)
 This function returns the current measured acceleration of all axis (x,y,z) More...
 
void adxl345_write (uint8_t reg, uint8_t data)
 This function writes data to the given register of the ADXL345. More...
 
uint8_t adxl345_read (uint8_t reg)
 This function reads from the given register of the ADXL345. More...
 
int8_t adxl345_ready (void)
 Checks whether the device is ready or not by reading the ID. More...
 

BW_RATE register/bits

#define ADXL345_BW_RATE_REG   0x2C
 ADXL Data Rate and Power Mode Control Register. More...
 
#define ADXL345_LOW_POWER   4
 Low power bit pos. More...
 
#define ADXL345_RATE_L   0
 Rate bits [4] pos. More...
 

POWER_CTL register/bits

#define ADXL345_POWER_CTL_REG   0x2D
 ADXL Power Control Register. More...
 
#define ADXL345_LINK   5
 Link bit pos. More...
 
#define ADXL345_AUTO_SLEEP   4
 AUTO_SLEEP bit pos. More...
 
#define ADXL345_MEASURE   3
 Measure bit pos. More...
 
#define ADXL345_SLEEP   2
 Sleep bit pos. More...
 
#define ADXL345_WAKEUP_L   0
 Wakeup bits [2] LSB pos. More...
 

DATA_FORMAT register/bits

#define ADXL345_DATA_FORMAT_REG   0x31
 ADXL Data Format Register Register. More...
 
#define ADXL345_SELF_TEST   7
 SELF_TEST bit pos. More...
 
#define ADXL345_SPI   6
 SPI bit pos. More...
 
#define ADXL345_INT_INVERT   5
 INT_INVERT bit pos. More...
 
#define ADXL345_FULL_RES   3
 FULL_RES bit pos. More...
 
#define ADXL345_JUSTIFY   2
 Justify bit pos. More...
 
#define ADXL345_RANGE_L   0
 Range bits [2] LSB pos. More...
 

FIFO_CTL register/bits

#define ADXL345_FIFO_CTL_REG   0x38
 FIFO control register.
 
#define ADXL345_FIFO_MODE_L   6
 FIFO_MODE bits [2] LSB pos. More...
 
#define ADXL345_TRIGGER   5
 Trigger bit pos. More...
 
#define ADXL345_SAMPLES_L   0
 Samples bits [5] LSB pos. More...
 

FIFO_STATUS register/bits

#define ADXL345_FIFO_STATUS_REG   0x39
 FIFO status register.
 
#define ADXL345_FIFO_TRIG   7
 FIFO_TRIG bit pos. More...
 
#define ADXL345_ENTRIES_L   0
 Entries bits [6] LSB pos. More...
 

g range settings

#define ADXL345_MODE_2G   (0x0 << ADXL345_RANGE_L)
 +/- 2g, 256 LSB/g
 
#define ADXL345_MODE_4G   (0x1 << ADXL345_RANGE_L)
 +/- 4g, 128 LSB/g
 
#define ADXL345_MODE_8G   (0x2 << ADXL345_RANGE_L)
 +/- 8g, 64 LSB/g
 
#define ADXL345_MODE_16G   (0x3 << ADXL345_RANGE_L)
 +/- 16g, 32 LSB/g
 

FIFO mode settings

#define ADXL345_MODE_BYPASS   (0x0 << ADXL345_FIFO_MODE_L)
 
#define ADXL345_MODE_FIFO   (0x1 << ADXL345_FIFO_MODE_L)
 
#define ADXL345_MODE_STREAM   (0x2 << ADXL345_FIFO_MODE_L)
 
#define ADXL345_MODE_TRIGGER   (0x3 << ADXL345_FIFO_MODE_L)
 

Power mode settings

#define ADXL345_PMODE_SLEEP   1
 
#define ADXL345_PMODE_WAKEUP   3
 
#define ADXL345_PMODE_STANDBY   4
 

Values for output data rate

See Also
ADXL345_BW_RATE_REG
#define ADXL345_ODR_0HZ10   (0x0 << ADXL345_RATE_L)
 
#define ADXL345_ODR_0HZ20   (0x1 << ADXL345_RATE_L)
 
#define ADXL345_ODR_0HZ39   (0x2 << ADXL345_RATE_L)
 
#define ADXL345_ODR_0HZ78   (0x3 << ADXL345_RATE_L)
 
#define ADXL345_ODR_1HZ56   (0x4 << ADXL345_RATE_L)
 
#define ADXL345_ODR_3HZ13   (0x5 << ADXL345_RATE_L)
 
#define ADXL345_ODR_6HZ25   (0x6 << ADXL345_RATE_L)
 
#define ADXL345_ODR_12HZ5   (0x7 << ADXL345_RATE_L)
 
#define ADXL345_ODR_25HZ   (0x8 << ADXL345_RATE_L)
 
#define ADXL345_ODR_50HZ   (0x9 << ADXL345_RATE_L)
 
#define ADXL345_ODR_100HZ   (0xA << ADXL345_RATE_L)
 
#define ADXL345_ODR_200HZ   (0xB << ADXL345_RATE_L)
 
#define ADXL345_ODR_400HZ   (0xC << ADXL345_RATE_L)
 
#define ADXL345_ODR_800HZ   (0xD << ADXL345_RATE_L)
 
#define ADXL345_ODR_1600HZ   (0xE << ADXL345_RATE_L)
 
#define ADXL345_ODR_3200HZ   (0xF << ADXL345_RATE_L)
 

Detailed Description

and Bits

All registers are named ADXL345_name_REG where name is the name of the register as described in the sensors datasheet.

All bit positions are named ADXL345_name for single bit entries and ADXL345_name_L for multiple bit entries, where the position always means the entries LSB.

Macro Definition Documentation

#define ADXL345_AUTO_SLEEP   4

AUTO_SLEEP bit pos.

Definition at line 91 of file adxl345.h.

#define ADXL345_BW_RATE_REG   0x2C

ADXL Data Rate and Power Mode Control Register.

Definition at line 77 of file adxl345.h.

Referenced by adxl345_init(), and adxl345_set_data_rate().

#define ADXL345_CS   2

SPI device order. The chip select number where the ADXL345 is connected to the BCD-decimal decoder

Definition at line 69 of file adxl345.h.

Referenced by adxl345_available(), adxl345_get(), adxl345_read(), and adxl345_write().

#define ADXL345_DATA_FORMAT_REG   0x31

ADXL Data Format Register Register.

Definition at line 103 of file adxl345.h.

Referenced by adxl345_init(), and adxl345_set_g_range().

#define ADXL345_ENTRIES_L   0

Entries bits [6] LSB pos.

Definition at line 137 of file adxl345.h.

#define ADXL345_FIFO_MODE_L   6

FIFO_MODE bits [2] LSB pos.

Definition at line 123 of file adxl345.h.

#define ADXL345_FIFO_TRIG   7

FIFO_TRIG bit pos.

Definition at line 135 of file adxl345.h.

#define ADXL345_FULL_RES   3

FULL_RES bit pos.

Definition at line 111 of file adxl345.h.

Referenced by adxl345_init().

#define ADXL345_INT_INVERT   5

INT_INVERT bit pos.

Definition at line 109 of file adxl345.h.

#define ADXL345_JUSTIFY   2

Justify bit pos.

Definition at line 113 of file adxl345.h.

#define ADXL345_LINK   5

Link bit pos.

Definition at line 89 of file adxl345.h.

#define ADXL345_LOW_POWER   4

Low power bit pos.

Definition at line 79 of file adxl345.h.

#define ADXL345_MEASURE   3

Measure bit pos.

Definition at line 93 of file adxl345.h.

Referenced by adxl345_init(), and adxl345_set_powermode().

#define ADXL345_POWER_CTL_REG   0x2D

ADXL Power Control Register.

Definition at line 87 of file adxl345.h.

Referenced by adxl345_init(), and adxl345_set_powermode().

#define ADXL345_RANGE_L   0

Range bits [2] LSB pos.

Definition at line 115 of file adxl345.h.

#define ADXL345_RATE_L   0

Rate bits [4] pos.

Definition at line 81 of file adxl345.h.

#define ADXL345_SAMPLES_L   0

Samples bits [5] LSB pos.

Definition at line 127 of file adxl345.h.

#define ADXL345_SELF_TEST   7

SELF_TEST bit pos.

Definition at line 105 of file adxl345.h.

#define ADXL345_SLEEP   2

Sleep bit pos.

Definition at line 95 of file adxl345.h.

Referenced by adxl345_set_powermode().

#define ADXL345_SPI   6

SPI bit pos.

Definition at line 107 of file adxl345.h.

#define ADXL345_TRIGGER   5

Trigger bit pos.

Definition at line 125 of file adxl345.h.

#define ADXL345_WAKEUP_L   0

Wakeup bits [2] LSB pos.

Definition at line 97 of file adxl345.h.

Referenced by adxl345_set_powermode().

Function Documentation

int8_t adxl345_available ( void  )

Checks if ADXL345 is available.

Return values
1available
0not availabe

Definition at line 57 of file adxl345.c.

References ADXL345_CS, ADXL345_DEVICE_ID_REG, adxl345_read(), MSPI_BAUD_MAX, mspi_chip_release(), mspi_init(), and MSPI_MODE_3.

Referenced by adxl345_init().

void adxl345_deinit ( void  )

Deinitilizes the ADX345 accelerometer.

I.e. sets it in standby mode.

Definition at line 93 of file adxl345.c.

References adxl345_set_powermode().

acc_data_t adxl345_get ( void  )

This function returns the current measured acceleration of all axis (x,y,z)

Note
This is more efficient than reading each axis individually
Returns
current acceleration value of all axis

Definition at line 180 of file adxl345.c.

References ADXL345_CS, ADXL345_OUTX_LOW_REG, mspi_chip_release(), mspi_chip_select(), and mspi_transceive().

uint8_t adxl345_get_fifo_level ( )

Returns the current fill level of the internal FIFO (when operating in FIFO/Stream mode).

Returns
Fill level [0 - 33]

Definition at line 143 of file adxl345.c.

References ADXL345_FIFO_STATUS_REG, and adxl345_read().

int16_t adxl345_get_x ( void  )

This function returns the current measured acceleration at the x-axis of the adxl345.

Returns
current x-axis acceleration value

Definition at line 149 of file adxl345.c.

References ADXL345_OUTX_HIGH_REG, ADXL345_OUTX_LOW_REG, and adxl345_read().

int16_t adxl345_get_y ( void  )

This function returns the current measured acceleration at the y-axis of the adxl345.

Returns
current y-axis acceleration value

Definition at line 160 of file adxl345.c.

References ADXL345_OUTY_HIGH_REG, ADXL345_OUTY_LOW_REG, and adxl345_read().

int16_t adxl345_get_z ( void  )

This function returns the current measured acceleration at the z-axis of the adxl345.

Returns
current z-axis acceleration value

Definition at line 170 of file adxl345.c.

References ADXL345_OUTZ_HIGH_REG, ADXL345_OUTZ_LOW_REG, and adxl345_read().

int8_t adxl345_init ( void  )

Initialize the ADXL345 accelerometer.

Default settings are: TODO

Return values
0Initialization succeeded
-1Initialization failed

Definition at line 74 of file adxl345.c.

References adxl345_available(), ADXL345_BW_RATE_REG, ADXL345_DATA_FORMAT_REG, ADXL345_FULL_RES, ADXL345_MEASURE, ADXL345_POWER_CTL_REG, and adxl345_write().

uint8_t adxl345_read ( uint8_t  reg)

This function reads from the given register of the ADXL345.

Parameters
regThe register address
Returns
The data value

Definition at line 224 of file adxl345.c.

References ADXL345_CS, mspi_chip_release(), mspi_chip_select(), and mspi_transceive().

Referenced by adxl345_available(), adxl345_get_fifo_level(), adxl345_get_x(), adxl345_get_y(), adxl345_get_z(), adxl345_set_data_rate(), adxl345_set_fifomode(), adxl345_set_g_range(), and adxl345_set_powermode().

int8_t adxl345_ready ( void  )

Checks whether the device is ready or not by reading the ID.

Return values
1ready
0not ready
void adxl345_set_data_rate ( uint8_t  rate)
Parameters
rate

Definition at line 107 of file adxl345.c.

References ADXL345_BW_RATE_REG, adxl345_read(), and adxl345_write().

void adxl345_set_fifomode ( uint8_t  mode)
Parameters
modeOne of ADXL345_MODE_BYPASS, ADXL345_MODE_FIFO, ADXL345_MODE_STREAM, ADXL345_MODE_TRIGGER

Definition at line 114 of file adxl345.c.

References ADXL345_FIFO_CTL_REG, adxl345_read(), and adxl345_write().

void adxl345_set_g_range ( uint8_t  range)
Parameters
range

Definition at line 100 of file adxl345.c.

References ADXL345_DATA_FORMAT_REG, adxl345_read(), and adxl345_write().

void adxl345_set_powermode ( uint8_t  mode)
Parameters
modeOne of ADXL345_PMODE_SLEEP, ADXL345_PMODE_WAKEUP, ADXL345_PMODE_STANDBY

Definition at line 121 of file adxl345.c.

References ADXL345_MEASURE, ADXL345_POWER_CTL_REG, adxl345_read(), ADXL345_SLEEP, ADXL345_WAKEUP_L, and adxl345_write().

Referenced by adxl345_deinit().

void adxl345_write ( uint8_t  reg,
uint8_t  data 
)

This function writes data to the given register of the ADXL345.

Parameters
regThe register address
dataThe data value

Definition at line 214 of file adxl345.c.

References ADXL345_CS, mspi_chip_release(), mspi_chip_select(), and mspi_transceive().

Referenced by adxl345_init(), adxl345_set_data_rate(), adxl345_set_fifomode(), adxl345_set_g_range(), and adxl345_set_powermode().