Skip to content

Commit 512a827

Browse files
committed
Merge branch 'feature/ChongOscar/fmc-support' of https://github.com/RIT-EVT/EVT-core into feature/ChongOscar/fmc-support
2 parents 43140da + 7900a3f commit 512a827

3 files changed

Lines changed: 59 additions & 60 deletions

File tree

include/core/io/SDRAM.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ class SDRAM {
6868
* Must be initialized before passing into the constructor
6969
*/
7070
struct SDRAMInitConfig {
71-
uint32_t sdBank; // Bank number for the controller (0 or 1 usually)
72-
uint32_t columnBitsNumber; // number of Horizontal Addressing Cells
73-
uint32_t rowBitsNumber; // number of Vertical Addressing Cells
74-
uint32_t memoryDataWidth; // How large the data is: 8, 16, or 32 bit
71+
uint32_t sdBank; // Bank number for the controller (0 or 1 usually)
72+
uint32_t columnBitsNumber; // number of Horizontal Addressing Cells
73+
uint32_t rowBitsNumber; // number of Vertical Addressing Cells
74+
uint32_t memoryDataWidth; // How large the data is: 8, 16, or 32 bit
7575
uint32_t internalBankNumber; // How many layers of columns and rows there are. Usually 1, 2, or 4
7676
uint32_t casLatency; // How many SDRAM CLK Cycles from data fetch received to data available from the output
7777
uint32_t writeProtection; // If you want bank protection on at initialization
78-
uint32_t sdClockPeriod; // How many MCU controller clock cycles per SDRAM CLK Cycle. Usually 1, 2, or 3
79-
uint32_t readBurst; // How many bytes to expect per read request.
80-
uint32_t readPipeDelay; // Number of SDRAM CLK Cycles until data is available from read. Usually 1
78+
uint32_t sdClockPeriod; // How many MCU controller clock cycles per SDRAM CLK Cycle. Usually 1, 2, or 3
79+
uint32_t readBurst; // How many bytes to expect per read request.
80+
uint32_t readPipeDelay; // Number of SDRAM CLK Cycles until data is available from read. Usually 1
8181
};
8282

8383
/**
@@ -86,13 +86,13 @@ class SDRAM {
8686
* Must be initialized before passing into the constructor
8787
*/
8888
struct SDRAMTimingConfig {
89-
uint32_t loadToActiveDelay; // Time to update the load/operation register to SDRAM being read for commands
89+
uint32_t loadToActiveDelay; // Time to update the load/operation register to SDRAM being read for commands
9090
uint32_t exitSelfRefreshDelay; // How long to exit the self-refresh mode
91-
uint32_t selfRefreshTime; // SDRAM CLK Cycles a row will be unavailable for while refreshing
92-
uint32_t rowCycleDelay; // Number of SDRAM CLK Cycles until a new active command can be submitted to a bank
91+
uint32_t selfRefreshTime; // SDRAM CLK Cycles a row will be unavailable for while refreshing
92+
uint32_t rowCycleDelay; // Number of SDRAM CLK Cycles until a new active command can be submitted to a bank
9393
uint32_t writeRecoveryTime; // SDRAM CLK Cycles from write until a precharge can be given
9494
uint32_t readToPrechargeDelay; // SDRAM CLK Cycles from read until a precharge
95-
uint32_t rcdDelay; // SDRAM CLK Cycles from an active to read/write
95+
uint32_t rcdDelay; // SDRAM CLK Cycles from an active to read/write
9696
};
9797

9898
struct SDRAMPinGroup {

src/core/io/SDRAM.cpp

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,50 @@
33

44
namespace 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

src/core/io/platform/f4xx/SDRAMf4xx.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ SDRAM::Status SDRAMf4xx::SendCommand(SDRAMCommand type, SDRAMCommandTarget targe
6464
}
6565

6666
SDRAM::Status SDRAMf4xx::ProgramRefreshRate(uint32_t rowCount, uint32_t refreshTime) {
67-
uint8_t clockPeriodNumber = getMCUClkPerSdramClk(sdram.Init.SDClockPeriod);
67+
uint8_t clockPeriodNumber = getMCUClkPerSdramClk(sdram.Init.SDClockPeriod);
6868
HAL_StatusTypeDef halStatus = FMC_SDRAM_ProgramRefreshRate(
69-
this->sdramDevice, (((refreshTime * 1000) / rowCount) * (getSdramClockFrequency(clockPeriodNumber) / 1000000)) - 20);
69+
this->sdramDevice,
70+
(((refreshTime * 1000) / rowCount) * (getSdramClockFrequency(clockPeriodNumber) / 1000000)) - 20);
7071

7172
return SDRAM::HALStatusToSDRAMStatus(halStatus);
7273
}

0 commit comments

Comments
 (0)