Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * bbb_eeprom.c Read Model & SerialNo from BeagleBone Black's EEPROM via i2c
- *
- * Compile with:
- * cc bbb_eeprom.c -o bbb_eeprom
- *
- * Winston Smith <smith.winston.101@gmail.com>
- *
- * 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement