1 /*
2 * Copyright (c) 2023 Google LLC
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #include "akm09918c.h"
7
8 #define DT_DRV_COMPAT asahi_kasei_akm09918c
9
akm09918c_decoder_get_frame_count(const uint8_t * buffer,struct sensor_chan_spec chan_spec,uint16_t * frame_count)10 static int akm09918c_decoder_get_frame_count(const uint8_t *buffer,
11 struct sensor_chan_spec chan_spec,
12 uint16_t *frame_count)
13 {
14 ARG_UNUSED(buffer);
15 ARG_UNUSED(chan_spec);
16
17 /* This sensor lacks a FIFO; there will always only be one frame at a time. */
18 *frame_count = 1;
19 return 0;
20 }
21
akm09918c_decoder_get_size_info(struct sensor_chan_spec chan_spec,size_t * base_size,size_t * frame_size)22 static int akm09918c_decoder_get_size_info(struct sensor_chan_spec chan_spec, size_t *base_size,
23 size_t *frame_size)
24 {
25 switch (chan_spec.chan_type) {
26 case SENSOR_CHAN_MAGN_X:
27 case SENSOR_CHAN_MAGN_Y:
28 case SENSOR_CHAN_MAGN_Z:
29 case SENSOR_CHAN_MAGN_XYZ:
30 *base_size = sizeof(struct sensor_three_axis_data);
31 *frame_size = sizeof(struct sensor_three_axis_sample_data);
32 return 0;
33 default:
34 return -ENOTSUP;
35 }
36 }
37
38 /** Fixed shift value to use. All channels (MAGN_X, _Y, and _Z) have the same fixed range of
39 * +/- 49.12 Gauss.
40 */
41 #define AKM09918C_SHIFT (6)
42
akm09918c_convert_raw_to_q31(int16_t reading,q31_t * out)43 static int akm09918c_convert_raw_to_q31(int16_t reading, q31_t *out)
44 {
45 int64_t intermediate = ((int64_t)reading * AKM09918C_MICRO_GAUSS_PER_BIT) *
46 ((int64_t)INT32_MAX + 1) /
47 ((1 << AKM09918C_SHIFT) * INT64_C(1000000));
48
49 *out = CLAMP(intermediate, INT32_MIN, INT32_MAX);
50 return 0;
51 }
52
akm09918c_decoder_decode(const uint8_t * buffer,struct sensor_chan_spec chan_spec,uint32_t * fit,uint16_t max_count,void * data_out)53 static int akm09918c_decoder_decode(const uint8_t *buffer, struct sensor_chan_spec chan_spec,
54 uint32_t *fit, uint16_t max_count, void *data_out)
55 {
56 const struct akm09918c_encoded_data *edata = (const struct akm09918c_encoded_data *)buffer;
57
58 if (*fit != 0) {
59 return 0;
60 }
61
62 switch (chan_spec.chan_type) {
63 case SENSOR_CHAN_MAGN_X:
64 case SENSOR_CHAN_MAGN_Y:
65 case SENSOR_CHAN_MAGN_Z:
66 case SENSOR_CHAN_MAGN_XYZ: {
67 struct sensor_three_axis_data *out = data_out;
68
69 out->header.base_timestamp_ns = edata->header.timestamp;
70 out->header.reading_count = 1;
71 out->shift = AKM09918C_SHIFT;
72
73 akm09918c_convert_raw_to_q31(edata->readings[0], &out->readings[0].x);
74 akm09918c_convert_raw_to_q31(edata->readings[1], &out->readings[0].y);
75 akm09918c_convert_raw_to_q31(edata->readings[2], &out->readings[0].z);
76 *fit = 1;
77
78 return 1;
79 }
80 default:
81 return -EINVAL;
82 }
83 }
84
85 SENSOR_DECODER_API_DT_DEFINE() = {
86 .get_frame_count = akm09918c_decoder_get_frame_count,
87 .get_size_info = akm09918c_decoder_get_size_info,
88 .decode = akm09918c_decoder_decode,
89 };
90
akm09918c_get_decoder(const struct device * dev,const struct sensor_decoder_api ** decoder)91 int akm09918c_get_decoder(const struct device *dev, const struct sensor_decoder_api **decoder)
92 {
93 ARG_UNUSED(dev);
94 *decoder = &SENSOR_DECODER_NAME();
95
96 return 0;
97 }
98