Skip to content

Commit 351d7b9

Browse files
KasslimKasslim
authored andcommitted
SPDAccessor.cpp: replace magic numbers with defines
1 parent f948d6e commit 351d7b9

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

SPDAccessor/SPDAccessor.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@
2121

2222
using namespace std::chrono_literals;
2323

24+
25+
// Sources for define values:
26+
// - https://en.wikipedia.org/wiki/Serial_presence_detect
27+
// - JEDEC DDR5 Serial Presence Detect (SPD): Table of contents
28+
29+
#define BASIC_MEMORY_TYPE_ADDR (0x02)
30+
31+
#define DDR4_JEDEC_ID_ADDR (0x140)
32+
#define DDR4_MANUF_SPECIFIC_START (0x161)
33+
#define DDR4_MANUF_SPECIFIC_END (0x17D)
34+
#define DDR4_MANUF_SPECIFIC_LEN (DDR4_MANUF_SPECIFIC_END - DDR4_MANUF_SPECIFIC_START + 1)
35+
36+
#define DDR5_JEDEC_ID_ADDR (0x200)
37+
#define DDR5_MANUF_SPECIFIC_START (0x22B)
38+
#define DDR5_MANUF_SPECIFIC_END (0x27F)
39+
#define DDR5_MANUF_SPECIFIC_LEN (DDR5_MANUF_SPECIFIC_END - DDR5_MANUF_SPECIFIC_START + 1)
40+
2441
const char *spd_memory_type_name[] =
2542
{
2643
"Reserved",
@@ -103,23 +120,24 @@ DDR4Accessor::~DDR4Accessor()
103120

104121
SPDMemoryType DDR4Accessor::memory_type()
105122
{
106-
return((SPDMemoryType)(this->at(0x02)));
123+
return((SPDMemoryType)(this->at(BASIC_MEMORY_TYPE_ADDR)));
107124
}
108125

109126
uint16_t DDR4Accessor::jedec_id()
110127
{
111-
return((this->at(0x140) << 8) + (this->at(0x141) & 0x7f) - 1);
128+
return((this->at(DDR4_JEDEC_ID_ADDR) << 8) + (this->at(DDR4_JEDEC_ID_ADDR+1) & 0x7f) - 1);
112129
}
113130

114131
uint8_t DDR4Accessor::manufacturer_data(uint16_t index)
115132
{
116-
if(index > 28)
133+
if(index > DDR4_MANUF_SPECIFIC_LEN-1)
117134
{
118135
return 0;
119136
}
120-
return this->at(0x161 + index);
137+
return this->at(DDR4_MANUF_SPECIFIC_START + index);
121138
}
122139

140+
123141
DDR5Accessor::DDR5Accessor(i2c_smbus_interface *bus, uint8_t spd_addr)
124142
: SPDAccessor(bus, spd_addr)
125143
{
@@ -131,19 +149,19 @@ DDR5Accessor::~DDR5Accessor()
131149

132150
SPDMemoryType DDR5Accessor::memory_type()
133151
{
134-
return((SPDMemoryType)(this->at(0x02)));
152+
return((SPDMemoryType)(this->at(BASIC_MEMORY_TYPE_ADDR)));
135153
}
136154

137155
uint16_t DDR5Accessor::jedec_id()
138156
{
139-
return((this->at(0x200) << 8) + (this->at(0x201) & 0x7f) - 1);
157+
return((this->at(DDR5_JEDEC_ID_ADDR) << 8) + (this->at(DDR5_JEDEC_ID_ADDR+1) & 0x7f) - 1);
140158
}
141159

142160
uint8_t DDR5Accessor::manufacturer_data(uint16_t index)
143161
{
144-
if(index > 84)
162+
if(index > DDR5_MANUF_SPECIFIC_LEN-1)
145163
{
146164
return 0;
147165
}
148-
return this->at(0x22B + index);
166+
return this->at(DDR5_MANUF_SPECIFIC_START + index);
149167
}

0 commit comments

Comments
 (0)