pca9685 pcf8591 ms561101ba03 update

This commit is contained in:
lilian 2017-08-04 18:27:59 -03:00
parent 5d105316df
commit 2eb50c5f22
9 changed files with 109 additions and 96 deletions

View file

@ -13,18 +13,20 @@
#include <i2c/i2c.h>
#include <ms561101ba03/ms561101ba03.h>
#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,
};

View file

@ -11,8 +11,9 @@
#include <pca9685/pca9685.h>
#include <stdio.h>
#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;

View file

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

View file

@ -8,7 +8,6 @@
* BSD Licensed as described in the file LICENSE
*/
#include "ms561101ba03.h"
#include <i2c/i2c.h>
#include <espressif/esp_common.h>
#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, &reg, tmp, 2))
if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, &reg, tmp, 2))
return false;
dev->config_data.sens = tmp[0] << 8 | tmp[1];
reg = 0xA4 ;
if (i2c_slave_read(dev->addr, &reg, tmp, 2))
if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, &reg, tmp, 2))
return false;
dev->config_data.off = tmp[0] << 8 | tmp[1];
reg = 0xA6 ;
if (i2c_slave_read(dev->addr, &reg, tmp, 2))
if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, &reg, tmp, 2))
return false;
dev->config_data.tcs = tmp[0] << 8 | tmp[1];
reg = 0xA8 ;
if (i2c_slave_read(dev->addr, &reg, tmp, 2))
if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, &reg, tmp, 2))
return false;
dev->config_data.tco = tmp[0] << 8 | tmp[1];
reg = 0xAA ;
if (i2c_slave_read(dev->addr, &reg, tmp, 2))
if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, &reg, tmp, 2))
return false;
dev->config_data.t_ref = tmp[0] << 8 | tmp[1];
reg = 0xAC ;
if (i2c_slave_read(dev->addr, &reg, tmp, 2))
if (i2c_slave_read(dev->i2c_dev.bus, dev->i2c_dev.addr, &reg, tmp, 2))
return false;
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, &reg, tmp, 3))
if (i2c_slave_read(i2c_dev->bus, i2c_dev->addr, &reg, 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);

View file

@ -12,6 +12,7 @@
#include <stdint.h>
#include <stdbool.h>
#include <i2c/i2c.h>
#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

View file

@ -7,7 +7,6 @@
*/
#include "pca9685.h"
#include <i2c/i2c.h>
#include <espressif/esp_common.h>
#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, &reg, &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, &reg, &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, &reg, &res, 1))
debug("Could not read from 0x%02x, addr = 0x%02x", reg, addr);
if (i2c_slave_read(dev->bus, dev->addr, &reg, &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, &reg, buf, 4);
i2c_slave_write(dev->bus, dev->addr, &reg, 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;
}

View file

@ -10,6 +10,7 @@
#include <stdint.h>
#include <stdbool.h>
#include <i2c/i2c.h>
#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
}

View file

@ -1,22 +1,20 @@
#include <stddef.h>
#include <stdint.h>
#include <i2c/i2c.h>
#include "pcf8591.h"
/**
* CAUTION: PLEASE SET I2C_FREQUENCY_400K IS 'false' IN 'i2c.h' FILE
* CAUTION: PLEASE SET LOW FREQUENCY
*/
#define PCF8591_CTRL_REG_READ 0x03
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;
}

View file

@ -9,6 +9,8 @@
#ifndef _EXTRAS_PCF8591_H_
#define _EXTRAS_PCF8591_H_
#include <i2c/i2c.h>
#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