Skip to content

Commit 74a8a0b

Browse files
committed
ReadLen,WriteLen, Don't allocate *data, if we don't expect ACK
1 parent 380da32 commit 74a8a0b

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/BNO055ESP32.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ std::exception BNO055::getException(uint8_t errcode){
7171
}
7272
}
7373

74-
void BNO055::readLen(bno055_reg_t reg, uint8_t len, uint8_t *buffer, uint32_t timoutMS){
74+
void BNO055::readLen(bno055_reg_t reg, uint8_t len, uint8_t *buffer, uint32_t timeoutMS){
7575
uint8_t res = 0;
7676
memset(buffer, 0, len);
7777

@@ -80,8 +80,11 @@ void BNO055::readLen(bno055_reg_t reg, uint8_t len, uint8_t *buffer, uint32_t ti
8080
cmd[1] = 0x01; // Read
8181
cmd[2] = reg & 0xFF;
8282
cmd[3] = len & 0xFF; // len in bytes
83+
uint8_t *data = NULL;
8384

84-
uint8_t *data = (uint8_t *) malloc(len+2);
85+
if (timeoutMS > 0){ // if we are expecting ack then allocate *data
86+
data = (uint8_t *) malloc(len+2);
87+
}
8588

8689
for (int round = 1; round <= UART_ROUND_NUM; round++){
8790
#ifndef BNO055_DEBUG_OFF
@@ -96,15 +99,14 @@ void BNO055::readLen(bno055_reg_t reg, uint8_t len, uint8_t *buffer, uint32_t ti
9699
ESP_LOG_BUFFER_HEXDUMP(BNO055_LOG_TAG, (const char*) cmd, 4, ESP_LOG_DEBUG);
97100
#endif
98101

99-
if (timoutMS <= 0){
102+
if (timeoutMS == 0){
100103
free(cmd);
101-
free(data);
102-
return; // Do no expect ACK
104+
return; // Do not expect ACK
103105
}
104106
// else expect ACK
105107

106108
// Read data from the UART
107-
int rxBytes = uart_read_bytes(_uartPort, data, (len+2), timoutMS / portTICK_RATE_MS);
109+
int rxBytes = uart_read_bytes(_uartPort, data, (len+2), timeoutMS / portTICK_RATE_MS);
108110
if (rxBytes > 0) {
109111
//data[rxBytes] = 0;
110112

@@ -154,7 +156,7 @@ void BNO055::readLen(bno055_reg_t reg, uint8_t len, uint8_t *buffer, uint32_t ti
154156
}
155157
}
156158

157-
void BNO055::writeLen(bno055_reg_t reg, uint8_t *data2write, uint8_t len, uint32_t timoutMS){
159+
void BNO055::writeLen(bno055_reg_t reg, uint8_t *data2write, uint8_t len, uint32_t timeoutMS){
158160
uint8_t res = 0;
159161

160162
uint8_t *cmd = (uint8_t *) malloc(len+4);
@@ -163,8 +165,11 @@ void BNO055::writeLen(bno055_reg_t reg, uint8_t *data2write, uint8_t len, uint32
163165
cmd[2] = reg & 0xFF;
164166
cmd[3] = len & 0xFF; // len in bytes
165167
memcpy(cmd+4, data2write, len);
168+
uint8_t *data = NULL;
166169

167-
uint8_t *data = (uint8_t *) malloc(2);
170+
if (timeoutMS > 0){ // if we are expecting ack allocate *data
171+
data = (uint8_t *) malloc(2);
172+
}
168173

169174
// Read data from the UART
170175
for (int round = 1; round <= UART_ROUND_NUM; round++){
@@ -181,14 +186,13 @@ void BNO055::writeLen(bno055_reg_t reg, uint8_t *data2write, uint8_t len, uint32
181186
ESP_LOG_BUFFER_HEXDUMP(BNO055_LOG_TAG, (const char*) cmd,(len+4), ESP_LOG_DEBUG);
182187
#endif
183188

184-
if (timoutMS <= 0){
189+
if (timeoutMS == 0){
185190
free(cmd);
186-
free(data);
187-
return; // do not expect ACK
191+
return; // Do not expect ACK
188192
}
189193
//else expect ACK
190194

191-
int rxBytes = uart_read_bytes(_uartPort, data, 2, timoutMS / portTICK_RATE_MS);
195+
int rxBytes = uart_read_bytes(_uartPort, data, 2, timeoutMS / portTICK_RATE_MS);
192196
if (rxBytes > 0) {
193197
res = data[1]; // in case of error, this will be the errorCode.
194198
//data[rxBytes] = 0;
@@ -235,12 +239,12 @@ void BNO055::writeLen(bno055_reg_t reg, uint8_t *data2write, uint8_t len, uint32
235239
}
236240
}
237241

238-
void BNO055::read8(bno055_reg_t reg, uint8_t *val, uint32_t timoutMS){
239-
readLen(reg, 1, val, timoutMS);
242+
void BNO055::read8(bno055_reg_t reg, uint8_t *val, uint32_t timeoutMS){
243+
readLen(reg, 1, val, timeoutMS);
240244
}
241245

242-
void BNO055::write8(bno055_reg_t reg, uint8_t val, uint32_t timoutMS){
243-
writeLen(reg, &val, 1, timoutMS);
246+
void BNO055::write8(bno055_reg_t reg, uint8_t val, uint32_t timeoutMS){
247+
writeLen(reg, &val, 1, timeoutMS);
244248
}
245249

246250
void BNO055::setPage(uint8_t page, bool forced){

src/BNO055ESP32.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -619,10 +619,10 @@ class BNO055{
619619

620620
std::exception getException(uint8_t errcode);
621621

622-
void readLen(bno055_reg_t reg, uint8_t len, uint8_t *buffer, uint32_t timoutMS = DEFAULT_UART_TIMEOUT_MS);
623-
void read8(bno055_reg_t reg, uint8_t *val, uint32_t timoutMS = DEFAULT_UART_TIMEOUT_MS);
624-
void writeLen(bno055_reg_t reg, uint8_t *data, uint8_t len, uint32_t timoutMS = DEFAULT_UART_TIMEOUT_MS);
625-
void write8(bno055_reg_t reg, uint8_t val, uint32_t timoutMS = DEFAULT_UART_TIMEOUT_MS);
622+
void readLen(bno055_reg_t reg, uint8_t len, uint8_t *buffer, uint32_t timeoutMS = DEFAULT_UART_TIMEOUT_MS);
623+
void read8(bno055_reg_t reg, uint8_t *val, uint32_t timeoutMS = DEFAULT_UART_TIMEOUT_MS);
624+
void writeLen(bno055_reg_t reg, uint8_t *data, uint8_t len, uint32_t timeoutMS = DEFAULT_UART_TIMEOUT_MS);
625+
void write8(bno055_reg_t reg, uint8_t val, uint32_t timeoutMS = DEFAULT_UART_TIMEOUT_MS);
626626

627627
bool interruptFlag = false;
628628

0 commit comments

Comments
 (0)