Skip to content

Commit

Permalink
feat: add hw semaphore sync for rng on stm32wb (#286)
Browse files Browse the repository at this point in the history
* 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
andjordan and richardapeters authored Mar 19, 2024
1 parent 5a8e6f5 commit 3e4a41d
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
4 changes: 4 additions & 0 deletions hal_st/synchronous_stm32fxxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ target_sources(hal_st.synchronous_stm32fxxx PRIVATE
SynchronousGpioStm.hpp
SynchronousFlashInternalStm.cpp
SynchronousFlashInternalStm.hpp
$<$<STREQUAL:${TARGET_MCU_FAMILY},stm32wbxx>:SynchronousHardwareSemaphoreStm.cpp>
$<$<STREQUAL:${TARGET_MCU_FAMILY},stm32wbxx>:SynchronousHardwareSemaphoreStm.hpp>
SynchronousRandomDataGeneratorStm.cpp
SynchronousRandomDataGeneratorStm.hpp
SynchronousSpiMasterStm.cpp
SynchronousSpiMasterStm.hpp
$<$<STREQUAL:${TARGET_MCU_FAMILY},stm32wbxx>:SynchronousSynchronizedRandomDataGeneratorStm.cpp>
$<$<STREQUAL:${TARGET_MCU_FAMILY},stm32wbxx>:SynchronousSynchronizedRandomDataGeneratorStm.hpp>
$<$<NOT:$<STREQUAL:${TARGET_MCU_FAMILY},stm32g0xx>>:SynchronousUartStm.cpp>
$<$<NOT:$<STREQUAL:${TARGET_MCU_FAMILY},stm32g0xx>>:SynchronousUartStm.hpp>
$<$<STREQUAL:${TARGET_MCU_FAMILY},stm32f7xx>:SynchronousQuadSpiStm.cpp>
Expand Down
43 changes: 43 additions & 0 deletions hal_st/synchronous_stm32fxxx/SynchronousHardwareSemaphoreStm.cpp
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 hal_st/synchronous_stm32fxxx/SynchronousHardwareSemaphoreStm.hpp
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
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();
}
}
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

0 comments on commit 3e4a41d

Please sign in to comment.