paul@218 | 1 | /* |
paul@218 | 2 | * Access various peripherals on the MIPS Creator CI20 board. |
paul@218 | 3 | * |
paul@253 | 4 | * Copyright (C) 2023, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@218 | 5 | * |
paul@218 | 6 | * This program is free software; you can redistribute it and/or |
paul@218 | 7 | * modify it under the terms of the GNU General Public License as |
paul@218 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@218 | 9 | * the License, or (at your option) any later version. |
paul@218 | 10 | * |
paul@218 | 11 | * This program is distributed in the hope that it will be useful, |
paul@218 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@218 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@218 | 14 | * GNU General Public License for more details. |
paul@218 | 15 | * |
paul@218 | 16 | * You should have received a copy of the GNU General Public License |
paul@218 | 17 | * along with this program; if not, write to the Free Software |
paul@218 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@218 | 19 | * Boston, MA 02110-1301, USA |
paul@218 | 20 | */ |
paul@218 | 21 | |
paul@218 | 22 | #include "common.h" |
paul@218 | 23 | #include "defs.h" |
paul@218 | 24 | |
paul@218 | 25 | #include <l4/devices/dma.h> |
paul@218 | 26 | #include <l4/devices/memory.h> |
paul@218 | 27 | |
paul@218 | 28 | #include <l4/io/io.h> |
paul@218 | 29 | #include <l4/re/env.h> |
paul@218 | 30 | #include <l4/re/c/util/cap_alloc.h> |
paul@218 | 31 | #include <l4/sys/cache.h> |
paul@218 | 32 | #include <l4/sys/factory.h> |
paul@218 | 33 | #include <l4/sys/icu.h> |
paul@218 | 34 | #include <l4/sys/ipc.h> |
paul@218 | 35 | #include <l4/sys/irq.h> |
paul@218 | 36 | #include <l4/sys/rcv_endpoint.h> |
paul@218 | 37 | #include <l4/vbus/vbus.h> |
paul@218 | 38 | |
paul@218 | 39 | #include <stdio.h> |
paul@218 | 40 | #include <unistd.h> |
paul@218 | 41 | #include <stdint.h> |
paul@218 | 42 | #include <stdlib.h> |
paul@218 | 43 | #include <string.h> |
paul@218 | 44 | |
paul@218 | 45 | |
paul@218 | 46 | |
paul@218 | 47 | /* Common configuration. */ |
paul@218 | 48 | |
paul@218 | 49 | static l4_cap_idx_t icucap; |
paul@218 | 50 | |
paul@236 | 51 | /* Memory definitions. */ |
paul@236 | 52 | |
paul@236 | 53 | static const unsigned int num_memory_regions = 8; |
paul@236 | 54 | |
paul@236 | 55 | static struct memory_region memory_regions[] = { |
paul@236 | 56 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, |
paul@236 | 57 | }; |
paul@236 | 58 | |
paul@218 | 59 | |
paul@218 | 60 | |
paul@218 | 61 | /* Device and resource discovery. */ |
paul@218 | 62 | |
paul@218 | 63 | static long item_in_range(long start, long end, long index) |
paul@218 | 64 | { |
paul@218 | 65 | if (start < end) |
paul@218 | 66 | return start + index; |
paul@218 | 67 | else if (start > end) |
paul@218 | 68 | return start - index; |
paul@218 | 69 | else |
paul@218 | 70 | return start; |
paul@218 | 71 | } |
paul@218 | 72 | |
paul@218 | 73 | |
paul@218 | 74 | |
paul@218 | 75 | /* Common functions. */ |
paul@218 | 76 | |
paul@218 | 77 | static int init_irq(int num, l4_cap_idx_t irq, l4_uint32_t start, l4_uint32_t end) |
paul@218 | 78 | { |
paul@218 | 79 | /* Create interrupt object. */ |
paul@218 | 80 | |
paul@218 | 81 | long err = l4_error(l4_factory_create_irq(l4re_global_env->factory, irq)); |
paul@218 | 82 | |
paul@218 | 83 | if (err) |
paul@218 | 84 | { |
paul@218 | 85 | printf("Could not create IRQ object: %ld\n", err); |
paul@218 | 86 | return 1; |
paul@218 | 87 | } |
paul@218 | 88 | |
paul@218 | 89 | /* Bind interrupt objects to IRQ numbers. */ |
paul@218 | 90 | |
paul@218 | 91 | err = l4_error(l4_icu_bind(icucap, |
paul@218 | 92 | item_in_range(start, end, num), |
paul@218 | 93 | irq)); |
paul@218 | 94 | |
paul@218 | 95 | if (err) |
paul@218 | 96 | { |
paul@218 | 97 | printf("Could not bind IRQ to the ICU: %ld\n", err); |
paul@218 | 98 | return 1; |
paul@218 | 99 | } |
paul@218 | 100 | |
paul@218 | 101 | /* Attach ourselves to the interrupt handler. */ |
paul@218 | 102 | |
paul@218 | 103 | err = l4_error(l4_rcv_ep_bind_thread(irq, l4re_env()->main_thread, num)); |
paul@218 | 104 | |
paul@218 | 105 | if (err) |
paul@218 | 106 | { |
paul@218 | 107 | printf("Could not attach to IRQs: %ld\n", err); |
paul@218 | 108 | return 1; |
paul@218 | 109 | } |
paul@218 | 110 | |
paul@218 | 111 | return 0; |
paul@218 | 112 | } |
paul@218 | 113 | |
paul@218 | 114 | static const char *delimiters = " \n"; |
paul@218 | 115 | |
paul@218 | 116 | static char *read_token(const char *message) |
paul@218 | 117 | { |
paul@218 | 118 | char *token; |
paul@218 | 119 | |
paul@218 | 120 | if ((token = strtok(NULL, delimiters)) == NULL) |
paul@218 | 121 | { |
paul@218 | 122 | if (message != NULL) |
paul@218 | 123 | printf("%s?\n", message); |
paul@218 | 124 | } |
paul@218 | 125 | |
paul@218 | 126 | return token; |
paul@218 | 127 | } |
paul@218 | 128 | |
paul@236 | 129 | static int read_flag(const char *message, int *value, const char *flag, size_t n) |
paul@218 | 130 | { |
paul@218 | 131 | char *token = read_token(message); |
paul@218 | 132 | |
paul@218 | 133 | if (token == NULL) |
paul@218 | 134 | return 0; |
paul@218 | 135 | |
paul@236 | 136 | *value = !strncmp(token, flag, n); |
paul@218 | 137 | return 1; |
paul@218 | 138 | } |
paul@218 | 139 | |
paul@218 | 140 | static int read_encoded_number(const char *message, const char *format, unsigned int *num) |
paul@218 | 141 | { |
paul@218 | 142 | char *token = read_token(message); |
paul@218 | 143 | |
paul@218 | 144 | if (token == NULL) |
paul@218 | 145 | return 0; |
paul@218 | 146 | |
paul@218 | 147 | sscanf(token, format, num); |
paul@218 | 148 | return 1; |
paul@218 | 149 | } |
paul@218 | 150 | |
paul@218 | 151 | static int read_number(const char *message, unsigned int *num) |
paul@218 | 152 | { |
paul@218 | 153 | char *token = read_token(message); |
paul@218 | 154 | |
paul@218 | 155 | if (token == NULL) |
paul@218 | 156 | return 0; |
paul@218 | 157 | |
paul@218 | 158 | if (!strncmp(token, "0x", 2) || !strncmp(token, "0X", 2)) |
paul@218 | 159 | sscanf(token, "%x", num); |
paul@218 | 160 | else |
paul@218 | 161 | *num = atoi(token); |
paul@218 | 162 | |
paul@218 | 163 | return 1; |
paul@218 | 164 | } |
paul@218 | 165 | |
paul@218 | 166 | static int get_resource_number(const char *type, unsigned int available) |
paul@218 | 167 | { |
paul@218 | 168 | unsigned int num; |
paul@218 | 169 | |
paul@218 | 170 | if (!read_number(type, &num)) |
paul@218 | 171 | return -1; |
paul@218 | 172 | |
paul@218 | 173 | if (num >= available) |
paul@218 | 174 | { |
paul@218 | 175 | printf("%s number out of range.\n", type); |
paul@218 | 176 | return -1; |
paul@218 | 177 | } |
paul@218 | 178 | |
paul@218 | 179 | return num; |
paul@218 | 180 | } |
paul@218 | 181 | |
paul@218 | 182 | static int get_channel_number(unsigned int available) |
paul@218 | 183 | { |
paul@218 | 184 | return get_resource_number("Channel", available); |
paul@218 | 185 | } |
paul@218 | 186 | |
paul@218 | 187 | static int get_region_number(unsigned int available) |
paul@218 | 188 | { |
paul@218 | 189 | return get_resource_number("Region", available); |
paul@218 | 190 | } |
paul@218 | 191 | |
paul@218 | 192 | static void *get_channel(int num_channels, void *channels[], int *num) |
paul@218 | 193 | { |
paul@218 | 194 | int n = get_channel_number(num_channels); |
paul@218 | 195 | void *channel; |
paul@218 | 196 | |
paul@218 | 197 | if (n < 0) |
paul@218 | 198 | return NULL; |
paul@218 | 199 | |
paul@218 | 200 | channel = channels[n]; |
paul@218 | 201 | |
paul@218 | 202 | if (channel == NULL) |
paul@218 | 203 | printf("Channel needs creating.\n"); |
paul@218 | 204 | |
paul@218 | 205 | if (num != NULL) |
paul@218 | 206 | *num = n; |
paul@218 | 207 | |
paul@218 | 208 | return channel; |
paul@218 | 209 | } |
paul@218 | 210 | |
paul@218 | 211 | static void list_channels(int num_channels, void *channels[]) |
paul@218 | 212 | { |
paul@218 | 213 | int num; |
paul@218 | 214 | void *channel; |
paul@218 | 215 | |
paul@218 | 216 | for (num = 0; num < num_channels; num++) |
paul@218 | 217 | { |
paul@218 | 218 | printf("Channel %d: ", num); |
paul@218 | 219 | |
paul@218 | 220 | channel = channels[num]; |
paul@218 | 221 | |
paul@218 | 222 | if (channel == NULL) |
paul@218 | 223 | printf("(inactive)\n"); |
paul@218 | 224 | else |
paul@218 | 225 | printf("active\n"); |
paul@218 | 226 | } |
paul@218 | 227 | } |
paul@218 | 228 | |
paul@221 | 229 | static struct dma_region *_get_dma_region(void) |
paul@221 | 230 | { |
paul@221 | 231 | int num = get_region_number(num_dma_regions); |
paul@221 | 232 | |
paul@221 | 233 | if (num < 0) |
paul@221 | 234 | return NULL; |
paul@221 | 235 | |
paul@221 | 236 | return &dma_regions[num]; |
paul@221 | 237 | } |
paul@218 | 238 | |
paul@236 | 239 | static struct memory_region *_get_memory_region(void) |
paul@236 | 240 | { |
paul@236 | 241 | int num = get_region_number(num_memory_regions); |
paul@236 | 242 | |
paul@236 | 243 | if (num < 0) |
paul@236 | 244 | return NULL; |
paul@236 | 245 | |
paul@236 | 246 | return &memory_regions[num]; |
paul@236 | 247 | } |
paul@236 | 248 | |
paul@236 | 249 | static int get_memory_region(unsigned int size, l4_addr_t *addr) |
paul@236 | 250 | { |
paul@253 | 251 | void *a = calloc(size, sizeof(char)); |
paul@253 | 252 | |
paul@253 | 253 | *addr = (l4_addr_t) a; |
paul@253 | 254 | return a == NULL ? 1 : 0; |
paul@236 | 255 | } |
paul@236 | 256 | |
paul@218 | 257 | |
paul@218 | 258 | |
paul@218 | 259 | /* AIC/I2S operations. */ |
paul@218 | 260 | |
paul@218 | 261 | static void new_aic_channel(void *aic) |
paul@218 | 262 | { |
paul@218 | 263 | int num = get_channel_number(num_aic_channels); |
paul@218 | 264 | void *channel; |
paul@218 | 265 | |
paul@218 | 266 | if (num < 0) |
paul@218 | 267 | return; |
paul@218 | 268 | |
paul@218 | 269 | channel = get_channel(num_dma_channels, dma_channels, NULL); |
paul@218 | 270 | |
paul@218 | 271 | if (channel == NULL) |
paul@218 | 272 | return; |
paul@218 | 273 | |
paul@218 | 274 | aic_channels[num] = aic_get_channel(aic, num, channel); |
paul@218 | 275 | } |
paul@218 | 276 | |
paul@218 | 277 | static void aic_transfer_data(void) |
paul@218 | 278 | { |
paul@218 | 279 | void *channel = get_channel(num_aic_channels, aic_channels, NULL); |
paul@221 | 280 | struct dma_region *region; |
paul@221 | 281 | uint32_t count, rate, resolution, transferred; |
paul@218 | 282 | |
paul@218 | 283 | if (channel == NULL) |
paul@218 | 284 | return; |
paul@218 | 285 | |
paul@221 | 286 | region = _get_dma_region(); |
paul@221 | 287 | |
paul@221 | 288 | if (region == NULL) |
paul@221 | 289 | return; |
paul@221 | 290 | |
paul@218 | 291 | if (!read_number("Sample size", &count)) |
paul@218 | 292 | return; |
paul@218 | 293 | |
paul@218 | 294 | if (!read_number("Sample rate", &rate)) |
paul@218 | 295 | return; |
paul@218 | 296 | |
paul@218 | 297 | if (!read_number("Sample resolution", &resolution)) |
paul@218 | 298 | return; |
paul@218 | 299 | |
paul@221 | 300 | transferred = aic_transfer(channel, region->paddr, count, rate, resolution); |
paul@218 | 301 | |
paul@218 | 302 | printf("Transferred: %d\n", transferred); |
paul@218 | 303 | } |
paul@218 | 304 | |
paul@218 | 305 | |
paul@218 | 306 | |
paul@218 | 307 | /* CPM operations. */ |
paul@218 | 308 | |
paul@218 | 309 | static const char *clock_id(enum Clock_identifiers clock) |
paul@218 | 310 | { |
paul@218 | 311 | for (int i = 0; clocks[i].id != NULL; i++) |
paul@218 | 312 | if (clock == clocks[i].clock) |
paul@218 | 313 | return clocks[i].id; |
paul@218 | 314 | |
paul@218 | 315 | return NULL; |
paul@218 | 316 | } |
paul@218 | 317 | |
paul@218 | 318 | static enum Clock_identifiers get_clock(char *id) |
paul@218 | 319 | { |
paul@218 | 320 | if (id != NULL) |
paul@218 | 321 | { |
paul@218 | 322 | for (int i = 0; clocks[i].id != NULL; i++) |
paul@218 | 323 | if (!strcmp(id, clocks[i].id)) |
paul@218 | 324 | return clocks[i].clock; |
paul@218 | 325 | } |
paul@218 | 326 | |
paul@243 | 327 | return Clock_none; |
paul@218 | 328 | } |
paul@218 | 329 | |
paul@218 | 330 | static const char list_clocks_separator[] = "--------------------------------"; |
paul@218 | 331 | |
paul@218 | 332 | #define sep(x) (list_clocks_separator + sizeof(list_clocks_separator) - 1 - x) |
paul@218 | 333 | |
paul@218 | 334 | static void list_clocks(void *cpm) |
paul@218 | 335 | { |
paul@218 | 336 | /* Read information from the clock and power management unit. */ |
paul@218 | 337 | |
paul@239 | 338 | printf("%-10s %-15s %-7s %-6s %-12s %-12s %-24s %-12s %-3s\n" |
paul@239 | 339 | "%-10s %-15s %-7s %-6s %-12s %-12s %-24s %-12s %-3s\n", |
paul@218 | 340 | "Id", "Clock", "Type", "Source", "Source Clock", "Source Freq.", |
paul@218 | 341 | "Parameters", "Frequency", "On", |
paul@239 | 342 | sep(10), sep(15), sep(7), sep(6), sep(12), sep(12), |
paul@218 | 343 | sep(24), sep(12), sep(3)); |
paul@218 | 344 | |
paul@218 | 345 | for (int i = 0; clocks[i].id != NULL; i++) |
paul@218 | 346 | { |
paul@218 | 347 | uint32_t parameters[4]; |
paul@218 | 348 | char parameter_str[25] = {0}; |
paul@218 | 349 | const char *source_id = clock_id(cpm_get_source_clock(cpm, clocks[i].clock)); |
paul@218 | 350 | int num_parameters = cpm_get_parameters(cpm, clocks[i].clock, parameters); |
paul@218 | 351 | |
paul@218 | 352 | for (int p = 0, pos = 0; p < num_parameters; p++) |
paul@218 | 353 | { |
paul@218 | 354 | int result = sprintf(parameter_str + pos, "%-7d ", parameters[p]); |
paul@218 | 355 | if (result < 0) |
paul@218 | 356 | break; |
paul@218 | 357 | pos += result; |
paul@218 | 358 | } |
paul@218 | 359 | |
paul@239 | 360 | printf("%-10s %-15s %-7s %-6d %-12s %-12lld %-24s %-12lld %-3s\n", |
paul@218 | 361 | clocks[i].id, |
paul@218 | 362 | clocks[i].name, |
paul@218 | 363 | cpm_clock_type(cpm, clocks[i].clock), |
paul@218 | 364 | cpm_get_source(cpm, clocks[i].clock), |
paul@218 | 365 | source_id != NULL ? source_id : "...", |
paul@218 | 366 | cpm_get_source_frequency(cpm, clocks[i].clock), |
paul@218 | 367 | parameter_str, |
paul@218 | 368 | cpm_get_frequency(cpm, clocks[i].clock), |
paul@218 | 369 | cpm_have_clock(cpm, clocks[i].clock) ? "on" : "off"); |
paul@218 | 370 | } |
paul@218 | 371 | } |
paul@218 | 372 | |
paul@218 | 373 | static enum Clock_identifiers get_clock_by_name(void) |
paul@218 | 374 | { |
paul@218 | 375 | char *token; |
paul@218 | 376 | enum Clock_identifiers clock; |
paul@218 | 377 | |
paul@218 | 378 | if ((token = read_token("Clock")) == NULL) |
paul@243 | 379 | return Clock_none; |
paul@218 | 380 | |
paul@218 | 381 | clock = get_clock(token); |
paul@218 | 382 | |
paul@243 | 383 | if (clock == Clock_none) |
paul@218 | 384 | { |
paul@218 | 385 | printf("Unrecognised clock: %s\n", token); |
paul@243 | 386 | return Clock_none; |
paul@218 | 387 | } |
paul@218 | 388 | |
paul@218 | 389 | return clock; |
paul@218 | 390 | } |
paul@218 | 391 | |
paul@218 | 392 | static void set_frequency(void *cpm) |
paul@218 | 393 | { |
paul@218 | 394 | enum Clock_identifiers clock; |
paul@218 | 395 | unsigned int frequency; |
paul@218 | 396 | |
paul@218 | 397 | clock = get_clock_by_name(); |
paul@218 | 398 | |
paul@243 | 399 | if (clock == Clock_none) |
paul@218 | 400 | return; |
paul@218 | 401 | |
paul@218 | 402 | if (!read_number("Frequency", &frequency)) |
paul@218 | 403 | return; |
paul@218 | 404 | |
paul@218 | 405 | if (!cpm_set_frequency(cpm, clock, frequency)) |
paul@218 | 406 | printf("Frequency not set.\n"); |
paul@218 | 407 | } |
paul@218 | 408 | |
paul@218 | 409 | static void set_parameters(void *cpm) |
paul@218 | 410 | { |
paul@218 | 411 | char *token; |
paul@218 | 412 | enum Clock_identifiers clock; |
paul@218 | 413 | uint32_t parameters[4]; |
paul@218 | 414 | int num_parameters; |
paul@218 | 415 | |
paul@218 | 416 | clock = get_clock_by_name(); |
paul@218 | 417 | |
paul@243 | 418 | if (clock == Clock_none) |
paul@218 | 419 | return; |
paul@218 | 420 | |
paul@218 | 421 | for (num_parameters = 0; num_parameters < 4; num_parameters++) |
paul@218 | 422 | { |
paul@218 | 423 | if ((token = read_token(NULL)) == NULL) |
paul@218 | 424 | break; |
paul@218 | 425 | |
paul@218 | 426 | parameters[num_parameters] = atoi(token); |
paul@218 | 427 | } |
paul@218 | 428 | |
paul@218 | 429 | if (!num_parameters) |
paul@218 | 430 | { |
paul@218 | 431 | printf("Parameters?\n"); |
paul@218 | 432 | return; |
paul@218 | 433 | } |
paul@218 | 434 | |
paul@218 | 435 | if (!cpm_set_parameters(cpm, clock, num_parameters, parameters)) |
paul@218 | 436 | printf("Too few or invalid parameters.\n"); |
paul@218 | 437 | } |
paul@218 | 438 | |
paul@218 | 439 | static void set_source(void *cpm) |
paul@218 | 440 | { |
paul@218 | 441 | enum Clock_identifiers clock; |
paul@218 | 442 | unsigned int source; |
paul@218 | 443 | |
paul@218 | 444 | clock = get_clock_by_name(); |
paul@218 | 445 | |
paul@243 | 446 | if (clock == Clock_none) |
paul@218 | 447 | return; |
paul@218 | 448 | |
paul@218 | 449 | if (!read_number("Source", &source)) |
paul@218 | 450 | return; |
paul@218 | 451 | |
paul@218 | 452 | cpm_set_source(cpm, clock, source); |
paul@218 | 453 | } |
paul@218 | 454 | |
paul@218 | 455 | static void start_clock(void *cpm) |
paul@218 | 456 | { |
paul@218 | 457 | enum Clock_identifiers clock; |
paul@218 | 458 | |
paul@218 | 459 | clock = get_clock_by_name(); |
paul@218 | 460 | |
paul@243 | 461 | if (clock == Clock_none) |
paul@218 | 462 | return; |
paul@218 | 463 | |
paul@218 | 464 | cpm_start_clock(cpm, clock); |
paul@218 | 465 | } |
paul@218 | 466 | |
paul@218 | 467 | static void stop_clock(void *cpm) |
paul@218 | 468 | { |
paul@218 | 469 | enum Clock_identifiers clock; |
paul@218 | 470 | |
paul@218 | 471 | clock = get_clock_by_name(); |
paul@218 | 472 | |
paul@243 | 473 | if (clock == Clock_none) |
paul@218 | 474 | return; |
paul@218 | 475 | |
paul@218 | 476 | cpm_stop_clock(cpm, clock); |
paul@218 | 477 | } |
paul@218 | 478 | |
paul@218 | 479 | |
paul@218 | 480 | |
paul@218 | 481 | /* DMA configuration. */ |
paul@218 | 482 | |
paul@218 | 483 | static l4_uint32_t dma_irq_start = 0, dma_irq_end = 0; |
paul@218 | 484 | |
paul@218 | 485 | /* DMA operations. */ |
paul@218 | 486 | |
paul@218 | 487 | static int init_dma(void) |
paul@218 | 488 | { |
paul@218 | 489 | unsigned int i; |
paul@218 | 490 | |
paul@218 | 491 | for (i = 0; i < num_dma_regions; i++) |
paul@218 | 492 | dma_regions[i].mem = L4_INVALID_CAP; |
paul@218 | 493 | |
paul@218 | 494 | /* Here, only one IRQ is used. */ |
paul@218 | 495 | |
paul@218 | 496 | dma_irq = l4re_util_cap_alloc(); |
paul@218 | 497 | return init_irq(0, dma_irq, dma_irq_start, dma_irq_end); |
paul@218 | 498 | } |
paul@218 | 499 | |
paul@218 | 500 | static void list_dma_regions(void) |
paul@218 | 501 | { |
paul@218 | 502 | unsigned int num; |
paul@218 | 503 | struct dma_region *region; |
paul@218 | 504 | unsigned int i; |
paul@218 | 505 | |
paul@218 | 506 | for (num = 0; num < num_dma_regions; num++) |
paul@218 | 507 | { |
paul@218 | 508 | printf("Region %d: ", num); |
paul@218 | 509 | |
paul@218 | 510 | region = &dma_regions[num]; |
paul@218 | 511 | |
paul@218 | 512 | if (l4_is_invalid_cap(region->mem)) |
paul@218 | 513 | printf("(inactive)\n"); |
paul@218 | 514 | else |
paul@218 | 515 | { |
paul@218 | 516 | printf("size = %d; align = %d; virtual = 0x%lx; physical = 0x%llx\ndata =", |
paul@218 | 517 | region->size, region->align, region->vaddr, region->paddr); |
paul@218 | 518 | |
paul@218 | 519 | for (i = 0; (i < region->size) && (i < 16); i++) |
paul@218 | 520 | printf(" %02x", *((uint8_t *) region->vaddr + i)); |
paul@218 | 521 | |
paul@218 | 522 | printf("\n"); |
paul@218 | 523 | } |
paul@218 | 524 | } |
paul@218 | 525 | } |
paul@218 | 526 | |
paul@218 | 527 | static void new_dma_channel(void *dma) |
paul@218 | 528 | { |
paul@218 | 529 | int num = get_channel_number(num_dma_channels); |
paul@218 | 530 | |
paul@218 | 531 | if (num < 0) |
paul@218 | 532 | return; |
paul@218 | 533 | |
paul@218 | 534 | if (dma_channels[num] != NULL) |
paul@218 | 535 | { |
paul@218 | 536 | printf("Channel already defined.\n"); |
paul@218 | 537 | return; |
paul@218 | 538 | } |
paul@218 | 539 | |
paul@218 | 540 | dma_channels[num] = dma_get_channel(dma, num, dma_irq); |
paul@218 | 541 | } |
paul@218 | 542 | |
paul@218 | 543 | static void new_dma_region(void) |
paul@218 | 544 | { |
paul@218 | 545 | struct dma_region *region = _get_dma_region(); |
paul@218 | 546 | |
paul@218 | 547 | if (region == NULL) |
paul@218 | 548 | { |
paul@218 | 549 | list_dma_regions(); |
paul@218 | 550 | return; |
paul@218 | 551 | } |
paul@218 | 552 | |
paul@218 | 553 | if (l4_is_valid_cap(region->mem)) |
paul@218 | 554 | { |
paul@218 | 555 | printf("Region already defined.\n"); |
paul@218 | 556 | return; |
paul@218 | 557 | } |
paul@218 | 558 | |
paul@218 | 559 | if (!read_number("Size", ®ion->size)) |
paul@218 | 560 | return; |
paul@218 | 561 | |
paul@218 | 562 | if (!read_number("Alignment", ®ion->align)) |
paul@218 | 563 | return; |
paul@218 | 564 | |
paul@218 | 565 | if (get_dma_region(region->size, region->align, ®ion->vaddr, ®ion->paddr, ®ion->mem)) |
paul@218 | 566 | printf("Could not allocate region.\n"); |
paul@218 | 567 | } |
paul@218 | 568 | |
paul@218 | 569 | static void set_dma_region(void) |
paul@218 | 570 | { |
paul@218 | 571 | char *token; |
paul@218 | 572 | struct dma_region *region = _get_dma_region(); |
paul@218 | 573 | FILE *fp; |
paul@218 | 574 | |
paul@218 | 575 | if (region == NULL) |
paul@218 | 576 | { |
paul@218 | 577 | list_dma_regions(); |
paul@218 | 578 | return; |
paul@218 | 579 | } |
paul@218 | 580 | |
paul@218 | 581 | if (l4_is_invalid_cap(region->mem)) |
paul@218 | 582 | { |
paul@218 | 583 | printf("Region needs creating.\n"); |
paul@218 | 584 | return; |
paul@218 | 585 | } |
paul@218 | 586 | |
paul@218 | 587 | memset((void *) region->vaddr, 0, region->size); |
paul@218 | 588 | |
paul@218 | 589 | if ((token = read_token("Filename")) == NULL) |
paul@218 | 590 | return; |
paul@218 | 591 | |
paul@218 | 592 | /* Populate the region from the file. */ |
paul@218 | 593 | |
paul@218 | 594 | fp = fopen(token, "r"); |
paul@218 | 595 | |
paul@218 | 596 | if (fp == NULL) |
paul@218 | 597 | { |
paul@218 | 598 | printf("File not readable.\n"); |
paul@218 | 599 | return; |
paul@218 | 600 | } |
paul@218 | 601 | |
paul@218 | 602 | fread((char *) region->vaddr, sizeof(char), region->size, fp); |
paul@218 | 603 | fclose(fp); |
paul@218 | 604 | |
paul@218 | 605 | l4_cache_flush_data(region->vaddr, region->vaddr + region->size); |
paul@218 | 606 | } |
paul@218 | 607 | |
paul@218 | 608 | static void dma_transfer_data(void) |
paul@218 | 609 | { |
paul@221 | 610 | uint32_t count, to_transfer, transferred; |
paul@218 | 611 | unsigned int source_address, destination_address, source_width, destination_width, |
paul@218 | 612 | transfer_unit_size, request_type; |
paul@218 | 613 | int source_increment, destination_increment; |
paul@218 | 614 | void *channel = get_channel(num_dma_channels, dma_channels, NULL); |
paul@218 | 615 | unsigned int i; |
paul@218 | 616 | |
paul@218 | 617 | if (channel == NULL) |
paul@218 | 618 | return; |
paul@218 | 619 | |
paul@218 | 620 | if (!read_number("Source address", &source_address)) |
paul@218 | 621 | return; |
paul@218 | 622 | |
paul@218 | 623 | if (!read_number("Destination address", &destination_address)) |
paul@218 | 624 | return; |
paul@218 | 625 | |
paul@218 | 626 | if (!read_number("Transfer size", &count)) |
paul@218 | 627 | return; |
paul@218 | 628 | |
paul@236 | 629 | if (!read_flag("Source increment", &source_increment, "i", 1)) |
paul@218 | 630 | return; |
paul@218 | 631 | |
paul@236 | 632 | if (!read_flag("Destination increment", &destination_increment, "i", 1)) |
paul@218 | 633 | return; |
paul@218 | 634 | |
paul@218 | 635 | if (!read_number("Source width", &source_width)) |
paul@218 | 636 | return; |
paul@218 | 637 | |
paul@218 | 638 | if (!read_number("Destination width", &destination_width)) |
paul@218 | 639 | return; |
paul@218 | 640 | |
paul@218 | 641 | if (!read_number("Transfer unit size", &transfer_unit_size)) |
paul@218 | 642 | return; |
paul@218 | 643 | |
paul@218 | 644 | if (!read_number("Request type", &request_type)) |
paul@218 | 645 | return; |
paul@218 | 646 | |
paul@218 | 647 | /* Invalidate DMA region contents in case of transfers between regions. */ |
paul@218 | 648 | |
paul@218 | 649 | for (i = 0; i < num_dma_regions; i++) |
paul@218 | 650 | l4_cache_inv_data(dma_regions[i].vaddr, dma_regions[i].vaddr + dma_regions[i].size); |
paul@218 | 651 | |
paul@218 | 652 | to_transfer = dma_transfer(channel, source_address, destination_address, |
paul@221 | 653 | count, |
paul@221 | 654 | source_increment, destination_increment, |
paul@221 | 655 | source_width, destination_width, |
paul@221 | 656 | transfer_unit_size, |
paul@221 | 657 | request_type); |
paul@218 | 658 | |
paul@218 | 659 | transferred = to_transfer ? count - dma_wait(channel) : 0; |
paul@218 | 660 | |
paul@218 | 661 | printf("Transferred: %d\n", transferred); |
paul@218 | 662 | } |
paul@218 | 663 | |
paul@218 | 664 | |
paul@218 | 665 | |
paul@218 | 666 | /* GPIO operations. */ |
paul@218 | 667 | |
paul@218 | 668 | static char get_gpio_dir_label(unsigned int value, unsigned int level) |
paul@218 | 669 | { |
paul@218 | 670 | return value == Fix_input ? (level ? 'I' : 'i') : (level ? 'O' : 'o'); |
paul@218 | 671 | } |
paul@218 | 672 | |
paul@218 | 673 | static char get_gpio_irq_label(unsigned int value) |
paul@218 | 674 | { |
paul@218 | 675 | switch (value) |
paul@218 | 676 | { |
paul@218 | 677 | case L4_IRQ_F_NEG_EDGE: return 'f'; |
paul@218 | 678 | case L4_IRQ_F_POS_EDGE: return 'r'; |
paul@218 | 679 | case L4_IRQ_F_LEVEL_LOW: return 'l'; |
paul@218 | 680 | case L4_IRQ_F_LEVEL_HIGH: return 'h'; |
paul@218 | 681 | default: return '?'; |
paul@218 | 682 | } |
paul@218 | 683 | } |
paul@218 | 684 | |
paul@218 | 685 | static void list_gpios(void *gpio[]) |
paul@218 | 686 | { |
paul@218 | 687 | unsigned int port, pin; |
paul@218 | 688 | unsigned int func, value, level; |
paul@218 | 689 | |
paul@218 | 690 | printf("gpio: I = input high; i = input low; O = output high; o = output low\n" |
paul@218 | 691 | "irq: h = high level; l = low level; r = rising edge; f = falling edge\n" |
paul@218 | 692 | "alt: function number\n\n"); |
paul@218 | 693 | |
paul@218 | 694 | /* Show pin numbering. */ |
paul@218 | 695 | |
paul@218 | 696 | printf("Port/Pin "); |
paul@218 | 697 | |
paul@218 | 698 | for (pin = 0; pin < 32; pin++) |
paul@218 | 699 | { |
paul@218 | 700 | if (!(pin % 10)) |
paul@218 | 701 | printf(" %d", pin); |
paul@218 | 702 | else |
paul@218 | 703 | printf(" %d", pin % 10); |
paul@218 | 704 | } |
paul@218 | 705 | |
paul@218 | 706 | printf("\n"); |
paul@218 | 707 | |
paul@218 | 708 | for (port = 0; port < num_gpio_ports; port++) |
paul@218 | 709 | { |
paul@218 | 710 | /* Show port and pin configuration. */ |
paul@218 | 711 | |
paul@218 | 712 | printf("%c ", gpio_port_labels[port]); |
paul@218 | 713 | |
paul@218 | 714 | for (pin = 0; pin < 32; pin++) |
paul@218 | 715 | { |
paul@218 | 716 | /* Pad below the first pin digit for multiples of ten other than zero. */ |
paul@218 | 717 | |
paul@218 | 718 | if (pin && !(pin % 10)) |
paul@218 | 719 | printf(" "); |
paul@218 | 720 | |
paul@218 | 721 | gpio_config_pad_get(gpio[port], pin, &func, &value); |
paul@218 | 722 | |
paul@218 | 723 | switch (func) |
paul@218 | 724 | { |
paul@218 | 725 | case Function_alt: printf(" %d", value); break; |
paul@218 | 726 | case Function_irq: printf(" %c", get_gpio_irq_label(value)); break; |
paul@218 | 727 | case Function_gpio: |
paul@218 | 728 | { |
paul@218 | 729 | level = gpio_get(gpio[port], pin); |
paul@218 | 730 | printf(" %c", get_gpio_dir_label(value, level)); |
paul@218 | 731 | break; |
paul@218 | 732 | } |
paul@218 | 733 | default: printf(" ?"); break; |
paul@218 | 734 | } |
paul@218 | 735 | } |
paul@218 | 736 | |
paul@218 | 737 | printf("\n"); |
paul@218 | 738 | } |
paul@218 | 739 | } |
paul@218 | 740 | |
paul@218 | 741 | static int get_port_and_pin(unsigned int *port, unsigned int *pin) |
paul@218 | 742 | { |
paul@218 | 743 | char *token; |
paul@218 | 744 | |
paul@218 | 745 | if ((token = read_token("Port")) == NULL) |
paul@218 | 746 | return 0; |
paul@218 | 747 | |
paul@218 | 748 | if ((token[0] < 'A') || ((unsigned int) token[0] - (unsigned int) 'A' >= num_gpio_ports)) |
paul@218 | 749 | { |
paul@218 | 750 | printf("Bad port: %c\n", token[0]); |
paul@218 | 751 | return 0; |
paul@218 | 752 | } |
paul@218 | 753 | |
paul@218 | 754 | *port = (unsigned int) token[0] - (unsigned int) 'A'; |
paul@218 | 755 | |
paul@218 | 756 | if (!read_number("Pin", pin)) |
paul@218 | 757 | return 0; |
paul@218 | 758 | |
paul@218 | 759 | return 1; |
paul@218 | 760 | } |
paul@218 | 761 | |
paul@218 | 762 | static void set_gpio_alt_func(void *gpio[]) |
paul@218 | 763 | { |
paul@218 | 764 | unsigned int port, pin, value; |
paul@218 | 765 | |
paul@218 | 766 | if (!get_port_and_pin(&port, &pin)) |
paul@218 | 767 | return; |
paul@218 | 768 | |
paul@218 | 769 | if (!read_number("Function", &value)) |
paul@218 | 770 | return; |
paul@218 | 771 | |
paul@218 | 772 | gpio_config_pad(gpio[port], pin, Function_alt, value); |
paul@218 | 773 | } |
paul@218 | 774 | |
paul@218 | 775 | static void set_gpio_pad(void *gpio[]) |
paul@218 | 776 | { |
paul@218 | 777 | char *token; |
paul@218 | 778 | unsigned int port, pin, mode, value = 0; |
paul@218 | 779 | |
paul@218 | 780 | if (!get_port_and_pin(&port, &pin)) |
paul@218 | 781 | return; |
paul@218 | 782 | |
paul@218 | 783 | if ((token = read_token("Mode")) == NULL) |
paul@218 | 784 | return; |
paul@218 | 785 | |
paul@218 | 786 | if (!strcmp(token, "in")) |
paul@218 | 787 | mode = Fix_input; |
paul@218 | 788 | else if (!strcmp(token, "irq")) |
paul@218 | 789 | mode = Fix_irq; |
paul@218 | 790 | else if (!strcmp(token, "out")) |
paul@218 | 791 | { |
paul@218 | 792 | mode = Fix_output; |
paul@218 | 793 | |
paul@218 | 794 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 795 | value = atoi(token); |
paul@218 | 796 | } |
paul@218 | 797 | else |
paul@218 | 798 | { |
paul@218 | 799 | printf("Mode not recognised.\n"); |
paul@218 | 800 | return; |
paul@218 | 801 | } |
paul@218 | 802 | |
paul@218 | 803 | gpio_setup(gpio[port], pin, mode, value); |
paul@218 | 804 | } |
paul@218 | 805 | |
paul@218 | 806 | static void set_gpio_pull(void *gpio[]) |
paul@218 | 807 | { |
paul@218 | 808 | char *token; |
paul@218 | 809 | unsigned int port, pin, mode; |
paul@218 | 810 | |
paul@218 | 811 | if (!get_port_and_pin(&port, &pin)) |
paul@218 | 812 | return; |
paul@218 | 813 | |
paul@218 | 814 | if ((token = read_token("Mode")) == NULL) |
paul@218 | 815 | return; |
paul@218 | 816 | |
paul@218 | 817 | if (!strcmp(token, "down")) |
paul@218 | 818 | mode = Pull_down; |
paul@218 | 819 | else if (!strcmp(token, "none")) |
paul@218 | 820 | mode = Pull_none; |
paul@218 | 821 | else if (!strcmp(token, "up")) |
paul@218 | 822 | mode = Pull_up; |
paul@218 | 823 | else |
paul@218 | 824 | { |
paul@218 | 825 | printf("Mode not recognised.\n"); |
paul@218 | 826 | return; |
paul@218 | 827 | } |
paul@218 | 828 | |
paul@218 | 829 | gpio_config_pull(gpio[port], pin, mode); |
paul@218 | 830 | } |
paul@218 | 831 | |
paul@218 | 832 | |
paul@218 | 833 | |
paul@218 | 834 | /* I2C configuration. */ |
paul@218 | 835 | |
paul@218 | 836 | static l4_uint32_t i2c_irq_start = 0, i2c_irq_end = 0; |
paul@218 | 837 | |
paul@218 | 838 | /* I2C operations. */ |
paul@218 | 839 | |
paul@218 | 840 | static int _i2c_read(void *channel, uint8_t *buf, unsigned length, |
paul@218 | 841 | int stop, l4_cap_idx_t irqcap, unsigned timeout) |
paul@218 | 842 | { |
paul@218 | 843 | l4_msgtag_t tag; |
paul@218 | 844 | |
paul@218 | 845 | i2c_start_read(channel, buf, length, stop); |
paul@218 | 846 | |
paul@218 | 847 | while (!i2c_read_done(channel)) |
paul@218 | 848 | { |
paul@218 | 849 | tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_from_us(timeout))); |
paul@218 | 850 | |
paul@218 | 851 | // NOTE: Error not returned. |
paul@218 | 852 | |
paul@218 | 853 | if (l4_ipc_error(tag, l4_utcb())) |
paul@218 | 854 | break; |
paul@218 | 855 | |
paul@218 | 856 | if (i2c_failed(channel)) |
paul@218 | 857 | break; |
paul@218 | 858 | |
paul@218 | 859 | i2c_read(channel); |
paul@218 | 860 | } |
paul@218 | 861 | |
paul@218 | 862 | if (stop) |
paul@218 | 863 | i2c_stop(channel); |
paul@218 | 864 | |
paul@218 | 865 | return i2c_have_read(channel); |
paul@218 | 866 | } |
paul@218 | 867 | |
paul@218 | 868 | static int _i2c_write(void *channel, uint8_t *buf, unsigned length, |
paul@218 | 869 | int stop, l4_cap_idx_t irqcap, unsigned timeout) |
paul@218 | 870 | { |
paul@218 | 871 | l4_msgtag_t tag; |
paul@218 | 872 | |
paul@218 | 873 | i2c_start_write(channel, buf, length, stop); |
paul@218 | 874 | |
paul@218 | 875 | while (!i2c_write_done(channel)) |
paul@218 | 876 | { |
paul@218 | 877 | tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_from_us(timeout))); |
paul@218 | 878 | |
paul@218 | 879 | // NOTE: Error not returned. |
paul@218 | 880 | |
paul@218 | 881 | if (l4_ipc_error(tag, l4_utcb())) |
paul@218 | 882 | break; |
paul@218 | 883 | |
paul@218 | 884 | if (i2c_failed(channel)) |
paul@218 | 885 | break; |
paul@218 | 886 | |
paul@218 | 887 | i2c_write(channel); |
paul@218 | 888 | } |
paul@218 | 889 | |
paul@218 | 890 | if (stop) |
paul@218 | 891 | i2c_stop(channel); |
paul@218 | 892 | |
paul@218 | 893 | return i2c_have_written(channel); |
paul@218 | 894 | } |
paul@218 | 895 | |
paul@218 | 896 | static void list_i2c_channels(void) |
paul@218 | 897 | { |
paul@218 | 898 | unsigned int num; |
paul@218 | 899 | void *channel; |
paul@218 | 900 | |
paul@218 | 901 | for (num = 0; num < num_i2c_channels; num++) |
paul@218 | 902 | { |
paul@218 | 903 | printf("Channel %d: ", num); |
paul@218 | 904 | |
paul@218 | 905 | channel = i2c_channels[num]; |
paul@218 | 906 | |
paul@218 | 907 | if (channel == NULL) |
paul@218 | 908 | printf("(inactive)\n"); |
paul@218 | 909 | else |
paul@218 | 910 | printf("%d Hz\n", i2c_get_frequency(channel)); |
paul@218 | 911 | } |
paul@218 | 912 | } |
paul@218 | 913 | |
paul@218 | 914 | static void new_i2c_channel(void *i2c) |
paul@218 | 915 | { |
paul@218 | 916 | l4_cap_idx_t irqcap; |
paul@218 | 917 | int num = get_channel_number(num_i2c_channels); |
paul@218 | 918 | |
paul@218 | 919 | if (num < 0) |
paul@218 | 920 | return; |
paul@218 | 921 | |
paul@218 | 922 | irqcap = l4re_util_cap_alloc(); |
paul@218 | 923 | |
paul@218 | 924 | if (init_irq(num, irqcap, i2c_irq_start, i2c_irq_end)) |
paul@218 | 925 | return; |
paul@218 | 926 | |
paul@218 | 927 | i2c_channels[num] = i2c_get_channel(i2c, num); |
paul@218 | 928 | i2c_irqs[num] = irqcap; |
paul@218 | 929 | } |
paul@218 | 930 | |
paul@218 | 931 | static void i2c_get(void) |
paul@218 | 932 | { |
paul@218 | 933 | void *channel; |
paul@218 | 934 | int num; |
paul@218 | 935 | uint8_t buffer[32]; |
paul@218 | 936 | unsigned int address, reg, count; |
paul@218 | 937 | int i, transferred; |
paul@218 | 938 | |
paul@218 | 939 | channel = get_channel(num_i2c_channels, i2c_channels, &num); |
paul@218 | 940 | |
paul@218 | 941 | if (channel == NULL) |
paul@218 | 942 | return; |
paul@218 | 943 | |
paul@218 | 944 | if (!read_encoded_number("Address", "%2x", &address)) |
paul@218 | 945 | return; |
paul@218 | 946 | |
paul@218 | 947 | if (address >= 0x80) |
paul@218 | 948 | { |
paul@218 | 949 | printf("Address must be less than 80.\n"); |
paul@218 | 950 | return; |
paul@218 | 951 | } |
paul@218 | 952 | |
paul@218 | 953 | if (!read_encoded_number("Register", "%2x", ®)) |
paul@218 | 954 | return; |
paul@218 | 955 | |
paul@218 | 956 | if (!read_number(NULL, &count)) |
paul@218 | 957 | count = 1; |
paul@218 | 958 | |
paul@218 | 959 | buffer[0] = (uint8_t) (reg & 0xff); |
paul@218 | 960 | |
paul@218 | 961 | i2c_set_target(channel, address); |
paul@218 | 962 | |
paul@218 | 963 | if (!_i2c_write(channel, buffer, 1, 0, i2c_irqs[num], 1000000)) |
paul@218 | 964 | { |
paul@218 | 965 | printf("Register request failed.\n"); |
paul@218 | 966 | return; |
paul@218 | 967 | } |
paul@218 | 968 | |
paul@218 | 969 | transferred = _i2c_read(channel, buffer, count, 1, i2c_irqs[num], 1000000); |
paul@218 | 970 | |
paul@218 | 971 | if (!transferred) |
paul@218 | 972 | { |
paul@218 | 973 | printf("Register read failed.\n"); |
paul@218 | 974 | return; |
paul@218 | 975 | } |
paul@218 | 976 | |
paul@218 | 977 | for (i = 0; i < transferred; i++) |
paul@218 | 978 | printf("%02x ", buffer[i]); |
paul@218 | 979 | printf("\n"); |
paul@218 | 980 | } |
paul@218 | 981 | |
paul@218 | 982 | static void i2c_scan(void) |
paul@218 | 983 | { |
paul@218 | 984 | void *channel; |
paul@218 | 985 | unsigned int address; |
paul@218 | 986 | uint8_t buffer[1]; |
paul@218 | 987 | int num; |
paul@218 | 988 | |
paul@218 | 989 | channel = get_channel(num_i2c_channels, i2c_channels, &num); |
paul@218 | 990 | |
paul@218 | 991 | if (channel == NULL) |
paul@218 | 992 | return; |
paul@218 | 993 | |
paul@218 | 994 | for (address = 0; address < 0x20; address++) |
paul@218 | 995 | printf("%02x ", address); |
paul@218 | 996 | printf("\n"); |
paul@218 | 997 | |
paul@218 | 998 | for (address = 0; address < 0x20; address++) |
paul@218 | 999 | printf("-- "); |
paul@218 | 1000 | |
paul@218 | 1001 | for (address = 0; address < 0x80; address++) |
paul@218 | 1002 | { |
paul@218 | 1003 | if ((address % 32) == 0) |
paul@218 | 1004 | printf("\n"); |
paul@218 | 1005 | |
paul@218 | 1006 | i2c_set_target(channel, address); |
paul@218 | 1007 | |
paul@218 | 1008 | if (_i2c_read(channel, buffer, 1, 1, i2c_irqs[num], 1000)) |
paul@218 | 1009 | printf("%02x ", address); |
paul@218 | 1010 | else |
paul@218 | 1011 | printf("-- "); |
paul@218 | 1012 | } |
paul@218 | 1013 | |
paul@218 | 1014 | printf("\n"); |
paul@218 | 1015 | for (address = 0; address < 0x20; address++) |
paul@218 | 1016 | printf("-- "); |
paul@218 | 1017 | printf("\n\n"); |
paul@218 | 1018 | } |
paul@218 | 1019 | |
paul@218 | 1020 | |
paul@218 | 1021 | |
paul@236 | 1022 | /* Memory operations. */ |
paul@236 | 1023 | |
paul@236 | 1024 | static void list_memory_regions(void) |
paul@236 | 1025 | { |
paul@236 | 1026 | unsigned int num; |
paul@236 | 1027 | struct memory_region *region; |
paul@236 | 1028 | unsigned int i; |
paul@236 | 1029 | |
paul@236 | 1030 | for (num = 0; num < num_memory_regions; num++) |
paul@236 | 1031 | { |
paul@236 | 1032 | printf("Region %d: ", num); |
paul@236 | 1033 | |
paul@236 | 1034 | region = &memory_regions[num]; |
paul@236 | 1035 | |
paul@236 | 1036 | if (!region->addr) |
paul@236 | 1037 | printf("(inactive)\n"); |
paul@236 | 1038 | else |
paul@236 | 1039 | { |
paul@236 | 1040 | printf("size = %d; virtual = 0x%lx; data =", |
paul@236 | 1041 | region->size, region->addr); |
paul@236 | 1042 | |
paul@236 | 1043 | for (i = 0; (i < region->size) && (i < 16); i++) |
paul@236 | 1044 | printf(" %02x", *((uint8_t *) region->addr + i)); |
paul@236 | 1045 | |
paul@236 | 1046 | printf("\n"); |
paul@236 | 1047 | } |
paul@236 | 1048 | } |
paul@236 | 1049 | } |
paul@236 | 1050 | |
paul@236 | 1051 | static void new_memory_region(void) |
paul@236 | 1052 | { |
paul@236 | 1053 | struct memory_region *region = _get_memory_region(); |
paul@236 | 1054 | |
paul@236 | 1055 | if (region == NULL) |
paul@236 | 1056 | { |
paul@236 | 1057 | list_memory_regions(); |
paul@236 | 1058 | return; |
paul@236 | 1059 | } |
paul@236 | 1060 | |
paul@236 | 1061 | if (region->addr) |
paul@236 | 1062 | { |
paul@236 | 1063 | printf("Region already defined.\n"); |
paul@236 | 1064 | return; |
paul@236 | 1065 | } |
paul@236 | 1066 | |
paul@236 | 1067 | if (!read_number("Size", ®ion->size)) |
paul@236 | 1068 | return; |
paul@236 | 1069 | |
paul@236 | 1070 | if (get_memory_region(region->size, ®ion->addr)) |
paul@236 | 1071 | printf("Could not allocate region.\n"); |
paul@236 | 1072 | } |
paul@236 | 1073 | |
paul@236 | 1074 | static void set_memory_region(void) |
paul@236 | 1075 | { |
paul@236 | 1076 | char *token; |
paul@236 | 1077 | struct memory_region *region = _get_memory_region(); |
paul@236 | 1078 | FILE *fp; |
paul@236 | 1079 | |
paul@236 | 1080 | if (region == NULL) |
paul@236 | 1081 | { |
paul@236 | 1082 | list_memory_regions(); |
paul@236 | 1083 | return; |
paul@236 | 1084 | } |
paul@236 | 1085 | |
paul@236 | 1086 | if (!region->addr) |
paul@236 | 1087 | { |
paul@236 | 1088 | printf("Region needs creating.\n"); |
paul@236 | 1089 | return; |
paul@236 | 1090 | } |
paul@236 | 1091 | |
paul@236 | 1092 | memset((void *) region->addr, 0, region->size); |
paul@236 | 1093 | |
paul@236 | 1094 | if ((token = read_token("Filename")) == NULL) |
paul@236 | 1095 | return; |
paul@236 | 1096 | |
paul@236 | 1097 | /* Populate the region from the file. */ |
paul@236 | 1098 | |
paul@236 | 1099 | fp = fopen(token, "r"); |
paul@236 | 1100 | |
paul@236 | 1101 | if (fp == NULL) |
paul@236 | 1102 | { |
paul@236 | 1103 | printf("File not readable.\n"); |
paul@236 | 1104 | return; |
paul@236 | 1105 | } |
paul@236 | 1106 | |
paul@236 | 1107 | fread((char *) region->addr, sizeof(char), region->size, fp); |
paul@236 | 1108 | fclose(fp); |
paul@236 | 1109 | |
paul@236 | 1110 | l4_cache_flush_data(region->addr, region->addr + region->size); |
paul@236 | 1111 | } |
paul@236 | 1112 | |
paul@236 | 1113 | |
paul@236 | 1114 | |
paul@237 | 1115 | /* RTC operations. */ |
paul@237 | 1116 | |
paul@238 | 1117 | static void rtc_reset(void *rtc, void *cpm) |
paul@238 | 1118 | { |
paul@238 | 1119 | unsigned int seconds; |
paul@238 | 1120 | |
paul@238 | 1121 | if (!read_number("Seconds", &seconds)) |
paul@238 | 1122 | return; |
paul@238 | 1123 | |
paul@238 | 1124 | /* NOTE: Assuming EXCLK/512 as RTC source. */ |
paul@238 | 1125 | |
paul@238 | 1126 | uint32_t rtc_seconds = rtc_get_seconds(rtc); |
paul@238 | 1127 | uint32_t value = seconds * cpm_get_frequency(cpm, Clock_external) / 512; |
paul@238 | 1128 | |
paul@239 | 1129 | rtc_alarm_disable(rtc); |
paul@238 | 1130 | rtc_set_alarm_seconds(rtc, rtc_seconds + value); |
paul@238 | 1131 | rtc_hibernate(rtc); |
paul@238 | 1132 | } |
paul@238 | 1133 | |
paul@237 | 1134 | static void _rtc_set_seconds(void *rtc, int alarm) |
paul@237 | 1135 | { |
paul@237 | 1136 | unsigned int seconds; |
paul@237 | 1137 | |
paul@237 | 1138 | if (!read_number("Seconds", &seconds)) |
paul@237 | 1139 | return; |
paul@237 | 1140 | |
paul@237 | 1141 | (alarm ? rtc_set_alarm_seconds : rtc_set_seconds)(rtc, seconds); |
paul@237 | 1142 | } |
paul@237 | 1143 | |
paul@237 | 1144 | |
paul@237 | 1145 | |
paul@218 | 1146 | /* SPI operations. */ |
paul@218 | 1147 | |
paul@222 | 1148 | static void new_spi_channel(void *spi, void *gpio[]) |
paul@221 | 1149 | { |
paul@222 | 1150 | unsigned int control_port, control_pin, control_alt_func_input; |
paul@222 | 1151 | int control_alt_func; |
paul@222 | 1152 | void *control_chip; |
paul@221 | 1153 | int num = get_channel_number(num_spi_channels); |
paul@221 | 1154 | void *channel; |
paul@221 | 1155 | uint32_t frequency; |
paul@221 | 1156 | |
paul@221 | 1157 | if (num < 0) |
paul@221 | 1158 | return; |
paul@221 | 1159 | |
paul@221 | 1160 | channel = get_channel(num_dma_channels, dma_channels, NULL); |
paul@221 | 1161 | |
paul@221 | 1162 | if (channel == NULL) |
paul@221 | 1163 | return; |
paul@221 | 1164 | |
paul@221 | 1165 | if (!read_number("Frequency", &frequency)) |
paul@221 | 1166 | return; |
paul@221 | 1167 | |
paul@222 | 1168 | if (!get_port_and_pin(&control_port, &control_pin)) |
paul@222 | 1169 | control_chip = NULL; |
paul@222 | 1170 | else |
paul@222 | 1171 | control_chip = gpio[control_port]; |
paul@222 | 1172 | |
paul@222 | 1173 | if (!read_number("Function", &control_alt_func_input)) |
paul@222 | 1174 | control_alt_func = -1; |
paul@222 | 1175 | else |
paul@222 | 1176 | control_alt_func = control_alt_func_input; |
paul@222 | 1177 | |
paul@222 | 1178 | spi_channels[num] = spi_get_channel(spi, num, channel, frequency, |
paul@222 | 1179 | control_chip, control_pin, |
paul@222 | 1180 | control_alt_func); |
paul@221 | 1181 | } |
paul@221 | 1182 | |
paul@221 | 1183 | static void new_spi_channel_gpio(void *gpio[]) |
paul@218 | 1184 | { |
paul@222 | 1185 | unsigned int clock_port, clock_pin, data_port, data_pin, enable_port, |
paul@222 | 1186 | enable_pin, control_port, control_pin; |
paul@222 | 1187 | void *control_chip; |
paul@218 | 1188 | int num = get_channel_number(num_spi_channels); |
paul@218 | 1189 | uint32_t frequency; |
paul@218 | 1190 | |
paul@218 | 1191 | if (num < 0) |
paul@218 | 1192 | return; |
paul@218 | 1193 | |
paul@222 | 1194 | if (!read_number("Frequency", &frequency)) |
paul@222 | 1195 | return; |
paul@222 | 1196 | |
paul@218 | 1197 | if (!get_port_and_pin(&clock_port, &clock_pin)) |
paul@218 | 1198 | return; |
paul@218 | 1199 | |
paul@218 | 1200 | if (!get_port_and_pin(&data_port, &data_pin)) |
paul@218 | 1201 | return; |
paul@218 | 1202 | |
paul@218 | 1203 | if (!get_port_and_pin(&enable_port, &enable_pin)) |
paul@218 | 1204 | return; |
paul@218 | 1205 | |
paul@222 | 1206 | if (!get_port_and_pin(&control_port, &control_pin)) |
paul@222 | 1207 | control_chip = NULL; |
paul@222 | 1208 | else |
paul@222 | 1209 | control_chip = gpio[control_port]; |
paul@218 | 1210 | |
paul@222 | 1211 | spi_channels[num] = spi_get_channel_gpio(frequency, |
paul@222 | 1212 | gpio[clock_port], clock_pin, |
paul@221 | 1213 | gpio[data_port], data_pin, |
paul@221 | 1214 | gpio[enable_port], enable_pin, |
paul@222 | 1215 | control_chip, control_pin); |
paul@222 | 1216 | } |
paul@222 | 1217 | |
paul@222 | 1218 | static void spi_control(int acquire) |
paul@222 | 1219 | { |
paul@222 | 1220 | unsigned int level; |
paul@222 | 1221 | void *channel = get_channel(num_spi_channels, spi_channels, NULL); |
paul@222 | 1222 | |
paul@222 | 1223 | if (acquire) |
paul@222 | 1224 | { |
paul@222 | 1225 | if (!read_number("Level", &level)) |
paul@222 | 1226 | return; |
paul@222 | 1227 | |
paul@222 | 1228 | spi_acquire_control(channel, level); |
paul@222 | 1229 | } |
paul@222 | 1230 | else |
paul@222 | 1231 | spi_release_control(channel); |
paul@218 | 1232 | } |
paul@218 | 1233 | |
paul@235 | 1234 | static void spi_send_data(void) |
paul@218 | 1235 | { |
paul@218 | 1236 | void *channel = get_channel(num_spi_channels, spi_channels, NULL); |
paul@218 | 1237 | int bytes; |
paul@218 | 1238 | unsigned int byte; |
paul@218 | 1239 | uint8_t buffer[256]; |
paul@218 | 1240 | |
paul@218 | 1241 | bytes = 0; |
paul@218 | 1242 | |
paul@218 | 1243 | while (read_encoded_number(NULL, "%2x", &byte)) |
paul@218 | 1244 | { |
paul@218 | 1245 | buffer[bytes] = (uint8_t) (byte & 0xff); |
paul@218 | 1246 | bytes++; |
paul@218 | 1247 | } |
paul@218 | 1248 | |
paul@235 | 1249 | spi_send(channel, bytes, buffer); |
paul@221 | 1250 | } |
paul@221 | 1251 | |
paul@221 | 1252 | static void spi_send_data_units(void) |
paul@221 | 1253 | { |
paul@221 | 1254 | void *channel = get_channel(num_spi_channels, spi_channels, NULL); |
paul@221 | 1255 | unsigned int char_size, unit_size, value; |
paul@221 | 1256 | uint8_t buffer[256]; |
paul@221 | 1257 | int byte = 0; |
paul@221 | 1258 | |
paul@221 | 1259 | if (!read_number("Unit size", &unit_size)) |
paul@221 | 1260 | return; |
paul@221 | 1261 | |
paul@221 | 1262 | if (!read_number("Character size", &char_size)) |
paul@221 | 1263 | return; |
paul@221 | 1264 | |
paul@221 | 1265 | /* Read hex digits for bytes. Multiple bytes make up each unit and are read |
paul@221 | 1266 | from most to least significant. Where the unit size exceeds the character |
paul@221 | 1267 | size, the last bit before the character indicates the GPC bit. */ |
paul@221 | 1268 | |
paul@221 | 1269 | while ((byte < 256) && read_encoded_number(NULL, "%2x", &value)) |
paul@221 | 1270 | buffer[byte++] = value; |
paul@221 | 1271 | |
paul@236 | 1272 | /* Explicitly indicate big endian data. */ |
paul@236 | 1273 | |
paul@236 | 1274 | spi_send_units(channel, byte, (uint8_t *) buffer, unit_size, char_size, 1); |
paul@221 | 1275 | } |
paul@221 | 1276 | |
paul@221 | 1277 | static void spi_transfer_data(void) |
paul@221 | 1278 | { |
paul@221 | 1279 | void *channel = get_channel(num_spi_channels, spi_channels, NULL); |
paul@236 | 1280 | struct dma_region *dma_region = NULL, *desc_region; |
paul@236 | 1281 | struct memory_region *memory_region = NULL; |
paul@221 | 1282 | unsigned int char_size, unit_size; |
paul@221 | 1283 | uint32_t count, transferred; |
paul@236 | 1284 | int using_dma; |
paul@221 | 1285 | |
paul@221 | 1286 | if (channel == NULL) |
paul@221 | 1287 | return; |
paul@221 | 1288 | |
paul@236 | 1289 | if (!read_flag("Using DMA", &using_dma, "d", 1)) |
paul@236 | 1290 | return; |
paul@221 | 1291 | |
paul@236 | 1292 | if (using_dma) |
paul@236 | 1293 | dma_region = _get_dma_region(); |
paul@236 | 1294 | else |
paul@236 | 1295 | memory_region = _get_memory_region(); |
paul@236 | 1296 | |
paul@236 | 1297 | if ((dma_region == NULL) && (memory_region == NULL)) |
paul@221 | 1298 | return; |
paul@221 | 1299 | |
paul@221 | 1300 | if (!read_number("Transfer size", &count)) |
paul@221 | 1301 | return; |
paul@221 | 1302 | |
paul@221 | 1303 | if (!read_number("Unit size", &unit_size)) |
paul@221 | 1304 | return; |
paul@221 | 1305 | |
paul@221 | 1306 | if (!read_number("Character size", &char_size)) |
paul@221 | 1307 | return; |
paul@221 | 1308 | |
paul@236 | 1309 | if (using_dma) |
paul@236 | 1310 | { |
paul@236 | 1311 | desc_region = _get_dma_region(); |
paul@223 | 1312 | |
paul@236 | 1313 | transferred = spi_transfer(channel, dma_region->vaddr, dma_region->paddr, count, |
paul@236 | 1314 | unit_size, char_size, |
paul@236 | 1315 | desc_region != NULL ? desc_region->vaddr : 0, |
paul@236 | 1316 | desc_region != NULL ? desc_region->paddr : 0); |
paul@236 | 1317 | } |
paul@236 | 1318 | else |
paul@236 | 1319 | transferred = spi_transfer(channel, memory_region->addr, 0, count, |
paul@236 | 1320 | unit_size, char_size, 0, 0); |
paul@221 | 1321 | |
paul@221 | 1322 | printf("Transferred: %d\n", transferred); |
paul@218 | 1323 | } |
paul@218 | 1324 | |
paul@218 | 1325 | |
paul@218 | 1326 | |
paul@253 | 1327 | /* TCU configuration. */ |
paul@253 | 1328 | |
paul@253 | 1329 | static l4_uint32_t tcu_irq_start = 0, tcu_irq_end = 0; |
paul@253 | 1330 | |
paul@253 | 1331 | /* TCU operations. */ |
paul@253 | 1332 | |
paul@253 | 1333 | static int init_tcu(void) |
paul@253 | 1334 | { |
paul@253 | 1335 | /* Here, only one IRQ is used. */ |
paul@253 | 1336 | |
paul@253 | 1337 | tcu_irq = l4re_util_cap_alloc(); |
paul@253 | 1338 | return init_irq(0, tcu_irq, tcu_irq_start, tcu_irq_end); |
paul@253 | 1339 | } |
paul@253 | 1340 | |
paul@253 | 1341 | static void disable_tcu_counter(void) |
paul@253 | 1342 | { |
paul@253 | 1343 | int num = get_channel_number(num_tcu_channels); |
paul@253 | 1344 | void *channel; |
paul@253 | 1345 | |
paul@253 | 1346 | if (num < 0) |
paul@253 | 1347 | return; |
paul@253 | 1348 | |
paul@253 | 1349 | channel = tcu_channels[num]; |
paul@253 | 1350 | |
paul@253 | 1351 | if (channel == NULL) |
paul@253 | 1352 | return; |
paul@253 | 1353 | |
paul@253 | 1354 | tcu_disable(channel); |
paul@253 | 1355 | } |
paul@253 | 1356 | |
paul@253 | 1357 | static void enable_tcu_counter(void) |
paul@253 | 1358 | { |
paul@253 | 1359 | int num = get_channel_number(num_tcu_channels); |
paul@253 | 1360 | void *channel; |
paul@253 | 1361 | |
paul@253 | 1362 | if (num < 0) |
paul@253 | 1363 | return; |
paul@253 | 1364 | |
paul@253 | 1365 | channel = tcu_channels[num]; |
paul@253 | 1366 | |
paul@253 | 1367 | if (channel == NULL) |
paul@253 | 1368 | return; |
paul@253 | 1369 | |
paul@253 | 1370 | tcu_enable(channel); |
paul@253 | 1371 | } |
paul@253 | 1372 | |
paul@253 | 1373 | static void new_tcu_channel(void *tcu) |
paul@253 | 1374 | { |
paul@253 | 1375 | int num = get_channel_number(num_tcu_channels); |
paul@253 | 1376 | |
paul@253 | 1377 | if (num < 0) |
paul@253 | 1378 | return; |
paul@253 | 1379 | |
paul@253 | 1380 | tcu_channels[num] = tcu_get_channel(tcu, num, tcu_irq); |
paul@253 | 1381 | } |
paul@253 | 1382 | |
paul@253 | 1383 | static void list_tcu_channels(void) |
paul@253 | 1384 | { |
paul@253 | 1385 | unsigned int num; |
paul@253 | 1386 | void *channel; |
paul@253 | 1387 | |
paul@253 | 1388 | printf(" Clock.. Counter......... Mask..............\n"); |
paul@253 | 1389 | printf("Channel Status C Pre Cnt Half Full Half Full Int\n"); |
paul@253 | 1390 | |
paul@253 | 1391 | for (num = 0; num < num_tcu_channels; num++) |
paul@253 | 1392 | { |
paul@253 | 1393 | printf("%d ", num); |
paul@253 | 1394 | |
paul@253 | 1395 | channel = tcu_channels[num]; |
paul@253 | 1396 | |
paul@253 | 1397 | if (channel == NULL) |
paul@253 | 1398 | printf("inactive\n"); |
paul@253 | 1399 | else |
paul@253 | 1400 | printf("%s %d %4d %04x %04x %04x %s %s %s\n", |
paul@253 | 1401 | tcu_is_enabled(channel) ? "enabled " : "disabled", |
paul@253 | 1402 | tcu_get_clock(channel), |
paul@253 | 1403 | tcu_get_prescale(channel), |
paul@253 | 1404 | tcu_get_counter(channel), |
paul@253 | 1405 | tcu_get_half_data_value(channel), |
paul@253 | 1406 | tcu_get_full_data_value(channel), |
paul@253 | 1407 | tcu_get_half_data_mask(channel) ? "masked " : "unmasked", |
paul@253 | 1408 | tcu_get_full_data_mask(channel) ? "masked " : "unmasked", |
paul@253 | 1409 | tcu_have_interrupt(channel) ? "!" : "_"); |
paul@253 | 1410 | } |
paul@253 | 1411 | } |
paul@253 | 1412 | |
paul@253 | 1413 | static void set_tcu_counter(void) |
paul@253 | 1414 | { |
paul@253 | 1415 | int num = get_channel_number(num_tcu_channels); |
paul@253 | 1416 | void *channel; |
paul@253 | 1417 | uint32_t counter; |
paul@253 | 1418 | |
paul@253 | 1419 | if (num < 0) |
paul@253 | 1420 | return; |
paul@253 | 1421 | |
paul@253 | 1422 | channel = tcu_channels[num]; |
paul@253 | 1423 | |
paul@253 | 1424 | if (channel == NULL) |
paul@253 | 1425 | return; |
paul@253 | 1426 | |
paul@253 | 1427 | if (!read_number("Value", &counter)) |
paul@253 | 1428 | return; |
paul@253 | 1429 | |
paul@253 | 1430 | tcu_set_counter(channel, counter); |
paul@253 | 1431 | } |
paul@253 | 1432 | |
paul@253 | 1433 | static void set_tcu_clock(void) |
paul@253 | 1434 | { |
paul@253 | 1435 | int num = get_channel_number(num_tcu_channels); |
paul@253 | 1436 | void *channel; |
paul@253 | 1437 | uint32_t clock; |
paul@253 | 1438 | |
paul@253 | 1439 | if (num < 0) |
paul@253 | 1440 | return; |
paul@253 | 1441 | |
paul@253 | 1442 | channel = tcu_channels[num]; |
paul@253 | 1443 | |
paul@253 | 1444 | if (channel == NULL) |
paul@253 | 1445 | return; |
paul@253 | 1446 | |
paul@253 | 1447 | if (!read_number("Clock", &clock)) |
paul@253 | 1448 | return; |
paul@253 | 1449 | |
paul@253 | 1450 | tcu_set_clock(channel, clock); |
paul@253 | 1451 | } |
paul@253 | 1452 | |
paul@253 | 1453 | static void set_tcu_full(void) |
paul@253 | 1454 | { |
paul@253 | 1455 | int num = get_channel_number(num_tcu_channels); |
paul@253 | 1456 | void *channel; |
paul@253 | 1457 | uint32_t value; |
paul@253 | 1458 | |
paul@253 | 1459 | if (num < 0) |
paul@253 | 1460 | return; |
paul@253 | 1461 | |
paul@253 | 1462 | channel = tcu_channels[num]; |
paul@253 | 1463 | |
paul@253 | 1464 | if (channel == NULL) |
paul@253 | 1465 | return; |
paul@253 | 1466 | |
paul@253 | 1467 | if (!read_number("Full value", &value)) |
paul@253 | 1468 | return; |
paul@253 | 1469 | |
paul@253 | 1470 | tcu_set_full_data_value(channel, value); |
paul@253 | 1471 | } |
paul@253 | 1472 | |
paul@253 | 1473 | static void set_tcu_half(void) |
paul@253 | 1474 | { |
paul@253 | 1475 | int num = get_channel_number(num_tcu_channels); |
paul@253 | 1476 | void *channel; |
paul@253 | 1477 | uint32_t value; |
paul@253 | 1478 | |
paul@253 | 1479 | if (num < 0) |
paul@253 | 1480 | return; |
paul@253 | 1481 | |
paul@253 | 1482 | channel = tcu_channels[num]; |
paul@253 | 1483 | |
paul@253 | 1484 | if (channel == NULL) |
paul@253 | 1485 | return; |
paul@253 | 1486 | |
paul@253 | 1487 | if (!read_number("Half value", &value)) |
paul@253 | 1488 | return; |
paul@253 | 1489 | |
paul@253 | 1490 | tcu_set_half_data_value(channel, value); |
paul@253 | 1491 | } |
paul@253 | 1492 | |
paul@253 | 1493 | static void set_tcu_mask(int full_mask, int masked) |
paul@253 | 1494 | { |
paul@253 | 1495 | int num = get_channel_number(num_tcu_channels); |
paul@253 | 1496 | void *channel; |
paul@253 | 1497 | |
paul@253 | 1498 | if (num < 0) |
paul@253 | 1499 | return; |
paul@253 | 1500 | |
paul@253 | 1501 | channel = tcu_channels[num]; |
paul@253 | 1502 | |
paul@253 | 1503 | if (channel == NULL) |
paul@253 | 1504 | return; |
paul@253 | 1505 | |
paul@253 | 1506 | if (full_mask) |
paul@253 | 1507 | tcu_set_full_data_mask(channel, masked); |
paul@253 | 1508 | else |
paul@253 | 1509 | tcu_set_half_data_mask(channel, masked); |
paul@253 | 1510 | } |
paul@253 | 1511 | |
paul@253 | 1512 | static void set_tcu_prescale(void) |
paul@253 | 1513 | { |
paul@253 | 1514 | int num = get_channel_number(num_tcu_channels); |
paul@253 | 1515 | void *channel; |
paul@253 | 1516 | uint32_t prescale; |
paul@253 | 1517 | |
paul@253 | 1518 | if (num < 0) |
paul@253 | 1519 | return; |
paul@253 | 1520 | |
paul@253 | 1521 | channel = tcu_channels[num]; |
paul@253 | 1522 | |
paul@253 | 1523 | if (channel == NULL) |
paul@253 | 1524 | return; |
paul@253 | 1525 | |
paul@253 | 1526 | if (!read_number("Prescale", &prescale)) |
paul@253 | 1527 | return; |
paul@253 | 1528 | |
paul@253 | 1529 | tcu_set_prescale(channel, prescale); |
paul@253 | 1530 | } |
paul@253 | 1531 | |
paul@253 | 1532 | static void tcu_wait(void) |
paul@253 | 1533 | { |
paul@253 | 1534 | int num = get_channel_number(num_tcu_channels); |
paul@253 | 1535 | void *channel; |
paul@253 | 1536 | uint32_t timeout; |
paul@253 | 1537 | |
paul@253 | 1538 | if (num < 0) |
paul@253 | 1539 | return; |
paul@253 | 1540 | |
paul@253 | 1541 | channel = tcu_channels[num]; |
paul@253 | 1542 | |
paul@253 | 1543 | if (channel == NULL) |
paul@253 | 1544 | return; |
paul@253 | 1545 | |
paul@253 | 1546 | if (!read_number("Timeout", &timeout)) |
paul@253 | 1547 | return; |
paul@253 | 1548 | |
paul@253 | 1549 | printf("IRQ received: %s\n", tcu_wait_for_irq(channel, timeout) ? "yes" : "no"); |
paul@253 | 1550 | } |
paul@253 | 1551 | |
paul@253 | 1552 | |
paul@253 | 1553 | |
paul@218 | 1554 | /* Command processing. */ |
paul@218 | 1555 | |
paul@218 | 1556 | static void handle_aic(void *aic) |
paul@218 | 1557 | { |
paul@218 | 1558 | char *token; |
paul@218 | 1559 | |
paul@218 | 1560 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1561 | { |
paul@218 | 1562 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1563 | list_channels(num_aic_channels, aic_channels); |
paul@218 | 1564 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@218 | 1565 | new_aic_channel(aic); |
paul@218 | 1566 | else if (!strcmp(token, "t") || !strcmp(token, "transfer")) |
paul@218 | 1567 | aic_transfer_data(); |
paul@218 | 1568 | else |
paul@236 | 1569 | printf("aic channel | list | transfer\n"); |
paul@218 | 1570 | } |
paul@218 | 1571 | else |
paul@218 | 1572 | list_channels(num_aic_channels, aic_channels); |
paul@218 | 1573 | } |
paul@218 | 1574 | |
paul@218 | 1575 | static void handle_cpm(void *cpm) |
paul@218 | 1576 | { |
paul@218 | 1577 | char *token; |
paul@218 | 1578 | |
paul@218 | 1579 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1580 | { |
paul@218 | 1581 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1582 | list_clocks(cpm); |
paul@218 | 1583 | else if (!strcmp(token, "f") || !strcmp(token, "frequency")) |
paul@218 | 1584 | set_frequency(cpm); |
paul@218 | 1585 | else if (!strcmp(token, "p") || !strcmp(token, "parameters")) |
paul@218 | 1586 | set_parameters(cpm); |
paul@218 | 1587 | else if (!strcmp(token, "s") || !strcmp(token, "source")) |
paul@218 | 1588 | set_source(cpm); |
paul@218 | 1589 | else if (!strcmp(token, "start")) |
paul@218 | 1590 | start_clock(cpm); |
paul@218 | 1591 | else if (!strcmp(token, "stop")) |
paul@218 | 1592 | stop_clock(cpm); |
paul@218 | 1593 | else |
paul@218 | 1594 | printf("cpm list | frequency | parameters | source | start | stop\n"); |
paul@218 | 1595 | } |
paul@218 | 1596 | else |
paul@218 | 1597 | list_clocks(cpm); |
paul@218 | 1598 | } |
paul@218 | 1599 | |
paul@218 | 1600 | static void handle_dma(void *dma) |
paul@218 | 1601 | { |
paul@218 | 1602 | char *token; |
paul@218 | 1603 | |
paul@218 | 1604 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1605 | { |
paul@218 | 1606 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1607 | list_channels(num_dma_channels, dma_channels); |
paul@218 | 1608 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@218 | 1609 | new_dma_channel(dma); |
paul@218 | 1610 | else if (!strcmp(token, "r") || !strcmp(token, "region")) |
paul@218 | 1611 | new_dma_region(); |
paul@218 | 1612 | else if (!strcmp(token, "s") || !strcmp(token, "set")) |
paul@218 | 1613 | set_dma_region(); |
paul@218 | 1614 | else if (!strcmp(token, "t") || !strcmp(token, "transfer")) |
paul@218 | 1615 | dma_transfer_data(); |
paul@218 | 1616 | else |
paul@218 | 1617 | printf("dma channel | list | region | set | transfer\n"); |
paul@218 | 1618 | } |
paul@218 | 1619 | else |
paul@218 | 1620 | list_channels(num_dma_channels, dma_channels); |
paul@218 | 1621 | } |
paul@218 | 1622 | |
paul@218 | 1623 | static void handle_gpio(void *gpio[]) |
paul@218 | 1624 | { |
paul@218 | 1625 | char *token; |
paul@218 | 1626 | |
paul@218 | 1627 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1628 | { |
paul@218 | 1629 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1630 | list_gpios(gpio); |
paul@218 | 1631 | else if (!strcmp(token, "a") || !strcmp(token, "alt")) |
paul@218 | 1632 | set_gpio_alt_func(gpio); |
paul@218 | 1633 | else if (!strcmp(token, "i") || !strcmp(token, "io")) |
paul@218 | 1634 | set_gpio_pad(gpio); |
paul@218 | 1635 | else if (!strcmp(token, "p") || !strcmp(token, "pull")) |
paul@218 | 1636 | set_gpio_pull(gpio); |
paul@218 | 1637 | else |
paul@218 | 1638 | printf("gpio list | alt | io | pull\n"); |
paul@218 | 1639 | } |
paul@218 | 1640 | else |
paul@218 | 1641 | list_gpios(gpio); |
paul@218 | 1642 | } |
paul@218 | 1643 | |
paul@218 | 1644 | static void handle_i2c(void *i2c) |
paul@218 | 1645 | { |
paul@218 | 1646 | char *token; |
paul@218 | 1647 | |
paul@218 | 1648 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1649 | { |
paul@218 | 1650 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1651 | list_i2c_channels(); |
paul@218 | 1652 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@218 | 1653 | new_i2c_channel(i2c); |
paul@218 | 1654 | else if (!strcmp(token, "g") || !strcmp(token, "get")) |
paul@218 | 1655 | i2c_get(); |
paul@218 | 1656 | else if (!strcmp(token, "s") || !strcmp(token, "scan")) |
paul@218 | 1657 | i2c_scan(); |
paul@218 | 1658 | else |
paul@218 | 1659 | printf("i2c channel | get | list | scan\n"); |
paul@218 | 1660 | } |
paul@218 | 1661 | else |
paul@218 | 1662 | list_i2c_channels(); |
paul@218 | 1663 | } |
paul@218 | 1664 | |
paul@236 | 1665 | static void handle_memory(void) |
paul@236 | 1666 | { |
paul@236 | 1667 | char *token; |
paul@236 | 1668 | |
paul@236 | 1669 | if ((token = read_token(NULL)) != NULL) |
paul@236 | 1670 | { |
paul@236 | 1671 | if (!strcmp(token, "r") || !strcmp(token, "region")) |
paul@236 | 1672 | new_memory_region(); |
paul@236 | 1673 | else if (!strcmp(token, "s") || !strcmp(token, "set")) |
paul@236 | 1674 | set_memory_region(); |
paul@236 | 1675 | else |
paul@236 | 1676 | printf("memory region | set\n"); |
paul@236 | 1677 | } |
paul@236 | 1678 | else |
paul@236 | 1679 | list_memory_regions(); |
paul@236 | 1680 | } |
paul@236 | 1681 | |
paul@238 | 1682 | static void handle_rtc(void *rtc, void *cpm) |
paul@237 | 1683 | { |
paul@237 | 1684 | char *token; |
paul@237 | 1685 | |
paul@237 | 1686 | if ((token = read_token(NULL)) != NULL) |
paul@237 | 1687 | { |
paul@237 | 1688 | if (!strcmp(token, "d") || !strcmp(token, "disable")) |
paul@237 | 1689 | rtc_disable(rtc); |
paul@237 | 1690 | else if (!strcmp(token, "e") || !strcmp(token, "enable")) |
paul@237 | 1691 | rtc_enable(rtc); |
paul@237 | 1692 | else if (!strcmp(token, "g") || !strcmp(token, "get")) |
paul@237 | 1693 | printf("seconds = %d\n", rtc_get_seconds(rtc)); |
paul@237 | 1694 | else if (!strcmp(token, "ga") || !strcmp(token, "get-alarm")) |
paul@237 | 1695 | printf("seconds = %d\n", rtc_get_alarm_seconds(rtc)); |
paul@237 | 1696 | else if (!strcmp(token, "p") || !strcmp(token, "power-down")) |
paul@237 | 1697 | rtc_power_down(rtc); |
paul@238 | 1698 | else if (!strcmp(token, "r") || !strcmp(token, "reset")) |
paul@238 | 1699 | rtc_reset(rtc, cpm); |
paul@237 | 1700 | else if (!strcmp(token, "s") || !strcmp(token, "set")) |
paul@237 | 1701 | _rtc_set_seconds(rtc, 0); |
paul@237 | 1702 | else if (!strcmp(token, "sa") || !strcmp(token, "set-alarm")) |
paul@237 | 1703 | _rtc_set_seconds(rtc, 1); |
paul@237 | 1704 | else |
paul@238 | 1705 | printf("rtc disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); |
paul@237 | 1706 | } |
paul@237 | 1707 | else |
paul@238 | 1708 | printf("rtc disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); |
paul@237 | 1709 | } |
paul@237 | 1710 | |
paul@221 | 1711 | static void handle_spi(void *spi, void *gpio[]) |
paul@218 | 1712 | { |
paul@218 | 1713 | char *token; |
paul@218 | 1714 | |
paul@218 | 1715 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1716 | { |
paul@218 | 1717 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1718 | list_channels(num_spi_channels, spi_channels); |
paul@222 | 1719 | else if (!strcmp(token, "a") || !strcmp(token, "control-acquire")) |
paul@222 | 1720 | spi_control(1); |
paul@222 | 1721 | else if (!strcmp(token, "r") || !strcmp(token, "control-release")) |
paul@222 | 1722 | spi_control(0); |
paul@218 | 1723 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@222 | 1724 | new_spi_channel(spi, gpio); |
paul@221 | 1725 | else if (!strcmp(token, "g") || !strcmp(token, "gpio") || !strcmp(token, "gpio-channel")) |
paul@221 | 1726 | new_spi_channel_gpio(gpio); |
paul@218 | 1727 | else if (!strcmp(token, "s") || !strcmp(token, "send")) |
paul@235 | 1728 | spi_send_data(); |
paul@221 | 1729 | else if (!strcmp(token, "S") || !strcmp(token, "send-units")) |
paul@221 | 1730 | spi_send_data_units(); |
paul@221 | 1731 | else if (!strcmp(token, "t") || !strcmp(token, "transfer")) |
paul@221 | 1732 | spi_transfer_data(); |
paul@218 | 1733 | else |
paul@222 | 1734 | printf("spi channel | control-acquire | control-release | gpio-channel | list | send | send-units | transfer\n"); |
paul@218 | 1735 | } |
paul@218 | 1736 | else |
paul@218 | 1737 | list_channels(num_spi_channels, spi_channels); |
paul@218 | 1738 | } |
paul@218 | 1739 | |
paul@253 | 1740 | static void handle_tcu(void *tcu) |
paul@253 | 1741 | { |
paul@253 | 1742 | char *token; |
paul@253 | 1743 | |
paul@253 | 1744 | if ((token = read_token(NULL)) != NULL) |
paul@253 | 1745 | { |
paul@253 | 1746 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@253 | 1747 | list_tcu_channels(); |
paul@253 | 1748 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@253 | 1749 | new_tcu_channel(tcu); |
paul@253 | 1750 | else if (!strcmp(token, "C") || !strcmp(token, "clock")) |
paul@253 | 1751 | set_tcu_clock(); |
paul@253 | 1752 | else if (!strcmp(token, "d") || !strcmp(token, "disable")) |
paul@253 | 1753 | disable_tcu_counter(); |
paul@253 | 1754 | else if (!strcmp(token, "e") || !strcmp(token, "enable")) |
paul@253 | 1755 | enable_tcu_counter(); |
paul@253 | 1756 | else if (!strcmp(token, "f") || !strcmp(token, "full")) |
paul@253 | 1757 | set_tcu_full(); |
paul@253 | 1758 | else if (!strcmp(token, "fm") || !strcmp(token, "full-mask")) |
paul@253 | 1759 | set_tcu_mask(1, 1); |
paul@253 | 1760 | else if (!strcmp(token, "fu") || !strcmp(token, "full-unmask")) |
paul@253 | 1761 | set_tcu_mask(1, 0); |
paul@253 | 1762 | else if (!strcmp(token, "h") || !strcmp(token, "half")) |
paul@253 | 1763 | set_tcu_half(); |
paul@253 | 1764 | else if (!strcmp(token, "hm") || !strcmp(token, "half-mask")) |
paul@253 | 1765 | set_tcu_mask(0, 1); |
paul@253 | 1766 | else if (!strcmp(token, "hu") || !strcmp(token, "half-unmask")) |
paul@253 | 1767 | set_tcu_mask(0, 0); |
paul@253 | 1768 | else if (!strcmp(token, "p") || !strcmp(token, "prescale")) |
paul@253 | 1769 | set_tcu_prescale(); |
paul@253 | 1770 | else if (!strcmp(token, "s") || !strcmp(token, "set")) |
paul@253 | 1771 | set_tcu_counter(); |
paul@253 | 1772 | else if (!strcmp(token, "w") || !strcmp(token, "wait")) |
paul@253 | 1773 | tcu_wait(); |
paul@253 | 1774 | else |
paul@253 | 1775 | printf("tcu channel | clock | disable | enable | full | full-mask | " \ |
paul@253 | 1776 | "full-unmask | half | half-mask | half-unmask | list | mask | " \ |
paul@253 | 1777 | "prescale | set | unmask | wait\n"); |
paul@253 | 1778 | } |
paul@253 | 1779 | else |
paul@253 | 1780 | list_tcu_channels(); |
paul@253 | 1781 | } |
paul@253 | 1782 | |
paul@218 | 1783 | |
paul@218 | 1784 | |
paul@218 | 1785 | int main(void) |
paul@218 | 1786 | { |
paul@218 | 1787 | l4_addr_t aic_base = 0, aic_base_end = 0; |
paul@218 | 1788 | l4_addr_t aic_phys_base = 0, aic_phys_base_end = 0; |
paul@218 | 1789 | l4_addr_t cpm_base = 0, cpm_base_end = 0; |
paul@218 | 1790 | l4_addr_t dma_base = 0, dma_base_end = 0; |
paul@218 | 1791 | l4_addr_t gpio_base = 0, gpio_base_end = 0; |
paul@218 | 1792 | l4_addr_t i2c_base = 0, i2c_base_end = 0; |
paul@237 | 1793 | l4_addr_t rtc_base = 0, rtc_base_end = 0; |
paul@221 | 1794 | l4_addr_t ssi_base = 0, ssi_base_end = 0; |
paul@221 | 1795 | l4_addr_t ssi_phys_base = 0, ssi_phys_base_end = 0; |
paul@253 | 1796 | l4_addr_t tcu_base = 0, tcu_base_end = 0; |
paul@253 | 1797 | void *aic, *cpm, *dma, *gpio[num_gpio_ports], *i2c, *rtc, *spi, *tcu; |
paul@218 | 1798 | int result = 0; |
paul@218 | 1799 | unsigned int port; |
paul@218 | 1800 | |
paul@218 | 1801 | icucap = l4re_env_get_cap("icu"); |
paul@218 | 1802 | |
paul@218 | 1803 | /* Obtain resource details describing I/O memory. */ |
paul@218 | 1804 | |
paul@218 | 1805 | printf("Access CPM...\n"); |
paul@218 | 1806 | |
paul@236 | 1807 | if ((result = get_memory(io_memory_regions[CPM], &cpm_base, &cpm_base_end)) < 0) |
paul@218 | 1808 | return 1; |
paul@218 | 1809 | |
paul@218 | 1810 | printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); |
paul@218 | 1811 | |
paul@218 | 1812 | cpm = cpm_init(cpm_base); |
paul@218 | 1813 | |
paul@218 | 1814 | printf("Access DMA...\n"); |
paul@218 | 1815 | |
paul@236 | 1816 | if ((result = get_memory(io_memory_regions[DMA], &dma_base, &dma_base_end)) < 0) |
paul@218 | 1817 | return 1; |
paul@218 | 1818 | |
paul@218 | 1819 | printf("DMA at 0x%lx...0x%lx.\n", dma_base, dma_base_end); |
paul@218 | 1820 | |
paul@218 | 1821 | dma = dma_init(dma_base, dma_base_end, cpm); |
paul@218 | 1822 | |
paul@236 | 1823 | if (get_irq(io_memory_regions[DMA], &dma_irq_start, &dma_irq_end) < 0) |
paul@218 | 1824 | return 1; |
paul@218 | 1825 | |
paul@218 | 1826 | printf("IRQ range at %d...%d.\n", dma_irq_start, dma_irq_end); |
paul@218 | 1827 | |
paul@218 | 1828 | if (init_dma()) |
paul@218 | 1829 | return 1; |
paul@218 | 1830 | |
paul@218 | 1831 | dma_enable(dma); |
paul@218 | 1832 | |
paul@218 | 1833 | printf("Access GPIO...\n"); |
paul@218 | 1834 | |
paul@236 | 1835 | if ((result = get_memory(io_memory_regions[GPIO], &gpio_base, &gpio_base_end)) < 0) |
paul@218 | 1836 | return 1; |
paul@218 | 1837 | |
paul@218 | 1838 | printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); |
paul@218 | 1839 | |
paul@218 | 1840 | for (port = 0; port < num_gpio_ports; port++) |
paul@218 | 1841 | gpio[port] = gpio_init(gpio_base + port * 0x100, gpio_base + (port + 1) * 0x100, |
paul@218 | 1842 | 32, gpio_ports[port].pull_ups, gpio_ports[port].pull_downs); |
paul@218 | 1843 | |
paul@218 | 1844 | printf("Access I2C...\n"); |
paul@218 | 1845 | |
paul@236 | 1846 | if ((result = get_memory(io_memory_regions[I2C], &i2c_base, &i2c_base_end)) < 0) |
paul@218 | 1847 | return 1; |
paul@218 | 1848 | |
paul@218 | 1849 | printf("I2C at 0x%lx...0x%lx.\n", i2c_base, i2c_base_end); |
paul@218 | 1850 | |
paul@218 | 1851 | i2c = i2c_init(i2c_base, i2c_base_end, cpm, 100000); |
paul@218 | 1852 | |
paul@236 | 1853 | if (get_irq(io_memory_regions[I2C], &i2c_irq_start, &i2c_irq_end) < 0) |
paul@218 | 1854 | return 1; |
paul@218 | 1855 | |
paul@218 | 1856 | printf("IRQ range at %d...%d.\n", i2c_irq_start, i2c_irq_end); |
paul@218 | 1857 | |
paul@218 | 1858 | printf("Access AIC...\n"); |
paul@218 | 1859 | |
paul@236 | 1860 | if ((result = get_memory_complete(io_memory_regions[AIC], &aic_base, &aic_base_end, |
paul@218 | 1861 | &aic_phys_base, &aic_phys_base_end)) < 0) |
paul@218 | 1862 | return 1; |
paul@218 | 1863 | |
paul@218 | 1864 | printf("AIC at 0x%lx...0x%lx.\n", aic_base, aic_base_end); |
paul@218 | 1865 | |
paul@218 | 1866 | aic = aic_init(aic_phys_base, aic_base, aic_base_end, cpm); |
paul@218 | 1867 | |
paul@237 | 1868 | printf("Access RTC...\n"); |
paul@237 | 1869 | |
paul@237 | 1870 | if ((result = get_memory(io_memory_regions[RTC], &rtc_base, &rtc_base_end)) < 0) |
paul@237 | 1871 | return 1; |
paul@237 | 1872 | |
paul@237 | 1873 | printf("RTC at 0x%lx...0x%lx.\n", rtc_base, rtc_base_end); |
paul@237 | 1874 | |
paul@240 | 1875 | rtc = rtc_init(rtc_base, cpm); |
paul@237 | 1876 | |
paul@221 | 1877 | printf("Access SSI...\n"); |
paul@221 | 1878 | |
paul@236 | 1879 | if ((result = get_memory_complete(io_memory_regions[SSI], &ssi_base, &ssi_base_end, |
paul@221 | 1880 | &ssi_phys_base, &ssi_phys_base_end)) < 0) |
paul@218 | 1881 | return 1; |
paul@218 | 1882 | |
paul@221 | 1883 | printf("SSI at 0x%lx...0x%lx.\n", ssi_base, ssi_base_end); |
paul@221 | 1884 | |
paul@221 | 1885 | spi = spi_init(ssi_phys_base, ssi_base, ssi_base_end, cpm); |
paul@218 | 1886 | |
paul@253 | 1887 | printf("Access TCU...\n"); |
paul@253 | 1888 | |
paul@253 | 1889 | if ((result = get_memory(io_memory_regions[TCU], &tcu_base, &tcu_base_end)) < 0) |
paul@253 | 1890 | return 1; |
paul@253 | 1891 | |
paul@253 | 1892 | printf("TCU at 0x%lx...0x%lx.\n", tcu_base, tcu_base_end); |
paul@253 | 1893 | |
paul@253 | 1894 | tcu = tcu_init(tcu_base, tcu_base_end); |
paul@253 | 1895 | |
paul@253 | 1896 | if (get_irq(io_memory_regions[TCU], &tcu_irq_start, &tcu_irq_end) < 0) |
paul@253 | 1897 | return 1; |
paul@253 | 1898 | |
paul@253 | 1899 | printf("IRQ range at %d...%d.\n", tcu_irq_start, tcu_irq_end); |
paul@253 | 1900 | |
paul@253 | 1901 | if (init_tcu()) |
paul@253 | 1902 | return 1; |
paul@253 | 1903 | |
paul@218 | 1904 | /* Start the interactive session. */ |
paul@218 | 1905 | |
paul@253 | 1906 | printf("aic, cpm, dma, gpio, i2c, rtc, spi, tcu\n"); |
paul@218 | 1907 | |
paul@218 | 1908 | while (1) |
paul@218 | 1909 | { |
paul@218 | 1910 | char cmdline[256], *token; |
paul@218 | 1911 | |
paul@218 | 1912 | printf("> "); |
paul@218 | 1913 | |
paul@221 | 1914 | token = fgets(cmdline, 256, stdin); |
paul@218 | 1915 | |
paul@218 | 1916 | if (token == NULL) |
paul@218 | 1917 | break; |
paul@218 | 1918 | |
paul@218 | 1919 | if ((token = strtok(cmdline, delimiters)) == NULL) |
paul@218 | 1920 | continue; |
paul@218 | 1921 | |
paul@218 | 1922 | /* AIC/I2S commands. */ |
paul@218 | 1923 | |
paul@218 | 1924 | if (!strcmp(token, "a") || !strcmp(token, "aic") || !strcmp(token, "i2s")) |
paul@218 | 1925 | handle_aic(aic); |
paul@218 | 1926 | |
paul@218 | 1927 | /* CPM commands. */ |
paul@218 | 1928 | |
paul@218 | 1929 | else if (!strcmp(token, "c") || !strcmp(token, "cpm")) |
paul@218 | 1930 | handle_cpm(cpm); |
paul@218 | 1931 | |
paul@218 | 1932 | /* DMA commands. */ |
paul@218 | 1933 | |
paul@218 | 1934 | else if (!strcmp(token, "d") || !strcmp(token, "dma")) |
paul@218 | 1935 | handle_dma(dma); |
paul@218 | 1936 | |
paul@218 | 1937 | /* GPIO commands. */ |
paul@218 | 1938 | |
paul@218 | 1939 | else if (!strcmp(token, "g") || !strcmp(token, "gpio")) |
paul@218 | 1940 | handle_gpio(gpio); |
paul@218 | 1941 | |
paul@218 | 1942 | /* I2C commands. */ |
paul@218 | 1943 | |
paul@218 | 1944 | else if (!strcmp(token, "i") || !strcmp(token, "i2c")) |
paul@218 | 1945 | handle_i2c(i2c); |
paul@218 | 1946 | |
paul@236 | 1947 | /* Generic memory commands. */ |
paul@236 | 1948 | |
paul@236 | 1949 | else if (!strcmp(token, "m") || !strcmp(token, "mem") || !strcmp(token, "memory")) |
paul@236 | 1950 | handle_memory(); |
paul@236 | 1951 | |
paul@237 | 1952 | /* RTC commands. */ |
paul@237 | 1953 | |
paul@237 | 1954 | else if (!strcmp(token, "r") || !strcmp(token, "rtc")) |
paul@238 | 1955 | handle_rtc(rtc, cpm); |
paul@237 | 1956 | |
paul@218 | 1957 | /* SPI commands. */ |
paul@218 | 1958 | |
paul@218 | 1959 | else if (!strcmp(token, "s") || !strcmp(token, "spi")) |
paul@221 | 1960 | handle_spi(spi, gpio); |
paul@218 | 1961 | |
paul@253 | 1962 | /* TCU commands. */ |
paul@253 | 1963 | |
paul@253 | 1964 | else if (!strcmp(token, "t") || !strcmp(token, "tcu")) |
paul@253 | 1965 | handle_tcu(tcu); |
paul@253 | 1966 | |
paul@221 | 1967 | /* Comments and blank lines. */ |
paul@221 | 1968 | |
paul@221 | 1969 | else if (strncmp(token, "#", 1) && strlen(token)) |
paul@218 | 1970 | printf("Command?\n"); |
paul@218 | 1971 | } |
paul@218 | 1972 | |
paul@218 | 1973 | printf("End of session.\n"); |
paul@218 | 1974 | return 0; |
paul@218 | 1975 | } |