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
|
/* 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.
*/
#define DT_DRV_COMPAT cros_lis2dw12_emul
#include <device.h>
#include <drivers/i2c.h>
#include <drivers/i2c_emul.h>
#include <emul.h>
#include <errno.h>
#include <sys/__assert.h>
#include "driver/accel_lis2dw12.h"
#include "emul/emul_common_i2c.h"
#include "emul/emul_lis2dw12.h"
#include "i2c.h"
#include <logging/log.h>
LOG_MODULE_REGISTER(lis2dw12_emul, CONFIG_LIS2DW12_EMUL_LOG_LEVEL);
#define LIS2DW12_DATA_FROM_I2C_EMUL(_emul) \
CONTAINER_OF(CONTAINER_OF(_emul, struct i2c_common_emul_data, emul), \
struct lis2dw12_emul_data, common)
struct lis2dw12_emul_data {
/** Common I2C data */
struct i2c_common_emul_data common;
/** Emulated who-am-i register */
uint8_t who_am_i_reg;
/** Emulated ctrl1 register */
uint8_t ctrl1_reg;
/** Emulated ctrl2 register */
uint8_t ctrl2_reg;
/** Soft reset count */
uint32_t soft_reset_count;
};
struct lis2dw12_emul_cfg {
/** Common I2C config */
struct i2c_common_emul_cfg common;
};
struct i2c_emul *lis2dw12_emul_to_i2c_emul(const struct emul *emul)
{
struct lis2dw12_emul_data *data = emul->data;
return &(data->common.emul);
}
void lis2dw12_emul_reset(const struct emul *emul)
{
struct lis2dw12_emul_data *data = emul->data;
struct i2c_emul *i2c_emul = lis2dw12_emul_to_i2c_emul(emul);
i2c_common_emul_set_read_fail_reg(i2c_emul,
I2C_COMMON_EMUL_NO_FAIL_REG);
i2c_common_emul_set_write_fail_reg(i2c_emul,
I2C_COMMON_EMUL_NO_FAIL_REG);
i2c_common_emul_set_read_func(i2c_emul, NULL, NULL);
i2c_common_emul_set_write_func(i2c_emul, NULL, NULL);
data->who_am_i_reg = LIS2DW12_WHO_AM_I;
data->ctrl2_reg = 0;
data->soft_reset_count = 0;
}
void lis2dw12_emul_set_who_am_i(const struct emul *emul, uint8_t who_am_i)
{
struct lis2dw12_emul_data *data = emul->data;
data->who_am_i_reg = who_am_i;
}
uint32_t lis2dw12_emul_get_soft_reset_count(const struct emul *emul)
{
struct lis2dw12_emul_data *data = emul->data;
return data->soft_reset_count;
}
static int lis2dw12_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val,
int bytes)
{
struct lis2dw12_emul_data *data = LIS2DW12_DATA_FROM_I2C_EMUL(emul);
switch (reg) {
case LIS2DW12_WHO_AM_I_REG:
__ASSERT_NO_MSG(bytes == 0);
*val = data->who_am_i_reg;
break;
case LIS2DW12_CTRL1_ADDR:
__ASSERT_NO_MSG(bytes == 0);
*val = data->ctrl1_reg;
break;
case LIS2DW12_CTRL2_ADDR:
__ASSERT_NO_MSG(bytes == 0);
*val = data->ctrl2_reg;
break;
default:
__ASSERT(false, "No read handler for register 0x%02x", reg);
return -EINVAL;
}
return 0;
}
static int lis2dw12_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val,
int bytes)
{
struct lis2dw12_emul_data *data = LIS2DW12_DATA_FROM_I2C_EMUL(emul);
switch (reg) {
case LIS2DW12_WHO_AM_I_REG:
LOG_ERR("Can't write to who-am-i register");
return -EINVAL;
case LIS2DW12_CTRL1_ADDR:
data->ctrl1_reg = val;
break;
case LIS2DW12_CTRL2_ADDR:
__ASSERT_NO_MSG(bytes == 1);
if ((val & LIS2DW12_SOFT_RESET_MASK) != 0) {
/* Soft reset */
data->soft_reset_count++;
}
data->ctrl2_reg = val & ~LIS2DW12_SOFT_RESET_MASK;
break;
default:
__ASSERT(false, "No write handler for register 0x%02x", reg);
return -EINVAL;
}
return 0;
}
static int emul_lis2dw12_init(const struct emul *emul,
const struct device *parent)
{
const struct lis2dw12_emul_cfg *lis2dw12_cfg = emul->cfg;
const struct i2c_common_emul_cfg *cfg = &(lis2dw12_cfg->common);
struct lis2dw12_emul_data *data = emul->data;
data->common.emul.api = &i2c_common_emul_api;
data->common.emul.addr = cfg->addr;
data->common.emul.parent = emul;
data->common.i2c = parent;
data->common.cfg = cfg;
i2c_common_emul_init(&data->common);
return i2c_emul_register(parent, emul->dev_label, &data->common.emul);
}
#define INIT_LIS2DW12(n) \
static struct lis2dw12_emul_data lis2dw12_emul_data_##n = { \
.common = { \
.write_byte = lis2dw12_emul_write_byte, \
.read_byte = lis2dw12_emul_read_byte, \
}, \
}; \
static const struct lis2dw12_emul_cfg lis2dw12_emul_cfg_##n = { \
.common = { \
.i2c_label = DT_INST_BUS_LABEL(n), \
.dev_label = DT_INST_LABEL(n), \
.addr = DT_INST_REG_ADDR(n), \
}, \
}; \
EMUL_DEFINE(emul_lis2dw12_init, DT_DRV_INST(n), \
&lis2dw12_emul_cfg_##n, &lis2dw12_emul_data_##n)
DT_INST_FOREACH_STATUS_OKAY(INIT_LIS2DW12)
|