Decawave / uwb-core

Ultrawideband Core
http://decawave.com
Apache License 2.0
58 stars 36 forks source link

Add DWM1001 BSP support for the LIS2DH12 accelerometer #18

Open seijikun opened 3 years ago

seijikun commented 3 years ago

Hey guys,

this PR adds mynewt driver support for the LIS2DH12 accelerometer integrated in DWM1001-DEV boards. This is essentially the same as this pull request on apache's DWM1001-dev bsp, since one seems to have to decide between this one and theirs.

Here is an example application:

#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "sysinit/sysinit.h"
#include "os/os.h"
#include "bsp/bsp.h"
#include "hal/hal_gpio.h"
#include "hal/hal_bsp.h"
#include "hal/hal_i2c.h"
#include "mcu/nrf52_hal.h"
#ifdef ARCH_sim
#include "mcu/mcu_sim.h"
#endif

#include <config/config.h>

#include <sensor/sensor.h>
#include "sensor/accel.h"
#include <lis2dh12/lis2dh12.h>

#if MYNEWT_VAL(DW1000_DEVICE_0)
#include <dw1000/dw1000_hal.h>
#endif

int accel_data_cb(struct sensor* sensor, void* a, void* databuf, sensor_type_t sensorType) {
    if(!databuf) { return SYS_EINVAL; }
    struct sensor_accel_data* sad = (struct sensor_accel_data*) databuf;
    if (!sad->sad_x_is_valid || !sad->sad_y_is_valid || !sad->sad_z_is_valid) { return SYS_EINVAL; }
    printf("%s: [ secs: %ld usecs: %d cputime: %u ]\n",
                   "LISTENER_CB",
                   (long int)sensor->s_sts.st_ostv.tv_sec,
                   (int)sensor->s_sts.st_ostv.tv_usec,
                   (unsigned int)sensor->s_sts.st_cputime);

    printf("x = %f, y = %f, z = %f\n", sad->sad_x, sad->sad_y, sad->sad_z);
    return 0;
}

static struct sensor_listener sensor_listener = {
    .sl_sensor_type = SENSOR_TYPE_ACCELEROMETER,
    .sl_func = accel_data_cb,  
    .sl_arg = NULL
};

int main(int argc, char **argv){
    sysinit();

    struct sensor* accel = sensor_mgr_find_next_bydevname("lis2dh12_0", NULL);
    assert(accel != NULL);

    assert(0 == lis2dh12_set_full_scale(&accel->s_itf, LIS2DH12_FS_2G));
    assert(0 == lis2dh12_set_op_mode(&accel->s_itf, LIS2DH12_OM_HIGH_RESOLUTION));

    sensor_set_poll_rate_ms(accel->s_dev->od_name, 1000 / 50);
    sensor_register_listener(accel, &sensor_listener);

    while (1) {
        os_eventq_run(os_eventq_dflt_get());
    }
    assert(0);
    return 0;
}