-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add STLM75 temperatire sensor driver library
CL: Add STLM75 temperature sensor driver library PUBLISHED_FROM=fa85465f4db0e970f5ac371a1eaae9b69ec072cd
- Loading branch information
Showing
4 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <stdbool.h> | ||
#include <stdint.h> | ||
|
||
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <stdlib.h> | ||
|
||
#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); | ||
} |