DevLab_ICM20948 1.0.0 ICM-20948 9-Axis Motion Sensor Driver

Loading...
Searching...
No Matches
BusIO_7Semi.h
Go to the documentation of this file.
1#pragma once
2#include <Arduino.h>
3
4 template <typename Bus>
5 class BusIO_7Semi
6{
7
8 public:
9 /**
10 * Constructor
11 *
12 * - Stores interface reference
13 */
14 BusIO_7Semi(Bus &busRef) : bus(busRef) {}
15
16 /** read 8-bit */
17 inline bool read(uint8_t reg, uint8_t &value)
18 {
19 return (bus.read(reg, &value, 1) == 0);
20 }
21
22 /** read 16-bit */
23 inline bool read(uint8_t reg, uint16_t &value)
24 {
25 uint8_t data[2];
26 if (bus.read(reg, data, 2) != 0)
27 return false;
28 value = (data[0] << 8) | data[1];
29 return true;
30 }
31
32 /** read burst */
33 inline bool read(uint8_t reg, uint8_t *data, uint32_t len)
34 {
35 return (bus.read(reg, data, len) == 0);
36 }
37
38 /** write 8-bit */
39 inline bool write(uint8_t reg, uint8_t value)
40 {
41 return (bus.write(reg, &value, 1) == 0);
42 }
43
44 /** write 16-bit */
45 inline bool write(uint8_t reg, uint16_t value)
46 {
47 uint8_t data[2] = {(uint8_t)(value >> 8), (uint8_t)(value & 0xFF)};
48 return (bus.write(reg, data, 2) == 0);
49 }
50
51 /** write burst */
52 inline bool write(uint8_t reg, const uint8_t *data, uint32_t len)
53 {
54 return (bus.write(reg, data, len) == 0);
55 }
56
57 /**
58 * readBits (8-bit register)
59 */
60 bool readBits(uint8_t reg, uint8_t pos, uint8_t len, uint8_t &value)
61 {
62 if (len == 0 || len > 8 || (pos + len) > 8)
63 return false;
64
65 uint8_t reg_val;
66 if (!read(reg, reg_val))
67 return false;
68
69 uint8_t mask = ((uint8_t)1 << len) - 1;
70
71 value = (reg_val >> pos) & mask;
72 return true;
73 }
74
75 /**
76 * readBits (16-bit register)
77 */
78 bool readBits(uint8_t reg, uint8_t pos, uint8_t len, uint16_t &value)
79 {
80 if (len == 0 || len > 16 || (pos + len) > 16)
81 return false;
82
83 uint8_t reg_val;
84 if (!read(reg, reg_val))
85 return false;
86
87 uint16_t mask = ((uint16_t)1 << len) - 1;
88
89 value = (reg_val >> pos) & mask;
90 return true;
91 }
92
93 /**
94 * writeBits (8-bit register)
95 *
96 * - Modifies specific bits in 8-bit register
97 */
98 bool writeBits(uint8_t reg, uint8_t pos, uint8_t len, uint8_t value)
99 {
100 if (len == 0 || len > 8 || (pos + len) > 8)
101 return false;
102
103 uint8_t reg_val;
104 if (!read(reg, reg_val))
105 return false;
106
107 uint8_t mask = ((uint8_t)1 << len) - 1;
108
109 reg_val &= ~(mask << pos);
110 reg_val |= (value & mask) << pos;
111
112 return write(reg, reg_val);
113 }
114
115 /**
116 * writeBits (16-bit register)
117 *
118 * - Modifies specific bits in 16-bit register
119 */
120 bool writeBits(uint8_t reg, uint8_t pos, uint8_t len, uint16_t value)
121 {
122 if (len == 0 || len > 16 || (pos + len) > 16)
123 return false;
124
125 uint8_t reg_val;
126 if (!read(reg, reg_val))
127 return false;
128
129 uint16_t mask = ((uint16_t)1 << len) - 1;
130
131 reg_val &= ~(mask << pos);
132 reg_val |= (value & mask) << pos;
133
134 return write(reg, reg_val);
135 }
136
137 bool readBit(uint8_t reg, uint8_t pos, uint8_t &value)
138 {
139 return readBits(reg, pos, 1, value);
140 }
141
142 bool readBit(uint8_t reg, uint8_t pos, uint16_t &value)
143 {
144 return readBits(reg, pos, 1, value);
145 }
146
147 bool writeBit(uint8_t reg, uint8_t pos, uint8_t value)
148 {
149 return writeBits(reg, pos, 1, (uint8_t)value );
150 }
151 bool writeBit(uint8_t reg, uint8_t pos, uint16_t value)
152 {
153 return writeBits(reg, pos, 1, value);
154 }
155
156 private:
157 Bus &bus;
158};
BusIO_7Semi
Definition BusIO_7Semi.h:6
BusIO_7Semi::read
bool read(uint8_t reg, uint16_t &value)
Definition BusIO_7Semi.h:23
BusIO_7Semi::readBit
bool readBit(uint8_t reg, uint8_t pos, uint8_t &value)
Definition BusIO_7Semi.h:137
BusIO_7Semi::readBits
bool readBits(uint8_t reg, uint8_t pos, uint8_t len, uint16_t &value)
Definition BusIO_7Semi.h:78
BusIO_7Semi::readBit
bool readBit(uint8_t reg, uint8_t pos, uint16_t &value)
Definition BusIO_7Semi.h:142
BusIO_7Semi::writeBit
bool writeBit(uint8_t reg, uint8_t pos, uint16_t value)
Definition BusIO_7Semi.h:151
BusIO_7Semi::read
bool read(uint8_t reg, uint8_t *data, uint32_t len)
Definition BusIO_7Semi.h:33
BusIO_7Semi::writeBits
bool writeBits(uint8_t reg, uint8_t pos, uint8_t len, uint8_t value)
Definition BusIO_7Semi.h:98
BusIO_7Semi::write
bool write(uint8_t reg, const uint8_t *data, uint32_t len)
Definition BusIO_7Semi.h:52
BusIO_7Semi::read
bool read(uint8_t reg, uint8_t &value)
Definition BusIO_7Semi.h:17
BusIO_7Semi::write
bool write(uint8_t reg, uint8_t value)
Definition BusIO_7Semi.h:39
BusIO_7Semi::write
bool write(uint8_t reg, uint16_t value)
Definition BusIO_7Semi.h:45
BusIO_7Semi::BusIO_7Semi
BusIO_7Semi(Bus &busRef)
Definition BusIO_7Semi.h:14
BusIO_7Semi::writeBit
bool writeBit(uint8_t reg, uint8_t pos, uint8_t value)
Definition BusIO_7Semi.h:147
BusIO_7Semi::readBits
bool readBits(uint8_t reg, uint8_t pos, uint8_t len, uint8_t &value)
Definition BusIO_7Semi.h:60
BusIO_7Semi::writeBits
bool writeBits(uint8_t reg, uint8_t pos, uint8_t len, uint16_t value)
Definition BusIO_7Semi.h:120