Expose serial number

This commit is contained in:
KurisuD 2023-01-03 14:53:54 +09:00
parent 27b5ea9c58
commit f385cb7d80
13 changed files with 161 additions and 3 deletions

View File

@ -343,6 +343,7 @@ UTILITIES
H/HELP :: Display command help :: H/HELP :: Display command help ::
HWVER :: Get hardware version :: gpioHardwareRevision HWVER :: Get hardware version :: gpioHardwareRevision
SERNM :: Get hardware serial number :: gpioHardwareSerialNumber
MICS v :: Microseconds delay :: gpioDelay MICS v :: Microseconds delay :: gpioDelay
MILS v :: Milliseconds delay :: gpioDelay MILS v :: Milliseconds delay :: gpioDelay
PIGPV :: Get pigpio library version :: gpioVersion PIGPV :: Get pigpio library version :: gpioVersion
@ -1259,6 +1260,19 @@ $ pigs hwver # On a B+
16 16
... ...
SERNM ::
This command returns the hardware serial number of the Pi.
The hardware serial number is found on the serial line of /proc/cpuinfo.
If the hardware revision can not be found or is not a valid hexadecimal
number the command returns 0.
...
$ pigs sernm
571858502
...
I2CC :: I2CC ::
This command closes an I2C handle [*h*] previously opened with [*I2CO*]. This command closes an I2C handle [*h*] previously opened with [*I2CO*].

View File

@ -1006,6 +1006,22 @@ uint32_t spi_flags<br></td>
<td valign="top">4*X<br></td> <td valign="top">4*X<br></td>
<td valign="top">uint32_t pars[X]<br></td> <td valign="top">uint32_t pars[X]<br></td>
</tr> </tr>
<!--tr>
<td valign="top">WVCAP<br></td>
<td valign="top">118<br></td>
<td valign="top">todo<br></td>
<td valign="top">todo<br></td>
<td valign="top">todo<br></td>
<td valign="top">todo<br></td>
</tr!-->
<tr>
<td valign="top">SERNM<br></td>
<td valign="top">117<br></td>
<td valign="top">script_id<br></td>
<td valign="top">0<br></td>
<td valign="top">4*X<br></td>
<td valign="top">uint32_t pars[X]<br></td>
</tr>
</tbody> </tbody>
</table> </table>
<h3><a name="Response" id="Response"></a>Response</h3> <h3><a name="Response" id="Response"></a>Response</h3>
@ -1984,6 +2000,22 @@ uint8_t data[X]</td>
<td valign="top">0<br></td> <td valign="top">0<br></td>
<td valign="top">-<br></td> <td valign="top">-<br></td>
</tr> </tr>
<!--tr>
<td valign="top">WVCAP<br></td>
<td valign="top">118<br></td>
<td valign="top">-<br></td>
<td valign="top">-<br></td>
<td valign="top">wave id<br></td>
<td valign="top">-<br></td>
</tr-->
<tr>
<td valign="top">SERNM *<br></td>
<td valign="top">119<br></td>
<td valign="top">-<br></td>
<td valign="top">-<br></td>
<td valign="top">serial number<br></td>
<td valign="top">-<br></td>
</tr>
</tbody> </tbody>
</table> </table>
<code><br></code> <code><br></code>

View File

@ -94,6 +94,7 @@ cmdInfo_t cmdInfo[]=
{PI_CMD_HP, "HP", 131, 0, 1}, // gpioHardwarePWM {PI_CMD_HP, "HP", 131, 0, 1}, // gpioHardwarePWM
{PI_CMD_HWVER, "HWVER", 101, 4, 1}, // gpioHardwareRevision {PI_CMD_HWVER, "HWVER", 101, 4, 1}, // gpioHardwareRevision
{PI_CMD_SERNM, "SERNM", 101, 4, 1}, // gpioHardwareSerialNumber
{PI_CMD_I2CC, "I2CC", 112, 0, 1}, // i2cClose {PI_CMD_I2CC, "I2CC", 112, 0, 1}, // i2cClose
{PI_CMD_I2CO, "I2CO", 131, 2, 1}, // i2cOpen {PI_CMD_I2CO, "I2CO", 131, 2, 1}, // i2cOpen
@ -307,6 +308,7 @@ H/HELP Display command help\n\
HC g f Set hardware clock frequency\n\ HC g f Set hardware clock frequency\n\
HP g f dc Set hardware PWM frequency and dutycycle\n\ HP g f dc Set hardware PWM frequency and dutycycle\n\
HWVER Get hardware version\n\ HWVER Get hardware version\n\
SERNM Get hardware serial number\n\
\n\ \n\
I2CC h Close I2C handle\n\ I2CC h Close I2C handle\n\
I2CO bus device flags | Open I2C bus and device with flags\n\ I2CO bus device flags | Open I2C bus and device with flags\n\
@ -671,7 +673,7 @@ int cmdParse(
case 101: /* BR1 BR2 CGI H HELP HWVER case 101: /* BR1 BR2 CGI H HELP HWVER
DCRA HALT INRA NO DCRA HALT INRA NO
PIGPV POPA PUSHA RET T TICK WVBSY WVCLR PIGPV POPA PUSHA RET T TICK WVBSY WVCLR
WVCRE WVGO WVGOR WVHLT WVNEW WVCRE WVGO WVGOR WVHLT WVNEW SERNM
No parameters, always valid. No parameters, always valid.
*/ */

View File

@ -10733,6 +10733,8 @@ A 16-bit word value.
.br .br
#define PI_CMD_WVCAP 118 #define PI_CMD_WVCAP 118
.br .br
#define PI_CMD_SERNM 119
.br
.br .br

View File

@ -2096,6 +2096,8 @@ static int myDoCommand(uintptr_t *p, unsigned bufSize, char *buf)
break; break;
case PI_CMD_HWVER: res = gpioHardwareRevision(); break; case PI_CMD_HWVER: res = gpioHardwareRevision(); break;
case PI_CMD_SERNM: res = gpioHardwareSerialNumber(); break;
@ -13846,6 +13848,41 @@ unsigned gpioHardwareRevision(void)
} }
unsigned int gpioHardwareSerialNumber(void)
{
static unsigned serial = 0;
FILE * filp;
char buf[512];
char term;
DBG(DBG_USER, "");
if (serial) return serial;
filp = fopen ("/proc/cpuinfo", "r");
if (filp != NULL)
{
while (fgets(buf, sizeof(buf), filp) != NULL)
{
if (!strncasecmp("serial\t\t: ", buf, 10))
{
if (sscanf(buf+10, "%x%c", &serial, &term) == 2)
{
if (term != '\n') serial = 0;
}
}
}
fclose(filp);
}
DBG(DBG_USER, "serial number=%x", serial);
return serial;
}
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */
int gpioCfgBufferSize(unsigned millis) int gpioCfgBufferSize(unsigned millis)

View File

@ -4298,6 +4298,7 @@ for "Revision : 000f" the function returns 15.
for "Revision : 000g" the function returns 0. for "Revision : 000g" the function returns 0.
D*/ D*/
unsigned int gpioHardwareSerialNumber(void);
/*F*/ /*F*/
unsigned gpioVersion(void); unsigned gpioVersion(void);
@ -6317,6 +6318,7 @@ PARAMS*/
#define PI_CMD_PROCU 117 #define PI_CMD_PROCU 117
#define PI_CMD_WVCAP 118 #define PI_CMD_WVCAP 118
#define PI_CMD_SERNM 119
/*DEF_E*/ /*DEF_E*/
/* /*

View File

@ -317,6 +317,7 @@ UTILITIES
get_current_tick Get current tick (microseconds) get_current_tick Get current tick (microseconds)
get_hardware_revision Get hardware revision get_hardware_revision Get hardware revision
get_hardware_serial_number Get hardware serial number
get_pigpio_version Get the pigpio version get_pigpio_version Get the pigpio version
pigpio.error_text Gets error text from error number pigpio.error_text Gets error text from error number
@ -573,6 +574,7 @@ _PI_CMD_EVT =116
_PI_CMD_PROCU=117 _PI_CMD_PROCU=117
_PI_CMD_WVCAP=118 _PI_CMD_WVCAP=118
_PI_CMD_SERNM=119
# pigpio error numbers # pigpio error numbers
@ -2089,6 +2091,29 @@ class pi():
""" """
return _pigpio_command(self.sl, _PI_CMD_HWVER, 0, 0) return _pigpio_command(self.sl, _PI_CMD_HWVER, 0, 0)
def get_hardware_serial_number(self):
"""
Returns the Pi's hardware serial number in decimal base.
The hardware serial number is the last few characters on the
Serial line of /proc/cpuinfo.
If the hardware revision can not be found or is not a valid
hexadecimal number the function returns 0.
...
print(pi.get_hardware_serial_number())
571858502
...
To get the hexadecimal value as displayed in /proc/cpuinfo:
...
print("0x{:016x}".format(pi.get_hardware_serial_number()))
0x000000002215de46
"""
return _pigpio_command(self.sl, _PI_CMD_SERNM, 0, 0)
def get_pigpio_version(self): def get_pigpio_version(self):
""" """
Returns the pigpio software version. Returns the pigpio software version.
@ -5669,7 +5694,7 @@ def xref():
percent:: 0-100 percent:: 0-100
The size of waveform as percentage of maximum available. The size of waveform as percentage of maximum available.
port: port:
The port used by the pigpio daemon, defaults to 8888. The port used by the pigpio daemon, defaults to 8888.

View File

@ -668,6 +668,9 @@ uint32_t get_current_tick(void)
uint32_t get_hardware_revision(void) uint32_t get_hardware_revision(void)
{return pigpio_command(gPigCommand, PI_CMD_HWVER, 0, 0, 1);} {return pigpio_command(gPigCommand, PI_CMD_HWVER, 0, 0, 1);}
uint32_t get_hardware_sernum(void)
{return pigpio_command(gPigCommand, PI_CMD_SERNM, 0, 0, 1);}
uint32_t get_pigpio_version(void) uint32_t get_pigpio_version(void)
{return pigpio_command(gPigCommand, PI_CMD_PIGPV, 0, 0, 1);} {return pigpio_command(gPigCommand, PI_CMD_PIGPV, 0, 0, 1);}

View File

@ -886,6 +886,9 @@ uint32_t get_current_tick(int pi)
uint32_t get_hardware_revision(int pi) uint32_t get_hardware_revision(int pi)
{return pigpio_command(pi, PI_CMD_HWVER, 0, 0, 1);} {return pigpio_command(pi, PI_CMD_HWVER, 0, 0, 1);}
uint32_t get_hardware_serial_number(int pi)
{return pigpio_command(pi, PI_CMD_SERNM, 0, 0, 1);}
uint32_t get_pigpio_version(int pi) uint32_t get_pigpio_version(int pi)
{return pigpio_command(pi, PI_CMD_PIGPV, 0, 0, 1);} {return pigpio_command(pi, PI_CMD_PIGPV, 0, 0, 1);}

31
pigs.1
View File

@ -589,7 +589,10 @@ Get waveform pulse stats
Display command help Display command help
.P .P
.B HWVER .B HWVER
Get hardware version Get hardware version
.P
.B SERNM
Get hardware serial number
.P .P
.B MICS v .B MICS v
Microseconds delay Microseconds delay
@ -2255,6 +2258,32 @@ $ pigs hwver # On a B+
16 16
.br .br
.br
.IP "\fBSERNM \fP - Get hardware serial number"
.IP "" 4
This command returns the hardware serial number of the Pi.
.br
The hardware serial number is found on the serial line of /proc/cpuinfo.
.br
If the hardware serial number can not be found or is not a valid hexadecimal
number the command returns 0.
.br
\fBExample\fP
.br
.EX
$ pigs sernm
.br
571858502
.br
.EE .EE
.br .br

View File

@ -50,6 +50,8 @@ void t0()
printf("pigpio version %d.\n", gpioVersion()); printf("pigpio version %d.\n", gpioVersion());
printf("Hardware revision %d.\n", gpioHardwareRevision()); printf("Hardware revision %d.\n", gpioHardwareRevision());
printf("Hardware serialnumber %u.\n", gpioHardwareSerialNumber());
} }
void t1() void t1()

3
x_pigs
View File

@ -55,6 +55,9 @@ if [[ ${#s} = 5423 ]]; then echo "HELP ok"; else echo "HELP fail (${#s})"; fi
s=$(pigs hwver) s=$(pigs hwver)
if [[ $s -ne 0 ]]; then echo "HWVER ok"; else echo "HWVER fail ($s)"; fi if [[ $s -ne 0 ]]; then echo "HWVER ok"; else echo "HWVER fail ($s)"; fi
s=$(pigs sernm)
if [[ $s -ne 0 ]]; then echo "SERNM ok"; else echo "SERNM fail ($s)"; fi
s=$(pigs mics 1000) s=$(pigs mics 1000)
if [[ $s = "" ]]; then echo "MICS ok"; else echo "MICS fail ($s)"; fi if [[ $s = "" ]]; then echo "MICS ok"; else echo "MICS fail ($s)"; fi

4
x_pipe
View File

@ -76,6 +76,10 @@ echo "hwver" >/dev/pigpio
read -t 1 s </dev/pigout read -t 1 s </dev/pigout
if [[ $s -ne 0 ]]; then echo "HWVER ok"; else echo "HWVER fail ($s)"; fi if [[ $s -ne 0 ]]; then echo "HWVER ok"; else echo "HWVER fail ($s)"; fi
echo "sernm" >/dev/pigpio
read -t 1 s </dev/pigout
if [[ $s -ne 0 ]]; then echo "SERNM ok"; else echo "SERNM fail ($s)"; fi
echo "mics 1000" >/dev/pigpio echo "mics 1000" >/dev/pigpio
read -t 1 s </dev/pigout read -t 1 s </dev/pigout
if [[ $s = 0 ]]; then echo "MICS ok"; else echo "MICS fail ($s)"; fi if [[ $s = 0 ]]; then echo "MICS ok"; else echo "MICS fail ($s)"; fi