Skip to content

Instantly share code, notes, and snippets.

@danjperron
Last active October 16, 2019 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danjperron/2728a6178f265564cdd7fba34b2e44a3 to your computer and use it in GitHub Desktop.
Save danjperron/2728a6178f265564cdd7fba34b2e44a3 to your computer and use it in GitHub Desktop.
Raspberry Pi general information
/*
List difference between Raspberry Pi board version
The MIT License (MIT)
Copyright (c) 2015 Daniel Perron
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// revision 1.0 Daniel Perron, 17 February, 2015
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;
//#include "RaspiInfo.h"
#define __MAIN__
// start of raspiInfo.h
#include <string>
class raspiInfo
{
public:
raspiInfo();
std::string Model;
std::string ProcessorName;
std::string Manufacturer;
int ProcessorCount;
unsigned short ProcessorType;
int Revision;
unsigned long IOBase;
unsigned long long SerialNumber;
int RamSizeInMB;
unsigned long BoardRevision;
char *GPIOPin;
int I2CDevice;
static char R1GPIOPin[32];
static char R2GPIOPin[32];
static char R2PlusGPIOPin[32];
private:
bool ExtractToken(std::string source, std::string label, std::string& Value);
};
// end of raspiInfo.h
#define PI_IO_BASE (0x20000000)
#define PI2_IO_BASE (0x3F000000)
char raspiInfo::R1GPIOPin[32]= { 3, 5, 0, 0, 7, 0, 0, 0,
24,21,19,23, 0, 0, 8,10,
0,11,12, 0, 0,13,15,16,
18,22, 0, 0,47,44,45,46};
char raspiInfo::R2GPIOPin[32]= { 0, 0, 3, 5, 7, 0, 0, 0,
24,21,19,23, 0, 0, 8,10,
0,11,12, 0, 0, 0,15,16,
18,22, 0,13,47,44,45,46};
char raspiInfo::R2PlusGPIOPin[32]= { 0, 0, 3, 5, 7,29,31,26,
24,21,19,23,32,33, 8,10,
36,11,12,35,38,40,15,16,
18,22,37,13,47,44,45,46};
bool raspiInfo::ExtractToken(string source, string label,string& value)
{
size_t found;
string theLabel;
found = source.find(":");
if(found == string::npos) return false;
theLabel = source.substr(0,found);
if(theLabel.find(label) == string::npos) return false;
value = source.substr(found+1);
return true;
}
raspiInfo::raspiInfo()
{
size_t pos_S,pos_E;
unsigned long ltemp;
string cmdline,cpuinfo;
string string1,string2;
istringstream ss;
unsigned char ManufacturerID;
Model="?";
Revision = -1;
SerialNumber=0;
RamSizeInMB=0;
IOBase=0;
Manufacturer="Egoman";
ProcessorName="";
ProcessorType=0;
ProcessorCount=0;
I2CDevice=1;
GPIOPin= R2GPIOPin;
stringstream myss;
try {
ifstream fs("/proc/cpuinfo");
while(!fs.eof())
{
std::getline(fs, string1);
if(ExtractToken(string1,"model name",string2))
ProcessorName= string2;
if(ExtractToken(string1,"processor",string2))
ProcessorCount++;
if(ExtractToken(string1,"Revision",string2))
{
ss.str(string2);
ss >> std::hex >> BoardRevision >> std::dec;
ss.clear();
}
if(ExtractToken(string1,"Serial",string2))
{
ss.str(string2);
ss >> std::hex >> SerialNumber >> std::dec;
ss.clear();
}
}
} catch (exception e) {
// unkown
return;
}
if(ProcessorCount==0)
ProcessorCount=1;
if(BoardRevision & 0x800000)
{
//ok new method
Revision = BoardRevision & 0xf;
// display Raspberry Pi type
unsigned char ModelType = (BoardRevision >> 4) & 0xff;
switch(ModelType)
{
case 0 : Model="A";break;
case 1 : Model="B";break;
case 2 : Model="A+";GPIOPin=R2PlusGPIOPin;break;
case 3 : Model="B+";GPIOPin=R2PlusGPIOPin;break;
case 4 : Model="Pi 2 B";GPIOPin=R2PlusGPIOPin;break;
case 5 : Model="Alpha";break;
case 6 : Model="Compute Module";break;
case 8 : Model="Pi 3 B"; GPIOPin=R2PlusGPIOPin;break;
case 9 : Model="Pi Zero";GPIOPin=R2PlusGPIOPin;break;
case 10 : Model="Compute Module 3";break;
case 12 : Model="Pi Zero W";GPIOPin=R2PlusGPIOPin;break;
default:
myss << "Unknown type :" << ModelType;
Model = myss.str();
}
// Ram size in MB
ltemp = (BoardRevision >> 20 ) & 7;
switch(ltemp)
{
case 0 : RamSizeInMB = 256;IOBase=PI_IO_BASE;break;
case 1 : RamSizeInMB = 512;IOBase=PI_IO_BASE;break;
case 2 : RamSizeInMB = 1024;IOBase=PI2_IO_BASE;break;
default : RamSizeInMB = 0;IOBase=0;break;
}
// processor
ProcessorType = (BoardRevision >> 12) & 0xffff;
// Manufacturer
ManufacturerID = (BoardRevision >> 16) & 15;
switch(ManufacturerID)
{
case 0: Manufacturer="Sony UK";break;
case 1: break;
case 2: Manufacturer="Embest";break;
case 3: Manufacturer="Sony Japan";break;
default: Manufacturer="Unknown";
}
}
else
{
//old method;
RamSizeInMB=512;
Revision = BoardRevision & 0x1f;
IOBase = PI_IO_BASE;
switch(Revision)
{
case 2:
case 3: I2CDevice=0;GPIOPin=R1GPIOPin;
case 4: Model="B";RamSizeInMB=256;break;
case 5:
case 6:
case 0xd:
case 0xe:
case 0xf: Model="B";break;
case 7:
case 8:
case 9: Model="A";RamSizeInMB=256;break;
case 0x10: Model="B+";GPIOPin=R2PlusGPIOPin;break;
case 0x11: Model="Compute Module";break;
case 0x12: Model="A+";RamSizeInMB=256;GPIOPin=R2PlusGPIOPin;break;
}
// ok manufacturer
switch(Revision)
{
case 4:
case 8:
case 0xe:
case 0x10:
case 0x11:
case 0x12: Manufacturer="Sony UK";break;
case 5:
case 9: Manufacturer="Qisda";break;
case 0x13:
case 0x14:
case 0x15: Manufacturer="Embest";
}
}
// check the existence of device-tree/model
ifstream fsm("/proc/device-tree/model");
if(!fsm.fail())
{
std::string gotModel;
std::getline(fsm,gotModel);
if(!fsm.fail())
{
Model=gotModel;
}
}
}
#ifdef __MAIN__
int main(void)
{
raspiInfo MyPi;
cout << "Raspberry PI Information" << endl;
cout << "D.J.Perron (c) 2015" << endl;
cout << "=======================" << endl;
cout << "Model : " << MyPi.Model << endl;
cout << "cpu :" << MyPi.ProcessorName << endl;
cout << "# of cpu(s) : " << MyPi.ProcessorCount << endl;
cout << "S/N : " << std::hex << MyPi.SerialNumber << endl;
cout << "Manufacturer : " << MyPi.Manufacturer << endl;
cout << "Board Revision : " << std::hex << MyPi.BoardRevision << endl;
cout << "Revision : " << std::hex << MyPi.Revision << endl;
cout << "Ram size (MB) : " << std::dec << MyPi.RamSizeInMB << endl;
cout << "I2C(#) On GPIO : i2c(" << std::dec << MyPi.I2CDevice << ")" <<endl;
cout << "IO Map address : " << std::hex << MyPi.IOBase << std::dec << endl;
return 0;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment