Skip to content

Commit cd04926

Browse files
Felipe NevesMaureenHelm
authored andcommitted
sensors: as5600: added as5600
magnetic angle sensor driver. Signed-off-by: Felipe Neves <[email protected]>
1 parent 33ffda4 commit cd04926

File tree

7 files changed

+133
-0
lines changed

7 files changed

+133
-0
lines changed

drivers/sensor/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ add_subdirectory_ifdef(CONFIG_ADXL362 adxl362)
66
add_subdirectory_ifdef(CONFIG_ADXL372 adxl372)
77
add_subdirectory_ifdef(CONFIG_AK8975 ak8975)
88
add_subdirectory_ifdef(CONFIG_AMG88XX amg88xx)
9+
add_subdirectory_ifdef(CONFIG_AMS_AS5600 ams_as5600)
910
add_subdirectory_ifdef(CONFIG_AMS_IAQ_CORE ams_iAQcore)
1011
add_subdirectory_ifdef(CONFIG_APDS9960 apds9960)
1112
add_subdirectory_ifdef(CONFIG_BMA280 bma280)

drivers/sensor/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ source "drivers/sensor/ak8975/Kconfig"
5353

5454
source "drivers/sensor/amg88xx/Kconfig"
5555

56+
source "drivers/sensor/ams_as5600/Kconfig"
57+
5658
source "drivers/sensor/ams_iAQcore/Kconfig"
5759

5860
source "drivers/sensor/apds9960/Kconfig"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
zephyr_library()
4+
5+
zephyr_library_sources(ams_as5600.c)

drivers/sensor/ams_as5600/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# AS5600 Angular position sensor configuration option
2+
3+
# Copyright (c) 2022, Felipe Neves.
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
config AMS_AS5600
7+
bool "AS5600 Angular position sensor"
8+
default y
9+
depends on DT_HAS_AMS_AS5600_ENABLED
10+
select I2C
11+
help
12+
Enable driver for AS5600 Angular position sensor.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2022, Felipe Neves
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define DT_DRV_COMPAT ams_as5600
8+
9+
#include <errno.h>
10+
#include <zephyr/sys/__assert.h>
11+
#include <zephyr/sys/util.h>
12+
#include <zephyr/device.h>
13+
#include <zephyr/init.h>
14+
#include <zephyr/drivers/sensor.h>
15+
#include <zephyr/drivers/i2c.h>
16+
#include <zephyr/logging/log.h>
17+
LOG_MODULE_REGISTER(ams_as5600, CONFIG_SENSOR_LOG_LEVEL);
18+
19+
#define AS5600_ANGLE_REGISTER_H 0x0E
20+
#define AS5600_FULL_ANGLE 360
21+
#define AS5600_PULSES_PER_REV 4096
22+
23+
struct as5600_dev_cfg {
24+
struct i2c_dt_spec i2c_port;
25+
};
26+
27+
/* Device run time data */
28+
struct as5600_dev_data {
29+
uint16_t position;
30+
};
31+
32+
static int as5600_fetch(const struct device *dev, enum sensor_channel chan)
33+
{
34+
struct as5600_dev_data *dev_data = dev->data;
35+
const struct as5600_dev_cfg *dev_cfg = dev->config;
36+
37+
uint8_t read_data[2] = {0, 0};
38+
uint8_t angle_reg = AS5600_ANGLE_REGISTER_H;
39+
40+
int err = i2c_write_read_dt(&dev_cfg->i2c_port,
41+
&angle_reg,
42+
1,
43+
&read_data,
44+
sizeof(read_data));
45+
46+
/* invalid readings preserves the last good value */
47+
if (!err) {
48+
dev_data->position = ((uint16_t)read_data[0] << 8) | read_data[1];
49+
}
50+
51+
return err;
52+
}
53+
54+
static int as5600_get(const struct device *dev, enum sensor_channel chan,
55+
struct sensor_value *val)
56+
{
57+
struct as5600_dev_data *dev_data = dev->data;
58+
59+
if (chan == SENSOR_CHAN_ROTATION) {
60+
val->val1 = ((int32_t)dev_data->position * AS5600_FULL_ANGLE) /
61+
AS5600_PULSES_PER_REV;
62+
63+
val->val2 = ((int32_t)dev_data->position * AS5600_FULL_ANGLE) -
64+
(val->val1 * AS5600_PULSES_PER_REV);
65+
} else {
66+
return -ENOTSUP;
67+
}
68+
69+
return 0;
70+
}
71+
72+
static int as5600_initialize(const struct device *dev)
73+
{
74+
struct as5600_dev_data *const dev_data = dev->data;
75+
76+
dev_data->position = 0;
77+
78+
LOG_INF("Device %s initialized", dev->name);
79+
80+
return 0;
81+
}
82+
83+
static const struct sensor_driver_api as5600_driver_api = {
84+
.sample_fetch = as5600_fetch,
85+
.channel_get = as5600_get,
86+
};
87+
88+
#define AS5600_INIT(n) \
89+
static struct as5600_dev_data as5600_data##n; \
90+
static const struct as5600_dev_cfg as5600_cfg##n = {\
91+
.i2c_port = I2C_DT_SPEC_INST_GET(n) \
92+
}; \
93+
\
94+
SENSOR_DEVICE_DT_INST_DEFINE(n, as5600_initialize, NULL, \
95+
&as5600_data##n, &as5600_cfg##n, \
96+
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
97+
&as5600_driver_api);
98+
99+
DT_INST_FOREACH_STATUS_OKAY(AS5600_INIT)

dts/bindings/sensor/ams,as5600.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2022, Felipe Neves
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: |
5+
AMS (Austria Mikro Systeme) AS5600 Angular position sensor
6+
7+
compatible: "ams,as5600"
8+
9+
include: [sensor-device.yaml, i2c-device.yaml]

tests/drivers/build_all/sensor/i2c.dtsi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,3 +641,8 @@ test_i2c_icp10125: icp10125@63 {
641641
temperature-measurement-mode = "normal";
642642
pressure-measurement-mode = "normal";
643643
};
644+
645+
test_i2c_as5600: as5600@54 {
646+
compatible = "ams,as5600";
647+
reg = <0x54>;
648+
};

0 commit comments

Comments
 (0)