STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_h3lis100dl.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "dev_h3lis100dl.h"
6#include "fake_regs.h"
7
8#define CHECK(c) \
9 do { \
10 if (!(c)) { \
11 fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, #c); \
12 exit(1); \
13 } \
14 } while (0)
15
16static void healthy(fake_regs_t* f) {
17 fake_regs_init(f, true); /* MS bit selects auto-increment */
18 f->regs[0x0F] = 0x32;
19 f->regs[0x20] = 0x07; /* power-on default: power-down (p.24) */
20}
21
24 healthy(&f);
25 dev_bus_t bus = fake_regs_bus(&f);
26 CHECK(h3lis_init(&bus) == DEV_OK);
27 static const uint8_t reg[] = {0x23, 0x22, 0x20};
28 static const uint8_t val[] = {0x00, 0x02, 0x37};
29 CHECK(f.writes == sizeof(reg));
30 for (unsigned i = 0; i < sizeof(reg); ++i) {
31 CHECK(f.write_reg[i] == reg[i]);
32 CHECK(f.write_val[i] == val[i]);
33 }
34}
35
38 healthy(&f);
39 f.regs[0x0F] = 0x33; /* not 0x32 (note: the H3LIS331DL also says 0x32) */
40 dev_bus_t bus = fake_regs_bus(&f);
41 CHECK(h3lis_init(&bus) == DEV_ERR_ID);
42 CHECK(f.writes == 0);
43}
44
47 healthy(&f);
48 f.stuck[0x22] = 0x02; /* INT1 routing bit stuck low */
49 dev_bus_t bus = fake_regs_bus(&f);
51}
52
53static void test_bus_error_is_reported(void) {
55 healthy(&f);
56 f.fail_after = 2;
57 dev_bus_t bus = fake_regs_bus(&f);
59}
60
63 healthy(&f);
64 f.regs[0x29] = 0x81; /* -127: -99.06 g */
65 f.regs[0x2A] = 0x55; /* reserved, must be skipped */
66 f.regs[0x2B] = 0x01; /* +1: 0.78 g */
67 f.regs[0x2C] = 0x55;
68 f.regs[0x2D] = 0x7F; /* +127 */
69 dev_bus_t bus = fake_regs_bus(&f);
71 CHECK(h3lis_read(&bus, &s) == DEV_OK);
72 CHECK(s.accel[0] == -127);
73 CHECK(s.accel[1] == 1);
74 CHECK(s.accel[2] == 127);
75 CHECK(f.transfers == 1);
76}
77
79 /* Guards the MS bit: with increment off the fake returns X for every
80 * byte, so a correct driver must see distinct Y and Z. */
82 healthy(&f);
83 f.regs[0x29] = 10;
84 f.regs[0x2B] = 20;
85 f.regs[0x2D] = 30;
86 dev_bus_t bus = fake_regs_bus(&f);
88 CHECK(h3lis_read(&bus, &s) == DEV_OK);
89 CHECK(s.accel[1] == 20 && s.accel[2] == 30);
90}
91
@ DEV_ERR_VERIFY
Definition dev_bus.h:20
@ DEV_OK
Definition dev_bus.h:17
@ DEV_ERR_ID
Definition dev_bus.h:19
@ DEV_ERR_BUS
Definition dev_bus.h:18
uint8_t reg
int h3lis_read(const dev_bus_t *bus, h3lis_sample_t *out)
int h3lis_init(const dev_bus_t *bus)
dev_bus_t fake_regs_bus(fake_regs_t *f)
Definition fake_regs.c:59
void fake_regs_init(fake_regs_t *f, bool ms_bit)
Definition fake_regs.c:5
uint8_t regs[128]
Definition fake_regs.h:17
unsigned writes
Definition fake_regs.h:26
uint8_t stuck[128]
Definition fake_regs.h:24
uint8_t write_reg[64]
Definition fake_regs.h:27
unsigned transfers
Definition fake_regs.h:25
int fail_after
Definition fake_regs.h:19
uint8_t write_val[64]
Definition fake_regs.h:28
int main()
static void test_wrong_identity_is_refused_before_any_write(void)
static void test_read_back_mismatch_fails(void)
static void test_bus_error_is_reported(void)
static void test_init_writes_the_datasheet_configuration(void)
static void test_read_takes_x_y_z_from_29_2b_2d(void)
#define CHECK(c)
static void test_read_without_ms_bit_would_read_x_three_times(void)
static void healthy(fake_regs_t *f)