Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions drivers/sensor/sensirion/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
# SPDX-License-Identifier: Apache-2.0

# zephyr-keep-sorted-start
add_subdirectory(sensirion_core)
add_subdirectory_ifdef(CONFIG_SCD4X scd4x)
add_subdirectory_ifdef(CONFIG_SGP40 sgp40)
add_subdirectory_ifdef(CONFIG_SHT3XD sht3xd)
add_subdirectory_ifdef(CONFIG_SHT4X sht4x)
add_subdirectory_ifdef(CONFIG_SHTCX shtcx)
add_subdirectory_ifdef(CONFIG_STCC4 stcc4)
add_subdirectory_ifdef(CONFIG_STS4X sts4x)
# zephyr-keep-sorted-stop
1 change: 1 addition & 0 deletions drivers/sensor/sensirion/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ source "drivers/sensor/sensirion/sgp40/Kconfig"
source "drivers/sensor/sensirion/sht3xd/Kconfig"
source "drivers/sensor/sensirion/sht4x/Kconfig"
source "drivers/sensor/sensirion/shtcx/Kconfig"
source "drivers/sensor/sensirion/stcc4/Kconfig"
source "drivers/sensor/sensirion/sts4x/Kconfig"
# zephyr-keep-sorted-stop
8 changes: 8 additions & 0 deletions drivers/sensor/sensirion/sensirion_core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2025 Sensirion
#
# SPDX-License-Identifier: Apache-2.0
#
zephyr_library()

zephyr_library_sources(sensirion_common.c)
zephyr_library_sources(sensirion_i2c.c)
46 changes: 46 additions & 0 deletions drivers/sensor/sensirion/sensirion_core/sensirion_common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2025 Sensirion
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "sensirion_common.h"

float sensirion_common_bytes_to_float(const uint8_t *bytes)
{
union {
uint32_t u32_value;
float float32;
} tmp;

tmp.u32_value = sys_get_be32(bytes);
return tmp.float32;
}

void sensirion_common_float_to_bytes(const float value, uint8_t *bytes)
{
union {
uint32_t u32_value;
float float32;
} tmp;
tmp.float32 = value;
sys_put_be32(tmp.u32_value, bytes);
}

void sensirion_common_to_integer(const uint8_t *source, uint8_t *destination, INT_TYPE int_type,
uint8_t data_length)
{
if (data_length > int_type) {
data_length = 0;
}

uint8_t offset = int_type - data_length;

for (uint8_t i = 0; i < offset; i++) {
destination[int_type - i - 1] = 0;
}

for (uint8_t i = 1; i <= data_length; i++) {
destination[int_type - offset - i] = source[i - 1];
}
}
83 changes: 83 additions & 0 deletions drivers/sensor/sensirion/sensirion_core/sensirion_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2025 Sensirion
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef SENSIRION_COMMON_H
#define SENSIRION_COMMON_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <zephyr/sys/byteorder.h>

#define NO_ERROR 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use standard errnos?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't 0 the errno for no error?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A define is not required. Please use errno directly without wrapping them in another define.

#define NOT_IMPLEMENTED_ERROR ENOSYS

#define SENSIRION_COMMAND_SIZE 2
#define SENSIRION_WORD_SIZE 2
#define SENSIRION_NUM_WORDS(x) (sizeof(x) / SENSIRION_WORD_SIZE)
#define SENSIRION_MAX_BUFFER_WORDS 32

#define sensirion_common_bytes_to_int16_t(src) ((int16_t)sys_get_be16((src)))
#define sensirion_common_bytes_to_uint16_t(src) (sys_get_be16((src)))
#define sensirion_common_bytes_to_int32_t(src) ((int32_t)sys_get_be32((src)))
#define sensirion_common_bytes_to_uint32_t(src) (sys_get_be32((src)))

#define sensirion_common_uint32_t_to_bytes(val, dst) (sys_put_be32((val), (dst)))
#define sensirion_common_int32_t_to_bytes(val, dst) (sys_put_be32((uint32_t)(val), (dst)))
#define sensirion_common_uint16_t_to_bytes(val, dst) (sys_put_be16((val), (dst)))
#define sensirion_common_int16_t_to_bytes(val, dst) (sys_put_be16((uint16_t)(val), (dst)))
#define sensirion_common_copy_bytes(src, dst, len) (memcpy((dst), (src), (len)))

/**
* Enum to describe the type of an integer
*/
typedef enum {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this:

  • An integer is not always 4 bytes
  • Names might clash with other defines since they are very generic.
  • Seems like only LONG_INTEGER is used.

BYTE = 1,
SHORT = 2,
INTEGER = 4,
LONG_INTEGER = 8
} INT_TYPE;

/**
* Convert an array of bytes to a float
*
* Convert an array of bytes received from the sensor in big-endian/MSB-first
* format to an float value in the correct system-endianness.
*
* @param bytes An array of at least four bytes (MSB first)
* @return The byte array represented as float
*/
float sensirion_common_bytes_to_float(const uint8_t *bytes);

/**
* Convert an float to an array of bytes
*
* Convert an float value in system-endianness to big-endian/MBS-first
* format to send to the sensor.
*
* @param value Value to convert
* @param bytes An array of at least four bytes
*/
void sensirion_common_float_to_bytes(const float value, uint8_t *bytes);

/**
* Copy bytes from byte array to integer.
*
* @param source Array of bytes to be copied.
* @param int_value Pointer to integer of bytes to be copied to.
* @param int_type Type (size) of the integer to be copied.
* @param data_length Number of bytes to copy.
*/
void sensirion_common_to_integer(const uint8_t *source, uint8_t *destination, INT_TYPE int_type,
uint8_t data_length);

#ifdef __cplusplus
}
#endif

#endif /* SENSIRION_COMMON_H */
Loading