-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master' into gpioFix
- Loading branch information
Showing
29 changed files
with
1,166 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include $(RIOTBASE)/Makefile.base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
/* | ||
* Copyright (C) 2017 Mesotic SAS | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @ingroup drivers_adxl345 | ||
* @{ | ||
* | ||
* @file | ||
* @brief Device driver implementation for the ADXL345 accelerometer (i2c only) | ||
* | ||
* @author Dylan Laduranty <[email protected]> | ||
* | ||
* @} | ||
*/ | ||
#include <stdint.h> | ||
#include <stdbool.h> | ||
#include <string.h> | ||
|
||
#include "assert.h" | ||
#include "periph/i2c.h" | ||
#include "adxl345.h" | ||
#include "adxl345_regs.h" | ||
|
||
#define ENABLE_DEBUG (0) | ||
#include "debug.h" | ||
|
||
#define I2C_SPEED I2C_SPEED_NORMAL | ||
|
||
#define BUS (dev->params.i2c) | ||
#define ADDR (dev->params.addr) | ||
|
||
int adxl345_init(adxl345_t *dev, const adxl345_params_t* params) | ||
{ | ||
uint8_t reg; | ||
|
||
assert(dev && params); | ||
|
||
/* get device descriptor */ | ||
dev->params= *params; | ||
|
||
/* Acquire exclusive access */ | ||
i2c_acquire(BUS); | ||
|
||
/* Initialize I2C interface */ | ||
if (i2c_init_master(BUS, I2C_SPEED) < 0) { | ||
i2c_release(BUS); | ||
DEBUG("[adxl345] init - error: unable to initialize I2C bus\n"); | ||
return ADXL345_NOI2C; | ||
} | ||
|
||
/* test if the target device responds */ | ||
i2c_read_reg(BUS, ADDR, ACCEL_ADXL345_CHIP_ID_REG, ®); | ||
if (reg != ACCEL_ADXL345_CHIP_ID) { | ||
i2c_release(BUS); | ||
DEBUG("[adxl345] init - error: invalid id value [0x%02x]\n", (int)reg); | ||
return ADXL345_NODEV; | ||
} | ||
/* configure the user offset */ | ||
i2c_write_regs(BUS, ADDR, ACCEL_ADXL345_OFFSET_X, dev->params.offset, 3); | ||
/* Basic device setup */ | ||
reg = (dev->params.full_res | dev->params.range); | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_DATA_FORMAT, reg); | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_BW_RATE, dev->params.rate); | ||
/* Put device in measure mode */ | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_POWER_CTL, MEASURE_BIT); | ||
|
||
/* Release the bus */ | ||
i2c_release(BUS); | ||
|
||
DEBUG("[adxl345] init: successful\n"); | ||
|
||
return ADXL345_OK; | ||
} | ||
|
||
void adxl345_read(adxl345_t *dev, adxl345_data_t *data) | ||
{ | ||
uint8_t result[6]; | ||
|
||
assert(dev && data); | ||
|
||
i2c_acquire(BUS); | ||
i2c_read_regs(BUS, ADDR, ACCEL_ADXL345_DATA_X0, result, 6); | ||
i2c_release(BUS); | ||
|
||
data->x = (((result[1] << 8)+result[0]) * dev->params.scale_factor); | ||
data->y = (((result[3] << 8)+result[2]) * dev->params.scale_factor); | ||
data->z = (((result[5] << 8)+result[4]) * dev->params.scale_factor); | ||
} | ||
|
||
void adxl345_set_interrupt(adxl345_t *dev) | ||
{ | ||
assert(dev); | ||
|
||
DEBUG("[adxl345] Update interruptions configuration\n"); | ||
|
||
i2c_acquire(BUS); | ||
/* Set threshold */ | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_THRESH_TAP, dev->params.interrupt.thres_tap); | ||
/* Set Map */ | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_INT_MAP, dev->params.interrupt.map); | ||
/* Set Duration */ | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_TAP_DUR, dev->params.interrupt.thres_dur); | ||
/* Enable axes */ | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_TAP_AXES, dev->params.interrupt.tap_axes); | ||
/* Set source */ | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_INT_SOURCE, dev->params.interrupt.source); | ||
/* Set latent threshold */ | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_TAP_LAT, dev->params.interrupt.thres_latent); | ||
/* Set window threshold */ | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_TAP_WIN, dev->params.interrupt.thres_window); | ||
/* Set activity threshold */ | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_THRESH_ACT, dev->params.interrupt.thres_act); | ||
/* Set inactivity threshold */ | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_THRESH_INACT, dev->params.interrupt.thres_inact); | ||
/* Set inactivity time */ | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_TIME_INACT, dev->params.interrupt.time_inact); | ||
/* Set free-fall threshold */ | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_THRESH_FF, dev->params.interrupt.thres_ff); | ||
/* Set free-fall time */ | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_TIME_FF, dev->params.interrupt.time_ff); | ||
/* Set axis control */ | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_ACT_INACT_CTL, dev->params.interrupt.act_inact); | ||
/* Enable interrupt */ | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_INT_ENABLE, dev->params.interrupt.enable); | ||
|
||
/* Release the bus */ | ||
i2c_release(BUS); | ||
} | ||
|
||
void adxl345_set_measure(adxl345_t *dev) | ||
{ | ||
uint8_t reg; | ||
|
||
assert(dev); | ||
|
||
DEBUG("[adxl345] set device to measure mode\n"); | ||
|
||
i2c_acquire(BUS); | ||
i2c_read_reg(BUS, ADDR, ACCEL_ADXL345_POWER_CTL, ®); | ||
reg |= MEASURE_BIT; | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_POWER_CTL, reg); | ||
i2c_release(BUS); | ||
} | ||
|
||
void adxl345_set_standby(adxl345_t *dev) | ||
{ | ||
uint8_t reg; | ||
|
||
assert(dev); | ||
|
||
DEBUG("[adxl345] set device to standby mode\n"); | ||
|
||
i2c_acquire(BUS); | ||
i2c_read_reg(BUS, ADDR, ACCEL_ADXL345_POWER_CTL, ®); | ||
reg &= ~MEASURE_BIT; | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_POWER_CTL, reg); | ||
i2c_release(BUS); | ||
} | ||
|
||
void adxl345_set_sleep(adxl345_t *dev) | ||
{ | ||
uint8_t reg; | ||
|
||
assert(dev); | ||
|
||
DEBUG("[adxl345] set device to sleep mode\n"); | ||
|
||
i2c_acquire(BUS); | ||
i2c_read_reg(BUS, ADDR, ACCEL_ADXL345_POWER_CTL, ®); | ||
reg |= SLEEP_BIT; | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_POWER_CTL, reg); | ||
i2c_release(BUS); | ||
} | ||
|
||
void adxl345_set_autosleep(adxl345_t *dev) | ||
{ | ||
uint8_t reg; | ||
|
||
assert(dev); | ||
|
||
DEBUG("[adxl345] set device to autosleep mode\n"); | ||
|
||
i2c_acquire(BUS); | ||
i2c_read_reg(BUS, ADDR, ACCEL_ADXL345_POWER_CTL, ®); | ||
reg |= AUTOSLEEP_BIT; | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_POWER_CTL, reg); | ||
i2c_release(BUS); | ||
} | ||
|
||
void adxl345_set_bandwidth_rate(adxl345_t *dev, uint8_t bw_rate) | ||
{ | ||
uint8_t reg; | ||
|
||
assert(dev); | ||
|
||
DEBUG("[adxl345] set device rate to %d Hz\n", (int)bw_rate); | ||
|
||
i2c_acquire(BUS); | ||
i2c_read_reg(BUS, ADDR, ACCEL_ADXL345_BW_RATE, ®); | ||
reg |= bw_rate; | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_BW_RATE, reg); | ||
i2c_release(BUS); | ||
} | ||
|
||
void adxl345_set_fifo_mode(adxl345_t *dev, uint8_t mode, | ||
uint8_t output, uint8_t value) | ||
{ | ||
uint8_t reg; | ||
|
||
assert(dev); | ||
|
||
DEBUG("[adxl345] set fifo mode to %d, output trigger to %d and trigger " | ||
"value to :%d\n", (int)mode, (int)output, (int)value); | ||
|
||
i2c_acquire(BUS); | ||
reg = ((mode << FIFO_MODE_POS) | (output << FIFO_TRIGGER_POS) | value); | ||
i2c_write_reg(BUS, ADDR, ACCEL_ADXL345_FIFO_CTL, reg); | ||
i2c_release(BUS); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2017 Mesotic SAS | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @ingroup drivers_adxl345 | ||
* @{ | ||
* | ||
* @file | ||
* @brief SAUL adaption for ADXL345 device | ||
* | ||
* @author Dylan Laduranty <[email protected]> | ||
* | ||
* @} | ||
*/ | ||
|
||
#include "saul.h" | ||
#include "adxl345.h" | ||
|
||
static int read_acc(void *dev, phydat_t *res) | ||
{ | ||
adxl345_t *d = (adxl345_t *)dev; | ||
adxl345_read(d, (adxl345_data_t *)res->val); | ||
|
||
res->unit = UNIT_G; | ||
res->scale = -3; | ||
return 3; | ||
} | ||
|
||
static int write(void *dev, phydat_t *state) | ||
{ | ||
return -ENOTSUP; | ||
} | ||
|
||
const saul_driver_t adxl345_saul_driver = { | ||
.read = read_acc, | ||
.write = write, | ||
.type = SAUL_SENSE_ACCEL, | ||
}; |
Oops, something went wrong.