source: umon/main/common/i2c.c @ 87db514

Last change on this file since 87db514 was 87db514, checked in by Amar Takhar <amar@…>, on 04/16/15 at 19:26:21

Initial commit of the umon repository.

Prior to this three changes were made:

  • Remove umon_ prefix from parent directories.
  • Collapse main/target/ into main/
  • Remove ports/template/flashtest.scr.ucon script.
  • Property mode set to 100644
File size: 3.8 KB
Line 
1/**************************************************************************
2 *
3 * Copyright (c) 2013 Alcatel-Lucent
4 *
5 * Alcatel Lucent licenses this file to You under the Apache License,
6 * Version 2.0 (the "License"); you may not use this file except in
7 * compliance with the License.  A copy of the License is contained the
8 * file LICENSE at the top level of this repository.
9 * You may also obtain a copy of the License at:
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 **************************************************************************
20 *
21 * i2c.c:
22 *
23 * This file contains all of the generic code to support a target
24 * with one or more I-Squared-C interface masters.
25 * It assumes that somewhere in target-specific space there are three
26 * i2c interface functions: i2cRead(), i2cWrite() and i2cCtrl().
27 *
28 * Original author:     Ed Sutter (ed.sutter@alcatel-lucent.com)
29 *
30 */
31#include "config.h"
32#include "cli.h"
33#include "i2c.h"
34#include "genlib.h"
35#include "stddefs.h"
36
37int     i2cVerbose;
38
39char *I2cHelp[] = {
40        "I-Squared-C Interface Access",
41        "[options] {init | show | read addr len | write addr data}",
42#if INCLUDE_VERBOSEHELP
43        "Options:",
44        " -b        burst",
45        " -i{##}    interface",
46        " -w{data}  pre-write data as part of read (uses repeated start)",
47        " -v        verbose",
48#endif
49        0,
50};
51
52int
53I2cCmd(int argc, char *argv[])
54{
55        char    *next;
56        uchar   buf[256];
57        int             i, wtot, opt, interface, len, rslt, addr, burst, ret, rwctrl;
58
59        rslt = 0;
60        wtot = 0;
61        burst = 0;
62        rwctrl = 0;
63        interface = 0;
64        i2cVerbose = 0;
65        ret = CMD_SUCCESS;
66        while ((opt=getopt(argc,argv,"bi:vw:")) != -1) {
67                switch(opt) {
68                case 'i':
69                        interface = atoi(optarg);
70                        break;
71                case 'b':
72                        burst = 1;              /* Do multiple accesses within one START/STOP */
73                        break;
74                case 'w':                       /* Pre-write in a read */
75                        burst = 1;
76                        rwctrl = REPEATED_START;
77                        for(wtot=1;wtot<sizeof(buf);wtot++) {
78                                buf[wtot] = (uchar)strtol(optarg,&next,0);
79                                if (*next == ',')
80                                        optarg = next+1;
81                                else
82                                        break;
83                        }
84                        buf[0] = wtot;
85                        break;
86                case 'v':
87                        i2cVerbose = 1;
88                        break;
89                default:
90                        return(CMD_FAILURE);
91                }
92        }
93
94        if (argc < (optind+1))
95                return(CMD_PARAM_ERROR);
96
97        if (strcmp(argv[optind],"read") == 0) {
98                if (argc != (optind+3))
99                        return(CMD_PARAM_ERROR);
100                addr = strtol(argv[optind+1],0,0);
101                len = strtol(argv[optind+2],0,0);
102                if (len > sizeof(buf))
103                        len = sizeof(buf);
104                if (burst) {
105                        addr |= rwctrl;
106                        rslt = i2cRead(interface,addr,buf,len);
107                }
108                else {
109                        for(i=0;i<len;i++) {
110                                rslt = i2cRead(interface,addr,buf+i,1);
111                                if (rslt < 0)
112                                        break;
113                        }
114                }
115                if (rslt < 0) {
116                        printf("i2cRead = %d\n",rslt);
117                        ret = CMD_FAILURE;
118                }
119                else
120                        printMem(buf,len,1);
121        }
122        else if (strcmp(argv[optind],"write") == 0) {
123                if (rwctrl == REPEATED_START) {
124                        printf("-w applies to read only\n");
125                        return(CMD_FAILURE);
126                }
127                if (argc < (optind+3))
128                        return(CMD_PARAM_ERROR);
129                addr = strtol(argv[optind+1],0,0);
130                for(len=0,i=optind+2;i<argc;i++,len++)
131                        buf[len] = (uchar)strtol(argv[i],0,0);
132
133                if (burst) {
134                        rslt = i2cWrite(interface,addr,buf,len);
135                }
136                else {
137                        for(i=0;i<len;i++) {
138                                rslt = i2cWrite(interface,addr,buf+i,1);
139                                if (rslt < 0)
140                                        break;
141                        }
142                }
143                if (rslt < 0) {
144                        printf("i2cWrite = %d\n",rslt);
145                        ret = CMD_FAILURE;
146                }
147        }
148        else if (strcmp(argv[optind],"init") == 0) {
149                i2cCtrl(interface,I2CCTRL_INIT,0,0);
150        }
151        else if (strcmp(argv[optind],"show") == 0) {
152                i2cCtrl(interface,I2CCTRL_SHOW,0,0);
153        }
154        else
155                return(CMD_PARAM_ERROR);
156
157        i2cVerbose = 0;
158        return(ret);
159}
Note: See TracBrowser for help on using the repository browser.