Closed UseGitNotSvn closed 7 years ago
This is a very good approach, I tested it on my Pi Zero and it did work.
Don't forget to increase the size of proc_model, otherwise the buffer provided for reading will not contain the hole file and also not the information we are interested in. The size of this file on my Pi Zero is 369 characters, so I set the size of proc_model to 500 (don't know if the file contains more characters on other PI's).
Oops yes, I spotted that but forgot to mention it ;-)
There is 4 times the cpu data on quad core Pis. I will check the size and allow for an 8 core processor, 1024 will probably cover it.
Oops, way off. 2207 allowing for a 8 core future CPU.
Why not read the ammount of characters first, create a buffer with the files size second and search afterwards?
Good suggestion, but I think I prefer a simpler fixed size buffer.
There are two ways to determine the size of the file, one is to read the file to the end which we don't want to do because we don't know how much memory to allow for it. I suppose we could allocate a small buffer and keep reading it in chunks but that seems like its getting complicated unless we were very short on memory which we're not.
The other way is to use the stat command which returns the file size as held by the filesystem. Unfortunately for this procfs pseudo file, stat returns zero length.
Thanks for that, however, I don't think that change will work for every Pi out there.
Poking around, I see that /proc/cpuinfo has a specific 'Hardware' attribute that calls out either the BCM2708 or BCM2709 peripheral set.
So this should work for every Pi that has a procfs:
if ((proc_fd = open("/proc/cpuinfo", O_RDONLY|O_SYNC) ) >= 0) { // if we can read the device tree, identify board type and select corresponding peripheral // memory address read(proc_fd, proc_model, sizeof proc_model); if (strstr(proc_model, "BCM2708")) { printf("\rBCM2708 peripheral set detected\n"); peri_base = BCM2708_PERI_BASE; } else if (strstr(proc_model, "BCM2709")) { printf("\rBCM2709 peripheral set detected\n"); peri_base = BCM2709_PERI_BASE; } else { printf("\rCould not identify Pi peripheral model.\n"); exit(-1); } }
Those that don't have procfs will default to BCM2708, a pretty safe bet.
Can you try this on your zero and let me know what you think?