Blame view

RIOT/tests/periph_gpio/main.c 5.36 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
  /*
   * 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       Manual test application for GPIO peripheral drivers
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   *
   * @}
   */
  
  #include <stdio.h>
  #include <stdlib.h>
  
  #include "shell.h"
  #include "periph/gpio.h"
  
  static void cb(void *arg)
  {
      printf("INT: external interrupt from pin %i\n", (int)arg);
  }
  
  static int init_pin(int argc, char **argv, gpio_mode_t mode)
  {
      int po, pi;
  
      if (argc < 3) {
          printf("usage: %s <port> <pin>\n", argv[0]);
          return 1;
      }
  
      po = atoi(argv[1]);
      pi = atoi(argv[2]);
  
      if (gpio_init(GPIO_PIN(po, pi), mode) < 0) {
          printf("Error to initialize GPIO_PIN(%i, %02i)\n", po, pi);
          return 1;
      }
  
      return 0;
  }
  
  static int init_out(int argc, char **argv)
  {
      return init_pin(argc, argv, GPIO_OUT);
  }
  
  static int init_in(int argc, char **argv)
  {
      return init_pin(argc, argv, GPIO_IN);
  }
  
  static int init_in_pu(int argc, char **argv)
  {
      return init_pin(argc, argv, GPIO_IN_PU);
  }
  
  static int init_in_pd(int argc, char **argv)
  {
      return init_pin(argc, argv, GPIO_IN_PD);
  }
  
  static int init_od(int argc, char **argv)
  {
      return init_pin(argc, argv, GPIO_OD);
  }
  
  static int init_od_pu(int argc, char **argv)
  {
      return init_pin(argc, argv, GPIO_OD_PU);
  }
  
  static int init_int(int argc, char **argv)
  {
      int po, pi;
      gpio_mode_t mode = GPIO_IN;
      gpio_flank_t flank;
      int fl;
  
      if (argc < 4) {
          printf("usage: %s <port> <pin> <flank> [pull_config]\n", argv[0]);
          puts("\tflank:\n"
               "\t0: falling\n"
               "\t1: rising\n"
               "\t2: both\n"
               "\tpull_config:\n"
               "\t0: no pull resistor (default)\n"
               "\t1: pull up\n"
               "\t2: pull down");
          return 1;
      }
  
      po = atoi(argv[1]);
      pi = atoi(argv[2]);
  
      fl = atoi(argv[3]);
      switch (fl) {
          case 0:
              flank = GPIO_FALLING;
              break;
          case 1:
              flank = GPIO_RISING;
              break;
          case 2:
              flank = GPIO_BOTH;
              break;
          default:
              puts("error: invalid value for active flank");
              return 1;
      }
  
      if (argc >= 5) {
          int pr = atoi(argv[4]);
          switch (pr) {
              case 0:
                  mode = GPIO_IN;
                  break;
              case 1:
                  mode = GPIO_IN_PU;
                  break;
              case 2:
                  mode = GPIO_IN_PD;
                  break;
              default:
                  puts("error: invalid pull resistor option");
                  return 1;
          }
      }
  
      if (gpio_init_int(GPIO_PIN(po, pi), mode, flank, cb, (void *)pi) < 0) {
          printf("error: init_int of GPIO_PIN(%i, %i) failed\n", po, pi);
          return 1;
      }
      printf("GPIO_PIN(%i, %i) successfully initialized as ext int\n", po, pi);
  
      return 0;
  }
  
  static int read(int argc, char **argv)
  {
      int port, pin;
  
      if (argc < 3) {
          printf("usage: %s <port> <pin>\n", argv[0]);
          return 1;
      }
  
      port = atoi(argv[1]);
      pin = atoi(argv[2]);
  
      if (gpio_read(GPIO_PIN(port, pin))) {
          printf("GPIO_PIN(%i.%02i) is HIGH\n", port, pin);
      }
      else {
          printf("GPIO_PIN(%i.%02i) is LOW\n", port, pin);
      }
  
      return 0;
  }
  
  static int set(int argc, char **argv)
  {
      if (argc < 3) {
          printf("usage: %s <port> <pin>\n", argv[0]);
          return 1;
      }
  
      gpio_set(GPIO_PIN(atoi(argv[1]), atoi(argv[2])));
  
      return 0;
  }
  
  static int clear(int argc, char **argv)
  {
      if (argc < 3) {
          printf("usage: %s <port> <pin>\n", argv[0]);
          return 1;
      }
  
      gpio_clear(GPIO_PIN(atoi(argv[1]), atoi(argv[2])));
  
      return 0;
  }
  
  static int toggle(int argc, char **argv)
  {
      if (argc < 3) {
          printf("usage: %s <port> <pin>\n", argv[0]);
          return 1;
      }
  
      gpio_toggle(GPIO_PIN(atoi(argv[1]), atoi(argv[2])));
  
      return 0;
  }
  
  static const shell_command_t shell_commands[] = {
      { "init_out", "init as output (push-pull mode)", init_out },
      { "init_in", "init as input w/o pull resistor", init_in },
      { "init_in_pu", "init as input with pull-up", init_in_pu },
      { "init_in_pd", "init as input with pull-down", init_in_pd },
      { "init_od", "init as output (open-drain without pull resistor)", init_od },
      { "init_od_pu", "init as output (open-drain with pull-up)", init_od_pu },
      { "init_int", "init as external INT w/o pull resistor", init_int },
      { "read", "read pin status", read },
      { "set", "set pin to HIGH", set },
      { "clear", "set pin to LOW", clear },
      { "toggle", "toggle pin", toggle },
      { NULL, NULL, NULL }
  };
  
  int main(void)
  {
      puts("GPIO peripheral driver test\n");
      puts("In this test, pins are specified by integer port and pin numbers.\n"
           "So if your platform has a pin PA01, it will be port=0 and pin=1,\n"
           "PC14 would be port=2 and pin=14 etc.\n\n"
           "NOTE: make sure the values you use exist on your platform! The\n"
           "      behavior for not existing ports/pins is not defined!");
  
      /* start the shell */
      char line_buf[SHELL_DEFAULT_BUFSIZE];
      shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
  
      return 0;
  }