Compare commits
5 Commits
61dfa80322
...
26827db6b6
Author | SHA1 | Date | |
---|---|---|---|
26827db6b6 | |||
d0ad1bdc91 | |||
643ab436fb | |||
66bd1cadce | |||
b7febb1bfe |
2
Makefile
2
Makefile
@ -1,7 +1,7 @@
|
|||||||
TARGET = mgos_i2c
|
TARGET = mgos_i2c
|
||||||
CC = gcc
|
CC = gcc
|
||||||
# CFLAGS = -g -O -Wall -Wextra -std=c89 -pedantic -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition
|
# CFLAGS = -g -O -Wall -Wextra -std=c89 -pedantic -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition
|
||||||
CFLAGS = -g -O2 -pedantic -Werror -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition
|
CFLAGS = -g -O2 -Werror -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition
|
||||||
LINKER = gcc
|
LINKER = gcc
|
||||||
LFLAGS = -O -Wall -I. -lm
|
LFLAGS = -O -Wall -I. -lm
|
||||||
|
|
||||||
|
@ -45,6 +45,7 @@ bool mgos_i2c_getbits_reg_b(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, ui
|
|||||||
|
|
||||||
bool mgos_i2c_setbits_reg_w(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, uint8_t bitoffset, uint8_t bitlen, uint16_t value) {
|
bool mgos_i2c_setbits_reg_w(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, uint8_t bitoffset, uint8_t bitlen, uint16_t value) {
|
||||||
uint16_t old, new;
|
uint16_t old, new;
|
||||||
|
uint8_t d[2];
|
||||||
|
|
||||||
if (!i2c || bitoffset + bitlen > 16 || bitlen == 0) {
|
if (!i2c || bitoffset + bitlen > 16 || bitlen == 0) {
|
||||||
return false;
|
return false;
|
||||||
@ -53,14 +54,18 @@ bool mgos_i2c_setbits_reg_w(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, ui
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mgos_i2c_read_reg_n(i2c, addr, reg, 2, (uint8_t *)&old)) {
|
if (!mgos_i2c_read_reg_n(i2c, addr, reg, 2, d)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
old = (d[0] << 8) | d[1];
|
||||||
new = old | (((1 << bitlen) - 1) << bitoffset);
|
new = old | (((1 << bitlen) - 1) << bitoffset);
|
||||||
new &= ~(((1 << bitlen) - 1) << bitoffset);
|
new &= ~(((1 << bitlen) - 1) << bitoffset);
|
||||||
new |= (value) << bitoffset;
|
new |= (value) << bitoffset;
|
||||||
|
|
||||||
if (!mgos_i2c_write_reg_n(i2c, addr, reg, 2, (uint8_t *)&new)) {
|
d[0] = new >> 8;
|
||||||
|
d[1] = new & 0xFF;
|
||||||
|
|
||||||
|
if (!mgos_i2c_write_reg_n(i2c, addr, reg, 2, d)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -68,14 +73,16 @@ bool mgos_i2c_setbits_reg_w(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, ui
|
|||||||
|
|
||||||
bool mgos_i2c_getbits_reg_w(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, uint8_t bitoffset, uint8_t bitlen, uint16_t *value) {
|
bool mgos_i2c_getbits_reg_w(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, uint8_t bitoffset, uint8_t bitlen, uint16_t *value) {
|
||||||
uint16_t val, mask;
|
uint16_t val, mask;
|
||||||
|
uint8_t d[2];
|
||||||
|
|
||||||
if (!i2c || bitoffset + bitlen > 16 || bitlen == 0 || !value) {
|
if (!i2c || bitoffset + bitlen > 16 || bitlen == 0 || !value) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mgos_i2c_read_reg_n(i2c, addr, reg, 2, (uint8_t *)&val)) {
|
if (!mgos_i2c_read_reg_n(i2c, addr, reg, 2, d)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
val = (d[0] << 8) | d[1];
|
||||||
|
|
||||||
mask = ((1 << bitlen) - 1);
|
mask = ((1 << bitlen) - 1);
|
||||||
mask <<= bitoffset;
|
mask <<= bitoffset;
|
||||||
|
@ -118,6 +118,14 @@ void mgos_gpio_clear_int(int pin) {
|
|||||||
(void)pin;
|
(void)pin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool mgos_gpio_setup_input(int pin, enum mgos_gpio_pull_type pull) {
|
||||||
|
LOG(LL_INFO, ("Not implemented."));
|
||||||
|
return true;
|
||||||
|
|
||||||
|
(void)pin;
|
||||||
|
(void)pull;
|
||||||
|
}
|
||||||
|
|
||||||
bool mgos_gpio_set_mode(int pin, enum mgos_gpio_mode mode) {
|
bool mgos_gpio_set_mode(int pin, enum mgos_gpio_mode mode) {
|
||||||
LOG(LL_INFO, ("Not implemented."));
|
LOG(LL_INFO, ("Not implemented."));
|
||||||
return true;
|
return true;
|
||||||
|
34
tests/ads1x1x.c
Normal file
34
tests/ads1x1x.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#include "mgos.h"
|
||||||
|
#include "mgos_ads1x1x.h"
|
||||||
|
#include "tests_autogen.h"
|
||||||
|
|
||||||
|
uint32_t test_ads1x1x_period_ms = 100;
|
||||||
|
bool test_ads1x1x_enabled = true;
|
||||||
|
|
||||||
|
static struct mgos_ads1x1x *s_adc = NULL;
|
||||||
|
|
||||||
|
bool test_ads1x1x_create(void) {
|
||||||
|
// LOG(LL_INFO, ("Setting up"));
|
||||||
|
s_adc = mgos_ads1x1x_create(mgos_i2c_get_global(), 0x48, ADC_ADS1115);
|
||||||
|
if (!s_adc) return false;
|
||||||
|
|
||||||
|
mgos_ads1x1x_set_fsr(s_adc, MGOS_ADS1X1X_FSR_4096);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool test_ads1x1x_run(void) {
|
||||||
|
int16_t result[4];
|
||||||
|
for(int i=0; i<4; i++) {
|
||||||
|
if (!mgos_ads1x1x_read(s_adc, i, &result[i])) {
|
||||||
|
LOG(LL_ERROR, ("Could not read device"));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LOG(LL_INFO, ("chan={%6d, %6d, %6d, %6d}", result[0], result[1], result[2], result[3]));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool test_ads1x1x_destroy(void) {
|
||||||
|
mgos_ads1x1x_destroy(&s_adc);
|
||||||
|
return true;
|
||||||
|
}
|
23
tests/imu.c
23
tests/imu.c
@ -19,20 +19,31 @@ static struct mgos_imu *s_imu = NULL;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool test_imu_create(void) {
|
bool test_imu_create(void) {
|
||||||
|
struct mgos_imu_acc_opts acc_opts;
|
||||||
|
struct mgos_imu_gyro_opts gyro_opts;
|
||||||
|
struct mgos_imu_mag_opts mag_opts;
|
||||||
if (!(s_imu = mgos_imu_create())) {
|
if (!(s_imu = mgos_imu_create())) {
|
||||||
LOG(LL_ERROR, ("Cannot create IMU"));
|
LOG(LL_ERROR, ("Cannot create IMU"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ACC_I2CADDR > 0 && !mgos_imu_accelerometer_create_i2c(s_imu, mgos_i2c_get_global(), ACC_I2CADDR, ACC_TYPE)) {
|
acc_opts.type = ACC_TYPE;
|
||||||
|
acc_opts.scale = 16.0; // G
|
||||||
|
acc_opts.odr = 100; // Hz
|
||||||
|
if (ACC_I2CADDR > 0 && !mgos_imu_accelerometer_create_i2c(s_imu, mgos_i2c_get_global(), ACC_I2CADDR, &acc_opts))
|
||||||
LOG(LL_ERROR, ("Cannot create accelerometer on IMU"));
|
LOG(LL_ERROR, ("Cannot create accelerometer on IMU"));
|
||||||
}
|
|
||||||
if (GYRO_I2CADDR > 0 && !mgos_imu_gyroscope_create_i2c(s_imu, mgos_i2c_get_global(), GYRO_I2CADDR, GYRO_TYPE)) {
|
gyro_opts.type = ACC_TYPE;
|
||||||
|
gyro_opts.scale = 2000; // deg/sec
|
||||||
|
gyro_opts.odr = 100; // Hz
|
||||||
|
if (GYRO_I2CADDR > 0 && !mgos_imu_gyroscope_create_i2c(s_imu, mgos_i2c_get_global(), GYRO_I2CADDR, &gyro_opts))
|
||||||
LOG(LL_ERROR, ("Cannot create gyroscope on IMU"));
|
LOG(LL_ERROR, ("Cannot create gyroscope on IMU"));
|
||||||
}
|
|
||||||
if (MAG_I2CADDR > 0 && !mgos_imu_magnetometer_create_i2c(s_imu, mgos_i2c_get_global(), MAG_I2CADDR, MAG_TYPE)) {
|
mag_opts.type = MAG_TYPE;
|
||||||
|
mag_opts.scale = 12.0; // Gauss
|
||||||
|
mag_opts.odr = 10; // Hz
|
||||||
|
if (MAG_I2CADDR > 0 && !mgos_imu_magnetometer_create_i2c(s_imu, mgos_i2c_get_global(), MAG_I2CADDR, &mag_opts))
|
||||||
LOG(LL_ERROR, ("Cannot create magnetometer on IMU"));
|
LOG(LL_ERROR, ("Cannot create magnetometer on IMU"));
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user