46 #include "lib/sensors.h" 50 #define PRINTF(...) printf(__VA_ARGS__) 59 #define ADXL346_ADDRESS (0x53) 60 #define ADXL346_DEVID_VALUE (0xE6) 67 #define ADXL346_DEVID_ADDR (0x00) 68 #define ADXL346_THRES_TAP_ADDR (0x1D) 69 #define ADXL346_OFSX_ADDR (0x1E) 70 #define ADXL346_OFSY_ADDR (0x1F) 71 #define ADXL346_OFSZ_ADDR (0x20) 72 #define ADXL346_DUR_ADDR (0x21) 73 #define ADXL346_LATENT_ADDR (0x22) 74 #define ADXL346_WINDOW_ADDR (0x23) 75 #define ADXL346_THRESH_ACT_ADDR (0x24) 76 #define ADXL346_THRESH_INACT_ADDR (0x25) 77 #define ADXL346_TIME_INACT_ADDR (0x26) 78 #define ADXL346_ACT_INACT_CTL_ADDR (0x27) 79 #define ADXL346_THRESH_FF_ADDR (0x28) 80 #define ADXL346_TIME_FF_ADDR (0x29) 81 #define ADXL346_TAP_AXES_ADDR (0x2A) 82 #define ADXL346_ACT_TAP_STATUS_ADDR (0x2B) 83 #define ADXL346_BW_RATE_ADDR (0x2C) 84 #define ADXL346_POWER_CTL_ADDR (0x2D) 85 #define ADXL346_INT_ENABLE_ADDR (0x2E) 86 #define ADXL346_INT_MAP_ADDR (0x2F) 87 #define ADXL346_INT_SOURCE_ADDR (0x30) 88 #define ADXL346_DATA_FORMAT_ADDR (0x31) 89 #define ADXL346_DATAX0_ADDR (0x32) 90 #define ADXL346_DATAX1_ADDR (0x33) 91 #define ADXL346_DATAY0_ADDR (0x34) 92 #define ADXL346_DATAY1_ADDR (0x35) 93 #define ADXL346_DATAZ0_ADDR (0x36) 94 #define ADXL346_DATAZ1_ADDR (0x37) 95 #define ADXL346_FIFO_CTL_ADDR (0x38) 96 #define ADXL346_FIFO_STATUS_ADDR (0x39) 97 #define ADXL346_TAP_SIGN_ADDR (0x3A) 98 #define ADXL346_ORIENT_CONF_ADDR (0x3B) 99 #define ADXL346_ORIENT_ADDR (0x3C) 106 #define ADXL346_INT_ENABLE_DATA_READY (1 << 7) 107 #define ADXL346_INT_ENABLE_SINGLE_TAP (1 << 6) 108 #define ADXL346_INT_ENABLE_DOUBLE_TAP (1 << 5) 109 #define ADXL346_INT_ENABLE_ACTIVITY (1 << 4) 110 #define ADXL346_INT_ENABLE_INACTIVITY (1 << 3) 111 #define ADXL346_INT_ENABLE_FREE_FALL (1 << 2) 112 #define ADXL346_INT_ENABLE_WATERMARK (1 << 1) 113 #define ADXL346_INT_ENABLE_OVERRUN (1 << 0) 115 #define ADXL346_ACT_INACT_CTL_ACT_ACDC (1 << 7) 116 #define ADXL346_ACT_INACT_CTL_ACT_X_EN (1 << 6) 117 #define ADXL346_ACT_INACT_CTL_ACT_Y_EN (1 << 5) 118 #define ADXL346_ACT_INACT_CTL_ACT_Z_EN (1 << 4) 119 #define ADXL346_ACT_INACT_CTL_INACT_ACDC (1 << 3) 120 #define ADXL346_ACT_INACT_CTL_INACT_X_EN (1 << 2) 121 #define ADXL346_ACT_INACT_CTL_INACT_Y_EN (1 << 1) 122 #define ADXL346_ACT_INACT_CTL_INACT_Z_EN (1 << 0) 124 #define ADXL346_TAP_AXES_SUPPRESS (1 << 3) 125 #define ADXL346_TAP_AXES_TAP_X_EN (1 << 2) 126 #define ADXL346_TAP_AXES_TAP_Y_EN (1 << 1) 127 #define ADXL346_TAP_AXES_TAP_Z_EN (1 << 0) 129 #define ADXL346_ACT_TAP_STATUS_ACT_X_SRC (1 << 6) 130 #define ADXL346_ACT_TAP_STATUS_ACT_Y_SRC (1 << 5) 131 #define ADXL346_ACT_TAP_STATUS_ACT_Z_SRC (1 << 4) 132 #define ADXL346_ACT_TAP_STATUS_ASLEEP (1 << 3) 133 #define ADXL346_ACT_TAP_STATUS_TAP_X_SRC (1 << 2) 134 #define ADXL346_ACT_TAP_STATUS_TAP_Y_SRC (1 << 1) 135 #define ADXL346_ACT_TAP_STATUS_TAP_Z_SRC (1 << 0) 137 #define ADXL346_BW_RATE_POWER (1 << 4) 138 #define ADXL346_BW_RATE_RATE(x) ((x) & 0x0F) 140 #define ADXL346_POWER_CTL_LINK (1 << 5) 141 #define ADXL346_POWER_CTL_AUTO_SLEEP (1 << 4) 142 #define ADXL346_POWER_CTL_MEASURE (1 << 3) 143 #define ADXL346_POWER_CTL_SLEEP (1 << 2) 144 #define ADXL346_POWER_CTL_WAKEUP(x) ((x) & 0x03) 146 #define ADXL346_DATA_FORMAT_SELF_TEST (1 << 7) 147 #define ADXL346_DATA_FORMAT_SPI (1 << 6) 148 #define ADXL346_DATA_FORMAT_INT_INVERT (1 << 5) 149 #define ADXL346_DATA_FORMAT_FULL_RES (1 << 3) 150 #define ADXL346_DATA_FORMAT_JUSTIFY (1 << 2) 151 #define ADXL346_DATA_FORMAT_RANGE(x) ((x) & 0x03) 152 #define ADXL346_DATA_FORMAT_RANGE_PM_2g (0) 153 #define ADXL346_DATA_FORMAT_RANGE_PM_4g (1) 154 #define ADXL346_DATA_FORMAT_RANGE_PM_8g (2) 155 #define ADXL346_DATA_FORMAT_RANGE_PM_16g (3) 157 #define ADXL346_USER_CONFIGURATION (ADXL346_DATA_FORMAT_RANGE_PM_2g) 161 static uint8_t enabled;
168 config[0] = ADXL346_BW_RATE_ADDR;
169 config[1] = (ADXL346_BW_RATE_RATE(6));
172 config[0] = ADXL346_DATA_FORMAT_ADDR;
173 config[1] = (ADXL346_USER_CONFIGURATION);
176 config[0] = ADXL346_POWER_CTL_ADDR;
177 config[1] = (ADXL346_POWER_CTL_MEASURE);
182 adxl346_is_present(
void)
189 return is_present == ADXL346_DEVID_VALUE;
193 adxl346_read_accel(uint8_t addr1, uint8_t addr2)
195 uint8_t acceleration[2];
203 result = (acceleration[1] << 8) | acceleration[0];
209 adxl346_convert_accel(int16_t accel)
213 result = (1000 * accel) / 256;
215 return (int16_t)result;
219 adxl346_calibrate_offset(
void)
227 config[0] = ADXL346_OFSX_ADDR;
230 config[0] = ADXL346_OFSY_ADDR;
233 config[0] = ADXL346_OFSZ_ADDR;
238 for(i = 0; i < 100; i++) {
241 x = adxl346_read_accel(ADXL346_DATAX0_ADDR, ADXL346_DATAX1_ADDR);
244 y = adxl346_read_accel(ADXL346_DATAY0_ADDR, ADXL346_DATAY1_ADDR);
247 z = adxl346_read_accel(ADXL346_DATAZ0_ADDR, ADXL346_DATAZ1_ADDR);
251 offset = (64 * accum_x) / 25600;
252 config[0] = ADXL346_OFSX_ADDR;
255 PRINTF(
"ADXL346: X calibration offset is %d\n", offset);
257 offset = (64 * accum_y) / 25600;
258 config[0] = ADXL346_OFSY_ADDR;
261 PRINTF(
"ADXL346: Y calibration offset is %d\n", offset);
263 offset = (64 * accum_z) / 25600;
264 config[0] = ADXL346_OFSZ_ADDR;
267 PRINTF(
"ADXL346: Z calibration offset is %d\n", offset);
286 PRINTF(
"ADXL346: sensor not started\n");
287 return ADXL346_ERROR;
290 if(type == ADXL346_READ_X) {
291 return adxl346_read_accel(ADXL346_DATAX0_ADDR, ADXL346_DATAX1_ADDR);
292 }
else if(type == ADXL346_READ_Y) {
293 return adxl346_read_accel(ADXL346_DATAY0_ADDR, ADXL346_DATAY1_ADDR);
294 }
else if(type == ADXL346_READ_Z) {
295 return adxl346_read_accel(ADXL346_DATAZ0_ADDR, ADXL346_DATAZ1_ADDR);
296 }
else if(type == ADXL346_READ_X_mG) {
297 accel = adxl346_read_accel(ADXL346_DATAX0_ADDR, ADXL346_DATAX1_ADDR);
298 return adxl346_convert_accel(accel);
299 }
else if(type == ADXL346_READ_Y_mG) {
300 accel = adxl346_read_accel(ADXL346_DATAY0_ADDR, ADXL346_DATAY1_ADDR);
301 return adxl346_convert_accel(accel);
302 }
else if(type == ADXL346_READ_Z_mG) {
303 accel = adxl346_read_accel(ADXL346_DATAZ0_ADDR, ADXL346_DATAZ1_ADDR);
304 return adxl346_convert_accel(accel);
306 PRINTF(
"ADXL346: invalid value requested\n");
307 return ADXL346_ERROR;
310 return ADXL346_ERROR;
314 configure(
int type,
int value)
316 if(type == ADXL346_ACTIVATE) {
317 if(!adxl346_is_present()) {
318 PRINTF(
"ADXL346: is not present\n");
320 return ADXL346_ERROR;
324 return ADXL346_SUCCESS;
328 if(type == ADXL346_CALIB_OFFSET && enabled) {
329 adxl346_calibrate_offset();
330 return ADXL346_SUCCESS;
333 return ADXL346_ERROR;
336 SENSORS_SENSOR(adxl346, ADXL346_SENSOR, value, configure, status);
uint8_t i2c_burst_send(uint8_t slave_addr, uint8_t *data, uint8_t len)
Perform all operations to send multiple bytes to a slave.
uint8_t i2c_single_receive(uint8_t slave_addr, uint8_t *data)
Perform all operations to receive a byte from a slave.
ADXL346 acceleration sensor driver header file.
uint8_t i2c_single_send(uint8_t slave_addr, uint8_t data)
Perform all operations to send a byte to a slave.