Advertisement
smithwinston

bbb_sysutil.c

May 2nd, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.85 KB | None | 0 0
  1. /*
  2.  * bbb_sysutil.c  Beaglebone Black System Utility
  3.  *
  4.  * Compile with:
  5.  *     cc bbb_sysutil.c -o bbb_sysutil
  6.  *
  7.  * Winston Smith <smith.winston.101@gmail.com>
  8.  */
  9.  
  10. #include <sys/cdefs.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <fcntl.h>
  15. #include <sys/ioctl.h>
  16. #include <errno.h>
  17. #include <unistd.h>
  18. #include <dev/iicbus/iic.h>
  19.  
  20. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  21.  
  22. /*
  23.  * Read data from an i2c device
  24.  */
  25. int i2c_read(int fd, int slave, uint8_t* buffer, uint16_t offset, uint16_t offsetlen, uint16_t max)
  26. {
  27.     struct iic_msg msg[2];
  28.     struct iic_rdwr_data rdwr;
  29.  
  30.  
  31.     msg[0].slave = slave;
  32.     msg[0].flags = IIC_M_WR;
  33.     msg[0].len = offsetlen;
  34.     msg[0].buf = (uint8_t*)&offset;
  35.  
  36.     msg[1].slave = slave;
  37.     msg[1].flags = IIC_M_RD;
  38.     msg[1].len = max;
  39.     msg[1].buf = buffer;
  40.  
  41.    
  42.     rdwr.nmsgs = 2;
  43.     rdwr.msgs = msg;
  44.  
  45.     if (ioctl(fd, I2CRDWR, &rdwr) < 0) {
  46.         return errno;
  47.     }
  48.  
  49.     return 0;
  50. }
  51.  
  52. /*
  53.  * BeagleBone Black system EEPROM signature
  54.  */
  55. uint8_t signature[] = { 0xAA, 0x55, 0x33, 0xEE };
  56.  
  57. /*
  58.  * Read data from the Beaglebone system EEPROM
  59.  */
  60. int read_eeprom(int fd)
  61. {
  62.     int     addr = 0x50;
  63.     uint8_t buffer[28];
  64.     char    work[32];
  65.     int     err;
  66.  
  67.  
  68.     // For sanity checking!
  69.     memset((void*)buffer, '@', sizeof(buffer));
  70.  
  71.     if ((err = i2c_read(fd, addr, buffer, 0, sizeof(uint16_t), sizeof(buffer))) != 0) {
  72.         printf("i2c_read() failed with: %s (%d)\n", strerror(err), err);
  73.         return 1;
  74.     }
  75.  
  76.     printf("EEPROM @ address %02X: signature=%02X:%02X:%02X:%02X\n",
  77.            addr,
  78.            buffer[0], buffer[1], buffer[2], buffer[3]);
  79.  
  80.     if (memcmp((void*)buffer, (void*)signature, sizeof(signature)) != 0) {
  81.         printf("ERROR: EEPROM signature mismatched\n");
  82.         return 1;
  83.     }
  84.        
  85.     // Extract the 12 bytes allocated to the model
  86.     strncpy(work, (char*)&buffer[4], 12);
  87.     work[12] = 0;
  88.     printf("Model:\t%s\n", work);
  89.  
  90.     // Extract the 12 bytes allocated to the serial number
  91.     strncpy(work, (char*)&buffer[16], 12);
  92.     work[12] = 0;
  93.     printf("Serial:\t%s\n", work);
  94.  
  95.  
  96.     return 0;
  97. }
  98.  
  99. typedef struct {
  100.     int value;
  101.     char* name;
  102. } KV;
  103.  
  104. static KV TPS65217_ID[] = {
  105.     { 0b01100000, "TPS65217D"  },
  106.     { 0b01110000, "TPS65217A"  },
  107.     { 0b11100000, "TPS65217C"  },
  108.     { 0b11110000, "TPS65217B"  },
  109.     { 0, 0 }
  110. };
  111.  
  112. static KV TPS65217_STATUS[] = {
  113.     { 0b1000, "ACPWR"   },
  114.     { 0b0100, "USBPWR"  },
  115.     { 0, 0 }
  116. };
  117.  
  118. size_t KV_lookup_value(int val, KV* table, char* buffer, size_t max)
  119. {
  120.     int i;
  121.  
  122.     for (i = 0; table[i].name; i++) {
  123.         if (val == table[i].value) {
  124.             size_t sz = MIN(max-1, strlen(table[i].name));
  125.             strncpy(buffer, table[i].name, sz);
  126.             buffer[sz] = 0;
  127.  
  128.             return sz;
  129.         }
  130.     }
  131.  
  132.     return 0;
  133. }
  134.  
  135. size_t KV_lookup_mask(int mask, KV* table, char* buffer, size_t max)
  136. {
  137.     int    i;
  138.     size_t used = 0;
  139.  
  140.    
  141.     buffer[0] = 0;
  142.  
  143.     for (i = 0; table[i].name; i++) {
  144.         if ((mask & table[i].value) != 0) {
  145.  
  146.             size_t sz = MIN(max - used - 1, strlen(table[i].name) + (used > 0 ? 1 : 0));
  147.            
  148.             if ((used > 0) && (sz > 0)) {
  149.                 strcat(buffer, ",");
  150.                 used++;
  151.                 sz--;
  152.             }
  153.  
  154.             strncat(buffer, table[i].name, sz);
  155.  
  156.             used += sz;
  157.         }
  158.     }
  159.  
  160.     return used;
  161. }
  162.  
  163. /*
  164.  * Read data from the Beaglebone system PMIC
  165.  */
  166. int read_pmic(int fd)
  167. {
  168.     int     addr = 0x24;
  169.     int     err;
  170.     uint8_t chipid;
  171.     uint8_t status;
  172.     char    work[32];
  173.  
  174.  
  175.     printf("TPS65217 PMIC @ address %02X:\n", addr);
  176.  
  177.     if ((err = i2c_read(fd, addr, &chipid, 0, sizeof(uint8_t), sizeof(chipid))) != 0) {
  178.         printf("i2c_read() failed with: %s (%d)\n", strerror(err), err);
  179.         return 1;
  180.     }
  181.  
  182.     if ((err = i2c_read(fd, addr, &status, 0x0A, sizeof(uint8_t), sizeof(status))) != 0) {
  183.         printf("i2c_read() failed with: %s (%d)\n", strerror(err), err);
  184.         return 1;
  185.     }
  186.  
  187.     KV_lookup_value(chipid & 0xF0, TPS65217_ID, work, sizeof(work));
  188.     printf("ChipID:\t%02X %s rev 1.%d\n", chipid, work, chipid & 0xF);
  189.  
  190.     KV_lookup_mask(status, TPS65217_STATUS, work, sizeof(work));
  191.     printf("Status:\t%02X %s\n", status, work);
  192.  
  193.     return 0;
  194. }
  195.  
  196. int main(int argc, char **argv)
  197. {
  198.     int     fd  = -1;
  199.     char*   dev = "/dev/iic0";
  200.     int     err = 0;
  201.  
  202.  
  203.     if (argc > 1) {
  204.         dev = argv[1];
  205.     }
  206.  
  207.     if ((fd = open(dev, O_RDWR)) < 0 )  {
  208.         perror("open failed");
  209.         err = 1;
  210.         goto exit;
  211.     }
  212.  
  213.     if ((err = read_pmic(fd)) != 0) {
  214.         goto exit;
  215.     }
  216.    
  217.     if ((err = read_eeprom(fd)) != 0) {
  218.         goto exit;
  219.     }
  220.  
  221. exit:
  222.     if (fd != -1) {
  223.         close(fd);
  224.     }
  225.  
  226.     exit(err);
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement