Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into gpioFix
Browse files Browse the repository at this point in the history
  • Loading branch information
Anon Mall committed Mar 23, 2017
2 parents 65e9bb8 + a4cc6ad commit 64169a9
Show file tree
Hide file tree
Showing 29 changed files with 1,166 additions and 9 deletions.
7 changes: 6 additions & 1 deletion Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ ifneq (0,$(shell test -d $(RIOTBOARD)/$(BOARD); echo $$?))
endif

# Use TOOLCHAIN environment variable to select the toolchain to use.
# Default: gnu
# Default for macOS: llvm; for other OS: gnu
ifeq ($(BOARD),native)
ifeq ($(OS),Darwin)
TOOLCHAIN ?= llvm
endif
endif
TOOLCHAIN ?= gnu

# TOOLCHAIN = clang is an alias for TOOLCHAIN = llvm
Expand Down
4 changes: 2 additions & 2 deletions cpu/native/async_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ void native_async_read_add_handler(int fd, void *arg, native_async_read_callback
_sigio_child(_next_index);
#else
/* configure fds to send signals on io */
if (fcntl(fd, F_SETOWN, _native_pid) == -1) {
if (real_fcntl(fd, F_SETOWN, _native_pid) == -1) {
err(EXIT_FAILURE, "native_async_read_add_handler(): fcntl(F_SETOWN)");
}
/* set file access mode to non-blocking */
if (fcntl(fd, F_SETFL, O_NONBLOCK | O_ASYNC) == -1) {
if (real_fcntl(fd, F_SETFL, O_NONBLOCK | O_ASYNC) == -1) {
err(EXIT_FAILURE, "native_async_read_add_handler(): fcntl(F_SETFL)");
}
#endif /* not OSX */
Expand Down
1 change: 1 addition & 0 deletions cpu/native/include/native_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ extern int (*real_accept)(int socket, ...);
extern int (*real_bind)(int socket, ...);
extern int (*real_chdir)(const char *path);
extern int (*real_close)(int);
extern int (*real_fcntl)(int, int, ...);
/* The ... is a hack to save includes: */
extern int (*real_creat)(const char *path, ...);
extern int (*real_dup2)(int, int);
Expand Down
2 changes: 2 additions & 0 deletions cpu/native/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ int (*real_getifaddrs)(struct ifaddrs **ifap);
int (*real_getpid)(void);
int (*real_chdir)(const char *path);
int (*real_close)(int);
int (*real_fcntl)(int, int, ...);
int (*real_creat)(const char *path, ...);
int (*real_dup2)(int, int);
int (*real_execve)(const char *, char *const[], char *const[]);
Expand Down Expand Up @@ -454,6 +455,7 @@ void _native_init_syscalls(void)
*(void **)(&real_pipe) = dlsym(RTLD_NEXT, "pipe");
*(void **)(&real_chdir) = dlsym(RTLD_NEXT, "chdir");
*(void **)(&real_close) = dlsym(RTLD_NEXT, "close");
*(void **)(&real_fcntl) = dlsym(RTLD_NEXT, "fcntl");
*(void **)(&real_creat) = dlsym(RTLD_NEXT, "creat");
*(void **)(&real_fork) = dlsym(RTLD_NEXT, "fork");
*(void **)(&real_dup2) = dlsym(RTLD_NEXT, "dup2");
Expand Down
4 changes: 4 additions & 0 deletions drivers/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# driver dependencies (in alphabetical order)

ifneq (,$(filter adxl345,$(USEMODULE)))
FEATURES_REQUIRED += periph_i2c
endif

ifneq (,$(filter at30tse75x,$(USEMODULE)))
USEMODULE += xtimer
FEATURES_REQUIRED += periph_i2c
Expand Down
3 changes: 3 additions & 0 deletions drivers/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,6 @@ endif
ifneq (,$(filter veml6070,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/veml6070/include
endif
ifneq (,$(filter adxl345,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/adxl345/include
endif
1 change: 1 addition & 0 deletions drivers/adxl345/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
224 changes: 224 additions & 0 deletions drivers/adxl345/adxl345.c
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, &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);
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);
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);
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);
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);
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);
}
43 changes: 43 additions & 0 deletions drivers/adxl345/adxl345_saul.c
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,
};
Loading

0 comments on commit 64169a9

Please sign in to comment.