Skip to content

Commit

Permalink
integrated with rpi5
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Kuker committed Jun 5, 2024
1 parent a46957c commit 7679f80
Show file tree
Hide file tree
Showing 5 changed files with 409 additions and 72 deletions.
46 changes: 43 additions & 3 deletions cpp/hal/rpi_revision_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ Rpi_Revision_Code::Rpi_Revision_Code(const string &cpuinfo_path) {
pclose);

if (!pipe) {
printf("Unable to parse the /proc/cpuinfo. Are you running as root?");
spdlog::error(
"Unable to parse the /proc/cpuinfo. Are you running as root?");
SetValid(false);
} else {
spdlog::debug("Found cpuinfo. Parsing...");
SetValid(true);
while (fgets(buffer.data(), static_cast<int>(buffer.size()), pipe.get()) !=
nullptr) {
Expand All @@ -58,6 +58,8 @@ Rpi_Revision_Code::Rpi_Revision_Code(const string &cpuinfo_path) {
spdlog::warn("The revision code is too short. It may be padded.");
SetValid(false);
}
spdlog::debug("cpuinfo data: <" + result + ">");

rpi_revcode = strtol(result.c_str(), NULL, 16);

if (rpi_revcode == 0xFFFFFFFF) {
Expand Down Expand Up @@ -88,13 +90,51 @@ Rpi_Revision_Code::Rpi_Revision_Code(const string &cpuinfo_path) {
}
if (!IsValid()) {
string hex_rev_code = fmt::format("{:08X}", rpi_revcode);
spdlog::warn("Raspberry Pi Revision code is: {} result code: {}",
spdlog::error("Invalid cpuinfo detected!! Raspberry Pi Revision code is: {} result code: {}",
hex_rev_code.c_str(), result.c_str());
}
else{
spdlog::info("Detected " + TypeStr() + " " + RevisionStr() + " " + MemorySizeStr() + " (" + ProcessorStr() + ")");
}
}
}

uint32_t Rpi_Revision_Code::extract_bits(int start_bit, int size) {
unsigned mask = ((1 << size) - 1) << start_bit;
return (rpi_revcode & mask) >> start_bit;
}
}

std::string Rpi_Revision_Code::RevisionStr(){
return "1." + std::to_string(Revision());
}
std::string Rpi_Revision_Code::TypeStr(){
if (valid_rpi_version_type(Type())){
return rpi_version_type_string[(uint32_t)Type()];
}
else{
return "<<Invalid Rpi Version>>";
}

}
std::string Rpi_Revision_Code::ProcessorStr(){
if(valid_cpu_type(Processor())){
return cpu_type_string[(int)Processor()];
}else{
return "<<Invalid CPU Type>>";
}

}
std::string Rpi_Revision_Code::ManufacturerStr(){
if(valid_manufacturer_type(Manufacturer())){
return manufacturer_type_string[(int)Manufacturer()];
}else{
return "<<Invalid Manufacturer>>";
}
}
std::string Rpi_Revision_Code::MemorySizeStr(){
if(valid_memory_size_type(MemorySize())){
return memory_size_type_string[(int)MemorySize()];
}else{
return "<<Invalid Memory Size>>";
}
}
119 changes: 97 additions & 22 deletions cpp/hal/rpi_revision_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <stdio.h>
#include <string>
#include <type_traits>
#include <vector>

using namespace std;

Expand All @@ -31,9 +32,11 @@ using namespace std;
//
//===========================================================================

class Rpi_Revision_Code {
class Rpi_Revision_Code
{
public:
enum class memory_size_type : uint8_t {
enum class memory_size_type : uint8_t
{
MEM_256MB = 0,
MEM_512MB = 1,
MEM_1GB = 2,
Expand All @@ -42,11 +45,25 @@ class Rpi_Revision_Code {
MEM_8GB = 5,
MEM_INVALID = 0x7,
};
bool valid_memory_size_type(memory_size_type value) {

private:
const vector<std::string> memory_size_type_string = {
"MEM_256MB",
"MEM_512MB",
"MEM_1GB",
"MEM_2GB",
"MEM_4GB",
"MEM_8GB",
};
bool valid_memory_size_type(memory_size_type value)
{
return (value >= memory_size_type::MEM_256MB &&
value <= memory_size_type::MEM_8GB);
}
enum class manufacturer_type : uint8_t {

public:
enum class manufacturer_type : uint8_t
{
SonyUK = 0,
Egoman = 1,
Embest2 = 2,
Expand All @@ -56,34 +73,50 @@ class Rpi_Revision_Code {
MANUFACTURER_INVALID = 0xF,
};

bool valid_manufacturer_type(manufacturer_type value) {
private:
const vector<std::string> manufacturer_type_string = {
"SonyUK", "Egoman", "Embest2", "SonyJapan", "Embest4", "Stadium"};

bool valid_manufacturer_type(manufacturer_type value)
{
return (value >= manufacturer_type::SonyUK &&
value <= manufacturer_type::Stadium);
}

enum class cpu_type : uint8_t {
public:
enum class cpu_type : uint8_t
{
BCM2835 = 0,
BCM2836 = 1,
BCM2837 = 2,
BCM2711 = 3,
BCM2712 = 4,
CPU_TYPE_INVALID = 0xF,
};
bool valid_cpu_type(cpu_type value) {

private:
const vector<std::string> cpu_type_string = {
"BCM2835", "BCM2836", "BCM2837", "BCM2711", "BCM2712"};
bool valid_cpu_type(cpu_type value)
{
return (value >= cpu_type::BCM2835 && value <= cpu_type::BCM2712);
}

enum class rpi_version_type : uint8_t {
rpi_version_A = 0,
rpi_version_B = 1,
rpi_version_Aplus = 2,
rpi_version_Bplus = 3,
rpi_version_2B = 4,
rpi_version_Alpha = 5, // (early prototype)
rpi_version_CM1 = 6,
rpi_version_3B = 8,
rpi_version_Zero = 9,
public:
enum class rpi_version_type : uint8_t
{
rpi_version_A = 0x0,
rpi_version_B = 0x1,
rpi_version_Aplus = 0x2,
rpi_version_Bplus = 0x3,
rpi_version_2B = 0x4,
rpi_version_Alpha = 0x5, // (early prototype)
rpi_version_CM1 = 0x6,
unused_7 = 0x7,
rpi_version_3B = 0x8,
rpi_version_Zero = 0x9,
rpi_version_CM3 = 0xa,
unused_B = 0xb,
rpi_version_ZeroW = 0xc,
rpi_version_3Bplus = 0xd,
rpi_version_3Aplus = 0xe,
Expand All @@ -98,7 +131,39 @@ class Rpi_Revision_Code {
rpi_version_5 = 0x17,
rpi_version_invalid = 0xFF
};
bool valid_rpi_version_type(rpi_version_type value) {

private:
const vector<std::string> rpi_version_type_string = {
"rpi_version_A",
"rpi_version_B",
"rpi_version_Aplus",
"rpi_version_Bplus",
"rpi_version_2B",
"rpi_version_Alpha",
"rpi_version_CM1",
"unused_7",
"rpi_version_3B",
"rpi_version_Zero",
"rpi_version_CM3",
"unused_B",
"rpi_version_ZeroW",
"rpi_version_3Bplus",
"rpi_version_3Aplus",
"rpi_version_InternalUseOnly1",
"rpi_version_CM3plus",
"rpi_version_4B",
"rpi_version_Zero2W",
"rpi_version_400",
"rpi_version_CM4",
"rpi_version_CM4S",
"rpi_version_InternalUseOnly2",
"rpi_version_5",
"unknown_24",
"unknown_25",

};
bool valid_rpi_version_type(rpi_version_type value)
{
return (value >= rpi_version_type::rpi_version_A &&
value <= rpi_version_type::rpi_version_5);
}
Expand All @@ -112,17 +177,21 @@ class Rpi_Revision_Code {
uint32_t extract_bits(int start_bit, int size);

public:
uint8_t Revision() {
uint8_t Revision()
{
return (uint8_t)extract_bits(0, 4);
} //: 4; // (bits 0-3)
rpi_version_type Type() {
rpi_version_type Type()
{
return (rpi_version_type)extract_bits(4, 8);
} //: // (bits 4-11)
cpu_type Processor() { return (cpu_type)extract_bits(12, 4); } // (bits 12-15)
manufacturer_type Manufacturer() {
manufacturer_type Manufacturer()
{
return (manufacturer_type)extract_bits(16, 4);
} // (bits 16-19)
memory_size_type MemorySize() {
memory_size_type MemorySize()
{
return (memory_size_type)extract_bits(20, 3);
} // (bits 20-22)
// 1: new-style revision
Expand All @@ -144,6 +213,12 @@ class Rpi_Revision_Code {
bool IsValid() { return is_valid; }
void SetValid(bool val) { is_valid = val; }

std::string RevisionStr();
std::string TypeStr();
std::string ProcessorStr();
std::string ManufacturerStr();
std::string MemorySizeStr();

private:
uint32_t rpi_revcode;
bool is_valid = true;
Expand Down
Loading

0 comments on commit 7679f80

Please sign in to comment.