33
44namespace core ::io {
55
6-
7- SDRAM::SDRAM (uint32_t * memoryAddress, SDRAMPinGroup& pins, const SDRAMInitConfig& initConfig,
8- const SDRAMTimingConfig& timingConfig, const SDRAMDevice& device)
9- : memoryAddress(memoryAddress), pins(pins), initConfig(initConfig), timingConfig(timingConfig), device(device) {}
10-
11- uint32_t SDRAM::getSdramClockFrequency (uint8_t mcuClkPerSdramClk) {
12- return HAL_RCC_GetSysClockFreq () / mcuClkPerSdramClk;
13- }
14-
15- uint8_t SDRAM::getMCUClkPerSdramClk (uint32_t sdClockPeriod) {
16- switch (sdClockPeriod) {
17- default :
18- case FMC_SDRAM_CLOCK_PERIOD_2 :
19- return 2 ;
20- break ;
21- case FMC_SDRAM_CLOCK_PERIOD_3 :
22- return 3 ;
23- break ;
24- }
25-
26- }
27-
28- uint32_t SDRAM::getSdramClockPeriodFS (uint32_t mcuClkPerSdramClk) {
29- return 1000000000UL / (getSdramClockFrequency (mcuClkPerSdramClk) / 1000000 );
30-
31- /* *
32- 1,000,000,000 / (getSdramClockFrequency(mcuClkPerSdramClk) / 1000000)
33- 1,000,000,000 / ((HAL_RCC_GetSysClockFreq() / mcuClkPerSdramClk) / 1000000
34- Assume HAL_RCC_GetSysClockFreq() returns 100,000,000 Hz and mcuClkPerSdramClk = 2
35- 1,000,000,000 / ((100,000,000Hz / 2) / 1000000)
36- 1,000,000,000 / (50,000,000Hz / 1000000)
37- 1,000,000,000 / 50Hz (1 / Hz = s)
38- 20,000,000 fs
39-
40- Why put 1 billion as the numerator? It is the largest base 10 number that would fit in a
41- 32-bit number. This gives maximum resolution for the following
42- function that uses it and keeps the calculation clean.
43-
44- Why divide getSdramClockFrequency(...) by 1 million? This gives us how many megahertz
45- the clock is running at, which is the most imporant part of the clock speed in our case.
46- This makes the denominator of the outer division as small as possible, allowing us to get a high
47- amount of accuracy for femtoseconds per SDRAM clock period
48- */
6+ SDRAM::SDRAM (uint32_t * memoryAddress, SDRAMPinGroup& pins, const SDRAMInitConfig& initConfig,
7+ const SDRAMTimingConfig& timingConfig, const SDRAMDevice& device)
8+ : memoryAddress(memoryAddress), pins(pins), initConfig(initConfig), timingConfig(timingConfig), device(device) {}
9+
10+ uint32_t SDRAM::getSdramClockFrequency (uint8_t mcuClkPerSdramClk) {
11+ return HAL_RCC_GetSysClockFreq () / mcuClkPerSdramClk;
12+ }
13+
14+ uint8_t SDRAM::getMCUClkPerSdramClk (uint32_t sdClockPeriod) {
15+ switch (sdClockPeriod) {
16+ default :
17+ case FMC_SDRAM_CLOCK_PERIOD_2 :
18+ return 2 ;
19+ break ;
20+ case FMC_SDRAM_CLOCK_PERIOD_3 :
21+ return 3 ;
22+ break ;
4923 }
50-
51- uint32_t SDRAM::NSToSdramClockCycles (uint32_t nanoseconds, uint8_t mcuClkPerSdramClk) {
52- return (nanoseconds * 1000000 ) / (getSdramClockPeriodFS (mcuClkPerSdramClk) + 1 );
53- } // Multiplied by 1,000,000 to get from nanoseconds to femtoseconds
24+ }
25+
26+ uint32_t SDRAM::getSdramClockPeriodFS (uint32_t mcuClkPerSdramClk) {
27+ return 1000000000UL / (getSdramClockFrequency (mcuClkPerSdramClk) / 1000000 );
28+
29+ /* *
30+ 1,000,000,000 / (getSdramClockFrequency(mcuClkPerSdramClk) / 1000000)
31+ 1,000,000,000 / ((HAL_RCC_GetSysClockFreq() / mcuClkPerSdramClk) / 1000000
32+ Assume HAL_RCC_GetSysClockFreq() returns 100,000,000 Hz and mcuClkPerSdramClk = 2
33+ 1,000,000,000 / ((100,000,000Hz / 2) / 1000000)
34+ 1,000,000,000 / (50,000,000Hz / 1000000)
35+ 1,000,000,000 / 50Hz (1 / Hz = s)
36+ 20,000,000 fs
37+
38+ Why put 1 billion as the numerator? It is the largest base 10 number that would fit in a
39+ 32-bit number. This gives maximum resolution for the following
40+ function that uses it and keeps the calculation clean.
41+
42+ Why divide getSdramClockFrequency(...) by 1 million? This gives us how many megahertz
43+ the clock is running at, which is the most imporant part of the clock speed in our case.
44+ This makes the denominator of the outer division as small as possible, allowing us to get a high
45+ amount of accuracy for femtoseconds per SDRAM clock period
46+ */
47+ }
48+
49+ uint32_t SDRAM::NSToSdramClockCycles (uint32_t nanoseconds, uint8_t mcuClkPerSdramClk) {
50+ return (nanoseconds * 1000000 ) / (getSdramClockPeriodFS (mcuClkPerSdramClk) + 1 );
51+ } // Multiplied by 1,000,000 to get from nanoseconds to femtoseconds
5452} // namespace core::io
0 commit comments