bmp280 ds1307 hmc5883l update

This commit is contained in:
lilian 2017-08-04 14:39:31 -03:00
parent 60cbd9c710
commit 13efa882d2
10 changed files with 169 additions and 160 deletions

View file

@ -12,7 +12,7 @@
// In forced mode user initiate measurement each time.
// In normal mode measurement is done continuously with specified standby time.
// #define MODE_FORCED
const uint8_t i2c_bus = 0;
const uint8_t scl_pin = 0;
const uint8_t sda_pin = 2;
@ -26,7 +26,8 @@ static void bmp280_task_forced(void *pvParameters)
params.mode = BMP280_MODE_FORCED;
bmp280_t bmp280_dev;
bmp280_dev.i2c_addr = BMP280_I2C_ADDRESS_0;
bmp280_dev.i2c_dev.bus = i2c_bus;
bmp280_dev.i2c_dev.addr = BMP280_I2C_ADDRESS_0;
while (1) {
while (!bmp280_init(&bmp280_dev, &params)) {
@ -67,7 +68,8 @@ static void bmp280_task_normal(void *pvParameters)
bmp280_init_default_params(&params);
bmp280_t bmp280_dev;
bmp280_dev.i2c_addr = BMP280_I2C_ADDRESS_0;
bmp280_dev.i2c_dev.bus = i2c_bus;
bmp280_dev.i2c_dev.addr = BMP280_I2C_ADDRESS_0;
while (1) {
while (!bmp280_init(&bmp280_dev, &params)) {
@ -103,7 +105,7 @@ void user_init(void)
printf("SDK version : %s\n", sdk_system_get_sdk_version());
printf("GIT version : %s\n", GITSHORTREV);
i2c_init(scl_pin, sda_pin);
i2c_init(i2c_bus, scl_pin, sda_pin, I2C_FREQ_400K);
#ifdef MODE_FORCED
xTaskCreate(bmp280_task_forced, "bmp280_task", 256, NULL, 2, NULL);

View file

@ -11,6 +11,7 @@
#include <ds1307/ds1307.h>
#include <stdio.h>
#define I2C_BUS 0
#define SCL_PIN 5
#define SDA_PIN 4
@ -19,8 +20,12 @@ void user_init(void)
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
i2c_init(SCL_PIN, SDA_PIN);
ds1307_start(true);
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_400K);
i2c_dev_t dev = {
.addr = DS1307_ADDR,
.bus = I2C_BUS,
};
ds1307_start(&dev, true);
// setup datetime: 2016-10-09 13:50:10
struct tm time = {
@ -31,11 +36,11 @@ void user_init(void)
.tm_min = 50,
.tm_sec = 10
};
ds1307_set_time(&time);
ds1307_set_time(&dev, &time);
while (true)
{
ds1307_get_time(&time);
ds1307_get_time(&dev, &time);
printf("%04d-%02d-%02d %02d:%02d:%02d\n", time.tm_year, time.tm_mon + 1,
time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec);

View file

@ -11,6 +11,7 @@
#include <i2c/i2c.h>
#include <hmc5883l/hmc5883l.h>
#define I2C_BUS 0
#define SCL_PIN 5
#define SDA_PIN 4
@ -19,20 +20,24 @@ void user_init(void)
uart_set_baud(0, 115200);
printf("SDK version:%s\n\n", sdk_system_get_sdk_version());
i2c_init(SCL_PIN, SDA_PIN);
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
i2c_dev_t dev = {
.addr = HMC5883L_ADDR,
.bus = I2C_BUS,
};
while (!hmc5883l_init())
while (!hmc5883l_init(&dev))
printf("Device not found\n");
hmc5883l_set_operating_mode(HMC5883L_MODE_CONTINUOUS);
hmc5883l_set_samples_averaged(HMC5883L_SAMPLES_8);
hmc5883l_set_data_rate(HMC5883L_DATA_RATE_07_50);
hmc5883l_set_gain(HMC5883L_GAIN_1090);
hmc5883l_set_operating_mode(&dev, HMC5883L_MODE_CONTINUOUS);
hmc5883l_set_samples_averaged(&dev, HMC5883L_SAMPLES_8);
hmc5883l_set_data_rate(&dev, HMC5883L_DATA_RATE_07_50);
hmc5883l_set_gain(&dev, HMC5883L_GAIN_1090);
while (true)
{
hmc5883l_data_t data;
hmc5883l_get_data(&data);
hmc5883l_get_data(&dev, &data);
printf("Magnetic data: X:%.2f mG, Y:%.2f mG, Z:%.2f mG\n", data.x, data.y, data.z);
for (uint32_t i = 0; i < 1000; i++)

View file

@ -179,4 +179,3 @@ uint16_t ad770x_raw_adc_value(const ad770x_params_t *params, uint8_t channel)
prepare(channel, REG_DATA, true, params->cs_pin, false);
return read_word(params->cs_pin);
}

View file

@ -23,7 +23,6 @@
*/
#include <stddef.h>
#include "bmp280.h"
#include "i2c/i2c.h"
#ifdef BMP280_DEBUG
#include <stdio.h>
@ -66,37 +65,36 @@ void bmp280_init_default_params(bmp280_params_t *params)
params->standby = BMP280_STANDBY_250;
}
static bool read_register16(uint8_t i2c_addr, uint8_t addr, uint16_t *value)
static bool read_register16(i2c_dev_t* dev, uint8_t addr, uint16_t *value)
{
uint8_t d[] = {0, 0};
if (!i2c_slave_read(i2c_addr, &addr, d, sizeof(d))) {
if (!i2c_slave_read(dev->bus, dev->addr, &addr, d, sizeof(d))) {
*value = d[0] | (d[1] << 8);
return true;
}
return false;
}
static inline int read_data(uint8_t i2c_addr, uint8_t addr, uint8_t *value, uint8_t len)
static inline int read_data(i2c_dev_t* dev, uint8_t addr, uint8_t *value, uint8_t len)
{
return i2c_slave_read(i2c_addr, &addr, value, len);
return i2c_slave_read(dev->bus, dev->addr, &addr, value, len);
}
static bool read_calibration_data(bmp280_t *dev)
{
uint8_t i2c_addr = dev->i2c_addr;
if (read_register16(i2c_addr, 0x88, &dev->dig_T1) &&
read_register16(i2c_addr, 0x8a, (uint16_t *)&dev->dig_T2) &&
read_register16(i2c_addr, 0x8c, (uint16_t *)&dev->dig_T3) &&
read_register16(i2c_addr, 0x8e, &dev->dig_P1) &&
read_register16(i2c_addr, 0x90, (uint16_t *)&dev->dig_P2) &&
read_register16(i2c_addr, 0x92, (uint16_t *)&dev->dig_P3) &&
read_register16(i2c_addr, 0x94, (uint16_t *)&dev->dig_P4) &&
read_register16(i2c_addr, 0x96, (uint16_t *)&dev->dig_P5) &&
read_register16(i2c_addr, 0x98, (uint16_t *)&dev->dig_P6) &&
read_register16(i2c_addr, 0x9a, (uint16_t *)&dev->dig_P7) &&
read_register16(i2c_addr, 0x9c, (uint16_t *)&dev->dig_P8) &&
read_register16(i2c_addr, 0x9e, (uint16_t *)&dev->dig_P9)) {
if (read_register16(&dev->i2c_dev, 0x88, &dev->dig_T1) &&
read_register16(&dev->i2c_dev, 0x8a, (uint16_t *)&dev->dig_T2) &&
read_register16(&dev->i2c_dev, 0x8c, (uint16_t *)&dev->dig_T3) &&
read_register16(&dev->i2c_dev, 0x8e, &dev->dig_P1) &&
read_register16(&dev->i2c_dev, 0x90, (uint16_t *)&dev->dig_P2) &&
read_register16(&dev->i2c_dev, 0x92, (uint16_t *)&dev->dig_P3) &&
read_register16(&dev->i2c_dev, 0x94, (uint16_t *)&dev->dig_P4) &&
read_register16(&dev->i2c_dev, 0x96, (uint16_t *)&dev->dig_P5) &&
read_register16(&dev->i2c_dev, 0x98, (uint16_t *)&dev->dig_P6) &&
read_register16(&dev->i2c_dev, 0x9a, (uint16_t *)&dev->dig_P7) &&
read_register16(&dev->i2c_dev, 0x9c, (uint16_t *)&dev->dig_P8) &&
read_register16(&dev->i2c_dev, 0x9e, (uint16_t *)&dev->dig_P9)) {
debug("Calibration data received:");
debug("dig_T1=%d", dev->dig_T1);
@ -120,15 +118,14 @@ static bool read_calibration_data(bmp280_t *dev)
static bool read_hum_calibration_data(bmp280_t *dev)
{
uint8_t i2c_addr = dev->i2c_addr;
uint16_t h4, h5;
if (!read_data(i2c_addr, 0xa1, &dev->dig_H1, 1) &&
read_register16(i2c_addr, 0xe1, (uint16_t *)&dev->dig_H2) &&
!read_data(i2c_addr, 0xe3, &dev->dig_H3, 1) &&
read_register16(i2c_addr, 0xe4, &h4) &&
read_register16(i2c_addr, 0xe5, &h5) &&
!read_data(i2c_addr, 0xe7, (uint8_t *)&dev->dig_H6, 1)) {
if (!read_data(&dev->i2c_dev, 0xa1, &dev->dig_H1, 1) &&
read_register16(&dev->i2c_dev, 0xe1, (uint16_t *)&dev->dig_H2) &&
!read_data(&dev->i2c_dev, 0xe3, &dev->dig_H3, 1) &&
read_register16(&dev->i2c_dev, 0xe4, &h4) &&
read_register16(&dev->i2c_dev, 0xe5, &h5) &&
!read_data(&dev->i2c_dev, 0xe7, (uint8_t *)&dev->dig_H6, 1)) {
dev->dig_H4 = (h4 & 0x00ff) << 4 | (h4 & 0x0f00) >> 8;
dev->dig_H5 = h5 >> 4;
debug("Calibration data received:");
@ -144,21 +141,20 @@ static bool read_hum_calibration_data(bmp280_t *dev)
return false;
}
static int write_register8(uint8_t i2c_addr, uint8_t addr, uint8_t value)
static int write_register8(i2c_dev_t* dev, uint8_t addr, uint8_t value)
{
return i2c_slave_write(i2c_addr, &addr, &value, 1);
return i2c_slave_write(dev->bus, dev->addr, &addr, &value, 1);
}
bool bmp280_init(bmp280_t *dev, bmp280_params_t *params)
{
uint8_t i2c_addr = dev->i2c_addr;
if (i2c_addr != BMP280_I2C_ADDRESS_0 && i2c_addr != BMP280_I2C_ADDRESS_1) {
if (dev->i2c_dev.addr != BMP280_I2C_ADDRESS_0 && dev->i2c_dev.addr != BMP280_I2C_ADDRESS_1) {
debug("Invalid I2C address");
return false;
}
if (read_data(i2c_addr, BMP280_REG_ID, &dev->id, 1)) {
if (read_data(&dev->i2c_dev, BMP280_REG_ID, &dev->id, 1)) {
debug("Sensor not found");
return false;
}
@ -169,7 +165,7 @@ bool bmp280_init(bmp280_t *dev, bmp280_params_t *params)
}
// Soft reset.
if (write_register8(i2c_addr, BMP280_REG_RESET, BMP280_RESET_VALUE)) {
if (write_register8(&dev->i2c_dev, BMP280_REG_RESET, BMP280_RESET_VALUE)) {
debug("Failed resetting sensor");
return false;
}
@ -177,7 +173,7 @@ bool bmp280_init(bmp280_t *dev, bmp280_params_t *params)
// Wait until finished copying over the NVP data.
while (1) {
uint8_t status;
if (!read_data(i2c_addr, BMP280_REG_STATUS, &status, 1) && (status & 1) == 0)
if (!read_data(&dev->i2c_dev, BMP280_REG_STATUS, &status, 1) && (status & 1) == 0)
break;
}
@ -193,7 +189,7 @@ bool bmp280_init(bmp280_t *dev, bmp280_params_t *params)
uint8_t config = (params->standby << 5) | (params->filter << 2);
debug("Writing config reg=%x", config);
if (write_register8(i2c_addr, BMP280_REG_CONFIG, config)) {
if (write_register8(&dev->i2c_dev, BMP280_REG_CONFIG, config)) {
debug("Failed configuring sensor");
return false;
}
@ -210,14 +206,14 @@ bool bmp280_init(bmp280_t *dev, bmp280_params_t *params)
// Write crtl hum reg first, only active after write to BMP280_REG_CTRL.
uint8_t ctrl_hum = params->oversampling_humidity;
debug("Writing ctrl hum reg=%x", ctrl_hum);
if (write_register8(i2c_addr, BMP280_REG_CTRL_HUM, ctrl_hum)) {
if (write_register8(&dev->i2c_dev, BMP280_REG_CTRL_HUM, ctrl_hum)) {
debug("Failed controlling sensor");
return false;
}
}
debug("Writing ctrl reg=%x", ctrl);
if (write_register8(i2c_addr, BMP280_REG_CTRL, ctrl)) {
if (write_register8(&dev->i2c_dev, BMP280_REG_CTRL, ctrl)) {
debug("Failed controlling sensor");
return false;
}
@ -228,12 +224,12 @@ bool bmp280_init(bmp280_t *dev, bmp280_params_t *params)
bool bmp280_force_measurement(bmp280_t *dev)
{
uint8_t ctrl;
if (read_data(dev->i2c_addr, BMP280_REG_CTRL, &ctrl, 1))
if (read_data(&dev->i2c_dev, BMP280_REG_CTRL, &ctrl, 1))
return false;
ctrl &= ~0b11; // clear two lower bits
ctrl |= BMP280_MODE_FORCED;
debug("Writing ctrl reg=%x", ctrl);
if (write_register8(dev->i2c_addr, BMP280_REG_CTRL, ctrl)) {
if (write_register8(&dev->i2c_dev, BMP280_REG_CTRL, ctrl)) {
debug("Failed starting forced mode");
return false;
}
@ -243,7 +239,7 @@ bool bmp280_force_measurement(bmp280_t *dev)
bool bmp280_is_measuring(bmp280_t *dev)
{
uint8_t status;
if (read_data(dev->i2c_addr, BMP280_REG_STATUS, &status, 1))
if (read_data(&dev->i2c_dev, BMP280_REG_STATUS, &status, 1))
return false;
if (status & (1 << 3)) {
debug("Status: measuring");
@ -345,7 +341,7 @@ bool bmp280_read_fixed(bmp280_t *dev, int32_t *temperature,
// Need to read in one sequence to ensure they match.
size_t size = humidity ? 8 : 6;
if (read_data(dev->i2c_addr, 0xf7, data, size)) {
if (read_data(&dev->i2c_dev, 0xf7, data, size)) {
debug("Failed reading");
return false;
}

View file

@ -26,6 +26,7 @@
#include <stdint.h>
#include <stdbool.h>
#include "i2c/i2c.h"
#ifdef __cplusplus
extern "C" {
@ -127,7 +128,7 @@ typedef struct {
int16_t dig_H5;
int8_t dig_H6;
uint8_t i2c_addr; /* I2C address. */
i2c_dev_t i2c_dev; /* I2C dev setting. */
uint8_t id; /* Chip ID */
} bmp280_t;

View file

@ -6,10 +6,8 @@
* BSD Licensed as described in the file LICENSE
*/
#include "ds1307.h"
#include <i2c/i2c.h>
#include <stdio.h>
#define ADDR 0x68
#define RAM_SIZE 56
#define TIME_REG 0
@ -40,36 +38,36 @@ static uint8_t dec2bcd(uint8_t val)
return ((val / 10) << 4) + (val % 10);
}
static uint8_t read_register(uint8_t reg)
static uint8_t read_register(i2c_dev_t* dev, uint8_t reg)
{
uint8_t val;
i2c_slave_read(ADDR, &reg, &val, 1);
i2c_slave_read(dev->bus, dev->addr, &reg, &val, 1);
return val;
}
static void update_register(uint8_t reg, uint8_t mask, uint8_t val)
static void update_register(i2c_dev_t* dev, uint8_t reg, uint8_t mask, uint8_t val)
{
uint8_t buf = (read_register(reg) & mask) | val;
uint8_t buf = (read_register(dev,reg) & mask) | val;
i2c_slave_write(ADDR, &reg, &buf, 1);
i2c_slave_write(dev->bus, dev->addr, &reg, &buf, 1);
}
void ds1307_start(bool start)
void ds1307_start(i2c_dev_t* dev, bool start)
{
update_register(TIME_REG, CH_MASK, start ? 0 : CH_BIT);
update_register(dev, TIME_REG, CH_MASK, start ? 0 : CH_BIT);
}
bool ds1307_is_running()
bool ds1307_is_running(i2c_dev_t* dev)
{
return !(read_register(TIME_REG) & CH_BIT);
return !(read_register(dev, TIME_REG) & CH_BIT);
}
void ds1307_get_time(struct tm *time)
void ds1307_get_time(i2c_dev_t* dev, struct tm *time)
{
uint8_t buf[7];
uint8_t reg = TIME_REG ;
i2c_slave_read(ADDR, &reg , buf, 7);
i2c_slave_read(dev->bus, dev->addr, &reg , buf, 7);
time->tm_sec = bcd2dec(buf[0] & SECONDS_MASK);
time->tm_min = bcd2dec(buf[1]);
@ -87,7 +85,7 @@ void ds1307_get_time(struct tm *time)
time->tm_year = bcd2dec(buf[6]) + 2000;
}
void ds1307_set_time(const struct tm *time)
void ds1307_set_time(i2c_dev_t* dev, const struct tm *time)
{
uint8_t buf[8];
buf[0] = TIME_REG;
@ -99,51 +97,51 @@ void ds1307_set_time(const struct tm *time)
buf[6] = dec2bcd(time->tm_mon + 1);
buf[7] = dec2bcd(time->tm_year - 2000);
i2c_slave_write(ADDR, &buf[0], &buf[1] , 7);
i2c_slave_write(dev->bus, dev->addr, &buf[0], &buf[1] , 7);
}
void ds1307_enable_squarewave(bool enable)
void ds1307_enable_squarewave(i2c_dev_t* dev, bool enable)
{
update_register(CONTROL_REG, SQWE_MASK, enable ? SQWE_BIT : 0);
update_register(dev, CONTROL_REG, SQWE_MASK, enable ? SQWE_BIT : 0);
}
bool ds1307_is_squarewave_enabled()
bool ds1307_is_squarewave_enabled(i2c_dev_t* dev)
{
return read_register(CONTROL_REG) & SQWE_BIT;
return read_register(dev, CONTROL_REG) & SQWE_BIT;
}
void ds1307_set_squarewave_freq(ds1307_squarewave_freq_t freq)
void ds1307_set_squarewave_freq(i2c_dev_t* dev, ds1307_squarewave_freq_t freq)
{
update_register(CONTROL_REG, SQWEF_MASK, (uint8_t)freq);
update_register(dev, CONTROL_REG, SQWEF_MASK, (uint8_t)freq);
}
ds1307_squarewave_freq_t ds1307_get_squarewave_freq()
ds1307_squarewave_freq_t ds1307_get_squarewave_freq(i2c_dev_t* dev)
{
return (ds1307_squarewave_freq_t)(read_register(CONTROL_REG) & SQWEF_MASK);
return (ds1307_squarewave_freq_t)(read_register(dev, CONTROL_REG) & SQWEF_MASK);
}
bool ds1307_get_output()
bool ds1307_get_output(i2c_dev_t* dev)
{
return read_register(CONTROL_REG) & OUT_BIT;
return read_register(dev, CONTROL_REG) & OUT_BIT;
}
void ds1307_set_output(bool value)
void ds1307_set_output(i2c_dev_t* dev, bool value)
{
update_register(CONTROL_REG, OUT_MASK, value ? OUT_BIT : 0);
update_register(dev, CONTROL_REG, OUT_MASK, value ? OUT_BIT : 0);
}
int ds1307_read_ram(uint8_t offset, uint8_t *buf, uint8_t len)
int ds1307_read_ram(i2c_dev_t* dev, uint8_t offset, uint8_t *buf, uint8_t len)
{
if (offset + len > RAM_SIZE) return false;
uint8_t reg = RAM_REG + offset ;
return i2c_slave_read(ADDR, &reg, buf, len);
return i2c_slave_read(dev->bus, dev->addr, &reg, buf, len);
}
int ds1307_write_ram(uint8_t offset, uint8_t *buf, uint8_t len)
int ds1307_write_ram(i2c_dev_t* dev, uint8_t offset, uint8_t *buf, uint8_t len)
{
if (offset + len > RAM_SIZE) return false;
uint8_t reg = RAM_REG + offset ;
return i2c_slave_write(ADDR, &reg, buf, len);
return i2c_slave_write(dev->bus, dev->addr, &reg, buf, len);
}

View file

@ -11,11 +11,13 @@
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <i2c/i2c.h>
#ifdef __cplusplus
extern "C" {
#endif
#define DS1307_ADDR 0x68
/**
* Squarewave frequency
*/
@ -31,62 +33,62 @@ typedef enum _ds1307_squarewave_freq_t
* \brief Start/stop clock
* \param start Start clock if true
*/
void ds1307_start(bool start);
void ds1307_start(i2c_dev_t* dev, bool start);
/**
* \brief Get current clock state
* \return true if clock running
*/
bool ds1307_is_running();
bool ds1307_is_running(i2c_dev_t* dev);
/**
* \brief Get current time
* \param time Pointer to the time struct to fill
*/
void ds1307_get_time(struct tm *time);
void ds1307_get_time(i2c_dev_t* dev, struct tm *time);
/**
* \brief Set time to RTC
* \param time Pointer to the time struct
*/
void ds1307_set_time(const struct tm *time);
void ds1307_set_time(i2c_dev_t* dev, const struct tm *time);
/**
* \brief Enable or disable square-wave oscillator output
* \param enable Enable oscillator if true
*/
void ds1307_enable_squarewave(bool enable);
void ds1307_enable_squarewave(i2c_dev_t* dev, bool enable);
/**
* \brief Get square-wave oscillator output
* \return true if square-wave oscillator enabled
*/
bool ds1307_is_squarewave_enabled();
bool ds1307_is_squarewave_enabled(i2c_dev_t* dev);
/**
* \brief Set square-wave oscillator frequency
* \param freq Frequency
*/
void ds1307_set_squarewave_freq(ds1307_squarewave_freq_t freq);
void ds1307_set_squarewave_freq(i2c_dev_t* dev, ds1307_squarewave_freq_t freq);
/**
* \brief Get current square-wave oscillator frequency
* \return Frequency
*/
ds1307_squarewave_freq_t ds1307_get_squarewave_freq();
ds1307_squarewave_freq_t ds1307_get_squarewave_freq(i2c_dev_t* dev);
/**
* \brief Get current output level of the SQW/OUT pin
* \return true if high
*/
bool ds1307_get_output();
bool ds1307_get_output(i2c_dev_t* dev);
/**
* \brief Set output level of the SQW/OUT pin
* Set output level if square-wave output is disabled
* \param value High level if true
*/
void ds1307_set_output(bool value);
void ds1307_set_output(i2c_dev_t* dev, bool value);
/**
* \brief Read RAM contents into the buffer
@ -95,7 +97,7 @@ void ds1307_set_output(bool value);
* \param len Bytes to read, 1..56
* \return Non-zero if error occured
*/
int ds1307_read_ram(uint8_t offset, uint8_t *buf, uint8_t len);
int ds1307_read_ram(i2c_dev_t* dev, uint8_t offset, uint8_t *buf, uint8_t len);
/**
* \brief Write buffer to RTC RAM
@ -104,7 +106,7 @@ int ds1307_read_ram(uint8_t offset, uint8_t *buf, uint8_t len);
* \param len Bytes to write, 1..56
* \return Non-zero if error occured
*/
int ds1307_write_ram(uint8_t offset, uint8_t *buf, uint8_t len);
int ds1307_write_ram(i2c_dev_t* dev, uint8_t offset, uint8_t *buf, uint8_t len);
#ifdef __cplusplus

View file

@ -6,10 +6,8 @@
* BSD Licensed as described in the file LICENSE
*/
#include "hmc5883l.h"
#include <i2c/i2c.h>
#include <espressif/esp_common.h>
#define ADDR 0x1e
#define REG_CR_A 0x00
#define REG_CR_B 0x01
@ -54,112 +52,112 @@ static const float gain_values [] = {
static float current_gain;
static hmc5883l_operating_mode_t current_mode;
static inline void write_register(uint8_t reg, uint8_t val)
static inline void write_register(i2c_dev_t* dev, uint8_t reg, uint8_t val)
{
i2c_slave_write(ADDR, &reg, &val, 1);
i2c_slave_write(dev->bus, dev->addr, &reg, &val, 1);
}
static inline uint8_t read_register(uint8_t reg)
static inline uint8_t read_register(i2c_dev_t* dev, uint8_t reg)
{
uint8_t res;
i2c_slave_read(ADDR, &reg, &res, 1);
i2c_slave_read(dev->bus, dev->addr, &reg, &res, 1);
return res;
}
static inline void update_register(uint8_t reg, uint8_t mask, uint8_t val)
static inline void update_register(i2c_dev_t* dev, uint8_t reg, uint8_t mask, uint8_t val)
{
write_register(reg, (read_register(reg) & mask) | val);
write_register(dev, reg, (read_register(dev, reg) & mask) | val);
}
bool hmc5883l_init()
bool hmc5883l_init(i2c_dev_t* dev)
{
if (hmc5883l_get_id() != HMC5883L_ID)
if (hmc5883l_get_id(dev) != HMC5883L_ID)
return false;
current_gain = gain_values[hmc5883l_get_gain()];
current_mode = hmc5883l_get_operating_mode();
current_gain = gain_values[hmc5883l_get_gain(dev)];
current_mode = hmc5883l_get_operating_mode(dev);
return true;
}
uint32_t hmc5883l_get_id()
uint32_t hmc5883l_get_id(i2c_dev_t* dev)
{
uint32_t res = 0;
uint8_t reg = REG_ID_A;
i2c_slave_read(ADDR, &reg, (uint8_t *)&res, 3);
i2c_slave_read(dev->bus, dev->addr, &reg, (uint8_t *)&res, 3);
return res;
}
hmc5883l_operating_mode_t hmc5883l_get_operating_mode()
hmc5883l_operating_mode_t hmc5883l_get_operating_mode(i2c_dev_t* dev)
{
uint8_t res = read_register(REG_MODE) & MASK_MD;
uint8_t res = read_register(dev, REG_MODE) & MASK_MD;
return res == 0 ? HMC5883L_MODE_CONTINUOUS : HMC5883L_MODE_SINGLE;
}
void hmc5883l_set_operating_mode(hmc5883l_operating_mode_t mode)
void hmc5883l_set_operating_mode(i2c_dev_t* dev, hmc5883l_operating_mode_t mode)
{
write_register(REG_MODE, mode);
write_register(dev, REG_MODE, mode);
current_mode = mode;
}
hmc5883l_samples_averaged_t hmc5883l_get_samples_averaged()
hmc5883l_samples_averaged_t hmc5883l_get_samples_averaged(i2c_dev_t* dev)
{
return (read_register(REG_CR_A) & MASK_MA) >> BIT_MA;
return (read_register(dev, REG_CR_A) & MASK_MA) >> BIT_MA;
}
void hmc5883l_set_samples_averaged(hmc5883l_samples_averaged_t samples)
void hmc5883l_set_samples_averaged(i2c_dev_t* dev, hmc5883l_samples_averaged_t samples)
{
update_register(REG_CR_A, MASK_MA, samples << BIT_MA);
update_register(dev, REG_CR_A, MASK_MA, samples << BIT_MA);
}
hmc5883l_data_rate_t hmc5883l_get_data_rate()
hmc5883l_data_rate_t hmc5883l_get_data_rate(i2c_dev_t* dev)
{
return (read_register(REG_CR_A) & MASK_DO) >> BIT_DO;
return (read_register(dev, REG_CR_A) & MASK_DO) >> BIT_DO;
}
void hmc5883l_set_data_rate(hmc5883l_data_rate_t rate)
void hmc5883l_set_data_rate(i2c_dev_t* dev, hmc5883l_data_rate_t rate)
{
update_register(REG_CR_A, MASK_DO, rate << BIT_DO);
update_register(dev, REG_CR_A, MASK_DO, rate << BIT_DO);
}
hmc5883l_bias_t hmc5883l_get_bias()
hmc5883l_bias_t hmc5883l_get_bias(i2c_dev_t* dev)
{
return read_register(REG_CR_A) & MASK_MS;
return read_register(dev, REG_CR_A) & MASK_MS;
}
void hmc5883l_set_bias(hmc5883l_bias_t bias)
void hmc5883l_set_bias(i2c_dev_t* dev, hmc5883l_bias_t bias)
{
update_register(REG_CR_A, MASK_MS, bias);
update_register(dev, REG_CR_A, MASK_MS, bias);
}
hmc5883l_gain_t hmc5883l_get_gain()
hmc5883l_gain_t hmc5883l_get_gain(i2c_dev_t* dev)
{
return read_register(REG_CR_B) >> BIT_GN;
return read_register(dev, REG_CR_B) >> BIT_GN;
}
void hmc5883l_set_gain(hmc5883l_gain_t gain)
void hmc5883l_set_gain(i2c_dev_t* dev, hmc5883l_gain_t gain)
{
write_register(REG_CR_B, gain << BIT_GN);
write_register(dev, REG_CR_B, gain << BIT_GN);
current_gain = gain_values[gain];
}
bool hmc5883l_data_is_locked()
bool hmc5883l_data_is_locked(i2c_dev_t* dev)
{
return read_register(REG_STAT) & MASK_DL;
return read_register(dev, REG_STAT) & MASK_DL;
}
bool hmc5883l_data_is_ready()
bool hmc5883l_data_is_ready(i2c_dev_t* dev)
{
return read_register(REG_STAT) & MASK_DR;
return read_register(dev, REG_STAT) & MASK_DR;
}
bool hmc5883l_get_raw_data(hmc5883l_raw_data_t *data)
bool hmc5883l_get_raw_data(i2c_dev_t* dev, hmc5883l_raw_data_t *data)
{
if (current_mode == HMC5883L_MODE_SINGLE)
{
// overwrite mode register for measurement
hmc5883l_set_operating_mode(current_mode);
hmc5883l_set_operating_mode(dev, current_mode);
// wait for data
uint32_t start = sdk_system_get_time();
while (!hmc5883l_data_is_ready())
while (!hmc5883l_data_is_ready(dev))
{
if (timeout_expired(start, MEASUREMENT_TIMEOUT))
return false;
@ -167,7 +165,7 @@ bool hmc5883l_get_raw_data(hmc5883l_raw_data_t *data)
}
uint8_t buf[6];
uint8_t reg = REG_DX_H;
i2c_slave_read(ADDR, &reg, buf, 6);
i2c_slave_read(dev->bus, dev->addr, &reg, buf, 6);
data->x = ((int16_t)buf[REG_DX_H - REG_DX_H] << 8) | buf[REG_DX_L - REG_DX_H];
data->y = ((int16_t)buf[REG_DY_H - REG_DX_H] << 8) | buf[REG_DY_L - REG_DX_H];
data->z = ((int16_t)buf[REG_DZ_H - REG_DX_H] << 8) | buf[REG_DZ_L - REG_DX_H];
@ -181,11 +179,11 @@ void hmc5883l_raw_to_mg(const hmc5883l_raw_data_t *raw, hmc5883l_data_t *mg)
mg->z = raw->z * current_gain;
}
bool hmc5883l_get_data(hmc5883l_data_t *data)
bool hmc5883l_get_data(i2c_dev_t* dev, hmc5883l_data_t *data)
{
hmc5883l_raw_data_t raw;
if (!hmc5883l_get_raw_data(&raw))
if (!hmc5883l_get_raw_data(dev, &raw))
return false;
hmc5883l_raw_to_mg(&raw, data);
return true;

View file

@ -10,12 +10,15 @@
#include <stdint.h>
#include <stdbool.h>
#include <i2c/i2c.h>
#ifdef __cplusplus
extern "C"
{
#endif
#define HMC5883L_ADDR 0x1e
#define HMC5883L_ID 0x00333448 // "H43"
/**
@ -101,82 +104,82 @@ typedef struct
* \brief Init device
* \return false if error occured
*/
bool hmc5883l_init();
bool hmc5883l_init(i2c_dev_t* dev);
/**
* \brief Get device ID
* Always returns 0x00333448 if IC functioning properly.
* \return Device ID
*/
uint32_t hmc5883l_get_id();
uint32_t hmc5883l_get_id(i2c_dev_t* dev);
/**
* \brief Get operating mode
* \return Measurement mode
*/
hmc5883l_operating_mode_t hmc5883l_get_operating_mode();
hmc5883l_operating_mode_t hmc5883l_get_operating_mode(i2c_dev_t* dev);
/**
* \brief Set operating mode
* \param mode Measurement mode
*/
void hmc5883l_set_operating_mode(hmc5883l_operating_mode_t mode);
void hmc5883l_set_operating_mode(i2c_dev_t* dev, hmc5883l_operating_mode_t mode);
/**
* \brief Get number of samples averaged per measurement output
* \return Number of samples
*/
hmc5883l_samples_averaged_t hmc5883l_get_samples_averaged();
hmc5883l_samples_averaged_t hmc5883l_get_samples_averaged(i2c_dev_t* dev);
/**
* \brief Set number of samples averaged per measurement output
* \param samples Number of samples
*/
void hmc5883l_set_samples_averaged(hmc5883l_samples_averaged_t samples);
void hmc5883l_set_samples_averaged(i2c_dev_t* dev, hmc5883l_samples_averaged_t samples);
/**
* \brief Get data output rate in continuous measurement mode
* \return Data output rate
*/
hmc5883l_data_rate_t hmc5883l_get_data_rate();
hmc5883l_data_rate_t hmc5883l_get_data_rate(i2c_dev_t* dev);
/**
* \brief Set data output rate in continuous measurement mode
* \param rate Data output rate
*/
void hmc5883l_set_data_rate(hmc5883l_data_rate_t rate);
void hmc5883l_set_data_rate(i2c_dev_t* dev, hmc5883l_data_rate_t rate);
/**
* \brief Get measurement mode (bias of the axes)
* See datasheet for self test description
* \return Bias
*/
hmc5883l_bias_t hmc5883l_get_bias();
hmc5883l_bias_t hmc5883l_get_bias(i2c_dev_t* dev);
/**
* \brief Set measurement mode (bias of the axes)
* See datasheet for self test description
* \param bias Bias
*/
void hmc5883l_set_bias(hmc5883l_bias_t bias);
void hmc5883l_set_bias(i2c_dev_t* dev, hmc5883l_bias_t bias);
/**
* \brief Get device gain
* \return Current gain
*/
hmc5883l_gain_t hmc5883l_get_gain();
hmc5883l_gain_t hmc5883l_get_gain(i2c_dev_t* dev);
/**
* \brief Set device gain
* \param gain Gain
*/
void hmc5883l_set_gain(hmc5883l_gain_t gain);
void hmc5883l_set_gain(i2c_dev_t* dev, hmc5883l_gain_t gain);
/**
* \brief Get data state
* \return true when data is written to all six data registers
*/
bool hmc5883l_data_is_ready();
bool hmc5883l_data_is_ready(i2c_dev_t* dev);
/**
* \brief Get lock state.
@ -188,14 +191,14 @@ bool hmc5883l_data_is_ready();
* 4. power is reset.
* \return true when data registers is locked
*/
bool hmc5883l_data_is_locked();
bool hmc5883l_data_is_locked(i2c_dev_t* dev);
/**
* \brief Get raw magnetic data
* \param data Pointer to the struct to write raw data
* \return false if error occured in single measurement mode, always true in continuous mode
*/
bool hmc5883l_get_raw_data(hmc5883l_raw_data_t *data);
bool hmc5883l_get_raw_data(i2c_dev_t* dev, hmc5883l_raw_data_t *data);
/**
* \brief Convert raw magnetic data to milligausses
@ -209,7 +212,7 @@ void hmc5883l_raw_to_mg(const hmc5883l_raw_data_t *raw, hmc5883l_data_t *mg);
* \param data Pointer to the struct to write data
* \return false if error occured in single measurement mode, always true in continuous mode
*/
bool hmc5883l_get_data(hmc5883l_data_t *data);
bool hmc5883l_get_data(i2c_dev_t* dev, hmc5883l_data_t *data);
#ifdef __cplusplus
}