From 515e6a38117aa5c7f97530b9e0ad941c6db58398 Mon Sep 17 00:00:00 2001 From: Deomid Ryabkov Date: Fri, 1 Mar 2019 18:57:56 +0000 Subject: [PATCH] Add STLM75 temperatire sensor driver library CL: Add STLM75 temperature sensor driver library PUBLISHED_FROM=fa85465f4db0e970f5ac371a1eaae9b69ec072cd --- LICENSE | 14 +++++++ include/mgos_stlm75.h | 82 ++++++++++++++++++++++++++++++++++++++ mos.yml | 16 ++++++++ src/mgos_stlm75.c | 91 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 203 insertions(+) create mode 100644 LICENSE create mode 100644 include/mgos_stlm75.h create mode 100644 mos.yml create mode 100644 src/mgos_stlm75.c diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6be1448 --- /dev/null +++ b/LICENSE @@ -0,0 +1,14 @@ +Copyright (c) 2019 Cesanta Software Limited +All rights reserved + +Licensed under the Apache License, Version 2.0 (the ""License""); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an ""AS IS"" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/include/mgos_stlm75.h b/include/mgos_stlm75.h new file mode 100644 index 0000000..19f9b9d --- /dev/null +++ b/include/mgos_stlm75.h @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2014-2019 Cesanta Software Limited + * All rights reserved + * + * Licensed under the Apache License, Version 2.0 (the ""License""); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an ""AS IS"" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "mgos_i2c.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// Lower 3 bits are determined by A2,A1,A0 pins, hence 0x48-0x4f are possible. +#define MGOS_STLM75_ADDR_000 0x48 +#define MGOS_STLM75_ADDR_001 0x49 +#define MGOS_STLM75_ADDR_010 0x4a +#define MGOS_STLM75_ADDR_011 0x4b +#define MGOS_STLM75_ADDR_100 0x4c +#define MGOS_STLM75_ADDR_101 0x4d +#define MGOS_STLM75_ADDR_110 0x4e +#define MGOS_STLM75_ADDR_111 0x4f + +struct mgos_stlm75 { + struct mgos_i2c *i2c; + uint16_t addr; +}; + +struct mgos_stlm75 *mgos_stlm75_create(struct mgos_i2c *i2c, uint16_t addr); + +void mgos_stlm75_init(struct mgos_stlm75 *ctx, struct mgos_i2c *i2c, + uint16_t addr); + +bool mgos_stlm75_read_temp(struct mgos_stlm75 *ctx, float *temp); + +// Interrup vs comparator mode. See datasheet for detailed explanation. +enum mgos_stlm75_int_mode { + MGOS_STLM75_INT_MODE_COMP = 0, + MGOS_STLM75_INT_MODE_INT = 1, +}; + +// Interrupt active polarity. +enum mgos_stlm75_int_pol { + MGOS_STLM75_INT_POL_NEG = 0, + MGOS_STLM75_INT_POL_POS = 1, +}; + +// "Fault tolerance" - the temperature value must exceed threshold +// for 1, 2, 4 or 6 cycles before interrupt is asserted. +enum mgos_stlm75_int_ft { + MGOS_STLM75_INT_FT_1 = 0, + MGOS_STLM75_INT_FT_2 = 1, + MGOS_STLM75_INT_FT_4 = 2, + MGOS_STLM75_INT_FT_6 = 3, +}; + +bool mgos_stlm75_set_alarm(struct mgos_stlm75 *ctx, float hi, float lo, + enum mgos_stlm75_int_mode mode, + enum mgos_stlm75_int_pol pol, + enum mgos_stlm75_int_ft ft); + +// Put the device in or out of shutdown. +bool mgos_stlm75_shutdown(struct mgos_stlm75 *ctx, bool shutdown); + +void mgos_stlm75_free(struct mgos_stlm75 *ctx); + +#ifdef __cplusplus +} +#endif diff --git a/mos.yml b/mos.yml new file mode 100644 index 0000000..8ede4fe --- /dev/null +++ b/mos.yml @@ -0,0 +1,16 @@ +author: mongoose-os +type: lib +description: STMicro STLM75 temperature sensor driver +version: 1.0 +sources: + - src +includes: + - include +libs: + - origin: https://github.com/mongoose-os-libs/i2c +tags: + - c + - hw + - docs:drivers:STLM75 + +manifest_version: 2017-09-29 diff --git a/src/mgos_stlm75.c b/src/mgos_stlm75.c new file mode 100644 index 0000000..43d2e6e --- /dev/null +++ b/src/mgos_stlm75.c @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2014-2019 Cesanta Software Limited + * All rights reserved + * + * Licensed under the Apache License, Version 2.0 (the ""License""); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an ""AS IS"" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mgos_stlm75.h" + +#include + +#include "common/cs_dbg.h" + +#define MGOS_STLM75_REG_TEMP 0 +#define MGOS_STLM75_REG_CONF 1 +#define MGOS_STLM75_REG_TOS 2 +#define MGOS_STLM75_REG_THYS 3 + +static float reg_val_to_temp(uint16_t val) { + int16_t val16 = ((int16_t) val) >> 7; + return ((float) val16) * 0.5f; +} + +static uint16_t temp_to_reg_val(float temp) { + int16_t val16 = (int16_t)(temp / 0.5f); + return ((uint16_t)(val16 << 7)); +} + +struct mgos_stlm75 *mgos_stlm75_create(struct mgos_i2c *i2c, uint16_t addr) { + if (i2c == NULL) return NULL; + struct mgos_stlm75 *ctx = (struct mgos_stlm75 *) calloc(1, sizeof(*ctx)); + if (ctx != NULL) mgos_stlm75_init(ctx, i2c, addr); + return ctx; +} + +void mgos_stlm75_init(struct mgos_stlm75 *ctx, struct mgos_i2c *i2c, + uint16_t addr) { + ctx->i2c = i2c; + ctx->addr = addr; +} + +bool mgos_stlm75_read_temp(struct mgos_stlm75 *ctx, float *temp) { + int val = mgos_i2c_read_reg_w(ctx->i2c, ctx->addr, MGOS_STLM75_REG_TEMP); + if (val < 0) { + *temp = 0; + return false; + } + *temp = reg_val_to_temp(val); + return true; +} + +bool mgos_stlm75_set_alarm(struct mgos_stlm75 *ctx, float hi, float lo, + enum mgos_stlm75_int_mode mode, + enum mgos_stlm75_int_pol pol, + enum mgos_stlm75_int_ft ft) { + uint8_t cfg = ((((uint8_t) ft) << 3) | (((uint8_t) pol) << 2) | + (((uint8_t) mode) << 1)); + if (!mgos_i2c_write_reg_w(ctx->i2c, ctx->addr, MGOS_STLM75_REG_TOS, + temp_to_reg_val(hi)) || + !mgos_i2c_write_reg_w(ctx->i2c, ctx->addr, MGOS_STLM75_REG_THYS, + temp_to_reg_val(lo)) || + !mgos_i2c_setbits_reg_b(ctx->i2c, ctx->addr, MGOS_STLM75_REG_CONF, 1, 4, + (cfg >> 1))) { + return false; + } + LOG(LL_INFO, + ("Set %04x %04x %02x", + mgos_i2c_read_reg_w(ctx->i2c, ctx->addr, MGOS_STLM75_REG_TOS), + mgos_i2c_read_reg_w(ctx->i2c, ctx->addr, MGOS_STLM75_REG_THYS), + mgos_i2c_read_reg_b(ctx->i2c, ctx->addr, MGOS_STLM75_REG_CONF))); + return true; +} + +bool mgos_stlm75_shutdown(struct mgos_stlm75 *ctx, bool shutdown) { + return mgos_i2c_setbits_reg_b(ctx->i2c, ctx->addr, MGOS_STLM75_REG_CONF, 0, 1, + (uint8_t) shutdown); +} + +void mgos_stlm75_free(struct mgos_stlm75 *ctx) { + free(ctx); +}