1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-25 09:16:07 +03:00

Tidied serializeDataflashReadReply

This commit is contained in:
Martin Budden 2016-10-19 22:45:25 +01:00
parent 613239b8c8
commit 92c3fb7d8d
2 changed files with 23 additions and 17 deletions

View file

@ -71,8 +71,9 @@
#include "io/serial_4way.h" #include "io/serial_4way.h"
#include "io/vtx.h" #include "io/vtx.h"
#include "msp/msp_protocol.h"
#include "msp/msp.h" #include "msp/msp.h"
#include "msp/msp_protocol.h"
#include "msp/msp_serial.h"
#include "rx/rx.h" #include "rx/rx.h"
#include "rx/msp.h" #include "rx/msp.h"
@ -339,30 +340,33 @@ static void serializeDataflashSummaryReply(sbuf_t *dst)
} }
#ifdef USE_FLASHFS #ifdef USE_FLASHFS
static void serializeDataflashReadReply(sbuf_t *dst, uint32_t address, uint16_t size, bool useLegacyFormat) static void serializeDataflashReadReply(sbuf_t *dst, uint32_t address, const uint16_t size, bool useLegacyFormat)
{ {
const int bytesRemaning = sbufBytesRemaining(dst); BUILD_BUG_ON(MSP_PORT_DATAFLASH_INFO_SIZE < 16);
if (size > bytesRemaning - 16) {
size = bytesRemaning - 16; uint16_t readLen = size;
const int bytesRemainingInBuf = sbufBytesRemaining(dst) - MSP_PORT_DATAFLASH_INFO_SIZE;
if (readLen > bytesRemainingInBuf) {
readLen = bytesRemainingInBuf;
} }
// bytesRead will be lower than that requested if we reach end of volume // size will be lower than that requested if we reach end of volume
if (size > flashfsGetSize() - address) { if (readLen > flashfsGetSize() - address) {
// Truncate the request // truncate the request
size = flashfsGetSize() - address; readLen = flashfsGetSize() - address;
} }
if (useLegacyFormat) { sbufWriteU32(dst, address);
sbufWriteU32(dst, address); if (!useLegacyFormat) {
} else { // new format supports variable read lengths
sbufWriteU32(dst, address); sbufWriteU16(dst, readLen);
sbufWriteU16(dst, size);
sbufWriteU8(dst, 0); // placeholder for compression format sbufWriteU8(dst, 0); // placeholder for compression format
} }
// bytesRead will equal size // bytesRead will equal readLen
const int bytesRead = flashfsReadAbs(address, sbufPtr(dst), size); const int bytesRead = flashfsReadAbs(address, sbufPtr(dst), readLen);
sbufAdvance(dst, bytesRead); sbufAdvance(dst, bytesRead);
if (useLegacyFormat) { if (useLegacyFormat) {
// pad the buffer with zeros
for (int i = bytesRead; i < size; i++) { for (int i = bytesRead; i < size; i++) {
sbufWriteU8(dst, 0); sbufWriteU8(dst, 0);
} }

View file

@ -39,7 +39,9 @@ typedef enum {
#define MSP_PORT_INBUF_SIZE 192 #define MSP_PORT_INBUF_SIZE 192
#ifdef USE_FLASHFS #ifdef USE_FLASHFS
#define MSP_PORT_OUTBUF_SIZE (4096 + 16) #define MSP_PORT_DATAFLASH_BUFFER_SIZE 4096
#define MSP_PORT_DATAFLASH_INFO_SIZE 16
#define MSP_PORT_OUTBUF_SIZE (MSP_PORT_DATAFLASH_BUFFER_SIZE + MSP_PORT_DATAFLASH_INFO_SIZE)
#else #else
#define MSP_PORT_OUTBUF_SIZE 256 #define MSP_PORT_OUTBUF_SIZE 256
#endif #endif