summaryrefslogtreecommitdiff
path: root/zephyr/include/emul/emul_bma255.h
blob: 1b40e06559b0ad8cf46efe35d9d6017a942425af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* Copyright 2021 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/**
 * @file
 *
 * @brief Backend API for BMA255 emulator
 */

#ifndef __EMUL_BMA255_H
#define __EMUL_BMA255_H

#include <emul.h>
#include <drivers/i2c.h>
#include <drivers/i2c_emul.h>

/**
 * @brief BMA255 emulator backend API
 * @defgroup bma_emul BMA255 emulator
 * @{
 *
 * BMA255 emulator supports responses to all write and read I2C messages.
 * Accelerometer registers are obtained from internal emulator state, range
 * register and offset. Only fast compensation is supported by default handler.
 * Registers backed in NVM are fully supported (GP0, GP1, offset). For proper
 * support for interrupts and FIFO, user needs to use custom handlers.
 * Application may alter emulator state:
 *
 * - define a Device Tree overlay file to set default NVM content, default
 *   static accelerometer value and which inadvisable driver behaviour should
 *   be treated as errors
 * - call @ref bma_emul_set_read_func and @ref bma_emul_set_write_func to setup
 *   custom handlers for I2C messages
 * - call @ref bma_emul_set_reg and @ref bma_emul_get_reg to set and get value
 *   of BMA255 registers
 * - call @ref bma_emul_set_off and @ref bma_emul_set_off to set and get
 *   internal offset value
 * - call @ref bma_emul_set_acc and @ref bma_emul_set_acc to set and get
 *   accelerometer value
 * - call bma_emul_set_err_* to change emulator behaviour on inadvisable driver
 *   behaviour
 * - call @ref bma_emul_set_read_fail_reg and @ref bma_emul_set_write_fail_reg
 *   to configure emulator to fail on given register read or write
 */

/**
 * Axis argument used in @ref bma_emul_set_acc @ref bma_emul_get_acc
 * @ref bma_emul_set_off and @ref bma_emul_get_off
 */
#define BMA_EMUL_AXIS_X		0
#define BMA_EMUL_AXIS_Y		1
#define BMA_EMUL_AXIS_Z		2

/**
 * Acceleration 1g in internal emulator units. It is helpful for using
 * functions @ref bma_emul_set_acc @ref bma_emul_get_acc
 * @ref bma_emul_set_off and @ref bma_emul_get_off
 */
#define BMA_EMUL_1G		BIT(10)

/**
 * Special register values used in @ref bma_emul_set_read_fail_reg and
 * @ref bma_emul_set_write_fail_reg
 */
#define BMA_EMUL_FAIL_ALL_REG	(-1)
#define BMA_EMUL_NO_FAIL_REG	(-2)

/**
 * @brief Get pointer to BMA255 emulator using device tree order number.
 *
 * @param ord Device tree order number obtained from DT_DEP_ORD macro
 *
 * @return Pointer to BMA255 emulator
 */
struct i2c_emul *bma_emul_get(int ord);

/**
 * @brief Custom function type that is used as user-defined callback in read
 *        I2C messages handling.
 *
 * @param emul Pointer to BMA255 emulator
 * @param reg Address which is now accessed by read command
 * @param data Pointer to custom user data
 *
 * @return 0 on success. Value of @p reg should be set by @ref bma_emul_set_reg
 * @return 1 continue with normal BMA255 emulator handler
 * @return negative on error
 */
typedef int (*bma_emul_read_func)(struct i2c_emul *emul, int reg, void *data);

/**
 * @brief Custom function type that is used as user-defined callback in write
 *        I2C messages handling.
 *
 * @param emul Pointer to BMA255 emulator
 * @param reg Address which is now accessed by write command
 * @param val Value which is being written to @p reg
 * @param data Pointer to custom user data
 *
 * @return 0 on success
 * @return 1 continue with normal BMA255 emulator handler
 * @return negative on error
 */
typedef int (*bma_emul_write_func)(struct i2c_emul *emul, int reg, uint8_t val,
				   void *data);

/**
 * @brief Lock access to BMA255 properties. After acquiring lock, user
 *        may change emulator behaviour in multi-thread setup.
 *
 * @param emul Pointer to BMA255 emulator
 * @param timeout Timeout in getting lock
 *
 * @return k_mutex_lock return code
 */
int bma_emul_lock_data(struct i2c_emul *emul, k_timeout_t timeout);

/**
 * @brief Unlock access to BMA255 properties.
 *
 * @param emul Pointer to BMA255 emulator
 *
 * @return k_mutex_unlock return code
 */
int bma_emul_unlock_data(struct i2c_emul *emul);

/**
 * @brief Set write handler for I2C messages. This function is called before
 *        generic handler.
 *
 * @param emul Pointer to BMA255 emulator
 * @param func Pointer to custom function
 * @param data User data passed on call of custom function
 */
void bma_emul_set_write_func(struct i2c_emul *emul, bma_emul_write_func func,
			     void *data);

/**
 * @brief Set read handler for I2C messages. This function is called before
 *        generic handler.
 *
 * @param emul Pointer to BMA255 emulator
 * @param func Pointer to custom function
 * @param data User data passed on call of custom function
 */
void bma_emul_set_read_func(struct i2c_emul *emul, bma_emul_read_func func,
			    void *data);

/**
 * @brief Set value of given register of BMA255
 *
 * @param emul Pointer to BMA255 emulator
 * @param reg Register address which value will be changed
 * @param val New value of the register
 */
void bma_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val);

/**
 * @brief Get value of given register of BMA255
 *
 * @param emul Pointer to BMA255 emulator
 * @param reg Register address
 *
 * @return Value of the register
 */
uint8_t bma_emul_get_reg(struct i2c_emul *emul, int reg);

/**
 * @brief Setup fail on read of given register of BMA255
 *
 * @param emul Pointer to BMA255 emulator
 * @param reg Register address or one of special values (BMA_EMUL_FAIL_ALL_REG,
 *            BMA_EMUL_NO_FAIL_REG)
 */
void bma_emul_set_read_fail_reg(struct i2c_emul *emul, int reg);

/**
 * @brief Setup fail on write of given register of BMA255
 *
 * @param emul Pointer to BMA255 emulator
 * @param reg Register address or one of special values (BMA_EMUL_FAIL_ALL_REG,
 *            BMA_EMUL_NO_FAIL_REG)
 */
void bma_emul_set_write_fail_reg(struct i2c_emul *emul, int reg);

/**
 * @brief Get internal value of offset for given axis
 *
 * @param emul Pointer to BMA255 emulator
 * @param axis Axis to access: 0 - X, 1 - Y, 2 - Z
 *
 * @return Offset of given axis. LSB is 0.97mg
 */
int16_t bma_emul_get_off(struct i2c_emul *emul, int axis);

/**
 * @brief Set internal value of offset for given axis
 *
 * @param emul Pointer to BMA255 emulator
 * @param axis Axis to access: 0 - X, 1 - Y, 2 - Z
 * @param val New value of offset. LSB is 0.97mg
 */
void bma_emul_set_off(struct i2c_emul *emul, int axis, int16_t val);

/**
 * @brief Get internal value of accelerometer for given axis
 *
 * @param emul Pointer to BMA255 emulator
 * @param axis Axis to access: 0 - X, 1 - Y, 2 - Z
 *
 * @return Acceleration of given axis. LSB is 0.97mg
 */
int16_t bma_emul_get_acc(struct i2c_emul *emul, int axis);

/**
 * @brief Set internal value of accelerometr for given axis
 *
 * @param emul Pointer to BMA255 emulator
 * @param axis Axis to access: 0 - X, 1 - Y, 2 - Z
 * @param val New value of accelerometer axis. LSB is 0.97mg
 */
void bma_emul_set_acc(struct i2c_emul *emul, int axis, int16_t val);

/**
 * @brief Set if error should be generated when fast compensation is triggered
 *        when not ready flag is set
 *
 * @param emul Pointer to BMA255 emulator
 * @param set Check for this error
 */
void bma_emul_set_err_on_cal_nrdy(struct i2c_emul *emul, bool set);

/**
 * @brief Set if error should be generated when fast compensation is triggered
 *        when range is not 2G
 *
 * @param emul Pointer to BMA255 emulator
 * @param set Check for this error
 */
void bma_emul_set_err_on_cal_bad_range(struct i2c_emul *emul, bool set);

/**
 * @brief Set if error should be generated when read only register is being
 *        written
 *
 * @param emul Pointer to BMA255 emulator
 * @param set Check for this error
 */
void bma_emul_set_err_on_ro_write(struct i2c_emul *emul, bool set);

/**
 * @brief Set if error should be generated when reserved bits of register are
 *        not set to 0 on write I2C message
 *
 * @param emul Pointer to BMA255 emulator
 * @param set Check for this error
 */
void bma_emul_set_err_on_rsvd_write(struct i2c_emul *emul, bool set);

/**
 * @brief Set if error should be generated when MSB register is accessed before
 *        LSB register
 *
 * @param emul Pointer to BMA255 emulator
 * @param set Check for this error
 */
void bma_emul_set_err_on_msb_first(struct i2c_emul *emul, bool set);

/**
 * @}
 */

#endif /* __EMUL_BMA255_H */