|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Croxel, Inc. |
| 3 | + * Copyright (c) 2025 CogniPilot Foundation |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#include <zephyr/drivers/sensor_clock.h> |
| 9 | +#include <zephyr/sys/util.h> |
| 10 | +#include "bmp581.h" |
| 11 | +#include "bmp581_decoder.h" |
| 12 | + |
| 13 | +#include <zephyr/logging/log.h> |
| 14 | +LOG_MODULE_REGISTER(BMP581_DECODER, CONFIG_SENSOR_LOG_LEVEL); |
| 15 | + |
| 16 | +static uint8_t bmp581_encode_channel(enum sensor_channel chan) |
| 17 | +{ |
| 18 | + uint8_t encode_bmask = 0; |
| 19 | + |
| 20 | + switch (chan) { |
| 21 | + case SENSOR_CHAN_AMBIENT_TEMP: |
| 22 | + encode_bmask |= BIT(0); |
| 23 | + break; |
| 24 | + case SENSOR_CHAN_PRESS: |
| 25 | + encode_bmask |= BIT(1); |
| 26 | + break; |
| 27 | + case SENSOR_CHAN_ALL: |
| 28 | + encode_bmask |= BIT(0) | BIT(1); |
| 29 | + break; |
| 30 | + default: |
| 31 | + break; |
| 32 | + } |
| 33 | + |
| 34 | + return encode_bmask; |
| 35 | +} |
| 36 | + |
| 37 | +int bmp581_encode(const struct device *dev, |
| 38 | + const struct sensor_read_config *read_config, |
| 39 | + uint8_t trigger_status, |
| 40 | + uint8_t *buf) |
| 41 | +{ |
| 42 | + struct bmp581_encoded_data *edata = (struct bmp581_encoded_data *)buf; |
| 43 | + struct bmp581_data *data = dev->data; |
| 44 | + uint64_t cycles; |
| 45 | + int err; |
| 46 | + |
| 47 | + edata->header.channels = 0; |
| 48 | + edata->header.press_en = data->osr_odr_press_config.press_en; |
| 49 | + |
| 50 | + if (trigger_status) { |
| 51 | + edata->header.channels |= bmp581_encode_channel(SENSOR_CHAN_ALL); |
| 52 | + } else { |
| 53 | + const struct sensor_chan_spec *const channels = read_config->channels; |
| 54 | + size_t num_channels = read_config->count; |
| 55 | + |
| 56 | + for (size_t i = 0; i < num_channels; i++) { |
| 57 | + edata->header.channels |= bmp581_encode_channel(channels[i].chan_type); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + err = sensor_clock_get_cycles(&cycles); |
| 62 | + if (err != 0) { |
| 63 | + return err; |
| 64 | + } |
| 65 | + |
| 66 | + edata->header.events = trigger_status ? BIT(0) : 0; |
| 67 | + edata->header.timestamp = sensor_clock_cycles_to_ns(cycles); |
| 68 | + |
| 69 | + return 0; |
| 70 | +} |
| 71 | + |
| 72 | +static int bmp581_decoder_get_frame_count(const uint8_t *buffer, |
| 73 | + struct sensor_chan_spec chan_spec, |
| 74 | + uint16_t *frame_count) |
| 75 | +{ |
| 76 | + const struct bmp581_encoded_data *edata = (const struct bmp581_encoded_data *)buffer; |
| 77 | + |
| 78 | + if (chan_spec.chan_idx != 0) { |
| 79 | + return -ENOTSUP; |
| 80 | + } |
| 81 | + |
| 82 | + uint8_t channel_request = bmp581_encode_channel(chan_spec.chan_type); |
| 83 | + |
| 84 | + /* Filter unknown channels and having no data */ |
| 85 | + if ((edata->header.channels & channel_request) != channel_request) { |
| 86 | + return -ENODATA; |
| 87 | + } |
| 88 | + |
| 89 | + *frame_count = 1; |
| 90 | + return 0; |
| 91 | +} |
| 92 | + |
| 93 | +static int bmp581_decoder_get_size_info(struct sensor_chan_spec chan_spec, |
| 94 | + size_t *base_size, |
| 95 | + size_t *frame_size) |
| 96 | +{ |
| 97 | + switch (chan_spec.chan_type) { |
| 98 | + case SENSOR_CHAN_AMBIENT_TEMP: |
| 99 | + case SENSOR_CHAN_PRESS: |
| 100 | + *base_size = sizeof(struct sensor_q31_data); |
| 101 | + *frame_size = sizeof(struct sensor_q31_sample_data); |
| 102 | + return 0; |
| 103 | + default: |
| 104 | + return -ENOTSUP; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +static int bmp581_decoder_decode(const uint8_t *buffer, |
| 109 | + struct sensor_chan_spec chan_spec, |
| 110 | + uint32_t *fit, |
| 111 | + uint16_t max_count, |
| 112 | + void *data_out) |
| 113 | +{ |
| 114 | + const struct bmp581_encoded_data *edata = (const struct bmp581_encoded_data *)buffer; |
| 115 | + uint8_t channel_request; |
| 116 | + |
| 117 | + if (*fit != 0) { |
| 118 | + return 0; |
| 119 | + } |
| 120 | + |
| 121 | + if (max_count == 0 || chan_spec.chan_idx != 0) { |
| 122 | + return -EINVAL; |
| 123 | + } |
| 124 | + |
| 125 | + channel_request = bmp581_encode_channel(chan_spec.chan_type); |
| 126 | + if ((channel_request & edata->header.channels) != channel_request) { |
| 127 | + return -ENODATA; |
| 128 | + } |
| 129 | + |
| 130 | + struct sensor_q31_data *out = data_out; |
| 131 | + |
| 132 | + out->header.base_timestamp_ns = edata->header.timestamp; |
| 133 | + out->header.reading_count = 1; |
| 134 | + |
| 135 | + switch (chan_spec.chan_type) { |
| 136 | + case SENSOR_CHAN_AMBIENT_TEMP: { |
| 137 | + /* Temperature is in data[2:0], data[2] is integer part */ |
| 138 | + uint32_t raw_temp = ((uint32_t)edata->payload[2] << 16) | |
| 139 | + ((uint16_t)edata->payload[1] << 8) | |
| 140 | + edata->payload[0]; |
| 141 | + int32_t raw_temp_signed = sign_extend(raw_temp, 23); |
| 142 | + |
| 143 | + out->shift = (31 - 16); /* 16 left shifts gives us the value in celsius */ |
| 144 | + out->readings[0].value = raw_temp_signed; |
| 145 | + break; |
| 146 | + } |
| 147 | + case SENSOR_CHAN_PRESS: |
| 148 | + if (!edata->header.press_en) { |
| 149 | + return -ENODATA; |
| 150 | + } |
| 151 | + /* Shift by 10 bits because we'll divide by 1000 to make it kPa */ |
| 152 | + uint64_t raw_press = (((uint32_t)edata->payload[5] << 16) | |
| 153 | + ((uint16_t)edata->payload[4] << 8) | |
| 154 | + edata->payload[3]); |
| 155 | + |
| 156 | + int64_t raw_press_signed = sign_extend_64(raw_press, 23); |
| 157 | + |
| 158 | + raw_press_signed *= 1024; |
| 159 | + raw_press_signed /= 1000; |
| 160 | + |
| 161 | + /* Original value was in Pa by left-shifting 6 spaces, but |
| 162 | + * we've multiplied by 2^10 to not lose precision when |
| 163 | + * converting to kPa. Hence, left-shift 16 spaces. |
| 164 | + */ |
| 165 | + out->shift = (31 - 6 - 10); |
| 166 | + out->readings[0].value = (int32_t)raw_press_signed; |
| 167 | + break; |
| 168 | + default: |
| 169 | + return -EINVAL; |
| 170 | + } |
| 171 | + |
| 172 | + *fit = 1; |
| 173 | + return 1; |
| 174 | +} |
| 175 | + |
| 176 | +static bool bmp581_decoder_has_trigger(const uint8_t *buffer, enum sensor_trigger_type trigger) |
| 177 | +{ |
| 178 | + const struct bmp581_encoded_data *edata = (const struct bmp581_encoded_data *)buffer; |
| 179 | + |
| 180 | + if ((trigger == SENSOR_TRIG_DATA_READY) && (edata->header.events != 0)) { |
| 181 | + return true; |
| 182 | + } |
| 183 | + |
| 184 | + return false; |
| 185 | +} |
| 186 | + |
| 187 | +SENSOR_DECODER_API_DT_DEFINE() = { |
| 188 | + .get_frame_count = bmp581_decoder_get_frame_count, |
| 189 | + .get_size_info = bmp581_decoder_get_size_info, |
| 190 | + .decode = bmp581_decoder_decode, |
| 191 | + .has_trigger = bmp581_decoder_has_trigger, |
| 192 | +}; |
| 193 | + |
| 194 | +int bmp581_get_decoder(const struct device *dev, const struct sensor_decoder_api **decoder) |
| 195 | +{ |
| 196 | + ARG_UNUSED(dev); |
| 197 | + *decoder = &SENSOR_DECODER_NAME(); |
| 198 | + return 0; |
| 199 | +} |
0 commit comments