summaryrefslogtreecommitdiff
path: root/packages/i2c-tools/picodlp-control/i2c.c
blob: 6c752b3beed858fc8ec30e65129525e3bc04caaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/****************************************************************************
*
*   Copyright (c) 2006 Dave Hylands     <dhylands@gmail.com>
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License version 2 as
*   published by the Free Software Foundation.
*
*   Alternatively, this software may be distributed under the terms of BSD
*   license.
*
*   See README and COPYING for more details.
*
****************************************************************************/
/**
*
*   @file   i2c.c
*
*   @brief  This program allows basic i2c commands to be sent out the i2c
*           bus,
*
****************************************************************************/

// ---- Include Files -------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/timeb.h>

#include "i2c-dev.h"
#include "i2c-api.h"
#include "i2c-io-api.h"
#include "Log.h"

// #include "svn-version.h"

// ---- Public Variables ----------------------------------------------------
// ---- Private Constants and Types -----------------------------------------

// ---- Private Variables ---------------------------------------------------

enum
{
    OPT_MEM_DEFAULT     = 0,

    // Options assigned a single character code can use that charater code
    // as a short option.

    OPT_COUNT           = 'c',

    // Options from this point onwards don't have any short option equivalents

    OPT_FIRST_LONG_OPT   = 0x80,

    OPT_HELP,
    OPT_VERSION,
};

enum
{
    CMD_DEFAULT,

    CMD_READ_BYTE,
    CMD_READ_BYTE_2,
    CMD_READ_BYTE_4,

    CMD_WRITE_BYTE,
    CMD_WRITE_BYTE_2,
    CMD_WRITE_BYTE_4,

    CMD_RECV_BYTE,
    CMD_RECV_BYTE_2,
    CMD_RECV_BYTE_4,

    CMD_SEND_BYTE,
    CMD_SEND_BYTE_2,
    CMD_SEND_BYTE_4,
};

struct 
{
    int         cmd;
    const char *cmdStr;

} gCmdMap[] =
{
    { CMD_READ_BYTE,    "ReadByte" },
    { CMD_READ_BYTE,    "rb" },
    { CMD_READ_BYTE_2,  "ReadByte2" },
    { CMD_READ_BYTE_2,  "rb2" },
    { CMD_READ_BYTE_4,  "ReadByte4" },
    { CMD_READ_BYTE_4,  "rb4" },

    { CMD_WRITE_BYTE,   "WriteByte" },
    { CMD_WRITE_BYTE,   "wb" },
    { CMD_WRITE_BYTE_2, "WriteByte2" },
    { CMD_WRITE_BYTE_2, "wb2" },
    { CMD_WRITE_BYTE_4, "WriteByte4" },
    { CMD_WRITE_BYTE_4, "wb4" },

    { CMD_RECV_BYTE,    "RecvByte" },
    { CMD_RECV_BYTE,    "vb" },
    { CMD_RECV_BYTE_2,  "RecvByte2" },
    { CMD_RECV_BYTE_2,  "vb2" },
    { CMD_RECV_BYTE_4,  "RecvByte4" },
    { CMD_RECV_BYTE_4,  "vb4" },

    { CMD_SEND_BYTE,    "SendByte" },
    { CMD_SEND_BYTE,    "sb" },
    { CMD_SEND_BYTE_2,  "SendByte2" },
    { CMD_SEND_BYTE_2,  "sb2" },
    { CMD_SEND_BYTE_4,  "SendByte4" },
    { CMD_SEND_BYTE_4,  "sb4" },
};

int gNumCmds = sizeof( gCmdMap ) / sizeof( gCmdMap[ 0 ]);

int gI2cAddr    = -1;
int gByteCount  = 1;

const char *gCmdStr;
int         gCmd        = CMD_DEFAULT;
const char *gAddrStr    = NULL;
const char *gDataStr    = NULL;

struct option gOption[] =
{
    { "count",      required_argument,  NULL,           OPT_COUNT },
    { "version",    no_argument,        NULL,           OPT_VERSION },
    { "verbose",    no_argument,        &gVerbose,      1 },
    { "debug",      no_argument,        &gDebug,        1 },
    { "help",       no_argument,        NULL,           OPT_HELP },
    { NULL }
};

#define TRUE    1
#define FALSE   0

typedef enum
{
    NoADC,
    AllowADC,

} AllowADC_t;

#define ADC_PORT    8

// ---- Private Function Prototypes -----------------------------------------

static  int  ParseByte( const char *byteStr, uint8_t *byte );
static  int  ParseBytes( const char *byteStr, uint8_t *byte, uint8_t maxBytes, uint8_t *bytesParsed );

static  void ProcessReadByteCommand( int i2cDev, const char *addrStr );
static  void ProcessWriteByteCommand( int i2cDev, const char *addrStr, const char *dataStr );
static  void ProcessRecvByteCommand( int i2cDev );
static  void ProcessSendByteCommand( int i2cDev, const char *dataStr );
static  void Usage( void );

// ---- Functions -----------------------------------------------------------

//***************************************************************************
/**
*   Main entry point
*/

int main( int argc, char **argv )
{
    char                shortOptsStr[ sizeof( gOption ) / sizeof( gOption[ 0 ] ) + 1 ];
    char               *shortOpts = shortOptsStr;
    struct option       *scanOpt;
    int                 opt;
    const char         *i2cDevName = "/dev/i2c-3";
    int                 i2cDev;
    int                 cmdIdx;

    LogInit( stdout );

    // Figure out the short options from our options structure

    for ( scanOpt = gOption; scanOpt->name != NULL; scanOpt++ ) 
    {
        if (( scanOpt->flag == NULL ) && ( scanOpt->val < OPT_FIRST_LONG_OPT ))
        {
            *shortOpts++ = (char)scanOpt->val;

            if ( scanOpt->has_arg != no_argument )
            {
                *shortOpts++ = ':';
            }
        }
    }
    *shortOpts++ = '\0';

    // Parse the command line options

    while (( opt = getopt_long( argc, argv, shortOptsStr, gOption, NULL )) != -1 )
    {
        switch ( opt )
        {
            case 0: 
            {
                // getopt_long returns 0 for entries where flag is non-NULL

                break;
            }

            case OPT_COUNT:
            {
                gByteCount = (int)strtol( optarg, NULL, 0 );
                if ( gByteCount <= 0 )
                {
                    LogError( "Expecting byte count >= 0, found: '%s'\n", optarg );
                    Usage();
                    exit( 1 );
                }
                if ( gByteCount > I2C_MAX_DATA_LEN )
                {
                    LogError( "Max byte count supported: %d, found %d\n", I2C_MAX_DATA_LEN, gByteCount );
                    Usage();
                    exit( 1 );
                }
                break;
            }

//            case OPT_VERSION:
//            {
//                Log( "i2c SVN Revision: %d\n", SVN_REVISION );
//                exit( 0 );
//                break;
//            }

            case '?':
            case OPT_HELP:
            default:
            {
                LogError( "opt:%d\n", opt );
                Usage();
                exit( 1 );
            }
        }
    }
    argc -= optind;
    argv += optind;

    // Verify that an i2c-address was specified

    if ( argc < 1 )
    {
        LogError( "Must specify an i2c address\n\n" );
        Usage();
        exit( 1 );
    }
    gI2cAddr = strtol( argv[ 0 ], NULL, 0 );
    if (( gI2cAddr <= 0 ) || ( gI2cAddr > 127 ))
    {
        LogError( "Expecting i2c address in the range of 1-127, Found: %d\n", gI2cAddr );
        Usage();
        exit( 1 );
    }

    // Verify that a command has been specified

    if ( argc < 2 )
    {
        LogError( "Must specify a command\n" );
        Usage();
        exit( 1 );
    }
    gCmdStr = argv[ 1 ];
    for ( cmdIdx = 0; cmdIdx < gNumCmds; cmdIdx++ ) 
    {
        if ( strcasecmp( gCmdStr, gCmdMap[ cmdIdx ].cmdStr ) == 0 )
        {
            gCmd = gCmdMap[ cmdIdx ].cmd;
            break;
        }
    }
    if ( gCmd == CMD_DEFAULT )
    {
        LogError( "Unrecognized command '%s'\n", gCmdStr );
        exit( 1 );
    }

    // Process command specific arguments

    if (( gCmd == CMD_READ_BYTE_2  )
    ||  ( gCmd == CMD_WRITE_BYTE_2 )
    ||  ( gCmd == CMD_RECV_BYTE_2 )
    ||  ( gCmd == CMD_SEND_BYTE_2 ))
    {
        gByteCount = 2;
    }
    else
    if (( gCmd == CMD_READ_BYTE_4  )
    ||  ( gCmd == CMD_WRITE_BYTE_4 )
    ||  ( gCmd == CMD_RECV_BYTE_4 )
    ||  ( gCmd == CMD_SEND_BYTE_4 ))
    {
        gByteCount = 4;
    }

    if (( gCmd == CMD_READ_BYTE )
    ||  ( gCmd == CMD_READ_BYTE_2 )
    ||  ( gCmd == CMD_READ_BYTE_4 ))
    {
        if ( argc < 3 )
        {
            LogError( "Expecting address\n" );
            Usage();
            exit( 1 );
        }
        if ( argc > 3 )
        {
            LogError( "Unexpected extra parameters\n" );
            Usage();
            exit( 1 );
        }

        gAddrStr = argv[ 2 ];
    }
    else
    if (( gCmd == CMD_WRITE_BYTE )
    ||  ( gCmd == CMD_WRITE_BYTE_2 )
    ||  ( gCmd == CMD_WRITE_BYTE_4 ))
    {
        if ( argc < 4 )
        {
            LogError( "Expecting address and data\n" );
            Usage();
            exit( 1 );
        }
        if ( argc > 4 )
        {
            LogError( "Unexpected extra parameters\n" );
            Usage();
            exit( 1 );
        }

        gAddrStr = argv[ 2 ];
        gDataStr = argv[ 3 ];
    }
    else
    if (( gCmd == CMD_RECV_BYTE )
    ||  ( gCmd == CMD_RECV_BYTE_2 )
    ||  ( gCmd == CMD_RECV_BYTE_4 ))
    {
        if ( argc > 2 )
        {
            LogError( "Unexpected extra parameters\n" );
            Usage();
            exit( 1 );
        }
    }
    else
    if (( gCmd == CMD_SEND_BYTE )
    ||  ( gCmd == CMD_SEND_BYTE_2 )
    ||  ( gCmd == CMD_SEND_BYTE_4 ))
    {
        if ( argc < 3 )
        {
            LogError( "Expecting data\n" );
            Usage();
            exit( 1 );
        }
        if ( argc > 3 )
        {
            LogError( "Unexpected extra parameters\n" );
            Usage();
            exit( 1 );
        }
        gDataStr = argv[ 2 ];
    }

    if ( gDebug )
    {
        Log( "i2cAddr:0x%02x Cmd: %s (%d)", gI2cAddr, gCmdStr, gCmd );
        if ( gAddrStr != NULL )
        {
            Log( " Addr: %s", gAddrStr );
        }
        if ( gDataStr != NULL )
        {
            Log( " Data: %s", gDataStr );
        }
        Log( "\n" );
    }

    // Try to open the i2c device

    if (( i2cDev = open( i2cDevName, O_RDWR )) < 0 )
    {
        LogError( "Error  opening '%s': %s\n", i2cDevName, strerror( errno ));
        exit( 1 );
    }

    // Indicate which slave we wish to speak to

    I2cSetSlaveAddress( i2cDev, gI2cAddr, I2C_NO_CRC );

    switch ( gCmd )
    {
        case CMD_READ_BYTE:
        case CMD_READ_BYTE_2:
        case CMD_READ_BYTE_4:
        {
            ProcessReadByteCommand( i2cDev, gAddrStr );
            break;
        }

        case CMD_WRITE_BYTE:
        case CMD_WRITE_BYTE_2:
        case CMD_WRITE_BYTE_4:
        {
            ProcessWriteByteCommand( i2cDev, gAddrStr, gDataStr );
            break;
        }

        case CMD_RECV_BYTE:
        case CMD_RECV_BYTE_2:
        case CMD_RECV_BYTE_4:
        {
            ProcessRecvByteCommand( i2cDev );
            break;
        }

        case CMD_SEND_BYTE:
        case CMD_SEND_BYTE_2:
        case CMD_SEND_BYTE_4:
        {
            ProcessSendByteCommand( i2cDev, gDataStr );
            break;
        }
    }

    close( i2cDev );

    return 0;

} // main

//***************************************************************************
/**
*   Parse a string looking for a single byte.
*/

int ParseByte( const char *byteStr, uint8_t *byte )
{
    char       *endPtr;

    *byte = (uint8_t)strtol( byteStr, &endPtr, 0 );

    if ( *endPtr != '\0' )
    {
        LogError( "Expecting numeric value, found '%s'\n", byteStr );
        return FALSE;
    }

    return TRUE;

} // ParseByte

//***************************************************************************
/**
*   Parse a string looking for an array of bytes.
*/

int ParseBytes( const char *byteStr, uint8_t *byte, uint8_t maxBytes, uint8_t *bytesParsed )
{
    char       *endPtr;

    if (( byteStr[ 0 ] == '0' ) && ( byteStr[ 1 ] == 'x' ))
    {
        const char *s = &byteStr[ 2 ];
        *bytesParsed = 0;

        // Could be a multi-byte hex string

        while ( *s != '\0' )
        {
            if ( *bytesParsed >= maxBytes )
            {
                LogError( "Too many bytes, max: %d\n", maxBytes );
                return FALSE;
            }

            (*bytesParsed)++;
            *byte = 0;

            if (( *s >= 'A' ) && ( *s <= 'F' ))
            {
                *byte = *s - 'A' + 10;
            }
            else
            if (( *s >= 'a' ) && ( *s <= 'f' ))
            {
                *byte = *s - 'a' + 10;
            }
            else
            if (( *s >= '0' ) && ( *s <= '9' ))
            {
                *byte = *s - '0';
            }
            else
            {
                LogError( "Expecting hex digit, found '%c'\n", *s );
                return FALSE;
            }
            s++;

            if ( *s == '\0' )
            {
                break;
            }

            *byte <<= 4;
            if (( *s >= 'A' ) && ( *s <= 'F' ))
            {
                *byte |= *s - 'A' + 10;
            }
            else
            if (( *s >= 'a' ) && ( *s <= 'f' ))
            {
                *byte |= *s - 'a' + 10;
            }
            else
            if (( *s >= '0' ) && ( *s <= '9' ))
            {
                *byte |= *s - '0';
            }
            else
            {
                LogError( "Expecting hex digit, found '%c'\n", *s );
                return FALSE;
            }
            s++;
            byte++;
        }
    }
    else
    {
        // It's decimal or octal - only a single byte

        *byte = (uint8_t)strtol( byteStr, &endPtr, 0 );

        if ( *endPtr != '\0' )
        {
            LogError( "Expecting numeric value, found '%s'\n", byteStr );
            return FALSE;
        }
        *bytesParsed = 1;
    }

    return TRUE;

} // ParseBytes

//***************************************************************************
/**
*   Issues a read byte command to read a byte from a particular address.
*/

void ProcessReadByteCommand( int i2cDev, const char *addrStr )
{
    uint8_t addr;
    uint8_t dataByte[ I2C_MAX_DATA_LEN ];
    int     rc;
    int     i;

    if ( !ParseByte( addrStr, &addr ))
    {
        return;
    }

    if (( rc = I2cReadBytes( i2cDev, addr, dataByte, gByteCount )) != 0 )
    {
        LogError( "I2cReadByte failed: %d\n", rc );
        return;
    }

    Log( "0x", dataByte[0] );

    for ( i = 0; i < gByteCount; i++ ) 
    {
        Log( "%02x", dataByte[i] );
    }
    Log( "\n" );

} // ProcessReadByteCommand

//***************************************************************************
/**
*   Issues a recv byte command to read bytes with no address.
*/

void ProcessRecvByteCommand( int i2cDev )
{
    uint8_t dataByte[ I2C_MAX_DATA_LEN ];
    int     rc;
    int     i;

    if (( rc = I2cReceiveBytes( i2cDev, dataByte, gByteCount )) != 0 )
    {
        LogError( "I2cRecvBytes failed: %d\n", rc );
        return;
    }

    Log( "0x", dataByte[0] );

    for ( i = 0; i < gByteCount; i++ ) 
    {
        Log( "%02x", dataByte[i] );
    }
    Log( "\n" );

} // ProcessRecvByteCommand

//***************************************************************************
/**
*   Issues a write byte command to write a byte to a particular address.
*/

void ProcessWriteByteCommand( int i2cDev, const char *addrStr, const char *dataStr )
{
    uint8_t addr;
    uint8_t dataByte[ I2C_MAX_DATA_LEN ];
    uint8_t bytesParsed;
    int     rc;

    if ( !ParseByte( addrStr, &addr ))
    {
        return;
    }

    if ( !ParseBytes( dataStr, dataByte, sizeof( dataByte ), &bytesParsed ))
    {
        return;
    }

    if (( rc = I2cWriteBytes( i2cDev, addr, dataByte, bytesParsed )) != 0 )
    {
        LogError( "I2cWriteBytes failed: %d\n", rc );
        return;
    }

} // ProcessWriteByteCommand

//***************************************************************************
/**
*   Issues a send byte command to write bytes with no address specified.
*/

void ProcessSendByteCommand( int i2cDev, const char *dataStr )
{
    uint8_t dataByte[ I2C_MAX_DATA_LEN ];
    uint8_t bytesParsed;
    int     rc;

    if ( !ParseBytes( dataStr, dataByte, sizeof( dataByte ), &bytesParsed ))
    {
        return;
    }

    if (( rc = I2cSendBytes( i2cDev, dataByte, bytesParsed )) != 0 )
    {
        LogError( "I2cSendBytes failed: %d\n", rc );
        return;
    }

} // ProcessSendByteCommand

//***************************************************************************
/**
*   Usage
*/

void Usage( void )
{
    fprintf( stderr, "Usage: i2c [options] i2c-addr cmd [cmd-arguments]\n" );
    fprintf( stderr, "Send I2C commands\n" );
    fprintf( stderr, "\n" );
    fprintf( stderr, "The following commands are supported:\n" );
    fprintf( stderr, "ReadByte addr         Retrieves byte(s) starting at the indicated address\n" );
    fprintf( stderr, "WriteByte addr data   Write byte(s) starting at the indicated address\n" );
    fprintf( stderr, "ReadByte2 addr        Retrieves two bytes from the indicated address\n" );
    fprintf( stderr, "WriteByte2 addr data  Writes two bytes into the indicated address\n" );
    fprintf( stderr, "ReadByte4 addr        Retrieves four bytes from the indicated address\n" );
    fprintf( stderr, "WriteByte4 addr data  Writes four bytes into the indicated address\n" );
    fprintf( stderr, "RecvByte              Retrieves byte(s)(no address specified)\n" );
    fprintf( stderr, "SendByte data         Writes byte(s)(no address specified)\n" );
    fprintf( stderr, "RecvByte2             Retrieves 2 bytes (no address specified)\n" );
    fprintf( stderr, "SendByte2 data        Writes 2 bytes(no address specified)\n" );
    fprintf( stderr, "RecvByte4             Retrieves 4 bytes (no address specified)\n" );
    fprintf( stderr, "SendByte4 data        Writes 4 bytes(no address specified)\n" );
    fprintf( stderr, "\n" );
    fprintf( stderr, "The above commands can be shortened to rb, wb, rb2, wb2, rb4, wb4, vb, sd, vb2 sb2 vb4, and sb4 \n" );
    fprintf( stderr, "respectively.\n" );
    fprintf( stderr, "\n" );
    fprintf( stderr, "The following options may be used:\n" );
    fprintf( stderr, "--count=n             Specifies how many bytes to read for ReadByte or RecvByte\n" );
    fprintf( stderr, "--version             Prints the SVN version of this program\n" );
    fprintf( stderr, "--verbose             Print additional information\n" );
    fprintf( stderr, "--help                Prints this information\n" );
}