SMBus.cpp
5.54 KB
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
/****************************************************************************
*
* Copyright (c) 2018 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file SMBus.cpp
* SMBus v2.0 protocol implementation.
*
* @author Jacob Dahl <dahl.jakejacob@gmail.com>
* @author Bazooka Joe <BazookaJoe1900@gmail.com>
*
* TODO
* - Enable SMBus mode at the NuttX level. This may be tricky sharing the bus with i2c.
* i.e STM32f4 see platforms/nuttx/Nuttx/nuttx/arch/arm/src/stm32/stm32f40xxx_i2c.c
* - Add remaining SMBus protocol messages as needed
*/
#include "SMBus.hpp"
#include <mathlib/mathlib.h>
SMBus::SMBus(int bus_num, uint16_t address) :
I2C(DRV_BAT_DEVTYPE_SMBUS, MODULE_NAME, bus_num, address, 100000)
{
}
SMBus::~SMBus()
{
perf_free(_interface_errors);
}
int SMBus::read_word(const uint8_t cmd_code, uint16_t &data)
{
uint8_t buf[6];
// 2 data bytes + pec byte
int result = transfer(&cmd_code, 1, buf + 3, 3);
if (result == PX4_OK) {
data = buf[3] | ((uint16_t)buf[4] << 8);
// Check PEC.
uint8_t addr = get_device_address() << 1;
buf[0] = addr | 0x00;
buf[1] = cmd_code;
buf[2] = addr | 0x01;
uint8_t pec = get_pec(buf, sizeof(buf) - 1);
if (pec != buf[sizeof(buf) - 1]) {
result = -EINVAL;
perf_count(_interface_errors);
}
} else {
perf_count(_interface_errors);
}
return result;
}
int SMBus::write_word(const uint8_t cmd_code, uint16_t data)
{
// 2 data bytes + pec byte
uint8_t buf[5];
buf[0] = (get_device_address() << 1) | 0x10;
buf[1] = cmd_code;
buf[2] = data & 0xff;
buf[3] = (data >> 8) & 0xff;
buf[4] = get_pec(buf, 4);
int result = transfer(&buf[1], 4, nullptr, 0);
if (result != PX4_OK) {
perf_count(_interface_errors);
}
return result;
}
int SMBus::block_read(const uint8_t cmd_code, void *data, const uint8_t length, const bool use_pec)
{
uint8_t byte_count = 0;
// addr(wr), cmd_code, addr(r), byte_count, data (MAX_BLOCK_LEN bytes max), pec
uint8_t rx_data[MAX_BLOCK_LEN + 5];
if (length > MAX_BLOCK_LEN) {
return -EINVAL;
}
int result = transfer(&cmd_code, 1, (uint8_t *)&rx_data[3], length + 2);
if (result != PX4_OK) {
perf_count(_interface_errors);
return result;
}
uint8_t device_address = get_device_address();
rx_data[0] = (device_address << 1) | 0x00;
rx_data[1] = cmd_code;
rx_data[2] = (device_address << 1) | 0x01;
byte_count = math::min(rx_data[3], MAX_BLOCK_LEN);
// ensure data is not longer than given buffer
memcpy(data, &rx_data[4], math::min(byte_count, length));
if (use_pec) {
uint8_t pec = get_pec(rx_data, byte_count + 4);
if (pec != rx_data[byte_count + 4]) {
result = -EIO;
perf_count(_interface_errors);
}
}
return result;
}
int SMBus::block_write(const uint8_t cmd_code, const void *data, uint8_t byte_count, const bool use_pec)
{
// cmd code[1], byte count[1], data[byte_count] (MAX_BLOCK_LEN max), pec[1] (optional)
uint8_t buf[MAX_BLOCK_LEN + 2];
if (byte_count > MAX_BLOCK_LEN) {
return -EINVAL;
}
buf[0] = cmd_code;
buf[1] = (uint8_t)byte_count;
memcpy(&buf[2], data, byte_count);
if (use_pec) {
uint8_t pec = get_pec(buf, byte_count + 2);
buf[byte_count + 2] = pec;
byte_count++;
}
unsigned i = 0;
int result = 0;
// If block_write fails, try up to 10 times.
while (i < 10 && ((result = transfer((uint8_t *)buf, byte_count + 2, nullptr, 0)) != PX4_OK)) {
perf_count(_interface_errors);
i++;
}
if (i == 10 || result) {
result = -EINVAL;
}
return result;
}
uint8_t SMBus::get_pec(uint8_t *buff, const uint8_t len)
{
// TODO: use "return crc8ccitt(buff, len);"
// Initialise CRC to zero.
uint8_t crc = 0;
uint8_t shift_register = 0;
bool invert_crc;
// Calculate crc for each byte in the stream.
for (uint8_t i = 0; i < len; i++) {
// Load next data byte into the shift register
shift_register = buff[i];
// Calculate crc for each bit in the current byte.
for (uint8_t j = 0; j < 8; j++) {
invert_crc = (crc ^ shift_register) & 0x80;
crc <<= 1;
shift_register <<= 1;
if (invert_crc) {
crc ^= SMBUS_PEC_POLYNOMIAL;
}
}
}
return crc;
}