1 /*
2  * Copyright (c) 2022, Thomas Stranger
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Extended public API for 1-Wire Sensors
10  *
11  * This header file exposes an attribute an helper function to allow the
12  * runtime configuration of ROM IDs for 1-Wire Sensors.
13  */
14 
15 #ifndef ZEPHYR_INCLUDE_DRIVERS_SENSOR_W1_SENSOR_H_
16 #define ZEPHYR_INCLUDE_DRIVERS_SENSOR_W1_SENSOR_H_
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include <zephyr/drivers/sensor.h>
23 #include <zephyr/drivers/w1.h>
24 
25 /**
26  * @brief 1-Wire Sensor API
27  * @defgroup w1_sensor 1-Wire Sensor API
28  * @ingroup w1_interface
29  * @{
30  */
31 
32 enum sensor_attribute_w1 {
33 	/** Device unique 1-Wire ROM */
34 	SENSOR_ATTR_W1_ROM = SENSOR_ATTR_PRIV_START,
35 };
36 
37 /**
38  * @brief Function to write a w1_rom struct to an sensor value ptr.
39  *
40  * @param rom  Pointer to the rom struct.
41  * @param val  Pointer to the sensor value.
42  */
w1_rom_to_sensor_value(const struct w1_rom * rom,struct sensor_value * val)43 static inline void w1_rom_to_sensor_value(const struct w1_rom *rom,
44 					  struct sensor_value *val)
45 {
46 	val->val1 = sys_get_be64((uint8_t *)rom) & BIT64_MASK(32);
47 	val->val2 = sys_get_be64((uint8_t *)rom) >> 32;
48 }
49 
50 /**
51  * @brief Function to write an rom id stored in a sensor value to a struct w1_rom ptr.
52  *
53  * @param val  Sensor_value representing the rom.
54  * @param rom  The rom struct ptr.
55  */
w1_sensor_value_to_rom(const struct sensor_value * val,struct w1_rom * rom)56 static inline void w1_sensor_value_to_rom(const struct sensor_value *val,
57 					  struct w1_rom *rom)
58 {
59 	uint64_t temp64 = ((uint64_t)((uint32_t)val->val2) << 32)
60 			  | (uint32_t)val->val1;
61 	sys_put_be64(temp64, (uint8_t *)rom);
62 }
63 
64 /**
65  * @}
66  */
67 
68 #ifdef __cplusplus
69 }
70 #endif
71 
72 #endif /* ZEPHYR_INCLUDE_DRIVERS_SENSOR_W1_SENSOR_H_ */
73