add LIS2DH accelerometer driver, test app

This commit is contained in:
Joey Castillo 2021-11-27 13:32:15 -05:00
parent 002d368a25
commit 470544823b
6 changed files with 407 additions and 0 deletions

View file

@ -0,0 +1,91 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "watch.h"
#include "lis2dh.h"
// This application displays data from the old Sensor Watch Motion sensor board.
// Note that this board required A0 to be set high to power the sensor.
// Future accelerometer boards will be powered directly from VCC.
// Also note that this board has its INT1 pin wired to A1, which is not an external
// wake pin. Future accelerometer boards will wire interrupt pins to A2 and A4.
uint8_t axis = 0;
void cb_light_pressed() {
axis = 1;
}
void cb_mode_pressed() {
axis = 2;
}
void cb_alarm_pressed() {
axis = 3;
}
void app_init() {
gpio_set_pin_direction(A0, GPIO_DIRECTION_OUT);
gpio_set_pin_function(A0, GPIO_PIN_FUNCTION_OFF);
gpio_set_pin_level(A0, true);
watch_enable_display();
watch_enable_external_interrupts();
watch_register_interrupt_callback(BTN_MODE, cb_mode_pressed, INTERRUPT_TRIGGER_RISING);
watch_register_interrupt_callback(BTN_LIGHT, cb_light_pressed, INTERRUPT_TRIGGER_RISING);
watch_register_interrupt_callback(BTN_ALARM, cb_alarm_pressed, INTERRUPT_TRIGGER_RISING);
watch_enable_i2c();
lis2dh_begin();
lis2dh_set_range(LIS2DH_RANGE_2_G);
lis2dh_set_data_rate(LIS2DH_DATA_RATE_10_HZ);
}
void app_wake_from_backup() {
}
void app_setup() {
}
void app_prepare_for_standby() {
}
void app_wake_from_standby() {
}
bool app_loop() {
if (lis2dh_have_new_data()) {
lis2dh_reading reading;
lis2dh_acceleration_measurement measurement = lis2dh_get_acceleration_measurement(&reading);
// printf("%d,%d,%d\n", reading.x, reading.y, reading.z);
printf("%f,%f,%f\n", measurement.x, measurement.y, measurement.z);
char buf[11] = {0};
switch (axis) {
case 1:
sprintf(buf, "AC X%-6d", reading.x);
break;
case 2:
sprintf(buf, "AC Y%-6d", reading.y);
break;
case 3:
sprintf(buf, "AC Z%-6d", reading.z);
break;
default:
sprintf(buf, " %2d%2d%2d", abs(reading.x >> 9), abs(reading.y >> 9), abs(reading.z >> 9));
if (reading.x < 0) buf[0] = '_';
if (reading.y < 0) buf[1] = '_';
if (reading.z < 0) buf[3] = '_';
break;
}
watch_display_string(buf, 0);
}
return false;
}

View file

@ -0,0 +1 @@
build/

View file

@ -0,0 +1,10 @@
TOP = ../../..
include $(TOP)/make.mk
INCLUDES += \
-I../
SRCS += \
../app.c
include $(TOP)/rules.mk

View file

@ -61,6 +61,7 @@ INCLUDES += \
-I$(TOP)/watch-library/config/ \ -I$(TOP)/watch-library/config/ \
-I$(TOP)/watch-library/hw/ \ -I$(TOP)/watch-library/hw/ \
-I$(TOP)/watch-library/watch/ \ -I$(TOP)/watch-library/watch/ \
-I$(TOP)/watch-library/driver/ \
-I$(TOP)/watch-library -I$(TOP)/watch-library
SRCS += \ SRCS += \
@ -112,6 +113,7 @@ SRCS += \
$(TOP)/watch-library/hpl/sercom/hpl_sercom.c \ $(TOP)/watch-library/hpl/sercom/hpl_sercom.c \
$(TOP)/watch-library/hpl/slcd/hpl_slcd.c \ $(TOP)/watch-library/hpl/slcd/hpl_slcd.c \
$(TOP)/watch-library/hpl/systick/hpl_systick.c \ $(TOP)/watch-library/hpl/systick/hpl_systick.c \
$(TOP)/watch-library/driver/lis2dh.c \
DEFINES += \ DEFINES += \
-D__SAML22J18A__ \ -D__SAML22J18A__ \

View file

@ -0,0 +1,116 @@
/*
* MIT License
*
* Copyright (c) 2021 Joey Castillo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "lis2dh.h"
#include "watch.h"
bool lis2dh_begin() {
if (lis2dh_get_device_id() != LIS2DH_WHO_AM_I_VAL) {
return false;
}
// Enable all axes, start at lowest possible data rate
watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL1, LIS2DH_CTRL1_VAL_XEN |
LIS2DH_CTRL1_VAL_YEN |
LIS2DH_CTRL1_VAL_ZEN |
LIS2DH_CTRL1_VAL_ODR_1HZ);
// Enable block data update (output registers not updated until MSB and LSB have been read)
watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL4, LIS2DH_CTRL4_VAL_BDU);
return true;
}
uint8_t lis2dh_get_device_id() {
return watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_WHO_AM_I);
}
bool lis2dh_have_new_data() {
uint8_t retval = watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_STATUS);
return !!retval; // return true if any bit is set
}
lis2dh_reading lis2dh_get_raw_reading() {
uint8_t buffer[6];
uint8_t reg = LIS2DH_REG_OUT_X_L | 0x80; // set high bit for consecutive reads
lis2dh_reading retval;
watch_i2c_send(LIS2DH_ADDRESS, &reg, 1);
watch_i2c_receive(LIS2DH_ADDRESS, (uint8_t *)&buffer, 6);
retval.x = buffer[0];
retval.x |= ((uint16_t)buffer[1]) << 8;
retval.y = buffer[2];
retval.y |= ((uint16_t)buffer[3]) << 8;
retval.z = buffer[4];
retval.z |= ((uint16_t)buffer[5]) << 8;
return retval;
}
lis2dh_acceleration_measurement lis2dh_get_acceleration_measurement(lis2dh_reading *out_reading) {
lis2dh_reading reading = lis2dh_get_raw_reading();
uint8_t range = lis2dh_get_range();
if (out_reading != NULL) *out_reading = reading;
// this bit is cribbed from Adafruit's LIS3DH driver; from their notes, the magic number below
// converts from 16-bit lsb to 10-bit and divides by 1k to convert from milli-gs.
// final value is raw_lsb => 10-bit lsb -> milli-gs -> gs
uint8_t lsb_value = 1;
if (range == LIS2DH_RANGE_2_G) lsb_value = 4;
if (range == LIS2DH_RANGE_4_G) lsb_value = 8;
if (range == LIS2DH_RANGE_8_G) lsb_value = 16;
if (range == LIS2DH_RANGE_16_G) lsb_value = 48;
lis2dh_acceleration_measurement retval;
retval.x = lsb_value * ((float)reading.x / 64000.0);
retval.y = lsb_value * ((float)reading.y / 64000.0);
retval.z = lsb_value * ((float)reading.z / 64000.0);
return retval;
}
void lis2dh_set_range(lis2dh_range_t range) {
uint8_t val = watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL4) & 0xCF;
uint8_t bits = range << 4;
watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL4, val | bits);
}
lis2dh_range_t lis2dh_get_range() {
uint8_t retval = watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL4) & 0x30;
retval >>= 4;
return (lis2dh_range_t)retval;
}
void lis2dh_set_data_rate(lis2dh_data_rate_t dataRate) {
uint8_t val = watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL1) & 0x0F;
uint8_t bits = dataRate << 4;
watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL1, val | bits);
}
lis2dh_data_rate_t lis2dh_get_data_rate() {
return watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL1) >> 4;
}

View file

@ -0,0 +1,187 @@
/*
* MIT License
*
* Copyright (c) 2021 Joey Castillo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef LIS2DH_H
#define LIS2DH_H
#include <stdbool.h>
#include <stdint.h>
typedef struct {
int16_t x;
int16_t y;
int16_t z;
} lis2dh_reading;
typedef struct {
float x;
float y;
float z;
} lis2dh_acceleration_measurement;
typedef enum {
LIS2DH_RANGE_16_G = 0b11, // +/- 16g
LIS2DH_RANGE_8_G = 0b10, // +/- 8g
LIS2DH_RANGE_4_G = 0b01, // +/- 4g
LIS2DH_RANGE_2_G = 0b00 // +/- 2g (default value)
} lis2dh_range_t;
typedef enum {
LIS2DH_DATA_RATE_POWERDOWN = 0,
LIS2DH_DATA_RATE_1_HZ = 0b0001,
LIS2DH_DATA_RATE_10_HZ = 0b0010,
LIS2DH_DATA_RATE_25_HZ = 0b0011,
LIS2DH_DATA_RATE_50_HZ = 0b0100,
LIS2DH_DATA_RATE_100_HZ = 0b0101,
LIS2DH_DATA_RATE_200_HZ = 0b0110,
LIS2DH_DATA_RATE_400_HZ = 0b0111,
LIS2DH_DATA_RATE_LP1620HZ = 0b1000,
LIS2DH_DATA_RATE_LP5376HZ = 0b1001,
} lis2dh_data_rate_t;
bool lis2dh_begin();
uint8_t lis2dh_get_device_id();
bool lis2dh_have_new_data();
lis2dh_reading lis2dh_get_raw_reading();
lis2dh_acceleration_measurement lis2dh_get_acceleration_measurement(lis2dh_reading *out_reading);
void lis2dh_set_range(lis2dh_range_t range);
lis2dh_range_t lis2dh_get_range();
void lis2dh_set_data_rate(lis2dh_data_rate_t dataRate);
lis2dh_data_rate_t lis2dh_get_data_rate();
// Assumes SA0 is high; if low, its 0x18
#define LIS2DH_ADDRESS (0x19)
#define LIS2DH_REG_STATUS_AUX 0x07 ///< Auxiliary status register
#define LIS2DH_REG_STATUS_AUX_TDA (1 << 2) ///< Temperature data available
#define LIS2DH_REG_STATUS_AUX_TOR (1 << 6) ///< Temperature data overrun
#define LIS2DH_REG_OUT_TEMP_L 0x0C ///< Temperature data low bit
#define LIS2DH_REG_OUT_TEMP_H 0x0D ///< Temperature data high bit
#define LIS2DH_REG_INT_COUNTER 0x0E
#define LIS2DH_REG_WHO_AM_I 0x0F ///< Device identification, will read 0x33
#define LIS2DH_WHO_AM_I_VAL 0x33 ///< Expected value of the WHO_AM_I register
#define LIS2DH_REG_TEMP_CFG 0x1F ///< Temperature configuration; 0 to disable, 0xC0 to enable.
#define LIS2DH_TEMP_CFG_VAL_ENABLE 0xC0 ///< Value for LIS2DH_REG_TEMP_CFG that enables temperature sensing.
#define LIS2DH_TEMP_CFG_VAL_DISABLE 0x00 ///< Value for LIS2DH_REG_TEMP_CFG that disables temperature sensing.
#define LIS2DH_REG_CTRL1 0x20 ///< CTRL_REG1 in the data sheet.
#define LIS2DH_CTRL1_VAL_XEN 0b00000001 ///< Enable X-axis
#define LIS2DH_CTRL1_VAL_YEN 0b00000010 ///< Enable Y-axis
#define LIS2DH_CTRL1_VAL_ZEN 0b00000100 ///< Enable Z-axis
#define LIS2DH_CTRL1_VAL_LPEN 0b00001000 ///< Enable low power mode
#define LIS2DH_CTRL1_VAL_ODR_POWERDOWN 0 ///< Power down
#define LIS2DH_CTRL1_VAL_ODR_1HZ (LIS2DH_DATA_RATE_1_HZ << 4)
#define LIS2DH_CTRL1_VAL_ODR_10HZ (LIS2DH_DATA_RATE_10_HZ << 4)
#define LIS2DH_CTRL1_VAL_ODR_25HZ (LIS2DH_DATA_RATE_25_HZ << 4)
#define LIS2DH_CTRL1_VAL_ODR_50HZ (LIS2DH_DATA_RATE_50_HZ << 4)
#define LIS2DH_CTRL1_VAL_ODR_100HZ (LIS2DH_DATA_RATE_100_HZ << 4)
#define LIS2DH_CTRL1_VAL_ODR_200HZ (LIS2DH_DATA_RATE_200_HZ << 4)
#define LIS2DH_CTRL1_VAL_ODR_400HZ (LIS2DH_DATA_RATE_400_HZ << 4)
#define LIS2DH_CTRL1_VAL_ODR_LP1620HZ (LIS2DH_DATA_RATE_LP1620HZ << 4)
#define LIS2DH_CTRL1_VAL_ODR_LP5376HZ (LIS2DH_DATA_RATE_LP5376HZ << 4)
#define LIS2DH_REG_CTRL2 0x21
#define LIS2DH_REG_CTRL3 0x22
#define LIS2DH_CTRL3_VAL_I1_CLICK 0b10000000
#define LIS2DH_CTRL3_VAL_I1_AOI1 0b01000000
#define LIS2DH_CTRL3_VAL_I1_AOI2 0b00100000
#define LIS2DH_CTRL3_VAL_I1_DRDY1 0b00010000
#define LIS2DH_CTRL3_VAL_I1_DRDY2 0b00001000
#define LIS2DH_CTRL3_VAL_I1_WTM 0b00000100
#define LIS2DH_CTRL3_VAL_I1_OVERRUN 0b00000010
#define LIS2DH_REG_CTRL4 0x23
#define LIS2DH_CTRL4_VAL_BDU 0b10000000
#define LIS2DH_CTRL4_VAL_BLE 0b01000000
#define LIS2DH_CTRL4_VAL_RANGE_2G (LIS2DH_RANGE_2_G << 4)
#define LIS2DH_CTRL4_VAL_RANGE_4G (LIS2DH_RANGE_4_G << 4)
#define LIS2DH_CTRL4_VAL_RANGE_8G (LIS2DH_RANGE_8_G << 4)
#define LIS2DH_CTRL4_VAL_RANGE_16G (LIS2DH_RANGE_16_G << 4)
#define LIS2DH_CTRL4_VAL_HR 0b00001000
#define LIS2DH_CTRL4_VAL_ST0 0b00000000
#define LIS2DH_CTRL4_VAL_ST1 0b00000000
#define LIS2DH_REG_CTRL5 0x24
#define LIS2DH_CTRL5_VAL_BOOT 0b10000000
#define LIS2DH_CTRL5_VAL_FIFO_EN 0b01000000
#define LIS2DH_CTRL5_VAL_LIR_INT1 0b00001000
#define LIS2DH_CTRL5_VAL_D4D_INT1 0b00000100
#define LIS2DH_CTRL5_VAL_LIR_INT2 0b00000010
#define LIS2DH_CTRL5_VAL_D4D_INT2 0b00000001
#define LIS2DH_REG_CTRL6 0x25
#define LIS2DH_CTRL6_VAL_I2_CLICK 0b10000000
#define LIS2DH_CTRL6_VAL_I2_INT1 0b01000000
#define LIS2DH_CTRL6_VAL_I2_INT2 0b00100000
#define LIS2DH_CTRL6_VAL_BOOT_I2 0b00010000
#define LIS2DH_CTRL6_VAL_P2_ACT 0b00001000
#define LIS2DH_CTRL6_VAL_H_L_ACTIVE 0b00000000
#define LIS2DH_REG_REFERENCE 0x26
#define LIS2DH_REG_STATUS 0x27
#define LIS2DH_STATUS_VAL_ZYXOR 0b10000000
#define LIS2DH_STATUS_VAL_ZOR 0b01000000
#define LIS2DH_STATUS_VAL_YOR 0b00100000
#define LIS2DH_STATUS_VAL_XOR 0b00010000
#define LIS2DH_STATUS_VAL_ZYXDA 0b00001000
#define LIS2DH_STATUS_VAL_ZDA 0b00000100
#define LIS2DH_STATUS_VAL_YDA 0b00000010
#define LIS2DH_STATUS_VAL_XDA 0b00000001
#define LIS2DH_REG_OUT_X_L 0x28
#define LIS2DH_REG_OUT_X_H 0x29
#define LIS2DH_REG_OUT_Y_L 0x2A
#define LIS2DH_REG_OUT_Y_H 0x2B
#define LIS2DH_REG_OUT_Z_L 0x2C
#define LIS2DH_REG_OUT_Z_H 0x2D
#define LIS2DH_REG_FIFO_CTRL 0x2E
#define LIS2DH_REG_FIFO_SRC 0x2F
#define LIS2DH_REG_INT1_CFG 0x30
#define LIS2DH_REG_INT1_SRC 0x31
#define LIS2DH_REG_INT1_THS 0x32
#define LIS2DH_REG_INT1_DUR 0x33
#define LIS2DH_REG_CLICK_CFG 0x38
#define LIS2DH_REG_CLICK_SRC 0x39
#define LIS2DH_REG_CLICK_THS 0x3A
#define LIS2DH_REG_TIME_LIMIT 0x3B
#define LIS2DH_REG_TIME_LATENCY 0x3C
#define LIS2DH_REG_TIME_WINDOW 0x3D
#endif // LIS2DH_H