Skip to content

Commit 9591db7

Browse files
fix: the HSI48 oscillator is disabled by the random number generator (#606)
1 parent c58fbcf commit 9591db7

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

hal_st/synchronous_stm32fxxx/SynchronousHardwareSemaphoreStm.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace hal
1717
{
1818
HSEM->C1IER |= 1 << static_cast<uint32_t>(semaphore);
1919

20-
while (HSEM->RLR[static_cast<uint32_t>(semaphore)] != (HSEM_R_LOCK | HSEM_CR_COREID_CURRENT))
20+
while (!IsLockedByCurrentCore(semaphore))
2121
{}
2222
}
2323

@@ -29,6 +29,11 @@ namespace hal
2929
HSEM->C1IER &= ~mask;
3030
}
3131

32+
bool SynchronousHardwareSemaphoreMasterStm::IsLockedByCurrentCore(hal::Semaphore semaphore) const
33+
{
34+
return HSEM->RLR[static_cast<uint32_t>(semaphore)] == HSEM_R_LOCK | HSEM_CR_COREID_CURRENT;
35+
}
36+
3237
SynchronousHardwareSemaphoreStm::SynchronousHardwareSemaphoreStm(SynchronousHardwareSemaphoreMasterStm& synchronousHardwareSemaphoreMaster, Semaphore semaphore)
3338
: synchronousHardwareSemaphoreMaster(synchronousHardwareSemaphoreMaster)
3439
, semaphore(semaphore)

hal_st/synchronous_stm32fxxx/SynchronousHardwareSemaphoreStm.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace hal
2727

2828
void WaitLock(hal::Semaphore semaphore) const;
2929
void Release(hal::Semaphore semaphore) const;
30+
bool IsLockedByCurrentCore(hal::Semaphore semaphore) const;
3031
};
3132

3233
class SynchronousHardwareSemaphoreStm
Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "hal_st/synchronous_stm32fxxx/SynchronousSynchronizedRandomDataGeneratorStm.hpp"
2+
#include "infra/util/ReallyAssert.hpp"
23
#include "stm32wbxx.h"
34

45
namespace hal
@@ -11,15 +12,35 @@ namespace hal
1112
void SynchronousSynchronizedRandomDataGeneratorStm::GenerateRandomData(infra::ByteRange result)
1213
{
1314
auto semaphore = hal::SynchronousHardwareSemaphoreStm(synchronousHardwareSemaphoreMasterStm, hal::Semaphore::randomNumberGenerator);
14-
15-
LL_RCC_HSI48_Enable();
16-
while (!LL_RCC_HSI48_IsReady())
17-
{}
15+
Hsi48Enabler hsi48Enabler(synchronousHardwareSemaphoreMasterStm);
1816

1917
hwRngCreator.Emplace();
2018
hwRngCreator->GenerateRandomData(result);
2119
hwRngCreator.Destroy();
20+
}
21+
22+
SynchronousSynchronizedRandomDataGeneratorStm::Hsi48Enabler::Hsi48Enabler(hal::SynchronousHardwareSemaphoreMasterStm& synchronousHardwareSemaphoreMasterStm)
23+
{
24+
bool clockConfigurationLocked = synchronousHardwareSemaphoreMasterStm.IsLockedByCurrentCore(hal::Semaphore::recoveryAndIndependentClockConfiguration);
2225

23-
LL_RCC_HSI48_Disable();
26+
if (clockConfigurationLocked)
27+
{
28+
// HSI48 should already be enabled when the clock configuration is locked. Don't manage its lifecycle.
29+
really_assert(LL_RCC_HSI48_IsReady());
30+
}
31+
else
32+
{
33+
LL_RCC_HSI48_Enable();
34+
while (!LL_RCC_HSI48_IsReady())
35+
{}
36+
37+
disableHsi48OnFinalization = true;
38+
}
39+
}
40+
41+
SynchronousSynchronizedRandomDataGeneratorStm::Hsi48Enabler::~Hsi48Enabler()
42+
{
43+
if (disableHsi48OnFinalization)
44+
LL_RCC_HSI48_Disable();
2445
}
2546
}

hal_st/synchronous_stm32fxxx/SynchronousSynchronizedRandomDataGeneratorStm.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ namespace hal
1616
void GenerateRandomData(infra::ByteRange result) override;
1717

1818
private:
19+
class Hsi48Enabler
20+
{
21+
public:
22+
Hsi48Enabler(hal::SynchronousHardwareSemaphoreMasterStm& synchronousHardwareSemaphoreMasterStm);
23+
~Hsi48Enabler();
24+
25+
private:
26+
bool disableHsi48OnFinalization = false;
27+
};
28+
1929
infra::DelayedProxyCreator<hal::SynchronousRandomDataGenerator, void()> hwRngCreator;
2030
hal::SynchronousHardwareSemaphoreMasterStm& synchronousHardwareSemaphoreMasterStm;
2131
};

0 commit comments

Comments
 (0)