Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add services/st_util/StUartProgammer #185

Merged
merged 4 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,61 @@ permissions:
contents: read

jobs:
host_build_test_ubuntu:
name: Host Build & Test (ubuntu-latest)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
with:
persist-credentials: false
- uses: hendrikmuhs/ccache-action@6d1841ec156c39a52b1b23a810da917ab98da1f4 # v1.2.10
with:
key: ${{ github.job }}-ubuntu-latest
variant: sccache
- uses: seanmiddleditch/gha-setup-ninja@16b940825621068d98711680b6c3ff92201f8fc0 # v3
- uses: lukka/run-cmake@c2b72aff009141774c5a5fabe74ea46c8c04d9c4 # v10.6
with:
configurePreset: "host"
buildPreset: "host-Debug" #-WithPackage"
#testPreset: "host"
configurePresetAdditionalArgs: "['-DCMAKE_C_COMPILER_LAUNCHER=sccache', '-DCMAKE_CXX_COMPILER_LAUNCHER=sccache']"
#- uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
# with:
# name: halst
# path: build/host/halst-*-Linux.tar.gz
# if-no-files-found: error
#- name: Upload test logs
# if: ${{ failure() }}
# uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
# with:
# name: test-logs
# path: build/host/Testing/Temporary/
host_build_test:
name: Host Build & Test
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, windows-2019, macos-latest]
steps:
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
with:
persist-credentials: false
- uses: hendrikmuhs/ccache-action@6d1841ec156c39a52b1b23a810da917ab98da1f4 # v1.2.10
with:
key: ${{ github.job }}-${{ matrix.os }}
variant: sccache
- uses: lukka/run-cmake@c2b72aff009141774c5a5fabe74ea46c8c04d9c4 # v10.6
with:
configurePreset: "host-single-Debug"
buildPreset: "host-single-Debug"
#testPreset: "host-single-Debug"
configurePresetAdditionalArgs: "['-DCMAKE_C_COMPILER_LAUNCHER=sccache', '-DCMAKE_CXX_COMPILER_LAUNCHER=sccache']"
#- name: Upload test logs
# if: ${{ failure() }}
# uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
# with:
# name: test-logs
# path: build/host/Testing/Temporary/
embedded_build:
name: Embedded Build
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ endif()
add_subdirectory(st)
add_subdirectory(hal_st)
add_subdirectory(hal_st_lwip)
add_subdirectory(services)
add_subdirectory(examples)

emil_clangformat_directories(hal_st DIRECTORIES .)
Expand Down
75 changes: 70 additions & 5 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,34 @@
"name": "defaults",
"hidden": true,
"binaryDir": "${sourceDir}/build/${presetName}",
"generator": "Ninja Multi-Config",
"cacheVariables": {
"CMAKE_CONFIGURATION_TYPES": "Debug;Release;RelWithDebInfo;MinSizeRel",
"HALST_BUILD_EXAMPLES": true
}
},
{
"name": "host",
"displayName": "host",
"description": "Build required tools for host",
"inherits": "defaults"
"displayName": "Configuration for Host Tooling and Tests",
"inherits": "defaults",
"cacheVariables": {
"CMAKE_CONFIGURATION_TYPES": "Debug;Release;RelWithDebInfo;MinSizeRel"
},
"generator": "Ninja Multi-Config"
},
{
"name": "host-single-Debug",
"displayName": "Configuration for Host Tooling and Tests, Single Config Generator, Debug",
"inherits": "defaults",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "host-single-MinSizeRel",
"displayName": "Configuration for Host Tooling and Tests, Single Config Generator, MinSizeRel",
"inherits": "defaults",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "MinSizeRel"
}
},
{
"name": "stm32",
Expand All @@ -30,6 +47,7 @@
"CMAKE_TOOLCHAIN_FILE": ""
},
"cacheVariables": {
"generator": "Ninja Multi-Config",
"EMIL_GENERATE_PACKAGE_CONFIG": false,
"TARGET_MCU_VENDOR": "st"
}
Expand Down Expand Up @@ -123,11 +141,33 @@
}
],
"buildPresets": [
{
"name": "host-Debug",
"configuration": "Debug",
"configurePreset": "host"
},
{
"name": "host-Debug-WithPackage",
"configuration": "Debug",
"configurePreset": "host",
"targets": ["all", "package"]
},
{
"name": "host-RelWithDebInfo",
"configuration": "RelWithDebInfo",
"configurePreset": "host"
},
{
"name": "host-single-Debug",
"configuration": "Debug",
"configurePreset": "host-single-Debug"
},
{
"name": "release-package",
"configuration": "MinSizeRel",
"configurePreset": "host-single-MinSizeRel",
"targets": ["package"]
},
{
"name": "stm32wb55-RelWithDebInfo",
"configuration": "RelWithDebInfo",
Expand Down Expand Up @@ -163,5 +203,30 @@
"configuration": "RelWithDebInfo",
"configurePreset": "stm32f767"
}
],
"testPresets": [
{
"name": "defaults",
"hidden": true,
"output": {
"outputOnFailure": true
},
"execution": {
"noTestsAction": "error",
"stopOnFailure": true
}
},
{
"name": "host",
"configurePreset": "host",
"configuration": "Debug",
"inherits": "defaults"
},
{
"name": "host-single-Debug",
"configurePreset": "host-single-Debug",
"configuration": "Debug",
"inherits": "defaults"
}
]
}
1 change: 1 addition & 0 deletions services/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(st_util)
18 changes: 18 additions & 0 deletions services/st_util/CMakeLists.txt
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
)
93 changes: 93 additions & 0 deletions services/st_util/FlashOnStUartProgrammer.cpp
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));
});
}
}
}
36 changes: 36 additions & 0 deletions services/st_util/FlashOnStUartProgrammer.hpp
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
Loading