Blame view

RIOT/tests/periph_i2c/main.c 6.89 KB
a752c7ab   elopes   add first test an...
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
  /*
   * Copyright (C) 2014 Freie Universitรคt Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup tests
   * @{
   *
   * @file
   * @brief       Test application for the low-level I2C peripheral driver
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   *
   * @}
   */
  
  #include <stdio.h>
  #include <stdlib.h>
  
  #include "periph_conf.h"
  #include "periph/i2c.h"
  #include "shell.h"
  
  #define BUFSIZE        (128U)
  
  static int i2c_dev = -1;
  
  int cmd_init_master(int argc, char **argv)
  {
      int dev, speed, res;
  
      if (argc != 3) {
          puts("Error: Init: Invalid number of arguments!");
          printf("Usage:\n%s: [DEVICE] [SPEED]\n", argv[0]);
          puts("    with DEVICE:");
          for (int i = 0; i < I2C_NUMOF; i++) {
              printf("          %i -> I2C_%i\n", i, i);
          }
          puts("         SPEED:");
          puts("          0 -> SPEED_LOW (10kbit/s)");
          puts("          1 -> SPEED_NORMAL (100kbit/s)");
          puts("          2 -> SPEED_FAST (400kbit/s)");
          puts("          3 -> SPEED_FAST_PLUS (1Mbit/s)");
          puts("          4 -> SPEED_HIGH (3.4Mbit/s)\n");
          return 1;
      }
  
      dev = atoi(argv[1]);
      speed = atoi(argv[2]);
  
      res = i2c_init_master(dev, speed);
      if (res == -1) {
          puts("Error: Init: Given device not available");
          return 1;
      }
      else if (res == -2) {
          puts("Error: Init: Unsupported speed value");
          return 1;
      }
      else {
          printf("I2C_%i successfully initialized as master!\n", dev);
          i2c_dev = dev;
      }
  
      return 0;
  }
  
  int cmd_write(int argc, char **argv)
  {
      int res;
      uint8_t addr;
      int length = argc - 2;
      uint8_t data[BUFSIZE];
  
      if (i2c_dev < 0) {
          puts("Error: no I2C device was initialized");
          return 1;
      }
      if (argc < 3) {
          puts("Error: not enough arguments given");
          printf("Usage:\n%s: ADDR BYTE0 [BYTE1 [BYTE_n [...]]]\n", argv[0]);
          return 1;
      }
  
      addr = atoi(argv[1]);
      for (int i = 0; i < length; i++) {
          data[i] = atoi(argv[i + 2]);
      }
  
      if (length == 1) {
          printf("i2c_write_byte(I2C_%i, 0x%02x, 0x%02x)\n", i2c_dev, addr, data[0]);
          res = i2c_write_byte(i2c_dev, addr, data[0]);
      }
      else {
          printf("i2c_write_bytes(I2C_%i, 0x%02x, [", i2c_dev, addr);
          for (int i = 0; i < length; i++) {
              printf(", 0x%02x", data[i]);
          }
          puts("])");
          res = i2c_write_bytes(i2c_dev, addr, data, length);
      }
  
      if (res < 0) {
          puts("Error: no bytes were written");
          return 1;
      }
      else {
          printf("I2C_%i: successfully wrote %i bytes to the bus\n", i2c_dev, res);
          return 0;
      }
  }
  
  int cmd_write_reg(int argc, char **argv)
  {
      int res;
      uint8_t addr, reg;
      int length = argc - 3;
      uint8_t data[BUFSIZE];
  
      if (i2c_dev < 0) {
          puts("Error: no I2C device initialized");
          return 1;
      }
      if (length < 1) {
          puts("Error: not enough arguments given");
          printf("Usage:\n%s ADDR REG BYTE0 [BYTE1 ...]\n", argv[0]);
          return 1;
      }
  
      addr = atoi(argv[1]);
      reg = atoi(argv[2]);
      for (int i = 0; i < length; i++) {
          data[i] = atoi(argv[i + 3]);
      }
  
      if (length == 1) {
          printf("i2c_write_reg(I2C_%i, 0x%02x, 0x%02x, 0x%02x)\n",
                 i2c_dev, addr, reg, data[0]);
          res = i2c_write_reg(i2c_dev, addr, reg, data[0]);
      }
      else {
          printf("i2c_write_regs(I2C_%i, 0x%02x, 0x%02x, [", i2c_dev, addr, reg);
          for (int i = 0; i < length; i++) {
              printf("0x%02x, ", data[i]);
          }
          puts("])");
          res = i2c_write_regs(i2c_dev, addr, reg, data, length);
      }
  
      if (res < 1) {
          puts("Error: no bytes were written");
          return 1;
      }
      else {
          printf("I2C_%i: successfully wrote %i bytes to register 0x%02x\n", i2c_dev, res, reg);
          return 0;
      }
  }
  
  int cmd_read(int argc, char **argv)
  {
      int res;
      uint8_t addr;
      int length;
      uint8_t data[BUFSIZE];
  
      if (i2c_dev < 0) {
          puts("Error: no I2C device initialized");
          return 1;
      }
      if (argc < 3) {
          puts("Error: not enough arguments given");
          printf("Usage:\n%s ADDR LENGTH\n", argv[0]);
          return 1;
      }
  
      addr = atoi(argv[1]);
      length = atoi(argv[2]);
  
      if (length < 1 || length > BUFSIZE) {
          puts("Error: invalid LENGTH parameter given\n");
          return 1;
      }
      else if (length == 1) {
          printf("i2c_read_byte(I2C_%i, 0x%02x)\n", i2c_dev, addr);
          res = i2c_read_byte(i2c_dev, addr, data);
      }
      else {
          printf("i2c_read_bytes(I2C_%i, 0x%02x, %i)\n", i2c_dev, addr, length);
          res = i2c_read_bytes(i2c_dev, addr, data, length);
      }
  
      if (res < 1) {
          puts("Error: no bytes were read");
          return 1;
      }
      else {
          printf("I2C_%i: successfully read %i bytes:\n  [", i2c_dev, res);
          for (int i = 0; i < res; i++) {
              printf("0x%02x, ", data[i]);
          }
          puts("]");
          return 0;
      }
  }
  
  int cmd_read_reg(int argc, char **argv)
  {
      int res;
      uint8_t addr, reg;
      int length;
      uint8_t data[BUFSIZE];
  
      if (i2c_dev < 0) {
          puts("Error: no I2C device initialized");
          return 1;
      }
      if (argc < 4) {
          puts("Error: not enough arguments given");
          printf("Usage:\n%s ADDR REG LENGTH\n", argv[0]);
          return 1;
      }
  
      addr = atoi(argv[1]);
      reg = atoi(argv[2]);
      length = atoi(argv[3]);
  
      if (length < 1 || length > BUFSIZE) {
          puts("Error: invalid LENGTH parameter given");
          return 1;
      }
      else if (length == 1) {
          printf("i2c_read_reg(I2C_%i, 0x%02x, 0x%02x)\n", i2c_dev, addr, reg);
          res = i2c_read_reg(i2c_dev, addr, reg, data);
      }
      else {
          printf("i2c_read_regs(I2C_%i, 0x%02x, 0x%02x, %i)\n", i2c_dev, addr, reg, length);
          res = i2c_read_regs(i2c_dev, addr, reg, data, length);
      }
  
      if (res < 1) {
          puts("Error: no bytes were read");
          return 1;
      }
      else {
          printf("I2C_%i: successfully read %i bytes from reg 0x%02x:\n  [", i2c_dev, res, reg);
          for (int i = 0; i < res; i++) {
              printf("0x%02x, ", data[i]);
          }
          puts("]");
          return 0;
      }
  
  }
  
  static const shell_command_t shell_commands[] = {
      { "init_master", "Initialize I2C as master", cmd_init_master },
      { "w", "write bytes to given address", cmd_write },
      { "wr", "write to register ", cmd_write_reg },
      { "r", "read bytes from given address", cmd_read },
      { "rr", "read bytes from register", cmd_read_reg },
      { NULL, NULL, NULL }
  };
  
  int main(void)
  {
      puts("Test for the low-level I2C driver");
  
      /* define own shell commands */
      char line_buf[SHELL_DEFAULT_BUFSIZE];
      shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
  
      return 0;
  }