Skip to content

Commit d49bbe0

Browse files
committed
drivers: sensor: STCC4 Add Driver
This adds support for Sensirion's STCC4 co2 sensor. Signed-off-by: Jan Faeh <[email protected]>
1 parent f1c2ce4 commit d49bbe0

File tree

14 files changed

+1543
-0
lines changed

14 files changed

+1543
-0
lines changed

drivers/sensor/sensirion/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
# zephyr-keep-sorted-start
5+
add_subdirectory(sensirion_core)
56
add_subdirectory_ifdef(CONFIG_SCD4X scd4x)
67
add_subdirectory_ifdef(CONFIG_SGP40 sgp40)
78
add_subdirectory_ifdef(CONFIG_SHT3XD sht3xd)
89
add_subdirectory_ifdef(CONFIG_SHT4X sht4x)
910
add_subdirectory_ifdef(CONFIG_SHTCX shtcx)
11+
add_subdirectory_ifdef(CONFIG_STCC4 stcc4)
1012
add_subdirectory_ifdef(CONFIG_STS4X sts4x)
1113
# zephyr-keep-sorted-stop

drivers/sensor/sensirion/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ source "drivers/sensor/sensirion/sgp40/Kconfig"
77
source "drivers/sensor/sensirion/sht3xd/Kconfig"
88
source "drivers/sensor/sensirion/sht4x/Kconfig"
99
source "drivers/sensor/sensirion/shtcx/Kconfig"
10+
source "drivers/sensor/sensirion/stcc4/Kconfig"
1011
source "drivers/sensor/sensirion/sts4x/Kconfig"
1112
# zephyr-keep-sorted-stop
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2025 Sensirion
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
zephyr_library()
6+
7+
zephyr_library_sources(sensirion_common.c)
8+
zephyr_library_sources(sensirion_i2c.c)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2025 Sensirion
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "sensirion_common.h"
8+
9+
float sensirion_common_bytes_to_float(const uint8_t *bytes)
10+
{
11+
union {
12+
uint32_t u32_value;
13+
float float32;
14+
} tmp;
15+
16+
tmp.u32_value = sys_get_be32(bytes);
17+
return tmp.float32;
18+
}
19+
20+
void sensirion_common_float_to_bytes(const float value, uint8_t *bytes)
21+
{
22+
union {
23+
uint32_t u32_value;
24+
float float32;
25+
} tmp;
26+
tmp.float32 = value;
27+
sys_put_be32(tmp.u32_value, bytes);
28+
}
29+
30+
void sensirion_common_to_integer(const uint8_t *source, uint8_t *destination, INT_TYPE int_type,
31+
uint8_t data_length)
32+
{
33+
if (data_length > int_type) {
34+
data_length = 0;
35+
}
36+
37+
uint8_t offset = int_type - data_length;
38+
39+
for (uint8_t i = 0; i < offset; i++) {
40+
destination[int_type - i - 1] = 0;
41+
}
42+
43+
for (uint8_t i = 1; i <= data_length; i++) {
44+
destination[int_type - offset - i] = source[i - 1];
45+
}
46+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2025 Sensirion
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef SENSIRION_COMMON_H
8+
#define SENSIRION_COMMON_H
9+
10+
#ifdef __cplusplus
11+
extern "C" {
12+
#endif
13+
14+
#include <stdint.h>
15+
#include <zephyr/sys/byteorder.h>
16+
17+
#define NO_ERROR 0
18+
#define NOT_IMPLEMENTED_ERROR ENOSYS
19+
20+
#define SENSIRION_COMMAND_SIZE 2
21+
#define SENSIRION_WORD_SIZE 2
22+
#define SENSIRION_NUM_WORDS(x) (sizeof(x) / SENSIRION_WORD_SIZE)
23+
#define SENSIRION_MAX_BUFFER_WORDS 32
24+
25+
#define sensirion_common_bytes_to_int16_t(src) ((int16_t)sys_get_be16((src)))
26+
#define sensirion_common_bytes_to_uint16_t(src) (sys_get_be16((src)))
27+
#define sensirion_common_bytes_to_int32_t(src) ((int32_t)sys_get_be32((src)))
28+
#define sensirion_common_bytes_to_uint32_t(src) (sys_get_be32((src)))
29+
30+
#define sensirion_common_uint32_t_to_bytes(val, dst) (sys_put_be32((val), (dst)))
31+
#define sensirion_common_int32_t_to_bytes(val, dst) (sys_put_be32((uint32_t)(val), (dst)))
32+
#define sensirion_common_uint16_t_to_bytes(val, dst) (sys_put_be16((val), (dst)))
33+
#define sensirion_common_int16_t_to_bytes(val, dst) (sys_put_be16((uint16_t)(val), (dst)))
34+
#define sensirion_common_copy_bytes(src, dst, len) (memcpy((dst), (src), (len)))
35+
36+
/**
37+
* Enum to describe the type of an integer
38+
*/
39+
typedef enum {
40+
BYTE = 1,
41+
SHORT = 2,
42+
INTEGER = 4,
43+
LONG_INTEGER = 8
44+
} INT_TYPE;
45+
46+
/**
47+
* Convert an array of bytes to a float
48+
*
49+
* Convert an array of bytes received from the sensor in big-endian/MSB-first
50+
* format to an float value in the correct system-endianness.
51+
*
52+
* @param bytes An array of at least four bytes (MSB first)
53+
* @return The byte array represented as float
54+
*/
55+
float sensirion_common_bytes_to_float(const uint8_t *bytes);
56+
57+
/**
58+
* Convert an float to an array of bytes
59+
*
60+
* Convert an float value in system-endianness to big-endian/MBS-first
61+
* format to send to the sensor.
62+
*
63+
* @param value Value to convert
64+
* @param bytes An array of at least four bytes
65+
*/
66+
void sensirion_common_float_to_bytes(const float value, uint8_t *bytes);
67+
68+
/**
69+
* Copy bytes from byte array to integer.
70+
*
71+
* @param source Array of bytes to be copied.
72+
* @param int_value Pointer to integer of bytes to be copied to.
73+
* @param int_type Type (size) of the integer to be copied.
74+
* @param data_length Number of bytes to copy.
75+
*/
76+
void sensirion_common_to_integer(const uint8_t *source, uint8_t *destination, INT_TYPE int_type,
77+
uint8_t data_length);
78+
79+
#ifdef __cplusplus
80+
}
81+
#endif
82+
83+
#endif /* SENSIRION_COMMON_H */

0 commit comments

Comments
 (0)