STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
dev_ism330dhcx.c
Go to the documentation of this file.
1#include "dev_ism330dhcx.h"
2
3#include <stddef.h>
4
5enum {
14};
15
16#define SPI_READ 0x80U /* bit 7 = 1: read (p.21) */
17#define CTRL3_SW_RESET 0x01U /* self-clearing (p.51) */
18#define RESET_POLLS 10U /* 1 ms apart; reset takes far less */
19
20static const struct {
21 uint8_t reg;
22 uint8_t value;
23} config[] = {
24 {REG_CTRL3_C, 0x44}, /* BDU + IF_INC */
25 {REG_CTRL4_C, 0x04}, /* I2C_disable */
26 {REG_CTRL1_XL, 0x74}, /* 833 Hz, +-16 g */
27 {REG_CTRL2_G, 0x71}, /* 833 Hz, +-4000 dps */
28 {REG_COUNTER_BDR_REG1, 0x80}, /* pulsed data-ready */
29 {REG_INT1_CTRL, 0x01}, /* INT1 = accelerometer data-ready */
30};
31
32static int write_reg(const dev_bus_t* bus, uint8_t reg, uint8_t value) {
33 const uint8_t tx[2] = {reg, value};
34 return bus->xfer(bus->ctx, tx, NULL, 2);
35}
36
37static int read_reg(const dev_bus_t* bus, uint8_t reg, uint8_t* value) {
38 const uint8_t tx[2] = {(uint8_t)(SPI_READ | reg), 0};
39 uint8_t rx[2];
40 int err = bus->xfer(bus->ctx, tx, rx, 2);
41 *value = rx[1];
42 return err;
43}
44
45int ism330_init(const dev_bus_t* bus) {
46 if (!bus || !bus->xfer || !bus->delay_ms) {
47 return DEV_ERR_PARAM;
48 }
49 uint8_t value = 0;
50 if (read_reg(bus, REG_WHO_AM_I, &value) != DEV_OK) {
51 return DEV_ERR_BUS;
52 }
54 return DEV_ERR_ID;
55 }
57 return DEV_ERR_BUS;
58 }
59 unsigned polls = 0;
60 do {
61 bus->delay_ms(bus->ctx, 1);
62 if (read_reg(bus, REG_CTRL3_C, &value) != DEV_OK) {
63 return DEV_ERR_BUS;
64 }
65 } while ((value & CTRL3_SW_RESET) && ++polls < RESET_POLLS);
66 if (value & CTRL3_SW_RESET) {
67 return DEV_ERR_TIMEOUT;
68 }
69 for (size_t i = 0; i < sizeof(config) / sizeof(config[0]); ++i) {
70 if (write_reg(bus, config[i].reg, config[i].value) != DEV_OK ||
71 read_reg(bus, config[i].reg, &value) != DEV_OK) {
72 return DEV_ERR_BUS;
73 }
74 if (value != config[i].value) {
75 return DEV_ERR_VERIFY;
76 }
77 }
78 return DEV_OK;
79}
80
82 if (!bus) {
83 return DEV_ERR_PARAM;
84 }
85 if (write_reg(bus, REG_CTRL1_XL, 0x00) != DEV_OK ||
86 write_reg(bus, REG_CTRL2_G, 0x00) != DEV_OK) {
87 return DEV_ERR_BUS;
88 }
89 return DEV_OK;
90}
91
92static int16_t le16(const uint8_t* p) {
93 return (int16_t)(uint16_t)(p[0] | (uint16_t)p[1] << 8);
94}
95
96int ism330_read(const dev_bus_t* bus, ism330_sample_t* out) {
97 if (!bus || !out) {
98 return DEV_ERR_PARAM;
99 }
100 uint8_t tx[15] = {(uint8_t)(SPI_READ | REG_OUT_TEMP_L)};
101 uint8_t rx[15];
102 if (bus->xfer(bus->ctx, tx, rx, sizeof(tx)) != DEV_OK) {
103 return DEV_ERR_BUS;
104 }
105 /* 20h-21h temperature, 22h-27h gyro x/y/z, 28h-2Dh accel x/y/z (p.64-66) */
106 out->temp = le16(&rx[1]);
107 for (int i = 0; i < 3; ++i) {
108 out->gyro[i] = le16(&rx[3 + 2 * i]);
109 out->accel[i] = le16(&rx[9 + 2 * i]);
110 }
111 return DEV_OK;
112}
@ DEV_ERR_VERIFY
Definition dev_bus.h:20
@ DEV_OK
Definition dev_bus.h:17
@ DEV_ERR_PARAM
Definition dev_bus.h:22
@ DEV_ERR_TIMEOUT
Definition dev_bus.h:21
@ DEV_ERR_ID
Definition dev_bus.h:19
@ DEV_ERR_BUS
Definition dev_bus.h:18
#define SPI_READ
uint8_t reg
@ REG_WHO_AM_I
#define CTRL3_SW_RESET
static int write_reg(const dev_bus_t *bus, uint8_t reg, uint8_t value)
int ism330_init(const dev_bus_t *bus)
@ REG_CTRL1_XL
@ REG_CTRL2_G
@ REG_CTRL4_C
@ REG_CTRL3_C
@ REG_COUNTER_BDR_REG1
@ REG_OUT_TEMP_L
@ REG_INT1_CTRL
int ism330_power_down(const dev_bus_t *bus)
static int16_t le16(const uint8_t *p)
int ism330_read(const dev_bus_t *bus, ism330_sample_t *out)
#define RESET_POLLS
static int read_reg(const dev_bus_t *bus, uint8_t reg, uint8_t *value)
#define ISM330_WHO_AM_I_VALUE
int32_t p
Definition flight_e2e.c:78
#define NULL
Definition usbd_def.h:44
static const idle_config_t config
Definition idle_task.c:17