Skip to content

Commit

Permalink
Merge pull request #24 from Sensirion/stdint
Browse files Browse the repository at this point in the history
Automated type conversion
  • Loading branch information
abrauchli authored May 13, 2019
2 parents 4823308 + e970128 commit 9a6924a
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 75 deletions.
2 changes: 1 addition & 1 deletion embedded-common
Submodule embedded-common updated 24 files
+16 −13 README.md
+6 −5 hw_i2c/sample-implementations/Atmel_SAMD2_series/sensirion_hw_i2c_implementation.c
+8 −6 hw_i2c/sample-implementations/Nordic_nRF5_series/sensirion_hw_i2c_implementation.c
+9 −8 hw_i2c/sample-implementations/STM32F1_series/sensirion_hw_i2c_implementation.c
+4 −3 hw_i2c/sample-implementations/arduino-alt-i2c/sensirion_hw_i2c_implementation.cpp
+0 −40 hw_i2c/sample-implementations/arduino/README.md
+0 −104 hw_i2c/sample-implementations/arduino/sensirion_arch_config.h
+6 −5 hw_i2c/sample-implementations/arduino/sensirion_hw_i2c_implementation.cpp
+5 −4 hw_i2c/sample-implementations/linux_user_space/sensirion_hw_i2c_implementation.c
+4 −3 hw_i2c/sample-implementations/mbed/sensirion_hw_i2c_implementation.cpp
+5 −5 hw_i2c/sample-implementations/mbed/sgp30_example_usage.cpp
+4 −3 hw_i2c/sensirion_hw_i2c_implementation.c
+10 −24 sensirion_arch_config.h
+42 −37 sensirion_common.c
+25 −16 sensirion_common.h
+4 −3 sensirion_i2c.h
+3 −3 sw_i2c/sample-implementations/Atmel_SAMD2_series/sensirion_sw_i2c_implementation.c
+13 −13 sw_i2c/sample-implementations/Nordic_nRF5_series/sensirion_sw_i2c_implementation.c
+5 −5 sw_i2c/sample-implementations/STM32F1_series/sensirion_sw_i2c_implementation.c
+3 −3 sw_i2c/sample-implementations/linux_user_space/sensirion_sw_i2c_implementation.c
+3 −3 sw_i2c/sample-implementations/mbed/sensirion_sw_i2c_implementation.cpp
+16 −15 sw_i2c/sensirion_sw_i2c.c
+3 −3 sw_i2c/sensirion_sw_i2c_gpio.h
+3 −3 sw_i2c/sensirion_sw_i2c_implementation.c
4 changes: 2 additions & 2 deletions sht-common/example_usage.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ int main(void) {
/* printf("SHT sensor probing successful\n"); */

while (1) {
s32 temperature, humidity;
int32_t temperature, humidity;
/* Measure temperature and relative humidity and store into variables
* temperature, humidity (each output multiplied by 1000).
*/
s8 ret = sht_measure_blocking_read(&temperature, &humidity);
int8_t ret = sht_measure_blocking_read(&temperature, &humidity);
if (ret == STATUS_OK) {
/* printf("measured temperature: %0.2f degreeCelsius, "
"measured humidity: %0.2f percentRH\n",
Expand Down
14 changes: 7 additions & 7 deletions sht-common/sht.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extern "C" {
*
* @return 0 if a sensor was detected
*/
s8 sht_probe(void);
int8_t sht_probe(void);

/**
* Starts a measurement and then reads out the results. This function blocks
Expand All @@ -75,7 +75,7 @@ s8 sht_probe(void);
* measurement
* @return 0 if the command was successful, else an error code.
*/
s8 sht_measure_blocking_read(s32 *temperature, s32 *humidity);
int8_t sht_measure_blocking_read(int32_t *temperature, int32_t *humidity);

/**
* Starts a measurement in high precision mode. Use sht_read() to read out the
Expand All @@ -84,7 +84,7 @@ s8 sht_measure_blocking_read(s32 *temperature, s32 *humidity);
*
* @return 0 if the command was successful, else an error code.
*/
s8 sht_measure(void);
int8_t sht_measure(void);

/**
* Reads out the results of a measurement that was previously started by
Expand All @@ -99,7 +99,7 @@ s8 sht_measure(void);
* measurement
* @return 0 if the command was successful, else an error code.
*/
s8 sht_read(s32 *temperature, s32 *humidity);
int8_t sht_read(int32_t *temperature, int32_t *humidity);

/**
* Enable or disable the SHT's sleep mode between measurements, if supported.
Expand All @@ -110,14 +110,14 @@ s8 sht_read(s32 *temperature, s32 *humidity);
* @return 0 if the command was successful,
* 1 if an error occured or if sleep mode is not supported
*/
s8 sht_disable_sleep(u8 disable_sleep);
int8_t sht_disable_sleep(uint8_t disable_sleep);

/**
* Enable or disable the SHT's low power mode
*
* @param enable_low_power_mode 1 to enable low power mode, 0 to disable
*/
void sht_enable_low_power_mode(u8 enable_low_power_mode);
void sht_enable_low_power_mode(uint8_t enable_low_power_mode);

/**
* sht_get_driver_version() - Return the driver version
Expand All @@ -131,7 +131,7 @@ const char *sht_get_driver_version(void);
*
* @return SHTxx_ADDRESS
*/
u8 sht_get_configured_sht_address(void);
uint8_t sht_get_configured_sht_address(void);

#ifdef __cplusplus
}
Expand Down
17 changes: 9 additions & 8 deletions sht-common/sht_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,26 @@
#include "sensirion_i2c.h"
#include "sht.h"

s8 sht_common_read_ticks(u8 address, s32 *temperature_ticks,
s32 *humidity_ticks) {
u8 data[6];
s8 ret = sensirion_i2c_read(address, data, sizeof(data));
int8_t sht_common_read_ticks(uint8_t address, int32_t *temperature_ticks,
int32_t *humidity_ticks) {
uint8_t data[6];
int8_t ret = sensirion_i2c_read(address, data, sizeof(data));
if (ret)
return ret;
if (sensirion_common_check_crc(data, 2, data[2]) ||
sensirion_common_check_crc(data + 3, 2, data[5])) {
return STATUS_CRC_FAIL;
}

*temperature_ticks = (data[1] & 0xff) | ((s32)data[0] << 8);
*humidity_ticks = (data[4] & 0xff) | ((s32)data[3] << 8);
*temperature_ticks = (data[1] & 0xff) | ((int32_t)data[0] << 8);
*humidity_ticks = (data[4] & 0xff) | ((int32_t)data[3] << 8);

return STATUS_OK;
}

s8 sht_common_read_measurement(u8 address, s32 *temperature, s32 *humidity) {
s8 ret = sht_common_read_ticks(address, temperature, humidity);
int8_t sht_common_read_measurement(uint8_t address, int32_t *temperature,
int32_t *humidity) {
int8_t ret = sht_common_read_ticks(address, temperature, humidity);
/**
* formulas for conversion of the sensor signals, optimized for fixed point
* algebra: Temperature = 175 * S_T / 2^16 - 45 Relative Humidity =
Expand Down
7 changes: 4 additions & 3 deletions sht-common/sht_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@
extern "C" {
#endif

s8 sht_common_read_ticks(u8 address, s32 *temperature_ticks,
s32 *humidity_ticks);
int8_t sht_common_read_ticks(uint8_t address, int32_t *temperature_ticks,
int32_t *humidity_ticks);

s8 sht_common_read_measurement(u8 address, s32 *temperature, s32 *humidity);
int8_t sht_common_read_measurement(uint8_t address, int32_t *temperature,
int32_t *humidity);

#ifdef __cplusplus
}
Expand Down
40 changes: 20 additions & 20 deletions sht3x/sht3x.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,45 +46,45 @@

/* all measurement commands return T (CRC) RH (CRC) */
#if USE_SENSIRION_CLOCK_STRETCHING
static const u8 CMD_MEASURE_HPM[] = {0x2C, 0x06};
static const u8 CMD_MEASURE_LPM[] = {0x2C, 0x10};
static const uint8_t CMD_MEASURE_HPM[] = {0x2C, 0x06};
static const uint8_t CMD_MEASURE_LPM[] = {0x2C, 0x10};
#else
static const u8 CMD_MEASURE_HPM[] = {0x24, 0x00};
static const u8 CMD_MEASURE_LPM[] = {0x24, 0x16};
static const uint8_t CMD_MEASURE_HPM[] = {0x24, 0x00};
static const uint8_t CMD_MEASURE_LPM[] = {0x24, 0x16};
#endif /* USE_SENSIRION_CLOCK_STRETCHING */
static const u8 CMD_READ_STATUS_REG[] = {0xF3, 0x2D};
static const u8 COMMAND_SIZE = sizeof(CMD_MEASURE_HPM);
static const uint8_t CMD_READ_STATUS_REG[] = {0xF3, 0x2D};
static const uint8_t COMMAND_SIZE = sizeof(CMD_MEASURE_HPM);
#ifdef SHT_ADDRESS
static const u8 SHT3X_ADDRESS = SHT_ADDRESS;
static const uint8_t SHT3X_ADDRESS = SHT_ADDRESS;
#else
static const u8 SHT3X_ADDRESS = 0x44;
static const uint8_t SHT3X_ADDRESS = 0x44;
#endif

static const u16 MEASUREMENT_DURATION_USEC = 15000;
static const uint16_t MEASUREMENT_DURATION_USEC = 15000;

static const u8 *cmd_measure = CMD_MEASURE_HPM;
static const uint8_t *cmd_measure = CMD_MEASURE_HPM;

s8 sht_measure_blocking_read(s32 *temperature, s32 *humidity) {
s8 ret = sht_measure();
int8_t sht_measure_blocking_read(int32_t *temperature, int32_t *humidity) {
int8_t ret = sht_measure();
if (ret == STATUS_OK) {
sensirion_sleep_usec(MEASUREMENT_DURATION_USEC);
ret = sht_read(temperature, humidity);
}
return ret;
}

s8 sht_measure() {
int8_t sht_measure() {
return sensirion_i2c_write(SHT3X_ADDRESS, CMD_MEASURE_HPM, COMMAND_SIZE);
}

s8 sht_read(s32 *temperature, s32 *humidity) {
int8_t sht_read(int32_t *temperature, int32_t *humidity) {
return sht_common_read_measurement(SHT3X_ADDRESS, temperature, humidity);
}

s8 sht_probe() {
u8 data[3];
int8_t sht_probe() {
uint8_t data[3];
sensirion_i2c_init();
s8 ret =
int8_t ret =
sensirion_i2c_write(SHT3X_ADDRESS, CMD_READ_STATUS_REG, COMMAND_SIZE);
if (ret)
return ret;
Expand All @@ -99,18 +99,18 @@ s8 sht_probe() {
return STATUS_OK;
}

s8 sht_disable_sleep(u8 disable_sleep) {
int8_t sht_disable_sleep(uint8_t disable_sleep) {
return STATUS_FAIL; /* sleep mode not supported */
}

void sht_enable_low_power_mode(u8 enable_low_power_mode) {
void sht_enable_low_power_mode(uint8_t enable_low_power_mode) {
cmd_measure = enable_low_power_mode ? CMD_MEASURE_LPM : CMD_MEASURE_HPM;
}

const char *sht_get_driver_version() {
return SHT_DRV_VERSION_STR;
}

u8 sht_get_configured_sht_address() {
uint8_t sht_get_configured_sht_address() {
return SHT3X_ADDRESS;
}
70 changes: 36 additions & 34 deletions shtc1/shtc1.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,30 @@

/* all measurement commands return T (CRC) RH (CRC) */
#if USE_SENSIRION_CLOCK_STRETCHING
static const u8 CMD_MEASURE_HPM[] = {0x7C, 0xA2};
static const u8 CMD_MEASURE_LPM[] = {0x64, 0x58};
static const uint8_t CMD_MEASURE_HPM[] = {0x7C, 0xA2};
static const uint8_t CMD_MEASURE_LPM[] = {0x64, 0x58};
#else
static const u8 CMD_MEASURE_HPM[] = {0x78, 0x66};
static const u8 CMD_MEASURE_LPM[] = {0x60, 0x9C};
static const u16 MEASUREMENT_DURATION_USEC = 14400;
static const uint8_t CMD_MEASURE_HPM[] = {0x78, 0x66};
static const uint8_t CMD_MEASURE_LPM[] = {0x60, 0x9C};
static const uint16_t MEASUREMENT_DURATION_USEC = 14400;
#endif /* USE_SENSIRION_CLOCK_STRETCHING */
static const u8 CMD_READ_ID_REG[] = {0xef, 0xc8};
static const u8 COMMAND_SIZE = sizeof(CMD_MEASURE_HPM);
static const uint8_t CMD_READ_ID_REG[] = {0xef, 0xc8};
static const uint8_t COMMAND_SIZE = sizeof(CMD_MEASURE_HPM);

static const u8 SHTC3_CMD_SLEEP[] = {0xB0, 0x98};
static const u8 SHTC3_CMD_WAKEUP[] = {0x35, 0x17};
static const uint8_t SHTC3_CMD_SLEEP[] = {0xB0, 0x98};
static const uint8_t SHTC3_CMD_WAKEUP[] = {0x35, 0x17};
#ifdef SHT_ADDRESS
static const u8 SHTC1_ADDRESS = SHT_ADDRESS;
static const uint8_t SHTC1_ADDRESS = SHT_ADDRESS;
#else
static const u8 SHTC1_ADDRESS = 0x70;
static const uint8_t SHTC1_ADDRESS = 0x70;
#endif

static const u16 SHTC1_PRODUCT_CODE_MASK = 0x001F;
static const u16 SHTC1_PRODUCT_CODE = 0x0007;
static const u16 SHTC3_PRODUCT_CODE_MASK = 0x083F;
static const u16 SHTC3_PRODUCT_CODE = 0x0807;
static const uint16_t SHTC1_PRODUCT_CODE_MASK = 0x001F;
static const uint16_t SHTC1_PRODUCT_CODE = 0x0007;
static const uint16_t SHTC3_PRODUCT_CODE_MASK = 0x083F;
static const uint16_t SHTC3_PRODUCT_CODE = 0x0807;

static const u8 *cmd_measure = CMD_MEASURE_HPM;
static const uint8_t *cmd_measure = CMD_MEASURE_HPM;

/**
* PM_SLEEP is equivalent to
Expand All @@ -98,25 +98,25 @@ static const u8 *cmd_measure = CMD_MEASURE_HPM;
(((ret) = sht_wakeup()) ? (sht_sleep(), (ret)) \
: ((ret) = (cmd) ? sht_sleep(), (ret) : (ret)))

static u8 supports_sleep = 1;
static u8 sleep_enabled = 1;
static uint8_t supports_sleep = 1;
static uint8_t sleep_enabled = 1;

static u8 sht_sleep() {
static uint8_t sht_sleep() {
if (!supports_sleep || !sleep_enabled)
return STATUS_OK;

return sensirion_i2c_write(SHTC1_ADDRESS, SHTC3_CMD_SLEEP, COMMAND_SIZE);
}

static u8 sht_wakeup() {
static uint8_t sht_wakeup() {
if (!supports_sleep || !sleep_enabled)
return STATUS_OK;

return sensirion_i2c_write(SHTC1_ADDRESS, SHTC3_CMD_WAKEUP, COMMAND_SIZE);
}

s8 sht_measure_blocking_read(s32 *temperature, s32 *humidity) {
s8 ret;
int8_t sht_measure_blocking_read(int32_t *temperature, int32_t *humidity) {
int8_t ret;

PM_WAKE(ret, sht_measure());
#if !defined(USE_SENSIRION_CLOCK_STRETCHING) || !USE_SENSIRION_CLOCK_STRETCHING
Expand All @@ -126,25 +126,27 @@ s8 sht_measure_blocking_read(s32 *temperature, s32 *humidity) {
return PM_SLEEP(ret);
}

s8 sht_measure() {
s8 ret;
int8_t sht_measure() {
int8_t ret;

return PM_WAKE(
ret, sensirion_i2c_write(SHTC1_ADDRESS, cmd_measure, COMMAND_SIZE));
}

s8 sht_read(s32 *temperature, s32 *humidity) {
s8 ret = sht_common_read_measurement(SHTC1_ADDRESS, temperature, humidity);
int8_t sht_read(int32_t *temperature, int32_t *humidity) {
int8_t ret =
sht_common_read_measurement(SHTC1_ADDRESS, temperature, humidity);

return PM_SLEEP(ret);
}

s8 sht_probe() {
u8 data[3];
u16 id;
int8_t sht_probe() {
uint8_t data[3];
uint16_t id;

sensirion_i2c_init();
s8 ret = sensirion_i2c_write(SHTC1_ADDRESS, CMD_READ_ID_REG, COMMAND_SIZE);
int8_t ret =
sensirion_i2c_write(SHTC1_ADDRESS, CMD_READ_ID_REG, COMMAND_SIZE);
if (ret) {
/* SHTC3 that's sleeping? */
if (sht_wakeup())
Expand All @@ -163,7 +165,7 @@ s8 sht_probe() {
if (ret)
return ret;

id = ((u16)data[0] << 8) | data[1];
id = ((uint16_t)data[0] << 8) | data[1];
if ((id & SHTC3_PRODUCT_CODE_MASK) == SHTC3_PRODUCT_CODE) {
supports_sleep = 1;
return sht_sleep();
Expand All @@ -177,7 +179,7 @@ s8 sht_probe() {
return STATUS_UNKNOWN_DEVICE;
}

s8 sht_disable_sleep(u8 disable_sleep) {
int8_t sht_disable_sleep(uint8_t disable_sleep) {
if (!supports_sleep)
return STATUS_FAIL;

Expand All @@ -189,14 +191,14 @@ s8 sht_disable_sleep(u8 disable_sleep) {
return sht_sleep();
}

void sht_enable_low_power_mode(u8 enable_low_power_mode) {
void sht_enable_low_power_mode(uint8_t enable_low_power_mode) {
cmd_measure = enable_low_power_mode ? CMD_MEASURE_LPM : CMD_MEASURE_HPM;
}

const char *sht_get_driver_version() {
return SHT_DRV_VERSION_STR;
}

u8 sht_get_configured_sht_address() {
uint8_t sht_get_configured_sht_address() {
return SHTC1_ADDRESS;
}

0 comments on commit 9a6924a

Please sign in to comment.