no-OS
adxl345.h
Go to the documentation of this file.
1 /***************************************************************************/
39 #ifndef __ADXL345_H__
40 #define __ADXL345_H__
41 
42 /******************************************************************************/
43 /***************************** Include Files **********************************/
44 /******************************************************************************/
45 #include <stdint.h>
46 #include "no_os_i2c.h"
47 #include "no_os_spi.h"
48 
49 /******************************************************************************/
50 /******************************** ADXL345 *************************************/
51 /******************************************************************************/
52 
53 /* Options for communicating with the device. */
54 #define ADXL345_SPI_COMM 0
55 #define ADXL345_I2C_COMM 1
56 
57 /* I2C address of the device */
58 #define ADXL345_ADDRESS 0x1D
59 
60 /* SPI commands */
61 #define ADXL345_SPI_READ (1 << 7)
62 #define ADXL345_SPI_WRITE (0 << 7)
63 #define ADXL345_SPI_MB (1 << 6)
64 
65 /* ADXL345 Register Map */
66 #define ADXL345_DEVID 0x00 // R Device ID.
67 #define ADXL345_THRESH_TAP 0x1D // R/W Tap threshold.
68 #define ADXL345_OFSX 0x1E // R/W X-axis offset.
69 #define ADXL345_OFSY 0x1F // R/W Y-axis offset.
70 #define ADXL345_OFSZ 0x20 // R/W Z-axis offset.
71 #define ADXL345_DUR 0x21 // R/W Tap duration.
72 #define ADXL345_LATENT 0x22 // R/W Tap latency.
73 #define ADXL345_WINDOW 0x23 // R/W Tap window.
74 #define ADXL345_THRESH_ACT 0x24 // R/W Activity threshold.
75 #define ADXL345_THRESH_INACT 0x25 // R/W Inactivity threshold.
76 #define ADXL345_TIME_INACT 0x26 // R/W Inactivity time.
77 #define ADXL345_ACT_INACT_CTL 0x27 // R/W Axis enable control for activity
78 // and inactivity detection.
79 #define ADXL345_THRESH_FF 0x28 // R/W Free-fall threshold.
80 #define ADXL345_TIME_FF 0x29 // R/W Free-fall time.
81 #define ADXL345_TAP_AXES 0x2A // R/W Axis control for tap/double tap.
82 #define ADXL345_ACT_TAP_STATUS 0x2B // R Source of tap/double tap.
83 #define ADXL345_BW_RATE 0x2C // R/W Data rate and power mode control.
84 #define ADXL345_POWER_CTL 0x2D // R/W Power saving features control.
85 #define ADXL345_INT_ENABLE 0x2E // R/W Interrupt enable control.
86 #define ADXL345_INT_MAP 0x2F // R/W Interrupt mapping control.
87 #define ADXL345_INT_SOURCE 0x30 // R Source of interrupts.
88 #define ADXL345_DATA_FORMAT 0x31 // R/W Data format control.
89 #define ADXL345_DATAX0 0x32 // R X-Axis Data 0.
90 #define ADXL345_DATAX1 0x33 // R X-Axis Data 1.
91 #define ADXL345_DATAY0 0x34 // R Y-Axis Data 0.
92 #define ADXL345_DATAY1 0x35 // R Y-Axis Data 1.
93 #define ADXL345_DATAZ0 0x36 // R Z-Axis Data 0.
94 #define ADXL345_DATAZ1 0x37 // R Z-Axis Data 1.
95 #define ADXL345_FIFO_CTL 0x38 // R/W FIFO control.
96 #define ADXL345_FIFO_STATUS 0x39 // R FIFO status.
97 
98 /* ADXL345_ACT_INACT_CTL definition */
99 #define ADXL345_ACT_ACDC (1 << 7)
100 #define ADXL345_ACT_X_EN (1 << 6)
101 #define ADXL345_ACT_Y_EN (1 << 5)
102 #define ADXL345_ACT_Z_EN (1 << 4)
103 #define ADXL345_INACT_ACDC (1 << 3)
104 #define ADXL345_INACT_X_EN (1 << 2)
105 #define ADXL345_INACT_Y_EN (1 << 1)
106 #define ADXL345_INACT_Z_EN (1 << 0)
107 
108 /* ADXL345_TAP_AXES definition */
109 #define ADXL345_SUPPRESS (1 << 3)
110 #define ADXL345_TAP_X_EN (1 << 2)
111 #define ADXL345_TAP_Y_EN (1 << 1)
112 #define ADXL345_TAP_Z_EN (1 << 0)
113 
114 /* ADXL345_ACT_TAP_STATUS definition */
115 #define ADXL345_ACT_X_SRC (1 << 6)
116 #define ADXL345_ACT_Y_SRC (1 << 5)
117 #define ADXL345_ACT_Z_SRC (1 << 4)
118 #define ADXL345_ASLEEP (1 << 3)
119 #define ADXL345_TAP_X_SRC (1 << 2)
120 #define ADXL345_TAP_Y_SRC (1 << 1)
121 #define ADXL345_TAP_Z_SRC (1 << 0)
122 
123 /* ADXL345_BW_RATE definition */
124 #define ADXL345_LOW_POWER (1 << 4)
125 #define ADXL345_RATE(x) ((x) & 0xF)
126 
127 /* ADXL345_POWER_CTL definition */
128 #define ADXL345_PCTL_LINK (1 << 5)
129 #define ADXL345_PCTL_AUTO_SLEEP (1 << 4)
130 #define ADXL345_PCTL_MEASURE (1 << 3)
131 #define ADXL345_PCTL_SLEEP (1 << 2)
132 #define ADXL345_PCTL_WAKEUP(x) ((x) & 0x3)
133 
134 /* ADXL345_INT_ENABLE / ADXL345_INT_MAP / ADXL345_INT_SOURCE definition */
135 #define ADXL345_DATA_READY (1 << 7)
136 #define ADXL345_SINGLE_TAP (1 << 6)
137 #define ADXL345_DOUBLE_TAP (1 << 5)
138 #define ADXL345_ACTIVITY (1 << 4)
139 #define ADXL345_INACTIVITY (1 << 3)
140 #define ADXL345_FREE_FALL (1 << 2)
141 #define ADXL345_WATERMARK (1 << 1)
142 #define ADXL345_OVERRUN (1 << 0)
143 
144 /* ADXL345_DATA_FORMAT definition */
145 #define ADXL345_SELF_TEST (1 << 7)
146 #define ADXL345_SPI (1 << 6)
147 #define ADXL345_INT_INVERT (1 << 5)
148 #define ADXL345_FULL_RES (1 << 3)
149 #define ADXL345_JUSTIFY (1 << 2)
150 #define ADXL345_RANGE(x) ((x) & 0x3)
151 
152 /* ADXL345_RANGE(x) options */
153 #define ADXL345_RANGE_PM_2G 0
154 #define ADXL345_RANGE_PM_4G 1
155 #define ADXL345_RANGE_PM_8G 2
156 #define ADXL345_RANGE_PM_16G 3
157 
158 /* ADXL345_FIFO_CTL definition */
159 #define ADXL345_FIFO_MODE(x) (((x) & 0x3) << 6)
160 #define ADXL345_TRIGGER (1 << 5)
161 #define ADXL345_SAMPLES(x) ((x) & 0x1F)
162 
163 /* ADXL345_FIFO_MODE(x) options */
164 #define ADXL345_FIFO_BYPASS 0
165 #define ADXL345_FIFO_FIFO 1
166 #define ADXL345_FIFO_STREAM 2
167 #define ADXL345_FIFO_TRIGGER 3
168 
169 /* ADXL345_FIFO_STATUS definition */
170 #define ADXL345_FIFO_TRIG (1 << 7)
171 #define ADXL345_ENTRIES(x) ((x) & 0x3F)
172 
173 /* ADXL345 ID */
174 #define ADXL345_ID 0xE5
175 
176 /* ADXL345 Full Resolution Scale Factor */
177 #define ADXL345_SCALE_FACTOR 0.0039
178 
179 /******************************************************************************/
180 /*************************** Types Declarations *******************************/
181 /******************************************************************************/
182 
187 struct adxl345_dev {
195  uint8_t selected_range;
198 };
199 
212  uint8_t selected_range;
215 };
216 
217 /******************************************************************************/
218 /************************ Functions Declarations ******************************/
219 /******************************************************************************/
220 
222 uint8_t adxl345_get_register_value(struct adxl345_dev *dev,
223  uint8_t register_address);
224 
226 void adxl345_set_register_value(struct adxl345_dev *dev,
227  uint8_t register_address,
228  uint8_t register_value);
229 
231 int32_t adxl345_init(struct adxl345_dev **device,
233 
235 int32_t adxl345_remove(struct adxl345_dev *dev);
236 
238 void adxl345_set_power_mode(struct adxl345_dev *dev,
239  uint8_t pwr_mode);
240 
242 void adxl345_get_xyz(struct adxl345_dev *dev,
243  int16_t* x,
244  int16_t* y,
245  int16_t* z);
246 
248 void adxl345_get_g_xyz(struct adxl345_dev *dev,
249  float* x,
250  float* y,
251  float* z);
252 
254 void adxl345_set_tap_detection(struct adxl345_dev *dev,
255  uint8_t tap_type,
256  uint8_t tap_axes,
257  uint8_t tap_dur,
258  uint8_t tap_latent,
259  uint8_t tap_window,
260  uint8_t tap_thresh,
261  uint8_t tap_int);
262 
265  uint8_t act_on_off,
266  uint8_t act_axes,
267  uint8_t act_ac_dc,
268  uint8_t act_thresh,
269  uint8_t act_int);
270 
273  uint8_t inact_on_off,
274  uint8_t inact_axes,
275  uint8_t inact_ac_dc,
276  uint8_t inact_thresh,
277  uint8_t inact_time,
278  uint8_t inact_int);
279 
282  uint8_t ff_on_off,
283  uint8_t ff_thresh,
284  uint8_t ff_time,
285  uint8_t ff_int);
286 
288 void adxl345_set_offset(struct adxl345_dev *dev,
289  uint8_t x_offset,
290  uint8_t y_offset,
291  uint8_t z_offset);
292 
295  uint8_t g_range,
296  uint8_t full_res);
297 
298 #endif /* __ADXL345_H__ */
ADXL345_WINDOW
#define ADXL345_WINDOW
Definition: adxl345.h:73
adxl345_get_g_xyz
void adxl345_get_g_xyz(struct adxl345_dev *dev, float *x, float *y, float *z)
Reads the raw output data of each axis and converts it to g.
Definition: adxl345.c:263
adxl345_init_param
Structure holding the parameters for ADXL345 device initialization.
Definition: adxl345.h:204
ADXL345_OFSY
#define ADXL345_OFSY
Definition: adxl345.h:69
no_os_i2c_write
int32_t no_os_i2c_write(struct no_os_i2c_desc *desc, uint8_t *data, uint8_t bytes_number, uint8_t stop_bit)
I2C Write data to slave device.
Definition: no_os_i2c.c:85
ADXL345_ACT_INACT_CTL
#define ADXL345_ACT_INACT_CTL
Definition: adxl345.h:77
no_os_i2c_init
int32_t no_os_i2c_init(struct no_os_i2c_desc **desc, const struct no_os_i2c_init_param *param)
Initialize the I2C communication peripheral.
Definition: no_os_i2c.c:51
adxl345_set_free_fall_detection
void adxl345_set_free_fall_detection(struct adxl345_dev *dev, uint8_t ff_on_off, uint8_t ff_thresh, uint8_t ff_time, uint8_t ff_int)
Enables/disables the free-fall detection.
Definition: adxl345.c:521
ADXL345_INT_ENABLE
#define ADXL345_INT_ENABLE
Definition: adxl345.h:85
adxl345_init_param::full_resolution_set
uint8_t full_resolution_set
Definition: adxl345.h:214
adxl345_set_range_resolution
void adxl345_set_range_resolution(struct adxl345_dev *dev, uint8_t g_range, uint8_t full_res)
Selects the measurement range.
Definition: adxl345.c:595
ADXL345_ACT_Y_EN
#define ADXL345_ACT_Y_EN
Definition: adxl345.h:101
no_os_spi_write_and_read
int32_t no_os_spi_write_and_read(struct no_os_spi_desc *desc, uint8_t *data, uint16_t bytes_number)
Write and read data to/from SPI.
Definition: no_os_spi.c:82
adxl345_set_free_fall_detection
void adxl345_set_free_fall_detection(struct adxl345_dev *dev, uint8_t ff_on_off, uint8_t ff_thresh, uint8_t ff_time, uint8_t ff_int)
Enables/disables the free-fall detection.
Definition: adxl345.c:521
ADXL345_SPI_MB
#define ADXL345_SPI_MB
Definition: adxl345.h:63
ADXL345_THRESH_INACT
#define ADXL345_THRESH_INACT
Definition: adxl345.h:75
no_os_spi.h
Header file of SPI Interface.
adxl345_dev::communication_type
uint8_t communication_type
Definition: adxl345.h:193
no_os_i2c_remove
int32_t no_os_i2c_remove(struct no_os_i2c_desc *desc)
Free the resources allocated by no_os_i2c_init().
Definition: no_os_i2c.c:70
ADXL345_ID
#define ADXL345_ID
Definition: adxl345.h:174
adxl345_init
int32_t adxl345_init(struct adxl345_dev **device, struct adxl345_init_param init_param)
Initializes the communication peripheral and checks if the ADXL345 part is present.
Definition: adxl345.c:130
adxl345_remove
int32_t adxl345_remove(struct adxl345_dev *dev)
Free the resources allocated by adxl345_init().
Definition: adxl345.c:165
adxl345_set_inactivity_detection
void adxl345_set_inactivity_detection(struct adxl345_dev *dev, uint8_t inact_on_off, uint8_t inact_axes, uint8_t inact_ac_dc, uint8_t inact_thresh, uint8_t inact_time, uint8_t inact_int)
Enables/disables the inactivity detection.
Definition: adxl345.c:456
device
Definition: ad9361_util.h:75
adxl345_init_param::selected_range
uint8_t selected_range
Definition: adxl345.h:212
ADXL345_TIME_INACT
#define ADXL345_TIME_INACT
Definition: adxl345.h:76
adxl345_set_register_value
void adxl345_set_register_value(struct adxl345_dev *dev, uint8_t register_address, uint8_t register_value)
Writes data into a register.
Definition: adxl345.c:94
ADXL345_THRESH_FF
#define ADXL345_THRESH_FF
Definition: adxl345.h:79
ADXL345_LATENT
#define ADXL345_LATENT
Definition: adxl345.h:72
adxl345_init_param::i2c_init
no_os_i2c_init_param i2c_init
Definition: adxl345.h:206
ADXL345_INACT_Z_EN
#define ADXL345_INACT_Z_EN
Definition: adxl345.h:106
ADXL345_DATAX0
#define ADXL345_DATAX0
Definition: adxl345.h:89
adxl345_get_register_value
uint8_t adxl345_get_register_value(struct adxl345_dev *dev, uint8_t register_address)
Reads the value of a register.
Definition: adxl345.c:58
adxl345_set_activity_detection
void adxl345_set_activity_detection(struct adxl345_dev *dev, uint8_t act_on_off, uint8_t act_axes, uint8_t act_ac_dc, uint8_t act_thresh, uint8_t act_int)
Enables/disables the activity detection.
Definition: adxl345.c:388
ADXL345_PCTL_MEASURE
#define ADXL345_PCTL_MEASURE
Definition: adxl345.h:130
adxl345_set_register_value
void adxl345_set_register_value(struct adxl345_dev *dev, uint8_t register_address, uint8_t register_value)
Writes data into a register.
Definition: adxl345.c:94
adxl345_init
int32_t adxl345_init(struct adxl345_dev **device, struct adxl345_init_param init_param)
Initializes the communication peripheral and checks if the ADXL345 part is present.
Definition: adxl345.c:130
ADXL345_INACT_Y_EN
#define ADXL345_INACT_Y_EN
Definition: adxl345.h:105
ADXL345_TAP_AXES
#define ADXL345_TAP_AXES
Definition: adxl345.h:81
ADXL345_ACT_ACDC
#define ADXL345_ACT_ACDC
Definition: adxl345.h:99
adxl345_set_offset
void adxl345_set_offset(struct adxl345_dev *dev, uint8_t x_offset, uint8_t y_offset, uint8_t z_offset)
Sets an offset value for each axis (Offset Calibration).
Definition: adxl345.c:564
ADXL345_ACTIVITY
#define ADXL345_ACTIVITY
Definition: adxl345.h:138
adxl345.h
Header file of ADXL345 Driver.
ADXL345_SPI_COMM
#define ADXL345_SPI_COMM
Definition: adxl345.h:54
ADXL345_DOUBLE_TAP
#define ADXL345_DOUBLE_TAP
Definition: adxl345.h:137
ADXL345_POWER_CTL
#define ADXL345_POWER_CTL
Definition: adxl345.h:84
ADXL345_DUR
#define ADXL345_DUR
Definition: adxl345.h:71
adxl345_dev::i2c_desc
no_os_i2c_desc * i2c_desc
Definition: adxl345.h:189
adxl345_set_tap_detection
void adxl345_set_tap_detection(struct adxl345_dev *dev, uint8_t tap_type, uint8_t tap_axes, uint8_t tap_dur, uint8_t tap_latent, uint8_t tap_window, uint8_t tap_thresh, uint8_t tap_int)
Enables/disables the tap detection.
Definition: adxl345.c:309
adxl345_get_xyz
void adxl345_get_xyz(struct adxl345_dev *dev, int16_t *x, int16_t *y, int16_t *z)
Reads the raw output data of each axis.
Definition: adxl345.c:214
adxl345_init_param::communication_type
uint8_t communication_type
Definition: adxl345.h:210
adxl345_init_param::spi_init
no_os_spi_init_param spi_init
Definition: adxl345.h:208
ADXL345_SPI_READ
#define ADXL345_SPI_READ
Definition: adxl345.h:61
ADXL345_DEVID
#define ADXL345_DEVID
Definition: adxl345.h:66
adxl345_dev
ADXL345 Device structure.
Definition: adxl345.h:187
no_os_spi_desc
Structure holding SPI descriptor.
Definition: no_os_spi.h:132
ADXL345_INACT_ACDC
#define ADXL345_INACT_ACDC
Definition: adxl345.h:103
ADXL345_OFSZ
#define ADXL345_OFSZ
Definition: adxl345.h:70
adxl345_dev::spi_desc
no_os_spi_desc * spi_desc
Definition: adxl345.h:191
adxl345_set_power_mode
void adxl345_set_power_mode(struct adxl345_dev *dev, uint8_t pwr_mode)
Places the device into standby/measure mode.
Definition: adxl345.c:189
ADXL345_TAP_Y_EN
#define ADXL345_TAP_Y_EN
Definition: adxl345.h:111
ADXL345_FULL_RES
#define ADXL345_FULL_RES
Definition: adxl345.h:148
adxl345_get_xyz
void adxl345_get_xyz(struct adxl345_dev *dev, int16_t *x, int16_t *y, int16_t *z)
Reads the raw output data of each axis.
Definition: adxl345.c:214
adxl345_set_range_resolution
void adxl345_set_range_resolution(struct adxl345_dev *dev, uint8_t g_range, uint8_t full_res)
Selects the measurement range.
Definition: adxl345.c:595
adxl345_set_power_mode
void adxl345_set_power_mode(struct adxl345_dev *dev, uint8_t pwr_mode)
Places the device into standby/measure mode.
Definition: adxl345.c:189
no_os_i2c_desc
Structure holding I2C descriptor.
Definition: no_os_i2c.h:81
ADXL345_INT_MAP
#define ADXL345_INT_MAP
Definition: adxl345.h:86
no_os_i2c_read
int32_t no_os_i2c_read(struct no_os_i2c_desc *desc, uint8_t *data, uint8_t bytes_number, uint8_t stop_bit)
I2C Read data from slave device.
Definition: no_os_i2c.c:104
adxl345_set_activity_detection
void adxl345_set_activity_detection(struct adxl345_dev *dev, uint8_t act_on_off, uint8_t act_axes, uint8_t act_ac_dc, uint8_t act_thresh, uint8_t act_int)
Enables/disables the activity detection.
Definition: adxl345.c:388
no_os_i2c.h
Header file of I2C Interface.
ADXL345_ACT_X_EN
#define ADXL345_ACT_X_EN
Definition: adxl345.h:100
ADXL345_TAP_X_EN
#define ADXL345_TAP_X_EN
Definition: adxl345.h:110
ADXL345_THRESH_TAP
#define ADXL345_THRESH_TAP
Definition: adxl345.h:67
adxl345_set_inactivity_detection
void adxl345_set_inactivity_detection(struct adxl345_dev *dev, uint8_t inact_on_off, uint8_t inact_axes, uint8_t inact_ac_dc, uint8_t inact_thresh, uint8_t inact_time, uint8_t inact_int)
Enables/disables the inactivity detection.
Definition: adxl345.c:456
adxl345_remove
int32_t adxl345_remove(struct adxl345_dev *dev)
Free the resources allocated by adxl345_init().
Definition: adxl345.c:165
adxl345_get_g_xyz
void adxl345_get_g_xyz(struct adxl345_dev *dev, float *x, float *y, float *z)
Reads the raw output data of each axis and converts it to g.
Definition: adxl345.c:263
no_os_i2c_init_param
Structure holding the parameters for I2C initialization.
Definition: no_os_i2c.h:64
ADXL345_SPI_WRITE
#define ADXL345_SPI_WRITE
Definition: adxl345.h:62
init_param
struct ad7616_init_param init_param
Definition: ad7616_sdz.c:96
adxl345_dev::selected_range
uint8_t selected_range
Definition: adxl345.h:195
adxl345_get_register_value
uint8_t adxl345_get_register_value(struct adxl345_dev *dev, uint8_t register_address)
Reads the value of a register.
Definition: adxl345.c:58
ADXL345_SINGLE_TAP
#define ADXL345_SINGLE_TAP
Definition: adxl345.h:136
no_os_spi_remove
int32_t no_os_spi_remove(struct no_os_spi_desc *desc)
Free the resources allocated by no_os_spi_init().
Definition: no_os_spi.c:70
ADXL345_SCALE_FACTOR
#define ADXL345_SCALE_FACTOR
Definition: adxl345.h:177
ADXL345_RANGE
#define ADXL345_RANGE(x)
Definition: adxl345.h:150
no_os_spi_init
int32_t no_os_spi_init(struct no_os_spi_desc **desc, const struct no_os_spi_init_param *param)
Initialize the SPI communication peripheral.
Definition: no_os_spi.c:51
adxl345_set_offset
void adxl345_set_offset(struct adxl345_dev *dev, uint8_t x_offset, uint8_t y_offset, uint8_t z_offset)
Sets an offset value for each axis (Offset Calibration).
Definition: adxl345.c:564
ADXL345_THRESH_ACT
#define ADXL345_THRESH_ACT
Definition: adxl345.h:74
ADXL345_INACT_X_EN
#define ADXL345_INACT_X_EN
Definition: adxl345.h:104
ADXL345_DATA_FORMAT
#define ADXL345_DATA_FORMAT
Definition: adxl345.h:88
ADXL345_TAP_Z_EN
#define ADXL345_TAP_Z_EN
Definition: adxl345.h:112
ADXL345_INACTIVITY
#define ADXL345_INACTIVITY
Definition: adxl345.h:139
adxl345_dev::full_resolution_set
uint8_t full_resolution_set
Definition: adxl345.h:197
ADXL345_ACT_Z_EN
#define ADXL345_ACT_Z_EN
Definition: adxl345.h:102
ADXL345_OFSX
#define ADXL345_OFSX
Definition: adxl345.h:68
ADXL345_FREE_FALL
#define ADXL345_FREE_FALL
Definition: adxl345.h:140
adxl345_set_tap_detection
void adxl345_set_tap_detection(struct adxl345_dev *dev, uint8_t tap_type, uint8_t tap_axes, uint8_t tap_dur, uint8_t tap_latent, uint8_t tap_window, uint8_t tap_thresh, uint8_t tap_int)
Enables/disables the tap detection.
Definition: adxl345.c:309
ADXL345_TIME_FF
#define ADXL345_TIME_FF
Definition: adxl345.h:80
no_os_spi_init_param
Structure holding the parameters for SPI initialization.
Definition: no_os_spi.h:112