-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc_noise.h
51 lines (40 loc) · 904 Bytes
/
crc_noise.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#define NOISE_POLY 0x1D872B41
namespace daisysp
{
class crc_noise
{
public:
crc_noise() {}
~crc_noise() {}
void Init()
{
__HAL_RCC_CRC_CLK_ENABLE();
hrng.Instance = RNG;
__RNG_CLK_ENABLE();
__HAL_RNG_ENABLE(&hrng);
HAL_RNG_GenerateRandomNumber(&hrng, &seed_);
CRC->POL = NOISE_POLY;
CRC->DR = seed_;
}
float Process(uint8_t i)
{
CRC->DR = i;
rand_ = CRC->DR;
// take the lower 16 bits and convert to a float
frand_ = (2.0 * ((float(rand_ & 0x0000FFFF) / 65535.0f) - 0.5));
return frand_;
}
float Process()
{
CRC->DR = (uint32_t)frand_;
rand_ = CRC->DR;
// take the lower 16 bits and convert to a float
frand_ = (2.0 * ((float(rand_ & 0x0000FFFF) / 65535.0f) - 0.5));
return frand_;
}
private:
uint32_t rand_, seed_;
float frand_;
RNG_HandleTypeDef hrng;
};
}