small interface improvements, renaming of device specific modules, starting with TTGO T-Wristband support

This commit is contained in:
Lurkars
2020-12-15 13:12:35 +01:00
parent 48528e027e
commit 17ebf801f1
45 changed files with 850 additions and 54 deletions
@@ -0,0 +1,7 @@
idf_component_register(
SRCS
"lsm9ds1.c"
INCLUDE_DIRS "."
PRIV_REQUIRES
i2c-main
)
+127
View File
@@ -0,0 +1,127 @@
// Copyright 2020 Lukas Haubaum
//
// Licensed under the GNU Affero General Public License, Version 3;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.gnu.org/licenses/agpl-3.0.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <time.h>
#include "driver/i2c.h"
#include "esp_log.h"
#include "i2c-main.h"
#include "lsm9ds1.h"
float aRes, gRes;
void lsm9ds1_i2c_read_bytes(uint8_t driver_addr, uint8_t start_addr, uint8_t number_Bytes, uint8_t *read_buffer)
{
i2c_cmd_handle_t cmd;
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (driver_addr << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, start_addr, true);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (driver_addr << 1) | I2C_MASTER_READ, true);
i2c_master_read(cmd, read_buffer, number_Bytes, I2C_MASTER_LAST_NACK);
i2c_master_stop(cmd);
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_cmd_begin(I2C_NUM_0, cmd, 10 / portTICK_PERIOD_MS));
i2c_cmd_link_delete(cmd);
}
void lsm9ds1_i2c_write_bytes(uint8_t driver_addr, uint8_t start_addr, uint8_t number_Bytes, uint8_t *write_buffer)
{
i2c_cmd_handle_t cmd;
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (driver_addr << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, start_addr, true);
i2c_master_write(cmd, write_buffer, number_Bytes, true);
i2c_master_stop(cmd);
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_cmd_begin(I2C_NUM_0, cmd, 10 / portTICK_PERIOD_MS));
i2c_cmd_link_delete(cmd);
}
int lsm9ds1_start(void)
{
unsigned char regdata;
// init ACC
regdata = 0x38;
lsm9ds1_i2c_write_bytes(ACC_ADDR, CTRL_REG5_A, 1, &regdata);
regdata = 0xC0;
lsm9ds1_i2c_write_bytes(ACC_ADDR, CTRL_REG6_A, 1, &regdata);
lsm9ds1_i2c_read_bytes(ACC_ADDR, CTRL_REG6_A, 1, &regdata);
regdata &= ~(0b00011000);
regdata |= ACCELRANGE_16G;
lsm9ds1_i2c_write_bytes(ACC_ADDR, CTRL_REG6_A, 1, &regdata);
// init gyr
regdata = 0xC0;
lsm9ds1_i2c_write_bytes(GYR_ADDR, CTRL_REG1_G, 1, &regdata);
lsm9ds1_i2c_read_bytes(GYR_ADDR, CTRL_REG1_G, 1, &regdata);
regdata &= ~(0b00011000);
regdata |= GYROSCALE_500DPS;
lsm9ds1_i2c_write_bytes(GYR_ADDR, CTRL_REG1_G, 1, &regdata);
aRes = 16.0 / 32768.0;
gRes = 500.0 / 32768.0;
return 0;
}
void lsm9ds1_get_accel_adc(int16_t *ax, int16_t *ay, int16_t *az)
{
uint8_t buf[6];
lsm9ds1_i2c_read_bytes(ACC_ADDR, OUT_X_L_A, 6, buf);
*ax = ((int16_t)buf[0] << 8) | buf[1];
*ay = ((int16_t)buf[2] << 8) | buf[3];
*az = ((int16_t)buf[4] << 8) | buf[5];
}
void lsm9ds1_get_gyro_adc(int16_t *gx, int16_t *gy, int16_t *gz)
{
uint8_t buf[6];
lsm9ds1_i2c_read_bytes(GYR_ADDR, OUT_X_L_G, 6, buf);
*gx = ((uint16_t)buf[0] << 8) | buf[1];
*gy = ((uint16_t)buf[2] << 8) | buf[3];
*gz = ((uint16_t)buf[4] << 8) | buf[5];
}
void lsm9ds1_get_accel_data(float *ax, float *ay, float *az)
{
int16_t accX = 0;
int16_t accY = 0;
int16_t accZ = 0;
lsm9ds1_get_accel_adc(&accX, &accY, &accZ);
*ax = (float)accX * aRes;
*ay = (float)accY * aRes;
*az = (float)accZ * aRes;
}
void lsm9ds1_get_gyro_data(float *gx, float *gy, float *gz)
{
int16_t gyroX = 0;
int16_t gyroY = 0;
int16_t gyroZ = 0;
lsm9ds1_get_gyro_adc(&gyroX, &gyroY, &gyroZ);
*gx = (float)gyroX * gRes;
*gy = (float)gyroY * gRes;
*gz = (float)gyroZ * gRes;
}
+138
View File
@@ -0,0 +1,138 @@
/*
Note: The MPU6886 is an I2C sensor and uses the Arduino Wire library.
Because the sensor is not 5V tolerant, we are using a 3.3 V 8 MHz Pro Mini or
a 3.3 V Teensy 3.1. We have disabled the internal pull-ups used by the Wire
library in the Wire.h/twi.c utility file. We are also using the 400 kHz fast
I2C mode by setting the TWI_FREQ to 400000L /twi.h utility file.
*/
#ifndef _IMU_LSM9DS1_H_
#define _IMU_LSM9DS1_H_
#include "stdio.h"
#define ID_AG 0x6F
#define ID_A 0x6F
#define ID_G 0x6F
#define ID_M 0x3D
#define FILENAME "/dev/i2c-1"
#define MAG_ADDR 0x1E
#define ACC_ADDR 0x6B
#define GYR_ADDR 0x6B
// Shared Accelerometer/Gyroscope Addresses
#define WHO_AM_I_AG 0x0F
#define CTRL_REG1_AG 0x10
#define CTRL_REG2_AG 0x11
#define CTRL_REG3_AG 0x12
#define OUT_TEMP_L_AG 0x15
#define OUT_TEMP_H_AG 0x16
#define REG_STATUS_REG_AG 0x17
#define CTRL_REG4_AG 0x1E
#define CTRL_REG5_AG 0x1F
#define CTRL_REG6_AG 0x20
#define CTRL_REG7_AG 0x21
#define CTRL_REG8_AG 0x22
#define CTRL_REG9_AG 0x23
#define CTRL_REG10_AG 0x24
// Gyroscope addresses
#define WHO_AM_I_G 0x0F
#define CTRL_REG1_G 0x10
#define CTRL_REG2_G 0x11
#define CTRL_REG3_G 0x12
#define OUT_X_L_G 0x18
#define OUT_X_H_G 0x19
#define OUT_Y_L_G 0x1A
#define OUT_Y_H_G 0x1B
#define OUT_Z_L_G 0x1C
#define OUT_Z_H_G 0x1D
// Accelerometer addresses
#define WHO_AM_I_A 0x0F
#define CTRL_REG5_A 0x1F
#define CTRL_REG6_A 0x20
#define CTRL_REG7_A 0x21
#define OUT_X_L_A 0x28
#define OUT_X_H_A 0x29
#define OUT_Y_L_A 0x2A
#define OUT_Y_H_A 0x2B
#define OUT_Z_L_A 0x2C
#define OUT_Z_H_A 0x2D
// Magnetometer addresses
#define WHO_AM_I_M 0x0F
#define CTRL_REG1_M 0x20
#define CTRL_REG2_M 0x21
#define CTRL_REG3_M 0x22
#define CTRL_REG4_M 0x23
#define CTRL_REG5_M 0x24
#define REG_STATUS_REG_M 0x27
#define OUT_X_L_M 0x28
#define OUT_X_H_M 0x29
#define OUT_Y_L_M 0x2A
#define OUT_Y_H_M 0x2B
#define OUT_Z_L_M 0x2C
#define OUT_Z_H_M 0x2D
#define REG_CFG_M 0x30
#define INT_SRC_M 0x31
// Settings
#define ACCELRANGE_2G 0x0 << 3
#define ACCELRANGE_16G 0x1 << 3
#define ACCELRANGE_4G 0x2 << 3
#define ACCELRANGE_8G 0x3 << 3
#define ACCELDATARATE_POWERDOWN 0x0 << 4
#define ACCELDATARATE_3_125HZ 0x1 << 4
#define ACCELDATARATE_6_25HZ 0x2 << 4
#define ACCELDATARATE_12_5HZ 0x3 << 4
#define ACCELDATARATE_25HZ 0x4 << 4
#define ACCELDATARATE_50HZ 0x5 << 4
#define ACCELDATARATE_100HZ 0x6 << 4
#define ACCELDATARATE_200HZ 0x7 << 4
#define ACCELDATARATE_400HZ 0x8 << 4
#define ACCELDATARATE_800HZ 0x9 << 4
#define ACCELDATARATE_1600HZ 0xa << 4
#define MAGGAIN_4GAUSS 0x0 << 5
#define MAGGAIN_8GAUSS 0x1 << 5
#define MAGGAIN_12GAUSS 0x2 << 5
#define MAGGAIN_16GAUSS 0x3 << 5
#define MAGDATARATE_3_125HZ 0x0 << 2
#define MAGDATARATE_6_25HZ 0x1 << 2
#define MAGDATARATE_12_5HZ 0x2 << 2
#define MAGDATARATE_25HZ 0x3 << 2
#define MAGDATARATE_50HZ 0x4 << 2
#define MAGDATARATE_100HZ 0x5 << 2
#define GYROSCALE_245DPS 0x0 << 4
#define GYROSCALE_500DPS 0x1 << 4
#define GYROSCALE_2000DPS 0x2 << 4
/* Conversions */
#define GRAVITY (9.80665F)
#define ACCEL_MG_LSB_2G (0.061F)
#define ACCEL_MG_LSB_4G (0.122F)
#define ACCEL_MG_LSB_8G (0.244F)
#define ACCEL_MG_LSB_16G (0.732F) // Is this right? Was expecting 0.488F
#define MAG_MGAUSS_4GAUSS (0.16F)
#define MAG_MGAUSS_8GAUSS (0.32F)
#define MAG_MGAUSS_12GAUSS (0.48F)
#define MAG_MGAUSS_16GAUSS (0.58F)
#define GYRO_DPS_DIGIT_245DPS (0.00875F)
#define GYRO_DPS_DIGIT_500DPS (0.01750F)
#define GYRO_DPS_DIGIT_2000DPS (0.07000F)
int lsm9ds1_start(void);
void lsm9ds1_get_accel_adc(int16_t *ax, int16_t *ay, int16_t *az);
void lsm9ds1_get_gyro_adc(int16_t *gx, int16_t *gy, int16_t *gz);
void lsm9ds1_get_accel_data(float *ax, float *ay, float *az);
void lsm9ds1_get_gyro_data(float *gx, float *gy, float *gz);
#endif