-
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 hw semaphore sync for rng on stm32wb (#286)
* feat: add hw semaphore sync for rng on stm32wb * Apply suggestions from code review Co-authored-by: Richard Peters <[email protected]> --------- Co-authored-by: Richard Peters <[email protected]>
- Loading branch information
1 parent
5a8e6f5
commit 3e4a41d
Showing
5 changed files
with
143 additions
and
0 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
43 changes: 43 additions & 0 deletions
43
hal_st/synchronous_stm32fxxx/SynchronousHardwareSemaphoreStm.cpp
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,43 @@ | ||
#include "hal_st/synchronous_stm32fxxx/SynchronousHardwareSemaphoreStm.hpp" | ||
#include "stm32wbxx.h" | ||
|
||
namespace hal | ||
{ | ||
SynchronousHardwareSemaphoreMasterStm::SynchronousHardwareSemaphoreMasterStm() | ||
{ | ||
__HAL_RCC_HSEM_CLK_ENABLE(); | ||
} | ||
|
||
SynchronousHardwareSemaphoreMasterStm::~SynchronousHardwareSemaphoreMasterStm() | ||
{ | ||
__HAL_RCC_HSEM_CLK_DISABLE(); | ||
} | ||
|
||
void SynchronousHardwareSemaphoreMasterStm::WaitLock(hal::Semaphore semaphore) const | ||
{ | ||
HSEM->C1IER |= 1 << static_cast<uint32_t>(semaphore); | ||
|
||
while (HSEM->RLR[static_cast<uint32_t>(semaphore)] != (HSEM_R_LOCK | HSEM_CR_COREID_CURRENT)) | ||
{} | ||
} | ||
|
||
void SynchronousHardwareSemaphoreMasterStm::Release(hal::Semaphore semaphore) const | ||
{ | ||
uint32_t mask = 1 << static_cast<uint32_t>(semaphore); | ||
HSEM->C1ICR = mask; | ||
HSEM->R[static_cast<uint32_t>(semaphore)] = HSEM_CR_COREID_CURRENT; | ||
HSEM->C1IER &= ~mask; | ||
} | ||
|
||
SynchronousHardwareSemaphoreStm::SynchronousHardwareSemaphoreStm(SynchronousHardwareSemaphoreMasterStm& synchronousHardwareSemaphoreMaster, Semaphore semaphore) | ||
: synchronousHardwareSemaphoreMaster(synchronousHardwareSemaphoreMaster) | ||
, semaphore(semaphore) | ||
{ | ||
synchronousHardwareSemaphoreMaster.WaitLock(semaphore); | ||
} | ||
|
||
SynchronousHardwareSemaphoreStm::~SynchronousHardwareSemaphoreStm() | ||
{ | ||
synchronousHardwareSemaphoreMaster.Release(semaphore); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
hal_st/synchronous_stm32fxxx/SynchronousHardwareSemaphoreStm.hpp
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,47 @@ | ||
#ifndef SYNCHRONOUS_HAL_SYNCHRONOUS_HARDWARE_SEMAPHORE_STM_HPP | ||
#define SYNCHRONOUS_HAL_SYNCHRONOUS_HARDWARE_SEMAPHORE_STM_HPP | ||
|
||
#include <stdint.h> | ||
|
||
namespace hal | ||
{ | ||
enum class Semaphore : uint32_t | ||
{ | ||
randomNumberGenerator = 0, | ||
publicKeyAccelerator, | ||
flashWriteErase, | ||
resetAndClockControl, | ||
clockControlForStopMode, | ||
recoveryAndIndependentClockConfiguration, | ||
preventCpu2FromWritingAndErasingToFlash, | ||
preventCpu1FromWritingAndErasingToFlash, | ||
preventCpu2FromUpdatingBlePersistentDataInSharedRam, | ||
preventCpu2FromUpdatingThreadPersistentDataInSharedRam, | ||
}; | ||
|
||
class SynchronousHardwareSemaphoreMasterStm | ||
{ | ||
public: | ||
SynchronousHardwareSemaphoreMasterStm(); | ||
~SynchronousHardwareSemaphoreMasterStm(); | ||
|
||
void WaitLock(hal::Semaphore semaphore) const; | ||
void Release(hal::Semaphore semaphore) const; | ||
}; | ||
|
||
class SynchronousHardwareSemaphoreStm | ||
{ | ||
public: | ||
explicit SynchronousHardwareSemaphoreStm(SynchronousHardwareSemaphoreMasterStm& synchronousHardwareSemaphoreMaster, hal::Semaphore semaphore); | ||
~SynchronousHardwareSemaphoreStm(); | ||
|
||
SynchronousHardwareSemaphoreStm(const SynchronousHardwareSemaphoreStm& other) = delete; | ||
SynchronousHardwareSemaphoreStm& operator=(const SynchronousHardwareSemaphoreStm& other) = delete; | ||
|
||
private: | ||
SynchronousHardwareSemaphoreMasterStm& synchronousHardwareSemaphoreMaster; | ||
hal::Semaphore semaphore; | ||
}; | ||
} | ||
|
||
#endif |
25 changes: 25 additions & 0 deletions
25
hal_st/synchronous_stm32fxxx/SynchronousSynchronizedRandomDataGeneratorStm.cpp
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,25 @@ | ||
#include "hal_st/synchronous_stm32fxxx/SynchronousSynchronizedRandomDataGeneratorStm.hpp" | ||
#include "stm32wbxx.h" | ||
|
||
namespace hal | ||
{ | ||
SynchronousSynchronizedRandomDataGeneratorStm::SynchronousSynchronizedRandomDataGeneratorStm(infra::CreatorBase<hal::SynchronousRandomDataGenerator, void()>& hwRngCreator, hal::SynchronousHardwareSemaphoreMasterStm& synchronousHardwareSemaphoreMasterStm) | ||
: hwRngCreator(hwRngCreator) | ||
, synchronousHardwareSemaphoreMasterStm(synchronousHardwareSemaphoreMasterStm) | ||
{} | ||
|
||
void SynchronousSynchronizedRandomDataGeneratorStm::GenerateRandomData(infra::ByteRange result) | ||
{ | ||
auto semaphore = hal::SynchronousHardwareSemaphoreStm(synchronousHardwareSemaphoreMasterStm, hal::Semaphore::randomNumberGenerator); | ||
|
||
LL_RCC_HSI48_Enable(); | ||
while (!LL_RCC_HSI48_IsReady()) | ||
{} | ||
|
||
hwRngCreator.Emplace(); | ||
hwRngCreator->GenerateRandomData(result); | ||
hwRngCreator.Destroy(); | ||
|
||
LL_RCC_HSI48_Disable(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
hal_st/synchronous_stm32fxxx/SynchronousSynchronizedRandomDataGeneratorStm.hpp
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,24 @@ | ||
#ifndef SYNCHRONOUS_HAL_SYNCHRONOUS_SYNCHRONIZED_RANDOM_DATA_GENERATOR_HPP | ||
#define SYNCHRONOUS_HAL_SYNCHRONOUS_SYNCHRONIZED_RANDOM_DATA_GENERATOR_HPP | ||
|
||
#include "hal/synchronous_interfaces/SynchronousRandomDataGenerator.hpp" | ||
#include "hal_st/synchronous_stm32fxxx/SynchronousHardwareSemaphoreStm.hpp" | ||
#include "infra/util/ProxyCreator.hpp" | ||
|
||
namespace hal | ||
{ | ||
class SynchronousSynchronizedRandomDataGeneratorStm | ||
: public hal::SynchronousRandomDataGenerator | ||
{ | ||
public: | ||
SynchronousSynchronizedRandomDataGeneratorStm(infra::CreatorBase<hal::SynchronousRandomDataGenerator, void()>& hwRngCreator, hal::SynchronousHardwareSemaphoreMasterStm& synchronousHardwareSemaphoreMasterStm); | ||
|
||
void GenerateRandomData(infra::ByteRange result) override; | ||
|
||
private: | ||
infra::DelayedProxyCreator<hal::SynchronousRandomDataGenerator, void()> hwRngCreator; | ||
hal::SynchronousHardwareSemaphoreMasterStm& synchronousHardwareSemaphoreMasterStm; | ||
}; | ||
} | ||
|
||
#endif |