Phase 3: STM32 Removal: Shared RX/TX parts (#3016)
[ExpressLRS.git] / src / lib / OPTIONS / EspFlashStream.cpp
blob01954808a99461d094a7284ba82fac815fa002e5
1 #if !defined(UNIT_TEST)
3 #include "EspFlashStream.h"
5 EspFlashStream::EspFlashStream()
7 setBaseAddress(0);
10 void EspFlashStream::fillBuffer()
12 // Could also use spi_flash_read() here, but the return values differ between ESP (SPI_FLASH_RESULT_OK) and ESP32 (ESP_OK)
13 if (ESP.flashRead(_flashBase + _flashOffset, (uint32_t *)_buffer, sizeof(_buffer)))
15 _bufferPos = 0;
17 else
19 // _bufferPos > sizeof() indicates error
20 _bufferPos = sizeof(_buffer) + 1;
24 void EspFlashStream::setBaseAddress(size_t base)
26 _flashBase = base;
27 _flashOffset = 0 - sizeof(_buffer); // underflow intentionally so offset+pos = 0
28 _bufferPos = sizeof(_buffer);
31 void EspFlashStream::setPosition(size_t offset)
33 // align(4), rounding down
34 _flashOffset = (offset >> 2) << 2;
35 // capture the new bufferPos before fill advances it
36 uint8_t newBufferPos = offset - _flashOffset;
38 fillBuffer();
39 _bufferPos = newBufferPos;
42 int EspFlashStream::read()
44 int retVal = peek();
45 ++_bufferPos;
46 return retVal;
49 int EspFlashStream::peek()
51 if (_bufferPos == sizeof(_buffer))
53 _flashOffset += sizeof(_buffer);
54 fillBuffer();
56 return _buffer[_bufferPos];
59 #endif