Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d3b54c3

Browse files
Jake Dahlbkueng
Jake Dahl
authored andcommitted
more changes based on suggestions
1 parent 985c5f4 commit d3b54c3

File tree

1 file changed

+17
-34
lines changed

1 file changed

+17
-34
lines changed

src/lib/drivers/smbus/SMBus.cpp

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -69,33 +69,23 @@ int SMBus::read_word(const uint8_t cmd_code, void *data)
6969
int SMBus::block_read(const uint8_t cmd_code, void *data, const uint8_t length, bool use_pec)
7070
{
7171
unsigned byte_count = 0;
72-
// Length of data (32max). byte_count(1), cmd_code(2) (sometimes), pec(1) (optional)
73-
uint8_t rx_data[32 + 4];
72+
// addr(wr), cmd_code, addr(r), byte_count, data (32 bytes max), pec
73+
uint8_t rx_data[32 + 5];
7474

75-
int result = transfer(&cmd_code, 1, (uint8_t *)rx_data, length + 2);
75+
int result = transfer(&cmd_code, 1, (uint8_t *)&rx_data[3], length + 2);
7676

77-
byte_count = rx_data[0];
77+
uint8_t device_address = get_device_address();
78+
rx_data[0] = (device_address << 1) | 0x00;
79+
rx_data[1] = cmd_code;
80+
rx_data[2] = (device_address << 1) | 0x01;
81+
byte_count = rx_data[3];
7882

79-
// byte_count, data[length], PEC
80-
memcpy(data, &rx_data[1], byte_count);
83+
memcpy(data, &rx_data[4], byte_count);
8184

8285
if (use_pec) {
86+
uint8_t pec = get_pec(rx_data, byte_count + 4);
8387

84-
// addr(wr), cmd_code, addr(r), byte_count, rx_data[]
85-
uint8_t device_address = get_device_address();
86-
uint8_t full_data_packet[32 + 4] = {};
87-
88-
full_data_packet[0] = (device_address << 1) | 0x00;
89-
full_data_packet[1] = cmd_code;
90-
full_data_packet[2] = (device_address << 1) | 0x01;
91-
full_data_packet[3] = byte_count;
92-
93-
memcpy(&full_data_packet[4], &rx_data[1], byte_count);
94-
95-
uint8_t pec = get_pec(full_data_packet, byte_count + 4);
96-
97-
// First byte is byte count, followed by data.
98-
if (pec != ((uint8_t *)rx_data)[byte_count + 1]) {
88+
if (pec != rx_data[byte_count + 4]) {
9989
result = -EINVAL;
10090
}
10191
}
@@ -122,20 +112,13 @@ int SMBus::block_write(const uint8_t cmd_code, void *data, uint8_t byte_count, b
122112
int result = 0;
123113

124114
// If block_write fails, try up to 10 times.
125-
while (i < 10) {
126-
result = transfer((uint8_t *)buf, byte_count + 2, nullptr, 0);
127-
128-
if (result != PX4_OK) {
129-
i++;
130-
131-
if (i == 10) {
132-
PX4_WARN("Block_write failed 10 times");
133-
result = -EINVAL;
134-
}
115+
while (i < 10 && (result = transfer((uint8_t *)buf, byte_count + 2, nullptr, 0)) != PX4_OK) {
116+
i++;
117+
}
135118

136-
} else {
137-
break;
138-
}
119+
if (i == 10) {
120+
PX4_WARN("Block_write failed 10 times");
121+
result = -EINVAL;
139122
}
140123

141124
return result;

0 commit comments

Comments
 (0)