Advertisement
smithwinston

bbb_eeprom.c

May 1st, 2014
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.78 KB | None | 0 0
  1. /*
  2.  * bbb_eeprom.c  Read Model & SerialNo from BeagleBone Black's EEPROM via i2c
  3.  *
  4.  * Compile with:
  5.  *     cc bbb_eeprom.c -o bbb_eeprom
  6.  *
  7.  * Winston Smith <smith.winston.101@gmail.com>
  8.  *
  9.  * History:
  10.  *   2014-05-01  Initial Implementation for FreeBSD
  11.  *   2014-05-17  Port to Linux
  12.  */
  13.  
  14. #include <stdint.h>
  15. #include <sys/cdefs.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <fcntl.h>
  20. #include <sys/ioctl.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23.  
  24. #if defined(__linux__)
  25. #include <linux/i2c.h>
  26. #include <linux/i2c-dev.h>
  27. #else
  28. #include <dev/iicbus/iic.h>
  29. #endif
  30.  
  31. #if defined(__linux__)
  32. #define I2CRDWR I2C_RDWR
  33. #define IIC_M_RD I2C_M_RD
  34. #define IIC_M_WR 0
  35. #define iic_rdwr_data i2c_rdwr_ioctl_data
  36. #define iic_msg i2c_msg
  37. #define slave addr
  38. #endif
  39.  
  40. /*
  41.  * Read data from an i2c device
  42.  */
  43. int i2c_read(int fd, int slave, uint8_t* buffer, uint16_t offset, uint16_t len)
  44. {
  45.     struct iic_msg msg[2];
  46.     struct iic_rdwr_data rdwr;
  47.  
  48.  
  49.     msg[0].slave = slave;
  50.     msg[0].flags = IIC_M_WR;
  51.     msg[0].len = sizeof(offset);
  52.     msg[0].buf = (uint8_t*)&offset;
  53.  
  54.     msg[1].slave = slave;
  55.     msg[1].flags = IIC_M_RD;
  56.     msg[1].len = len;
  57.     msg[1].buf = buffer;
  58.  
  59.    
  60.     rdwr.nmsgs = 2;
  61.     rdwr.msgs = msg;
  62.  
  63.     if (ioctl(fd, I2CRDWR, &rdwr) < 0) {
  64.         return errno;
  65.     }
  66.  
  67.     return 0;
  68. }
  69.  
  70. /*
  71.  * BeagleBone Black system EEPROM signature
  72.  */
  73. uint8_t signature[] = { 0xAA, 0x55, 0x33, 0xEE };
  74.  
  75. int main(int argc, char **argv)
  76. {
  77.     int     fd;
  78. #if defined(__linux__)
  79.     char*   dev = "/dev/i2c-0";
  80. #else
  81.     char*   dev = "/dev/iic0";
  82. #endif
  83.     int     addr = 0x50;
  84.     uint8_t buffer[28];
  85.     char    work[32];
  86.     int     err;
  87.  
  88.  
  89.     if (argc > 1) {
  90.         dev = argv[1];
  91.     }
  92.  
  93.     if ((fd = open(dev, O_RDWR)) < 0 )  {
  94.         perror("open failed");
  95.         exit(-1);
  96.     }
  97.  
  98.     // For sanity checking!
  99.     memset((void*)buffer, '@', sizeof(buffer));
  100.  
  101.     if ((err = i2c_read(fd, addr, buffer, 0, sizeof(buffer))) != 0) {
  102.         printf("i2c_read() failed with: %s (%d)\n", strerror(err), err);
  103.         exit(1);
  104.     }
  105.  
  106.     close(fd);
  107.  
  108.     printf("Read from slave %02X on %s: signature=%02X:%02X:%02X:%02X\n",
  109.            addr,
  110.            dev,
  111.            buffer[0], buffer[1], buffer[2], buffer[3]);
  112.  
  113.     if (memcmp((void*)buffer, (void*)signature, sizeof(signature)) != 0) {
  114.         printf("ERROR: EEPROM signature mismatched\n");
  115.         exit(1);
  116.     }
  117.        
  118.     // Extract the 12 bytes allocated to the model
  119.     strncpy(work, (char*)&buffer[4], 12);
  120.     work[12] = 0;
  121.     printf("Model:\t%s\n", work);
  122.  
  123.     // Extract the 12 bytes allocated to the serial number
  124.     strncpy(work, (char*)&buffer[16], 12);
  125.     work[12] = 0;
  126.     printf("Serial:\t%s\n", work);
  127.    
  128.  
  129.     exit(0);
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement