1 /*
2  * Copyright (c) 2023 Trackunit Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_BMI323_BMI323_H_
8 #define ZEPHYR_DRIVERS_SENSOR_BMI323_BMI323_H_
9 
10 #include <zephyr/sys/util.h>
11 #include <zephyr/types.h>
12 
13 #define IMU_BOSCH_BMI323_REG_ACC_DATA_X (0x03)
14 #define IMU_BOSCH_BMI323_REG_ACC_DATA_Y (0x04)
15 #define IMU_BOSCH_BMI323_REG_ACC_DATA_Z (0x05)
16 
17 #define IMU_BOSCH_BMI323_REG_GYRO_DATA_X (0x06)
18 #define IMU_BOSCH_BMI323_REG_GYRO_DATA_Y (0x07)
19 #define IMU_BOSCH_BMI323_REG_GYRO_DATA_Z (0x08)
20 
21 #define IMU_BOSCH_BMI323_REG_TEMP_DATA (0x09)
22 
23 #define IMU_BOSCH_BMI323_REG_FEATURE_IO0 (0x10)
24 
25 #define IMU_BOSCH_BMI323_REG_FEATURE_IO0_MOTION_X_EN_OFFSET  (0x03)
26 #define IMU_BOSCH_BMI323_REG_FEATURE_IO0_MOTION_X_EN_SIZE    (0x01)
27 #define IMU_BOSCH_BMI323_REG_FEATURE_IO0_MOTION_X_EN_VAL_DIS (0x00)
28 #define IMU_BOSCH_BMI323_REG_FEATURE_IO0_MOTION_X_EN_VAL_EN  (0x01)
29 
30 #define IMU_BOSCH_BMI323_REG_FEATURE_IO0_MOTION_Y_EN_OFFSET  (0x04)
31 #define IMU_BOSCH_BMI323_REG_FEATURE_IO0_MOTION_Y_EN_SIZE    (0x01)
32 #define IMU_BOSCH_BMI323_REG_FEATURE_IO0_MOTION_Y_EN_VAL_DIS (0x00)
33 #define IMU_BOSCH_BMI323_REG_FEATURE_IO0_MOTION_Y_EN_VAL_EN  (0x01)
34 
35 #define IMU_BOSCH_BMI323_REG_FEATURE_IO0_MOTION_Z_EN_OFFSET  (0x05)
36 #define IMU_BOSCH_BMI323_REG_FEATURE_IO0_MOTION_Z_EN_SIZE    (0x01)
37 #define IMU_BOSCH_BMI323_REG_FEATURE_IO0_MOTION_Z_EN_VAL_DIS (0x00)
38 #define IMU_BOSCH_BMI323_REG_FEATURE_IO0_MOTION_Z_EN_VAL_EN  (0x01)
39 
40 #define IMU_BOSCH_BMI323_REG_FEATURE_IO2 (0x12)
41 
42 #define IMU_BOSCH_BMI323_REG_FEATURE_IO_STATUS (0x14)
43 
44 #define IMU_BOSCH_BMI323_REG_FEATURE_IO_STATUS_STATUS_OFFSET  (0x00)
45 #define IMU_BOSCH_BMI323_REG_FEATURE_IO_STATUS_STATUS_SIZE    (0x01)
46 #define IMU_BOSCH_BMI323_REG_FEATURE_IO_STATUS_STATUS_VAL_SET (0x01)
47 
48 #define IMU_BOSCH_BMI323_REG_INT_STATUS_INT1 (0x0D)
49 #define IMU_BOSCH_BMI323_REG_INT_STATUS_INT2 (0x0E)
50 
51 #define IMU_BOSCH_BMI323_REG_ACC_CONF (0x20)
52 
53 #define IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_OFFSET	(0x00)
54 #define IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_SIZE		(0x04)
55 #define IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ0P78125 (0x01)
56 #define IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ1P5625	(0x02)
57 #define IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ3P125	(0x03)
58 #define IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ6P25	(0x04)
59 #define IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ12P5	(0x05)
60 #define IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ25	(0x06)
61 #define IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ50	(0x07)
62 #define IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ100	(0x08)
63 #define IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ200	(0x09)
64 #define IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ400	(0x0A)
65 #define IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ800	(0x0B)
66 #define IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ1600	(0x0C)
67 #define IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ3200	(0x0D)
68 #define IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ6400	(0x0E)
69 
70 #define IMU_BOSCH_BMI323_REG_ACC_CONF_RANGE_OFFSET  (0x04)
71 #define IMU_BOSCH_BMI323_REG_ACC_CONF_RANGE_SIZE    (0x03)
72 #define IMU_BOSCH_BMI323_REG_ACC_CONF_RANGE_VAL_G2  (0x00)
73 #define IMU_BOSCH_BMI323_REG_ACC_CONF_RANGE_VAL_G4  (0x01)
74 #define IMU_BOSCH_BMI323_REG_ACC_CONF_RANGE_VAL_G8  (0x02)
75 #define IMU_BOSCH_BMI323_REG_ACC_CONF_RANGE_VAL_G16 (0x03)
76 
77 #define IMU_BOSCH_BMI323_REG_ACC_CONF_MODE_OFFSET   (0x0C)
78 #define IMU_BOSCH_BMI323_REG_ACC_CONF_MODE_SIZE	    (0x03)
79 #define IMU_BOSCH_BMI323_REG_ACC_CONF_MODE_VAL_DIS  (0x00)
80 #define IMU_BOSCH_BMI323_REG_ACC_CONF_MODE_VAL_LPWR (0x04)
81 #define IMU_BOSCH_BMI323_REG_ACC_CONF_MODE_VAL_HPWR (0x07)
82 
83 #define IMU_BOSCH_BMI323_REG_GYRO_CONF (0x21)
84 
85 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_OFFSET	 (0x00)
86 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_SIZE		 (0x04)
87 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ0P78125 (0x01)
88 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ1P5625	 (0x02)
89 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ3P125	 (0x03)
90 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ6P25	 (0x04)
91 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ12P5	 (0x05)
92 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ25	 (0x06)
93 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ50	 (0x07)
94 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ100	 (0x08)
95 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ200	 (0x09)
96 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ400	 (0x0A)
97 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ800	 (0x0B)
98 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ1600	 (0x0C)
99 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ3200	 (0x0D)
100 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ6400	 (0x0E)
101 
102 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_RANGE_OFFSET	 (0x04)
103 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_RANGE_SIZE	 (0x03)
104 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_RANGE_VAL_DPS125	 (0x00)
105 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_RANGE_VAL_DPS250	 (0x01)
106 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_RANGE_VAL_DPS500	 (0x02)
107 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_RANGE_VAL_DPS1000 (0x03)
108 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_RANGE_VAL_DPS2000 (0x04)
109 
110 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_MODE_OFFSET   (0x0C)
111 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_MODE_SIZE     (0x03)
112 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_MODE_VAL_DIS  (0x00)
113 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_MODE_VAL_LPWR (0x04)
114 #define IMU_BOSCH_BMI323_REG_GYRO_CONF_MODE_VAL_HPWR (0x07)
115 
116 #define IMU_BOSCH_BMI323_REG_IO_INT_CTRL (0x38)
117 
118 #define IMU_BOSCH_BMI323_REG_IO_INT_CTRL_INT1_LVL_OFFSET	(0x00)
119 #define IMU_BOSCH_BMI323_REG_IO_INT_CTRL_INT1_LVL_SIZE		(0x01)
120 #define IMU_BOSCH_BMI323_REG_IO_INT_CTRL_INT1_LVL_VAL_ACT_LOW	(0x00)
121 #define IMU_BOSCH_BMI323_REG_IO_INT_CTRL_INT1_LVL_VAL_ACT_HIGH	(0x01)
122 #define IMU_BOSCH_BMI323_REG_IO_INT_CTRL_INT1_OD_OFFSET		(0x01)
123 #define IMU_BOSCH_BMI323_REG_IO_INT_CTRL_INT1_OD_SIZE		(0x01)
124 #define IMU_BOSCH_BMI323_REG_IO_INT_CTRL_INT1_OD_VAL_PUSH_PULL	(0x00)
125 #define IMU_BOSCH_BMI323_REG_IO_INT_CTRL_INT1_OD_VAL_OPEN_DRAIN (0x01)
126 #define IMU_BOSCH_BMI323_REG_IO_INT_CTRL_INT1_OUTPUT_EN_OFFSET	(0x02)
127 #define IMU_BOSCH_BMI323_REG_IO_INT_CTRL_INT1_OUTPUT_EN_SIZE	(0x01)
128 #define IMU_BOSCH_BMI323_REG_IO_INT_CTRL_INT1_OUTPUT_EN_VAL_DIS (0x00)
129 #define IMU_BOSCH_BMI323_REG_IO_INT_CTRL_INT1_OUTPUT_EN_VAL_EN	(0x01)
130 
131 #define IMU_BOSCH_BMI323_REG_INT_CONF (0x39)
132 
133 #define IMU_BOSCH_BMI323_REG_INT_CONF_INT_LATCH_OFFSET		(0x01)
134 #define IMU_BOSCH_BMI323_REG_INT_CONF_INT_LATCH_SIZE		(0x01)
135 #define IMU_BOSCH_BMI323_REG_INT_CONF_INT_LATCH_VAL_NON_LATCHED (0x00)
136 #define IMU_BOSCH_BMI323_REG_INT_CONF_INT_LATCH_VAL_LATCHED	(0x01)
137 
138 #define IMU_BOSCH_BMI323_REG_INT_MAP1 (0x3A)
139 
140 #define IMU_BOSCH_BMI323_REG_INT_MAP1_MOTION_OUT_OFFSET	  (0x02)
141 #define IMU_BOSCH_BMI323_REG_INT_MAP1_MOTION_OUT_SIZE	  (0x02)
142 #define IMU_BOSCH_BMI323_REG_INT_MAP1_MOTION_OUT_VAL_DIS  (0x00)
143 #define IMU_BOSCH_BMI323_REG_INT_MAP1_MOTION_OUT_VAL_INT1 (0x01)
144 #define IMU_BOSCH_BMI323_REG_INT_MAP1_MOTION_OUT_VAL_INT2 (0x02)
145 
146 #define IMU_BOSCH_BMI323_REG_INT_MAP2 (0x3B)
147 
148 #define IMU_BOSCH_BMI323_REG_INT_MAP2_ACC_DRDY_INT_OFFSET   (0x0A)
149 #define IMU_BOSCH_BMI323_REG_INT_MAP2_ACC_DRDY_INT_SIZE	    (0x02)
150 #define IMU_BOSCH_BMI323_REG_INT_MAP2_ACC_DRDY_INT_VAL_DIS  (0x00)
151 #define IMU_BOSCH_BMI323_REG_INT_MAP2_ACC_DRDY_INT_VAL_INT1 (0x01)
152 #define IMU_BOSCH_BMI323_REG_INT_MAP2_ACC_DRDY_INT_VAL_INT2 (0x02)
153 
154 #define IMU_BOSCH_BMI323_REG_FEATURE_CTRL (0x40)
155 
156 #define IMU_BOSCH_BMI323_REG_FEATURE_CTRL_ENABLE_OFFSET	 (0x00)
157 #define IMU_BOSCH_BMI323_REG_FEATURE_CTRL_ENABLE_SIZE	 (0x01)
158 #define IMU_BOSCH_BMI323_REG_FEATURE_CTRL_ENABLE_VAL_DIS (0x00)
159 #define IMU_BOSCH_BMI323_REG_FEATURE_CTRL_ENABLE_VAL_EN	 (0x01)
160 
161 #define IMU_BOSCH_BMI323_REG_CMD		    (0x7E)
162 #define IMU_BOSCH_BMI323_REG_CMD_CMD_OFFSET	    (0x00)
163 #define IMU_BOSCH_BMI323_REG_CMD_CMD_SIZE	    (0x10)
164 #define IMU_BOSCH_BMI323_REG_CMD_CMD_VAL_SOFT_RESET (0xDEAF)
165 
166 #define IMU_BOSCH_BMI323_REG_MASK(reg, field)                                                      \
167 	(BIT_MASK(IMU_BOSCH_BMI323_REG_##reg##_##field##_SIZE)                                     \
168 	 << IMU_BOSCH_BMI323_REG_##reg##_##field##_OFFSET)
169 
170 #define IMU_BOSCH_BMI323_REG_VALUE(reg, field, val)                                                \
171 	(IMU_BOSCH_BMI323_REG_##reg##_##field##_VAL_##val                                          \
172 	 << IMU_BOSCH_BMI323_REG_##reg##_##field##_OFFSET)
173 
174 #define IMU_BOSCH_BMI323_REG_VALUE_GET_FIELD(reg_value, reg, field)                                \
175 	((reg_value >> IMU_BOSCH_BMI323_REG_##reg##_##field##_OFFSET) &                            \
176 	 BIT_MASK(IMU_BOSCH_BMI323_REG_##reg##_##field##_SIZE))
177 
178 struct bosch_bmi323_bus_api {
179 	/* Read up to multiple words from the BMI323 */
180 	int (*read_words)(const void *context, uint8_t offset, uint16_t *words,
181 			  uint16_t words_count);
182 
183 	/* Write up to multiple words to the BLI323 */
184 	int (*write_words)(const void *context, uint8_t offset, uint16_t *words,
185 			   uint16_t words_count);
186 
187 	/* Initialize the bus */
188 	int (*init)(const void *context);
189 };
190 
191 struct bosch_bmi323_bus {
192 	const void *context;
193 	const struct bosch_bmi323_bus_api *api;
194 };
195 
196 #endif /* ZEPHYR_DRIVERS_SENSOR_BMI323_BMI323_H_ */
197