-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
main.c
312 lines (252 loc) · 9.27 KB
/
main.c
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2018, Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is part of the Simba project.
*/
#include "simba.h"
#include "drivers/network/i2c_mock.h"
#include "drivers/network/spi_mock.h"
struct bmp280_driver_t bmp280_i2c;
struct i2c_driver_t i2c;
struct bmp280_transport_i2c_t transport_i2c;
struct bmp280_driver_t bmp280_spi;
struct spi_driver_t spi;
struct bmp280_transport_spi_t transport_spi;
static int test_init(void)
{
BTASSERT(bmp280_module_init() == 0);
BTASSERT(bmp280_module_init() == 0);
return (0);
}
static int test_i2c_start(void)
{
BTASSERT(bmp280_transport_i2c_init(&transport_i2c,
&i2c,
BMP280_I2C_ADDRESS_0) == 0);
BTASSERT(bmp280_init(&bmp280_i2c,
&transport_i2c.base,
bmp280_mode_forced_t,
bmp280_standby_time_500_us_t,
bmp280_filter_off_t,
bmp280_temperature_oversampling_1_t,
bmp280_pressure_oversampling_1_t) == 0);
mock_write_i2c_scan(0x76, 1);
/* Chip id. */
mock_write_i2c_write(0x76, "\xd0", 1, 1);
mock_write_i2c_read(0x76, "\x58", 1, 1);
/* Calibration data. */
mock_write_i2c_write(0x76, "\x88", 1, 1);
mock_write_i2c_read(0x76,
"\x70\x6b\x43\x67\x18\xfc\x5f\x8e"
"\x43\xd6\xd0\x0b\x27\x0b\x8c\x00"
"\xf9\xff\x8c\x3c\xf8\xc6\x70\x17",
24,
24);
BTASSERT(bmp280_start(&bmp280_i2c) == 0);
return (0);
}
static int test_i2c_read(void)
{
float temperature;
float pressure;
/* Start a convertion. */
mock_write_i2c_write(0x76, "\xf4\x25", 2, 2);
/* Status reading - not ready. */
mock_write_i2c_write(0x76, "\xf3", 1, 1);
mock_write_i2c_read(0x76, "\xff", 1, 1);
/* Status reading - ready. */
mock_write_i2c_write(0x76, "\xf3", 1, 1);
mock_write_i2c_read(0x76, "\x00", 1, 1);
/* Temperature and pressure reading. */
mock_write_i2c_write(0x76, "\xf7", 1, 1);
mock_write_i2c_read(0x76, "\x65\x5a\xc0\x7e\xed\x00", 6, 6);
BTASSERT(bmp280_read(&bmp280_i2c, &temperature, &pressure) == 0);
BTASSERT(temperature > 25.082);
BTASSERT(temperature < 25.083);
BTASSERT(pressure > 100700.0);
BTASSERT(pressure < 100800.0);
return (0);
}
static int test_i2c_read_meas_start_error(void)
{
float temperature;
float pressure;
/* Measurement start - IO error. */
mock_write_i2c_write(0x76, "\xf4\x25", 2, -EIO);
BTASSERT(bmp280_read(&bmp280_i2c, &temperature, &pressure) == -EIO);
return (0);
}
static int test_i2c_read_status_error(void)
{
float temperature;
float pressure;
/* Start a convertion. */
mock_write_i2c_write(0x76, "\xf4\x25", 2, 2);
/* Status register - IO error. */
mock_write_i2c_write(0x76, "\xf3", 1, -EIO);
BTASSERT(bmp280_read(&bmp280_i2c, &temperature, &pressure) == -EIO);
return (0);
}
static int test_i2c_read_status_timeout(void)
{
float temperature;
float pressure;
int i;
/* Start a convertion. */
mock_write_i2c_write(0x76, "\xf4\x25", 2, 2);
/* Status reading - not ready. */
for (i = 0; i < CONFIG_BMP280_COVERTION_TIMEOUT_MS; i++) {
mock_write_i2c_write(0x76, "\xf3", 1, 1);
mock_write_i2c_read(0x76, "\xff", 1, 1);
}
BTASSERT(bmp280_read(&bmp280_i2c, &temperature, &pressure) == -ETIMEDOUT);
return (0);
}
static int test_i2c_stop(void)
{
BTASSERT(bmp280_stop(&bmp280_i2c) == -ENOSYS);
return (0);
}
static int test_i2c_start_automatic(void)
{
BTASSERT(bmp280_transport_i2c_init(&transport_i2c,
&i2c,
BMP280_I2C_ADDRESS_AUTOMATIC) == 0);
BTASSERT(bmp280_init(&bmp280_i2c,
&transport_i2c.base,
bmp280_mode_forced_t,
bmp280_standby_time_500_us_t,
bmp280_filter_off_t,
bmp280_temperature_oversampling_1_t,
bmp280_pressure_oversampling_1_t) == 0);
/* Found device with id 0x77. */
mock_write_i2c_scan(0x76, 0);
mock_write_i2c_scan(0x77, 1);
/* Chip id. */
mock_write_i2c_write(0x77, "\xd0", 1, 1);
mock_write_i2c_read(0x77, "\x48", 1, 1);
/* Calibration data. */
mock_write_i2c_write(0x77, "\x88", 1, 1);
mock_write_i2c_read(0x77,
"\x70\x6b\x43\x67\x18\xfc\x5f\x8e"
"\x43\xd6\xd0\x0b\x27\x0b\x8c\x00"
"\xf9\xff\x8c\x3c\xf8\xc6\x70\x17",
24,
24);
BTASSERTI(bmp280_start(&bmp280_i2c), ==, 0);
return (0);
}
static int test_i2c_start_automatic_no_dev(void)
{
BTASSERT(bmp280_transport_i2c_init(&transport_i2c,
&i2c,
BMP280_I2C_ADDRESS_AUTOMATIC) == 0);
BTASSERT(bmp280_init(&bmp280_i2c,
&transport_i2c.base,
bmp280_mode_forced_t,
bmp280_standby_time_500_us_t,
bmp280_filter_off_t,
bmp280_temperature_oversampling_1_t,
bmp280_pressure_oversampling_1_t) == 0);
/* No device found. */
mock_write_i2c_scan(0x76, 0);
mock_write_i2c_scan(0x77, 0);
BTASSERTI(bmp280_start(&bmp280_i2c), ==, -ENODEV);
return (0);
}
static int test_spi_start(void)
{
BTASSERT(bmp280_transport_spi_init(&transport_spi,
&spi) == 0);
BTASSERT(bmp280_init(&bmp280_spi,
&transport_spi.base,
bmp280_mode_normal_t,
bmp280_standby_time_500_us_t,
bmp280_filter_off_t,
bmp280_temperature_oversampling_1_t,
bmp280_pressure_oversampling_1_t) == 0);
/* Chip id. */
mock_write_spi_select(0);
mock_write_spi_write("\xd0", 1, 1);
mock_write_spi_read("\x58", 1, 1);
mock_write_spi_deselect(0);
/* Normal mode configuration. */
mock_write_spi_select(0);
mock_write_spi_write("\x74\x27", 2, 2);
mock_write_spi_deselect(0);
/* Calibration data. */
mock_write_spi_select(0);
mock_write_spi_write("\x88", 1, 1);
mock_write_spi_read("\x70\x6b\x43\x67\x18\xfc\x5f\x8e"
"\x43\xd6\xd0\x0b\x27\x0b\x8c\x00"
"\xf9\xff\x8c\x3c\xf8\xc6\x70\x17",
24,
24);
mock_write_spi_deselect(0);
BTASSERT(bmp280_start(&bmp280_spi) == 0);
return (0);
}
static int test_spi_read_fixed_point(void)
{
long temperature;
long pressure;
/* Temperature and pressure reading. */
mock_write_spi_select(0);
mock_write_spi_write("\xf7", 1, 1);
mock_write_spi_read("\x65\x5a\xc0\x7e\xed\x00", 6, 6);
mock_write_spi_deselect(0);
BTASSERT(bmp280_read_fixed_point(&bmp280_spi,
&temperature,
&pressure) == 0);
BTASSERTI(temperature, ==, 25082);
BTASSERTI(pressure, ==, 100739000);
return (0);
}
static int test_spi_stop(void)
{
BTASSERT(bmp280_stop(&bmp280_spi) == -ENOSYS);
return (0);
}
int main()
{
struct harness_testcase_t testcases[] = {
{ test_init, "test_init" },
{ test_i2c_start, "test_i2c_start" },
{ test_i2c_read, "test_i2c_read" },
{ test_i2c_read_meas_start_error, "test_i2c_read_meas_start_error" },
{ test_i2c_read_status_error, "test_i2c_read_status_error" },
{ test_i2c_read_status_timeout, "test_i2c_read_status_timeout" },
{ test_i2c_stop, "test_i2c_stop" },
{ test_i2c_start_automatic, "test_i2c_start_automatic" },
{ test_i2c_start_automatic_no_dev, "test_i2c_start_automatic_no_dev" },
{ test_spi_start, "test_spi_start" },
{ test_spi_read_fixed_point, "test_spi_read_fixed_point" },
{ test_spi_stop, "test_spi_stop" },
{ NULL, NULL }
};
sys_start();
harness_run(testcases);
return (0);
}