-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add support for One-Time Programmable Flash #763
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
Open
Fabian Gottstein (fabiangottstein)
wants to merge
3
commits into
main
Choose a base branch
from
feature/otp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
| #include "hal_st/stm32fxxx/HighCycleAreaOrOtpIrqHandler.hpp" | ||
| #include "infra/util/ReallyAssert.hpp" | ||
| #include DEVICE_HEADER | ||
|
|
||
| namespace hal | ||
| { | ||
| HighCycleAreaOrOtpIrqHandler::HighCycleAreaOrOtpIrqHandler() | ||
| : nmi{ | ||
| IRQn_Type::NonMaskableInt_IRQn, [] | ||
| { | ||
| // From RM0481 Rev 4: | ||
| // Section 7.3.4 FLASH read operations: | ||
| // If the application reads an OTP data or flash high-cycle data not previously written, a | ||
| // double ECC error is reported and only a word full of set bits is returned (see | ||
| // Section (fixed:) 7.9.10 for details). The read data (in 16 bits) is stored in FLASH_ECCDR | ||
| // register, so that the user can identify if the double ECC error is due to a virgin data or a | ||
| // real ECC error. | ||
| really_assert(__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD) && READ_REG(FLASH->ECCDR) == 0xFFFF); | ||
|
|
||
| __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ECCD); | ||
| } | ||
| } | ||
| {} | ||
| } |
This file contains hidden or 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,22 @@ | ||
| #ifndef HALST_HIGH_CYCLE_AREA_OR_OTP_IRQ_HANDLER_HPP | ||
| #define HALST_HIGH_CYCLE_AREA_OR_OTP_IRQ_HANDLER_HPP | ||
|
|
||
| #include "hal_st/cortex/InterruptCortex.hpp" | ||
|
|
||
| namespace hal | ||
| { | ||
| // This class is used as a base class for both FlashInternalHighCycleAreaStm::WithIrqHandler and OneTimeProgrammableFlashStm::WithIrqHandler, | ||
| // as they share the same NMI handler which is responsible for clearing the flash end of operation flag. | ||
| class HighCycleAreaOrOtpIrqHandler | ||
| { | ||
| public: | ||
| HighCycleAreaOrOtpIrqHandler(); | ||
| HighCycleAreaOrOtpIrqHandler(const HighCycleAreaOrOtpIrqHandler&) = delete; | ||
| HighCycleAreaOrOtpIrqHandler& operator=(const HighCycleAreaOrOtpIrqHandler&) = delete; | ||
|
|
||
| private: | ||
| hal::ImmediateInterruptHandler nmi; | ||
| }; | ||
| } | ||
|
|
||
| #endif | ||
This file contains hidden or 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,120 @@ | ||
| #include "hal_st/stm32fxxx/OneTimeProgrammableFlashStm.hpp" | ||
| #include "hal_st/stm32fxxx/FlashInternalStmDetail.hpp" | ||
| #include "infra/event/EventDispatcher.hpp" | ||
| #include "infra/util/LogAndAbort.hpp" | ||
| #include "infra/util/ReallyAssert.hpp" | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <cstdlib> | ||
| #include DEVICE_HEADER | ||
|
|
||
| namespace | ||
| { | ||
| using ConstHalfWordRange = hal::OneTimeProgrammableFlashWorker::ConstHalfWordRange; | ||
|
|
||
| template<uint32_t address> | ||
| constexpr auto ptr() | ||
| { | ||
| return reinterpret_cast<const uint16_t*>(address); | ||
| } | ||
|
|
||
| constexpr size_t halfWordSize = sizeof(uint16_t); | ||
| constexpr uint32_t sectorNumber{ 32 }; | ||
| constexpr uint32_t sectorSize{ FLASH_OTP_SIZE / sectorNumber }; | ||
| const ConstHalfWordRange OtpRange{ ptr<FLASH_OTP_BASE>(), ptr<FLASH_OTP_BASE + FLASH_OTP_SIZE>() }; | ||
|
|
||
| void Copy(const uint16_t* begin, const uint16_t* end, uint16_t* destination) | ||
| { | ||
| for (; begin != end; ++destination, ++begin) | ||
| *destination = *begin; | ||
| } | ||
| } | ||
|
|
||
| namespace hal | ||
| { | ||
| OneTimeProgrammableFlashWorker::OneTimeProgrammableFlashWorker() | ||
| : flashMemory{ OtpRange } | ||
| {} | ||
|
|
||
| void OneTimeProgrammableFlashWorker::ReadBuffer(infra::ByteRange buffer, uint32_t address) | ||
| { | ||
| really_assert(buffer.size() % sizeof(uint16_t) == 0); | ||
| // address is byte-based, addressAdjusted is half-word-based | ||
| auto addressAdjusted = address / halfWordSize; | ||
| auto destination = infra::ReinterpretCastMemoryRange<uint16_t>(buffer); | ||
| // explicitely copy data uint16_t aligned and prevent gcc from (falsely) optimizing to memmove which will reinterpret as arrays of uint8_t | ||
| Copy(flashMemory.begin() + addressAdjusted, flashMemory.begin() + addressAdjusted + destination.size(), destination.begin()); | ||
| } | ||
|
|
||
| void OneTimeProgrammableFlashWorker::WriteBuffer(infra::ConstByteRange buffer, uint32_t address) | ||
| { | ||
| AssertDestinationIsUntouched(buffer, address); | ||
|
|
||
| HAL_FLASH_Unlock(); | ||
|
|
||
| detail::AlignedWriteBuffer<uint16_t, FLASH_TYPEPROGRAM_HALFWORD_OTP, true>(buffer, address, reinterpret_cast<uint32_t>(flashMemory.begin())); | ||
|
|
||
| HAL_FLASH_Lock(); | ||
| } | ||
|
|
||
| [[noreturn]] void OneTimeProgrammableFlashWorker::EraseSectors([[maybe_unused]] uint32_t beginIndex, [[maybe_unused]] uint32_t endIndex) | ||
| { | ||
| // OTP cannot be erased | ||
| LOG_AND_ABORT("Not supported"); | ||
| } | ||
|
|
||
| uint32_t OneTimeProgrammableFlashWorker::SectorNumber() | ||
| { | ||
| return sectorNumber; | ||
| } | ||
|
|
||
| uint32_t OneTimeProgrammableFlashWorker::SectorSize() | ||
| { | ||
| return sectorSize; | ||
| } | ||
|
|
||
| infra::MemoryRange<volatile uint16_t> OneTimeProgrammableFlashWorker::Range() | ||
| { | ||
| auto* base = reinterpret_cast<volatile uint16_t*>(FLASH_OTP_BASE); | ||
| auto* end = reinterpret_cast<volatile uint16_t*>(FLASH_OTP_BASE + FLASH_OTP_SIZE); | ||
| return infra::MakeRange(base, end); | ||
| } | ||
|
|
||
| void OneTimeProgrammableFlashWorker::AssertDestinationIsUntouched(infra::ConstByteRange buffer, uint32_t address) | ||
| { | ||
| auto writeBuffer = infra::ReinterpretCastMemoryRange<const uint16_t>(buffer); | ||
| auto otpDestination = infra::Head(infra::DiscardHead(Range(), address / halfWordSize), writeBuffer.size()); | ||
| // From RM0481 Rev 4: | ||
| // Section 7.3.9 OTP and RO memory access: | ||
| // Overwriting an already programmed 16-bit half-word can lead to data and ECC errors, | ||
| // and is therefore not supported. | ||
| for (size_t offset = 0; const auto& halfWord : otpDestination) | ||
| { | ||
| really_assert(halfWord == 0xFFFF || halfWord == writeBuffer[offset]); | ||
| ++offset; | ||
| } | ||
| } | ||
|
|
||
| OneTimeProgrammableFlashStm::OneTimeProgrammableFlashStm() | ||
| : FlashHomogeneous{ OneTimeProgrammableFlashWorker::SectorNumber(), OneTimeProgrammableFlashWorker::SectorSize() } | ||
| , OneTimeProgrammableFlashWorker{} | ||
| {} | ||
|
|
||
| void OneTimeProgrammableFlashStm::ReadBuffer(infra::ByteRange buffer, uint32_t address, infra::Function<void()> onDone) | ||
| { | ||
| OneTimeProgrammableFlashWorker::ReadBuffer(buffer, address); | ||
| infra::EventDispatcher::Instance().Schedule(onDone); | ||
| } | ||
|
|
||
| void OneTimeProgrammableFlashStm::WriteBuffer(infra::ConstByteRange buffer, uint32_t address, infra::Function<void()> onDone) | ||
| { | ||
| OneTimeProgrammableFlashWorker::WriteBuffer(buffer, address); | ||
| infra::EventDispatcher::Instance().Schedule(onDone); | ||
| } | ||
|
|
||
| void OneTimeProgrammableFlashStm::EraseSectors(uint32_t beginIndex, uint32_t endIndex, infra::Function<void()> onDone) | ||
| { | ||
| OneTimeProgrammableFlashWorker::EraseSectors(beginIndex, endIndex); | ||
| infra::EventDispatcher::Instance().Schedule(onDone); | ||
| } | ||
| } |
This file contains hidden or 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,57 @@ | ||
| #ifndef HALST_ONE_TIME_PROGRAMMABLE_FLASH_STM_HPP | ||
| #define HALST_ONE_TIME_PROGRAMMABLE_FLASH_STM_HPP | ||
|
|
||
| #include "hal/interfaces/FlashHomogeneous.hpp" | ||
| #include "hal_st/stm32fxxx/HighCycleAreaOrOtpIrqHandler.hpp" | ||
| #include <cstdint> | ||
|
|
||
| namespace hal | ||
| { | ||
| // This class is used to implement both the synchronous and asynchronous version of the OTP flash, | ||
| // as the underlying implementation is the same except for scheduling the onDone callback. | ||
| class OneTimeProgrammableFlashWorker | ||
| { | ||
| public: | ||
| using ConstHalfWordRange = infra::MemoryRange<const uint16_t>; | ||
|
|
||
| class WithIrqHandler; | ||
|
|
||
| OneTimeProgrammableFlashWorker(); | ||
|
|
||
| void WriteBuffer(infra::ConstByteRange buffer, uint32_t address); | ||
| void ReadBuffer(infra::ByteRange buffer, uint32_t address); | ||
| [[noreturn]] void EraseSectors(uint32_t beginIndex, uint32_t endIndex); | ||
|
|
||
| static uint32_t SectorNumber(); | ||
| static uint32_t SectorSize(); | ||
| static infra::MemoryRange<volatile uint16_t> Range(); | ||
|
|
||
| private: | ||
| ConstHalfWordRange flashMemory; | ||
| void AssertDestinationIsUntouched(infra::ConstByteRange buffer, uint32_t address); | ||
| }; | ||
|
|
||
| class OneTimeProgrammableFlashStm | ||
| : public FlashHomogeneous | ||
| , private OneTimeProgrammableFlashWorker | ||
| { | ||
| public: | ||
| class WithIrqHandler; | ||
|
|
||
| OneTimeProgrammableFlashStm(); | ||
|
|
||
| // implementation of Flash interface | ||
| void WriteBuffer(infra::ConstByteRange buffer, uint32_t address, infra::Function<void()> onDone) override; | ||
| void ReadBuffer(infra::ByteRange buffer, uint32_t address, infra::Function<void()> onDone) override; | ||
| void EraseSectors(uint32_t beginIndex, uint32_t endIndex, infra::Function<void()> onDone) override; | ||
|
|
||
| using OneTimeProgrammableFlashWorker::Range; | ||
| }; | ||
|
|
||
| class OneTimeProgrammableFlashStm::WithIrqHandler | ||
| : public OneTimeProgrammableFlashStm | ||
| , private HighCycleAreaOrOtpIrqHandler | ||
| {}; | ||
| } | ||
|
|
||
| #endif |
This file contains hidden or 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
24 changes: 24 additions & 0 deletions
24
hal_st/synchronous_stm32fxxx/SynchronousOneTimeProgrammableFlashStm.cpp
This file contains hidden or 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,24 @@ | ||
| #include "hal_st/synchronous_stm32fxxx/SynchronousOneTimeProgrammableFlashStm.hpp" | ||
|
|
||
| namespace hal | ||
| { | ||
| SynchronousOneTimeProgrammableFlashStm::SynchronousOneTimeProgrammableFlashStm() | ||
| : SynchronousFlashHomogeneous{ OneTimeProgrammableFlashWorker::SectorNumber(), OneTimeProgrammableFlashWorker::SectorSize() } | ||
| , OneTimeProgrammableFlashWorker{} | ||
| {} | ||
|
|
||
| void SynchronousOneTimeProgrammableFlashStm::ReadBuffer(infra::ByteRange buffer, uint32_t address) | ||
| { | ||
| OneTimeProgrammableFlashWorker::ReadBuffer(buffer, address); | ||
| } | ||
|
|
||
| void SynchronousOneTimeProgrammableFlashStm::WriteBuffer(infra::ConstByteRange buffer, uint32_t address) | ||
| { | ||
| OneTimeProgrammableFlashWorker::WriteBuffer(buffer, address); | ||
| } | ||
|
|
||
| void SynchronousOneTimeProgrammableFlashStm::EraseSectors(uint32_t beginIndex, uint32_t endIndex) | ||
| { | ||
| OneTimeProgrammableFlashWorker::EraseSectors(beginIndex, endIndex); | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
hal_st/synchronous_stm32fxxx/SynchronousOneTimeProgrammableFlashStm.hpp
This file contains hidden or 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,32 @@ | ||
| #ifndef HALST_SYNCHRONOUS_ONE_TIME_PROGRAMMABLE_FLASH_STM_HPP | ||
| #define HALST_SYNCHRONOUS_ONE_TIME_PROGRAMMABLE_FLASH_STM_HPP | ||
|
|
||
| #include "hal/synchronous_interfaces/SynchronousFlashHomogeneous.hpp" | ||
| #include "hal_st/stm32fxxx/OneTimeProgrammableFlashStm.hpp" | ||
|
|
||
| namespace hal | ||
| { | ||
| class SynchronousOneTimeProgrammableFlashStm | ||
| : public SynchronousFlashHomogeneous | ||
| , private OneTimeProgrammableFlashWorker | ||
| { | ||
| public: | ||
| class WithIrqHandler; | ||
|
|
||
| SynchronousOneTimeProgrammableFlashStm(); | ||
|
|
||
| // implementation of SynchronousFlash interface | ||
| void WriteBuffer(infra::ConstByteRange buffer, uint32_t address) override; | ||
| void ReadBuffer(infra::ByteRange buffer, uint32_t address) override; | ||
| void EraseSectors(uint32_t beginIndex, uint32_t endIndex) override; | ||
|
|
||
| using OneTimeProgrammableFlashWorker::Range; | ||
| }; | ||
|
|
||
| class SynchronousOneTimeProgrammableFlashStm::WithIrqHandler | ||
| : public SynchronousOneTimeProgrammableFlashStm | ||
| , private HighCycleAreaOrOtpIrqHandler | ||
| {}; | ||
| } | ||
|
|
||
| #endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.