kfsw-modules/temperature-sensor-example/src/temperature_sensor_example_sensor.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdint.h> | ||
| 3 | |||
| 4 | #include <zephyr/device.h> | ||
| 5 | #include <zephyr/devicetree.h> | ||
| 6 | #include <zephyr/drivers/sensor.h> | ||
| 7 | #include <zephyr/sys/util.h> | ||
| 8 | |||
| 9 | #include "temperature_sensor_example_internal.h" | ||
| 10 | |||
| 11 | /* TODO(template): the hardware backend. Bind the device from devicetree here and | ||
| 12 | * keep everything else in the generic file, so the module builds and tests | ||
| 13 | * with this file left out. | ||
| 14 | */ | ||
| 15 | #define KFSW_TEMP_EXAMPLE_NODE DT_CHOSEN(kfsw_die_temp) | ||
| 16 | |||
| 17 | BUILD_ASSERT(DT_NODE_EXISTS(KFSW_TEMP_EXAMPLE_NODE), | ||
| 18 | "kfsw,die-temp must select a temperature sensor node"); | ||
| 19 | |||
| 20 | static const struct device *const temp_sensor = DEVICE_DT_GET(KFSW_TEMP_EXAMPLE_NODE); | ||
| 21 | |||
| 22 | 2 | int kfsw_temp_example_sensor_prepare(void) | |
| 23 | { | ||
| 24 | 2 | if (!device_is_ready(temp_sensor)) { | |
| 25 | ✗ | return -EACCES; | |
| 26 | } | ||
| 27 | 2 | return 0; | |
| 28 | } | ||
| 29 | |||
| 30 | 8 | int kfsw_temp_example_sensor_read(int32_t *milli_c) | |
| 31 | { | ||
| 32 | struct sensor_value value; | ||
| 33 | int result; | ||
| 34 | |||
| 35 | 8 | if (milli_c == NULL) { | |
| 36 | ✗ | return -EINVAL; | |
| 37 | } | ||
| 38 | |||
| 39 | /* TODO(template): the conversion of one reading for this sensor. */ | ||
| 40 | 8 | result = sensor_sample_fetch_chan(temp_sensor, SENSOR_CHAN_DIE_TEMP); | |
| 41 | 6 | if (result != 0) { | |
| 42 | 2 | return result; | |
| 43 | } | ||
| 44 | 4 | result = sensor_channel_get(temp_sensor, SENSOR_CHAN_DIE_TEMP, &value); | |
| 45 | 4 | if (result != 0) { | |
| 46 | ✗ | return result; | |
| 47 | } | ||
| 48 | |||
| 49 | /* val1 is whole degrees and val2 millionths, carrying the same sign, so | ||
| 50 | * this holds either side of zero. | ||
| 51 | */ | ||
| 52 | 4 | *milli_c = (value.val1 * 1000) + (value.val2 / 1000); | |
| 53 | 4 | return 0; | |
| 54 | } | ||
| 55 |