ds3231 ads111x bh1750 update + I2c device desciptor
This commit is contained in:
parent
130385fecd
commit
a731034e65
12 changed files with 190 additions and 177 deletions
|
|
@ -8,7 +8,6 @@
|
||||||
#include <esp/uart.h>
|
#include <esp/uart.h>
|
||||||
#include <espressif/esp_common.h>
|
#include <espressif/esp_common.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <i2c/i2c.h>
|
|
||||||
#include <ad770x/ad770x.h>
|
#include <ad770x/ad770x.h>
|
||||||
#include <FreeRTOS.h>
|
#include <FreeRTOS.h>
|
||||||
#include <task.h>
|
#include <task.h>
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
// Connect ADDR pin to GND
|
// Connect ADDR pin to GND
|
||||||
#define ADDR ADS111X_ADDR_GND
|
#define ADDR ADS111X_ADDR_GND
|
||||||
|
|
||||||
|
#define I2C_BUS 0
|
||||||
#define SCL_PIN 5
|
#define SCL_PIN 5
|
||||||
#define SDA_PIN 4
|
#define SDA_PIN 4
|
||||||
|
|
||||||
|
|
@ -28,23 +29,27 @@ void user_init(void)
|
||||||
uart_set_baud(0, 115200);
|
uart_set_baud(0, 115200);
|
||||||
printf("SDK version:%s\n", sdk_system_get_sdk_version());
|
printf("SDK version:%s\n", sdk_system_get_sdk_version());
|
||||||
|
|
||||||
i2c_init(SCL_PIN, SDA_PIN);
|
i2c_dev_t dev = {
|
||||||
|
.addr = ADDR,
|
||||||
|
.bus = I2C_BUS,
|
||||||
|
};
|
||||||
|
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
|
||||||
|
|
||||||
ads111x_set_mode(ADDR, ADS111X_MODE_CONTUNOUS);
|
ads111x_set_mode(&dev, ADS111X_MODE_CONTUNOUS);
|
||||||
ads111x_set_data_rate(ADDR, ADS111X_DATA_RATE_32);
|
ads111x_set_data_rate(&dev, ADS111X_DATA_RATE_32);
|
||||||
|
|
||||||
ads111x_set_input_mux(ADDR, ADS111X_MUX_0_GND);
|
ads111x_set_input_mux(&dev, ADS111X_MUX_0_GND);
|
||||||
ads111x_set_gain(ADDR, GAIN);
|
ads111x_set_gain(&dev, GAIN);
|
||||||
|
|
||||||
float gain_val = ads111x_gain_values[GAIN];
|
float gain_val = ads111x_gain_values[GAIN];
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
// wait for conversion end
|
// wait for conversion end
|
||||||
while (ads111x_busy(ADDR)) {}
|
while (ads111x_busy(&dev)) {}
|
||||||
|
|
||||||
// Read result
|
// Read result
|
||||||
int16_t raw = ads111x_get_value(ADDR);
|
int16_t raw = ads111x_get_value(&dev);
|
||||||
|
|
||||||
float voltage = gain_val / ADS111X_MAX_VALUE * raw;
|
float voltage = gain_val / ADS111X_MAX_VALUE * raw;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,17 +11,20 @@
|
||||||
|
|
||||||
#define SCL_PIN 5
|
#define SCL_PIN 5
|
||||||
#define SDA_PIN 4
|
#define SDA_PIN 4
|
||||||
|
#define I2C_BUS 0
|
||||||
|
|
||||||
static void measure(void *pvParameters)
|
static void measure(void *pvParameters)
|
||||||
{
|
{
|
||||||
bh1750_configure(BH1750_ADDR_LO,
|
i2c_dev_t dev = {
|
||||||
BH1750_CONTINUOUS_MODE | BH1750_HIGH_RES_MODE);
|
.addr = BH1750_ADDR_LO,
|
||||||
|
.bus = I2C_BUS,
|
||||||
|
};
|
||||||
|
bh1750_configure(&dev, BH1750_CONTINUOUS_MODE | BH1750_HIGH_RES_MODE);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
while(1) {
|
while(1) {
|
||||||
vTaskDelay(200 / portTICK_PERIOD_MS);
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
||||||
|
printf("Lux: %d\n", bh1750_read(&dev));
|
||||||
printf("Lux: %d\n", bh1750_read(BH1750_ADDR_LO));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -35,7 +38,7 @@ void user_init(void)
|
||||||
printf("SDK version : %s\n", sdk_system_get_sdk_version());
|
printf("SDK version : %s\n", sdk_system_get_sdk_version());
|
||||||
printf("GIT version : %s\n", GITSHORTREV);
|
printf("GIT version : %s\n", GITSHORTREV);
|
||||||
|
|
||||||
i2c_init(SCL_PIN, SDA_PIN);
|
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
|
||||||
|
|
||||||
xTaskCreate(measure, "measure_task", 256, NULL, 2, NULL);
|
xTaskCreate(measure, "measure_task", 256, NULL, 2, NULL);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,15 +12,21 @@
|
||||||
|
|
||||||
#include "ds3231/ds3231.h"
|
#include "ds3231/ds3231.h"
|
||||||
|
|
||||||
|
#define ADDR DS3231_ADDR
|
||||||
|
#define I2C_BUS 0
|
||||||
|
|
||||||
void task1(void *pvParameters)
|
void task1(void *pvParameters)
|
||||||
{
|
{
|
||||||
struct tm time;
|
struct tm time;
|
||||||
float tempFloat;
|
float tempFloat;
|
||||||
|
i2c_dev_t dev = {
|
||||||
|
.addr = ADDR,
|
||||||
|
.bus = I2C_BUS,
|
||||||
|
};
|
||||||
while(1) {
|
while(1) {
|
||||||
vTaskDelay(100);
|
vTaskDelay(100);
|
||||||
ds3231_getTime(&time);
|
ds3231_getTime(&dev, &time);
|
||||||
ds3231_getTempFloat(&tempFloat);
|
ds3231_getTempFloat(&dev, &tempFloat);
|
||||||
printf("TIME:%d:%d:%d, TEMPERATURE:%.2f DegC\r\n", time.tm_hour, time.tm_min, time.tm_sec, tempFloat);
|
printf("TIME:%d:%d:%d, TEMPERATURE:%.2f DegC\r\n", time.tm_hour, time.tm_min, time.tm_sec, tempFloat);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -36,7 +42,6 @@ void user_init(void)
|
||||||
printf("GIT version : %s\n", GITSHORTREV);
|
printf("GIT version : %s\n", GITSHORTREV);
|
||||||
|
|
||||||
i2c_init(0,scl,sda,I2C_FREQ_400K);
|
i2c_init(0,scl,sda,I2C_FREQ_400K);
|
||||||
ds3231_Init(0);
|
|
||||||
|
|
||||||
xTaskCreate(task1, "tsk1", 256, NULL, 2, NULL);
|
xTaskCreate(task1, "tsk1", 256, NULL, 2, NULL);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@
|
||||||
* BSD Licensed as described in the file LICENSE
|
* BSD Licensed as described in the file LICENSE
|
||||||
*/
|
*/
|
||||||
#include "ads111x.h"
|
#include "ads111x.h"
|
||||||
#include <i2c/i2c.h>
|
|
||||||
|
|
||||||
#define ADS111X_DEBUG
|
#define ADS111X_DEBUG
|
||||||
|
|
||||||
|
|
@ -52,144 +51,144 @@ const float ads111x_gain_values[] = {
|
||||||
[ADS111X_GAIN_0V256_3] = 0.256
|
[ADS111X_GAIN_0V256_3] = 0.256
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t read_reg(uint8_t addr, uint8_t reg)
|
static uint16_t read_reg(i2c_dev_t* dev, uint8_t reg)
|
||||||
{
|
{
|
||||||
uint16_t res = 0;
|
uint16_t res = 0;
|
||||||
if (i2c_slave_read(addr, ®, (uint8_t *)&res, 2))
|
if (i2c_slave_read(dev->bus, dev->addr, ®, (uint8_t *)&res, 2))
|
||||||
debug("Could not read register %d", reg);
|
debug("Could not read register %d", reg);
|
||||||
//debug("Read %d: 0x%04x", reg, res);
|
//debug("Read %d: 0x%04x", reg, res);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void write_reg(uint8_t addr, uint8_t reg, uint16_t val)
|
static void write_reg(i2c_dev_t* dev, uint8_t reg, uint16_t val)
|
||||||
{
|
{
|
||||||
//debug("Write %d: 0x%04x", reg, val);
|
//debug("Write %d: 0x%04x", reg, val);
|
||||||
uint8_t buf[2] = { val >> 8, val};
|
uint8_t buf[2] = { val >> 8, val};
|
||||||
if (i2c_slave_write(addr, ®, buf, 2))
|
if (i2c_slave_write(dev->bus, dev->addr, ®, buf, 2))
|
||||||
debug("Could not write 0x%04x to register %d", val, reg);
|
debug("Could not write 0x%04x to register %d", val, reg);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint16_t read_conf_bits(uint8_t addr, uint8_t offs, uint16_t mask)
|
static uint16_t read_conf_bits(i2c_dev_t* dev, uint8_t offs, uint16_t mask)
|
||||||
{
|
{
|
||||||
return (read_reg(addr, REG_CONFIG) >> offs) & mask;
|
return (read_reg(dev, REG_CONFIG) >> offs) & mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void write_conf_bits(uint8_t addr, uint16_t val, uint8_t offs, uint16_t mask)
|
static void write_conf_bits(i2c_dev_t* dev, uint16_t val, uint8_t offs, uint16_t mask)
|
||||||
{
|
{
|
||||||
write_reg(addr, REG_CONFIG, (read_reg(addr, REG_CONFIG) & ~(mask << offs)) | (val << offs));
|
write_reg(dev, REG_CONFIG, (read_reg(dev, REG_CONFIG) & ~(mask << offs)) | (val << offs));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ads111x_busy(uint8_t addr)
|
bool ads111x_busy(i2c_dev_t* dev)
|
||||||
{
|
{
|
||||||
return read_conf_bits(addr, OS_OFFSET, OS_MASK);
|
return read_conf_bits(dev, OS_OFFSET, OS_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ads111x_start_conversion(uint8_t addr)
|
void ads111x_start_conversion(i2c_dev_t* dev)
|
||||||
{
|
{
|
||||||
write_conf_bits(addr, 1, OS_OFFSET, OS_MASK);
|
write_conf_bits(dev, 1, OS_OFFSET, OS_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t ads111x_get_value(uint8_t addr)
|
int16_t ads111x_get_value(i2c_dev_t* dev)
|
||||||
{
|
{
|
||||||
return read_reg(addr, REG_CONVERSION);
|
return read_reg(dev, REG_CONVERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
ads111x_gain_t ads111x_get_gain(uint8_t addr)
|
ads111x_gain_t ads111x_get_gain(i2c_dev_t* dev)
|
||||||
{
|
{
|
||||||
return read_conf_bits(addr, PGA_OFFSET, PGA_MASK);
|
return read_conf_bits(dev, PGA_OFFSET, PGA_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ads111x_set_gain(uint8_t addr, ads111x_gain_t gain)
|
void ads111x_set_gain(i2c_dev_t* dev, ads111x_gain_t gain)
|
||||||
{
|
{
|
||||||
write_conf_bits(addr, gain, PGA_OFFSET, PGA_MASK);
|
write_conf_bits(dev, gain, PGA_OFFSET, PGA_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
ads111x_mux_t ads111x_get_input_mux(uint8_t addr)
|
ads111x_mux_t ads111x_get_input_mux(i2c_dev_t* dev)
|
||||||
{
|
{
|
||||||
return read_conf_bits(addr, MUX_OFFSET, MUX_MASK);
|
return read_conf_bits(dev, MUX_OFFSET, MUX_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ads111x_set_input_mux(uint8_t addr, ads111x_mux_t mux)
|
void ads111x_set_input_mux(i2c_dev_t* dev, ads111x_mux_t mux)
|
||||||
{
|
{
|
||||||
write_conf_bits(addr, mux, MUX_OFFSET, MUX_MASK);
|
write_conf_bits(dev, mux, MUX_OFFSET, MUX_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
ads111x_mode_t ads111x_get_mode(uint8_t addr)
|
ads111x_mode_t ads111x_get_mode(i2c_dev_t* dev)
|
||||||
{
|
{
|
||||||
return read_conf_bits(addr, MODE_OFFSET, MODE_MASK);
|
return read_conf_bits(dev, MODE_OFFSET, MODE_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ads111x_set_mode(uint8_t addr, ads111x_mode_t mode)
|
void ads111x_set_mode(i2c_dev_t* dev, ads111x_mode_t mode)
|
||||||
{
|
{
|
||||||
write_conf_bits(addr, mode, MODE_OFFSET, MODE_MASK);
|
write_conf_bits(dev, mode, MODE_OFFSET, MODE_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
ads111x_data_rate_t ads111x_get_data_rate(uint8_t addr)
|
ads111x_data_rate_t ads111x_get_data_rate(i2c_dev_t* dev)
|
||||||
{
|
{
|
||||||
return read_conf_bits(addr, DR_OFFSET, DR_MASK);
|
return read_conf_bits(dev, DR_OFFSET, DR_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ads111x_set_data_rate(uint8_t addr, ads111x_data_rate_t rate)
|
void ads111x_set_data_rate(i2c_dev_t* dev, ads111x_data_rate_t rate)
|
||||||
{
|
{
|
||||||
write_conf_bits(addr, rate, DR_OFFSET, DR_MASK);
|
write_conf_bits(dev, rate, DR_OFFSET, DR_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
ads111x_comp_mode_t ads111x_get_comp_mode(uint8_t addr)
|
ads111x_comp_mode_t ads111x_get_comp_mode(i2c_dev_t* dev)
|
||||||
{
|
{
|
||||||
return read_conf_bits(addr, COMP_MODE_OFFSET, COMP_MODE_MASK);
|
return read_conf_bits(dev, COMP_MODE_OFFSET, COMP_MODE_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ads111x_set_comp_mode(uint8_t addr, ads111x_comp_mode_t mode)
|
void ads111x_set_comp_mode(i2c_dev_t* dev, ads111x_comp_mode_t mode)
|
||||||
{
|
{
|
||||||
write_conf_bits(addr, mode, COMP_MODE_OFFSET, COMP_MODE_MASK);
|
write_conf_bits(dev, mode, COMP_MODE_OFFSET, COMP_MODE_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
ads111x_comp_polarity_t ads111x_get_comp_polarity(uint8_t addr)
|
ads111x_comp_polarity_t ads111x_get_comp_polarity(i2c_dev_t* dev)
|
||||||
{
|
{
|
||||||
return read_conf_bits(addr, COMP_POL_OFFSET, COMP_POL_MASK);
|
return read_conf_bits(dev, COMP_POL_OFFSET, COMP_POL_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ads111x_set_comp_polarity(uint8_t addr, ads111x_comp_polarity_t polarity)
|
void ads111x_set_comp_polarity(i2c_dev_t* dev, ads111x_comp_polarity_t polarity)
|
||||||
{
|
{
|
||||||
write_conf_bits(addr, polarity, COMP_POL_OFFSET, COMP_POL_MASK);
|
write_conf_bits(dev, polarity, COMP_POL_OFFSET, COMP_POL_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
ads111x_comp_latch_t ads111x_get_comp_latch(uint8_t addr)
|
ads111x_comp_latch_t ads111x_get_comp_latch(i2c_dev_t* dev)
|
||||||
{
|
{
|
||||||
return read_conf_bits(addr, COMP_LAT_OFFSET, COMP_LAT_MASK);
|
return read_conf_bits(dev, COMP_LAT_OFFSET, COMP_LAT_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ads111x_set_comp_latch(uint8_t addr, ads111x_comp_latch_t latch)
|
void ads111x_set_comp_latch(i2c_dev_t* dev, ads111x_comp_latch_t latch)
|
||||||
{
|
{
|
||||||
write_conf_bits(addr, latch, COMP_LAT_OFFSET, COMP_LAT_MASK);
|
write_conf_bits(dev, latch, COMP_LAT_OFFSET, COMP_LAT_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
ads111x_comp_queue_t ads111x_get_comp_queue(uint8_t addr)
|
ads111x_comp_queue_t ads111x_get_comp_queue(i2c_dev_t* dev)
|
||||||
{
|
{
|
||||||
return read_conf_bits(addr, COMP_QUE_OFFSET, COMP_QUE_MASK);
|
return read_conf_bits(dev, COMP_QUE_OFFSET, COMP_QUE_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ads111x_set_comp_queue(uint8_t addr, ads111x_comp_queue_t queue)
|
void ads111x_set_comp_queue(i2c_dev_t* dev, ads111x_comp_queue_t queue)
|
||||||
{
|
{
|
||||||
write_conf_bits(addr, queue, COMP_QUE_OFFSET, COMP_QUE_MASK);
|
write_conf_bits(dev, queue, COMP_QUE_OFFSET, COMP_QUE_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t ads111x_get_comp_low_thresh(uint8_t addr)
|
int16_t ads111x_get_comp_low_thresh(i2c_dev_t* dev)
|
||||||
{
|
{
|
||||||
return read_reg(addr, REG_THRESH_L);
|
return read_reg(dev, REG_THRESH_L);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ads111x_set_comp_low_thresh(uint8_t addr, int16_t thresh)
|
void ads111x_set_comp_low_thresh(i2c_dev_t* dev, int16_t thresh)
|
||||||
{
|
{
|
||||||
write_reg(addr, REG_THRESH_L, thresh);
|
write_reg(dev, REG_THRESH_L, thresh);
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t ads111x_get_comp_high_thresh(uint8_t addr)
|
int16_t ads111x_get_comp_high_thresh(i2c_dev_t* dev)
|
||||||
{
|
{
|
||||||
return read_reg(addr, REG_THRESH_H);
|
return read_reg(dev, REG_THRESH_H);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ads111x_set_comp_high_thresh(uint8_t addr, int16_t thresh)
|
void ads111x_set_comp_high_thresh(i2c_dev_t* dev, int16_t thresh)
|
||||||
{
|
{
|
||||||
write_reg(addr, REG_THRESH_H, thresh);
|
write_reg(dev, REG_THRESH_H, thresh);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <i2c/i2c.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
@ -124,20 +125,20 @@ typedef enum
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @return true when device performing conversion
|
* @return true when device performing conversion
|
||||||
*/
|
*/
|
||||||
bool ads111x_busy(uint8_t addr);
|
bool ads111x_busy(i2c_dev_t* dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Begin a single conversion (when in single-shot mode)
|
* Begin a single conversion (when in single-shot mode)
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
*/
|
*/
|
||||||
void ads111x_start_conversion(uint8_t addr);
|
void ads111x_start_conversion(i2c_dev_t* dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read last conversion result
|
* Read last conversion result
|
||||||
* @param addr
|
* @param addr
|
||||||
* @return Last conversion result
|
* @return Last conversion result
|
||||||
*/
|
*/
|
||||||
int16_t ads111x_get_value(uint8_t addr);
|
int16_t ads111x_get_value(i2c_dev_t* dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read the programmable gain amplifier configuration
|
* Read the programmable gain amplifier configuration
|
||||||
|
|
@ -145,70 +146,70 @@ int16_t ads111x_get_value(uint8_t addr);
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @return Gain value
|
* @return Gain value
|
||||||
*/
|
*/
|
||||||
ads111x_gain_t ads111x_get_gain(uint8_t addr);
|
ads111x_gain_t ads111x_get_gain(i2c_dev_t* dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configure the programmable gain amplifier (ADS1114 and ADS1115 only)
|
* Configure the programmable gain amplifier (ADS1114 and ADS1115 only)
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @param gain Gain value
|
* @param gain Gain value
|
||||||
*/
|
*/
|
||||||
void ads111x_set_gain(uint8_t addr, ads111x_gain_t gain);
|
void ads111x_set_gain(i2c_dev_t* dev, ads111x_gain_t gain);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read the input multiplexer configuration (ADS1115 only)
|
* Read the input multiplexer configuration (ADS1115 only)
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @return Input multiplexer configuration
|
* @return Input multiplexer configuration
|
||||||
*/
|
*/
|
||||||
ads111x_mux_t ads111x_get_input_mux(uint8_t addr);
|
ads111x_mux_t ads111x_get_input_mux(i2c_dev_t* dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configure the input multiplexer configuration (ADS1115 only)
|
* Configure the input multiplexer configuration (ADS1115 only)
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @param mux Input multiplexer configuration
|
* @param mux Input multiplexer configuration
|
||||||
*/
|
*/
|
||||||
void ads111x_set_input_mux(uint8_t addr, ads111x_mux_t mux);
|
void ads111x_set_input_mux(i2c_dev_t* dev, ads111x_mux_t mux);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read the device operating mode
|
* Read the device operating mode
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @return Device operating mode
|
* @return Device operating mode
|
||||||
*/
|
*/
|
||||||
ads111x_mode_t ads111x_get_mode(uint8_t addr);
|
ads111x_mode_t ads111x_get_mode(i2c_dev_t* dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the device operating mode
|
* Set the device operating mode
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @param mode Device operating mode
|
* @param mode Device operating mode
|
||||||
*/
|
*/
|
||||||
void ads111x_set_mode(uint8_t addr, ads111x_mode_t mode);
|
void ads111x_set_mode(i2c_dev_t* dev, ads111x_mode_t mode);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read the data rate
|
* Read the data rate
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @return Data rate
|
* @return Data rate
|
||||||
*/
|
*/
|
||||||
ads111x_data_rate_t ads111x_get_data_rate(uint8_t addr);
|
ads111x_data_rate_t ads111x_get_data_rate(i2c_dev_t* dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configure the data rate
|
* Configure the data rate
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @param rate Data rate
|
* @param rate Data rate
|
||||||
*/
|
*/
|
||||||
void ads111x_set_data_rate(uint8_t addr, ads111x_data_rate_t rate);
|
void ads111x_set_data_rate(i2c_dev_t* dev, ads111x_data_rate_t rate);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get comparator mode (ADS1114 and ADS1115 only)
|
* Get comparator mode (ADS1114 and ADS1115 only)
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @return Comparator mode
|
* @return Comparator mode
|
||||||
*/
|
*/
|
||||||
ads111x_comp_mode_t ads111x_get_comp_mode(uint8_t addr);
|
ads111x_comp_mode_t ads111x_get_comp_mode(i2c_dev_t* dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set comparator mode (ADS1114 and ADS1115 only)
|
* Set comparator mode (ADS1114 and ADS1115 only)
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @param mode Comparator mode
|
* @param mode Comparator mode
|
||||||
*/
|
*/
|
||||||
void ads111x_set_comp_mode(uint8_t addr, ads111x_comp_mode_t mode);
|
void ads111x_set_comp_mode(i2c_dev_t* dev, ads111x_comp_mode_t mode);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get polarity of the comparator output pin ALERT/RDY
|
* Get polarity of the comparator output pin ALERT/RDY
|
||||||
|
|
@ -216,7 +217,7 @@ void ads111x_set_comp_mode(uint8_t addr, ads111x_comp_mode_t mode);
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @return Comparator output pin polarity
|
* @return Comparator output pin polarity
|
||||||
*/
|
*/
|
||||||
ads111x_comp_polarity_t ads111x_get_comp_polarity(uint8_t addr);
|
ads111x_comp_polarity_t ads111x_get_comp_polarity(i2c_dev_t* dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set polarity of the comparator output pin ALERT/RDY
|
* Set polarity of the comparator output pin ALERT/RDY
|
||||||
|
|
@ -224,7 +225,7 @@ ads111x_comp_polarity_t ads111x_get_comp_polarity(uint8_t addr);
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @param polarity Comparator output pin polarity
|
* @param polarity Comparator output pin polarity
|
||||||
*/
|
*/
|
||||||
void ads111x_set_comp_polarity(uint8_t addr, ads111x_comp_polarity_t polarity);
|
void ads111x_set_comp_polarity(i2c_dev_t* dev, ads111x_comp_polarity_t polarity);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get comparator output latch mode, see datasheet.
|
* Get comparator output latch mode, see datasheet.
|
||||||
|
|
@ -232,14 +233,14 @@ void ads111x_set_comp_polarity(uint8_t addr, ads111x_comp_polarity_t polarity);
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @return Comparator output latch mode
|
* @return Comparator output latch mode
|
||||||
*/
|
*/
|
||||||
ads111x_comp_latch_t ads111x_get_comp_latch(uint8_t addr);
|
ads111x_comp_latch_t ads111x_get_comp_latch(i2c_dev_t* dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set comparator output latch mode (ADS1114 and ADS1115 only)
|
* Set comparator output latch mode (ADS1114 and ADS1115 only)
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @param latch Comparator output latch mode
|
* @param latch Comparator output latch mode
|
||||||
*/
|
*/
|
||||||
void ads111x_set_comp_latch(uint8_t addr, ads111x_comp_latch_t latch);
|
void ads111x_set_comp_latch(i2c_dev_t* dev, ads111x_comp_latch_t latch);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set number of the comparator conversions before pin ALERT/RDY
|
* Set number of the comparator conversions before pin ALERT/RDY
|
||||||
|
|
@ -247,7 +248,7 @@ void ads111x_set_comp_latch(uint8_t addr, ads111x_comp_latch_t latch);
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @return Number of the comparator conversions
|
* @return Number of the comparator conversions
|
||||||
*/
|
*/
|
||||||
ads111x_comp_queue_t ads111x_get_comp_queue(uint8_t addr);
|
ads111x_comp_queue_t ads111x_get_comp_queue(i2c_dev_t* dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get number of the comparator conversions before pin ALERT/RDY
|
* Get number of the comparator conversions before pin ALERT/RDY
|
||||||
|
|
@ -255,35 +256,35 @@ ads111x_comp_queue_t ads111x_get_comp_queue(uint8_t addr);
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @param queue Number of the comparator conversions
|
* @param queue Number of the comparator conversions
|
||||||
*/
|
*/
|
||||||
void ads111x_set_comp_queue(uint8_t addr, ads111x_comp_queue_t queue);
|
void ads111x_set_comp_queue(i2c_dev_t* dev, ads111x_comp_queue_t queue);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the lower threshold value used by comparator
|
* Get the lower threshold value used by comparator
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @return Lower threshold value
|
* @return Lower threshold value
|
||||||
*/
|
*/
|
||||||
int16_t ads111x_get_comp_low_thresh(uint8_t addr);
|
int16_t ads111x_get_comp_low_thresh(i2c_dev_t* dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the lower threshold value used by comparator
|
* Set the lower threshold value used by comparator
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @param thresh Lower threshold value
|
* @param thresh Lower threshold value
|
||||||
*/
|
*/
|
||||||
void ads111x_set_comp_low_thresh(uint8_t addr, int16_t thresh);
|
void ads111x_set_comp_low_thresh(i2c_dev_t* dev, int16_t thresh);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the upper threshold value used by comparator
|
* Get the upper threshold value used by comparator
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @return Upper threshold value
|
* @return Upper threshold value
|
||||||
*/
|
*/
|
||||||
int16_t ads111x_get_comp_high_thresh(uint8_t addr);
|
int16_t ads111x_get_comp_high_thresh(i2c_dev_t* dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the upper threshold value used by comparator
|
* Set the upper threshold value used by comparator
|
||||||
* @param addr Deivce address
|
* @param addr Deivce address
|
||||||
* @param thresh Upper threshold value
|
* @param thresh Upper threshold value
|
||||||
*/
|
*/
|
||||||
void ads111x_set_comp_high_thresh(uint8_t addr, int16_t thresh);
|
void ads111x_set_comp_high_thresh(i2c_dev_t* dev, int16_t thresh);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,20 +6,19 @@
|
||||||
* BSD Licensed as described in the file LICENSE
|
* BSD Licensed as described in the file LICENSE
|
||||||
*/
|
*/
|
||||||
#include "bh1750.h"
|
#include "bh1750.h"
|
||||||
#include <i2c/i2c.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
void bh1750_configure(uint8_t addr, uint8_t mode)
|
void bh1750_configure(i2c_dev_t *dev, uint8_t mode)
|
||||||
{
|
{
|
||||||
i2c_slave_write(addr, NULL, &mode, 1);
|
i2c_slave_write(dev->bus, dev->addr, NULL, &mode, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t bh1750_read(uint8_t addr)
|
uint16_t bh1750_read(i2c_dev_t *dev)
|
||||||
{
|
{
|
||||||
uint8_t buf[2];
|
uint8_t buf[2];
|
||||||
uint16_t level;
|
uint16_t level;
|
||||||
|
|
||||||
i2c_slave_read(addr, NULL, buf, 2);
|
i2c_slave_read(dev->bus, dev->addr, NULL, buf, 2);
|
||||||
|
|
||||||
level = buf[0] << 8 | buf[1];
|
level = buf[0] << 8 | buf[1];
|
||||||
level = (level * 10) / 12; // convert to LUX
|
level = (level * 10) / 12; // convert to LUX
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <i2c/i2c.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
@ -65,7 +66,7 @@ extern "C" {
|
||||||
*
|
*
|
||||||
* Example: BH1750_ADDR_LO, BH1750_CONTINUOUS_MODE | BH1750_HIGH_RES_MODE
|
* Example: BH1750_ADDR_LO, BH1750_CONTINUOUS_MODE | BH1750_HIGH_RES_MODE
|
||||||
*/
|
*/
|
||||||
void bh1750_configure(uint8_t addr, uint8_t mode);
|
void bh1750_configure(i2c_dev_t *dev, uint8_t mode);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read LUX value from the device.
|
* Read LUX value from the device.
|
||||||
|
|
@ -73,7 +74,7 @@ void bh1750_configure(uint8_t addr, uint8_t mode);
|
||||||
* @param addr Device address
|
* @param addr Device address
|
||||||
* @returns read value in lux units
|
* @returns read value in lux units
|
||||||
*/
|
*/
|
||||||
uint16_t bh1750_read(uint8_t addr);
|
uint16_t bh1750_read(i2c_dev_t *dev);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,6 @@
|
||||||
#include "espressif/sdk_private.h"
|
#include "espressif/sdk_private.h"
|
||||||
#include "esp8266.h"
|
#include "esp8266.h"
|
||||||
|
|
||||||
#include "i2c/i2c.h"
|
|
||||||
|
|
||||||
static uint8_t _bus;
|
|
||||||
|
|
||||||
/* Convert normal decimal to binary coded decimal */
|
/* Convert normal decimal to binary coded decimal */
|
||||||
static inline uint8_t decToBcd(uint8_t dec)
|
static inline uint8_t decToBcd(uint8_t dec)
|
||||||
|
|
@ -30,20 +27,20 @@ static inline uint8_t bcdToDec(uint8_t bcd)
|
||||||
/* Send a number of bytes to the rtc over i2c
|
/* Send a number of bytes to the rtc over i2c
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
static inline int ds3231_send(uint8_t reg, uint8_t *data, uint8_t len)
|
static inline int ds3231_send(i2c_dev_t *dev, uint8_t reg, uint8_t *data, uint8_t len)
|
||||||
{
|
{
|
||||||
return i2c_slave_write(_bus, DS3231_ADDR, ®, data, len);
|
return i2c_slave_write(dev->bus, dev->addr, ®, data, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read a number of bytes from the rtc over i2c
|
/* Read a number of bytes from the rtc over i2c
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
static inline int ds3231_recv(uint8_t reg, uint8_t *data, uint8_t len)
|
static inline int ds3231_recv(i2c_dev_t *dev, uint8_t reg, uint8_t *data, uint8_t len)
|
||||||
{
|
{
|
||||||
return i2c_slave_read(_bus, DS3231_ADDR, ®, data, len);
|
return i2c_slave_read(dev->bus, dev->addr, ®, data, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ds3231_setTime(struct tm *time)
|
int ds3231_setTime(i2c_dev_t *dev, struct tm *time)
|
||||||
{
|
{
|
||||||
uint8_t data[7];
|
uint8_t data[7];
|
||||||
|
|
||||||
|
|
@ -56,10 +53,10 @@ int ds3231_setTime(struct tm *time)
|
||||||
data[5] = decToBcd(time->tm_mon + 1);
|
data[5] = decToBcd(time->tm_mon + 1);
|
||||||
data[6] = decToBcd(time->tm_year - 100);
|
data[6] = decToBcd(time->tm_year - 100);
|
||||||
|
|
||||||
return ds3231_send(DS3231_ADDR_TIME, data, 7);
|
return ds3231_send(dev, DS3231_ADDR_TIME, data, 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ds3231_setAlarm(uint8_t alarms, struct tm *time1, uint8_t option1, struct tm *time2, uint8_t option2)
|
int ds3231_setAlarm(i2c_dev_t *dev, uint8_t alarms, struct tm *time1, uint8_t option1, struct tm *time2, uint8_t option2)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
uint8_t data[7];
|
uint8_t data[7];
|
||||||
|
|
@ -83,7 +80,7 @@ int ds3231_setAlarm(uint8_t alarms, struct tm *time1, uint8_t option1, struct tm
|
||||||
(option2 == DS3231_ALARM2_MATCH_MINHOURDATE ? decToBcd(time2->tm_mday) : DS3231_ALARM_NOTSET));
|
(option2 == DS3231_ALARM2_MATCH_MINHOURDATE ? decToBcd(time2->tm_mday) : DS3231_ALARM_NOTSET));
|
||||||
}
|
}
|
||||||
|
|
||||||
return ds3231_send((alarms == DS3231_ALARM_2 ? DS3231_ADDR_ALARM2 : DS3231_ADDR_ALARM1), data, i);
|
return ds3231_send(dev, (alarms == DS3231_ALARM_2 ? DS3231_ADDR_ALARM2 : DS3231_ADDR_ALARM1), data, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get a byte containing just the requested bits
|
/* Get a byte containing just the requested bits
|
||||||
|
|
@ -93,12 +90,12 @@ int ds3231_setAlarm(uint8_t alarms, struct tm *time1, uint8_t option1, struct tm
|
||||||
* of use a mask of 0xff to just return the whole register byte
|
* of use a mask of 0xff to just return the whole register byte
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_getFlag(uint8_t addr, uint8_t mask, uint8_t *flag)
|
bool ds3231_getFlag(i2c_dev_t *dev, uint8_t addr, uint8_t mask, uint8_t *flag)
|
||||||
{
|
{
|
||||||
uint8_t data;
|
uint8_t data;
|
||||||
|
|
||||||
/* get register */
|
/* get register */
|
||||||
if (!ds3231_recv(addr, &data, 1))
|
if (!ds3231_recv(dev, addr, &data, 1))
|
||||||
{
|
{
|
||||||
/* return only requested flag */
|
/* return only requested flag */
|
||||||
*flag = (data & mask);
|
*flag = (data & mask);
|
||||||
|
|
@ -114,12 +111,12 @@ bool ds3231_getFlag(uint8_t addr, uint8_t mask, uint8_t *flag)
|
||||||
* DS3231_SET/DS3231_CLEAR/DS3231_REPLACE
|
* DS3231_SET/DS3231_CLEAR/DS3231_REPLACE
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_setFlag(uint8_t addr, uint8_t bits, uint8_t mode)
|
bool ds3231_setFlag(i2c_dev_t *dev, uint8_t addr, uint8_t bits, uint8_t mode)
|
||||||
{
|
{
|
||||||
uint8_t data;
|
uint8_t data;
|
||||||
|
|
||||||
/* get status register */
|
/* get status register */
|
||||||
if (!ds3231_recv(addr, &data, 1))
|
if (!ds3231_recv(dev, addr, &data, 1))
|
||||||
{
|
{
|
||||||
/* clear the flag */
|
/* clear the flag */
|
||||||
if (mode == DS3231_REPLACE)
|
if (mode == DS3231_REPLACE)
|
||||||
|
|
@ -129,18 +126,18 @@ bool ds3231_setFlag(uint8_t addr, uint8_t bits, uint8_t mode)
|
||||||
else
|
else
|
||||||
data &= ~bits;
|
data &= ~bits;
|
||||||
|
|
||||||
if (!ds3231_send(addr, &data, 1))
|
if (!ds3231_send(dev, addr, &data, 1))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ds3231_getOscillatorStopFlag(bool *flag)
|
bool ds3231_getOscillatorStopFlag(i2c_dev_t *dev, bool *flag)
|
||||||
{
|
{
|
||||||
uint8_t f;
|
uint8_t f;
|
||||||
|
|
||||||
if (ds3231_getFlag(DS3231_ADDR_STATUS, DS3231_STAT_OSCILLATOR, &f))
|
if (ds3231_getFlag(dev, DS3231_ADDR_STATUS, DS3231_STAT_OSCILLATOR, &f))
|
||||||
{
|
{
|
||||||
*flag = (f ? true : false);
|
*flag = (f ? true : false);
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -149,76 +146,76 @@ bool ds3231_getOscillatorStopFlag(bool *flag)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool ds3231_clearOscillatorStopFlag()
|
inline bool ds3231_clearOscillatorStopFlag(i2c_dev_t *dev)
|
||||||
{
|
{
|
||||||
return ds3231_setFlag(DS3231_ADDR_STATUS, DS3231_STAT_OSCILLATOR, DS3231_CLEAR);
|
return ds3231_setFlag(dev, DS3231_ADDR_STATUS, DS3231_STAT_OSCILLATOR, DS3231_CLEAR);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool ds3231_getAlarmFlags(uint8_t *alarms)
|
inline bool ds3231_getAlarmFlags(i2c_dev_t *dev, uint8_t *alarms)
|
||||||
{
|
{
|
||||||
return ds3231_getFlag(DS3231_ADDR_STATUS, DS3231_ALARM_BOTH, alarms);
|
return ds3231_getFlag(dev, DS3231_ADDR_STATUS, DS3231_ALARM_BOTH, alarms);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool ds3231_clearAlarmFlags(uint8_t alarms)
|
inline bool ds3231_clearAlarmFlags(i2c_dev_t *dev, uint8_t alarms)
|
||||||
{
|
{
|
||||||
return ds3231_setFlag(DS3231_ADDR_STATUS, alarms, DS3231_CLEAR);
|
return ds3231_setFlag(dev, DS3231_ADDR_STATUS, alarms, DS3231_CLEAR);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool ds3231_enableAlarmInts(uint8_t alarms)
|
inline bool ds3231_enableAlarmInts(i2c_dev_t *dev, uint8_t alarms)
|
||||||
{
|
{
|
||||||
return ds3231_setFlag(DS3231_ADDR_CONTROL, DS3231_CTRL_ALARM_INTS | alarms, DS3231_SET);
|
return ds3231_setFlag(dev, DS3231_ADDR_CONTROL, DS3231_CTRL_ALARM_INTS | alarms, DS3231_SET);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool ds3231_disableAlarmInts(uint8_t alarms)
|
inline bool ds3231_disableAlarmInts(i2c_dev_t *dev, uint8_t alarms)
|
||||||
{
|
{
|
||||||
/* Just disable specific alarm(s) requested
|
/* Just disable specific alarm(s) requested
|
||||||
* does not disable alarm interrupts generally (which would enable the squarewave)
|
* does not disable alarm interrupts generally (which would enable the squarewave)
|
||||||
*/
|
*/
|
||||||
return ds3231_setFlag(DS3231_ADDR_CONTROL, alarms, DS3231_CLEAR);
|
return ds3231_setFlag(dev, DS3231_ADDR_CONTROL, alarms, DS3231_CLEAR);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool ds3231_enable32khz()
|
inline bool ds3231_enable32khz(i2c_dev_t *dev)
|
||||||
{
|
{
|
||||||
return ds3231_setFlag(DS3231_ADDR_STATUS, DS3231_STAT_32KHZ, DS3231_SET);
|
return ds3231_setFlag(dev, DS3231_ADDR_STATUS, DS3231_STAT_32KHZ, DS3231_SET);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool ds3231_disable32khz()
|
inline bool ds3231_disable32khz(i2c_dev_t *dev)
|
||||||
{
|
{
|
||||||
return ds3231_setFlag(DS3231_ADDR_STATUS, DS3231_STAT_32KHZ, DS3231_CLEAR);
|
return ds3231_setFlag(dev, DS3231_ADDR_STATUS, DS3231_STAT_32KHZ, DS3231_CLEAR);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool ds3231_enableSquarewave()
|
inline bool ds3231_enableSquarewave(i2c_dev_t *dev)
|
||||||
{
|
{
|
||||||
return ds3231_setFlag(DS3231_ADDR_CONTROL, DS3231_CTRL_ALARM_INTS, DS3231_CLEAR);
|
return ds3231_setFlag(dev, DS3231_ADDR_CONTROL, DS3231_CTRL_ALARM_INTS, DS3231_CLEAR);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool ds3231_disableSquarewave()
|
inline bool ds3231_disableSquarewave(i2c_dev_t *dev)
|
||||||
{
|
{
|
||||||
return ds3231_setFlag(DS3231_ADDR_CONTROL, DS3231_CTRL_ALARM_INTS, DS3231_SET);
|
return ds3231_setFlag(dev, DS3231_ADDR_CONTROL, DS3231_CTRL_ALARM_INTS, DS3231_SET);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ds3231_setSquarewaveFreq(uint8_t freq)
|
bool ds3231_setSquarewaveFreq(i2c_dev_t *dev, uint8_t freq)
|
||||||
{
|
{
|
||||||
uint8_t flag = 0;
|
uint8_t flag = 0;
|
||||||
|
|
||||||
if (ds3231_getFlag(DS3231_ADDR_CONTROL, 0xff, &flag))
|
if (ds3231_getFlag(dev, DS3231_ADDR_CONTROL, 0xff, &flag))
|
||||||
{
|
{
|
||||||
/* clear current rate */
|
/* clear current rate */
|
||||||
flag &= ~DS3231_CTRL_SQWAVE_8192HZ;
|
flag &= ~DS3231_CTRL_SQWAVE_8192HZ;
|
||||||
/* set new rate */
|
/* set new rate */
|
||||||
flag |= freq;
|
flag |= freq;
|
||||||
|
|
||||||
return ds3231_setFlag(DS3231_ADDR_CONTROL, flag, DS3231_REPLACE);
|
return ds3231_setFlag(dev, DS3231_ADDR_CONTROL, flag, DS3231_REPLACE);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ds3231_getRawTemp(int16_t *temp)
|
bool ds3231_getRawTemp(i2c_dev_t *dev, int16_t *temp)
|
||||||
{
|
{
|
||||||
uint8_t data[2];
|
uint8_t data[2];
|
||||||
|
|
||||||
data[0] = DS3231_ADDR_TEMP;
|
data[0] = DS3231_ADDR_TEMP;
|
||||||
if (!ds3231_recv(DS3231_ADDR_TEMP,data, 2))
|
if (!ds3231_recv(dev, DS3231_ADDR_TEMP,data, 2))
|
||||||
{
|
{
|
||||||
*temp = (int16_t)(int8_t)data[0] << 2 | data[1] >> 6;
|
*temp = (int16_t)(int8_t)data[0] << 2 | data[1] >> 6;
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -227,11 +224,11 @@ bool ds3231_getRawTemp(int16_t *temp)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ds3231_getTempInteger(int8_t *temp)
|
bool ds3231_getTempInteger(i2c_dev_t *dev, int8_t *temp)
|
||||||
{
|
{
|
||||||
int16_t tInt;
|
int16_t tInt;
|
||||||
|
|
||||||
if (ds3231_getRawTemp(&tInt)) {
|
if (ds3231_getRawTemp(dev, &tInt)) {
|
||||||
*temp = tInt >> 2;
|
*temp = tInt >> 2;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -239,11 +236,11 @@ bool ds3231_getTempInteger(int8_t *temp)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ds3231_getTempFloat(float *temp)
|
bool ds3231_getTempFloat(i2c_dev_t *dev, float *temp)
|
||||||
{
|
{
|
||||||
int16_t tInt;
|
int16_t tInt;
|
||||||
|
|
||||||
if (ds3231_getRawTemp(&tInt)) {
|
if (ds3231_getRawTemp(dev, &tInt)) {
|
||||||
*temp = tInt * 0.25;
|
*temp = tInt * 0.25;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -251,12 +248,12 @@ bool ds3231_getTempFloat(float *temp)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ds3231_getTime(struct tm *time)
|
bool ds3231_getTime(i2c_dev_t *dev, struct tm *time)
|
||||||
{
|
{
|
||||||
uint8_t data[7];
|
uint8_t data[7];
|
||||||
|
|
||||||
/* read time */
|
/* read time */
|
||||||
if (ds3231_recv(DS3231_ADDR_TIME, data, 7))
|
if (ds3231_recv(dev, DS3231_ADDR_TIME, data, 7))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -285,9 +282,3 @@ bool ds3231_getTime(struct tm *time)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ds3231_Init(uint8_t bus)
|
|
||||||
{
|
|
||||||
_bus = bus;
|
|
||||||
//i2c_init(0, scl, sda);
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include "i2c/i2c.h"
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
@ -87,7 +89,7 @@ enum {
|
||||||
* I suggest using GMT and applying timezone and DST when read back
|
* I suggest using GMT and applying timezone and DST when read back
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
int ds3231_setTime(struct tm *time);
|
int ds3231_setTime(i2c_dev_t *dev, struct tm *time);
|
||||||
|
|
||||||
/* Set alarms
|
/* Set alarms
|
||||||
* alarm1 works with seconds, minutes, hours and day of week/month, or fires every second
|
* alarm1 works with seconds, minutes, hours and day of week/month, or fires every second
|
||||||
|
|
@ -100,30 +102,30 @@ int ds3231_setTime(struct tm *time);
|
||||||
* if you want to enable interrupts for the alarms you need to do that separately
|
* if you want to enable interrupts for the alarms you need to do that separately
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
int ds3231_setAlarm(uint8_t alarms, struct tm *time1, uint8_t option1, struct tm *time2, uint8_t option2);
|
int ds3231_setAlarm(i2c_dev_t *dev, uint8_t alarms, struct tm *time1, uint8_t option1, struct tm *time2, uint8_t option2);
|
||||||
|
|
||||||
/* Check if oscillator has previously stopped, e.g. no power/battery or disabled
|
/* Check if oscillator has previously stopped, e.g. no power/battery or disabled
|
||||||
* sets flag to true if there has been a stop
|
* sets flag to true if there has been a stop
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_getOscillatorStopFlag(bool *flag);
|
bool ds3231_getOscillatorStopFlag(i2c_dev_t *dev, bool *flag);
|
||||||
|
|
||||||
/* Clear the oscillator stopped flag
|
/* Clear the oscillator stopped flag
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_clearOscillatorStopFlag();
|
bool ds3231_clearOscillatorStopFlag(i2c_dev_t *dev);
|
||||||
|
|
||||||
/* Check which alarm(s) have past
|
/* Check which alarm(s) have past
|
||||||
* sets alarms to DS3231_ALARM_NONE/DS3231_ALARM_1/DS3231_ALARM_2/DS3231_ALARM_BOTH
|
* sets alarms to DS3231_ALARM_NONE/DS3231_ALARM_1/DS3231_ALARM_2/DS3231_ALARM_BOTH
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_getAlarmFlags(uint8_t *alarms);
|
bool ds3231_getAlarmFlags(i2c_dev_t *dev, uint8_t *alarms);
|
||||||
|
|
||||||
/* Clear alarm past flag(s)
|
/* Clear alarm past flag(s)
|
||||||
* pass DS3231_ALARM_1/DS3231_ALARM_2/DS3231_ALARM_BOTH
|
* pass DS3231_ALARM_1/DS3231_ALARM_2/DS3231_ALARM_BOTH
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_clearAlarmFlags(uint8_t alarm);
|
bool ds3231_clearAlarmFlags(i2c_dev_t *dev, uint8_t alarm);
|
||||||
|
|
||||||
/* enable alarm interrupts (and disables squarewave)
|
/* enable alarm interrupts (and disables squarewave)
|
||||||
* pass DS3231_ALARM_1/DS3231_ALARM_2/DS3231_ALARM_BOTH
|
* pass DS3231_ALARM_1/DS3231_ALARM_2/DS3231_ALARM_BOTH
|
||||||
|
|
@ -132,61 +134,60 @@ bool ds3231_clearAlarmFlags(uint8_t alarm);
|
||||||
* interrupt enabled, else it will trigger immediately
|
* interrupt enabled, else it will trigger immediately
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_enableAlarmInts(uint8_t alarms);
|
bool ds3231_enableAlarmInts(i2c_dev_t *dev, uint8_t alarms);
|
||||||
|
|
||||||
/* Disable alarm interrupts (does not (re-)enable squarewave)
|
/* Disable alarm interrupts (does not (re-)enable squarewave)
|
||||||
* pass DS3231_ALARM_1/DS3231_ALARM_2/DS3231_ALARM_BOTH
|
* pass DS3231_ALARM_1/DS3231_ALARM_2/DS3231_ALARM_BOTH
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_disableAlarmInts(uint8_t alarms);
|
bool ds3231_disableAlarmInts(i2c_dev_t *dev, uint8_t alarms);
|
||||||
|
|
||||||
/* Enable the output of 32khz signal
|
/* Enable the output of 32khz signal
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_enable32khz();
|
bool ds3231_enable32khz(i2c_dev_t *dev);
|
||||||
|
|
||||||
/* Disable the output of 32khz signal
|
/* Disable the output of 32khz signal
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_disable32khz();
|
bool ds3231_disable32khz(i2c_dev_t *dev);
|
||||||
|
|
||||||
/* Enable the squarewave output (disables alarm interrupt functionality)
|
/* Enable the squarewave output (disables alarm interrupt functionality)
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_enableSquarewave();
|
bool ds3231_enableSquarewave(i2c_dev_t *dev);
|
||||||
|
|
||||||
/* Disable the squarewave output (which re-enables alarm interrupts, but individual
|
/* Disable the squarewave output (which re-enables alarm interrupts, but individual
|
||||||
* alarm interrupts also need to be enabled, if not already, before they will trigger)
|
* alarm interrupts also need to be enabled, if not already, before they will trigger)
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_disableSquarewave();
|
bool ds3231_disableSquarewave(i2c_dev_t *dev);
|
||||||
|
|
||||||
/* Set the frequency of the squarewave output (but does not enable it)
|
/* Set the frequency of the squarewave output (but does not enable it)
|
||||||
* pass DS3231_SQUAREWAVE_RATE_1HZ/DS3231_SQUAREWAVE_RATE_1024HZ/DS3231_SQUAREWAVE_RATE_4096HZ/DS3231_SQUAREWAVE_RATE_8192HZ
|
* pass DS3231_SQUAREWAVE_RATE_1HZ/DS3231_SQUAREWAVE_RATE_1024HZ/DS3231_SQUAREWAVE_RATE_4096HZ/DS3231_SQUAREWAVE_RATE_8192HZ
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_setSquarewaveFreq(uint8_t freq);
|
bool ds3231_setSquarewaveFreq(i2c_dev_t *dev, uint8_t freq);
|
||||||
|
|
||||||
/* Get the raw value
|
/* Get the raw value
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_getRawTemp(int16_t *temp);
|
bool ds3231_getRawTemp(i2c_dev_t *dev, int16_t *temp);
|
||||||
|
|
||||||
/* Get the temperature as an integer
|
/* Get the temperature as an integer
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_getTempInteger(int8_t *temp);
|
bool ds3231_getTempInteger(i2c_dev_t *dev, int8_t *temp);
|
||||||
|
|
||||||
/* Get the temerapture as a float (in quarter degree increments)
|
/* Get the temerapture as a float (in quarter degree increments)
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_getTempFloat(float *temp);
|
bool ds3231_getTempFloat(i2c_dev_t *dev, float *temp);
|
||||||
|
|
||||||
/* Get the time from the rtc, populates a supplied tm struct
|
/* Get the time from the rtc, populates a supplied tm struct
|
||||||
* returns true to indicate success
|
* returns true to indicate success
|
||||||
*/
|
*/
|
||||||
bool ds3231_getTime(struct tm *time);
|
bool ds3231_getTime(i2c_dev_t *dev, struct tm *time);
|
||||||
void ds3231_Init(uint8_t bus);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ void i2c_init(uint8_t bus, uint8_t scl_pin, uint8_t sda_pin, uint8_t freq)
|
||||||
|
|
||||||
// Prevent user, if frequency is high
|
// Prevent user, if frequency is high
|
||||||
if (sdk_system_get_cpu_freq() == SYS_CPU_80MHZ)
|
if (sdk_system_get_cpu_freq() == SYS_CPU_80MHZ)
|
||||||
if (I2C_CUSTOM_DELAY_80MHZ == 1)
|
if (i2c_freq_array[i2c_bus[bus].frequency][1] == 1)
|
||||||
debug("Max frequency is 320Khz at 80MHz");
|
debug("Max frequency is 320Khz at 80MHz");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -130,12 +130,12 @@ static inline void clear_sda(uint8_t bus)
|
||||||
// Output start condition
|
// Output start condition
|
||||||
void i2c_start(uint8_t bus)
|
void i2c_start(uint8_t bus)
|
||||||
{
|
{
|
||||||
uint32_t clk_stretch = CLK_STRETCH;
|
|
||||||
freq = sdk_system_get_cpu_freq();
|
freq = sdk_system_get_cpu_freq();
|
||||||
if (i2c_bus[bus].started) { // if started, do a restart cond
|
if (i2c_bus[bus].started) { // if started, do a restart cond
|
||||||
// Set SDA to 1
|
// Set SDA to 1
|
||||||
(void) read_sda(bus);
|
(void) read_sda(bus);
|
||||||
i2c_delay(bus);
|
i2c_delay(bus);
|
||||||
|
uint32_t clk_stretch = CLK_STRETCH;
|
||||||
while (read_scl(bus) == 0 && clk_stretch--) ;
|
while (read_scl(bus) == 0 && clk_stretch--) ;
|
||||||
// Repeated start setup time, minimum 4.7us
|
// Repeated start setup time, minimum 4.7us
|
||||||
i2c_delay(bus);
|
i2c_delay(bus);
|
||||||
|
|
@ -229,7 +229,7 @@ uint8_t i2c_read(uint8_t bus, bool ack)
|
||||||
uint8_t byte = 0;
|
uint8_t byte = 0;
|
||||||
uint8_t bit;
|
uint8_t bit;
|
||||||
for (bit = 0; bit < 8; bit++) {
|
for (bit = 0; bit < 8; bit++) {
|
||||||
byte = (byte << 1) | i2c_read_bit(bus);
|
byte = ((byte << 1)) | (i2c_read_bit(bus));
|
||||||
}
|
}
|
||||||
i2c_write_bit(bus,ack);
|
i2c_write_bit(bus,ack);
|
||||||
return byte;
|
return byte;
|
||||||
|
|
|
||||||
|
|
@ -53,8 +53,17 @@ extern "C" {
|
||||||
|
|
||||||
const static uint8_t i2c_freq_array[3][2] = { {100,20}, {10,1}, {6,1} } ;
|
const static uint8_t i2c_freq_array[3][2] = { {100,20}, {10,1}, {6,1} } ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Device descriptor
|
||||||
|
*/
|
||||||
|
typedef struct i2c_dev {
|
||||||
|
uint8_t bus ;
|
||||||
|
uint8_t addr ;
|
||||||
|
} i2c_dev_t ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bus settings
|
||||||
|
*/
|
||||||
typedef struct i2c_bus_description {
|
typedef struct i2c_bus_description {
|
||||||
uint8_t g_scl_pin; // Scl pin
|
uint8_t g_scl_pin; // Scl pin
|
||||||
uint8_t g_sda_pin; // Sda pin
|
uint8_t g_sda_pin; // Sda pin
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue