1 /*
2 * Copyright (c) 2020 TDK Invensense
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/logging/log.h>
8 #include <zephyr/sys/__assert.h>
9 #include "icm42605_spi.h"
10
11 LOG_MODULE_DECLARE(ICM42605, CONFIG_SENSOR_LOG_LEVEL);
12
inv_spi_single_write(const struct spi_dt_spec * bus,uint8_t reg,uint8_t * data)13 int inv_spi_single_write(const struct spi_dt_spec *bus, uint8_t reg, uint8_t *data)
14 {
15 int result;
16
17 const struct spi_buf buf[2] = {
18 {
19 .buf = ®,
20 .len = 1,
21 },
22 {
23 .buf = data,
24 .len = 1,
25 }
26 };
27 const struct spi_buf_set tx = {
28 .buffers = buf,
29 .count = 2,
30 };
31
32 result = spi_write_dt(bus, &tx);
33
34 if (result) {
35 return result;
36 }
37
38 return 0;
39 }
40
inv_spi_read(const struct spi_dt_spec * bus,uint8_t reg,uint8_t * data,size_t len)41 int inv_spi_read(const struct spi_dt_spec *bus, uint8_t reg, uint8_t *data, size_t len)
42 {
43 int result;
44 unsigned char tx_buffer[2] = { 0, };
45
46 tx_buffer[0] = 0x80 | reg;
47
48 const struct spi_buf tx_buf = {
49 .buf = tx_buffer,
50 .len = 1,
51 };
52 const struct spi_buf_set tx = {
53 .buffers = &tx_buf,
54 .count = 1,
55 };
56
57 struct spi_buf rx_buf[2] = {
58 {
59 .buf = tx_buffer,
60 .len = 1,
61 },
62 {
63 .buf = data,
64 .len = len,
65 }
66 };
67
68 const struct spi_buf_set rx = {
69 .buffers = rx_buf,
70 .count = 2,
71 };
72
73 result = spi_transceive_dt(bus, &tx, &rx);
74
75 if (result) {
76 return result;
77 }
78
79 return 0;
80 }
81