From b83c2629b9710597563e9e67d8deb6eda6b593d7 Mon Sep 17 00:00:00 2001 From: Zaltora Date: Fri, 1 Sep 2017 06:29:32 -0300 Subject: [PATCH] I2C bus upgrade (#432) --- examples/ad770x/main.c | 1 - examples/ads1115_test/main.c | 19 ++- examples/bh1750/bh1750_example.c | 13 +- examples/bmp180_i2c/bmp180_i2c.c | 20 ++- examples/bmp280/bmp280_example.c | 12 +- examples/ds1307/main.c | 13 +- examples/ds3231_test/ds3231_test.c | 16 +- examples/fatfs_rtc/main.c | 9 +- examples/hmc5883l/main.c | 19 ++- examples/i2c_lcd_test/main.c | 6 +- examples/ina3221_test/main.c | 6 +- examples/mcp4725_test/main.c | 25 +-- examples/ms561101ba03/main.c | 6 +- examples/pca9685_pwm/main.c | 19 ++- examples/pcf8591/main.c | 10 +- examples/ssd1306_example/main.c | 6 +- examples/ssd1306_fps/main.c | 6 +- examples/tsl2561/tsl2561_example.c | 6 +- examples/tsl4531/tsl4531_example.c | 6 +- extras/ad770x/ad770x.c | 1 - extras/ads111x/ads111x.c | 109 +++++++------ extras/ads111x/ads111x.h | 47 +++--- extras/bh1750/bh1750.c | 9 +- extras/bh1750/bh1750.h | 5 +- extras/bmp180/bmp180.c | 112 ++++++------- extras/bmp180/bmp180.h | 18 ++- extras/bmp280/bmp280.c | 74 ++++----- extras/bmp280/bmp280.h | 3 +- extras/ds1307/ds1307.c | 60 ++++--- extras/ds1307/ds1307.h | 26 +-- extras/ds3231/ds3231.c | 94 +++++------ extras/ds3231/ds3231.h | 37 ++--- extras/hd44780/hd44780.c | 6 +- extras/hd44780/hd44780.h | 9 +- extras/hmc5883l/hmc5883l.c | 86 +++++----- extras/hmc5883l/hmc5883l.h | 35 ++-- extras/i2c/i2c.c | 252 +++++++++++++++-------------- extras/i2c/i2c.h | 98 +++++++---- extras/ina3221/ina3221.c | 62 +++---- extras/ina3221/ina3221.h | 2 +- extras/mcp4725/mcp4725.c | 29 ++-- extras/mcp4725/mcp4725.h | 19 +-- extras/ms561101ba03/ms561101ba03.c | 31 ++-- extras/ms561101ba03/ms561101ba03.h | 3 +- extras/pca9685/pca9685.c | 91 ++++++----- extras/pca9685/pca9685.h | 31 ++-- extras/pcf8574/pcf8574.c | 25 ++- extras/pcf8574/pcf8574.h | 13 +- extras/pcf8591/pcf8591.c | 8 +- extras/pcf8591/pcf8591.h | 6 +- extras/ssd1306/ssd1306.c | 5 +- extras/ssd1306/ssd1306.h | 3 +- extras/tsl2561/tsl2561.c | 51 +++--- extras/tsl2561/tsl2561.h | 3 +- extras/tsl4531/tsl4531.c | 29 ++-- extras/tsl4531/tsl4531.h | 3 +- 56 files changed, 909 insertions(+), 804 deletions(-) diff --git a/examples/ad770x/main.c b/examples/ad770x/main.c index 6b13afc..2a021cc 100644 --- a/examples/ad770x/main.c +++ b/examples/ad770x/main.c @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include diff --git a/examples/ads1115_test/main.c b/examples/ads1115_test/main.c index 8065f04..5c0c4db 100644 --- a/examples/ads1115_test/main.c +++ b/examples/ads1115_test/main.c @@ -17,6 +17,7 @@ // Connect ADDR pin to GND #define ADDR ADS111X_ADDR_GND +#define I2C_BUS 0 #define SCL_PIN 5 #define SDA_PIN 4 @@ -28,23 +29,27 @@ void user_init(void) uart_set_baud(0, 115200); printf("SDK version:%s\n", sdk_system_get_sdk_version()); - i2c_init(SCL_PIN, SDA_PIN); + 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_data_rate(ADDR, ADS111X_DATA_RATE_32); + ads111x_set_mode(&dev, ADS111X_MODE_CONTUNOUS); + ads111x_set_data_rate(&dev, ADS111X_DATA_RATE_32); - ads111x_set_input_mux(ADDR, ADS111X_MUX_0_GND); - ads111x_set_gain(ADDR, GAIN); + ads111x_set_input_mux(&dev, ADS111X_MUX_0_GND); + ads111x_set_gain(&dev, GAIN); float gain_val = ads111x_gain_values[GAIN]; while (true) { // wait for conversion end - while (ads111x_busy(ADDR)) {} + while (ads111x_busy(&dev)) {} // Read result - int16_t raw = ads111x_get_value(ADDR); + int16_t raw = ads111x_get_value(&dev); float voltage = gain_val / ADS111X_MAX_VALUE * raw; diff --git a/examples/bh1750/bh1750_example.c b/examples/bh1750/bh1750_example.c index fad67bd..b75115c 100644 --- a/examples/bh1750/bh1750_example.c +++ b/examples/bh1750/bh1750_example.c @@ -11,17 +11,20 @@ #define SCL_PIN 5 #define SDA_PIN 4 +#define I2C_BUS 0 static void measure(void *pvParameters) { - bh1750_configure(BH1750_ADDR_LO, - BH1750_CONTINUOUS_MODE | BH1750_HIGH_RES_MODE); + i2c_dev_t dev = { + .addr = BH1750_ADDR_LO, + .bus = I2C_BUS, + }; + bh1750_configure(&dev, BH1750_CONTINUOUS_MODE | BH1750_HIGH_RES_MODE); while (1) { while(1) { vTaskDelay(200 / portTICK_PERIOD_MS); - - printf("Lux: %d\n", bh1750_read(BH1750_ADDR_LO)); + printf("Lux: %d\n", bh1750_read(&dev)); } } } @@ -35,7 +38,7 @@ void user_init(void) printf("SDK version : %s\n", sdk_system_get_sdk_version()); printf("GIT version : %s\n", GITSHORTREV); - i2c_init(SCL_PIN, SDA_PIN); + i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K); xTaskCreate(measure, "measure_task", 256, NULL, 2, NULL); } diff --git a/examples/bmp180_i2c/bmp180_i2c.c b/examples/bmp180_i2c/bmp180_i2c.c index 1e188bc..7153c64 100644 --- a/examples/bmp180_i2c/bmp180_i2c.c +++ b/examples/bmp180_i2c/bmp180_i2c.c @@ -13,18 +13,25 @@ // BMP180 driver #include "bmp180/bmp180.h" -#define MY_EVT_TIMER 0x01 -#define MY_EVT_BMP180 0x02 - +#define I2C_BUS 0 #define SCL_PIN GPIO_ID_PIN((0)) #define SDA_PIN GPIO_ID_PIN((2)) +#define MY_EVT_TIMER 0x01 +#define MY_EVT_BMP180 0x02 + typedef struct { uint8_t event_type; bmp180_result_t bmp180_data; } my_event_t; +//device descriptor +i2c_dev_t dev = { + .addr = BMP180_DEVICE_ADDRESS, + .bus = I2C_BUS, +}; + // Communication Queue static QueueHandle_t mainqueue; static TimerHandle_t timerHandle; @@ -70,7 +77,7 @@ void bmp180_task(void *pvParameters) case MY_EVT_TIMER: printf("%s: Received Timer Event\n", __FUNCTION__); - bmp180_trigger_measurement(com_queue); + bmp180_trigger_measurement(&dev, com_queue); break; case MY_EVT_BMP180: 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 sdk_os_delay_us(500); + + // Init I2C bus Interface + i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K); } void user_init(void) @@ -107,7 +117,7 @@ void user_init(void) bmp180_informUser = bmp180_i2c_informUser; // Init BMP180 Interface - bmp180_init(SCL_PIN, SDA_PIN); + bmp180_init(&dev); // Create Main Communication Queue mainqueue = xQueueCreate(10, sizeof(my_event_t)); diff --git a/examples/bmp280/bmp280_example.c b/examples/bmp280/bmp280_example.c index c4c7c11..4ef7ec2 100644 --- a/examples/bmp280/bmp280_example.c +++ b/examples/bmp280/bmp280_example.c @@ -10,9 +10,9 @@ #include "bmp280/bmp280.h" // 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 - +const uint8_t i2c_bus = 0; const uint8_t scl_pin = 0; const uint8_t sda_pin = 2; @@ -26,7 +26,8 @@ static void bmp280_task_forced(void *pvParameters) params.mode = BMP280_MODE_FORCED; bmp280_t bmp280_dev; - bmp280_dev.i2c_addr = BMP280_I2C_ADDRESS_0; + bmp280_dev.i2c_dev.bus = i2c_bus; + bmp280_dev.i2c_dev.addr = BMP280_I2C_ADDRESS_0; while (1) { while (!bmp280_init(&bmp280_dev, ¶ms)) { @@ -67,7 +68,8 @@ static void bmp280_task_normal(void *pvParameters) bmp280_init_default_params(¶ms); bmp280_t bmp280_dev; - bmp280_dev.i2c_addr = BMP280_I2C_ADDRESS_0; + bmp280_dev.i2c_dev.bus = i2c_bus; + bmp280_dev.i2c_dev.addr = BMP280_I2C_ADDRESS_0; while (1) { while (!bmp280_init(&bmp280_dev, ¶ms)) { @@ -103,7 +105,7 @@ void user_init(void) printf("SDK version : %s\n", sdk_system_get_sdk_version()); printf("GIT version : %s\n", GITSHORTREV); - i2c_init(scl_pin, sda_pin); + i2c_init(i2c_bus, scl_pin, sda_pin, I2C_FREQ_400K); #ifdef MODE_FORCED xTaskCreate(bmp280_task_forced, "bmp280_task", 256, NULL, 2, NULL); diff --git a/examples/ds1307/main.c b/examples/ds1307/main.c index a1b847e..a276f0b 100644 --- a/examples/ds1307/main.c +++ b/examples/ds1307/main.c @@ -11,6 +11,7 @@ #include #include +#define I2C_BUS 0 #define SCL_PIN 5 #define SDA_PIN 4 @@ -19,8 +20,12 @@ void user_init(void) uart_set_baud(0, 115200); printf("SDK version:%s\n", sdk_system_get_sdk_version()); - i2c_init(SCL_PIN, SDA_PIN); - ds1307_start(true); + i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_400K); + i2c_dev_t dev = { + .addr = DS1307_ADDR, + .bus = I2C_BUS, + }; + ds1307_start(&dev, true); // setup datetime: 2016-10-09 13:50:10 struct tm time = { @@ -31,11 +36,11 @@ void user_init(void) .tm_min = 50, .tm_sec = 10 }; - ds1307_set_time(&time); + ds1307_set_time(&dev, &time); while (true) { - ds1307_get_time(&time); + ds1307_get_time(&dev, &time); printf("%04d-%02d-%02d %02d:%02d:%02d\n", time.tm_year, time.tm_mon + 1, time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec); diff --git a/examples/ds3231_test/ds3231_test.c b/examples/ds3231_test/ds3231_test.c index 858b0a0..b6f41bb 100644 --- a/examples/ds3231_test/ds3231_test.c +++ b/examples/ds3231_test/ds3231_test.c @@ -1,4 +1,4 @@ -/* Test code for DS3231 high precision RTC module +/* Test code for DS3231 high precision RTC module * * Part of esp-open-rtos * Copyright (C) 2016 Bhuvanchandra DV @@ -12,15 +12,21 @@ #include "ds3231/ds3231.h" +#define ADDR DS3231_ADDR +#define I2C_BUS 0 + void task1(void *pvParameters) { struct tm time; float tempFloat; - + i2c_dev_t dev = { + .addr = ADDR, + .bus = I2C_BUS, + }; while(1) { vTaskDelay(100); - ds3231_getTime(&time); - ds3231_getTempFloat(&tempFloat); + ds3231_getTime(&dev, &time); + ds3231_getTempFloat(&dev, &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("GIT version : %s\n", GITSHORTREV); - ds3231_Init(scl, sda); + i2c_init(0,scl,sda,I2C_FREQ_400K); xTaskCreate(task1, "tsk1", 256, NULL, 2, NULL); } diff --git a/examples/fatfs_rtc/main.c b/examples/fatfs_rtc/main.c index 613eb6f..4a9ec76 100644 --- a/examples/fatfs_rtc/main.c +++ b/examples/fatfs_rtc/main.c @@ -18,6 +18,7 @@ #define CS_GPIO_PIN 2 // ds1307 +#define I2C_BUS 0 #define SCL_PIN 5 #define SDA_PIN 4 @@ -29,7 +30,11 @@ uint32_t get_fattime() { 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) | ((uint32_t)time.tm_mon << 21) @@ -127,7 +132,7 @@ void user_init(void) uart_set_baud(0, 115200); printf("SDK version:%s\n\n", sdk_system_get_sdk_version()); - i2c_init (SCL_PIN, SDA_PIN); + i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_400K); xTaskCreate(rewrite_file_task, "task1", 512, NULL, 2, NULL); } diff --git a/examples/hmc5883l/main.c b/examples/hmc5883l/main.c index c4b0bf4..866ed25 100644 --- a/examples/hmc5883l/main.c +++ b/examples/hmc5883l/main.c @@ -11,6 +11,7 @@ #include #include +#define I2C_BUS 0 #define SCL_PIN 5 #define SDA_PIN 4 @@ -19,20 +20,24 @@ void user_init(void) uart_set_baud(0, 115200); printf("SDK version:%s\n\n", sdk_system_get_sdk_version()); - i2c_init(SCL_PIN, SDA_PIN); + i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K); + i2c_dev_t dev = { + .addr = HMC5883L_ADDR, + .bus = I2C_BUS, + }; - while (!hmc5883l_init()) + while (!hmc5883l_init(&dev)) printf("Device not found\n"); - hmc5883l_set_operating_mode(HMC5883L_MODE_CONTINUOUS); - hmc5883l_set_samples_averaged(HMC5883L_SAMPLES_8); - hmc5883l_set_data_rate(HMC5883L_DATA_RATE_07_50); - hmc5883l_set_gain(HMC5883L_GAIN_1090); + hmc5883l_set_operating_mode(&dev, HMC5883L_MODE_CONTINUOUS); + hmc5883l_set_samples_averaged(&dev, HMC5883L_SAMPLES_8); + hmc5883l_set_data_rate(&dev, HMC5883L_DATA_RATE_07_50); + hmc5883l_set_gain(&dev, HMC5883L_GAIN_1090); while (true) { hmc5883l_data_t data; - hmc5883l_get_data(&data); + hmc5883l_get_data(&dev, &data); printf("Magnetic data: X:%.2f mG, Y:%.2f mG, Z:%.2f mG\n", data.x, data.y, data.z); for (uint32_t i = 0; i < 1000; i++) diff --git a/examples/i2c_lcd_test/main.c b/examples/i2c_lcd_test/main.c index 1951777..cb2af04 100644 --- a/examples/i2c_lcd_test/main.c +++ b/examples/i2c_lcd_test/main.c @@ -13,6 +13,7 @@ #include +#define I2C_BUS 0 #define SCL_PIN 5 #define SDA_PIN 4 #define ADDR 0x27 @@ -27,10 +28,11 @@ void user_init(void) uart_set_baud(0, 115200); printf("SDK version:%s\n", sdk_system_get_sdk_version()); - i2c_init(SCL_PIN, SDA_PIN); + i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K); hd44780_t lcd = { - .addr = ADDR, + .i2c_dev.bus = I2C_BUS, + .i2c_dev.addr = ADDR, .font = HD44780_FONT_5X8, .lines = 2, .pins = { diff --git a/examples/ina3221_test/main.c b/examples/ina3221_test/main.c index 5a883d2..cd8150e 100644 --- a/examples/ina3221_test/main.c +++ b/examples/ina3221_test/main.c @@ -13,6 +13,7 @@ #include #include "ina3221/ina3221.h" +#define I2C_BUS 0 #define PIN_SCL 5 #define PIN_SDA 2 #define ADDR INA3221_ADDR_0 @@ -33,7 +34,8 @@ void ina_measure(void *pvParameters) // Create ina3221 device 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 .mask.mask_register = INA3221_DEFAULT_MASK, // Init .config.config_register = INA3221_DEFAULT_CONFIG, // Init @@ -120,7 +122,7 @@ void user_init(void) uart_set_baud(0, 115200); 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); } diff --git a/examples/mcp4725_test/main.c b/examples/mcp4725_test/main.c index f3eae0b..b7d42af 100644 --- a/examples/mcp4725_test/main.c +++ b/examples/mcp4725_test/main.c @@ -14,14 +14,15 @@ #include #include +#define I2C_BUS 0 #define SCL_PIN 5 #define SDA_PIN 4 #define ADDR MCP4725A0_ADDR0 #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"); vTaskDelay(1); @@ -33,21 +34,25 @@ void user_init(void) uart_set_baud(0, 115200); printf("SDK version:%s\n", sdk_system_get_sdk_version()); - i2c_init(SCL_PIN, SDA_PIN); + i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K); + i2c_dev_t dev = { + .addr = ADDR, + .bus = I2C_BUS, + }; // 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"); - mcp4725_set_power_mode(ADDR, MCP4725_PM_NORMAL, true); - wait_for_eeprom(); + mcp4725_set_power_mode(&dev, MCP4725_PM_NORMAL, true); + wait_for_eeprom(&dev); } printf("Set default DAC ouptut value to MAX...\n"); - mcp4725_set_raw_output(ADDR, MCP4725_MAX_VALUE, true); - wait_for_eeprom(); + mcp4725_set_raw_output(&dev, MCP4725_MAX_VALUE, true); + 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"); @@ -58,7 +63,7 @@ void user_init(void) if (vout > VDD) vout = 0; 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 vTaskDelay(100 / portTICK_PERIOD_MS); diff --git a/examples/ms561101ba03/main.c b/examples/ms561101ba03/main.c index 9e993d1..b4b9c73 100644 --- a/examples/ms561101ba03/main.c +++ b/examples/ms561101ba03/main.c @@ -13,18 +13,20 @@ #include #include +#define I2C_BUS 0 #define SCL_PIN 5 #define SDA_PIN 4 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); printf("SDK version:%s\n\n", sdk_system_get_sdk_version()); ms561101ba03_t device = { - .addr = MS561101BA03_ADDR_CSB_LOW, + .i2c_dev.bus = I2C_BUS, + .i2c_dev.addr = MS561101BA03_ADDR_CSB_LOW, .osr = MS561101BA03_OSR_4096, }; diff --git a/examples/pca9685_pwm/main.c b/examples/pca9685_pwm/main.c index b191334..07c0aac 100644 --- a/examples/pca9685_pwm/main.c +++ b/examples/pca9685_pwm/main.c @@ -11,8 +11,9 @@ #include #include -#define ADDR 0x40 +#define ADDR PCA9685_ADDR_BASE +#define I2C_BUS 0 #define SCL_PIN 5 #define SDA_PIN 4 @@ -23,19 +24,23 @@ void user_init(void) uart_set_baud(0, 115200); printf("SDK version:%s\n", sdk_system_get_sdk_version()); - i2c_init(SCL_PIN, SDA_PIN); + 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); - printf("Freq 1000Hz, real %d\n", pca9685_get_pwm_frequency(ADDR)); + pca9685_set_pwm_frequency(&dev, 1000); + printf("Freq 1000Hz, real %d\n", pca9685_get_pwm_frequency(&dev)); uint16_t val = 0; while (true) { printf("Set ch0 to %d, ch4 to %d\n", val, 4096 - val); - pca9685_set_pwm_value(ADDR, 0, val); - pca9685_set_pwm_value(ADDR, 4, 4096 - val); + pca9685_set_pwm_value(&dev, 0, val); + pca9685_set_pwm_value(&dev, 4, 4096 - val); if (val++ == 4096) val = 0; diff --git a/examples/pcf8591/main.c b/examples/pcf8591/main.c index 7547c75..a4dc8a3 100644 --- a/examples/pcf8591/main.c +++ b/examples/pcf8591/main.c @@ -9,15 +9,21 @@ #include "i2c/i2c.h" #include "pcf8591/pcf8591.h" +#define ADDR PCF8591_DEFAULT_ADDRESS +#define I2C_BUS 0 #define SCL_PIN 5 #define SDA_PIN 4 static void measure(void *pvParameters) { + i2c_dev_t dev = { + .addr = ADDR, + .bus = I2C_BUS, + }; while (1) { 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("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); } diff --git a/examples/ssd1306_example/main.c b/examples/ssd1306_example/main.c index 58781c5..f88d798 100644 --- a/examples/ssd1306_example/main.c +++ b/examples/ssd1306_example/main.c @@ -23,6 +23,7 @@ #ifdef I2C_CONNECTION #define PROTOCOL SSD1306_PROTO_I2C #define ADDR SSD1306_I2C_ADDR_0 + #define I2C_BUS 0 #define SCL_PIN 5 #define SDA_PIN 4 #else @@ -35,7 +36,8 @@ static const ssd1306_t dev = { .protocol = PROTOCOL, #ifdef I2C_CONNECTION - .addr = ADDR, + .i2c_dev.bus = I2C_BUS, + .i2c_dev.addr = ADDR, #else .cs_pin = CS_PIN, .dc_pin = DC_PIN, @@ -97,7 +99,7 @@ void user_init(void) printf("SDK version:%s\n", sdk_system_get_sdk_version()); #ifdef I2C_CONNECTION - i2c_init(SCL_PIN, SDA_PIN); + i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_400K); #endif while (ssd1306_init(&dev) != 0) diff --git a/examples/ssd1306_fps/main.c b/examples/ssd1306_fps/main.c index b35dbfb..dc1f1a9 100644 --- a/examples/ssd1306_fps/main.c +++ b/examples/ssd1306_fps/main.c @@ -29,6 +29,7 @@ #ifdef I2C_CONNECTION #define PROTOCOL SSD1306_PROTO_I2C #define ADDR SSD1306_I2C_ADDR_0 + #define I2C_BUS 0 #define SCL_PIN 5 #define SDA_PIN 4 #else @@ -43,7 +44,8 @@ static const ssd1306_t dev = { .protocol = PROTOCOL, #ifdef I2C_CONNECTION - .addr = ADDR, +.i2c_dev.bus = I2C_BUS, +.i2c_dev.addr = ADDR, #else .cs_pin = CS_PIN, .dc_pin = DC_PIN, @@ -162,7 +164,7 @@ void user_init(void) printf("SDK version:%s\n", sdk_system_get_sdk_version()); #ifdef I2C_CONNECTION - i2c_init(SCL_PIN, SDA_PIN); + i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_400K); #endif while (ssd1306_init(&dev) != 0) { diff --git a/examples/tsl2561/tsl2561_example.c b/examples/tsl2561/tsl2561_example.c index b97804b..6f3582f 100644 --- a/examples/tsl2561/tsl2561_example.c +++ b/examples/tsl2561/tsl2561_example.c @@ -16,6 +16,7 @@ * Connect 3.3v from the ESP to Vin and GND to GND */ +#define I2C_BUS (0) #define SCL_PIN (2) #define SDA_PIN (0) @@ -27,7 +28,8 @@ void tsl2561MeasurementTask(void *pvParameters) // TSL2561_I2C_ADDR_VCC (0x49) // TSL2561_I2C_ADDR_GND (0x29) // 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); @@ -63,7 +65,7 @@ void tsl2561MeasurementTask(void *pvParameters) void user_init(void) { 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); } diff --git a/examples/tsl4531/tsl4531_example.c b/examples/tsl4531/tsl4531_example.c index 9b69b29..f40213a 100644 --- a/examples/tsl4531/tsl4531_example.c +++ b/examples/tsl4531/tsl4531_example.c @@ -16,13 +16,15 @@ * Connect 3.3v from the ESP to Vin and GND to GND */ +#define I2C_BUS (0) #define SCL_PIN (2) #define SDA_PIN (0) void tsl4531MeasurementTask(void *pvParameters) { 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_set_integration_time(&lightSensor, TSL4531_INTEGRATION_400MS); @@ -49,7 +51,7 @@ void tsl4531MeasurementTask(void *pvParameters) void user_init(void) { 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); } diff --git a/extras/ad770x/ad770x.c b/extras/ad770x/ad770x.c index 9bd8af5..0882d94 100644 --- a/extras/ad770x/ad770x.c +++ b/extras/ad770x/ad770x.c @@ -179,4 +179,3 @@ uint16_t ad770x_raw_adc_value(const ad770x_params_t *params, uint8_t channel) prepare(channel, REG_DATA, true, params->cs_pin, false); return read_word(params->cs_pin); } - diff --git a/extras/ads111x/ads111x.c b/extras/ads111x/ads111x.c index c9d3cf3..8e68ee4 100644 --- a/extras/ads111x/ads111x.c +++ b/extras/ads111x/ads111x.c @@ -6,7 +6,6 @@ * BSD Licensed as described in the file LICENSE */ #include "ads111x.h" -#include #define ADS111X_DEBUG @@ -52,144 +51,144 @@ const float ads111x_gain_values[] = { [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; - if (i2c_slave_read(addr, ®, (uint8_t *)&res, 2)) + if (i2c_slave_read(dev->bus, dev->addr, ®, (uint8_t *)&res, 2)) debug("Could not read register %d", reg); //debug("Read %d: 0x%04x", reg, 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); uint8_t buf[2] = { val >> 8, val}; - if (i2c_slave_write(addr, ®, buf, 2)) + if (i2c_slave_write(dev->bus, dev->addr, ®, buf, 2)) debug("Could not write 0x%04x to register %d", val, reg); } -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); } diff --git a/extras/ads111x/ads111x.h b/extras/ads111x/ads111x.h index fe64464..af93d4e 100644 --- a/extras/ads111x/ads111x.h +++ b/extras/ads111x/ads111x.h @@ -10,6 +10,7 @@ #include #include +#include #ifdef __cplusplus extern "C" { @@ -124,20 +125,20 @@ typedef enum * @param addr Deivce address * @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) * @param addr Deivce address */ -void ads111x_start_conversion(uint8_t addr); +void ads111x_start_conversion(i2c_dev_t* dev); /** * Read last conversion result * @param addr * @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 @@ -145,70 +146,70 @@ int16_t ads111x_get_value(uint8_t addr); * @param addr Deivce address * @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) * @param addr Deivce address * @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) * @param addr Deivce address * @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) * @param addr Deivce address * @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 * @param addr Deivce address * @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 * @param addr Deivce address * @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 * @param addr Deivce address * @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 * @param addr Deivce address * @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) * @param addr Deivce address * @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) * @param addr Deivce address * @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 @@ -216,7 +217,7 @@ void ads111x_set_comp_mode(uint8_t addr, ads111x_comp_mode_t mode); * @param addr Deivce address * @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 @@ -224,7 +225,7 @@ ads111x_comp_polarity_t ads111x_get_comp_polarity(uint8_t addr); * @param addr Deivce address * @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. @@ -232,14 +233,14 @@ void ads111x_set_comp_polarity(uint8_t addr, ads111x_comp_polarity_t polarity); * @param addr Deivce address * @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) * @param addr Deivce address * @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 @@ -247,7 +248,7 @@ void ads111x_set_comp_latch(uint8_t addr, ads111x_comp_latch_t latch); * @param addr Deivce address * @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 @@ -255,35 +256,35 @@ ads111x_comp_queue_t ads111x_get_comp_queue(uint8_t addr); * @param addr Deivce address * @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 * @param addr Deivce address * @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 * @param addr Deivce address * @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 * @param addr Deivce address * @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 * @param addr Deivce address * @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 } diff --git a/extras/bh1750/bh1750.c b/extras/bh1750/bh1750.c index 68e623a..51860d0 100644 --- a/extras/bh1750/bh1750.c +++ b/extras/bh1750/bh1750.c @@ -6,20 +6,19 @@ * BSD Licensed as described in the file LICENSE */ #include "bh1750.h" -#include #include -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]; 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 = (level * 10) / 12; // convert to LUX diff --git a/extras/bh1750/bh1750.h b/extras/bh1750/bh1750.h index b280c45..a6bcae6 100644 --- a/extras/bh1750/bh1750.h +++ b/extras/bh1750/bh1750.h @@ -13,6 +13,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -65,7 +66,7 @@ extern "C" { * * 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. @@ -73,7 +74,7 @@ void bh1750_configure(uint8_t addr, uint8_t mode); * @param addr Device address * @returns read value in lux units */ -uint16_t bh1750_read(uint8_t addr); +uint16_t bh1750_read(i2c_dev_t *dev); #ifdef __cplusplus } diff --git a/extras/bmp180/bmp180.c b/extras/bmp180/bmp180.c index df7a503..2cf513e 100644 --- a/extras/bmp180/bmp180.c +++ b/extras/bmp180/bmp180.c @@ -7,13 +7,9 @@ #include "espressif/esp_common.h" #include "espressif/sdk_private.h" -#include "i2c/i2c.h" - #define BMP180_RX_QUEUE_SIZE 10 #define BMP180_TASK_PRIORITY 9 -#define BMP180_DEVICE_ADDRESS 0x77 - #define BMP180_VERSION_REG 0xD0 #define BMP180_CONTROL_REG 0xF4 #define BMP180_RESET_REG 0xE0 @@ -39,43 +35,43 @@ // #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 }; int error ; - if ((error = i2c_slave_read(BMP180_DEVICE_ADDRESS, ®, d, 2))) + if ((error = i2c_slave_read(dev->bus, dev->addr, ®, d, 2))) return error; *r = ((int16_t)d[0] << 8) | (d[1]); 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 ; - return i2c_slave_write(BMP180_DEVICE_ADDRESS, ®, &cmd, 1); + return i2c_slave_write(dev->bus, dev->addr, ®, &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. - if (bmp180_start_Messurement(BMP180_MEASURE_TEMP)) + if (bmp180_start_Messurement(dev, BMP180_MEASURE_TEMP)) return false; // Wait 5ms, datasheet states 4.5ms sdk_os_delay_us(5000); int16_t v; - if (bmp180_readRegister16(BMP180_OUT_MSB_REG, &v)) + if (bmp180_readRegister16(dev, BMP180_OUT_MSB_REG, &v)) return false; *ut = v; 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; @@ -89,14 +85,14 @@ static bool bmp180_get_uncompensated_pressure(uint8_t oss, uint32_t *up) } // 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; sdk_os_delay_us(us); uint8_t d[] = { 0, 0, 0 }; uint8_t reg = BMP180_OUT_MSB_REG; - if (i2c_slave_read(BMP180_DEVICE_ADDRESS, ®, d, 3)) + if (i2c_slave_read(dev->bus, dev->addr, ®, d, 3)) return false; 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. -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) || - bmp180_readRegister16(BMP180_CALIBRATION_REG+2, &c->AC2) || - bmp180_readRegister16(BMP180_CALIBRATION_REG+4, &c->AC3) || - bmp180_readRegister16(BMP180_CALIBRATION_REG+6, (int16_t *)&c->AC4) || - bmp180_readRegister16(BMP180_CALIBRATION_REG+8, (int16_t *)&c->AC5) || - bmp180_readRegister16(BMP180_CALIBRATION_REG+10, (int16_t *)&c->AC6) || - bmp180_readRegister16(BMP180_CALIBRATION_REG+12, &c->B1) || - bmp180_readRegister16(BMP180_CALIBRATION_REG+14, &c->B2) || - bmp180_readRegister16(BMP180_CALIBRATION_REG+16, &c->MB) || - bmp180_readRegister16(BMP180_CALIBRATION_REG+18, &c->MC) || - bmp180_readRegister16(BMP180_CALIBRATION_REG+20, &c->MD)) { + if (bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+0, &c->AC1) || + bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+2, &c->AC2) || + bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+4, &c->AC3) || + bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+6, (int16_t *)&c->AC4) || + bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+8, (int16_t *)&c->AC5) || + bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+10, (int16_t *)&c->AC6) || + bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+12, &c->B1) || + bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+14, &c->B2) || + bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+16, &c->MB) || + bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+18, &c->MC) || + bmp180_readRegister16(dev, BMP180_CALIBRATION_REG+20, &c->MD)) { return false; } @@ -139,16 +135,16 @@ bool bmp180_fillInternalConstants(bmp180_constants_t *c) 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 reg = BMP180_VERSION_REG; - if (i2c_slave_read(BMP180_DEVICE_ADDRESS, ®, &id, 1)) + if (i2c_slave_read(dev->bus, dev->addr, ®, &id, 1)) return false; 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) { int32_t T, P; @@ -160,7 +156,7 @@ bool bmp180_measure(bmp180_constants_t *c, int32_t *temperature, // // Calculation taken from BMP180 Datasheet int32_t UT, X1, X2, B5; - if (!bmp180_get_uncompensated_temperature(&UT)) + if (!bmp180_get_uncompensated_temperature(dev, &UT)) return false; 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; uint32_t B4, B7, UP; - if (!bmp180_get_uncompensated_pressure(oss, &UP)) + if (!bmp180_get_uncompensated_pressure(dev, oss, &UP)) return false; // Calculation taken from BMP180 Datasheet @@ -220,8 +216,8 @@ typedef struct } bmp180_command_t; // Just works due to the fact that QueueHandle_t is a "void *" -static QueueHandle_t bmp180_rx_queue = NULL; -static TaskHandle_t bmp180_task_handle = NULL; +static QueueHandle_t bmp180_rx_queue[MAX_I2C_BUS] = { NULL }; +static TaskHandle_t bmp180_task_handle[MAX_I2C_BUS] = { NULL }; // // Forward declarations @@ -237,6 +233,7 @@ static void bmp180_driver_task(void *pvParameters) // Data to be received from user bmp180_command_t current_command; bmp180_constants_t bmp180_constants; + i2c_dev_t *dev = (i2c_dev_t*)pvParameters; #ifdef BMP180_DEBUG // Wait for commands from the outside @@ -244,14 +241,14 @@ static void bmp180_driver_task(void *pvParameters) #endif // Initialize all internal constants. - if (!bmp180_fillInternalConstants(&bmp180_constants)) { + if (!bmp180_fillInternalConstants(dev, &bmp180_constants)) { printf("%s: reading internal constants failed\n", __FUNCTION__); vTaskDelete(NULL); } while(1) { // Wait for user to insert commands - if (xQueueReceive(bmp180_rx_queue, ¤t_command, portMAX_DELAY) == pdTRUE) { + if (xQueueReceive(bmp180_rx_queue[dev->bus], ¤t_command, portMAX_DELAY) == pdTRUE) { #ifdef BMP180_DEBUG printf("%s: Received user command %d 0x%p\n", __FUNCTION__, current_command.cmd, current_command.resultQueue); #endif @@ -261,7 +258,7 @@ static void bmp180_driver_task(void *pvParameters) int32_t T = 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 ... if (!bmp180_informUser(current_command.resultQueue, 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 - if (bmp180_rx_queue == NULL) - bmp180_rx_queue = xQueueCreate(BMP180_RX_QUEUE_SIZE, sizeof(bmp180_result_t)); + // Just create them once by bus + if (bmp180_rx_queue[dev->bus] == NULL) + 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 portBASE_TYPE x = pdPASS; - if (bmp180_task_handle == NULL) { - x = xTaskCreate(bmp180_driver_task, "bmp180_driver_task", 256, NULL, BMP180_TASK_PRIORITY, &bmp180_task_handle); + if (bmp180_task_handle[dev->bus] == NULL) { + 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; } @@ -309,54 +306,51 @@ static bool bmp180_informUser_Impl(const QueueHandle_t* resultQueue, uint8_t cmd } // 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 bool result = false; - if (bmp180_create_communication_queues()) { - // 2. Init i2c driver - i2c_init(scl, sda); - // 3. Check for bmp180 ... - if (bmp180_is_available()) { - // 4. Start driver task - if (bmp180_createTask()) { + if (bmp180_create_communication_queues(dev)) { + // 2. Check for bmp180 ... + if (bmp180_is_available(dev)) { + // 3. Start driver task + if (bmp180_createTask(dev)) { // We are finished result = true; } } } - 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; c.cmd = BMP180_PRESSURE + BMP180_TEMPERATURE; 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; c.cmd = BMP180_PRESSURE; 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; c.cmd = BMP180_TEMPERATURE; c.resultQueue = resultQueue; - xQueueSend(bmp180_rx_queue, &c, 0); + xQueueSend(bmp180_rx_queue[dev->bus], &c, 0); } diff --git a/extras/bmp180/bmp180.h b/extras/bmp180/bmp180.h index 2f46150..a29a1ae 100644 --- a/extras/bmp180/bmp180.h +++ b/extras/bmp180/bmp180.h @@ -14,9 +14,13 @@ #include "FreeRTOS.h" #include "queue.h" +#include "i2c/i2c.h" + // Uncomment to enable debug output //#define BMP180_DEBUG +#define BMP180_DEVICE_ADDRESS 0x77 + #define BMP180_TEMPERATURE (1<<0) #define BMP180_PRESSURE (1<<1) @@ -42,16 +46,16 @@ typedef struct } bmp180_result_t; // 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) -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) -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) -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 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; // 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. -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 // 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); #ifdef __cplusplus diff --git a/extras/bmp280/bmp280.c b/extras/bmp280/bmp280.c index a106b07..abff6fd 100644 --- a/extras/bmp280/bmp280.c +++ b/extras/bmp280/bmp280.c @@ -23,7 +23,6 @@ */ #include #include "bmp280.h" -#include "i2c/i2c.h" #ifdef BMP280_DEBUG #include @@ -66,37 +65,36 @@ void bmp280_init_default_params(bmp280_params_t *params) params->standby = BMP280_STANDBY_250; } -static bool read_register16(uint8_t i2c_addr, uint8_t addr, uint16_t *value) +static bool read_register16(i2c_dev_t* dev, uint8_t addr, uint16_t *value) { uint8_t d[] = {0, 0}; - if (!i2c_slave_read(i2c_addr, &addr, d, sizeof(d))) { + if (!i2c_slave_read(dev->bus, dev->addr, &addr, d, sizeof(d))) { *value = d[0] | (d[1] << 8); return true; } return false; } -static inline int read_data(uint8_t i2c_addr, uint8_t addr, uint8_t *value, uint8_t len) +static inline int read_data(i2c_dev_t* dev, uint8_t addr, uint8_t *value, uint8_t len) { - return i2c_slave_read(i2c_addr, &addr, value, len); + return i2c_slave_read(dev->bus, dev->addr, &addr, value, len); } static bool read_calibration_data(bmp280_t *dev) { - uint8_t i2c_addr = dev->i2c_addr; - if (read_register16(i2c_addr, 0x88, &dev->dig_T1) && - read_register16(i2c_addr, 0x8a, (uint16_t *)&dev->dig_T2) && - read_register16(i2c_addr, 0x8c, (uint16_t *)&dev->dig_T3) && - read_register16(i2c_addr, 0x8e, &dev->dig_P1) && - read_register16(i2c_addr, 0x90, (uint16_t *)&dev->dig_P2) && - read_register16(i2c_addr, 0x92, (uint16_t *)&dev->dig_P3) && - read_register16(i2c_addr, 0x94, (uint16_t *)&dev->dig_P4) && - read_register16(i2c_addr, 0x96, (uint16_t *)&dev->dig_P5) && - read_register16(i2c_addr, 0x98, (uint16_t *)&dev->dig_P6) && - read_register16(i2c_addr, 0x9a, (uint16_t *)&dev->dig_P7) && - read_register16(i2c_addr, 0x9c, (uint16_t *)&dev->dig_P8) && - read_register16(i2c_addr, 0x9e, (uint16_t *)&dev->dig_P9)) { + if (read_register16(&dev->i2c_dev, 0x88, &dev->dig_T1) && + read_register16(&dev->i2c_dev, 0x8a, (uint16_t *)&dev->dig_T2) && + read_register16(&dev->i2c_dev, 0x8c, (uint16_t *)&dev->dig_T3) && + read_register16(&dev->i2c_dev, 0x8e, &dev->dig_P1) && + read_register16(&dev->i2c_dev, 0x90, (uint16_t *)&dev->dig_P2) && + read_register16(&dev->i2c_dev, 0x92, (uint16_t *)&dev->dig_P3) && + read_register16(&dev->i2c_dev, 0x94, (uint16_t *)&dev->dig_P4) && + read_register16(&dev->i2c_dev, 0x96, (uint16_t *)&dev->dig_P5) && + read_register16(&dev->i2c_dev, 0x98, (uint16_t *)&dev->dig_P6) && + read_register16(&dev->i2c_dev, 0x9a, (uint16_t *)&dev->dig_P7) && + read_register16(&dev->i2c_dev, 0x9c, (uint16_t *)&dev->dig_P8) && + read_register16(&dev->i2c_dev, 0x9e, (uint16_t *)&dev->dig_P9)) { debug("Calibration data received:"); debug("dig_T1=%d", dev->dig_T1); @@ -120,15 +118,14 @@ static bool read_calibration_data(bmp280_t *dev) static bool read_hum_calibration_data(bmp280_t *dev) { - uint8_t i2c_addr = dev->i2c_addr; uint16_t h4, h5; - if (!read_data(i2c_addr, 0xa1, &dev->dig_H1, 1) && - read_register16(i2c_addr, 0xe1, (uint16_t *)&dev->dig_H2) && - !read_data(i2c_addr, 0xe3, &dev->dig_H3, 1) && - read_register16(i2c_addr, 0xe4, &h4) && - read_register16(i2c_addr, 0xe5, &h5) && - !read_data(i2c_addr, 0xe7, (uint8_t *)&dev->dig_H6, 1)) { + if (!read_data(&dev->i2c_dev, 0xa1, &dev->dig_H1, 1) && + read_register16(&dev->i2c_dev, 0xe1, (uint16_t *)&dev->dig_H2) && + !read_data(&dev->i2c_dev, 0xe3, &dev->dig_H3, 1) && + read_register16(&dev->i2c_dev, 0xe4, &h4) && + read_register16(&dev->i2c_dev, 0xe5, &h5) && + !read_data(&dev->i2c_dev, 0xe7, (uint8_t *)&dev->dig_H6, 1)) { dev->dig_H4 = (h4 & 0x00ff) << 4 | (h4 & 0x0f00) >> 8; dev->dig_H5 = h5 >> 4; debug("Calibration data received:"); @@ -144,21 +141,20 @@ static bool read_hum_calibration_data(bmp280_t *dev) return false; } -static int write_register8(uint8_t i2c_addr, uint8_t addr, uint8_t value) +static int write_register8(i2c_dev_t* dev, uint8_t addr, uint8_t value) { - return i2c_slave_write(i2c_addr, &addr, &value, 1); + return i2c_slave_write(dev->bus, dev->addr, &addr, &value, 1); } bool bmp280_init(bmp280_t *dev, bmp280_params_t *params) { - uint8_t i2c_addr = dev->i2c_addr; - if (i2c_addr != BMP280_I2C_ADDRESS_0 && i2c_addr != BMP280_I2C_ADDRESS_1) { + if (dev->i2c_dev.addr != BMP280_I2C_ADDRESS_0 && dev->i2c_dev.addr != BMP280_I2C_ADDRESS_1) { debug("Invalid I2C address"); return false; } - if (read_data(i2c_addr, BMP280_REG_ID, &dev->id, 1)) { + if (read_data(&dev->i2c_dev, BMP280_REG_ID, &dev->id, 1)) { debug("Sensor not found"); return false; } @@ -169,7 +165,7 @@ bool bmp280_init(bmp280_t *dev, bmp280_params_t *params) } // Soft reset. - if (write_register8(i2c_addr, BMP280_REG_RESET, BMP280_RESET_VALUE)) { + if (write_register8(&dev->i2c_dev, BMP280_REG_RESET, BMP280_RESET_VALUE)) { debug("Failed resetting sensor"); return false; } @@ -177,7 +173,7 @@ bool bmp280_init(bmp280_t *dev, bmp280_params_t *params) // Wait until finished copying over the NVP data. while (1) { uint8_t status; - if (!read_data(i2c_addr, BMP280_REG_STATUS, &status, 1) && (status & 1) == 0) + if (!read_data(&dev->i2c_dev, BMP280_REG_STATUS, &status, 1) && (status & 1) == 0) break; } @@ -193,7 +189,7 @@ bool bmp280_init(bmp280_t *dev, bmp280_params_t *params) uint8_t config = (params->standby << 5) | (params->filter << 2); debug("Writing config reg=%x", config); - if (write_register8(i2c_addr, BMP280_REG_CONFIG, config)) { + if (write_register8(&dev->i2c_dev, BMP280_REG_CONFIG, config)) { debug("Failed configuring sensor"); return false; } @@ -210,14 +206,14 @@ bool bmp280_init(bmp280_t *dev, bmp280_params_t *params) // Write crtl hum reg first, only active after write to BMP280_REG_CTRL. uint8_t ctrl_hum = params->oversampling_humidity; debug("Writing ctrl hum reg=%x", ctrl_hum); - if (write_register8(i2c_addr, BMP280_REG_CTRL_HUM, ctrl_hum)) { + if (write_register8(&dev->i2c_dev, BMP280_REG_CTRL_HUM, ctrl_hum)) { debug("Failed controlling sensor"); return false; } } debug("Writing ctrl reg=%x", ctrl); - if (write_register8(i2c_addr, BMP280_REG_CTRL, ctrl)) { + if (write_register8(&dev->i2c_dev, BMP280_REG_CTRL, ctrl)) { debug("Failed controlling sensor"); return false; } @@ -228,12 +224,12 @@ bool bmp280_init(bmp280_t *dev, bmp280_params_t *params) bool bmp280_force_measurement(bmp280_t *dev) { uint8_t ctrl; - if (read_data(dev->i2c_addr, BMP280_REG_CTRL, &ctrl, 1)) + if (read_data(&dev->i2c_dev, BMP280_REG_CTRL, &ctrl, 1)) return false; ctrl &= ~0b11; // clear two lower bits ctrl |= BMP280_MODE_FORCED; debug("Writing ctrl reg=%x", ctrl); - if (write_register8(dev->i2c_addr, BMP280_REG_CTRL, ctrl)) { + if (write_register8(&dev->i2c_dev, BMP280_REG_CTRL, ctrl)) { debug("Failed starting forced mode"); return false; } @@ -243,7 +239,7 @@ bool bmp280_force_measurement(bmp280_t *dev) bool bmp280_is_measuring(bmp280_t *dev) { uint8_t status; - if (read_data(dev->i2c_addr, BMP280_REG_STATUS, &status, 1)) + if (read_data(&dev->i2c_dev, BMP280_REG_STATUS, &status, 1)) return false; if (status & (1 << 3)) { debug("Status: measuring"); @@ -345,7 +341,7 @@ bool bmp280_read_fixed(bmp280_t *dev, int32_t *temperature, // Need to read in one sequence to ensure they match. size_t size = humidity ? 8 : 6; - if (read_data(dev->i2c_addr, 0xf7, data, size)) { + if (read_data(&dev->i2c_dev, 0xf7, data, size)) { debug("Failed reading"); return false; } diff --git a/extras/bmp280/bmp280.h b/extras/bmp280/bmp280.h index 4646d25..39805fc 100644 --- a/extras/bmp280/bmp280.h +++ b/extras/bmp280/bmp280.h @@ -26,6 +26,7 @@ #include #include +#include "i2c/i2c.h" #ifdef __cplusplus extern "C" { @@ -127,7 +128,7 @@ typedef struct { int16_t dig_H5; int8_t dig_H6; - uint8_t i2c_addr; /* I2C address. */ + i2c_dev_t i2c_dev; /* I2C dev setting. */ uint8_t id; /* Chip ID */ } bmp280_t; diff --git a/extras/ds1307/ds1307.c b/extras/ds1307/ds1307.c index f0f52c2..7cee34a 100644 --- a/extras/ds1307/ds1307.c +++ b/extras/ds1307/ds1307.c @@ -6,10 +6,8 @@ * BSD Licensed as described in the file LICENSE */ #include "ds1307.h" -#include #include -#define ADDR 0x68 #define RAM_SIZE 56 #define TIME_REG 0 @@ -40,36 +38,36 @@ static uint8_t dec2bcd(uint8_t val) return ((val / 10) << 4) + (val % 10); } -static uint8_t read_register(uint8_t reg) +static uint8_t read_register(i2c_dev_t* dev, uint8_t reg) { uint8_t val; - i2c_slave_read(ADDR, ®, &val, 1); + i2c_slave_read(dev->bus, dev->addr, ®, &val, 1); return val; } -static void update_register(uint8_t reg, uint8_t mask, uint8_t val) +static void update_register(i2c_dev_t* dev, uint8_t reg, uint8_t mask, uint8_t val) { - uint8_t buf = (read_register(reg) & mask) | val; + uint8_t buf = (read_register(dev,reg) & mask) | val; - i2c_slave_write(ADDR, ®, &buf, 1); + i2c_slave_write(dev->bus, dev->addr, ®, &buf, 1); } -void ds1307_start(bool start) +void ds1307_start(i2c_dev_t* dev, bool start) { - update_register(TIME_REG, CH_MASK, start ? 0 : CH_BIT); + update_register(dev, TIME_REG, CH_MASK, start ? 0 : CH_BIT); } -bool ds1307_is_running() +bool ds1307_is_running(i2c_dev_t* dev) { - return !(read_register(TIME_REG) & CH_BIT); + return !(read_register(dev, TIME_REG) & CH_BIT); } -void ds1307_get_time(struct tm *time) +void ds1307_get_time(i2c_dev_t* dev, struct tm *time) { uint8_t buf[7]; uint8_t reg = TIME_REG ; - i2c_slave_read(ADDR, ® , buf, 7); + i2c_slave_read(dev->bus, dev->addr, ® , buf, 7); time->tm_sec = bcd2dec(buf[0] & SECONDS_MASK); time->tm_min = bcd2dec(buf[1]); @@ -87,7 +85,7 @@ void ds1307_get_time(struct tm *time) time->tm_year = bcd2dec(buf[6]) + 2000; } -void ds1307_set_time(const struct tm *time) +void ds1307_set_time(i2c_dev_t* dev, const struct tm *time) { uint8_t buf[8]; buf[0] = TIME_REG; @@ -99,51 +97,51 @@ void ds1307_set_time(const struct tm *time) buf[6] = dec2bcd(time->tm_mon + 1); buf[7] = dec2bcd(time->tm_year - 2000); - i2c_slave_write(ADDR, &buf[0], &buf[1] , 7); + i2c_slave_write(dev->bus, dev->addr, &buf[0], &buf[1] , 7); } -void ds1307_enable_squarewave(bool enable) +void ds1307_enable_squarewave(i2c_dev_t* dev, bool enable) { - update_register(CONTROL_REG, SQWE_MASK, enable ? SQWE_BIT : 0); + update_register(dev, CONTROL_REG, SQWE_MASK, enable ? SQWE_BIT : 0); } -bool ds1307_is_squarewave_enabled() +bool ds1307_is_squarewave_enabled(i2c_dev_t* dev) { - return read_register(CONTROL_REG) & SQWE_BIT; + return read_register(dev, CONTROL_REG) & SQWE_BIT; } -void ds1307_set_squarewave_freq(ds1307_squarewave_freq_t freq) +void ds1307_set_squarewave_freq(i2c_dev_t* dev, ds1307_squarewave_freq_t freq) { - update_register(CONTROL_REG, SQWEF_MASK, (uint8_t)freq); + update_register(dev, CONTROL_REG, SQWEF_MASK, (uint8_t)freq); } -ds1307_squarewave_freq_t ds1307_get_squarewave_freq() +ds1307_squarewave_freq_t ds1307_get_squarewave_freq(i2c_dev_t* dev) { - return (ds1307_squarewave_freq_t)(read_register(CONTROL_REG) & SQWEF_MASK); + return (ds1307_squarewave_freq_t)(read_register(dev, CONTROL_REG) & SQWEF_MASK); } -bool ds1307_get_output() +bool ds1307_get_output(i2c_dev_t* dev) { - return read_register(CONTROL_REG) & OUT_BIT; + return read_register(dev, CONTROL_REG) & OUT_BIT; } -void ds1307_set_output(bool value) +void ds1307_set_output(i2c_dev_t* dev, bool value) { - update_register(CONTROL_REG, OUT_MASK, value ? OUT_BIT : 0); + update_register(dev, CONTROL_REG, OUT_MASK, value ? OUT_BIT : 0); } -int ds1307_read_ram(uint8_t offset, uint8_t *buf, uint8_t len) +int ds1307_read_ram(i2c_dev_t* dev, uint8_t offset, uint8_t *buf, uint8_t len) { if (offset + len > RAM_SIZE) return false; uint8_t reg = RAM_REG + offset ; - return i2c_slave_read(ADDR, ®, buf, len); + return i2c_slave_read(dev->bus, dev->addr, ®, buf, len); } -int ds1307_write_ram(uint8_t offset, uint8_t *buf, uint8_t len) +int ds1307_write_ram(i2c_dev_t* dev, uint8_t offset, uint8_t *buf, uint8_t len) { if (offset + len > RAM_SIZE) return false; uint8_t reg = RAM_REG + offset ; - return i2c_slave_write(ADDR, ®, buf, len); + return i2c_slave_write(dev->bus, dev->addr, ®, buf, len); } diff --git a/extras/ds1307/ds1307.h b/extras/ds1307/ds1307.h index 5af38ae..9baa904 100644 --- a/extras/ds1307/ds1307.h +++ b/extras/ds1307/ds1307.h @@ -11,11 +11,13 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { #endif +#define DS1307_ADDR 0x68 /** * Squarewave frequency */ @@ -31,62 +33,62 @@ typedef enum _ds1307_squarewave_freq_t * \brief Start/stop clock * \param start Start clock if true */ -void ds1307_start(bool start); +void ds1307_start(i2c_dev_t* dev, bool start); /** * \brief Get current clock state * \return true if clock running */ -bool ds1307_is_running(); +bool ds1307_is_running(i2c_dev_t* dev); /** * \brief Get current time * \param time Pointer to the time struct to fill */ -void ds1307_get_time(struct tm *time); +void ds1307_get_time(i2c_dev_t* dev, struct tm *time); /** * \brief Set time to RTC * \param time Pointer to the time struct */ -void ds1307_set_time(const struct tm *time); +void ds1307_set_time(i2c_dev_t* dev, const struct tm *time); /** * \brief Enable or disable square-wave oscillator output * \param enable Enable oscillator if true */ -void ds1307_enable_squarewave(bool enable); +void ds1307_enable_squarewave(i2c_dev_t* dev, bool enable); /** * \brief Get square-wave oscillator output * \return true if square-wave oscillator enabled */ -bool ds1307_is_squarewave_enabled(); +bool ds1307_is_squarewave_enabled(i2c_dev_t* dev); /** * \brief Set square-wave oscillator frequency * \param freq Frequency */ -void ds1307_set_squarewave_freq(ds1307_squarewave_freq_t freq); +void ds1307_set_squarewave_freq(i2c_dev_t* dev, ds1307_squarewave_freq_t freq); /** * \brief Get current square-wave oscillator frequency * \return Frequency */ -ds1307_squarewave_freq_t ds1307_get_squarewave_freq(); +ds1307_squarewave_freq_t ds1307_get_squarewave_freq(i2c_dev_t* dev); /** * \brief Get current output level of the SQW/OUT pin * \return true if high */ -bool ds1307_get_output(); +bool ds1307_get_output(i2c_dev_t* dev); /** * \brief Set output level of the SQW/OUT pin * Set output level if square-wave output is disabled * \param value High level if true */ -void ds1307_set_output(bool value); +void ds1307_set_output(i2c_dev_t* dev, bool value); /** * \brief Read RAM contents into the buffer @@ -95,7 +97,7 @@ void ds1307_set_output(bool value); * \param len Bytes to read, 1..56 * \return Non-zero if error occured */ -int ds1307_read_ram(uint8_t offset, uint8_t *buf, uint8_t len); +int ds1307_read_ram(i2c_dev_t* dev, uint8_t offset, uint8_t *buf, uint8_t len); /** * \brief Write buffer to RTC RAM @@ -104,7 +106,7 @@ int ds1307_read_ram(uint8_t offset, uint8_t *buf, uint8_t len); * \param len Bytes to write, 1..56 * \return Non-zero if error occured */ -int ds1307_write_ram(uint8_t offset, uint8_t *buf, uint8_t len); +int ds1307_write_ram(i2c_dev_t* dev, uint8_t offset, uint8_t *buf, uint8_t len); #ifdef __cplusplus diff --git a/extras/ds3231/ds3231.c b/extras/ds3231/ds3231.c index 4de534c..dfc5757 100644 --- a/extras/ds3231/ds3231.c +++ b/extras/ds3231/ds3231.c @@ -11,7 +11,6 @@ #include "espressif/sdk_private.h" #include "esp8266.h" -#include "i2c/i2c.h" /* Convert normal decimal to binary coded decimal */ 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 * 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, ®, data, len); + return i2c_slave_write(dev->bus, dev->addr, ®, data, len); } /* Read a number of bytes from the rtc over i2c * 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, ®, data, len); + return i2c_slave_read(dev->bus, dev->addr, ®, data, len); } -int ds3231_setTime(struct tm *time) +int ds3231_setTime(i2c_dev_t *dev, struct tm *time) { uint8_t data[7]; @@ -54,10 +53,10 @@ int ds3231_setTime(struct tm *time) data[5] = decToBcd(time->tm_mon + 1); 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; 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)); } - 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 @@ -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 * 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; /* get register */ - if (!ds3231_recv(addr, &data, 1)) + if (!ds3231_recv(dev, addr, &data, 1)) { /* return only requested flag */ *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 * 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; /* get status register */ - if (!ds3231_recv(addr, &data, 1)) + if (!ds3231_recv(dev, addr, &data, 1)) { /* clear the flag */ if (mode == DS3231_REPLACE) @@ -127,18 +126,18 @@ bool ds3231_setFlag(uint8_t addr, uint8_t bits, uint8_t mode) else data &= ~bits; - if (!ds3231_send(addr, &data, 1)) + if (!ds3231_send(dev, addr, &data, 1)) return true; } return false; } -bool ds3231_getOscillatorStopFlag(bool *flag) +bool ds3231_getOscillatorStopFlag(i2c_dev_t *dev, bool *flag) { 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); return true; @@ -147,76 +146,76 @@ bool ds3231_getOscillatorStopFlag(bool *flag) 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 * 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; - if (ds3231_getFlag(DS3231_ADDR_CONTROL, 0xff, &flag)) + if (ds3231_getFlag(dev, DS3231_ADDR_CONTROL, 0xff, &flag)) { /* clear current rate */ flag &= ~DS3231_CTRL_SQWAVE_8192HZ; /* set new rate */ flag |= freq; - return ds3231_setFlag(DS3231_ADDR_CONTROL, flag, DS3231_REPLACE); + return ds3231_setFlag(dev, DS3231_ADDR_CONTROL, flag, DS3231_REPLACE); } return false; } -bool ds3231_getRawTemp(int16_t *temp) +bool ds3231_getRawTemp(i2c_dev_t *dev, int16_t *temp) { uint8_t data[2]; 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; return true; @@ -225,11 +224,11 @@ bool ds3231_getRawTemp(int16_t *temp) return false; } -bool ds3231_getTempInteger(int8_t *temp) +bool ds3231_getTempInteger(i2c_dev_t *dev, int8_t *temp) { int16_t tInt; - if (ds3231_getRawTemp(&tInt)) { + if (ds3231_getRawTemp(dev, &tInt)) { *temp = tInt >> 2; return true; } @@ -237,11 +236,11 @@ bool ds3231_getTempInteger(int8_t *temp) return false; } -bool ds3231_getTempFloat(float *temp) +bool ds3231_getTempFloat(i2c_dev_t *dev, float *temp) { int16_t tInt; - if (ds3231_getRawTemp(&tInt)) { + if (ds3231_getRawTemp(dev, &tInt)) { *temp = tInt * 0.25; return true; } @@ -249,12 +248,12 @@ bool ds3231_getTempFloat(float *temp) return false; } -bool ds3231_getTime(struct tm *time) +bool ds3231_getTime(i2c_dev_t *dev, struct tm *time) { uint8_t data[7]; /* read time */ - if (ds3231_recv(DS3231_ADDR_TIME, data, 7)) + if (ds3231_recv(dev, DS3231_ADDR_TIME, data, 7)) { return false; } @@ -281,10 +280,5 @@ bool ds3231_getTime(struct tm *time) //applyTZ(time); return true; - -} -void ds3231_Init(uint8_t scl, uint8_t sda) -{ - i2c_init(scl, sda); } diff --git a/extras/ds3231/ds3231.h b/extras/ds3231/ds3231.h index 40ff596..6ef9ff7 100644 --- a/extras/ds3231/ds3231.h +++ b/extras/ds3231/ds3231.h @@ -12,6 +12,8 @@ #include #include #include +#include "i2c/i2c.h" + #ifdef __cplusplus extern "C" { @@ -87,7 +89,7 @@ enum { * I suggest using GMT and applying timezone and DST when read back * returns true to indicate success */ -int ds3231_setTime(struct tm *time); +int ds3231_setTime(i2c_dev_t *dev, struct tm *time); /* Set alarms * 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 * 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 * sets flag to true if there has been a stop * returns true to indicate success */ -bool ds3231_getOscillatorStopFlag(bool *flag); +bool ds3231_getOscillatorStopFlag(i2c_dev_t *dev, bool *flag); /* Clear the oscillator stopped flag * returns true to indicate success */ -bool ds3231_clearOscillatorStopFlag(); +bool ds3231_clearOscillatorStopFlag(i2c_dev_t *dev); /* Check which alarm(s) have past * sets alarms to DS3231_ALARM_NONE/DS3231_ALARM_1/DS3231_ALARM_2/DS3231_ALARM_BOTH * 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) * pass DS3231_ALARM_1/DS3231_ALARM_2/DS3231_ALARM_BOTH * 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) * 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 * 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) * pass DS3231_ALARM_1/DS3231_ALARM_2/DS3231_ALARM_BOTH * 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 * returns true to indicate success */ -bool ds3231_enable32khz(); +bool ds3231_enable32khz(i2c_dev_t *dev); /* Disable the output of 32khz signal * returns true to indicate success */ -bool ds3231_disable32khz(); +bool ds3231_disable32khz(i2c_dev_t *dev); /* Enable the squarewave output (disables alarm interrupt functionality) * 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 * alarm interrupts also need to be enabled, if not already, before they will trigger) * 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) * pass DS3231_SQUAREWAVE_RATE_1HZ/DS3231_SQUAREWAVE_RATE_1024HZ/DS3231_SQUAREWAVE_RATE_4096HZ/DS3231_SQUAREWAVE_RATE_8192HZ * 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 * 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 * 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) * 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 * returns true to indicate success */ -bool ds3231_getTime(struct tm *time); -void ds3231_Init(uint8_t scl, uint8_t sda); +bool ds3231_getTime(i2c_dev_t *dev, struct tm *time); #ifdef __cplusplus } diff --git a/extras/hd44780/hd44780.c b/extras/hd44780/hd44780.c index b44f65a..d7bbc0a 100644 --- a/extras/hd44780/hd44780.c +++ b/extras/hd44780/hd44780.c @@ -60,8 +60,8 @@ static void write_nibble(const hd44780_t *lcd, uint8_t b, bool rs) | (rs ? 1 << lcd->pins.rs : 0) | (lcd->backlight ? 1 << lcd->pins.bl : 0); - pcf8574_port_write(lcd->addr, data | (1 << lcd->pins.e)); - pcf8574_port_write(lcd->addr, data); + pcf8574_port_write(&lcd->i2c_dev, data | (1 << lcd->pins.e)); + pcf8574_port_write(&lcd->i2c_dev, data); #else gpio_write(lcd->pins.d7, (b >> 3) & 1); gpio_write(lcd->pins.d6, (b >> 2) & 1); @@ -164,7 +164,7 @@ void hd44780_set_backlight(hd44780_t *lcd, bool on) return; #if (HD44780_I2C) - pcf8574_gpio_write(lcd->addr, lcd->pins.bl, on); + pcf8574_gpio_write(&lcd->i2c_dev, lcd->pins.bl, on); #else gpio_write(lcd->pins.bl, on); #endif diff --git a/extras/hd44780/hd44780.h b/extras/hd44780/hd44780.h index a00302b..c5152de 100644 --- a/extras/hd44780/hd44780.h +++ b/extras/hd44780/hd44780.h @@ -12,7 +12,10 @@ #include #ifndef HD44780_I2C -#define HD44780_I2C 0 +#define HD44780_I2C 1 +#endif +#if (HD44780_I2C) +#include #endif #ifdef __cplusplus @@ -36,7 +39,9 @@ typedef enum */ typedef struct { - uint8_t addr; //!< PCF8574 address (0b0100) +#if (HD44780_I2C) + i2c_dev_t i2c_dev; //!< PCF8574 device settings (0b0100) +#endif struct { uint8_t rs; //!< gpio/register bit used for RS pin diff --git a/extras/hmc5883l/hmc5883l.c b/extras/hmc5883l/hmc5883l.c index 265258d..e970fa2 100644 --- a/extras/hmc5883l/hmc5883l.c +++ b/extras/hmc5883l/hmc5883l.c @@ -6,10 +6,8 @@ * BSD Licensed as described in the file LICENSE */ #include "hmc5883l.h" -#include #include -#define ADDR 0x1e #define REG_CR_A 0x00 #define REG_CR_B 0x01 @@ -54,112 +52,112 @@ static const float gain_values [] = { static float current_gain; static hmc5883l_operating_mode_t current_mode; -static inline void write_register(uint8_t reg, uint8_t val) +static inline void write_register(i2c_dev_t* dev, uint8_t reg, uint8_t val) { - i2c_slave_write(ADDR, ®, &val, 1); + i2c_slave_write(dev->bus, dev->addr, ®, &val, 1); } -static inline uint8_t read_register(uint8_t reg) +static inline uint8_t read_register(i2c_dev_t* dev, uint8_t reg) { uint8_t res; - i2c_slave_read(ADDR, ®, &res, 1); + i2c_slave_read(dev->bus, dev->addr, ®, &res, 1); return res; } -static inline void update_register(uint8_t reg, uint8_t mask, uint8_t val) +static inline void update_register(i2c_dev_t* dev, uint8_t reg, uint8_t mask, uint8_t val) { - write_register(reg, (read_register(reg) & mask) | val); + write_register(dev, reg, (read_register(dev, reg) & mask) | val); } -bool hmc5883l_init() +bool hmc5883l_init(i2c_dev_t* dev) { - if (hmc5883l_get_id() != HMC5883L_ID) + if (hmc5883l_get_id(dev) != HMC5883L_ID) return false; - current_gain = gain_values[hmc5883l_get_gain()]; - current_mode = hmc5883l_get_operating_mode(); + current_gain = gain_values[hmc5883l_get_gain(dev)]; + current_mode = hmc5883l_get_operating_mode(dev); return true; } -uint32_t hmc5883l_get_id() +uint32_t hmc5883l_get_id(i2c_dev_t* dev) { uint32_t res = 0; uint8_t reg = REG_ID_A; - i2c_slave_read(ADDR, ®, (uint8_t *)&res, 3); + i2c_slave_read(dev->bus, dev->addr, ®, (uint8_t *)&res, 3); return res; } -hmc5883l_operating_mode_t hmc5883l_get_operating_mode() +hmc5883l_operating_mode_t hmc5883l_get_operating_mode(i2c_dev_t* dev) { - uint8_t res = read_register(REG_MODE) & MASK_MD; + uint8_t res = read_register(dev, REG_MODE) & MASK_MD; return res == 0 ? HMC5883L_MODE_CONTINUOUS : HMC5883L_MODE_SINGLE; } -void hmc5883l_set_operating_mode(hmc5883l_operating_mode_t mode) +void hmc5883l_set_operating_mode(i2c_dev_t* dev, hmc5883l_operating_mode_t mode) { - write_register(REG_MODE, mode); + write_register(dev, REG_MODE, mode); current_mode = mode; } -hmc5883l_samples_averaged_t hmc5883l_get_samples_averaged() +hmc5883l_samples_averaged_t hmc5883l_get_samples_averaged(i2c_dev_t* dev) { - return (read_register(REG_CR_A) & MASK_MA) >> BIT_MA; + return (read_register(dev, REG_CR_A) & MASK_MA) >> BIT_MA; } -void hmc5883l_set_samples_averaged(hmc5883l_samples_averaged_t samples) +void hmc5883l_set_samples_averaged(i2c_dev_t* dev, hmc5883l_samples_averaged_t samples) { - update_register(REG_CR_A, MASK_MA, samples << BIT_MA); + update_register(dev, REG_CR_A, MASK_MA, samples << BIT_MA); } -hmc5883l_data_rate_t hmc5883l_get_data_rate() +hmc5883l_data_rate_t hmc5883l_get_data_rate(i2c_dev_t* dev) { - return (read_register(REG_CR_A) & MASK_DO) >> BIT_DO; + return (read_register(dev, REG_CR_A) & MASK_DO) >> BIT_DO; } -void hmc5883l_set_data_rate(hmc5883l_data_rate_t rate) +void hmc5883l_set_data_rate(i2c_dev_t* dev, hmc5883l_data_rate_t rate) { - update_register(REG_CR_A, MASK_DO, rate << BIT_DO); + update_register(dev, REG_CR_A, MASK_DO, rate << BIT_DO); } -hmc5883l_bias_t hmc5883l_get_bias() +hmc5883l_bias_t hmc5883l_get_bias(i2c_dev_t* dev) { - return read_register(REG_CR_A) & MASK_MS; + return read_register(dev, REG_CR_A) & MASK_MS; } -void hmc5883l_set_bias(hmc5883l_bias_t bias) +void hmc5883l_set_bias(i2c_dev_t* dev, hmc5883l_bias_t bias) { - update_register(REG_CR_A, MASK_MS, bias); + update_register(dev, REG_CR_A, MASK_MS, bias); } -hmc5883l_gain_t hmc5883l_get_gain() +hmc5883l_gain_t hmc5883l_get_gain(i2c_dev_t* dev) { - return read_register(REG_CR_B) >> BIT_GN; + return read_register(dev, REG_CR_B) >> BIT_GN; } -void hmc5883l_set_gain(hmc5883l_gain_t gain) +void hmc5883l_set_gain(i2c_dev_t* dev, hmc5883l_gain_t gain) { - write_register(REG_CR_B, gain << BIT_GN); + write_register(dev, REG_CR_B, gain << BIT_GN); current_gain = gain_values[gain]; } -bool hmc5883l_data_is_locked() +bool hmc5883l_data_is_locked(i2c_dev_t* dev) { - return read_register(REG_STAT) & MASK_DL; + return read_register(dev, REG_STAT) & MASK_DL; } -bool hmc5883l_data_is_ready() +bool hmc5883l_data_is_ready(i2c_dev_t* dev) { - return read_register(REG_STAT) & MASK_DR; + return read_register(dev, REG_STAT) & MASK_DR; } -bool hmc5883l_get_raw_data(hmc5883l_raw_data_t *data) +bool hmc5883l_get_raw_data(i2c_dev_t* dev, hmc5883l_raw_data_t *data) { if (current_mode == HMC5883L_MODE_SINGLE) { // overwrite mode register for measurement - hmc5883l_set_operating_mode(current_mode); + hmc5883l_set_operating_mode(dev, current_mode); // wait for data uint32_t start = sdk_system_get_time(); - while (!hmc5883l_data_is_ready()) + while (!hmc5883l_data_is_ready(dev)) { if (timeout_expired(start, MEASUREMENT_TIMEOUT)) return false; @@ -167,7 +165,7 @@ bool hmc5883l_get_raw_data(hmc5883l_raw_data_t *data) } uint8_t buf[6]; uint8_t reg = REG_DX_H; - i2c_slave_read(ADDR, ®, buf, 6); + i2c_slave_read(dev->bus, dev->addr, ®, buf, 6); data->x = ((int16_t)buf[REG_DX_H - REG_DX_H] << 8) | buf[REG_DX_L - REG_DX_H]; data->y = ((int16_t)buf[REG_DY_H - REG_DX_H] << 8) | buf[REG_DY_L - REG_DX_H]; data->z = ((int16_t)buf[REG_DZ_H - REG_DX_H] << 8) | buf[REG_DZ_L - REG_DX_H]; @@ -181,11 +179,11 @@ void hmc5883l_raw_to_mg(const hmc5883l_raw_data_t *raw, hmc5883l_data_t *mg) mg->z = raw->z * current_gain; } -bool hmc5883l_get_data(hmc5883l_data_t *data) +bool hmc5883l_get_data(i2c_dev_t* dev, hmc5883l_data_t *data) { hmc5883l_raw_data_t raw; - if (!hmc5883l_get_raw_data(&raw)) + if (!hmc5883l_get_raw_data(dev, &raw)) return false; hmc5883l_raw_to_mg(&raw, data); return true; diff --git a/extras/hmc5883l/hmc5883l.h b/extras/hmc5883l/hmc5883l.h index 720a385..5958e61 100644 --- a/extras/hmc5883l/hmc5883l.h +++ b/extras/hmc5883l/hmc5883l.h @@ -10,12 +10,15 @@ #include #include +#include #ifdef __cplusplus extern "C" { #endif +#define HMC5883L_ADDR 0x1e + #define HMC5883L_ID 0x00333448 // "H43" /** @@ -101,82 +104,82 @@ typedef struct * \brief Init device * \return false if error occured */ -bool hmc5883l_init(); +bool hmc5883l_init(i2c_dev_t* dev); /** * \brief Get device ID * Always returns 0x00333448 if IC functioning properly. * \return Device ID */ -uint32_t hmc5883l_get_id(); +uint32_t hmc5883l_get_id(i2c_dev_t* dev); /** * \brief Get operating mode * \return Measurement mode */ -hmc5883l_operating_mode_t hmc5883l_get_operating_mode(); +hmc5883l_operating_mode_t hmc5883l_get_operating_mode(i2c_dev_t* dev); /** * \brief Set operating mode * \param mode Measurement mode */ -void hmc5883l_set_operating_mode(hmc5883l_operating_mode_t mode); +void hmc5883l_set_operating_mode(i2c_dev_t* dev, hmc5883l_operating_mode_t mode); /** * \brief Get number of samples averaged per measurement output * \return Number of samples */ -hmc5883l_samples_averaged_t hmc5883l_get_samples_averaged(); +hmc5883l_samples_averaged_t hmc5883l_get_samples_averaged(i2c_dev_t* dev); /** * \brief Set number of samples averaged per measurement output * \param samples Number of samples */ -void hmc5883l_set_samples_averaged(hmc5883l_samples_averaged_t samples); +void hmc5883l_set_samples_averaged(i2c_dev_t* dev, hmc5883l_samples_averaged_t samples); /** * \brief Get data output rate in continuous measurement mode * \return Data output rate */ -hmc5883l_data_rate_t hmc5883l_get_data_rate(); +hmc5883l_data_rate_t hmc5883l_get_data_rate(i2c_dev_t* dev); /** * \brief Set data output rate in continuous measurement mode * \param rate Data output rate */ -void hmc5883l_set_data_rate(hmc5883l_data_rate_t rate); +void hmc5883l_set_data_rate(i2c_dev_t* dev, hmc5883l_data_rate_t rate); /** * \brief Get measurement mode (bias of the axes) * See datasheet for self test description * \return Bias */ -hmc5883l_bias_t hmc5883l_get_bias(); +hmc5883l_bias_t hmc5883l_get_bias(i2c_dev_t* dev); /** * \brief Set measurement mode (bias of the axes) * See datasheet for self test description * \param bias Bias */ -void hmc5883l_set_bias(hmc5883l_bias_t bias); +void hmc5883l_set_bias(i2c_dev_t* dev, hmc5883l_bias_t bias); /** * \brief Get device gain * \return Current gain */ -hmc5883l_gain_t hmc5883l_get_gain(); +hmc5883l_gain_t hmc5883l_get_gain(i2c_dev_t* dev); /** * \brief Set device gain * \param gain Gain */ -void hmc5883l_set_gain(hmc5883l_gain_t gain); +void hmc5883l_set_gain(i2c_dev_t* dev, hmc5883l_gain_t gain); /** * \brief Get data state * \return true when data is written to all six data registers */ -bool hmc5883l_data_is_ready(); +bool hmc5883l_data_is_ready(i2c_dev_t* dev); /** * \brief Get lock state. @@ -188,14 +191,14 @@ bool hmc5883l_data_is_ready(); * 4. power is reset. * \return true when data registers is locked */ -bool hmc5883l_data_is_locked(); +bool hmc5883l_data_is_locked(i2c_dev_t* dev); /** * \brief Get raw magnetic data * \param data Pointer to the struct to write raw data * \return false if error occured in single measurement mode, always true in continuous mode */ -bool hmc5883l_get_raw_data(hmc5883l_raw_data_t *data); +bool hmc5883l_get_raw_data(i2c_dev_t* dev, hmc5883l_raw_data_t *data); /** * \brief Convert raw magnetic data to milligausses @@ -209,7 +212,7 @@ void hmc5883l_raw_to_mg(const hmc5883l_raw_data_t *raw, hmc5883l_data_t *mg); * \param data Pointer to the struct to write data * \return false if error occured in single measurement mode, always true in continuous mode */ -bool hmc5883l_get_data(hmc5883l_data_t *data); +bool hmc5883l_get_data(i2c_dev_t* dev, hmc5883l_data_t *data); #ifdef __cplusplus } diff --git a/extras/i2c/i2c.c b/extras/i2c/i2c.c index fb875f4..034bcdc 100644 --- a/extras/i2c/i2c.c +++ b/extras/i2c/i2c.c @@ -1,18 +1,18 @@ -/* +/* * The MIT License (MIT) - * + * * Copyright (c) 2015 Johan Kanflo (github.com/kanflo) - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -37,217 +37,219 @@ #define CLK_STRETCH (10) -static bool started; -static bool flag; -static bool force; -static uint8_t freq ; -static uint8_t g_scl_pin; -static uint8_t g_sda_pin; +static uint8_t freq ; // Store CPU frequency for optimisation speed in delay function ( Warning: Don't change CPU frequency during a transaction) +static i2c_bus_description_t i2c_bus[MAX_I2C_BUS]; -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; - flag = false ; - g_scl_pin = scl_pin; - g_sda_pin = sda_pin; + i2c_bus[bus].started = false; + i2c_bus[bus].flag = false ; + i2c_bus[bus].g_scl_pin = scl_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. - gpio_set_pullup(g_scl_pin, 1, 1); - gpio_set_pullup(g_sda_pin, 1, 1); + gpio_set_pullup(i2c_bus[bus].g_scl_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(g_sda_pin, GPIO_OUT_OPEN_DRAIN); + gpio_enable(i2c_bus[bus].g_scl_pin, GPIO_OUT_OPEN_DRAIN); + gpio_enable(i2c_bus[bus].g_sda_pin, GPIO_OUT_OPEN_DRAIN); // I2C bus idle state. - gpio_write(g_scl_pin, 1); - gpio_write(g_sda_pin, 1); + gpio_write(i2c_bus[bus].g_scl_pin, 1); + gpio_write(i2c_bus[bus].g_sda_pin, 1); // Prevent user, if frequency is high 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"); } -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; if (freq == SYS_CPU_160MHZ) { - __asm volatile ( - "movi %0, %1" "\n" - "1: addi %0, %0, -1" "\n" - "bnez %0, 1b" "\n" - : "=a" (delay) : "i" (I2C_CUSTOM_DELAY_160MHZ)); + delay = i2c_freq_array[i2c_bus[bus].frequency][0]; + __asm volatile ( + "1: addi %0, %0, -1" "\n" + "bnez %0, 1b" "\n" + :: "a" (delay)); } else { - __asm volatile ( - "movi %0, %1" "\n" - "1: addi %0, %0, -1" "\n" - "bnez %0, 1b" "\n" - : "=a" (delay) : "i" (I2C_CUSTOM_DELAY_80MHZ)); + delay = i2c_freq_array[i2c_bus[bus].frequency][1]; + __asm volatile ( + "1: addi %0, %0, -1" "\n" + "bnez %0, 1b" "\n" + :: "a" (delay)); } } // Set SCL as input, allowing it to float high, and return current // 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); - return gpio_read(g_scl_pin); // Clock high, valid ACK + gpio_write(i2c_bus[bus].g_scl_pin, 1); + 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 // 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 - i2c_delay(); - return gpio_read(g_sda_pin); // Clock high, valid ACK + i2c_delay(bus); + return gpio_read(i2c_bus[bus].g_sda_pin); // Clock high, valid ACK } // 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 -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 -void i2c_start(void) +void i2c_start(uint8_t bus) { - uint32_t clk_stretch = CLK_STRETCH; 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 - (void) read_sda(); - i2c_delay(); - while (read_scl() == 0 && clk_stretch--) ; + (void) read_sda(bus); + i2c_delay(bus); + uint32_t clk_stretch = CLK_STRETCH; + while (read_scl(bus) == 0 && clk_stretch--) ; // Repeated start setup time, minimum 4.7us - i2c_delay(); + i2c_delay(bus); } - started = true; - if (read_sda() == 0) { - debug("arbitration lost in i2c_start"); + i2c_bus[bus].started = true; + if (read_sda(bus) == 0) { + debug("arbitration lost in i2c_start from bus %u",bus); } // SCL is high, set SDA from 1 to 0. - clear_sda(); - i2c_delay(); - clear_scl(); + clear_sda(bus); + i2c_delay(bus); + clear_scl(bus); } // Output stop condition -bool i2c_stop(void) +bool i2c_stop(uint8_t bus) { uint32_t clk_stretch = CLK_STRETCH; // Set SDA to 0 - clear_sda(); - i2c_delay(); + clear_sda(bus); + i2c_delay(bus); // Clock stretching - while (read_scl() == 0 && clk_stretch--) ; + while (read_scl(bus) == 0 && clk_stretch--) ; // Stop bit setup time, minimum 4us - i2c_delay(); + i2c_delay(bus); // SCL is high, set SDA from 0 to 1 - if (read_sda() == 0) { - debug("arbitration lost in i2c_stop"); + if (read_sda(bus) == 0) { + debug("arbitration lost in i2c_stop from bus %u",bus); } - i2c_delay(); - if (!started) { - debug("link was break!"); + i2c_delay(bus); + if (!i2c_bus[bus].started) { + debug("bus %u link was break!",bus); return false ; //If bus was stop in other way, the current transmission Failed } - started = false; + i2c_bus[bus].started = false; return true; } // 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; if (bit) { - (void) read_sda(); + (void) read_sda(bus); } else { - clear_sda(); + clear_sda(bus); } - i2c_delay(); + i2c_delay(bus); // Clock stretching - while (read_scl() == 0 && clk_stretch--) ; + while (read_scl(bus) == 0 && clk_stretch--) ; // SCL is high, now data is valid // If SDA is high, check that nobody else is driving SDA - if (bit && read_sda() == 0) { - debug("arbitration lost in i2c_write_bit"); + if (bit && read_sda(bus) == 0) { + debug("arbitration lost in i2c_write_bit from bus %u",bus); } - i2c_delay(); - clear_scl(); + i2c_delay(bus); + clear_scl(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; bool bit; // Let the slave drive data - (void) read_sda(); - i2c_delay(); + (void) read_sda(bus); + i2c_delay(bus); // Clock stretching - while (read_scl() == 0 && clk_stretch--) ; + while (read_scl(bus) == 0 && clk_stretch--) ; // SCL is high, now data is valid - bit = read_sda(); - i2c_delay(); - clear_scl(); + bit = read_sda(bus); + i2c_delay(bus); + clear_scl(bus); return bit; } -bool i2c_write(uint8_t byte) +bool i2c_write(uint8_t bus, uint8_t byte) { bool nack; uint8_t bit; for (bit = 0; bit < 8; bit++) { - i2c_write_bit((byte & 0x80) != 0); + i2c_write_bit(bus,(byte & 0x80) != 0); byte <<= 1; } - nack = i2c_read_bit(); + nack = i2c_read_bit(bus); return !nack; } -uint8_t i2c_read(bool ack) +uint8_t i2c_read(uint8_t bus, bool ack) { uint8_t byte = 0; uint8_t 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; } -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! - bool status = flag ; // get current status - if(force) + bool status = i2c_bus[bus].flag ; // get current status + if(i2c_bus[bus].force) { - flag = true ; // force bus on + i2c_bus[bus].flag = true ; // force bus on taskEXIT_CRITICAL(); if(status) - i2c_stop(); //Bus was busy, stop it. + i2c_stop(bus); //Bus was busy, stop it. } else { @@ -260,68 +262,68 @@ static int i2c_bus_test() } else { - flag = true ; // Set Bus busy + i2c_bus[bus].flag = true ; // Set Bus busy taskEXIT_CRITICAL(); } } 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 ; - i2c_start(); - if (!i2c_write(slave_addr << 1)) + i2c_start(bus); + if (!i2c_write(bus, slave_addr << 1)) goto error; if(data != NULL) - if (!i2c_write(*data)) + if (!i2c_write(bus,*data)) goto error; while (len--) { - if (!i2c_write(*buf++)) + if (!i2c_write(bus,*buf++)) goto error; } - if (!i2c_stop()) + if (!i2c_stop(bus)) goto error; - flag = false ; // Bus free + i2c_bus[bus].flag = false ; // Bus free return 0; error: - debug("Write Error"); - i2c_stop(); - flag = false ; // Bus free + debug("Bus %u Write Error",bus); + i2c_stop(bus); + i2c_bus[bus].flag = false ; // Bus free 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 ; if(data != NULL) { - i2c_start(); - if (!i2c_write(slave_addr << 1)) + i2c_start(bus); + if (!i2c_write(bus,slave_addr << 1)) goto error; - if (!i2c_write(*data)) + if (!i2c_write(bus,*data)) goto error; - if (!i2c_stop()) + if (!i2c_stop(bus)) goto error; } - i2c_start(); - if (!i2c_write(slave_addr << 1 | 1)) // Slave address + read + i2c_start(bus); + if (!i2c_write(bus,slave_addr << 1 | 1)) // Slave address + read goto error; while(len) { - *buf = i2c_read(len == 1); + *buf = i2c_read(bus,len == 1); buf++; len--; } - if (!i2c_stop()) + if (!i2c_stop(bus)) goto error; - flag = false ; // Bus free + i2c_bus[bus].flag = false ; // Bus free return 0; error: debug("Read Error"); - i2c_stop(); - flag = false ; // Bus free + i2c_stop(bus); + i2c_bus[bus].flag = false ; // Bus free return -EIO; } diff --git a/extras/i2c/i2c.h b/extras/i2c/i2c.h index b7936ad..c2e31cd 100644 --- a/extras/i2c/i2c.h +++ b/extras/i2c/i2c.h @@ -1,18 +1,18 @@ -/* +/* * The MIT License (MIT) - * + * * Copyright (c) 2015 Johan Kanflo (github.com/kanflo) - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -37,22 +37,47 @@ extern "C" { /* - * Some bit can be transmit slower. - * 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 i2c bus max number */ +#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 // 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 + * @param bus Bus i2c selection * @param scl_pin SCL 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. + * @param bus Bus i2c selection * @param byte Pointer to device descriptor * @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. + * @param bus Bus i2c selection * @param ack Set Ack for slave (false: Ack // true: NoAck) * @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 + * @param bus Bus i2c selection */ -void i2c_start(void); +void i2c_start(uint8_t bus); /** * Send stop condition + * @param bus Bus i2c selection * @return false if link was broken */ -bool i2c_stop(void); +bool i2c_stop(uint8_t bus); /** * get status from I2C bus. + * @param bus Bus i2c selection * @return true if busy. */ -bool i2c_status(void); +bool i2c_status(uint8_t bus); //Level 1 API (Don't need functions above) /** * 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. + * @param bus Bus i2c selection * @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 . + * @param bus Bus i2c selection * @param slave_addr slave device address * @param data Pointer to register address to send if non-null * @param buf Pointer to data buffer * @param len Number of byte to send * @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 * from slave into 'buf'. + * @param bus Bus i2c selection * @param slave_addr slave device address * @param data Pointer to register address to send if non-null * @param buf Pointer to data buffer * @param len Number of byte to read * @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 } diff --git a/extras/ina3221/ina3221.c b/extras/ina3221/ina3221.c index 805b09d..dc79735 100644 --- a/extras/ina3221/ina3221.c +++ b/extras/ina3221/ina3221.c @@ -18,32 +18,32 @@ #define debug(fmt, ...) #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 }; d[1] = value & 0x00FF; d[0] = (value >> 8) & 0x00FF; - debug("Data write to %02X : %02X+%04X\n",addr,reg,value); - return i2c_slave_write(addr, ®, d, sizeof(d)); + debug("Data write to bus %u at %02X : %02X+%04X\n",dev->bus, dev->addr, reg, value); + return i2c_slave_write(dev->bus, dev->addr, ®, 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}; - int error = i2c_slave_read(addr, ®, d, sizeof(d)) - debug("Data read from %02X: %02X+%04X\n",addr,reg,*value); + int error = i2c_slave_read(dev->bus, dev->addr, ®, d, sizeof(d)) + debug("Data read from bus %u at %02X: %02X+%04X\n",dev->bus, dev->addr, reg, *value); *value = d[1] | (d[0] << 8); return error; } 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) { - 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) @@ -51,17 +51,17 @@ int ina3221_sync(ina3221_t *dev) uint16_t ptr_data; int err = 0; //////////////////////// 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; 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; } //////////////////////// 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; 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 0; @@ -72,7 +72,7 @@ int ina3221_setting(ina3221_t *dev ,bool mode, bool bus, bool shunt) dev->config.mode = mode; dev->config.ebus = bus; 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) @@ -80,7 +80,7 @@ int ina3221_enableChannel(ina3221_t *dev ,bool ch1, bool ch2, bool ch3) dev->config.ch1 = ch1; dev->config.ch2 = ch2; 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) @@ -88,32 +88,32 @@ int ina3221_enableChannelSum(ina3221_t *dev ,bool ch1, bool ch2, bool ch3) dev->mask.scc1 = ch1; dev->mask.scc2 = ch2; 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) { dev->mask.wen = warning; 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) { 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) { 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) { 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) @@ -121,14 +121,14 @@ int ina3221_reset(ina3221_t *dev) dev->config.config_register = INA3221_DEFAULT_CONFIG ; //dev reset dev->mask.mask_register = INA3221_DEFAULT_CONFIG ; //dev reset 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) { int16_t raw_value; 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; *voltage = raw_value*0.001 ; //V 8mV step return 0; @@ -138,12 +138,12 @@ int ina3221_getShuntValue(ina3221_t *dev, ina3221_channel_t channel, float *volt { int16_t raw_value; 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; *voltage = raw_value*0.005; //mV 40uV step 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; } *current = (*voltage*1000.0)/dev->shunt[channel] ; //mA @@ -154,7 +154,7 @@ int ina3221_getSumShuntValue(ina3221_t *dev, float *voltage) { int16_t raw_value; 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; *voltage = raw_value*0.02; //uV 40uV step 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) { 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) { 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) { 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) { 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; } 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) { 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; } 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); } diff --git a/extras/ina3221/ina3221.h b/extras/ina3221/ina3221.h index 74cec76..627f3aa 100644 --- a/extras/ina3221/ina3221.h +++ b/extras/ina3221/ina3221.h @@ -132,7 +132,7 @@ typedef union * Device description */ 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) ina3221_config_t config; //Memory of ina3221 config ina3221_mask_t mask; //Memory of mask_config diff --git a/extras/mcp4725/mcp4725.c b/extras/mcp4725/mcp4725.c index f53d6bd..a478e67 100644 --- a/extras/mcp4725/mcp4725.c +++ b/extras/mcp4725/mcp4725.c @@ -6,62 +6,59 @@ * BSD Licensed as described in the file LICENSE */ #include "mcp4725.h" -#include #define CMD_DAC 0x40 #define CMD_EEPROM 0x60 #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; - read_data(addr, &res, 1); + read_data(dev, &res, 1); 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]; - read_data(addr, buf, eeprom ? 4 : 1); + read_data(dev, buf, eeprom ? 4 : 1); 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[] = { (eeprom ? CMD_EEPROM : CMD_DAC) | ((uint8_t)mode << 1), 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]; - read_data(addr, buf, eeprom ? 5 : 3); + read_data(dev, buf, eeprom ? 5 : 3); return eeprom ? ((uint16_t)(buf[3] & 0x0f) << 8) | buf[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[] = { (eeprom ? CMD_EEPROM : CMD_DAC), 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); } - - diff --git a/extras/mcp4725/mcp4725.h b/extras/mcp4725/mcp4725.h index ac6d905..edb29de 100644 --- a/extras/mcp4725/mcp4725.h +++ b/extras/mcp4725/mcp4725.h @@ -10,6 +10,7 @@ #include #include +#include #ifdef __cplusplus extern "C" @@ -41,7 +42,7 @@ typedef enum * @param addr Device address * @return true when EEPROM is busy */ -bool mcp4725_eeprom_busy(uint8_t addr); +bool mcp4725_eeprom_busy(i2c_dev_t* dev); /** * Get power mode @@ -49,7 +50,7 @@ bool mcp4725_eeprom_busy(uint8_t addr); * @param eeprom Read power mode from EEPROM if true * @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 @@ -57,7 +58,7 @@ mcp4725_power_mode_t mcp4725_get_power_mode(uint8_t addr, bool eeprom); * @param mode Power mode * @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 @@ -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 * @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 @@ -73,7 +74,7 @@ uint16_t mcp4725_get_raw_output(uint8_t addr, bool eeprom); * @param value Raw output value, 0..4095 * @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 @@ -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 * @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 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 diff --git a/extras/ms561101ba03/ms561101ba03.c b/extras/ms561101ba03/ms561101ba03.c index 965e9df..d88f488 100644 --- a/extras/ms561101ba03/ms561101ba03.c +++ b/extras/ms561101ba03/ms561101ba03.c @@ -8,7 +8,6 @@ * BSD Licensed as described in the file LICENSE */ #include "ms561101ba03.h" -#include #include #include "FreeRTOS.h" #include "task.h" @@ -26,10 +25,10 @@ */ #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 }; - 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) @@ -37,32 +36,32 @@ static inline bool read_prom(ms561101ba03_t *dev) uint8_t tmp[2] = { 0, 0 }; uint8_t reg = 0xA2 ; - if (i2c_slave_read(dev->addr, ®, tmp, 2)) + if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, ®, tmp, 2)) return false; dev->config_data.sens = tmp[0] << 8 | tmp[1]; reg = 0xA4 ; - if (i2c_slave_read(dev->addr, ®, tmp, 2)) + if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, ®, tmp, 2)) return false; dev->config_data.off = tmp[0] << 8 | tmp[1]; reg = 0xA6 ; - if (i2c_slave_read(dev->addr, ®, tmp, 2)) + if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, ®, tmp, 2)) return false; dev->config_data.tcs = tmp[0] << 8 | tmp[1]; reg = 0xA8 ; - if (i2c_slave_read(dev->addr, ®, tmp, 2)) + if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, ®, tmp, 2)) return false; dev->config_data.tco = tmp[0] << 8 | tmp[1]; reg = 0xAA ; - if (i2c_slave_read(dev->addr, ®, tmp, 2)) + if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, ®, tmp, 2)) return false; dev->config_data.t_ref = tmp[0] << 8 | tmp[1]; reg = 0xAC ; - if (i2c_slave_read(dev->addr, ®, tmp, 2)) + if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, ®, tmp, 2)) return false; 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 { 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 { 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; uint8_t tmp[3]; uint8_t reg = 0x00 ; - if (i2c_slave_read(addr, ®, tmp, 3)) + if (i2c_slave_read(i2c_dev->bus, i2c_dev->addr, ®, tmp, 3)) return false; *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); - if (!read_adc(dev->addr, result)) + if (!read_adc(&dev->i2c_dev, result)) return false; return true; @@ -157,7 +156,7 @@ static inline bool get_raw_pressure(ms561101ba03_t *dev, uint32_t *result) vTaskDelay(CONVERSION_TIME); - if (!read_adc(dev->addr, result)) + if (!read_adc(&dev->i2c_dev, result)) return false; return true; @@ -216,7 +215,7 @@ bool ms561101ba03_get_sensor_data(ms561101ba03_t *dev) bool ms561101ba03_init(ms561101ba03_t *dev) { // First of all we need to reset the chip - if (reset(dev->addr)) + if (reset(&dev->i2c_dev)) return false; // Wait a bit for the device to reset vTaskDelay(CONVERSION_TIME); diff --git a/extras/ms561101ba03/ms561101ba03.h b/extras/ms561101ba03/ms561101ba03.h index d658be2..fb357e7 100644 --- a/extras/ms561101ba03/ms561101ba03.h +++ b/extras/ms561101ba03/ms561101ba03.h @@ -12,6 +12,7 @@ #include #include +#include #ifdef __cplusplus extern "C" @@ -60,7 +61,7 @@ typedef struct */ typedef struct { - uint8_t addr; //!< I2C address + i2c_dev_t i2c_dev; //!< I2C device settings ms561101ba03_osr_t osr; //!< Oversampling setting ms561101ba03_config_data_t config_data; //!< Device configuration, filled upon initalize ms561101ba03_result_t result; //!< Result, filled upon co diff --git a/extras/pca9685/pca9685.c b/extras/pca9685/pca9685.c index daea4fc..6b04c31 100644 --- a/extras/pca9685/pca9685.c +++ b/extras/pca9685/pca9685.c @@ -7,7 +7,6 @@ */ #include "pca9685.h" -#include #include #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; } -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, ®, &val, 1)) - debug("Could not write 0x%02x to 0x%02x, addr = 0x%02x", reg, val, addr); + if (i2c_slave_write(dev->bus, dev->addr, ®, &val, 1)) + 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; - if (i2c_slave_read(addr, ®, &res, 1)) - debug("Could not read from 0x%02x, addr = 0x%02x", reg, addr); + if (i2c_slave_read(dev->bus, dev->addr, ®, &res, 1)) + debug("Could not read from 0x%02x, bus %u, addr = 0x%02x", reg, dev->bus, dev->addr); 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 - 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) { @@ -92,63 +91,63 @@ bool pca9685_set_subaddr(uint8_t addr, uint8_t num, uint8_t subaddr, bool enable 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); - update_reg(addr, REG_MODE1, mask, enable ? mask : 0); + update_reg(dev, REG_MODE1, mask, enable ? mask : 0); 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) 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) { - write_reg(addr, REG_MODE1, mode & ~MODE1_SLEEP); + write_reg(dev, REG_MODE1, mode & ~MODE1_SLEEP); 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) { @@ -156,18 +155,18 @@ bool pca9685_set_prescaler(uint8_t addr, uint8_t prescaler) return false; } - pca9685_sleep(addr, true); - write_reg(addr, REG_PRE_SCALE, prescaler); - pca9685_sleep(addr, false); + pca9685_sleep(dev, true); + write_reg(dev, REG_PRE_SCALE, prescaler); + pca9685_sleep(dev, false); 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; 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 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); if (val == 0) { // 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) { // Normal uint8_t buf[4] = { 0, 0, val, val >> 8 }; - i2c_slave_write(addr, ®, buf, 4); + i2c_slave_write(dev->bus, dev->addr, ®, buf, 4); } else { // 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) { @@ -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 ++) - pca9685_set_pwm_value(addr, first_ch + i, values [i]); + pca9685_set_pwm_value(dev, first_ch + i, values [i]); return true; } diff --git a/extras/pca9685/pca9685.h b/extras/pca9685/pca9685.h index 0ee0000..a92b964 100644 --- a/extras/pca9685/pca9685.h +++ b/extras/pca9685/pca9685.h @@ -10,6 +10,7 @@ #include #include +#include #ifdef __cplusplus extern "C" @@ -22,7 +23,7 @@ extern "C" * Init device * @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) @@ -32,62 +33,62 @@ void pca9685_init(uint8_t addr); * @param enable True to enable subaddress, false to disable * @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) * @param addr Device address */ -void pca9685_restart(uint8_t addr); +void pca9685_restart(i2c_dev_t* dev); /** * Check if device is in sleep mode * @param addr Device address * @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. * @param addr Device address * @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 * @param addr Device address * @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) * @param addr Device address * @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 * @param addr Device address * @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 * @param addr Device address * @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. * @param addr Device address * @return Frequency prescaler */ -uint8_t pca9685_get_prescaler(uint8_t addr); +uint8_t pca9685_get_prescaler(i2c_dev_t* dev); /** * Set PWM frequency prescaler. @@ -95,14 +96,14 @@ uint8_t pca9685_get_prescaler(uint8_t addr); * @param prescaler Prescaler value * @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 * @param addr Device address * @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 @@ -110,7 +111,7 @@ uint16_t pca9685_get_pwm_frequency(uint8_t addr); * @param freq PWM frequency, Hz * @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 @@ -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 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 @@ -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 * @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 } diff --git a/extras/pcf8574/pcf8574.c b/extras/pcf8574/pcf8574.c index 1d08625..ff9a92c 100644 --- a/extras/pcf8574/pcf8574.c +++ b/extras/pcf8574/pcf8574.c @@ -1,47 +1,46 @@ #include "pcf8574.h" -#include -uint8_t pcf8574_port_read(uint8_t addr) +uint8_t pcf8574_port_read(i2c_dev_t* dev) { 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 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; 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 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; 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 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 mask = ~(1 << num); - pcf8574_port_write (addr, (pcf8574_port_read(addr) & mask) | bit); + pcf8574_port_write (dev, (pcf8574_port_read(dev) & mask) | bit); } diff --git a/extras/pcf8574/pcf8574.h b/extras/pcf8574/pcf8574.h index c14e25f..84b6ce0 100644 --- a/extras/pcf8574/pcf8574.h +++ b/extras/pcf8574/pcf8574.h @@ -8,6 +8,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" @@ -19,7 +20,7 @@ extern "C" * \param addr I2C register address (0b0100 for PCF8574) * \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 @@ -28,14 +29,14 @@ uint8_t pcf8574_port_read(uint8_t addr); * @param len Buffer length * @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 * \param addr I2C register address (0b0100 for PCF8574) * \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 @@ -44,7 +45,7 @@ void pcf8574_port_write(uint8_t addr, uint8_t value); * @param len Buffer length * @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 @@ -52,7 +53,7 @@ size_t pcf8574_port_write_buf(uint8_t addr, void *buf, size_t len); * \param num pin number (0..7) * \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 @@ -62,7 +63,7 @@ bool pcf8574_gpio_read(uint8_t addr, uint8_t num); * \param num pin number (0..7) * \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 } diff --git a/extras/pcf8591/pcf8591.c b/extras/pcf8591/pcf8591.c index 42b9df4..ba06a5e 100644 --- a/extras/pcf8591/pcf8591.c +++ b/extras/pcf8591/pcf8591.c @@ -1,22 +1,20 @@ #include #include -#include #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 -uint8_t -pcf8591_read(uint8_t addr, uint8_t analog_pin) +uint8_t pcf8591_read(i2c_dev_t* dev, uint8_t analog_pin) { uint8_t res = 0; 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; } diff --git a/extras/pcf8591/pcf8591.h b/extras/pcf8591/pcf8591.h index 3deac57..5327323 100644 --- a/extras/pcf8591/pcf8591.h +++ b/extras/pcf8591/pcf8591.h @@ -9,6 +9,8 @@ #ifndef _EXTRAS_PCF8591_H_ #define _EXTRAS_PCF8591_H_ +#include + #ifdef __cplusplus extern "C" { @@ -20,7 +22,7 @@ extern "C" #define PCF8591_DEFAULT_ADDRESS 0x48 -void pcf8591_init(void); +void pcf8591_init(void); //FIXME : library incomplete ? /** * Read input value of an analog pin. @@ -32,7 +34,7 @@ void pcf8591_init(void); * 3 - AIN3 * @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 diff --git a/extras/ssd1306/ssd1306.c b/extras/ssd1306/ssd1306.c index 4da3e10..372e867 100644 --- a/extras/ssd1306/ssd1306.c +++ b/extras/ssd1306/ssd1306.c @@ -11,9 +11,6 @@ */ #include "ssd1306.h" #include -#if (SSD1306_I2C_SUPPORT) - #include -#endif #if (SSD1306_SPI4_SUPPORT) || (SSD1306_SPI3_SUPPORT) #include #endif @@ -92,7 +89,7 @@ #if (SSD1306_I2C_SUPPORT) static int inline i2c_send(const ssd1306_t *dev, uint8_t reg, uint8_t* data, uint8_t len) { - return i2c_slave_write(dev->addr, ®, data, len); + return i2c_slave_write(dev->i2c_dev.bus, dev->i2c_dev.addr , ®, data, len); } #endif diff --git a/extras/ssd1306/ssd1306.h b/extras/ssd1306/ssd1306.h index cdfae61..639517b 100644 --- a/extras/ssd1306/ssd1306.h +++ b/extras/ssd1306/ssd1306.h @@ -19,6 +19,7 @@ // shifted #if (SSD1306_I2C_SUPPORT) + #include #define SSD1306_I2C_ADDR_0 (0x3C) #define SSD1306_I2C_ADDR_1 (0x3D) #endif @@ -67,7 +68,7 @@ typedef struct ssd1306_screen_t screen ; union { #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 uint8_t cs_pin ; //!< Chip Select GPIO pin, used by SSD1306_PROTO_SPI3, SSD1306_PROTO_SPI4 } ; diff --git a/extras/tsl2561/tsl2561.c b/extras/tsl2561/tsl2561.c index f60825a..cc89021 100644 --- a/extras/tsl2561/tsl2561.c +++ b/extras/tsl2561/tsl2561.c @@ -6,7 +6,6 @@ #include #include "FreeRTOS.h" -#include "i2c/i2c.h" #include "task.h" #include "tsl2561.h" @@ -96,18 +95,18 @@ #define B8C 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; - return i2c_slave_write(i2c_addr, ®, &value, 1); + return i2c_slave_write(i2c_dev->bus, i2c_dev->addr, ®, &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]; reg = TSL2561_REG_COMMAND | reg; - if (i2c_slave_read(i2c_addr, ®, data, 1)) + if (i2c_slave_read(i2c_dev->bus, i2c_dev->addr, ®, data, 1)) { 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]; } -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; uint8_t data[2]; 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"); } @@ -131,24 +130,24 @@ static uint16_t read_register_16(uint8_t i2c_addr, uint8_t low_register_addr) 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) { - if (enable(device->i2c_addr)) + if (enable(&device->i2c_dev)) { 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) { @@ -156,39 +155,39 @@ void tsl2561_init(tsl2561_t *device) } // 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; device->package_type = package; // 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->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) { - enable(device->i2c_addr); - write_register(device->i2c_addr, TSL2561_REG_TIMING, integration_time_id | device->gain); - disable(device->i2c_addr); + enable(&device->i2c_dev); + write_register(&device->i2c_dev, TSL2561_REG_TIMING, integration_time_id | device->gain); + disable(&device->i2c_dev); device->integration_time = integration_time_id; } void tsl2561_set_gain(tsl2561_t *device, tsl2561_gain_t gain) { - enable(device->i2c_addr); - write_register(device->i2c_addr, TSL2561_REG_TIMING, gain | device->integration_time); - disable(device->i2c_addr); + enable(&device->i2c_dev); + write_register(&device->i2c_dev, TSL2561_REG_TIMING, gain | device->integration_time); + disable(&device->i2c_dev); device->gain = gain; } 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 // 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; } - *channel0 = read_register_16(device->i2c_addr, TSL2561_REG_CHANNEL_0_LOW); - *channel1 = read_register_16(device->i2c_addr, TSL2561_REG_CHANNEL_1_LOW); + *channel0 = read_register_16(&device->i2c_dev, TSL2561_REG_CHANNEL_0_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) diff --git a/extras/tsl2561/tsl2561.h b/extras/tsl2561/tsl2561.h index ad5109f..9f880b3 100644 --- a/extras/tsl2561/tsl2561.h +++ b/extras/tsl2561/tsl2561.h @@ -9,6 +9,7 @@ #include #include +#include #ifdef __cplusplus extern "C" { @@ -38,7 +39,7 @@ typedef enum } tsl2561_gain_t; typedef struct { - tsl2561_i2c_addr_t i2c_addr; + i2c_dev_t i2c_dev; uint8_t integration_time; uint8_t gain; uint8_t package_type; diff --git a/extras/tsl4531/tsl4531.c b/extras/tsl4531/tsl4531.c index 54963e9..a15a0ee 100644 --- a/extras/tsl4531/tsl4531.c +++ b/extras/tsl4531/tsl4531.c @@ -6,7 +6,6 @@ #include #include "FreeRTOS.h" -#include "i2c/i2c.h" #include "task.h" #include "tsl4531.h" @@ -27,18 +26,18 @@ #define TSL4531_INTEGRATION_TIME_200MS 240 #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; - return i2c_slave_write(i2c_addr, ®, &value, 1); + return i2c_slave_write(i2c_dev->bus, i2c_dev->addr, ®, &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]; reg = TSL4531_REG_COMMAND | reg; - if (i2c_slave_read(i2c_addr, ®, data, 1)) + if (i2c_slave_read(i2c_dev->bus, i2c_dev->addr, ®, data, 1)) { 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]; } -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; uint8_t data[2]; 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"); } @@ -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) { - 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) { - 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) @@ -79,13 +78,13 @@ void tsl4531_init(tsl4531_t *device) 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) { 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; 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; 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); 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; 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); device->skip_power_save = skip_power_save; @@ -156,8 +155,8 @@ bool tsl4531_read_lux(tsl4531_t *device, uint16_t *lux) 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); *lux = multiplier * lux_data; diff --git a/extras/tsl4531/tsl4531.h b/extras/tsl4531/tsl4531.h index 3b57664..d4ab7ec 100644 --- a/extras/tsl4531/tsl4531.h +++ b/extras/tsl4531/tsl4531.h @@ -9,6 +9,7 @@ #include #include +#include #ifdef __cplusplus extern "C" { @@ -38,7 +39,7 @@ typedef enum } tsl4531_part_id_t; typedef struct { - tsl4531_i2c_addr_t i2c_addr; + i2c_dev_t i2c_dev; uint8_t integration_time_id; bool skip_power_save; tsl4531_part_id_t part_id;