|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#include <irq.h> |
| 6 | +#include <uart.h> |
| 7 | +#include <generated/csr.h> |
| 8 | +#include <console.h> |
| 9 | + |
| 10 | +#include "test_mode.h" |
| 11 | + |
| 12 | +static void leds(char *value) |
| 13 | +{ |
| 14 | + char *c; |
| 15 | + unsigned int value2; |
| 16 | + |
| 17 | + if(*value == 0) { |
| 18 | + printf("leds <value>\n"); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + value2 = strtoul(value, &c, 0); |
| 23 | + if(*c != 0) { |
| 24 | + printf("incorrect value\n"); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + leds_out_write(value2); |
| 29 | +} |
| 30 | + |
| 31 | +#if 0 |
| 32 | +static void inputs(void) |
| 33 | +{ |
| 34 | + int inp; |
| 35 | + |
| 36 | + inp = test_inputs_in_read(); |
| 37 | + printf("0x%02x\n", inp); |
| 38 | +} |
| 39 | + |
| 40 | +static void ttlout(char *value) |
| 41 | +{ |
| 42 | + char *c; |
| 43 | + unsigned int value2; |
| 44 | + |
| 45 | + if(*value == 0) { |
| 46 | + printf("ttlout <value>\n"); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + value2 = strtoul(value, &c, 0); |
| 51 | + if(*c != 0) { |
| 52 | + printf("incorrect value\n"); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + test_ttl_oe_write(0x3); |
| 57 | + test_ttl_o_write(value2); |
| 58 | +} |
| 59 | + |
| 60 | +static void ttlin(void) |
| 61 | +{ |
| 62 | + test_ttl_oe_write(0x0); |
| 63 | + printf("0x%04x\n", test_ttl_i_read()); |
| 64 | +} |
| 65 | + |
| 66 | +#define DDS_REG(x) MMPTR(0xb0000000 + 4*(x)) |
| 67 | +#define DDS_FUD DDS_REG(64) |
| 68 | +#define DDS_GPIO DDS_REG(65) |
| 69 | + |
| 70 | +static void ddssel(char *n) |
| 71 | +{ |
| 72 | + char *c; |
| 73 | + unsigned int n2; |
| 74 | + |
| 75 | + if(*n == 0) { |
| 76 | + printf("ddssel <n>\n"); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + n2 = strtoul(n, &c, 0); |
| 81 | + if(*c != 0) { |
| 82 | + printf("incorrect number\n"); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + DDS_GPIO = n2; |
| 87 | +} |
| 88 | + |
| 89 | +static void ddsw(char *addr, char *value) |
| 90 | +{ |
| 91 | + char *c; |
| 92 | + unsigned int addr2, value2; |
| 93 | + |
| 94 | + if((*addr == 0) || (*value == 0)) { |
| 95 | + printf("ddsr <addr> <value>\n"); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + addr2 = strtoul(addr, &c, 0); |
| 100 | + if(*c != 0) { |
| 101 | + printf("incorrect address\n"); |
| 102 | + return; |
| 103 | + } |
| 104 | + value2 = strtoul(value, &c, 0); |
| 105 | + if(*c != 0) { |
| 106 | + printf("incorrect value\n"); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + DDS_REG(addr2) = value2; |
| 111 | +} |
| 112 | + |
| 113 | +static void ddsr(char *addr) |
| 114 | +{ |
| 115 | + char *c; |
| 116 | + unsigned int addr2; |
| 117 | + |
| 118 | + if(*addr == 0) { |
| 119 | + printf("ddsr <addr>\n"); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + addr2 = strtoul(addr, &c, 0); |
| 124 | + if(*c != 0) { |
| 125 | + printf("incorrect address\n"); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + printf("0x%02x\n", DDS_REG(addr2)); |
| 130 | +} |
| 131 | + |
| 132 | +static void ddsfud(void) |
| 133 | +{ |
| 134 | + DDS_FUD = 0; |
| 135 | +} |
| 136 | + |
| 137 | +static void ddsftw(char *n, char *ftw) |
| 138 | +{ |
| 139 | + char *c; |
| 140 | + unsigned int n2, ftw2; |
| 141 | + |
| 142 | + if((*n == 0) || (*ftw == 0)) { |
| 143 | + printf("ddsftw <n> <ftw>\n"); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + n2 = strtoul(n, &c, 0); |
| 148 | + if(*c != 0) { |
| 149 | + printf("incorrect number\n"); |
| 150 | + return; |
| 151 | + } |
| 152 | + ftw2 = strtoul(ftw, &c, 0); |
| 153 | + if(*c != 0) { |
| 154 | + printf("incorrect value\n"); |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + DDS_GPIO = n2; |
| 159 | + DDS_REG(0x0a) = ftw2 & 0xff; |
| 160 | + DDS_REG(0x0b) = (ftw2 >> 8) & 0xff; |
| 161 | + DDS_REG(0x0c) = (ftw2 >> 16) & 0xff; |
| 162 | + DDS_REG(0x0d) = (ftw2 >> 24) & 0xff; |
| 163 | + DDS_FUD = 0; |
| 164 | +} |
| 165 | + |
| 166 | +static void ddsreset(void) |
| 167 | +{ |
| 168 | + DDS_GPIO |= 1 << 7; |
| 169 | + DDS_GPIO &= ~(1 << 7); |
| 170 | +} |
| 171 | + |
| 172 | +static void ddsinit(void) |
| 173 | +{ |
| 174 | + ddsreset(); |
| 175 | + DDS_REG(0x00) = 0x78; |
| 176 | + DDS_REG(0x01) = 0x00; |
| 177 | + DDS_REG(0x02) = 0x00; |
| 178 | + DDS_REG(0x03) = 0x00; |
| 179 | + ddsfud(); |
| 180 | +} |
| 181 | + |
| 182 | +static void ddstest_one(unsigned int i) |
| 183 | +{ |
| 184 | + unsigned int v[12] = { |
| 185 | + 0xaaaaaaaa, 0x55555555, 0xa5a5a5a5, 0x5a5a5a5a, |
| 186 | + 0x00000000, 0xffffffff, 0x12345678, 0x87654321, |
| 187 | + 0x0000ffff, 0xffff0000, 0x00ff00ff, 0xff00ff00, |
| 188 | + }; |
| 189 | + unsigned int f, g, j; |
| 190 | + |
| 191 | + DDS_GPIO = i; |
| 192 | + ddsinit(); |
| 193 | + |
| 194 | + for(j=0; j<12; j++) { |
| 195 | + f = v[j]; |
| 196 | + DDS_REG(0x0a) = f & 0xff; |
| 197 | + DDS_REG(0x0b) = (f >> 8) & 0xff; |
| 198 | + DDS_REG(0x0c) = (f >> 16) & 0xff; |
| 199 | + DDS_REG(0x0d) = (f >> 24) & 0xff; |
| 200 | + DDS_FUD = 0; |
| 201 | + g = DDS_REG(0x0a); |
| 202 | + g |= DDS_REG(0x0b) << 8; |
| 203 | + g |= DDS_REG(0x0c) << 16; |
| 204 | + g |= DDS_REG(0x0d) << 24; |
| 205 | + if(g != f) |
| 206 | + printf("readback fail on DDS %d, 0x%08x != 0x%08x\n", i, g, f); |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +static void ddstest(char *n) |
| 211 | +{ |
| 212 | + int i, j; |
| 213 | + char *c; |
| 214 | + unsigned int n2; |
| 215 | + |
| 216 | + if (*n == 0) { |
| 217 | + printf("ddstest <cycles>\n"); |
| 218 | + return; |
| 219 | + } |
| 220 | + n2 = strtoul(n, &c, 0); |
| 221 | + |
| 222 | + for(i=0; i<n2; i++) { |
| 223 | + for(j=0; j<8; j++) { |
| 224 | + ddstest_one(j); |
| 225 | + } |
| 226 | + } |
| 227 | +} |
| 228 | +#endif |
| 229 | + |
| 230 | +static void help(void) |
| 231 | +{ |
| 232 | + puts("ARTIQ DDS/TTL Tester"); |
| 233 | + puts("Available commands:"); |
| 234 | + puts("help - this message"); |
| 235 | + puts("inputs - read inputs"); |
| 236 | + puts("ttlout <n> - output ttl"); |
| 237 | + puts("ttlin - read ttl"); |
| 238 | + puts("ddssel <n> - select a dds"); |
| 239 | + puts("ddsinit - reset, cfr, fud dds"); |
| 240 | + puts("ddsreset - reset dds"); |
| 241 | + puts("ddsw <a> <d> - write to dds register"); |
| 242 | + puts("ddsr <a> - read dds register"); |
| 243 | + puts("ddsfud - pulse FUD"); |
| 244 | + puts("ddsftw <n> <d> - write FTW"); |
| 245 | + puts("ddstest <n> - perform test sequence on dds"); |
| 246 | + puts("leds <n> - set leds"); |
| 247 | +} |
| 248 | + |
| 249 | +static void readstr(char *s, int size) |
| 250 | +{ |
| 251 | + char c[2]; |
| 252 | + int ptr; |
| 253 | + |
| 254 | + c[1] = 0; |
| 255 | + ptr = 0; |
| 256 | + while(1) { |
| 257 | + c[0] = readchar(); |
| 258 | + switch(c[0]) { |
| 259 | + case 0x7f: |
| 260 | + case 0x08: |
| 261 | + if(ptr > 0) { |
| 262 | + ptr--; |
| 263 | + putsnonl("\x08 \x08"); |
| 264 | + } |
| 265 | + break; |
| 266 | + case 0x07: |
| 267 | + break; |
| 268 | + case '\r': |
| 269 | + case '\n': |
| 270 | + s[ptr] = 0x00; |
| 271 | + putsnonl("\n"); |
| 272 | + return; |
| 273 | + default: |
| 274 | + putsnonl(c); |
| 275 | + s[ptr] = c[0]; |
| 276 | + ptr++; |
| 277 | + break; |
| 278 | + } |
| 279 | + } |
| 280 | +} |
| 281 | + |
| 282 | +static char *get_token(char **str) |
| 283 | +{ |
| 284 | + char *c, *d; |
| 285 | + |
| 286 | + c = (char *)strchr(*str, ' '); |
| 287 | + if(c == NULL) { |
| 288 | + d = *str; |
| 289 | + *str = *str+strlen(*str); |
| 290 | + return d; |
| 291 | + } |
| 292 | + *c = 0; |
| 293 | + d = *str; |
| 294 | + *str = c+1; |
| 295 | + return d; |
| 296 | +} |
| 297 | + |
| 298 | +static void do_command(char *c) |
| 299 | +{ |
| 300 | + char *token; |
| 301 | + |
| 302 | + token = get_token(&c); |
| 303 | + |
| 304 | + if(strcmp(token, "help") == 0) help(); |
| 305 | + else if(strcmp(token, "leds") == 0) leds(get_token(&c)); |
| 306 | +/* |
| 307 | + else if(strcmp(token, "inputs") == 0) inputs(); |
| 308 | + else if(strcmp(token, "ttlout") == 0) ttlout(get_token(&c)); |
| 309 | + else if(strcmp(token, "ttlin") == 0) ttlin(); |
| 310 | +
|
| 311 | + else if(strcmp(token, "ddssel") == 0) ddssel(get_token(&c)); |
| 312 | + else if(strcmp(token, "ddsw") == 0) ddsw(get_token(&c), get_token(&c)); |
| 313 | + else if(strcmp(token, "ddsr") == 0) ddsr(get_token(&c)); |
| 314 | + else if(strcmp(token, "ddsreset") == 0) ddsreset(); |
| 315 | + else if(strcmp(token, "ddsinit") == 0) ddsinit(); |
| 316 | + else if(strcmp(token, "ddsfud") == 0) ddsfud(); |
| 317 | + else if(strcmp(token, "ddsftw") == 0) ddsftw(get_token(&c), get_token(&c)); |
| 318 | + else if(strcmp(token, "ddstest") == 0) ddstest(get_token(&c));*/ |
| 319 | + |
| 320 | + else if(strcmp(token, "") != 0) |
| 321 | + printf("Command not found\n"); |
| 322 | +} |
| 323 | + |
| 324 | +void test_main(void) |
| 325 | +{ |
| 326 | + char buffer[64]; |
| 327 | + |
| 328 | + while(1) { |
| 329 | + putsnonl("\e[1mtest>\e[0m "); |
| 330 | + readstr(buffer, 64); |
| 331 | + do_command(buffer); |
| 332 | + } |
| 333 | +} |
0 commit comments