-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add services/st_util/StUartProgammer (#185)
* feat: add services/st_util/StUartProgammer * .github/workflows/ci.yml: build and test for hosts * CMakePresets: Fix test preset * Update ci to match amp-embedded-infra-lib
- Loading branch information
1 parent
d76e1c8
commit 8faceb4
Showing
9 changed files
with
639 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_subdirectory(st_util) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
add_library(services.st_util STATIC) | ||
|
||
target_include_directories(services.st_util PUBLIC | ||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../..>" | ||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" | ||
) | ||
|
||
target_sources(services.st_util PRIVATE | ||
FlashOnStUartProgrammer.cpp | ||
FlashOnStUartProgrammer.hpp | ||
StUartProgrammer.cpp | ||
StUartProgrammer.hpp | ||
) | ||
|
||
target_link_libraries(services.st_util PUBLIC | ||
hal.interfaces | ||
services.util | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include "services/st_util/FlashOnStUartProgrammer.hpp" | ||
|
||
namespace services | ||
{ | ||
FlashOnStUartProgrammer::FlashOnStUartProgrammer(infra::MemoryRange<uint32_t> sectorSizes, StUartProgrammer& programmer) | ||
: sectorSizes(sectorSizes) | ||
, programmer(programmer) | ||
{} | ||
|
||
uint32_t FlashOnStUartProgrammer::NumberOfSectors() const | ||
{ | ||
return sectorSizes.size(); | ||
} | ||
|
||
uint32_t FlashOnStUartProgrammer::FlashOnStUartProgrammer::SizeOfSector(uint32_t sectorIndex) const | ||
{ | ||
return sectorSizes[sectorIndex]; | ||
} | ||
|
||
uint32_t FlashOnStUartProgrammer::SectorOfAddress(uint32_t address) const | ||
{ | ||
uint32_t totalSize = 0; | ||
for (uint32_t sector = 0; sector != sectorSizes.size(); ++sector) | ||
{ | ||
totalSize += sectorSizes[sector]; | ||
if (address < totalSize) | ||
return sector; | ||
} | ||
|
||
assert(address == totalSize); | ||
return sectorSizes.size(); | ||
} | ||
|
||
uint32_t FlashOnStUartProgrammer::AddressOfSector(uint32_t sectorIndex) const | ||
{ | ||
uint32_t address = 0; | ||
for (uint32_t sector = 0; sector != sectorIndex; ++sector) | ||
address += sectorSizes[sector]; | ||
return address; | ||
} | ||
|
||
void FlashOnStUartProgrammer::WriteBuffer(infra::ConstByteRange buffer, uint32_t address, infra::Function<void()> onDone) | ||
{ | ||
if (buffer.empty()) | ||
onDone(); | ||
else | ||
{ | ||
this->onDone = onDone; | ||
auto head = infra::Head(buffer, 256); | ||
writeTail = infra::DiscardHead(buffer, 256); | ||
tailAddress = address + head.size(); | ||
programmer.WriteMemory(address + 0x8000000, head, [this]() | ||
{ | ||
WriteBuffer(writeTail, tailAddress, std::exchange(this->onDone, nullptr)); | ||
}); | ||
} | ||
} | ||
|
||
void FlashOnStUartProgrammer::ReadBuffer(infra::ByteRange buffer, uint32_t address, infra::Function<void()> onDone) | ||
{ | ||
if (buffer.empty()) | ||
onDone(); | ||
else | ||
{ | ||
this->onDone = onDone; | ||
auto head = infra::Head(buffer, 256); | ||
readTail = infra::DiscardHead(buffer, 256); | ||
tailAddress = address + head.size(); | ||
programmer.ReadMemory(address + 0x8000000, head, [this]() | ||
{ | ||
ReadBuffer(readTail, tailAddress, std::exchange(this->onDone, nullptr)); | ||
}); | ||
} | ||
} | ||
|
||
void FlashOnStUartProgrammer::EraseSectors(uint32_t beginIndex, uint32_t endIndex, infra::Function<void()> onDone) | ||
{ | ||
if (beginIndex == endIndex) | ||
onDone(); | ||
if (beginIndex == 0 && endIndex == NumberOfSectors()) | ||
programmer.EraseAll(onDone); | ||
else | ||
{ | ||
this->onDone = onDone; | ||
this->beginIndex = beginIndex; | ||
this->endIndex = endIndex; | ||
programmer.ErasePage(beginIndex, [this]() | ||
{ | ||
EraseSectors(this->beginIndex + 1, this->endIndex, std::exchange(this->onDone, nullptr)); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#ifndef SERVICES_ST_UTIL_FLASH_ON_ST_UART_PROGRAMMER_HPP | ||
#define SERVICES_ST_UTIL_FLASH_ON_ST_UART_PROGRAMMER_HPP | ||
|
||
#include "hal/interfaces/Flash.hpp" | ||
#include "services/st_util/StUartProgrammer.hpp" | ||
|
||
namespace services | ||
{ | ||
class FlashOnStUartProgrammer | ||
: public hal::Flash | ||
{ | ||
public: | ||
FlashOnStUartProgrammer(infra::MemoryRange<uint32_t> sectorSizes, StUartProgrammer& programmer); | ||
|
||
virtual uint32_t NumberOfSectors() const override; | ||
virtual uint32_t SizeOfSector(uint32_t sectorIndex) const override; | ||
virtual uint32_t SectorOfAddress(uint32_t address) const override; | ||
virtual uint32_t AddressOfSector(uint32_t sectorIndex) const override; | ||
virtual void WriteBuffer(infra::ConstByteRange buffer, uint32_t address, infra::Function<void()> onDone) override; | ||
virtual void ReadBuffer(infra::ByteRange buffer, uint32_t address, infra::Function<void()> onDone) override; | ||
virtual void EraseSectors(uint32_t beginIndex, uint32_t endIndex, infra::Function<void()> onDone) override; | ||
|
||
private: | ||
infra::MemoryRange<uint32_t> sectorSizes; | ||
StUartProgrammer& programmer; | ||
|
||
infra::Function<void()> onDone; | ||
infra::ConstByteRange writeTail; | ||
infra::ByteRange readTail; | ||
uint32_t tailAddress = 0; | ||
uint32_t beginIndex = 0; | ||
uint32_t endIndex = 0; | ||
}; | ||
} | ||
|
||
#endif |
Oops, something went wrong.