I2C bus upgrade (#432)

This commit is contained in:
Zaltora 2017-09-01 06:29:32 -03:00 committed by Ruslan V. Uss
parent d100f42b1f
commit b83c2629b9
56 changed files with 909 additions and 804 deletions

View file

@ -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>

View file

@ -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;

View file

@ -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);
} }

View file

@ -13,18 +13,25 @@
// BMP180 driver // BMP180 driver
#include "bmp180/bmp180.h" #include "bmp180/bmp180.h"
#define MY_EVT_TIMER 0x01 #define I2C_BUS 0
#define MY_EVT_BMP180 0x02
#define SCL_PIN GPIO_ID_PIN((0)) #define SCL_PIN GPIO_ID_PIN((0))
#define SDA_PIN GPIO_ID_PIN((2)) #define SDA_PIN GPIO_ID_PIN((2))
#define MY_EVT_TIMER 0x01
#define MY_EVT_BMP180 0x02
typedef struct typedef struct
{ {
uint8_t event_type; uint8_t event_type;
bmp180_result_t bmp180_data; bmp180_result_t bmp180_data;
} my_event_t; } my_event_t;
//device descriptor
i2c_dev_t dev = {
.addr = BMP180_DEVICE_ADDRESS,
.bus = I2C_BUS,
};
// Communication Queue // Communication Queue
static QueueHandle_t mainqueue; static QueueHandle_t mainqueue;
static TimerHandle_t timerHandle; static TimerHandle_t timerHandle;
@ -70,7 +77,7 @@ void bmp180_task(void *pvParameters)
case MY_EVT_TIMER: case MY_EVT_TIMER:
printf("%s: Received Timer Event\n", __FUNCTION__); printf("%s: Received Timer Event\n", __FUNCTION__);
bmp180_trigger_measurement(com_queue); bmp180_trigger_measurement(&dev, com_queue);
break; break;
case MY_EVT_BMP180: case MY_EVT_BMP180:
printf("%s: Received BMP180 Event temp:=%d.%dC press=%d.%02dhPa\n", __FUNCTION__, \ printf("%s: Received BMP180 Event temp:=%d.%dC press=%d.%02dhPa\n", __FUNCTION__, \
@ -91,6 +98,9 @@ void user_setup(void)
// Give the UART some time to settle // Give the UART some time to settle
sdk_os_delay_us(500); sdk_os_delay_us(500);
// Init I2C bus Interface
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
} }
void user_init(void) void user_init(void)
@ -107,7 +117,7 @@ void user_init(void)
bmp180_informUser = bmp180_i2c_informUser; bmp180_informUser = bmp180_i2c_informUser;
// Init BMP180 Interface // Init BMP180 Interface
bmp180_init(SCL_PIN, SDA_PIN); bmp180_init(&dev);
// Create Main Communication Queue // Create Main Communication Queue
mainqueue = xQueueCreate(10, sizeof(my_event_t)); mainqueue = xQueueCreate(10, sizeof(my_event_t));

View file

@ -12,7 +12,7 @@
// In forced mode user initiate measurement each time. // In forced mode user initiate measurement each time.
// In normal mode measurement is done continuously with specified standby time. // In normal mode measurement is done continuously with specified standby time.
// #define MODE_FORCED // #define MODE_FORCED
const uint8_t i2c_bus = 0;
const uint8_t scl_pin = 0; const uint8_t scl_pin = 0;
const uint8_t sda_pin = 2; const uint8_t sda_pin = 2;
@ -26,7 +26,8 @@ static void bmp280_task_forced(void *pvParameters)
params.mode = BMP280_MODE_FORCED; params.mode = BMP280_MODE_FORCED;
bmp280_t bmp280_dev; 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 (1) {
while (!bmp280_init(&bmp280_dev, &params)) { while (!bmp280_init(&bmp280_dev, &params)) {
@ -67,7 +68,8 @@ static void bmp280_task_normal(void *pvParameters)
bmp280_init_default_params(&params); bmp280_init_default_params(&params);
bmp280_t bmp280_dev; 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 (1) {
while (!bmp280_init(&bmp280_dev, &params)) { 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("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_400K);
#ifdef MODE_FORCED #ifdef MODE_FORCED
xTaskCreate(bmp280_task_forced, "bmp280_task", 256, NULL, 2, NULL); xTaskCreate(bmp280_task_forced, "bmp280_task", 256, NULL, 2, NULL);

View file

@ -11,6 +11,7 @@
#include <ds1307/ds1307.h> #include <ds1307/ds1307.h>
#include <stdio.h> #include <stdio.h>
#define I2C_BUS 0
#define SCL_PIN 5 #define SCL_PIN 5
#define SDA_PIN 4 #define SDA_PIN 4
@ -19,8 +20,12 @@ 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_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_400K);
ds1307_start(true); i2c_dev_t dev = {
.addr = DS1307_ADDR,
.bus = I2C_BUS,
};
ds1307_start(&dev, true);
// setup datetime: 2016-10-09 13:50:10 // setup datetime: 2016-10-09 13:50:10
struct tm time = { struct tm time = {
@ -31,11 +36,11 @@ void user_init(void)
.tm_min = 50, .tm_min = 50,
.tm_sec = 10 .tm_sec = 10
}; };
ds1307_set_time(&time); ds1307_set_time(&dev, &time);
while (true) 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, 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); time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec);

View file

@ -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);
} }
} }
@ -35,7 +41,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);
ds3231_Init(scl, sda); i2c_init(0,scl,sda,I2C_FREQ_400K);
xTaskCreate(task1, "tsk1", 256, NULL, 2, NULL); xTaskCreate(task1, "tsk1", 256, NULL, 2, NULL);
} }

View file

@ -18,6 +18,7 @@
#define CS_GPIO_PIN 2 #define CS_GPIO_PIN 2
// ds1307 // ds1307
#define I2C_BUS 0
#define SCL_PIN 5 #define SCL_PIN 5
#define SDA_PIN 4 #define SDA_PIN 4
@ -29,7 +30,11 @@
uint32_t get_fattime() uint32_t get_fattime()
{ {
struct tm time; struct tm time;
ds1307_get_time(&time); i2c_dev_t dev = {
.addr = DS1307_ADDR,
.bus = I2C_BUS,
};
ds1307_get_time(&dev, &time);
return ((uint32_t)(time.tm_year - 1980) << 25) return ((uint32_t)(time.tm_year - 1980) << 25)
| ((uint32_t)time.tm_mon << 21) | ((uint32_t)time.tm_mon << 21)
@ -127,7 +132,7 @@ void user_init(void)
uart_set_baud(0, 115200); uart_set_baud(0, 115200);
printf("SDK version:%s\n\n", sdk_system_get_sdk_version()); 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_400K);
xTaskCreate(rewrite_file_task, "task1", 512, NULL, 2, NULL); xTaskCreate(rewrite_file_task, "task1", 512, NULL, 2, NULL);
} }

View file

@ -11,6 +11,7 @@
#include <i2c/i2c.h> #include <i2c/i2c.h>
#include <hmc5883l/hmc5883l.h> #include <hmc5883l/hmc5883l.h>
#define I2C_BUS 0
#define SCL_PIN 5 #define SCL_PIN 5
#define SDA_PIN 4 #define SDA_PIN 4
@ -19,20 +20,24 @@ void user_init(void)
uart_set_baud(0, 115200); uart_set_baud(0, 115200);
printf("SDK version:%s\n\n", sdk_system_get_sdk_version()); 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"); printf("Device not found\n");
hmc5883l_set_operating_mode(HMC5883L_MODE_CONTINUOUS); hmc5883l_set_operating_mode(&dev, HMC5883L_MODE_CONTINUOUS);
hmc5883l_set_samples_averaged(HMC5883L_SAMPLES_8); hmc5883l_set_samples_averaged(&dev, HMC5883L_SAMPLES_8);
hmc5883l_set_data_rate(HMC5883L_DATA_RATE_07_50); hmc5883l_set_data_rate(&dev, HMC5883L_DATA_RATE_07_50);
hmc5883l_set_gain(HMC5883L_GAIN_1090); hmc5883l_set_gain(&dev, HMC5883L_GAIN_1090);
while (true) while (true)
{ {
hmc5883l_data_t data; 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); 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++) for (uint32_t i = 0; i < 1000; i++)

View file

@ -13,6 +13,7 @@
#include <hd44780/hd44780.h> #include <hd44780/hd44780.h>
#define I2C_BUS 0
#define SCL_PIN 5 #define SCL_PIN 5
#define SDA_PIN 4 #define SDA_PIN 4
#define ADDR 0x27 #define ADDR 0x27
@ -27,10 +28,11 @@ 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_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
hd44780_t lcd = { hd44780_t lcd = {
.addr = ADDR, .i2c_dev.bus = I2C_BUS,
.i2c_dev.addr = ADDR,
.font = HD44780_FONT_5X8, .font = HD44780_FONT_5X8,
.lines = 2, .lines = 2,
.pins = { .pins = {

View file

@ -13,6 +13,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "ina3221/ina3221.h" #include "ina3221/ina3221.h"
#define I2C_BUS 0
#define PIN_SCL 5 #define PIN_SCL 5
#define PIN_SDA 2 #define PIN_SDA 2
#define ADDR INA3221_ADDR_0 #define ADDR INA3221_ADDR_0
@ -33,7 +34,8 @@ void ina_measure(void *pvParameters)
// Create ina3221 device // Create ina3221 device
ina3221_t dev = { ina3221_t dev = {
.addr = ADDR, .i2c_dev.bus = I2C_BUS,
.i2c_dev.addr = ADDR,
.shunt = { 100 ,100 ,100 }, // shunt values are 100 mOhm for each channel .shunt = { 100 ,100 ,100 }, // shunt values are 100 mOhm for each channel
.mask.mask_register = INA3221_DEFAULT_MASK, // Init .mask.mask_register = INA3221_DEFAULT_MASK, // Init
.config.config_register = INA3221_DEFAULT_CONFIG, // Init .config.config_register = INA3221_DEFAULT_CONFIG, // Init
@ -120,7 +122,7 @@ 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(PIN_SCL,PIN_SDA); i2c_init(I2C_BUS, PIN_SCL, PIN_SDA, I2C_FREQ_400K);
xTaskCreate(ina_measure, "Measurements_task", 512, NULL, 2, NULL); xTaskCreate(ina_measure, "Measurements_task", 512, NULL, 2, NULL);
} }

View file

@ -14,14 +14,15 @@
#include <FreeRTOS.h> #include <FreeRTOS.h>
#include <task.h> #include <task.h>
#define I2C_BUS 0
#define SCL_PIN 5 #define SCL_PIN 5
#define SDA_PIN 4 #define SDA_PIN 4
#define ADDR MCP4725A0_ADDR0 #define ADDR MCP4725A0_ADDR0
#define VDD 3.3 #define VDD 3.3
inline static void wait_for_eeprom() inline static void wait_for_eeprom(i2c_dev_t* dev)
{ {
while (mcp4725_eeprom_busy(ADDR)) while (mcp4725_eeprom_busy(dev))
{ {
printf("...DAC is busy, waiting...\n"); printf("...DAC is busy, waiting...\n");
vTaskDelay(1); vTaskDelay(1);
@ -33,21 +34,25 @@ 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_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
i2c_dev_t dev = {
.addr = ADDR,
.bus = I2C_BUS,
};
// setup EEPROM values // setup EEPROM values
if (mcp4725_get_power_mode(ADDR, true) != MCP4725_PM_NORMAL) if (mcp4725_get_power_mode(&dev, true) != MCP4725_PM_NORMAL)
{ {
printf("DAC was sleeping... Wake up Neo!\n"); printf("DAC was sleeping... Wake up Neo!\n");
mcp4725_set_power_mode(ADDR, MCP4725_PM_NORMAL, true); mcp4725_set_power_mode(&dev, MCP4725_PM_NORMAL, true);
wait_for_eeprom(); wait_for_eeprom(&dev);
} }
printf("Set default DAC ouptut value to MAX...\n"); printf("Set default DAC ouptut value to MAX...\n");
mcp4725_set_raw_output(ADDR, MCP4725_MAX_VALUE, true); mcp4725_set_raw_output(&dev, MCP4725_MAX_VALUE, true);
wait_for_eeprom(); wait_for_eeprom(&dev);
printf("And now default DAC output value is 0x%03x\n", mcp4725_get_raw_output(ADDR, true)); printf("And now default DAC output value is 0x%03x\n", mcp4725_get_raw_output(&dev, true));
printf("Now let's generate the sawtooth wave in slow manner\n"); printf("Now let's generate the sawtooth wave in slow manner\n");
@ -58,7 +63,7 @@ void user_init(void)
if (vout > VDD) vout = 0; if (vout > VDD) vout = 0;
printf("Vout: %.02f\n", vout); printf("Vout: %.02f\n", vout);
mcp4725_set_voltage(ADDR, VDD, vout, false); mcp4725_set_voltage(&dev, VDD, vout, false);
// It will be very low freq wave // It will be very low freq wave
vTaskDelay(100 / portTICK_PERIOD_MS); vTaskDelay(100 / portTICK_PERIOD_MS);

View file

@ -13,18 +13,20 @@
#include <i2c/i2c.h> #include <i2c/i2c.h>
#include <ms561101ba03/ms561101ba03.h> #include <ms561101ba03/ms561101ba03.h>
#define I2C_BUS 0
#define SCL_PIN 5 #define SCL_PIN 5
#define SDA_PIN 4 #define SDA_PIN 4
void user_init(void) void user_init(void)
{ {
i2c_init(SCL_PIN, SDA_PIN); i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
uart_set_baud(0, 115200); uart_set_baud(0, 115200);
printf("SDK version:%s\n\n", sdk_system_get_sdk_version()); printf("SDK version:%s\n\n", sdk_system_get_sdk_version());
ms561101ba03_t device = { ms561101ba03_t device = {
.addr = MS561101BA03_ADDR_CSB_LOW, .i2c_dev.bus = I2C_BUS,
.i2c_dev.addr = MS561101BA03_ADDR_CSB_LOW,
.osr = MS561101BA03_OSR_4096, .osr = MS561101BA03_OSR_4096,
}; };

View file

@ -11,8 +11,9 @@
#include <pca9685/pca9685.h> #include <pca9685/pca9685.h>
#include <stdio.h> #include <stdio.h>
#define ADDR 0x40 #define ADDR PCA9685_ADDR_BASE
#define I2C_BUS 0
#define SCL_PIN 5 #define SCL_PIN 5
#define SDA_PIN 4 #define SDA_PIN 4
@ -23,19 +24,23 @@ 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_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
i2c_dev_t dev = {
.addr = ADDR,
.bus = I2C_BUS,
};
pca9685_init(ADDR); pca9685_init(&dev);
pca9685_set_pwm_frequency(ADDR, 1000); pca9685_set_pwm_frequency(&dev, 1000);
printf("Freq 1000Hz, real %d\n", pca9685_get_pwm_frequency(ADDR)); printf("Freq 1000Hz, real %d\n", pca9685_get_pwm_frequency(&dev));
uint16_t val = 0; uint16_t val = 0;
while (true) while (true)
{ {
printf("Set ch0 to %d, ch4 to %d\n", val, 4096 - val); printf("Set ch0 to %d, ch4 to %d\n", val, 4096 - val);
pca9685_set_pwm_value(ADDR, 0, val); pca9685_set_pwm_value(&dev, 0, val);
pca9685_set_pwm_value(ADDR, 4, 4096 - val); pca9685_set_pwm_value(&dev, 4, 4096 - val);
if (val++ == 4096) if (val++ == 4096)
val = 0; val = 0;

View file

@ -9,15 +9,21 @@
#include "i2c/i2c.h" #include "i2c/i2c.h"
#include "pcf8591/pcf8591.h" #include "pcf8591/pcf8591.h"
#define ADDR PCF8591_DEFAULT_ADDRESS
#define I2C_BUS 0
#define SCL_PIN 5 #define SCL_PIN 5
#define SDA_PIN 4 #define SDA_PIN 4
static void measure(void *pvParameters) static void measure(void *pvParameters)
{ {
i2c_dev_t dev = {
.addr = ADDR,
.bus = I2C_BUS,
};
while (1) while (1)
{ {
vTaskDelay(1000 / portTICK_PERIOD_MS); vTaskDelay(1000 / portTICK_PERIOD_MS);
printf("Value: %d\n", pcf8591_read(PCF8591_DEFAULT_ADDRESS, 0x03)); printf("Value: %d\n", pcf8591_read(&dev, 0x03));
} }
} }
@ -30,7 +36,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);
} }

View file

@ -23,6 +23,7 @@
#ifdef I2C_CONNECTION #ifdef I2C_CONNECTION
#define PROTOCOL SSD1306_PROTO_I2C #define PROTOCOL SSD1306_PROTO_I2C
#define ADDR SSD1306_I2C_ADDR_0 #define ADDR SSD1306_I2C_ADDR_0
#define I2C_BUS 0
#define SCL_PIN 5 #define SCL_PIN 5
#define SDA_PIN 4 #define SDA_PIN 4
#else #else
@ -35,7 +36,8 @@
static const ssd1306_t dev = { static const ssd1306_t dev = {
.protocol = PROTOCOL, .protocol = PROTOCOL,
#ifdef I2C_CONNECTION #ifdef I2C_CONNECTION
.addr = ADDR, .i2c_dev.bus = I2C_BUS,
.i2c_dev.addr = ADDR,
#else #else
.cs_pin = CS_PIN, .cs_pin = CS_PIN,
.dc_pin = DC_PIN, .dc_pin = DC_PIN,
@ -97,7 +99,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());
#ifdef I2C_CONNECTION #ifdef I2C_CONNECTION
i2c_init(SCL_PIN, SDA_PIN); i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_400K);
#endif #endif
while (ssd1306_init(&dev) != 0) while (ssd1306_init(&dev) != 0)

View file

@ -29,6 +29,7 @@
#ifdef I2C_CONNECTION #ifdef I2C_CONNECTION
#define PROTOCOL SSD1306_PROTO_I2C #define PROTOCOL SSD1306_PROTO_I2C
#define ADDR SSD1306_I2C_ADDR_0 #define ADDR SSD1306_I2C_ADDR_0
#define I2C_BUS 0
#define SCL_PIN 5 #define SCL_PIN 5
#define SDA_PIN 4 #define SDA_PIN 4
#else #else
@ -43,7 +44,8 @@
static const ssd1306_t dev = { static const ssd1306_t dev = {
.protocol = PROTOCOL, .protocol = PROTOCOL,
#ifdef I2C_CONNECTION #ifdef I2C_CONNECTION
.addr = ADDR, .i2c_dev.bus = I2C_BUS,
.i2c_dev.addr = ADDR,
#else #else
.cs_pin = CS_PIN, .cs_pin = CS_PIN,
.dc_pin = DC_PIN, .dc_pin = DC_PIN,
@ -162,7 +164,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());
#ifdef I2C_CONNECTION #ifdef I2C_CONNECTION
i2c_init(SCL_PIN, SDA_PIN); i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_400K);
#endif #endif
while (ssd1306_init(&dev) != 0) { while (ssd1306_init(&dev) != 0) {

View file

@ -16,6 +16,7 @@
* Connect 3.3v from the ESP to Vin and GND to GND * Connect 3.3v from the ESP to Vin and GND to GND
*/ */
#define I2C_BUS (0)
#define SCL_PIN (2) #define SCL_PIN (2)
#define SDA_PIN (0) #define SDA_PIN (0)
@ -27,7 +28,8 @@ void tsl2561MeasurementTask(void *pvParameters)
// TSL2561_I2C_ADDR_VCC (0x49) // TSL2561_I2C_ADDR_VCC (0x49)
// TSL2561_I2C_ADDR_GND (0x29) // TSL2561_I2C_ADDR_GND (0x29)
// TSL2561_I2C_ADDR_FLOAT (0x39) Default // TSL2561_I2C_ADDR_FLOAT (0x39) Default
lightSensor.i2c_addr = TSL2561_I2C_ADDR_FLOAT; lightSensor.i2c_dev.bus = I2C_BUS;
lightSensor.i2c_dev.addr = TSL2561_I2C_ADDR_FLOAT;
tsl2561_init(&lightSensor); tsl2561_init(&lightSensor);
@ -63,7 +65,7 @@ void tsl2561MeasurementTask(void *pvParameters)
void user_init(void) void user_init(void)
{ {
uart_set_baud(0, 115200); uart_set_baud(0, 115200);
i2c_init(SCL_PIN, SDA_PIN); i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
xTaskCreate(tsl2561MeasurementTask, "tsl2561MeasurementTask", 256, NULL, 2, NULL); xTaskCreate(tsl2561MeasurementTask, "tsl2561MeasurementTask", 256, NULL, 2, NULL);
} }

View file

@ -16,13 +16,15 @@
* Connect 3.3v from the ESP to Vin and GND to GND * Connect 3.3v from the ESP to Vin and GND to GND
*/ */
#define I2C_BUS (0)
#define SCL_PIN (2) #define SCL_PIN (2)
#define SDA_PIN (0) #define SDA_PIN (0)
void tsl4531MeasurementTask(void *pvParameters) void tsl4531MeasurementTask(void *pvParameters)
{ {
tsl4531_t lightSensor; tsl4531_t lightSensor;
lightSensor.i2c_addr = TSL4531_I2C_ADDR; lightSensor.i2c_dev.bus= I2C_BUS;
lightSensor.i2c_dev.addr= TSL4531_I2C_ADDR;
tsl4531_init(&lightSensor); tsl4531_init(&lightSensor);
tsl4531_set_integration_time(&lightSensor, TSL4531_INTEGRATION_400MS); tsl4531_set_integration_time(&lightSensor, TSL4531_INTEGRATION_400MS);
@ -49,7 +51,7 @@ void tsl4531MeasurementTask(void *pvParameters)
void user_init(void) void user_init(void)
{ {
uart_set_baud(0, 115200); uart_set_baud(0, 115200);
i2c_init(SCL_PIN, SDA_PIN); i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
xTaskCreate(tsl4531MeasurementTask, "tsl4531MeasurementTask", 256, NULL, 2, NULL); xTaskCreate(tsl4531MeasurementTask, "tsl4531MeasurementTask", 256, NULL, 2, NULL);
} }

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); prepare(channel, REG_DATA, true, params->cs_pin, false);
return read_word(params->cs_pin); return read_word(params->cs_pin);
} }

View file

@ -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, &reg, (uint8_t *)&res, 2)) if (i2c_slave_read(dev->bus, dev->addr, &reg, (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, &reg, buf, 2)) if (i2c_slave_write(dev->bus, dev->addr, &reg, 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);
} }

View file

@ -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
} }

View file

@ -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

View file

@ -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
} }

View file

@ -7,13 +7,9 @@
#include "espressif/esp_common.h" #include "espressif/esp_common.h"
#include "espressif/sdk_private.h" #include "espressif/sdk_private.h"
#include "i2c/i2c.h"
#define BMP180_RX_QUEUE_SIZE 10 #define BMP180_RX_QUEUE_SIZE 10
#define BMP180_TASK_PRIORITY 9 #define BMP180_TASK_PRIORITY 9
#define BMP180_DEVICE_ADDRESS 0x77
#define BMP180_VERSION_REG 0xD0 #define BMP180_VERSION_REG 0xD0
#define BMP180_CONTROL_REG 0xF4 #define BMP180_CONTROL_REG 0xF4
#define BMP180_RESET_REG 0xE0 #define BMP180_RESET_REG 0xE0
@ -39,43 +35,43 @@
// //
#define BMP180_RESET_VALUE 0xB6 #define BMP180_RESET_VALUE 0xB6
static int bmp180_readRegister16(uint8_t reg, int16_t *r) static int bmp180_readRegister16(i2c_dev_t *dev, uint8_t reg, int16_t *r)
{ {
uint8_t d[] = { 0, 0 }; uint8_t d[] = { 0, 0 };
int error ; int error ;
if ((error = i2c_slave_read(BMP180_DEVICE_ADDRESS, &reg, d, 2))) if ((error = i2c_slave_read(dev->bus, dev->addr, &reg, d, 2)))
return error; return error;
*r = ((int16_t)d[0] << 8) | (d[1]); *r = ((int16_t)d[0] << 8) | (d[1]);
return 0; return 0;
} }
static int bmp180_start_Messurement(uint8_t cmd) static int bmp180_start_Messurement(i2c_dev_t *dev, uint8_t cmd)
{ {
uint8_t reg = BMP180_CONTROL_REG ; uint8_t reg = BMP180_CONTROL_REG ;
return i2c_slave_write(BMP180_DEVICE_ADDRESS, &reg, &cmd, 1); return i2c_slave_write(dev->bus, dev->addr, &reg, &cmd, 1);
} }
static bool bmp180_get_uncompensated_temperature(int32_t *ut) static bool bmp180_get_uncompensated_temperature(i2c_dev_t *dev, int32_t *ut)
{ {
// Write Start Code into reg 0xF4. // Write Start Code into reg 0xF4.
if (bmp180_start_Messurement(BMP180_MEASURE_TEMP)) if (bmp180_start_Messurement(dev, BMP180_MEASURE_TEMP))
return false; return false;
// Wait 5ms, datasheet states 4.5ms // Wait 5ms, datasheet states 4.5ms
sdk_os_delay_us(5000); sdk_os_delay_us(5000);
int16_t v; int16_t v;
if (bmp180_readRegister16(BMP180_OUT_MSB_REG, &v)) if (bmp180_readRegister16(dev, BMP180_OUT_MSB_REG, &v))
return false; return false;
*ut = v; *ut = v;
return true; return true;
} }
static bool bmp180_get_uncompensated_pressure(uint8_t oss, uint32_t *up) static bool bmp180_get_uncompensated_pressure(i2c_dev_t *dev, uint8_t oss, uint32_t *up)
{ {
uint16_t us; uint16_t us;
@ -89,14 +85,14 @@ static bool bmp180_get_uncompensated_pressure(uint8_t oss, uint32_t *up)
} }
// Write Start Code into reg 0xF4 // Write Start Code into reg 0xF4
if (bmp180_start_Messurement(BMP180_MEASURE_PRESS | (oss << 6))) if (bmp180_start_Messurement(dev, BMP180_MEASURE_PRESS | (oss << 6)))
return false; return false;
sdk_os_delay_us(us); sdk_os_delay_us(us);
uint8_t d[] = { 0, 0, 0 }; uint8_t d[] = { 0, 0, 0 };
uint8_t reg = BMP180_OUT_MSB_REG; uint8_t reg = BMP180_OUT_MSB_REG;
if (i2c_slave_read(BMP180_DEVICE_ADDRESS, &reg, d, 3)) if (i2c_slave_read(dev->bus, dev->addr, &reg, d, 3))
return false; return false;
uint32_t r = ((uint32_t)d[0] << 16) | ((uint32_t)d[1] << 8) | d[2]; uint32_t r = ((uint32_t)d[0] << 16) | ((uint32_t)d[1] << 8) | d[2];
@ -106,19 +102,19 @@ static bool bmp180_get_uncompensated_pressure(uint8_t oss, uint32_t *up)
} }
// Returns true of success else false. // Returns true of success else false.
bool bmp180_fillInternalConstants(bmp180_constants_t *c) bool bmp180_fillInternalConstants(i2c_dev_t *dev, bmp180_constants_t *c)
{ {
if (bmp180_readRegister16(BMP180_CALIBRATION_REG+0, &c->AC1) || if (bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+0, &c->AC1) ||
bmp180_readRegister16(BMP180_CALIBRATION_REG+2, &c->AC2) || bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+2, &c->AC2) ||
bmp180_readRegister16(BMP180_CALIBRATION_REG+4, &c->AC3) || bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+4, &c->AC3) ||
bmp180_readRegister16(BMP180_CALIBRATION_REG+6, (int16_t *)&c->AC4) || bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+6, (int16_t *)&c->AC4) ||
bmp180_readRegister16(BMP180_CALIBRATION_REG+8, (int16_t *)&c->AC5) || bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+8, (int16_t *)&c->AC5) ||
bmp180_readRegister16(BMP180_CALIBRATION_REG+10, (int16_t *)&c->AC6) || bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+10, (int16_t *)&c->AC6) ||
bmp180_readRegister16(BMP180_CALIBRATION_REG+12, &c->B1) || bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+12, &c->B1) ||
bmp180_readRegister16(BMP180_CALIBRATION_REG+14, &c->B2) || bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+14, &c->B2) ||
bmp180_readRegister16(BMP180_CALIBRATION_REG+16, &c->MB) || bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+16, &c->MB) ||
bmp180_readRegister16(BMP180_CALIBRATION_REG+18, &c->MC) || bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+18, &c->MC) ||
bmp180_readRegister16(BMP180_CALIBRATION_REG+20, &c->MD)) { bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+20, &c->MD)) {
return false; return false;
} }
@ -139,16 +135,16 @@ bool bmp180_fillInternalConstants(bmp180_constants_t *c)
c->MB == 0xffff || c->MC == 0xffff || c->MD == 0xffff); c->MB == 0xffff || c->MC == 0xffff || c->MD == 0xffff);
} }
bool bmp180_is_available() bool bmp180_is_available(i2c_dev_t *dev)
{ {
uint8_t id; uint8_t id;
uint8_t reg = BMP180_VERSION_REG; uint8_t reg = BMP180_VERSION_REG;
if (i2c_slave_read(BMP180_DEVICE_ADDRESS, &reg, &id, 1)) if (i2c_slave_read(dev->bus, dev->addr, &reg, &id, 1))
return false; return false;
return id == BMP180_CHIP_ID; return id == BMP180_CHIP_ID;
} }
bool bmp180_measure(bmp180_constants_t *c, int32_t *temperature, bool bmp180_measure(i2c_dev_t *dev, bmp180_constants_t *c, int32_t *temperature,
uint32_t *pressure, uint8_t oss) uint32_t *pressure, uint8_t oss)
{ {
int32_t T, P; int32_t T, P;
@ -160,7 +156,7 @@ bool bmp180_measure(bmp180_constants_t *c, int32_t *temperature,
// //
// Calculation taken from BMP180 Datasheet // Calculation taken from BMP180 Datasheet
int32_t UT, X1, X2, B5; int32_t UT, X1, X2, B5;
if (!bmp180_get_uncompensated_temperature(&UT)) if (!bmp180_get_uncompensated_temperature(dev, &UT))
return false; return false;
X1 = ((UT - (int32_t)c->AC6) * (int32_t)c->AC5) >> 15; X1 = ((UT - (int32_t)c->AC6) * (int32_t)c->AC5) >> 15;
@ -177,7 +173,7 @@ bool bmp180_measure(bmp180_constants_t *c, int32_t *temperature,
int32_t X3, B3, B6; int32_t X3, B3, B6;
uint32_t B4, B7, UP; uint32_t B4, B7, UP;
if (!bmp180_get_uncompensated_pressure(oss, &UP)) if (!bmp180_get_uncompensated_pressure(dev, oss, &UP))
return false; return false;
// Calculation taken from BMP180 Datasheet // Calculation taken from BMP180 Datasheet
@ -220,8 +216,8 @@ typedef struct
} bmp180_command_t; } bmp180_command_t;
// Just works due to the fact that QueueHandle_t is a "void *" // Just works due to the fact that QueueHandle_t is a "void *"
static QueueHandle_t bmp180_rx_queue = NULL; static QueueHandle_t bmp180_rx_queue[MAX_I2C_BUS] = { NULL };
static TaskHandle_t bmp180_task_handle = NULL; static TaskHandle_t bmp180_task_handle[MAX_I2C_BUS] = { NULL };
// //
// Forward declarations // Forward declarations
@ -237,6 +233,7 @@ static void bmp180_driver_task(void *pvParameters)
// Data to be received from user // Data to be received from user
bmp180_command_t current_command; bmp180_command_t current_command;
bmp180_constants_t bmp180_constants; bmp180_constants_t bmp180_constants;
i2c_dev_t *dev = (i2c_dev_t*)pvParameters;
#ifdef BMP180_DEBUG #ifdef BMP180_DEBUG
// Wait for commands from the outside // Wait for commands from the outside
@ -244,14 +241,14 @@ static void bmp180_driver_task(void *pvParameters)
#endif #endif
// Initialize all internal constants. // Initialize all internal constants.
if (!bmp180_fillInternalConstants(&bmp180_constants)) { if (!bmp180_fillInternalConstants(dev, &bmp180_constants)) {
printf("%s: reading internal constants failed\n", __FUNCTION__); printf("%s: reading internal constants failed\n", __FUNCTION__);
vTaskDelete(NULL); vTaskDelete(NULL);
} }
while(1) { while(1) {
// Wait for user to insert commands // Wait for user to insert commands
if (xQueueReceive(bmp180_rx_queue, &current_command, portMAX_DELAY) == pdTRUE) { if (xQueueReceive(bmp180_rx_queue[dev->bus], &current_command, portMAX_DELAY) == pdTRUE) {
#ifdef BMP180_DEBUG #ifdef BMP180_DEBUG
printf("%s: Received user command %d 0x%p\n", __FUNCTION__, current_command.cmd, current_command.resultQueue); printf("%s: Received user command %d 0x%p\n", __FUNCTION__, current_command.cmd, current_command.resultQueue);
#endif #endif
@ -261,7 +258,7 @@ static void bmp180_driver_task(void *pvParameters)
int32_t T = 0; int32_t T = 0;
uint32_t P = 0; uint32_t P = 0;
if (bmp180_measure(&bmp180_constants, &T, (current_command.cmd & BMP180_PRESSURE) ? &P : NULL, 3)) { if (bmp180_measure(dev, &bmp180_constants, &T, (current_command.cmd & BMP180_PRESSURE) ? &P : NULL, 3)) {
// Inform the user ... // Inform the user ...
if (!bmp180_informUser(current_command.resultQueue, if (!bmp180_informUser(current_command.resultQueue,
current_command.cmd, current_command.cmd,
@ -276,22 +273,22 @@ static void bmp180_driver_task(void *pvParameters)
} }
} }
static bool bmp180_create_communication_queues() static bool bmp180_create_communication_queues(i2c_dev_t *dev)
{ {
// Just create them once // Just create them once by bus
if (bmp180_rx_queue == NULL) if (bmp180_rx_queue[dev->bus] == NULL)
bmp180_rx_queue = xQueueCreate(BMP180_RX_QUEUE_SIZE, sizeof(bmp180_result_t)); bmp180_rx_queue[dev->bus] = xQueueCreate(BMP180_RX_QUEUE_SIZE, sizeof(bmp180_result_t));
return bmp180_rx_queue != NULL; return bmp180_rx_queue[dev->bus] != NULL;
} }
static bool bmp180_createTask() static bool bmp180_createTask(i2c_dev_t *dev)
{ {
// We already have a task // We already have a task
portBASE_TYPE x = pdPASS; portBASE_TYPE x = pdPASS;
if (bmp180_task_handle == NULL) { if (bmp180_task_handle[dev->bus] == NULL) {
x = xTaskCreate(bmp180_driver_task, "bmp180_driver_task", 256, NULL, BMP180_TASK_PRIORITY, &bmp180_task_handle); x = xTaskCreate(bmp180_driver_task, "bmp180_driver_task", 256, (void*)dev, BMP180_TASK_PRIORITY, &bmp180_task_handle[dev->bus]); //TODO: name task with i2c bus
} }
return x == pdPASS; return x == pdPASS;
} }
@ -309,54 +306,51 @@ static bool bmp180_informUser_Impl(const QueueHandle_t* resultQueue, uint8_t cmd
} }
// Just init all needed queues // Just init all needed queues
bool bmp180_init(uint8_t scl, uint8_t sda) bool bmp180_init(i2c_dev_t *dev)
{ {
// 1. Create required queues // 1. Create required queues
bool result = false; bool result = false;
if (bmp180_create_communication_queues()) { if (bmp180_create_communication_queues(dev)) {
// 2. Init i2c driver // 2. Check for bmp180 ...
i2c_init(scl, sda); if (bmp180_is_available(dev)) {
// 3. Check for bmp180 ... // 3. Start driver task
if (bmp180_is_available()) { if (bmp180_createTask(dev)) {
// 4. Start driver task
if (bmp180_createTask()) {
// We are finished // We are finished
result = true; result = true;
} }
} }
} }
return result; return result;
} }
void bmp180_trigger_measurement(const QueueHandle_t* resultQueue) void bmp180_trigger_measurement(i2c_dev_t *dev, const QueueHandle_t* resultQueue)
{ {
bmp180_command_t c; bmp180_command_t c;
c.cmd = BMP180_PRESSURE + BMP180_TEMPERATURE; c.cmd = BMP180_PRESSURE + BMP180_TEMPERATURE;
c.resultQueue = resultQueue; c.resultQueue = resultQueue;
xQueueSend(bmp180_rx_queue, &c, 0); xQueueSend(bmp180_rx_queue[dev->bus], &c, 0);
} }
void bmp180_trigger_pressure_measurement(const QueueHandle_t* resultQueue) void bmp180_trigger_pressure_measurement(i2c_dev_t *dev, const QueueHandle_t* resultQueue)
{ {
bmp180_command_t c; bmp180_command_t c;
c.cmd = BMP180_PRESSURE; c.cmd = BMP180_PRESSURE;
c.resultQueue = resultQueue; c.resultQueue = resultQueue;
xQueueSend(bmp180_rx_queue, &c, 0); xQueueSend(bmp180_rx_queue[dev->bus], &c, 0);
} }
void bmp180_trigger_temperature_measurement(const QueueHandle_t* resultQueue) void bmp180_trigger_temperature_measurement(i2c_dev_t *dev, const QueueHandle_t* resultQueue)
{ {
bmp180_command_t c; bmp180_command_t c;
c.cmd = BMP180_TEMPERATURE; c.cmd = BMP180_TEMPERATURE;
c.resultQueue = resultQueue; c.resultQueue = resultQueue;
xQueueSend(bmp180_rx_queue, &c, 0); xQueueSend(bmp180_rx_queue[dev->bus], &c, 0);
} }

View file

@ -14,9 +14,13 @@
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "queue.h" #include "queue.h"
#include "i2c/i2c.h"
// Uncomment to enable debug output // Uncomment to enable debug output
//#define BMP180_DEBUG //#define BMP180_DEBUG
#define BMP180_DEVICE_ADDRESS 0x77
#define BMP180_TEMPERATURE (1<<0) #define BMP180_TEMPERATURE (1<<0)
#define BMP180_PRESSURE (1<<1) #define BMP180_PRESSURE (1<<1)
@ -42,16 +46,16 @@ typedef struct
} bmp180_result_t; } bmp180_result_t;
// Init bmp180 driver ... // Init bmp180 driver ...
bool bmp180_init(uint8_t scl, uint8_t sda); bool bmp180_init(i2c_dev_t *dev);
// Trigger a "complete" measurement (temperature and pressure will be valid when given to "bmp180_informUser) // Trigger a "complete" measurement (temperature and pressure will be valid when given to "bmp180_informUser)
void bmp180_trigger_measurement(const QueueHandle_t* resultQueue); void bmp180_trigger_measurement(i2c_dev_t *dev, const QueueHandle_t* resultQueue);
// Trigger a "temperature only" measurement (only temperature will be valid when given to "bmp180_informUser) // Trigger a "temperature only" measurement (only temperature will be valid when given to "bmp180_informUser)
void bmp180_trigger_temperature_measurement(const QueueHandle_t* resultQueue); void bmp180_trigger_temperature_measurement(i2c_dev_t *dev, const QueueHandle_t* resultQueue);
// Trigger a "pressure only" measurement (only pressure will be valid when given to "bmp180_informUser) // Trigger a "pressure only" measurement (only pressure will be valid when given to "bmp180_informUser)
void bmp180_trigger_pressure_measurement(const QueueHandle_t* resultQueue); void bmp180_trigger_pressure_measurement(i2c_dev_t *dev, const QueueHandle_t* resultQueue);
// Give the user the chance to create it's own handler // Give the user the chance to create it's own handler
extern bool (*bmp180_informUser)(const QueueHandle_t* resultQueue, uint8_t cmd, bmp180_temp_t temperature, bmp180_press_t pressure); extern bool (*bmp180_informUser)(const QueueHandle_t* resultQueue, uint8_t cmd, bmp180_temp_t temperature, bmp180_press_t pressure);
@ -75,12 +79,12 @@ typedef struct
} bmp180_constants_t; } bmp180_constants_t;
// Returns true if the bmp180 is detected. // Returns true if the bmp180 is detected.
bool bmp180_is_available(); bool bmp180_is_available(i2c_dev_t *dev);
// Reads all the internal constants, returning true on success. // Reads all the internal constants, returning true on success.
bool bmp180_fillInternalConstants(bmp180_constants_t *c); bool bmp180_fillInternalConstants(i2c_dev_t *dev, bmp180_constants_t *c);
// Reads an optional temperature and pressure. The over sampling // Reads an optional temperature and pressure. The over sampling
// setting, oss, may be 0 to 3. Returns true on success. // setting, oss, may be 0 to 3. Returns true on success.
bool bmp180_measure(bmp180_constants_t *c, int32_t *temperature, bool bmp180_measure(i2c_dev_t *dev, bmp180_constants_t *c, int32_t *temperature,
uint32_t *pressure, uint8_t oss); uint32_t *pressure, uint8_t oss);
#ifdef __cplusplus #ifdef __cplusplus

View file

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

View file

@ -26,6 +26,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" {
@ -127,7 +128,7 @@ typedef struct {
int16_t dig_H5; int16_t dig_H5;
int8_t dig_H6; int8_t dig_H6;
uint8_t i2c_addr; /* I2C address. */ i2c_dev_t i2c_dev; /* I2C dev setting. */
uint8_t id; /* Chip ID */ uint8_t id; /* Chip ID */
} bmp280_t; } bmp280_t;

View file

@ -6,10 +6,8 @@
* BSD Licensed as described in the file LICENSE * BSD Licensed as described in the file LICENSE
*/ */
#include "ds1307.h" #include "ds1307.h"
#include <i2c/i2c.h>
#include <stdio.h> #include <stdio.h>
#define ADDR 0x68
#define RAM_SIZE 56 #define RAM_SIZE 56
#define TIME_REG 0 #define TIME_REG 0
@ -40,36 +38,36 @@ static uint8_t dec2bcd(uint8_t val)
return ((val / 10) << 4) + (val % 10); 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; uint8_t val;
i2c_slave_read(ADDR, &reg, &val, 1); i2c_slave_read(dev->bus, dev->addr, &reg, &val, 1);
return val; 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 buf[7];
uint8_t reg = TIME_REG ; 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_sec = bcd2dec(buf[0] & SECONDS_MASK);
time->tm_min = bcd2dec(buf[1]); time->tm_min = bcd2dec(buf[1]);
@ -87,7 +85,7 @@ void ds1307_get_time(struct tm *time)
time->tm_year = bcd2dec(buf[6]) + 2000; 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]; uint8_t buf[8];
buf[0] = TIME_REG; buf[0] = TIME_REG;
@ -99,51 +97,51 @@ void ds1307_set_time(const struct tm *time)
buf[6] = dec2bcd(time->tm_mon + 1); buf[6] = dec2bcd(time->tm_mon + 1);
buf[7] = dec2bcd(time->tm_year - 2000); 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; if (offset + len > RAM_SIZE) return false;
uint8_t reg = RAM_REG + offset ; 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; if (offset + len > RAM_SIZE) return false;
uint8_t reg = RAM_REG + offset ; 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 <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" {
#endif #endif
#define DS1307_ADDR 0x68
/** /**
* Squarewave frequency * Squarewave frequency
*/ */
@ -31,62 +33,62 @@ typedef enum _ds1307_squarewave_freq_t
* \brief Start/stop clock * \brief Start/stop clock
* \param start Start clock if true * \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 * \brief Get current clock state
* \return true if clock running * \return true if clock running
*/ */
bool ds1307_is_running(); bool ds1307_is_running(i2c_dev_t* dev);
/** /**
* \brief Get current time * \brief Get current time
* \param time Pointer to the time struct to fill * \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 * \brief Set time to RTC
* \param time Pointer to the time struct * \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 * \brief Enable or disable square-wave oscillator output
* \param enable Enable oscillator if true * \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 * \brief Get square-wave oscillator output
* \return true if square-wave oscillator enabled * \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 * \brief Set square-wave oscillator frequency
* \param freq 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 * \brief Get current square-wave oscillator frequency
* \return 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 * \brief Get current output level of the SQW/OUT pin
* \return true if high * \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 * \brief Set output level of the SQW/OUT pin
* Set output level if square-wave output is disabled * Set output level if square-wave output is disabled
* \param value High level if true * \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 * \brief Read RAM contents into the buffer
@ -95,7 +97,7 @@ void ds1307_set_output(bool value);
* \param len Bytes to read, 1..56 * \param len Bytes to read, 1..56
* \return Non-zero if error occured * \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 * \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 * \param len Bytes to write, 1..56
* \return Non-zero if error occured * \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 #ifdef __cplusplus

View file

@ -11,7 +11,6 @@
#include "espressif/sdk_private.h" #include "espressif/sdk_private.h"
#include "esp8266.h" #include "esp8266.h"
#include "i2c/i2c.h"
/* 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)
@ -28,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(DS3231_ADDR, &reg, data, len); return i2c_slave_write(dev->bus, dev->addr, &reg, 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(DS3231_ADDR, &reg, data, len); return i2c_slave_read(dev->bus, dev->addr, &reg, 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];
@ -54,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];
@ -81,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
@ -91,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);
@ -112,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)
@ -127,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;
@ -147,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;
@ -225,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;
} }
@ -237,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;
} }
@ -249,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;
} }
@ -283,8 +282,3 @@ bool ds3231_getTime(struct tm *time)
return true; return true;
} }
void ds3231_Init(uint8_t scl, uint8_t sda)
{
i2c_init(scl, sda);
}

View file

@ -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 scl, uint8_t sda);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -60,8 +60,8 @@ static void write_nibble(const hd44780_t *lcd, uint8_t b, bool rs)
| (rs ? 1 << lcd->pins.rs : 0) | (rs ? 1 << lcd->pins.rs : 0)
| (lcd->backlight ? 1 << lcd->pins.bl : 0); | (lcd->backlight ? 1 << lcd->pins.bl : 0);
pcf8574_port_write(lcd->addr, data | (1 << lcd->pins.e)); pcf8574_port_write(&lcd->i2c_dev, data | (1 << lcd->pins.e));
pcf8574_port_write(lcd->addr, data); pcf8574_port_write(&lcd->i2c_dev, data);
#else #else
gpio_write(lcd->pins.d7, (b >> 3) & 1); gpio_write(lcd->pins.d7, (b >> 3) & 1);
gpio_write(lcd->pins.d6, (b >> 2) & 1); gpio_write(lcd->pins.d6, (b >> 2) & 1);
@ -164,7 +164,7 @@ void hd44780_set_backlight(hd44780_t *lcd, bool on)
return; return;
#if (HD44780_I2C) #if (HD44780_I2C)
pcf8574_gpio_write(lcd->addr, lcd->pins.bl, on); pcf8574_gpio_write(&lcd->i2c_dev, lcd->pins.bl, on);
#else #else
gpio_write(lcd->pins.bl, on); gpio_write(lcd->pins.bl, on);
#endif #endif

View file

@ -12,7 +12,10 @@
#include <stdbool.h> #include <stdbool.h>
#ifndef HD44780_I2C #ifndef HD44780_I2C
#define HD44780_I2C 0 #define HD44780_I2C 1
#endif
#if (HD44780_I2C)
#include <i2c/i2c.h>
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
@ -36,7 +39,9 @@ typedef enum
*/ */
typedef struct typedef struct
{ {
uint8_t addr; //!< PCF8574 address (0b0100<A2><A1><A0>) #if (HD44780_I2C)
i2c_dev_t i2c_dev; //!< PCF8574 device settings (0b0100<A2><A1><A0>)
#endif
struct struct
{ {
uint8_t rs; //!< gpio/register bit used for RS pin uint8_t rs; //!< gpio/register bit used for RS pin

View file

@ -6,10 +6,8 @@
* BSD Licensed as described in the file LICENSE * BSD Licensed as described in the file LICENSE
*/ */
#include "hmc5883l.h" #include "hmc5883l.h"
#include <i2c/i2c.h>
#include <espressif/esp_common.h> #include <espressif/esp_common.h>
#define ADDR 0x1e
#define REG_CR_A 0x00 #define REG_CR_A 0x00
#define REG_CR_B 0x01 #define REG_CR_B 0x01
@ -54,112 +52,112 @@ static const float gain_values [] = {
static float current_gain; static float current_gain;
static hmc5883l_operating_mode_t current_mode; 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; uint8_t res;
i2c_slave_read(ADDR, &reg, &res, 1); i2c_slave_read(dev->bus, dev->addr, &reg, &res, 1);
return res; 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; return false;
current_gain = gain_values[hmc5883l_get_gain()]; current_gain = gain_values[hmc5883l_get_gain(dev)];
current_mode = hmc5883l_get_operating_mode(); current_mode = hmc5883l_get_operating_mode(dev);
return true; return true;
} }
uint32_t hmc5883l_get_id() uint32_t hmc5883l_get_id(i2c_dev_t* dev)
{ {
uint32_t res = 0; uint32_t res = 0;
uint8_t reg = REG_ID_A; 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; 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; 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; 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]; 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) if (current_mode == HMC5883L_MODE_SINGLE)
{ {
// overwrite mode register for measurement // overwrite mode register for measurement
hmc5883l_set_operating_mode(current_mode); hmc5883l_set_operating_mode(dev, current_mode);
// wait for data // wait for data
uint32_t start = sdk_system_get_time(); uint32_t start = sdk_system_get_time();
while (!hmc5883l_data_is_ready()) while (!hmc5883l_data_is_ready(dev))
{ {
if (timeout_expired(start, MEASUREMENT_TIMEOUT)) if (timeout_expired(start, MEASUREMENT_TIMEOUT))
return false; return false;
@ -167,7 +165,7 @@ bool hmc5883l_get_raw_data(hmc5883l_raw_data_t *data)
} }
uint8_t buf[6]; uint8_t buf[6];
uint8_t reg = REG_DX_H; 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->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->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]; 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; 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; hmc5883l_raw_data_t raw;
if (!hmc5883l_get_raw_data(&raw)) if (!hmc5883l_get_raw_data(dev, &raw))
return false; return false;
hmc5883l_raw_to_mg(&raw, data); hmc5883l_raw_to_mg(&raw, data);
return true; return true;

View file

@ -10,12 +10,15 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <i2c/i2c.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
#endif #endif
#define HMC5883L_ADDR 0x1e
#define HMC5883L_ID 0x00333448 // "H43" #define HMC5883L_ID 0x00333448 // "H43"
/** /**
@ -101,82 +104,82 @@ typedef struct
* \brief Init device * \brief Init device
* \return false if error occured * \return false if error occured
*/ */
bool hmc5883l_init(); bool hmc5883l_init(i2c_dev_t* dev);
/** /**
* \brief Get device ID * \brief Get device ID
* Always returns 0x00333448 if IC functioning properly. * Always returns 0x00333448 if IC functioning properly.
* \return Device ID * \return Device ID
*/ */
uint32_t hmc5883l_get_id(); uint32_t hmc5883l_get_id(i2c_dev_t* dev);
/** /**
* \brief Get operating mode * \brief Get operating mode
* \return Measurement 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 * \brief Set operating mode
* \param mode Measurement 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 * \brief Get number of samples averaged per measurement output
* \return Number of samples * \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 * \brief Set number of samples averaged per measurement output
* \param samples Number of samples * \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 * \brief Get data output rate in continuous measurement mode
* \return Data output rate * \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 * \brief Set data output rate in continuous measurement mode
* \param rate Data output rate * \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) * \brief Get measurement mode (bias of the axes)
* See datasheet for self test description * See datasheet for self test description
* \return Bias * \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) * \brief Set measurement mode (bias of the axes)
* See datasheet for self test description * See datasheet for self test description
* \param bias Bias * \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 * \brief Get device gain
* \return Current gain * \return Current gain
*/ */
hmc5883l_gain_t hmc5883l_get_gain(); hmc5883l_gain_t hmc5883l_get_gain(i2c_dev_t* dev);
/** /**
* \brief Set device gain * \brief Set device gain
* \param gain 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 * \brief Get data state
* \return true when data is written to all six data registers * \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. * \brief Get lock state.
@ -188,14 +191,14 @@ bool hmc5883l_data_is_ready();
* 4. power is reset. * 4. power is reset.
* \return true when data registers is locked * \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 * \brief Get raw magnetic data
* \param data Pointer to the struct to write raw 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 * \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 * \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 * \param data Pointer to the struct to write data
* \return false if error occured in single measurement mode, always true in continuous mode * \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 #ifdef __cplusplus
} }

View file

@ -37,217 +37,219 @@
#define CLK_STRETCH (10) #define CLK_STRETCH (10)
static bool started; static uint8_t freq ; // Store CPU frequency for optimisation speed in delay function ( Warning: Don't change CPU frequency during a transaction)
static bool flag; static i2c_bus_description_t i2c_bus[MAX_I2C_BUS];
static bool force;
static uint8_t freq ;
static uint8_t g_scl_pin;
static uint8_t g_sda_pin;
inline bool i2c_status(void) inline bool i2c_status(uint8_t bus)
{ {
return started; return i2c_bus[bus].started;
} }
void i2c_init(uint8_t scl_pin, uint8_t sda_pin) void i2c_init(uint8_t bus, uint8_t scl_pin, uint8_t sda_pin, i2c_freq_t freq)
{ {
started = false; i2c_bus[bus].started = false;
flag = false ; i2c_bus[bus].flag = false ;
g_scl_pin = scl_pin; i2c_bus[bus].g_scl_pin = scl_pin;
g_sda_pin = sda_pin; i2c_bus[bus].g_sda_pin = sda_pin;
i2c_bus[bus].frequency = freq ;
// Just to prevent these pins floating too much if not connected. // Just to prevent these pins floating too much if not connected.
gpio_set_pullup(g_scl_pin, 1, 1); gpio_set_pullup(i2c_bus[bus].g_scl_pin, 1, 1);
gpio_set_pullup(g_sda_pin, 1, 1); gpio_set_pullup(i2c_bus[bus].g_sda_pin, 1, 1);
gpio_enable(g_scl_pin, GPIO_OUT_OPEN_DRAIN); gpio_enable(i2c_bus[bus].g_scl_pin, GPIO_OUT_OPEN_DRAIN);
gpio_enable(g_sda_pin, GPIO_OUT_OPEN_DRAIN); gpio_enable(i2c_bus[bus].g_sda_pin, GPIO_OUT_OPEN_DRAIN);
// I2C bus idle state. // I2C bus idle state.
gpio_write(g_scl_pin, 1); gpio_write(i2c_bus[bus].g_scl_pin, 1);
gpio_write(g_sda_pin, 1); gpio_write(i2c_bus[bus].g_sda_pin, 1);
// 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");
} }
static inline void i2c_delay(void) void i2c_frequency(uint8_t bus, i2c_freq_t freq)
{
i2c_bus[bus].frequency = freq ;
}
static inline void i2c_delay(uint8_t bus)
{ {
uint32_t delay; uint32_t delay;
if (freq == SYS_CPU_160MHZ) if (freq == SYS_CPU_160MHZ)
{ {
delay = i2c_freq_array[i2c_bus[bus].frequency][0];
__asm volatile ( __asm volatile (
"movi %0, %1" "\n"
"1: addi %0, %0, -1" "\n" "1: addi %0, %0, -1" "\n"
"bnez %0, 1b" "\n" "bnez %0, 1b" "\n"
: "=a" (delay) : "i" (I2C_CUSTOM_DELAY_160MHZ)); :: "a" (delay));
} }
else else
{ {
delay = i2c_freq_array[i2c_bus[bus].frequency][1];
__asm volatile ( __asm volatile (
"movi %0, %1" "\n"
"1: addi %0, %0, -1" "\n" "1: addi %0, %0, -1" "\n"
"bnez %0, 1b" "\n" "bnez %0, 1b" "\n"
: "=a" (delay) : "i" (I2C_CUSTOM_DELAY_80MHZ)); :: "a" (delay));
} }
} }
// Set SCL as input, allowing it to float high, and return current // Set SCL as input, allowing it to float high, and return current
// level of line, 0 or 1 // level of line, 0 or 1
static inline bool read_scl(void) static inline bool read_scl(uint8_t bus)
{ {
gpio_write(g_scl_pin, 1); gpio_write(i2c_bus[bus].g_scl_pin, 1);
return gpio_read(g_scl_pin); // Clock high, valid ACK return gpio_read(i2c_bus[bus].g_scl_pin); // Clock high, valid ACK
} }
// Set SDA as input, allowing it to float high, and return current // Set SDA as input, allowing it to float high, and return current
// level of line, 0 or 1 // level of line, 0 or 1
static inline bool read_sda(void) static inline bool read_sda(uint8_t bus)
{ {
gpio_write(g_sda_pin, 1); gpio_write(i2c_bus[bus].g_sda_pin, 1);
// TODO: Without this delay we get arbitration lost in i2c_stop // TODO: Without this delay we get arbitration lost in i2c_stop
i2c_delay(); i2c_delay(bus);
return gpio_read(g_sda_pin); // Clock high, valid ACK return gpio_read(i2c_bus[bus].g_sda_pin); // Clock high, valid ACK
} }
// Actively drive SCL signal low // Actively drive SCL signal low
static inline void clear_scl(void) static inline void clear_scl(uint8_t bus)
{ {
gpio_write(g_scl_pin, 0); gpio_write(i2c_bus[bus].g_scl_pin, 0);
} }
// Actively drive SDA signal low // Actively drive SDA signal low
static inline void clear_sda(void) static inline void clear_sda(uint8_t bus)
{ {
gpio_write(g_sda_pin, 0); gpio_write(i2c_bus[bus].g_sda_pin, 0);
} }
// Output start condition // Output start condition
void i2c_start(void) 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 (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(); (void) read_sda(bus);
i2c_delay(); i2c_delay(bus);
while (read_scl() == 0 && clk_stretch--) ; uint32_t clk_stretch = 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(); i2c_delay(bus);
} }
started = true; i2c_bus[bus].started = true;
if (read_sda() == 0) { if (read_sda(bus) == 0) {
debug("arbitration lost in i2c_start"); debug("arbitration lost in i2c_start from bus %u",bus);
} }
// SCL is high, set SDA from 1 to 0. // SCL is high, set SDA from 1 to 0.
clear_sda(); clear_sda(bus);
i2c_delay(); i2c_delay(bus);
clear_scl(); clear_scl(bus);
} }
// Output stop condition // Output stop condition
bool i2c_stop(void) bool i2c_stop(uint8_t bus)
{ {
uint32_t clk_stretch = CLK_STRETCH; uint32_t clk_stretch = CLK_STRETCH;
// Set SDA to 0 // Set SDA to 0
clear_sda(); clear_sda(bus);
i2c_delay(); i2c_delay(bus);
// Clock stretching // Clock stretching
while (read_scl() == 0 && clk_stretch--) ; while (read_scl(bus) == 0 && clk_stretch--) ;
// Stop bit setup time, minimum 4us // Stop bit setup time, minimum 4us
i2c_delay(); i2c_delay(bus);
// SCL is high, set SDA from 0 to 1 // SCL is high, set SDA from 0 to 1
if (read_sda() == 0) { if (read_sda(bus) == 0) {
debug("arbitration lost in i2c_stop"); debug("arbitration lost in i2c_stop from bus %u",bus);
} }
i2c_delay(); i2c_delay(bus);
if (!started) { if (!i2c_bus[bus].started) {
debug("link was break!"); debug("bus %u link was break!",bus);
return false ; //If bus was stop in other way, the current transmission Failed return false ; //If bus was stop in other way, the current transmission Failed
} }
started = false; i2c_bus[bus].started = false;
return true; return true;
} }
// Write a bit to I2C bus // Write a bit to I2C bus
static void i2c_write_bit(bool bit) static void i2c_write_bit(uint8_t bus, bool bit)
{ {
uint32_t clk_stretch = CLK_STRETCH; uint32_t clk_stretch = CLK_STRETCH;
if (bit) { if (bit) {
(void) read_sda(); (void) read_sda(bus);
} else { } else {
clear_sda(); clear_sda(bus);
} }
i2c_delay(); i2c_delay(bus);
// Clock stretching // Clock stretching
while (read_scl() == 0 && clk_stretch--) ; while (read_scl(bus) == 0 && clk_stretch--) ;
// SCL is high, now data is valid // SCL is high, now data is valid
// If SDA is high, check that nobody else is driving SDA // If SDA is high, check that nobody else is driving SDA
if (bit && read_sda() == 0) { if (bit && read_sda(bus) == 0) {
debug("arbitration lost in i2c_write_bit"); debug("arbitration lost in i2c_write_bit from bus %u",bus);
} }
i2c_delay(); i2c_delay(bus);
clear_scl(); clear_scl(bus);
} }
// Read a bit from I2C bus // Read a bit from I2C bus
static bool i2c_read_bit(void) static bool i2c_read_bit(uint8_t bus)
{ {
uint32_t clk_stretch = CLK_STRETCH; uint32_t clk_stretch = CLK_STRETCH;
bool bit; bool bit;
// Let the slave drive data // Let the slave drive data
(void) read_sda(); (void) read_sda(bus);
i2c_delay(); i2c_delay(bus);
// Clock stretching // Clock stretching
while (read_scl() == 0 && clk_stretch--) ; while (read_scl(bus) == 0 && clk_stretch--) ;
// SCL is high, now data is valid // SCL is high, now data is valid
bit = read_sda(); bit = read_sda(bus);
i2c_delay(); i2c_delay(bus);
clear_scl(); clear_scl(bus);
return bit; return bit;
} }
bool i2c_write(uint8_t byte) bool i2c_write(uint8_t bus, uint8_t byte)
{ {
bool nack; bool nack;
uint8_t bit; uint8_t bit;
for (bit = 0; bit < 8; bit++) { for (bit = 0; bit < 8; bit++) {
i2c_write_bit((byte & 0x80) != 0); i2c_write_bit(bus,(byte & 0x80) != 0);
byte <<= 1; byte <<= 1;
} }
nack = i2c_read_bit(); nack = i2c_read_bit(bus);
return !nack; return !nack;
} }
uint8_t i2c_read(bool ack) 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(); byte = ((byte << 1)) | (i2c_read_bit(bus));
} }
i2c_write_bit(ack); i2c_write_bit(bus,ack);
return byte; return byte;
} }
void i2c_force_bus(bool state) void i2c_force_bus(uint8_t bus, bool state)
{ {
force = state ; i2c_bus[bus].force = state ;
} }
static int i2c_bus_test() static int i2c_bus_test(uint8_t bus)
{ {
taskENTER_CRITICAL(); // To prevent task swaping after checking flag and before set it! taskENTER_CRITICAL(); // To prevent task swaping after checking flag and before set it!
bool status = flag ; // get current status bool status = i2c_bus[bus].flag ; // get current status
if(force) if(i2c_bus[bus].force)
{ {
flag = true ; // force bus on i2c_bus[bus].flag = true ; // force bus on
taskEXIT_CRITICAL(); taskEXIT_CRITICAL();
if(status) if(status)
i2c_stop(); //Bus was busy, stop it. i2c_stop(bus); //Bus was busy, stop it.
} }
else else
{ {
@ -260,68 +262,68 @@ static int i2c_bus_test()
} }
else else
{ {
flag = true ; // Set Bus busy i2c_bus[bus].flag = true ; // Set Bus busy
taskEXIT_CRITICAL(); taskEXIT_CRITICAL();
} }
} }
return 0 ; return 0 ;
} }
int i2c_slave_write(uint8_t slave_addr, const uint8_t *data, const uint8_t *buf, uint32_t len) int i2c_slave_write(uint8_t bus, uint8_t slave_addr, const uint8_t *data, const uint8_t *buf, uint32_t len)
{ {
if(i2c_bus_test()) if(i2c_bus_test(bus))
return -EBUSY ; return -EBUSY ;
i2c_start(); i2c_start(bus);
if (!i2c_write(slave_addr << 1)) if (!i2c_write(bus, slave_addr << 1))
goto error; goto error;
if(data != NULL) if(data != NULL)
if (!i2c_write(*data)) if (!i2c_write(bus,*data))
goto error; goto error;
while (len--) { while (len--) {
if (!i2c_write(*buf++)) if (!i2c_write(bus,*buf++))
goto error; goto error;
} }
if (!i2c_stop()) if (!i2c_stop(bus))
goto error; goto error;
flag = false ; // Bus free i2c_bus[bus].flag = false ; // Bus free
return 0; return 0;
error: error:
debug("Write Error"); debug("Bus %u Write Error",bus);
i2c_stop(); i2c_stop(bus);
flag = false ; // Bus free i2c_bus[bus].flag = false ; // Bus free
return -EIO; return -EIO;
} }
int i2c_slave_read(uint8_t slave_addr, const uint8_t *data, uint8_t *buf, uint32_t len) int i2c_slave_read(uint8_t bus, uint8_t slave_addr, const uint8_t *data, uint8_t *buf, uint32_t len)
{ {
if(i2c_bus_test()) if(i2c_bus_test(bus))
return -EBUSY ; return -EBUSY ;
if(data != NULL) { if(data != NULL) {
i2c_start(); i2c_start(bus);
if (!i2c_write(slave_addr << 1)) if (!i2c_write(bus,slave_addr << 1))
goto error; goto error;
if (!i2c_write(*data)) if (!i2c_write(bus,*data))
goto error; goto error;
if (!i2c_stop()) if (!i2c_stop(bus))
goto error; goto error;
} }
i2c_start(); i2c_start(bus);
if (!i2c_write(slave_addr << 1 | 1)) // Slave address + read if (!i2c_write(bus,slave_addr << 1 | 1)) // Slave address + read
goto error; goto error;
while(len) { while(len) {
*buf = i2c_read(len == 1); *buf = i2c_read(bus,len == 1);
buf++; buf++;
len--; len--;
} }
if (!i2c_stop()) if (!i2c_stop(bus))
goto error; goto error;
flag = false ; // Bus free i2c_bus[bus].flag = false ; // Bus free
return 0; return 0;
error: error:
debug("Read Error"); debug("Read Error");
i2c_stop(); i2c_stop(bus);
flag = false ; // Bus free i2c_bus[bus].flag = false ; // Bus free
return -EIO; return -EIO;
} }

View file

@ -37,22 +37,47 @@ extern "C" {
/* /*
* Some bit can be transmit slower. * Define i2c bus max number
* Selected frequency fix the speed of a bit transmission
* I2C lib take the maximum frequency defined
* Don't change frequency when I2C transaction had begin
*/ */
#define MAX_I2C_BUS 2
/*
* following array contain value for different frequency
* Warning : 1 is minimal, that mean at 80MHz clock, frequency max is 320kHz
* Array format is { {160MHz, 80MHz} , {160MHz, 80MHz} , ... }
*/
#define NB_FREQ_AVAILABLE 4
typedef enum {
I2C_FREQ_80K = 0,
I2C_FREQ_100K,
I2C_FREQ_400K,
I2C_FREQ_500K,
} i2c_freq_t;
const static uint8_t i2c_freq_array[NB_FREQ_AVAILABLE][2] = { {255,35}, {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 {
uint8_t g_scl_pin; // Scl pin
uint8_t g_sda_pin; // Sda pin
uint8_t frequency; // frequency selection
bool started;
bool flag;
bool force;
} i2c_bus_description_t ;
#ifdef I2C_FREQUENCY_500K
#define I2C_CUSTOM_DELAY_160MHZ 6
#define I2C_CUSTOM_DELAY_80MHZ 1 //Sry, maximum is 320kHz at 80MHz
#elif defined(I2C_FREQUENCY_400K)
#define I2C_CUSTOM_DELAY_160MHZ 10
#define I2C_CUSTOM_DELAY_80MHZ 1 //Sry, maximum is 320kHz at 80MHz
#else
#define I2C_CUSTOM_DELAY_160MHZ 100
#define I2C_CUSTOM_DELAY_80MHZ 20
#endif
// I2C driver for ESP8266 written for use with esp-open-rtos // I2C driver for ESP8266 written for use with esp-open-rtos
// Based on https://en.wikipedia.org/wiki/I²C#Example_of_bit-banging_the_I.C2.B2C_Master_protocol // Based on https://en.wikipedia.org/wiki/I²C#Example_of_bit-banging_the_I.C2.B2C_Master_protocol
@ -62,71 +87,88 @@ extern "C" {
/** /**
* Init bitbanging I2C driver on given pins * Init bitbanging I2C driver on given pins
* @param bus Bus i2c selection
* @param scl_pin SCL pin for I2C * @param scl_pin SCL pin for I2C
* @param sda_pin SDA pin for I2C * @param sda_pin SDA pin for I2C
* @param freq frequency of bus (ex : I2C_FREQ_400K)
*/ */
void i2c_init(uint8_t scl_pin, uint8_t sda_pin); void i2c_init(uint8_t bus, uint8_t scl_pin, uint8_t sda_pin, i2c_freq_t freq);
/**
* Change bus frequency
* @param bus Bus i2c selection
* @param freq frequency of bus (ex : I2C_FREQ_400K)
*/
void i2c_frequency(uint8_t bus, i2c_freq_t freq);
/** /**
* Write a byte to I2C bus. * Write a byte to I2C bus.
* @param bus Bus i2c selection
* @param byte Pointer to device descriptor * @param byte Pointer to device descriptor
* @return true if slave acked * @return true if slave acked
*/ */
bool i2c_write(uint8_t byte); bool i2c_write(uint8_t bus, uint8_t byte);
/** /**
* Read a byte from I2C bus. * Read a byte from I2C bus.
* @param bus Bus i2c selection
* @param ack Set Ack for slave (false: Ack // true: NoAck) * @param ack Set Ack for slave (false: Ack // true: NoAck)
* @return byte read from slave. * @return byte read from slave.
*/ */
uint8_t i2c_read(bool ack); uint8_t i2c_read(uint8_t bus, bool ack);
/** /**
* Send start or restart condition * Send start or restart condition
* @param bus Bus i2c selection
*/ */
void i2c_start(void); void i2c_start(uint8_t bus);
/** /**
* Send stop condition * Send stop condition
* @param bus Bus i2c selection
* @return false if link was broken * @return false if link was broken
*/ */
bool i2c_stop(void); bool i2c_stop(uint8_t bus);
/** /**
* get status from I2C bus. * get status from I2C bus.
* @param bus Bus i2c selection
* @return true if busy. * @return true if busy.
*/ */
bool i2c_status(void); bool i2c_status(uint8_t bus);
//Level 1 API (Don't need functions above) //Level 1 API (Don't need functions above)
/** /**
* This function will allow you to force a transmission I2C, cancel current transmission. * This function will allow you to force a transmission I2C, cancel current transmission.
* Warning: Use with precaution. Don't use it if you can avoid it. Usefull for priority transmission. * Warning: Use with precaution. Don't use it if you can avoid it. Usefull for priority transmission.
* @param bus Bus i2c selection
* @param state Force the next I2C transmission if true (Use with precaution) * @param state Force the next I2C transmission if true (Use with precaution)
*/ */
void i2c_force_bus(bool state); void i2c_force_bus(uint8_t bus, bool state);
/** /**
* Write 'len' bytes from 'buf' to slave at 'data' register adress . * Write 'len' bytes from 'buf' to slave at 'data' register adress .
* @param bus Bus i2c selection
* @param slave_addr slave device address * @param slave_addr slave device address
* @param data Pointer to register address to send if non-null * @param data Pointer to register address to send if non-null
* @param buf Pointer to data buffer * @param buf Pointer to data buffer
* @param len Number of byte to send * @param len Number of byte to send
* @return Non-Zero if error occured * @return Non-Zero if error occured
*/ */
int i2c_slave_write(uint8_t slave_addr, const uint8_t *data, const uint8_t *buf, uint32_t len); int i2c_slave_write(uint8_t bus, uint8_t slave_addr, const uint8_t *data, const uint8_t *buf, uint32_t len);
/** /**
* Issue a send operation of 'data' register adress, followed by reading 'len' bytes * Issue a send operation of 'data' register adress, followed by reading 'len' bytes
* from slave into 'buf'. * from slave into 'buf'.
* @param bus Bus i2c selection
* @param slave_addr slave device address * @param slave_addr slave device address
* @param data Pointer to register address to send if non-null * @param data Pointer to register address to send if non-null
* @param buf Pointer to data buffer * @param buf Pointer to data buffer
* @param len Number of byte to read * @param len Number of byte to read
* @return Non-Zero if error occured * @return Non-Zero if error occured
*/ */
int i2c_slave_read(uint8_t slave_addr, const uint8_t *data, uint8_t *buf, uint32_t len); int i2c_slave_read(uint8_t bus, uint8_t slave_addr, const uint8_t *data, uint8_t *buf, uint32_t len);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -18,32 +18,32 @@
#define debug(fmt, ...) #define debug(fmt, ...)
#endif #endif
static int _wireWriteRegister (uint8_t addr, uint8_t reg, uint16_t value) static int _wireWriteRegister (const i2c_dev_t* dev, uint8_t reg, uint16_t value)
{ {
uint8_t d[2] = { 0 , 0 }; uint8_t d[2] = { 0 , 0 };
d[1] = value & 0x00FF; d[1] = value & 0x00FF;
d[0] = (value >> 8) & 0x00FF; d[0] = (value >> 8) & 0x00FF;
debug("Data write to %02X : %02X+%04X\n",addr,reg,value); debug("Data write to bus %u at %02X : %02X+%04X\n",dev->bus, dev->addr, reg, value);
return i2c_slave_write(addr, &reg, d, sizeof(d)); return i2c_slave_write(dev->bus, dev->addr, &reg, d, sizeof(d));
} }
static int _wireReadRegister(uint8_t addr, uint8_t reg, uint16_t *value) static int _wireReadRegister(const i2c_dev_t* dev, uint8_t reg, uint16_t *value)
{ {
uint8_t d[] = {0, 0}; uint8_t d[] = {0, 0};
int error = i2c_slave_read(addr, &reg, d, sizeof(d)) int error = i2c_slave_read(dev->bus, dev->addr, &reg, d, sizeof(d))
debug("Data read from %02X: %02X+%04X\n",addr,reg,*value); debug("Data read from bus %u at %02X: %02X+%04X\n",dev->bus, dev->addr, reg, *value);
*value = d[1] | (d[0] << 8); *value = d[1] | (d[0] << 8);
return error; return error;
} }
int ina3221_trigger(ina3221_t *dev) int ina3221_trigger(ina3221_t *dev)
{ {
return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register); return _wireWriteRegister(&dev->i2c_dev, INA3221_REG_CONFIG, dev->config.config_register);
} }
int ina3221_getStatus(ina3221_t *dev) int ina3221_getStatus(ina3221_t *dev)
{ {
return _wireReadRegister(dev->addr, INA3221_REG_MASK, &dev->mask.mask_register); return _wireReadRegister(&dev->i2c_dev, INA3221_REG_MASK, &dev->mask.mask_register);
} }
int ina3221_sync(ina3221_t *dev) int ina3221_sync(ina3221_t *dev)
@ -51,17 +51,17 @@ int ina3221_sync(ina3221_t *dev)
uint16_t ptr_data; uint16_t ptr_data;
int err = 0; int err = 0;
//////////////////////// Sync config register //////////////////////// Sync config register
if ((err = _wireReadRegister(dev->addr, INA3221_REG_CONFIG, &ptr_data))) // Read config if ((err = _wireReadRegister(&dev->i2c_dev, INA3221_REG_CONFIG, &ptr_data))) // Read config
return err; return err;
if( ptr_data != dev->config.config_register) { if( ptr_data != dev->config.config_register) {
if ((err = _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register))) // Update config if ((err = _wireWriteRegister(&dev->i2c_dev, INA3221_REG_CONFIG, dev->config.config_register))) // Update config
return err; return err;
} }
//////////////////////// Sync mask register config //////////////////////// Sync mask register config
if ((err = _wireReadRegister(dev->addr, INA3221_REG_MASK, &ptr_data))) // Read mask if ((err = _wireReadRegister(&dev->i2c_dev, INA3221_REG_MASK, &ptr_data))) // Read mask
return err; return err;
if( (ptr_data & INA3221_MASK_CONFIG) != (dev->mask.mask_register & INA3221_MASK_CONFIG)) { if( (ptr_data & INA3221_MASK_CONFIG) != (dev->mask.mask_register & INA3221_MASK_CONFIG)) {
if ((err = _wireWriteRegister(dev->addr, INA3221_REG_MASK, dev->mask.mask_register & INA3221_MASK_CONFIG))) // Update config if ((err = _wireWriteRegister(&dev->i2c_dev, INA3221_REG_MASK, dev->mask.mask_register & INA3221_MASK_CONFIG))) // Update config
return err; return err;
} }
return 0; return 0;
@ -72,7 +72,7 @@ int ina3221_setting(ina3221_t *dev ,bool mode, bool bus, bool shunt)
dev->config.mode = mode; dev->config.mode = mode;
dev->config.ebus = bus; dev->config.ebus = bus;
dev->config.esht = shunt; dev->config.esht = shunt;
return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register); return _wireWriteRegister(&dev->i2c_dev, INA3221_REG_CONFIG, dev->config.config_register);
} }
int ina3221_enableChannel(ina3221_t *dev ,bool ch1, bool ch2, bool ch3) int ina3221_enableChannel(ina3221_t *dev ,bool ch1, bool ch2, bool ch3)
@ -80,7 +80,7 @@ int ina3221_enableChannel(ina3221_t *dev ,bool ch1, bool ch2, bool ch3)
dev->config.ch1 = ch1; dev->config.ch1 = ch1;
dev->config.ch2 = ch2; dev->config.ch2 = ch2;
dev->config.ch3 = ch3; dev->config.ch3 = ch3;
return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register); return _wireWriteRegister(&dev->i2c_dev, INA3221_REG_CONFIG, dev->config.config_register);
} }
int ina3221_enableChannelSum(ina3221_t *dev ,bool ch1, bool ch2, bool ch3) int ina3221_enableChannelSum(ina3221_t *dev ,bool ch1, bool ch2, bool ch3)
@ -88,32 +88,32 @@ int ina3221_enableChannelSum(ina3221_t *dev ,bool ch1, bool ch2, bool ch3)
dev->mask.scc1 = ch1; dev->mask.scc1 = ch1;
dev->mask.scc2 = ch2; dev->mask.scc2 = ch2;
dev->mask.scc3 = ch3; dev->mask.scc3 = ch3;
return _wireWriteRegister(dev->addr, INA3221_REG_MASK, dev->mask.mask_register & INA3221_MASK_CONFIG); return _wireWriteRegister(&dev->i2c_dev, INA3221_REG_MASK, dev->mask.mask_register & INA3221_MASK_CONFIG);
} }
int ina3221_enableLatchPin(ina3221_t *dev ,bool warning, bool critical) int ina3221_enableLatchPin(ina3221_t *dev ,bool warning, bool critical)
{ {
dev->mask.wen = warning; dev->mask.wen = warning;
dev->mask.cen = critical; dev->mask.cen = critical;
return _wireWriteRegister(dev->addr, INA3221_REG_MASK, dev->mask.mask_register & INA3221_MASK_CONFIG); return _wireWriteRegister(&dev->i2c_dev, INA3221_REG_MASK, dev->mask.mask_register & INA3221_MASK_CONFIG);
} }
int ina3221_setAverage(ina3221_t *dev, ina3221_avg_t avg) int ina3221_setAverage(ina3221_t *dev, ina3221_avg_t avg)
{ {
dev->config.avg = avg; dev->config.avg = avg;
return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register); return _wireWriteRegister(&dev->i2c_dev, INA3221_REG_CONFIG, dev->config.config_register);
} }
int ina3221_setBusConversionTime(ina3221_t *dev,ina3221_ct_t ct) int ina3221_setBusConversionTime(ina3221_t *dev,ina3221_ct_t ct)
{ {
dev->config.vbus = ct; dev->config.vbus = ct;
return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register); return _wireWriteRegister(&dev->i2c_dev, INA3221_REG_CONFIG, dev->config.config_register);
} }
int ina3221_setShuntConversionTime(ina3221_t *dev,ina3221_ct_t ct) int ina3221_setShuntConversionTime(ina3221_t *dev,ina3221_ct_t ct)
{ {
dev->config.vsht = ct; dev->config.vsht = ct;
return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register); return _wireWriteRegister(&dev->i2c_dev, INA3221_REG_CONFIG, dev->config.config_register);
} }
int ina3221_reset(ina3221_t *dev) int ina3221_reset(ina3221_t *dev)
@ -121,14 +121,14 @@ int ina3221_reset(ina3221_t *dev)
dev->config.config_register = INA3221_DEFAULT_CONFIG ; //dev reset dev->config.config_register = INA3221_DEFAULT_CONFIG ; //dev reset
dev->mask.mask_register = INA3221_DEFAULT_CONFIG ; //dev reset dev->mask.mask_register = INA3221_DEFAULT_CONFIG ; //dev reset
dev->config.rst = 1 ; dev->config.rst = 1 ;
return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register); // send reset to device return _wireWriteRegister(&dev->i2c_dev, INA3221_REG_CONFIG, dev->config.config_register); // send reset to device
} }
int ina3221_getBusVoltage(ina3221_t *dev, ina3221_channel_t channel, float *voltage) int ina3221_getBusVoltage(ina3221_t *dev, ina3221_channel_t channel, float *voltage)
{ {
int16_t raw_value; int16_t raw_value;
int err = 0; int err = 0;
if ((err = _wireReadRegister(dev->addr,INA3221_REG_BUSVOLTAGE_1+channel*2, (uint16_t*)&raw_value))) if ((err = _wireReadRegister(&dev->i2c_dev,INA3221_REG_BUSVOLTAGE_1+channel*2, (uint16_t*)&raw_value)))
return err; return err;
*voltage = raw_value*0.001 ; //V 8mV step *voltage = raw_value*0.001 ; //V 8mV step
return 0; return 0;
@ -138,12 +138,12 @@ int ina3221_getShuntValue(ina3221_t *dev, ina3221_channel_t channel, float *volt
{ {
int16_t raw_value; int16_t raw_value;
int err = 0; int err = 0;
if ((err = _wireReadRegister(dev->addr,INA3221_REG_SHUNTVOLTAGE_1+channel*2, (uint16_t*)&raw_value))) if ((err = _wireReadRegister(&dev->i2c_dev,INA3221_REG_SHUNTVOLTAGE_1+channel*2, (uint16_t*)&raw_value)))
return err; return err;
*voltage = raw_value*0.005; //mV 40uV step *voltage = raw_value*0.005; //mV 40uV step
if(!dev->shunt[channel]) if(!dev->shunt[channel])
{ {
debug("No shunt configured for channel %u. Dev:%X\n",channel+1, dev->addr); debug("No shunt configured for channel %u. Dev:%u:%X\n",channel+1, dev->bus, dev->addr);
return -EINVAL; return -EINVAL;
} }
*current = (*voltage*1000.0)/dev->shunt[channel] ; //mA *current = (*voltage*1000.0)/dev->shunt[channel] ; //mA
@ -154,7 +154,7 @@ int ina3221_getSumShuntValue(ina3221_t *dev, float *voltage)
{ {
int16_t raw_value; int16_t raw_value;
int err = 0; int err = 0;
if ((err = _wireReadRegister(dev->addr,INA3221_REG_SHUNT_VOLTAGE_SUM, (uint16_t*)&raw_value))) if ((err = _wireReadRegister(&dev->i2c_dev,INA3221_REG_SHUNT_VOLTAGE_SUM, (uint16_t*)&raw_value)))
return err; return err;
*voltage = raw_value*0.02; //uV 40uV step *voltage = raw_value*0.02; //uV 40uV step
return 0; return 0;
@ -163,39 +163,39 @@ int ina3221_getSumShuntValue(ina3221_t *dev, float *voltage)
int ina3221_setCriticalAlert(ina3221_t *dev, ina3221_channel_t channel, float current) int ina3221_setCriticalAlert(ina3221_t *dev, ina3221_channel_t channel, float current)
{ {
int16_t raw_value = current*dev->shunt[channel]*0.2; // format int16_t raw_value = current*dev->shunt[channel]*0.2; // format
return _wireWriteRegister(dev->addr,INA3221_REG_CRITICAL_ALERT_1+channel*2, *(uint16_t*)&raw_value); return _wireWriteRegister(&dev->i2c_dev,INA3221_REG_CRITICAL_ALERT_1+channel*2, *(uint16_t*)&raw_value);
} }
int ina3221_setWarningAlert(ina3221_t *dev, ina3221_channel_t channel, float current) int ina3221_setWarningAlert(ina3221_t *dev, ina3221_channel_t channel, float current)
{ {
int16_t raw_value = current*dev->shunt[channel]*0.2 ; // format int16_t raw_value = current*dev->shunt[channel]*0.2 ; // format
return _wireWriteRegister(dev->addr,INA3221_REG_WARNING_ALERT_1+channel*2, *(uint16_t*)&raw_value); return _wireWriteRegister(&dev->i2c_dev,INA3221_REG_WARNING_ALERT_1+channel*2, *(uint16_t*)&raw_value);
} }
int ina3221_setSumWarningAlert(ina3221_t *dev, float voltage) int ina3221_setSumWarningAlert(ina3221_t *dev, float voltage)
{ {
int16_t raw_value = voltage*50.0 ; // format int16_t raw_value = voltage*50.0 ; // format
return _wireWriteRegister(dev->addr,INA3221_REG_SHUNT_VOLTAGE_SUM_LIMIT, *(uint16_t*)&raw_value); return _wireWriteRegister(&dev->i2c_dev,INA3221_REG_SHUNT_VOLTAGE_SUM_LIMIT, *(uint16_t*)&raw_value);
} }
int ina3221_setPowerValidUpperLimit(ina3221_t *dev, float voltage) int ina3221_setPowerValidUpperLimit(ina3221_t *dev, float voltage)
{ {
if(!dev->config.ebus) if(!dev->config.ebus)
{ {
debug("Bus not enable. Dev:%X\n", dev->addr); debug("Bus not enable. Dev:%u:%X\n", dev->bus, dev->addr);
return -ENOTSUP; return -ENOTSUP;
} }
int16_t raw_value = voltage*1000.0; //format int16_t raw_value = voltage*1000.0; //format
return _wireWriteRegister(dev->addr,INA3221_REG_VALID_POWER_UPPER_LIMIT, *(uint16_t*)&raw_value); return _wireWriteRegister(&dev->i2c_dev,INA3221_REG_VALID_POWER_UPPER_LIMIT, *(uint16_t*)&raw_value);
} }
int ina3221_setPowerValidLowerLimit(ina3221_t *dev, float voltage) int ina3221_setPowerValidLowerLimit(ina3221_t *dev, float voltage)
{ {
if(!dev->config.ebus) if(!dev->config.ebus)
{ {
debug("Bus not enable. Dev:%X\n", dev->addr); debug("Bus not enable. Dev:%u:%X\n", dev->bus, dev->addr);
return -ENOTSUP; return -ENOTSUP;
} }
int16_t raw_value = voltage*1000.0; // round and format int16_t raw_value = voltage*1000.0; // round and format
return _wireWriteRegister(dev->addr,INA3221_REG_VALID_POWER_LOWER_LIMIT, *(uint16_t*)&raw_value); return _wireWriteRegister(&dev->i2c_dev,INA3221_REG_VALID_POWER_LOWER_LIMIT, *(uint16_t*)&raw_value);
} }

View file

@ -132,7 +132,7 @@ typedef union
* Device description * Device description
*/ */
typedef struct { typedef struct {
const uint8_t addr; // ina3221 I2C address const i2c_dev_t i2c_dev; // ina3221 I2C address
const uint16_t shunt[BUS_NUMBER]; //Memory of shunt value (mOhm) const uint16_t shunt[BUS_NUMBER]; //Memory of shunt value (mOhm)
ina3221_config_t config; //Memory of ina3221 config ina3221_config_t config; //Memory of ina3221 config
ina3221_mask_t mask; //Memory of mask_config ina3221_mask_t mask; //Memory of mask_config

View file

@ -6,62 +6,59 @@
* BSD Licensed as described in the file LICENSE * BSD Licensed as described in the file LICENSE
*/ */
#include "mcp4725.h" #include "mcp4725.h"
#include <i2c/i2c.h>
#define CMD_DAC 0x40 #define CMD_DAC 0x40
#define CMD_EEPROM 0x60 #define CMD_EEPROM 0x60
#define BIT_READY 0x80 #define BIT_READY 0x80
static void read_data(uint8_t addr, uint8_t *buf, uint8_t size) static void read_data(i2c_dev_t* dev, uint8_t *buf, uint8_t size)
{ {
i2c_slave_read(addr, NULL, buf, size); i2c_slave_read(dev->bus, dev->addr , NULL, buf, size);
} }
bool mcp4725_eeprom_busy(uint8_t addr) bool mcp4725_eeprom_busy(i2c_dev_t* dev)
{ {
uint8_t res; uint8_t res;
read_data(addr, &res, 1); read_data(dev, &res, 1);
return !(res & BIT_READY); return !(res & BIT_READY);
} }
mcp4725_power_mode_t mcp4725_get_power_mode(uint8_t addr, bool eeprom) mcp4725_power_mode_t mcp4725_get_power_mode(i2c_dev_t* dev, bool eeprom)
{ {
uint8_t buf[4]; uint8_t buf[4];
read_data(addr, buf, eeprom ? 4 : 1); read_data(dev, buf, eeprom ? 4 : 1);
return (eeprom ? buf[3] >> 5 : buf[0] >> 1) & 0x03; return (eeprom ? buf[3] >> 5 : buf[0] >> 1) & 0x03;
} }
void mcp4725_set_power_mode(uint8_t addr, mcp4725_power_mode_t mode, bool eeprom) void mcp4725_set_power_mode(i2c_dev_t* dev, mcp4725_power_mode_t mode, bool eeprom)
{ {
uint16_t value = mcp4725_get_raw_output(addr, eeprom); uint16_t value = mcp4725_get_raw_output(dev, eeprom);
uint8_t data[] = { uint8_t data[] = {
(eeprom ? CMD_EEPROM : CMD_DAC) | ((uint8_t)mode << 1), (eeprom ? CMD_EEPROM : CMD_DAC) | ((uint8_t)mode << 1),
value >> 4, value >> 4,
value << 4 value << 4
}; };
i2c_slave_write(addr, &data[0], &data[1], 2); i2c_slave_write(dev->bus, dev->addr, &data[0], &data[1], 2);
} }
uint16_t mcp4725_get_raw_output(uint8_t addr, bool eeprom) uint16_t mcp4725_get_raw_output(i2c_dev_t* dev, bool eeprom)
{ {
uint8_t buf[5]; uint8_t buf[5];
read_data(addr, buf, eeprom ? 5 : 3); read_data(dev, buf, eeprom ? 5 : 3);
return eeprom return eeprom
? ((uint16_t)(buf[3] & 0x0f) << 8) | buf[4] ? ((uint16_t)(buf[3] & 0x0f) << 8) | buf[4]
: ((uint16_t)buf[0] << 4) | (buf[1] >> 4); : ((uint16_t)buf[0] << 4) | (buf[1] >> 4);
} }
void mcp4725_set_raw_output(uint8_t addr, uint16_t value, bool eeprom) void mcp4725_set_raw_output(i2c_dev_t* dev, uint16_t value, bool eeprom)
{ {
uint8_t data[] = { uint8_t data[] = {
(eeprom ? CMD_EEPROM : CMD_DAC), (eeprom ? CMD_EEPROM : CMD_DAC),
value >> 4, value >> 4,
value << 4 value << 4
}; };
i2c_slave_write(addr, &data[0], &data[1], 2); i2c_slave_write(dev->bus, dev->addr, &data[0], &data[1], 2);
} }

View file

@ -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"
@ -41,7 +42,7 @@ typedef enum
* @param addr Device address * @param addr Device address
* @return true when EEPROM is busy * @return true when EEPROM is busy
*/ */
bool mcp4725_eeprom_busy(uint8_t addr); bool mcp4725_eeprom_busy(i2c_dev_t* dev);
/** /**
* Get power mode * Get power mode
@ -49,7 +50,7 @@ bool mcp4725_eeprom_busy(uint8_t addr);
* @param eeprom Read power mode from EEPROM if true * @param eeprom Read power mode from EEPROM if true
* @return Power mode * @return Power mode
*/ */
mcp4725_power_mode_t mcp4725_get_power_mode(uint8_t addr, bool eeprom); mcp4725_power_mode_t mcp4725_get_power_mode(i2c_dev_t* dev, bool eeprom);
/** /**
* Set power mode * Set power mode
@ -57,7 +58,7 @@ mcp4725_power_mode_t mcp4725_get_power_mode(uint8_t addr, bool eeprom);
* @param mode Power mode * @param mode Power mode
* @param eeprom Store mode to device EEPROM if true * @param eeprom Store mode to device EEPROM if true
*/ */
void mcp4725_set_power_mode(uint8_t addr, mcp4725_power_mode_t mode, bool eeprom); void mcp4725_set_power_mode(i2c_dev_t* dev, mcp4725_power_mode_t mode, bool eeprom);
/** /**
* Get current DAC value * Get current DAC value
@ -65,7 +66,7 @@ void mcp4725_set_power_mode(uint8_t addr, mcp4725_power_mode_t mode, bool eeprom
* @param eeprom Read value from device EEPROM if true * @param eeprom Read value from device EEPROM if true
* @return Raw output value, 0..4095 * @return Raw output value, 0..4095
*/ */
uint16_t mcp4725_get_raw_output(uint8_t addr, bool eeprom); uint16_t mcp4725_get_raw_output(i2c_dev_t* dev, bool eeprom);
/** /**
* Set DAC output value * Set DAC output value
@ -73,7 +74,7 @@ uint16_t mcp4725_get_raw_output(uint8_t addr, bool eeprom);
* @param value Raw output value, 0..4095 * @param value Raw output value, 0..4095
* @param eeprom Store value to device EEPROM if true * @param eeprom Store value to device EEPROM if true
*/ */
void mcp4725_set_raw_output(uint8_t addr, uint16_t value, bool eeprom); void mcp4725_set_raw_output(i2c_dev_t* dev, uint16_t value, bool eeprom);
/** /**
* Get current DAC output voltage * Get current DAC output voltage
@ -82,9 +83,9 @@ void mcp4725_set_raw_output(uint8_t addr, uint16_t value, bool eeprom);
* @param eeprom Read voltage from device EEPROM if true * @param eeprom Read voltage from device EEPROM if true
* @return Current output voltage, volts * @return Current output voltage, volts
*/ */
inline float mcp4725_get_voltage(uint8_t addr, float vdd, bool eeprom) inline float mcp4725_get_voltage(i2c_dev_t* dev, float vdd, bool eeprom)
{ {
return vdd / MCP4725_MAX_VALUE * mcp4725_get_raw_output(addr, eeprom); return vdd / MCP4725_MAX_VALUE * mcp4725_get_raw_output(dev, eeprom);
} }
/** /**
@ -94,9 +95,9 @@ inline float mcp4725_get_voltage(uint8_t addr, float vdd, bool eeprom)
* @param value Output value, volts * @param value Output value, volts
* @param eeprom Store value to device EEPROM if true * @param eeprom Store value to device EEPROM if true
*/ */
inline void mcp4725_set_voltage(uint8_t addr, float vdd, float value, bool eeprom) inline void mcp4725_set_voltage(i2c_dev_t* dev, float vdd, float value, bool eeprom)
{ {
mcp4725_set_raw_output(addr, MCP4725_MAX_VALUE / vdd * value, eeprom); mcp4725_set_raw_output(dev, MCP4725_MAX_VALUE / vdd * value, eeprom);
} }
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -8,7 +8,6 @@
* BSD Licensed as described in the file LICENSE * BSD Licensed as described in the file LICENSE
*/ */
#include "ms561101ba03.h" #include "ms561101ba03.h"
#include <i2c/i2c.h>
#include <espressif/esp_common.h> #include <espressif/esp_common.h>
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "task.h" #include "task.h"
@ -26,10 +25,10 @@
*/ */
#define CONVERSION_TIME 20 / portTICK_PERIOD_MS // milliseconds #define CONVERSION_TIME 20 / portTICK_PERIOD_MS // milliseconds
static inline int reset(uint8_t addr) static inline int reset(i2c_dev_t* i2c_dev)
{ {
uint8_t buf[1] = { RESET }; uint8_t buf[1] = { RESET };
return i2c_slave_write(addr, NULL, buf, 1); return i2c_slave_write(i2c_dev->bus, i2c_dev->addr, NULL, buf, 1);
} }
static inline bool read_prom(ms561101ba03_t *dev) static inline bool read_prom(ms561101ba03_t *dev)
@ -37,32 +36,32 @@ static inline bool read_prom(ms561101ba03_t *dev)
uint8_t tmp[2] = { 0, 0 }; uint8_t tmp[2] = { 0, 0 };
uint8_t reg = 0xA2 ; uint8_t reg = 0xA2 ;
if (i2c_slave_read(dev->addr, &reg, tmp, 2)) if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, &reg, tmp, 2))
return false; return false;
dev->config_data.sens = tmp[0] << 8 | tmp[1]; dev->config_data.sens = tmp[0] << 8 | tmp[1];
reg = 0xA4 ; reg = 0xA4 ;
if (i2c_slave_read(dev->addr, &reg, tmp, 2)) if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, &reg, tmp, 2))
return false; return false;
dev->config_data.off = tmp[0] << 8 | tmp[1]; dev->config_data.off = tmp[0] << 8 | tmp[1];
reg = 0xA6 ; reg = 0xA6 ;
if (i2c_slave_read(dev->addr, &reg, tmp, 2)) if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, &reg, tmp, 2))
return false; return false;
dev->config_data.tcs = tmp[0] << 8 | tmp[1]; dev->config_data.tcs = tmp[0] << 8 | tmp[1];
reg = 0xA8 ; reg = 0xA8 ;
if (i2c_slave_read(dev->addr, &reg, tmp, 2)) if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, &reg, tmp, 2))
return false; return false;
dev->config_data.tco = tmp[0] << 8 | tmp[1]; dev->config_data.tco = tmp[0] << 8 | tmp[1];
reg = 0xAA ; reg = 0xAA ;
if (i2c_slave_read(dev->addr, &reg, tmp, 2)) if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, &reg, tmp, 2))
return false; return false;
dev->config_data.t_ref = tmp[0] << 8 | tmp[1]; dev->config_data.t_ref = tmp[0] << 8 | tmp[1];
reg = 0xAC ; reg = 0xAC ;
if (i2c_slave_read(dev->addr, &reg, tmp, 2)) if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, &reg, tmp, 2))
return false; return false;
dev->config_data.tempsens = tmp[0] << 8 | tmp[1]; dev->config_data.tempsens = tmp[0] << 8 | tmp[1];
@ -72,21 +71,21 @@ static inline bool read_prom(ms561101ba03_t *dev)
static inline int start_pressure_conversion(ms561101ba03_t *dev) //D1 static inline int start_pressure_conversion(ms561101ba03_t *dev) //D1
{ {
uint8_t buf = CONVERT_D1 + dev->osr; uint8_t buf = CONVERT_D1 + dev->osr;
return i2c_slave_write(dev->addr, NULL, &buf, 1); return i2c_slave_write(dev->i2c_dev.bus, dev->i2c_dev.addr, NULL, &buf, 1);
} }
static inline int start_temperature_conversion(ms561101ba03_t *dev) //D2 static inline int start_temperature_conversion(ms561101ba03_t *dev) //D2
{ {
uint8_t buf = CONVERT_D2 + dev->osr; uint8_t buf = CONVERT_D2 + dev->osr;
return i2c_slave_write(dev->addr, NULL, &buf, 1); return i2c_slave_write(dev->i2c_dev.bus, dev->i2c_dev.addr, NULL, &buf, 1);
} }
static inline bool read_adc(uint8_t addr, uint32_t *result) static inline bool read_adc(i2c_dev_t* i2c_dev, uint32_t *result)
{ {
*result = 0; *result = 0;
uint8_t tmp[3]; uint8_t tmp[3];
uint8_t reg = 0x00 ; uint8_t reg = 0x00 ;
if (i2c_slave_read(addr, &reg, tmp, 3)) if (i2c_slave_read(i2c_dev->bus, i2c_dev->addr, &reg, tmp, 3))
return false; return false;
*result = (tmp[0] << 16) | (tmp[1] << 8) | tmp[2]; *result = (tmp[0] << 16) | (tmp[1] << 8) | tmp[2];
@ -144,7 +143,7 @@ static inline bool get_raw_temperature(ms561101ba03_t *dev, uint32_t *result)
vTaskDelay(CONVERSION_TIME); vTaskDelay(CONVERSION_TIME);
if (!read_adc(dev->addr, result)) if (!read_adc(&dev->i2c_dev, result))
return false; return false;
return true; return true;
@ -157,7 +156,7 @@ static inline bool get_raw_pressure(ms561101ba03_t *dev, uint32_t *result)
vTaskDelay(CONVERSION_TIME); vTaskDelay(CONVERSION_TIME);
if (!read_adc(dev->addr, result)) if (!read_adc(&dev->i2c_dev, result))
return false; return false;
return true; return true;
@ -216,7 +215,7 @@ bool ms561101ba03_get_sensor_data(ms561101ba03_t *dev)
bool ms561101ba03_init(ms561101ba03_t *dev) bool ms561101ba03_init(ms561101ba03_t *dev)
{ {
// First of all we need to reset the chip // First of all we need to reset the chip
if (reset(dev->addr)) if (reset(&dev->i2c_dev))
return false; return false;
// Wait a bit for the device to reset // Wait a bit for the device to reset
vTaskDelay(CONVERSION_TIME); vTaskDelay(CONVERSION_TIME);

View file

@ -12,6 +12,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"
@ -60,7 +61,7 @@ typedef struct
*/ */
typedef struct typedef struct
{ {
uint8_t addr; //!< I2C address i2c_dev_t i2c_dev; //!< I2C device settings
ms561101ba03_osr_t osr; //!< Oversampling setting ms561101ba03_osr_t osr; //!< Oversampling setting
ms561101ba03_config_data_t config_data; //!< Device configuration, filled upon initalize ms561101ba03_config_data_t config_data; //!< Device configuration, filled upon initalize
ms561101ba03_result_t result; //!< Result, filled upon co ms561101ba03_result_t result; //!< Result, filled upon co

View file

@ -7,7 +7,6 @@
*/ */
#include "pca9685.h" #include "pca9685.h"
#include <i2c/i2c.h>
#include <espressif/esp_common.h> #include <espressif/esp_common.h>
#define REG_MODE1 0x00 #define REG_MODE1 0x00
@ -59,32 +58,32 @@ inline static uint32_t round_div(uint32_t x, uint32_t y)
return (x + y / 2) / y; return (x + y / 2) / y;
} }
inline static void write_reg(uint8_t addr, uint8_t reg, uint8_t val) inline static void write_reg(i2c_dev_t* dev, uint8_t reg, uint8_t val)
{ {
if (i2c_slave_write(addr, &reg, &val, 1)) if (i2c_slave_write(dev->bus, dev->addr, &reg, &val, 1))
debug("Could not write 0x%02x to 0x%02x, addr = 0x%02x", reg, val, addr); debug("Could not write 0x%02x to 0x%02x, bus %u, addr = 0x%02x", reg, val, dev->bus, dev->addr);
} }
inline static uint8_t read_reg(uint8_t addr, uint8_t reg) inline static uint8_t read_reg(i2c_dev_t* dev, uint8_t reg)
{ {
uint8_t res = 0; uint8_t res = 0;
if (i2c_slave_read(addr, &reg, &res, 1)) if (i2c_slave_read(dev->bus, dev->addr, &reg, &res, 1))
debug("Could not read from 0x%02x, addr = 0x%02x", reg, addr); debug("Could not read from 0x%02x, bus %u, addr = 0x%02x", reg, dev->bus, dev->addr);
return res; return res;
} }
inline static void update_reg(uint8_t addr, uint8_t reg, uint8_t mask, uint8_t val) inline static void update_reg(i2c_dev_t* dev, uint8_t reg, uint8_t mask, uint8_t val)
{ {
write_reg(addr, reg, (read_reg(addr, reg) & ~mask) | val); write_reg(dev, reg, (read_reg(dev, reg) & ~mask) | val);
} }
void pca9685_init(uint8_t addr) void pca9685_init(i2c_dev_t* dev)
{ {
// Enable autoincrement // Enable autoincrement
update_reg(addr, REG_MODE1, MODE1_AI, MODE1_AI); update_reg(dev, REG_MODE1, MODE1_AI, MODE1_AI);
} }
bool pca9685_set_subaddr(uint8_t addr, uint8_t num, uint8_t subaddr, bool enable) bool pca9685_set_subaddr(i2c_dev_t* dev, uint8_t num, uint8_t subaddr, bool enable)
{ {
if (num > MAX_SUBADDR) if (num > MAX_SUBADDR)
{ {
@ -92,63 +91,63 @@ bool pca9685_set_subaddr(uint8_t addr, uint8_t num, uint8_t subaddr, bool enable
return false; return false;
} }
write_reg(addr, REG_SUBADR1 + num, subaddr << 1); write_reg(dev, REG_SUBADR1 + num, subaddr << 1);
uint8_t mask = 1 << (MODE1_SUB_BIT - num); uint8_t mask = 1 << (MODE1_SUB_BIT - num);
update_reg(addr, REG_MODE1, mask, enable ? mask : 0); update_reg(dev, REG_MODE1, mask, enable ? mask : 0);
return true; return true;
} }
bool pca9685_is_sleeping(uint8_t addr) bool pca9685_is_sleeping(i2c_dev_t* dev)
{ {
return (read_reg(addr, REG_MODE1) & MODE1_SLEEP) != 0; return (read_reg(dev, REG_MODE1) & MODE1_SLEEP) != 0;
} }
void pca9685_sleep(uint8_t addr, bool sleep) void pca9685_sleep(i2c_dev_t* dev, bool sleep)
{ {
update_reg(addr, REG_MODE1, MODE1_SLEEP, sleep ? MODE1_SLEEP : 0); update_reg(dev, REG_MODE1, MODE1_SLEEP, sleep ? MODE1_SLEEP : 0);
if (!sleep) if (!sleep)
sdk_os_delay_us(WAKEUP_DELAY_US); sdk_os_delay_us(WAKEUP_DELAY_US);
} }
void pca9685_restart(uint8_t addr) void pca9685_restart(i2c_dev_t* dev)
{ {
uint8_t mode = read_reg(addr, REG_MODE1); uint8_t mode = read_reg(dev, REG_MODE1);
if (mode & MODE1_RESTART) if (mode & MODE1_RESTART)
{ {
write_reg(addr, REG_MODE1, mode & ~MODE1_SLEEP); write_reg(dev, REG_MODE1, mode & ~MODE1_SLEEP);
sdk_os_delay_us(WAKEUP_DELAY_US); sdk_os_delay_us(WAKEUP_DELAY_US);
} }
write_reg(addr, REG_MODE1, (mode & ~MODE1_SLEEP) | MODE1_RESTART); write_reg(dev, REG_MODE1, (mode & ~MODE1_SLEEP) | MODE1_RESTART);
} }
bool pca9685_is_output_inverted(uint8_t addr) bool pca9685_is_output_inverted(i2c_dev_t* dev)
{ {
return (read_reg(addr, REG_MODE2) & MODE2_INVRT) != 0; return (read_reg(dev, REG_MODE2) & MODE2_INVRT) != 0;
} }
void pca9685_set_output_inverted(uint8_t addr, bool inverted) void pca9685_set_output_inverted(i2c_dev_t* dev, bool inverted)
{ {
update_reg(addr, REG_MODE2, MODE2_INVRT, inverted ? MODE2_INVRT : 0); update_reg(dev, REG_MODE2, MODE2_INVRT, inverted ? MODE2_INVRT : 0);
} }
bool pca9685_get_output_open_drain(uint8_t addr) bool pca9685_get_output_open_drain(i2c_dev_t* dev)
{ {
return (read_reg(addr, REG_MODE2) & MODE2_OUTDRV) == 0; return (read_reg(dev, REG_MODE2) & MODE2_OUTDRV) == 0;
} }
void pca9685_set_output_open_drain(uint8_t addr, bool open_drain) void pca9685_set_output_open_drain(i2c_dev_t* dev, bool open_drain)
{ {
update_reg(addr, REG_MODE2, MODE2_OUTDRV, open_drain ? 0 : MODE2_OUTDRV); update_reg(dev, REG_MODE2, MODE2_OUTDRV, open_drain ? 0 : MODE2_OUTDRV);
} }
uint8_t pca9685_get_prescaler(uint8_t addr) uint8_t pca9685_get_prescaler(i2c_dev_t* dev)
{ {
return read_reg(addr, REG_PRE_SCALE); return read_reg(dev, REG_PRE_SCALE);
} }
bool pca9685_set_prescaler(uint8_t addr, uint8_t prescaler) bool pca9685_set_prescaler(i2c_dev_t* dev, uint8_t prescaler)
{ {
if (prescaler < MIN_PRESCALER) if (prescaler < MIN_PRESCALER)
{ {
@ -156,18 +155,18 @@ bool pca9685_set_prescaler(uint8_t addr, uint8_t prescaler)
return false; return false;
} }
pca9685_sleep(addr, true); pca9685_sleep(dev, true);
write_reg(addr, REG_PRE_SCALE, prescaler); write_reg(dev, REG_PRE_SCALE, prescaler);
pca9685_sleep(addr, false); pca9685_sleep(dev, false);
return true; return true;
} }
uint16_t pca9685_get_pwm_frequency(uint8_t addr) uint16_t pca9685_get_pwm_frequency(i2c_dev_t* dev)
{ {
return INTERNAL_FREQ / ((uint32_t)4096 * (read_reg(addr, REG_PRE_SCALE) + 1)); return INTERNAL_FREQ / ((uint32_t)4096 * (read_reg(dev, REG_PRE_SCALE) + 1));
} }
bool pca9685_set_pwm_frequency(uint8_t addr, uint16_t freq) bool pca9685_set_pwm_frequency(i2c_dev_t* dev, uint16_t freq)
{ {
uint16_t prescaler = round_div(INTERNAL_FREQ, (uint32_t)4096 * freq) - 1; uint16_t prescaler = round_div(INTERNAL_FREQ, (uint32_t)4096 * freq) - 1;
if (prescaler < MIN_PRESCALER || prescaler > MAX_PRESCALER) if (prescaler < MIN_PRESCALER || prescaler > MAX_PRESCALER)
@ -176,32 +175,32 @@ bool pca9685_set_pwm_frequency(uint8_t addr, uint16_t freq)
return false; return false;
} }
return pca9685_set_prescaler(addr, prescaler); return pca9685_set_prescaler(dev, prescaler);
} }
void pca9685_set_pwm_value(uint8_t addr, uint8_t channel, uint16_t val) void pca9685_set_pwm_value(i2c_dev_t* dev, uint8_t channel, uint16_t val)
{ {
uint8_t reg = channel > MAX_CHANNEL ? REG_ALL_LED : REG_LED_N(channel); uint8_t reg = channel > MAX_CHANNEL ? REG_ALL_LED : REG_LED_N(channel);
if (val == 0) if (val == 0)
{ {
// Full off // Full off
write_reg(addr, reg + OFFS_REG_LED_OFF, LED_FULL_ON_OFF); write_reg(dev, reg + OFFS_REG_LED_OFF, LED_FULL_ON_OFF);
} }
else if (val < 4096) else if (val < 4096)
{ {
// Normal // Normal
uint8_t buf[4] = { 0, 0, val, val >> 8 }; uint8_t buf[4] = { 0, 0, val, val >> 8 };
i2c_slave_write(addr, &reg, buf, 4); i2c_slave_write(dev->bus, dev->addr, &reg, buf, 4);
} }
else else
{ {
// Full on // Full on
write_reg(addr, reg + OFFS_REG_LED_ON, LED_FULL_ON_OFF); write_reg(dev, reg + OFFS_REG_LED_ON, LED_FULL_ON_OFF);
} }
} }
bool pca9685_set_pwm_values(uint8_t addr, uint8_t first_ch, uint8_t channels, const uint16_t *values) bool pca9685_set_pwm_values(i2c_dev_t* dev, uint8_t first_ch, uint8_t channels, const uint16_t *values)
{ {
if (channels == 0 || first_ch + channels - 1 > MAX_CHANNEL) if (channels == 0 || first_ch + channels - 1 > MAX_CHANNEL)
{ {
@ -210,7 +209,7 @@ bool pca9685_set_pwm_values(uint8_t addr, uint8_t first_ch, uint8_t channels, co
} }
for (uint8_t i = 0; i < channels; i ++) for (uint8_t i = 0; i < channels; i ++)
pca9685_set_pwm_value(addr, first_ch + i, values [i]); pca9685_set_pwm_value(dev, first_ch + i, values [i]);
return true; return true;
} }

View file

@ -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"
@ -22,7 +23,7 @@ extern "C"
* Init device * Init device
* @param addr Device address * @param addr Device address
*/ */
void pca9685_init(uint8_t addr); void pca9685_init(i2c_dev_t* dev);
/** /**
* Setup device subaddress (see section 7.3.6 if the datasheet) * Setup device subaddress (see section 7.3.6 if the datasheet)
@ -32,62 +33,62 @@ void pca9685_init(uint8_t addr);
* @param enable True to enable subaddress, false to disable * @param enable True to enable subaddress, false to disable
* @return False if error occured * @return False if error occured
*/ */
bool pca9685_set_subaddr(uint8_t addr, uint8_t num, uint8_t subaddr, bool enable); bool pca9685_set_subaddr(i2c_dev_t* dev, uint8_t num, uint8_t subaddr, bool enable);
/** /**
* Restart device (see section 7.3.1.1 of the datasheet) * Restart device (see section 7.3.1.1 of the datasheet)
* @param addr Device address * @param addr Device address
*/ */
void pca9685_restart(uint8_t addr); void pca9685_restart(i2c_dev_t* dev);
/** /**
* Check if device is in sleep mode * Check if device is in sleep mode
* @param addr Device address * @param addr Device address
* @return True if device is sleeping * @return True if device is sleeping
*/ */
bool pca9685_is_sleeping(uint8_t addr); bool pca9685_is_sleeping(i2c_dev_t* dev);
/** /**
* Switch device to low-power mode or wake it up. * Switch device to low-power mode or wake it up.
* @param addr Device address * @param addr Device address
* @param sleep True for sleep mode, false for wake up * @param sleep True for sleep mode, false for wake up
*/ */
void pca9685_sleep(uint8_t addr, bool sleep); void pca9685_sleep(i2c_dev_t* dev, bool sleep);
/** /**
* Get logic inversion of the outputs * Get logic inversion of the outputs
* @param addr Device address * @param addr Device address
* @return True if outputs are inverted, false otherwise * @return True if outputs are inverted, false otherwise
*/ */
bool pca9685_is_output_inverted(uint8_t addr); bool pca9685_is_output_inverted(i2c_dev_t* dev);
/** /**
* Logically invert outputs (see section 7.7 of the datasheet) * Logically invert outputs (see section 7.7 of the datasheet)
* @param addr Device address * @param addr Device address
* @param inverted True for inverted outputs * @param inverted True for inverted outputs
*/ */
void pca9685_set_output_inverted(uint8_t addr, bool inverted); void pca9685_set_output_inverted(i2c_dev_t* dev, bool inverted);
/** /**
* Get outputs mode * Get outputs mode
* @param addr Device address * @param addr Device address
* @return True if outputs are in open drain mode * @return True if outputs are in open drain mode
*/ */
bool pca9685_get_output_open_drain(uint8_t addr); bool pca9685_get_output_open_drain(i2c_dev_t* dev);
/** /**
* Set outputs mode * Set outputs mode
* @param addr Device address * @param addr Device address
* @param open_drain True to set open drain mode, false to normal mode * @param open_drain True to set open drain mode, false to normal mode
*/ */
void pca9685_set_output_open_drain(uint8_t addr, bool open_drain); void pca9685_set_output_open_drain(i2c_dev_t* dev, bool open_drain);
/** /**
* Get current PWM frequency prescaler. * Get current PWM frequency prescaler.
* @param addr Device address * @param addr Device address
* @return Frequency prescaler * @return Frequency prescaler
*/ */
uint8_t pca9685_get_prescaler(uint8_t addr); uint8_t pca9685_get_prescaler(i2c_dev_t* dev);
/** /**
* Set PWM frequency prescaler. * Set PWM frequency prescaler.
@ -95,14 +96,14 @@ uint8_t pca9685_get_prescaler(uint8_t addr);
* @param prescaler Prescaler value * @param prescaler Prescaler value
* @return False if error occured * @return False if error occured
*/ */
bool pca9685_set_prescaler(uint8_t addr, uint8_t prescaler); bool pca9685_set_prescaler(i2c_dev_t* dev, uint8_t prescaler);
/** /**
* Get current PWM frequency * Get current PWM frequency
* @param addr Device address * @param addr Device address
* @return PWM frequency, Hz * @return PWM frequency, Hz
*/ */
uint16_t pca9685_get_pwm_frequency(uint8_t addr); uint16_t pca9685_get_pwm_frequency(i2c_dev_t* dev);
/** /**
* Set PWM frequency * Set PWM frequency
@ -110,7 +111,7 @@ uint16_t pca9685_get_pwm_frequency(uint8_t addr);
* @param freq PWM frequency, Hz * @param freq PWM frequency, Hz
* @return False if error occured * @return False if error occured
*/ */
bool pca9685_set_pwm_frequency(uint8_t addr, uint16_t freq); bool pca9685_set_pwm_frequency(i2c_dev_t* dev, uint16_t freq);
/** /**
* Set PWM value on output channel * Set PWM value on output channel
@ -118,7 +119,7 @@ bool pca9685_set_pwm_frequency(uint8_t addr, uint16_t freq);
* @param channel Channel number, 0..15 or >15 for all channels * @param channel Channel number, 0..15 or >15 for all channels
* @param val PWM value, 0..4096 * @param val PWM value, 0..4096
*/ */
void pca9685_set_pwm_value(uint8_t addr, uint8_t channel, uint16_t val); void pca9685_set_pwm_value(i2c_dev_t* dev, uint8_t channel, uint16_t val);
/** /**
* Set PWM values on output channels * Set PWM values on output channels
@ -128,7 +129,7 @@ void pca9685_set_pwm_value(uint8_t addr, uint8_t channel, uint16_t val);
* @param values Array of the channel values, each 0..4096 * @param values Array of the channel values, each 0..4096
* @return False if error occured * @return False if error occured
*/ */
bool pca9685_set_pwm_values(uint8_t addr, uint8_t first_ch, uint8_t channels, const uint16_t *values); bool pca9685_set_pwm_values(i2c_dev_t* dev, uint8_t first_ch, uint8_t channels, const uint16_t *values);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -1,47 +1,46 @@
#include "pcf8574.h" #include "pcf8574.h"
#include <i2c/i2c.h>
uint8_t pcf8574_port_read(uint8_t addr) uint8_t pcf8574_port_read(i2c_dev_t* dev)
{ {
uint8_t res; uint8_t res;
if (i2c_slave_read(addr, NULL, &res, 1)) if (i2c_slave_read(dev->bus, dev->addr, NULL, &res, 1))
return 0; return 0;
return res; return res;
} }
size_t pcf8574_port_read_buf(uint8_t addr, void *buf, size_t len) size_t pcf8574_port_read_buf(i2c_dev_t* dev, void *buf, size_t len)
{ {
if (!len || !buf) return 0; if (!len || !buf) return 0;
uint8_t *_buf = (uint8_t *)buf; uint8_t *_buf = (uint8_t *)buf;
if (i2c_slave_read(addr, NULL, _buf, len)) if (i2c_slave_read(dev->bus, dev->addr, NULL, _buf, len))
return 0; return 0;
return len; return len;
} }
size_t pcf8574_port_write_buf(uint8_t addr, void *buf, size_t len) size_t pcf8574_port_write_buf(const i2c_dev_t* dev, void *buf, size_t len)
{ {
if (!len || !buf) return 0; if (!len || !buf) return 0;
uint8_t *_buf = (uint8_t *)buf; uint8_t *_buf = (uint8_t *)buf;
if (i2c_slave_write(addr, NULL, _buf, len)) if (i2c_slave_write(dev->bus, dev->addr, NULL, _buf, len))
return 0; return 0;
return len; return len;
} }
void pcf8574_port_write(uint8_t addr, uint8_t value) void pcf8574_port_write(const i2c_dev_t* dev, uint8_t value)
{ {
i2c_slave_write(addr, NULL, &value, 1); i2c_slave_write(dev->bus, dev->addr, NULL, &value, 1);
} }
bool pcf8574_gpio_read(uint8_t addr, uint8_t num) bool pcf8574_gpio_read(i2c_dev_t* dev, uint8_t num)
{ {
return (bool)((pcf8574_port_read(addr) >> num) & 1); return (bool)((pcf8574_port_read(dev) >> num) & 1);
} }
void pcf8574_gpio_write(uint8_t addr, uint8_t num, bool value) void pcf8574_gpio_write(i2c_dev_t* dev, uint8_t num, bool value)
{ {
uint8_t bit = (uint8_t)value << num; uint8_t bit = (uint8_t)value << num;
uint8_t mask = ~(1 << num); uint8_t mask = ~(1 << num);
pcf8574_port_write (addr, (pcf8574_port_read(addr) & mask) | bit); pcf8574_port_write (dev, (pcf8574_port_read(dev) & mask) | bit);
} }

View file

@ -8,6 +8,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <i2c/i2c.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
@ -19,7 +20,7 @@ extern "C"
* \param addr I2C register address (0b0100<A2><A1><A0> for PCF8574) * \param addr I2C register address (0b0100<A2><A1><A0> for PCF8574)
* \return 8-bit GPIO port value * \return 8-bit GPIO port value
*/ */
uint8_t pcf8574_port_read(uint8_t addr); uint8_t pcf8574_port_read(i2c_dev_t* dev);
/** /**
* \brief Continiously read GPIO port values to buffer * \brief Continiously read GPIO port values to buffer
@ -28,14 +29,14 @@ uint8_t pcf8574_port_read(uint8_t addr);
* @param len Buffer length * @param len Buffer length
* @return Number of bytes read * @return Number of bytes read
*/ */
size_t pcf8574_port_read_buf(uint8_t addr, void *buf, size_t len); size_t pcf8574_port_read_buf(i2c_dev_t* dev, void *buf, size_t len);
/** /**
* \brief Write value to GPIO port * \brief Write value to GPIO port
* \param addr I2C register address (0b0100<A2><A1><A0> for PCF8574) * \param addr I2C register address (0b0100<A2><A1><A0> for PCF8574)
* \param value GPIO port value * \param value GPIO port value
*/ */
void pcf8574_port_write(uint8_t addr, uint8_t value); void pcf8574_port_write(const i2c_dev_t* dev, uint8_t value);
/** /**
* \brief Continiously write GPIO values to GPIO port * \brief Continiously write GPIO values to GPIO port
@ -44,7 +45,7 @@ void pcf8574_port_write(uint8_t addr, uint8_t value);
* @param len Buffer length * @param len Buffer length
* @return Number of bytes written * @return Number of bytes written
*/ */
size_t pcf8574_port_write_buf(uint8_t addr, void *buf, size_t len); size_t pcf8574_port_write_buf(const i2c_dev_t* dev, void *buf, size_t len);
/** /**
* \brief Read input value of a GPIO pin * \brief Read input value of a GPIO pin
@ -52,7 +53,7 @@ size_t pcf8574_port_write_buf(uint8_t addr, void *buf, size_t len);
* \param num pin number (0..7) * \param num pin number (0..7)
* \return GPIO pin value * \return GPIO pin value
*/ */
bool pcf8574_gpio_read(uint8_t addr, uint8_t num); bool pcf8574_gpio_read(i2c_dev_t* dev, uint8_t num);
/** /**
* \brief Set GPIO pin output * \brief Set GPIO pin output
@ -62,7 +63,7 @@ bool pcf8574_gpio_read(uint8_t addr, uint8_t num);
* \param num pin number (0..7) * \param num pin number (0..7)
* \param value true for high level * \param value true for high level
*/ */
void pcf8574_gpio_write(uint8_t addr, uint8_t num, bool value); void pcf8574_gpio_write(i2c_dev_t* dev, uint8_t num, bool value);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -1,22 +1,20 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <i2c/i2c.h>
#include "pcf8591.h" #include "pcf8591.h"
/** /**
* CAUTION: PLEASE SET I2C_FREQUENCY_400K IS 'false' IN 'i2c.h' FILE * CAUTION: PLEASE SET LOW FREQUENCY
*/ */
#define PCF8591_CTRL_REG_READ 0x03 #define PCF8591_CTRL_REG_READ 0x03
uint8_t uint8_t pcf8591_read(i2c_dev_t* dev, uint8_t analog_pin)
pcf8591_read(uint8_t addr, uint8_t analog_pin)
{ {
uint8_t res = 0; uint8_t res = 0;
uint8_t control_reg = PCF8591_CTRL_REG_READ & analog_pin; uint8_t control_reg = PCF8591_CTRL_REG_READ & analog_pin;
i2c_slave_read(addr, &control_reg, &res, 1); i2c_slave_read(dev->bus, dev->addr, &control_reg, &res, 1);
return res; return res;
} }

View file

@ -9,6 +9,8 @@
#ifndef _EXTRAS_PCF8591_H_ #ifndef _EXTRAS_PCF8591_H_
#define _EXTRAS_PCF8591_H_ #define _EXTRAS_PCF8591_H_
#include <i2c/i2c.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
@ -20,7 +22,7 @@ extern "C"
#define PCF8591_DEFAULT_ADDRESS 0x48 #define PCF8591_DEFAULT_ADDRESS 0x48
void pcf8591_init(void); void pcf8591_init(void); //FIXME : library incomplete ?
/** /**
* Read input value of an analog pin. * Read input value of an analog pin.
@ -32,7 +34,7 @@ void pcf8591_init(void);
* 3 - AIN3 * 3 - AIN3
* @return analog value * @return analog value
*/ */
uint8_t pcf8591_read(uint8_t addr, uint8_t analog_pin); uint8_t pcf8591_read(i2c_dev_t* dev, uint8_t analog_pin);
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -11,9 +11,6 @@
*/ */
#include "ssd1306.h" #include "ssd1306.h"
#include <stdio.h> #include <stdio.h>
#if (SSD1306_I2C_SUPPORT)
#include <i2c/i2c.h>
#endif
#if (SSD1306_SPI4_SUPPORT) || (SSD1306_SPI3_SUPPORT) #if (SSD1306_SPI4_SUPPORT) || (SSD1306_SPI3_SUPPORT)
#include <esp/spi.h> #include <esp/spi.h>
#endif #endif
@ -92,7 +89,7 @@
#if (SSD1306_I2C_SUPPORT) #if (SSD1306_I2C_SUPPORT)
static int inline i2c_send(const ssd1306_t *dev, uint8_t reg, uint8_t* data, uint8_t len) static int inline i2c_send(const ssd1306_t *dev, uint8_t reg, uint8_t* data, uint8_t len)
{ {
return i2c_slave_write(dev->addr, &reg, data, len); return i2c_slave_write(dev->i2c_dev.bus, dev->i2c_dev.addr , &reg, data, len);
} }
#endif #endif

View file

@ -19,6 +19,7 @@
// shifted // shifted
#if (SSD1306_I2C_SUPPORT) #if (SSD1306_I2C_SUPPORT)
#include <i2c/i2c.h>
#define SSD1306_I2C_ADDR_0 (0x3C) #define SSD1306_I2C_ADDR_0 (0x3C)
#define SSD1306_I2C_ADDR_1 (0x3D) #define SSD1306_I2C_ADDR_1 (0x3D)
#endif #endif
@ -67,7 +68,7 @@ typedef struct
ssd1306_screen_t screen ; ssd1306_screen_t screen ;
union { union {
#if (SSD1306_I2C_SUPPORT) #if (SSD1306_I2C_SUPPORT)
uint8_t addr ; //!< I2C address, used by SSD1306_PROTO_I2C i2c_dev_t i2c_dev; //!< I2C devuce descriptor, used by SSD1306_PROTO_I2C
#endif #endif
uint8_t cs_pin ; //!< Chip Select GPIO pin, used by SSD1306_PROTO_SPI3, SSD1306_PROTO_SPI4 uint8_t cs_pin ; //!< Chip Select GPIO pin, used by SSD1306_PROTO_SPI3, SSD1306_PROTO_SPI4
} ; } ;

View file

@ -6,7 +6,6 @@
#include <stdio.h> #include <stdio.h>
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "i2c/i2c.h"
#include "task.h" #include "task.h"
#include "tsl2561.h" #include "tsl2561.h"
@ -96,18 +95,18 @@
#define B8C 0x0000 // 0.000 * 2^LUX_SCALE #define B8C 0x0000 // 0.000 * 2^LUX_SCALE
#define M8C 0x0000 // 0.000 * 2^LUX_SCALE #define M8C 0x0000 // 0.000 * 2^LUX_SCALE
static int write_register(uint8_t i2c_addr, uint8_t reg, uint8_t value) static int write_register(i2c_dev_t* i2c_dev, uint8_t reg, uint8_t value)
{ {
reg = TSL2561_REG_COMMAND | reg; reg = TSL2561_REG_COMMAND | reg;
return i2c_slave_write(i2c_addr, &reg, &value, 1); return i2c_slave_write(i2c_dev->bus, i2c_dev->addr, &reg, &value, 1);
} }
static uint8_t read_register(uint8_t i2c_addr, uint8_t reg) static uint8_t read_register(i2c_dev_t* i2c_dev, uint8_t reg)
{ {
uint8_t data[1]; uint8_t data[1];
reg = TSL2561_REG_COMMAND | reg; reg = TSL2561_REG_COMMAND | reg;
if (i2c_slave_read(i2c_addr, &reg, data, 1)) if (i2c_slave_read(i2c_dev->bus, i2c_dev->addr, &reg, data, 1))
{ {
printf("Error in tsl2561 read_register\n"); printf("Error in tsl2561 read_register\n");
} }
@ -115,13 +114,13 @@ static uint8_t read_register(uint8_t i2c_addr, uint8_t reg)
return data[0]; return data[0];
} }
static uint16_t read_register_16(uint8_t i2c_addr, uint8_t low_register_addr) static uint16_t read_register_16(i2c_dev_t* i2c_dev, uint8_t low_register_addr)
{ {
uint16_t value = 0; uint16_t value = 0;
uint8_t data[2]; uint8_t data[2];
low_register_addr = TSL2561_REG_COMMAND | TSL2561_READ_WORD | low_register_addr; low_register_addr = TSL2561_REG_COMMAND | TSL2561_READ_WORD | low_register_addr;
if (i2c_slave_read(i2c_addr, &low_register_addr, data, 2)) if (i2c_slave_read(i2c_dev->bus, i2c_dev->addr, &low_register_addr, data, 2))
{ {
printf("Error with i2c_slave_read in read_register_16\n"); printf("Error with i2c_slave_read in read_register_16\n");
} }
@ -131,24 +130,24 @@ static uint16_t read_register_16(uint8_t i2c_addr, uint8_t low_register_addr)
return value; return value;
} }
static int enable(uint8_t i2c_addr) static int enable(i2c_dev_t* i2c_dev)
{ {
return write_register(i2c_addr, TSL2561_REG_CONTROL, TSL2561_ON); return write_register(i2c_dev, TSL2561_REG_CONTROL, TSL2561_ON);
} }
static int disable(uint8_t i2c_addr) static int disable(i2c_dev_t* i2c_dev)
{ {
return write_register(i2c_addr, TSL2561_REG_CONTROL, TSL2561_OFF); return write_register(i2c_dev, TSL2561_REG_CONTROL, TSL2561_OFF);
} }
void tsl2561_init(tsl2561_t *device) void tsl2561_init(tsl2561_t *device)
{ {
if (enable(device->i2c_addr)) if (enable(&device->i2c_dev))
{ {
printf("Error initializing tsl2561\n"); printf("Error initializing tsl2561\n");
} }
uint8_t control_reg = (read_register(device->i2c_addr, TSL2561_REG_CONTROL) & TSL2561_ON); uint8_t control_reg = (read_register(&device->i2c_dev, TSL2561_REG_CONTROL) & TSL2561_ON);
if (control_reg != TSL2561_ON) if (control_reg != TSL2561_ON)
{ {
@ -156,39 +155,39 @@ void tsl2561_init(tsl2561_t *device)
} }
// Fetch the package type // Fetch the package type
uint8_t part_reg = read_register(device->i2c_addr, TSL2561_REG_PART_ID); uint8_t part_reg = read_register(&device->i2c_dev, TSL2561_REG_PART_ID);
uint8_t package = part_reg >> 6; uint8_t package = part_reg >> 6;
device->package_type = package; device->package_type = package;
// Fetch the gain and integration time // Fetch the gain and integration time
uint8_t timing_register = read_register(device->i2c_addr, TSL2561_REG_TIMING); uint8_t timing_register = read_register(&device->i2c_dev, TSL2561_REG_TIMING);
device->gain = timing_register & 0x10; device->gain = timing_register & 0x10;
device->integration_time = timing_register & 0x03; device->integration_time = timing_register & 0x03;
disable(device->i2c_addr); disable(&device->i2c_dev);
} }
void tsl2561_set_integration_time(tsl2561_t *device, tsl2561_integration_time_t integration_time_id) void tsl2561_set_integration_time(tsl2561_t *device, tsl2561_integration_time_t integration_time_id)
{ {
enable(device->i2c_addr); enable(&device->i2c_dev);
write_register(device->i2c_addr, TSL2561_REG_TIMING, integration_time_id | device->gain); write_register(&device->i2c_dev, TSL2561_REG_TIMING, integration_time_id | device->gain);
disable(device->i2c_addr); disable(&device->i2c_dev);
device->integration_time = integration_time_id; device->integration_time = integration_time_id;
} }
void tsl2561_set_gain(tsl2561_t *device, tsl2561_gain_t gain) void tsl2561_set_gain(tsl2561_t *device, tsl2561_gain_t gain)
{ {
enable(device->i2c_addr); enable(&device->i2c_dev);
write_register(device->i2c_addr, TSL2561_REG_TIMING, gain | device->integration_time); write_register(&device->i2c_dev, TSL2561_REG_TIMING, gain | device->integration_time);
disable(device->i2c_addr); disable(&device->i2c_dev);
device->gain = gain; device->gain = gain;
} }
static void get_channel_data(tsl2561_t *device, uint16_t *channel0, uint16_t *channel1) static void get_channel_data(tsl2561_t *device, uint16_t *channel0, uint16_t *channel1)
{ {
enable(device->i2c_addr); enable(&device->i2c_dev);
// Since we just enabled the chip, we need to sleep // Since we just enabled the chip, we need to sleep
// for the chip's integration time so it can gather a reading // for the chip's integration time so it can gather a reading
@ -205,10 +204,10 @@ static void get_channel_data(tsl2561_t *device, uint16_t *channel0, uint16_t *ch
break; break;
} }
*channel0 = read_register_16(device->i2c_addr, TSL2561_REG_CHANNEL_0_LOW); *channel0 = read_register_16(&device->i2c_dev, TSL2561_REG_CHANNEL_0_LOW);
*channel1 = read_register_16(device->i2c_addr, TSL2561_REG_CHANNEL_1_LOW); *channel1 = read_register_16(&device->i2c_dev, TSL2561_REG_CHANNEL_1_LOW);
disable(device->i2c_addr); disable(&device->i2c_dev);
} }
bool tsl2561_read_lux(tsl2561_t *device, uint32_t *lux) bool tsl2561_read_lux(tsl2561_t *device, uint32_t *lux)

View file

@ -9,6 +9,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" {
@ -38,7 +39,7 @@ typedef enum
} tsl2561_gain_t; } tsl2561_gain_t;
typedef struct { typedef struct {
tsl2561_i2c_addr_t i2c_addr; i2c_dev_t i2c_dev;
uint8_t integration_time; uint8_t integration_time;
uint8_t gain; uint8_t gain;
uint8_t package_type; uint8_t package_type;

View file

@ -6,7 +6,6 @@
#include <stdio.h> #include <stdio.h>
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "i2c/i2c.h"
#include "task.h" #include "task.h"
#include "tsl4531.h" #include "tsl4531.h"
@ -27,18 +26,18 @@
#define TSL4531_INTEGRATION_TIME_200MS 240 #define TSL4531_INTEGRATION_TIME_200MS 240
#define TSL4531_INTEGRATION_TIME_400MS 480 // Default #define TSL4531_INTEGRATION_TIME_400MS 480 // Default
static int write_register(uint8_t i2c_addr, uint8_t reg, uint8_t value) static int write_register(i2c_dev_t* i2c_dev, uint8_t reg, uint8_t value)
{ {
reg = TSL4531_REG_COMMAND | reg; reg = TSL4531_REG_COMMAND | reg;
return i2c_slave_write(i2c_addr, &reg, &value, 1); return i2c_slave_write(i2c_dev->bus, i2c_dev->addr, &reg, &value, 1);
} }
static uint8_t read_register(uint8_t i2c_addr, uint8_t reg) static uint8_t read_register(i2c_dev_t* i2c_dev, uint8_t reg)
{ {
uint8_t data[1]; uint8_t data[1];
reg = TSL4531_REG_COMMAND | reg; reg = TSL4531_REG_COMMAND | reg;
if (i2c_slave_read(i2c_addr, &reg, data, 1)) if (i2c_slave_read(i2c_dev->bus, i2c_dev->addr, &reg, data, 1))
{ {
printf("Error in tsl4531 read_register\n"); printf("Error in tsl4531 read_register\n");
} }
@ -46,13 +45,13 @@ static uint8_t read_register(uint8_t i2c_addr, uint8_t reg)
return data[0]; return data[0];
} }
static uint16_t read_register_16(uint8_t i2c_addr, uint8_t low_register_addr) static uint16_t read_register_16(i2c_dev_t* i2c_dev, uint8_t low_register_addr)
{ {
uint16_t value = 0; uint16_t value = 0;
uint8_t data[2]; uint8_t data[2];
low_register_addr = TSL4531_REG_COMMAND | low_register_addr; low_register_addr = TSL4531_REG_COMMAND | low_register_addr;
if (i2c_slave_read(i2c_addr, &low_register_addr, data, 2)) if (i2c_slave_read(i2c_dev->bus, i2c_dev->addr, &low_register_addr, data, 2))
{ {
printf("Error with i2c_slave_read in read_register_16\n"); printf("Error with i2c_slave_read in read_register_16\n");
} }
@ -64,12 +63,12 @@ static uint16_t read_register_16(uint8_t i2c_addr, uint8_t low_register_addr)
static int enable(tsl4531_t *device) static int enable(tsl4531_t *device)
{ {
return write_register(device->i2c_addr, TSL4531_REG_CONTROL, TSL4531_ON); return write_register(&device->i2c_dev, TSL4531_REG_CONTROL, TSL4531_ON);
} }
static int disable(tsl4531_t *device) static int disable(tsl4531_t *device)
{ {
return write_register(device->i2c_addr, TSL4531_REG_CONTROL, TSL4531_OFF); return write_register(&device->i2c_dev, TSL4531_REG_CONTROL, TSL4531_OFF);
} }
void tsl4531_init(tsl4531_t *device) void tsl4531_init(tsl4531_t *device)
@ -79,13 +78,13 @@ void tsl4531_init(tsl4531_t *device)
printf("Error initializing tsl4531, the enable write failed\n"); printf("Error initializing tsl4531, the enable write failed\n");
} }
uint8_t control_reg = read_register(device->i2c_addr, TSL4531_REG_CONTROL); uint8_t control_reg = read_register(&device->i2c_dev, TSL4531_REG_CONTROL);
if (control_reg != TSL4531_ON) { if (control_reg != TSL4531_ON) {
printf("Error initializing tsl4531, control register wasn't set to ON\n"); printf("Error initializing tsl4531, control register wasn't set to ON\n");
} }
uint8_t idRegister = read_register(device->i2c_addr, TSL4531_REG_DEVICE_ID); uint8_t idRegister = read_register(&device->i2c_dev, TSL4531_REG_DEVICE_ID);
uint8_t id = (idRegister & 0xF0) >> 4; uint8_t id = (idRegister & 0xF0) >> 4;
if (id == TSL4531_PART_TSL45317) { if (id == TSL4531_PART_TSL45317) {
@ -110,7 +109,7 @@ void tsl4531_set_integration_time(tsl4531_t *device, tsl4531_integration_time_t
uint8_t new_config_reg = power_save_bit | integration_time_bits; uint8_t new_config_reg = power_save_bit | integration_time_bits;
enable(device); enable(device);
write_register(device->i2c_addr, TSL4531_REG_CONFIG, new_config_reg); write_register(&device->i2c_dev, TSL4531_REG_CONFIG, new_config_reg);
disable(device); disable(device);
device->integration_time_id = integration_time_id; device->integration_time_id = integration_time_id;
@ -123,7 +122,7 @@ void tsl4531_set_power_save_skip(tsl4531_t *device, bool skip_power_save)
uint8_t new_config_reg = power_save_bit | integration_time_bits; uint8_t new_config_reg = power_save_bit | integration_time_bits;
enable(device); enable(device);
write_register(device->i2c_addr, TSL4531_REG_CONFIG, new_config_reg); write_register(&device->i2c_dev, TSL4531_REG_CONFIG, new_config_reg);
disable(device); disable(device);
device->skip_power_save = skip_power_save; device->skip_power_save = skip_power_save;
@ -156,7 +155,7 @@ bool tsl4531_read_lux(tsl4531_t *device, uint16_t *lux)
break; break;
} }
uint16_t lux_data = read_register_16(device->i2c_addr, TSL4531_REG_DATA_LOW); uint16_t lux_data = read_register_16(&device->i2c_dev, TSL4531_REG_DATA_LOW);
disable(device); disable(device);

View file

@ -9,6 +9,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" {
@ -38,7 +39,7 @@ typedef enum
} tsl4531_part_id_t; } tsl4531_part_id_t;
typedef struct { typedef struct {
tsl4531_i2c_addr_t i2c_addr; i2c_dev_t i2c_dev;
uint8_t integration_time_id; uint8_t integration_time_id;
bool skip_power_save; bool skip_power_save;
tsl4531_part_id_t part_id; tsl4531_part_id_t part_id;