IEEE 802.15.4 subsystem
Sign in or create your account | Project List | Help
IEEE 802.15.4 subsystem Git Source Tree
Root/
Source at commit 33b0400 created 12 years 9 months ago. By Werner Almesberger, atrf-gpio: accept 'o' as an alias for 'l' | |
---|---|
1 | /* |
2 | * atrf-gpio/atusb.c - ATUSB-specific GPIO driver |
3 | * |
4 | * Written 2011 by Werner Almesberger |
5 | * Copyright 2011 Werner Almesberger |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | */ |
12 | |
13 | |
14 | #include <stdlib.h> |
15 | #include <stdio.h> |
16 | |
17 | #include <usb.h> |
18 | |
19 | #include "atusb/ep0.h" |
20 | #include "atrf.h" |
21 | |
22 | #include "atrf-gpio.h" |
23 | |
24 | |
25 | #define FROM_DEV ATUSB_FROM_DEV(0) |
26 | |
27 | |
28 | static const char *name[24] = { |
29 | "PB0", "PB1/P16", "PB2/P15", "PB3/P14", |
30 | "PB4/SLP_TR", "PB5", "PB6/LED", "PB7", |
31 | "PC0", "PC1/P13", "PC2", "PC3(NC)", |
32 | "PC4", "PC5", "PC6", "PC7/nRST_RF", |
33 | "PD0/IRQ_RF", "PD1/nSS", "PD2/MISO", "PD3/MOSI", |
34 | "PD4", "PD5/SCLK", "PD6", "PD7" |
35 | }; |
36 | |
37 | |
38 | static struct atrf_dsc *orig_dsc; |
39 | static uint8_t orig_data[3], orig_dir[3]; |
40 | |
41 | |
42 | static void dump_port(int port, uint8_t data, uint8_t dir, uint8_t mask) |
43 | { |
44 | int i; |
45 | |
46 | fprintf(stderr, "name\t\tcfg\n"); |
47 | for (i = 0; i != 8; i++) { |
48 | int bit = 1 << (i & 7); |
49 | |
50 | fprintf(stderr, "%-16s%c\n", name[port*8+i], |
51 | mask & bit ? dir & bit ? data & bit ? '1' : '0' : |
52 | data & bit ? 'R' : 'Z' : 'x'); |
53 | } |
54 | } |
55 | |
56 | |
57 | static uint8_t gpio(struct atrf_dsc *dsc, |
58 | uint8_t port, uint8_t *data, uint8_t *dir, uint8_t mask) |
59 | { |
60 | uint8_t buf[3]; |
61 | int res; |
62 | |
63 | res = usb_control_msg(atrf_usb_handle(dsc), |
64 | FROM_DEV, ATUSB_GPIO, *dir << 8 | *data, mask << 8 | port, |
65 | (void *) buf, sizeof(buf), 1000); |
66 | if (res < 0) { |
67 | dump_port(port-1, *data, *dir, mask); |
68 | fprintf(stderr, "ATUSB_GPIO: %s\n", usb_strerror()); |
69 | _exit(1); |
70 | } |
71 | if (res != 3) { |
72 | fprintf(stderr, "ATUSB_GPIO: expected 3 bytes, got %d\n", res); |
73 | _exit(1); |
74 | } |
75 | *data = buf[1]; |
76 | *dir = buf[2]; |
77 | return buf[0]; |
78 | } |
79 | |
80 | |
81 | static void dump(const uint8_t *data, const uint8_t *dir, |
82 | const uint8_t *expect, const uint8_t *got, const uint8_t *read) |
83 | { |
84 | int i; |
85 | |
86 | fprintf(stderr, "name\t\tcfg exp got\n"); |
87 | for (i = 0; i != 24; i++) { |
88 | int port = i >> 3; |
89 | int bit = 1 << (i & 7); |
90 | |
91 | fprintf(stderr, "%-16s%c %c %d", name[i], |
92 | dir[port] & bit ? data[port] & bit ? '1' : '0' : |
93 | data[port] & bit ? 'R' : 'Z', |
94 | read[port] & bit ? expect[port] & bit ? '1' : '0' : '-', |
95 | !!(got[port] & bit)); |
96 | if ((expect[port] ^ got[port]) & read[port] & bit) |
97 | fprintf(stderr, "\t***"); |
98 | fputc('\n', stderr); |
99 | } |
100 | } |
101 | |
102 | |
103 | /* ----- Decode and apply pattern ------------------------------------------ */ |
104 | |
105 | |
106 | static void restore_gpios(void) |
107 | { |
108 | int i; |
109 | |
110 | for (i = 0; i != 3; i++) |
111 | gpio(orig_dsc, i+1, orig_data+i, orig_dir+i, 0xff); |
112 | } |
113 | |
114 | |
115 | void do_atusb(struct atrf_dsc *dsc, const char *pattern, const char *next) |
116 | { |
117 | static int first = 1; |
118 | const char *p; |
119 | int i, pin = 0; |
120 | uint8_t data[3], dir[3], mask[3], read[3], expect[3], got[3]; |
121 | uint8_t bit; |
122 | int port; |
123 | |
124 | data[0] = data[1] = data[2] = 0; |
125 | dir[0] = dir[1] = dir[2] = 0; |
126 | mask[0] = mask[1] = mask[2] = 0; |
127 | read[0] = read[1] = read[2] = 0; |
128 | expect[0] = expect[1] = expect[2] = 0; |
129 | |
130 | if (first) { |
131 | orig_dsc = dsc; |
132 | for (i = 0; i != 3; i++) |
133 | gpio(dsc, i+1, orig_data+i, orig_dir+i, 0); |
134 | atexit(restore_gpios); |
135 | first = 0; |
136 | } |
137 | |
138 | for (p = pattern; *p; p++) { |
139 | bit = 1 << (pin & 7); |
140 | port = pin >> 3; |
141 | switch (*p) { |
142 | case '1': |
143 | data[port] |= bit; |
144 | /* fall through */ |
145 | case '0': |
146 | dir[port] |= bit; |
147 | mask[port] |= bit; |
148 | break; |
149 | case 'H': |
150 | expect[port] |= bit; |
151 | /* fall through */ |
152 | case 'L': |
153 | read[port] |= bit; |
154 | /* fall through */ |
155 | case 'Z': |
156 | data[port] |= bit; |
157 | mask[port] |= bit; |
158 | break; |
159 | case 'h': |
160 | expect[port] |= bit; |
161 | /* fall through */ |
162 | case 'l': |
163 | case 'o': |
164 | read[port] |= bit; |
165 | /* fall through */ |
166 | case 'z': |
167 | mask[port] |= bit; |
168 | break; |
169 | case 'x': |
170 | pin++; |
171 | continue; |
172 | default: |
173 | continue; |
174 | } |
175 | pin++; |
176 | } |
177 | |
178 | for (i = 0; i <= port; i++) |
179 | got[i] = gpio(dsc, i+1, data+i, dir+i, mask[i]); |
180 | for (i = 0; i <= port; i++) |
181 | if ((got[i] & read[i]) != expect[i]) { |
182 | dump(data, dir, expect, got, read); |
183 | fprintf(stderr, "at \"%s\", next \"%s\"\n", pattern, |
184 | next); |
185 | exit(1); |
186 | } |
187 | } |
188 |