Subversion Repositories svnkaklik

Rev

Rev 409 | Blame | Compare with Previous | Last modification | View Log | Download

/*****************************************************************************/
/*
 *  BoardController.cpp - communication with NGW100 Board controller
 *
 *      Copyright (C) 2007  Karel Hojdar
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *
 *
 *  Revision history
 *    15.06.2007   1.0   Initial release
 */
/*****************************************************************************/

#include <iostream>

#include <getopt.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "linux/i2c-dev.h"
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;

static char* version = "BoardController, version 0.1";

static char* help = "Usage: BoardController [OPTION]\n  -I\t\tshow ID\n  -M\t\tshow model\n  -R\t\tshow revision\n  -S\t\tshow serial no.\n  -T\t\tshow temperature\n\n";

#define I2C_SLAVE       0x0703  /* Change slave address                 */
#define I2C_RDWR        0x0707  /* Combined R/W transfer (one stop only)*/

#define BC_Addr 0x0B

void DoExit(int ex)
{
        exit(ex);
}

unsigned char xfunc[5] = {0x99, 0x9A, 0x9B, 0x9E, 0x8D};
unsigned char xlen[5] = {8, 6, 1, 15, 2};

int main(int argc, char *argv[], char *envp[])
{
        char    *progname;
        int c, func = 0, Len;
        int     i2cbus = 0;
        char filename[20], Buf[64];
        int file;

        progname = strrchr(argv[0], '/');
        progname = progname ? progname + 1 : argv[0];

        while ((c = getopt (argc, argv, "IiMmRrSsTth")) != -1)
                switch (c)
                {
                case 'I':
                case 'i':
                        func = 0;
                        break;

                case 'M':
                case 'm':
                        func = 1;
                        break;

                case 'R':
                case 'r':
                        func = 2;
                        break;

                case 'S':
                case 's':
                        func = 3;
                        break;

                case 'T':
                case 't':
                        func = 4;
                        break;

                case 'h':
                        printf ("%s\n%s", version, help);
                        return 1;

                case '?':
                        printf ("Unknown option `-%c', try %s -h.\n", optopt,progname);
                        return 1;
                }

        sprintf(filename, "/dev/i2c-%d", i2cbus);
        file = open(filename, O_RDWR);

        if (file < 0)
        {
                cerr << "Could not open /dev/i2c-0." << endl;
                return -1;
        }

        if (ioctl(file, I2C_SLAVE, BC_Addr) == -1)
        {
                fprintf(stderr, "Failed to set address to 0x%02x.\n", BC_Addr);
                DoExit(-2);
        }

        int Loops = 0;

        do
        {
                Buf[0] = xfunc[func];
                if ( write(file, Buf, 1) != 1)
                {
                        fprintf(stderr, "Failed to write byte to address to 0x%02x, errno %i.\n", BC_Addr, errno);
                        DoExit(-3);
                }

                if (read(file, Buf, 1) != 1)
                {
                        fprintf(stderr, "Failed to read response length, errno %i.\n", errno);
                        DoExit(-4);
                }

                Len = Buf[0];
                if (read(file, Buf, Len) != Len)
                {
                        fprintf(stderr, "Failed to read response, errno %i.\n", errno);
                        DoExit(-5);
                }

                Loops++;
        } while (Len != xlen[func]);

        if (Loops > 1)
                fprintf(stderr, "After %i attempts got: \n", Loops);

        switch (func)
        {
                case 0:
                        Buf[Len] = 0x00;
                        fprintf(stdout, "Board ID is %s.\n", Buf);
                        break;
                case 1:
                        Buf[Len] = 0x00;
                        fprintf(stdout, "Model of the board is %s.\n", Buf);
                        break;
                case 2:
                        fprintf(stdout, "Revision of the board is 0x%02X.\n", Buf[0]);
                        break;
                case 3:
                        Buf[Len] = 0x00;
                        fprintf(stdout, "Serial number of the board is %s.\n", Buf);
                        break;
                case 4:
                        fprintf(stdout, "Temperature is %i or %i.\n", (Buf[0] << 8) + Buf[1], (Buf[1] << 8) + Buf[0]);
                        break;

        }

        close(file);

        return 0;
}