Skip to content

Instantly share code, notes, and snippets.

@zhanglongqi
Last active January 31, 2020 07:15
Show Gist options
  • Save zhanglongqi/a2413f363079177f627fc52df71187f0 to your computer and use it in GitHub Desktop.
Save zhanglongqi/a2413f363079177f627fc52df71187f0 to your computer and use it in GitHub Desktop.
Beaglebone Black operation
/*
* bbb_eeprom.c Read Model & SerialNo from BeagleBone Black's EEPROM via i2c
*
* Compile with:
* cc bbb_eeprom.c -o bbb_eeprom
*
* Winston Smith <[email protected]>
*
* History:
* 2014-05-01 Initial Implementation for FreeBSD
* 2014-05-17 Port to Linux
*/
#include <stdint.h>
#include <sys/cdefs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <unistd.h>
#if defined(__linux__)
# include <linux/i2c.h>
# include <linux/i2c-dev.h>
#else
# include <dev/iicbus/iic.h>
#endif
#if defined(__linux__)
# define I2CRDWR I2C_RDWR
# define IIC_M_RD I2C_M_RD
# define IIC_M_WR 0
# define iic_rdwr_data i2c_rdwr_ioctl_data
# define iic_msg i2c_msg
# define slave addr
#endif
/*
* Read data from an i2c device
*/
int i2c_read(int fd, int slave, uint8_t *buffer, uint16_t offset, uint16_t len)
{
struct iic_msg msg[2];
struct iic_rdwr_data rdwr;
msg[0].slave = slave;
msg[0].flags = IIC_M_WR;
msg[0].len = sizeof(offset);
msg[0].buf = (uint8_t *)&offset;
msg[1].slave = slave;
msg[1].flags = IIC_M_RD;
msg[1].len = len;
msg[1].buf = buffer;
rdwr.nmsgs = 2;
rdwr.msgs = msg;
if (ioctl(fd, I2CRDWR, &rdwr) < 0) {
return errno;
}
return 0;
}
/*
* BeagleBone Black system EEPROM signature
*/
uint8_t signature[] = { 0xAA, 0x55, 0x33, 0xEE };
int main(int argc, char **argv)
{
int fd;
#if defined(__linux__)
char *dev = "/dev/i2c-0";
#else
char *dev = "/dev/iic0";
#endif
int addr = 0x50;
uint8_t buffer[28];
char work[32];
int err;
if (argc > 1) {
dev = argv[1];
}
if ((fd = open(dev, O_RDWR)) < 0) {
perror("open failed");
exit(-1);
}
// For sanity checking!
memset((void *)buffer, '@', sizeof(buffer));
if ((err = i2c_read(fd, addr, buffer, 0, sizeof(buffer))) != 0) {
printf("i2c_read() failed with: %s (%d)\n", strerror(err), err);
exit(1);
}
close(fd);
printf("Read from slave %02X on %s: signature=%02X:%02X:%02X:%02X\n",
addr,
dev,
buffer[0], buffer[1], buffer[2], buffer[3]);
if (memcmp((void *)buffer, (void *)signature, sizeof(signature)) != 0) {
printf("ERROR: EEPROM signature mismatched\n");
exit(1);
}
// Extract the 12 bytes allocated to the model
strncpy(work, (char *)&buffer[4], 12);
work[12] = 0;
printf("Model:\t%s\n", work);
// Extract the 12 bytes allocated to the serial number
strncpy(work, (char *)&buffer[16], 12);
work[12] = 0;
printf("Serial:\t%s\n", work);
exit(0);
}

read eeprom using hexdump in debian

$ sudo hexdump  -C  /sys/devices/platform/ocp/44e0b000.i2c/i2c-0/0-0050/eeprom 
00000000  aa 55 33 ee 41 33 33 35  42 4e 4c 54 30 30 41 35  |.U3.A335BNLT00A5|
00000010  34 30 33 36 42 42 42 4b  39 30 30 30 45 4d 45 58  |4036BBBK9000EMEX|
00000020  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00000030  30 31 32 33 34 35 36 37  38 39 41 42 43 44 45 46  |0123456789ABCDEF|
00000040  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
*
00001000  aa 55 33 ee 41 33 33 35  42 4e 4c 54 30 30 41 35  |.U3.A335BNLT00A5|
00001010  34 30 33 36 42 42 42 4b  39 30 30 30 45 4d 45 58  |4036BBBK9000EMEX|
00001020  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00001030  30 31 32 33 34 35 36 37  38 39 41 42 43 44 45 46  |0123456789ABCDEF|
00001040  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
*
00002000  aa 55 33 ee 41 33 33 35  42 4e 4c 54 30 30 41 35  |.U3.A335BNLT00A5|
00002010  34 30 33 36 42 42 42 4b  39 30 30 30 45 4d 45 58  |4036BBBK9000EMEX|
00002020  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00002030  30 31 32 33 34 35 36 37  38 39 41 42 43 44 45 46  |0123456789ABCDEF|
00002040  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
*
00003000  aa 55 33 ee 41 33 33 35  42 4e 4c 54 30 30 41 35  |.U3.A335BNLT00A5|
00003010  34 30 33 36 42 42 42 4b  39 30 30 30 45 4d 45 58  |4036BBBK9000EMEX|
00003020  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00003030  30 31 32 33 34 35 36 37  38 39 41 42 43 44 45 46  |0123456789ABCDEF|
00003040  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
*
00004000  aa 55 33 ee 41 33 33 35  42 4e 4c 54 30 30 41 35  |.U3.A335BNLT00A5|
00004010  34 30 33 36 42 42 42 4b  39 30 30 30 45 4d 45 58  |4036BBBK9000EMEX|
00004020  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00004030  30 31 32 33 34 35 36 37  38 39 41 42 43 44 45 46  |0123456789ABCDEF|
00004040  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
*
00005000  aa 55 33 ee 41 33 33 35  42 4e 4c 54 30 30 41 35  |.U3.A335BNLT00A5|
00005010  34 30 33 36 42 42 42 4b  39 30 30 30 45 4d 45 58  |4036BBBK9000EMEX|
00005020  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00005030  30 31 32 33 34 35 36 37  38 39 41 42 43 44 45 46  |0123456789ABCDEF|
00005040  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
*
00006000  aa 55 33 ee 41 33 33 35  42 4e 4c 54 30 30 41 35  |.U3.A335BNLT00A5|
00006010  34 30 33 36 42 42 42 4b  39 30 30 30 45 4d 45 58  |4036BBBK9000EMEX|
00006020  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00006030  30 31 32 33 34 35 36 37  38 39 41 42 43 44 45 46  |0123456789ABCDEF|
00006040  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
*
00007000  aa 55 33 ee 41 33 33 35  42 4e 4c 54 30 30 41 35  |.U3.A335BNLT00A5|
00007010  34 30 33 36 42 42 42 4b  39 30 30 30 45 4d 45 58  |4036BBBK9000EMEX|
00007020  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00007030  30 31 32 33 34 35 36 37  38 39 41 42 43 44 45 46  |0123456789ABCDEF|
00007040  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
*
00008000

compile u-boot

$ alias amake='make -j8 ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- '

$ amake am335x_boneblack_vboot_defconfig
#
# configuration written to .config
#

read eeprom by i2c from u-boot command line interface

  • set i2c bus to 0
=>i2c dev 0
Setting bus to 0
  • i2c device probe
=> i2c probe     
Valid chip addresses: 24 34 50

0x50 is the address of eeprom

  • read the eeprom from i2c address 0x50 by 16 bits .2 for 0x30 bytes
=> i2c md 0x50 0.2 0x30
0000: aa 55 33 ee 41 33 33 35 42 4e 4c 54 30 30 41 35    .U3.A335BNLT00A5
0010: 34 30 33 36 42 42 42 4b 39 30 30 30 45 4d 45 58    4036BBBK9000EMEX
0020: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment