|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Felipe Neves |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT ams_as5048 |
| 8 | + |
| 9 | +#include <errno.h> |
| 10 | +#include <zephyr/device.h> |
| 11 | +#include <zephyr/drivers/sensor.h> |
| 12 | +#include <zephyr/drivers/spi.h> |
| 13 | +#include <zephyr/init.h> |
| 14 | +#include <zephyr/logging/log.h> |
| 15 | +#include <zephyr/sys/byteorder.h> |
| 16 | + |
| 17 | +LOG_MODULE_REGISTER(ams_as5048, CONFIG_SENSOR_LOG_LEVEL); |
| 18 | + |
| 19 | +#define AS5048_REG_ANGLE 0x3FFF |
| 20 | +#define AS5048_READ_BIT BIT(14) |
| 21 | +#define AS5048_PARITY_BIT BIT(15) |
| 22 | +#define AS5048_DATA_MASK GENMASK(13, 0) |
| 23 | +#define AS5048_ERROR_BIT BIT(14) |
| 24 | +#define AS5048_MAX_STEPS 16384 |
| 25 | +#define AS5048_FULL_ANGLE_DEG 360 |
| 26 | +#define AS5048_MICRO_DEGREE 1000000 |
| 27 | + |
| 28 | +struct as5048_config { |
| 29 | + struct spi_dt_spec spi; |
| 30 | +}; |
| 31 | + |
| 32 | +struct as5048_data { |
| 33 | + uint16_t angle_raw; |
| 34 | +}; |
| 35 | + |
| 36 | +static uint16_t as5048_build_read_command(uint16_t reg) |
| 37 | +{ |
| 38 | + uint16_t cmd = reg & AS5048_DATA_MASK; |
| 39 | + |
| 40 | + cmd |= AS5048_READ_BIT; |
| 41 | + |
| 42 | + /* Even parity across bits 0..14 (read bit + address). */ |
| 43 | + if (__builtin_parity(cmd)) { |
| 44 | + cmd |= AS5048_PARITY_BIT; |
| 45 | + } |
| 46 | + |
| 47 | + return cmd; |
| 48 | +} |
| 49 | + |
| 50 | +static int as5048_sample_fetch(const struct device *dev, enum sensor_channel chan) |
| 51 | +{ |
| 52 | + struct as5048_data *data = dev->data; |
| 53 | + const struct as5048_config *cfg = dev->config; |
| 54 | + uint16_t tx_buf[2]; |
| 55 | + uint16_t rx_buf[2]; |
| 56 | + const struct spi_buf tx[] = { |
| 57 | + { |
| 58 | + .buf = &tx_buf[0], |
| 59 | + .len = sizeof(tx_buf[0]), |
| 60 | + }, |
| 61 | + { |
| 62 | + .buf = &tx_buf[1], |
| 63 | + .len = sizeof(tx_buf[1]), |
| 64 | + }, |
| 65 | + }; |
| 66 | + struct spi_buf rx[] = { |
| 67 | + { |
| 68 | + .buf = &rx_buf[0], |
| 69 | + .len = sizeof(rx_buf[0]), |
| 70 | + }, |
| 71 | + { |
| 72 | + .buf = &rx_buf[1], |
| 73 | + .len = sizeof(rx_buf[1]), |
| 74 | + }, |
| 75 | + }; |
| 76 | + int ret; |
| 77 | + |
| 78 | + if ((chan != SENSOR_CHAN_ALL) && (chan != SENSOR_CHAN_ROTATION)) { |
| 79 | + return -ENOTSUP; |
| 80 | + } |
| 81 | + |
| 82 | + tx_buf[0] = sys_cpu_to_be16(as5048_build_read_command(AS5048_REG_ANGLE)); |
| 83 | + tx_buf[1] = sys_cpu_to_be16(0x0000); |
| 84 | + |
| 85 | + ret = spi_transceive_dt(&cfg->spi, tx, ARRAY_SIZE(tx), rx, ARRAY_SIZE(rx)); |
| 86 | + if (ret < 0) { |
| 87 | + LOG_ERR("spi transceive failed (%d)", ret); |
| 88 | + return ret; |
| 89 | + } |
| 90 | + |
| 91 | + uint16_t raw = sys_be16_to_cpu(rx_buf[1]); |
| 92 | + |
| 93 | + if (raw & AS5048_ERROR_BIT) { |
| 94 | + LOG_ERR("AS5048 reported error (0x%04x)", raw); |
| 95 | + return -EIO; |
| 96 | + } |
| 97 | + |
| 98 | + data->angle_raw = raw & AS5048_DATA_MASK; |
| 99 | + |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | +static int as5048_channel_get(const struct device *dev, |
| 104 | + enum sensor_channel chan, |
| 105 | + struct sensor_value *val) |
| 106 | +{ |
| 107 | + struct as5048_data *data = dev->data; |
| 108 | + |
| 109 | + if (chan != SENSOR_CHAN_ROTATION) { |
| 110 | + return -ENOTSUP; |
| 111 | + } |
| 112 | + |
| 113 | + int32_t scaled = (int32_t)data->angle_raw * AS5048_FULL_ANGLE_DEG; |
| 114 | + |
| 115 | + val->val1 = scaled / AS5048_MAX_STEPS; |
| 116 | + val->val2 = ((scaled % AS5048_MAX_STEPS) * AS5048_MICRO_DEGREE) / AS5048_MAX_STEPS; |
| 117 | + |
| 118 | + return 0; |
| 119 | +} |
| 120 | + |
| 121 | +static int as5048_init(const struct device *dev) |
| 122 | +{ |
| 123 | + const struct as5048_config *cfg = dev->config; |
| 124 | + |
| 125 | + if (!spi_is_ready_dt(&cfg->spi)) { |
| 126 | + LOG_ERR("SPI device not ready"); |
| 127 | + return -ENODEV; |
| 128 | + } |
| 129 | + |
| 130 | + return 0; |
| 131 | +} |
| 132 | + |
| 133 | +static SENSOR_DEVICE_API(as5048_api) = { |
| 134 | + .sample_fetch = as5048_sample_fetch, |
| 135 | + .channel_get = as5048_channel_get, |
| 136 | +}; |
| 137 | + |
| 138 | +#define AS5048_INIT(inst) \ |
| 139 | + static struct as5048_data as5048_data_##inst; \ |
| 140 | + static const struct as5048_config as5048_config_##inst = { \ |
| 141 | + .spi = SPI_DT_SPEC_INST_GET(inst, \ |
| 142 | + SPI_WORD_SET(16) | SPI_TRANSFER_MSB, 0U), \ |
| 143 | + }; \ |
| 144 | + SENSOR_DEVICE_DT_INST_DEFINE(inst, as5048_init, NULL, \ |
| 145 | + &as5048_data_##inst, &as5048_config_##inst, \ |
| 146 | + POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \ |
| 147 | + &as5048_api); |
| 148 | + |
| 149 | +DT_INST_FOREACH_STATUS_OKAY(AS5048_INIT) |
0 commit comments