Root/tornado/fw/ben/ben.c

Source at commit 30c683effd7ab362d1517950700c96cfa09e45e8 created 11 years 17 days ago.
By Werner Almesberger, tornado/fw/sim/p: pass arguments to "alg"; label graphs; better separate "up"
1/*
2 * ben/ben.c - Cross-platform testing on the Ben Nanonote
3 *
4 * Written 2012 by Werner Almesberger
5 * Copyright 2012 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#include <stdlib.h>
14#include <stdio.h>
15#include <string.h>
16#include <unistd.h>
17#include <fcntl.h>
18#include <sys/mman.h>
19
20#include "../mmc.h"
21#include "io.h"
22
23
24#define SOC_BASE 0x10000000
25#define PAGE_SIZE 4096
26
27
28void *ben_mem;
29
30
31static void io_setup(void)
32{
33    int fd;
34
35    fd = open("/dev/mem", O_RDWR | O_SYNC);
36    if (fd < 0) {
37        perror("/dev/mem");
38        exit(1);
39    }
40    ben_mem = mmap(NULL, PAGE_SIZE*3*16, PROT_READ | PROT_WRITE,
41        MAP_SHARED, fd, SOC_BASE);
42    if (ben_mem == MAP_FAILED) {
43        perror("mmap");
44        exit(1);
45    }
46
47    PDDATS = CARD_nPWR;
48    PDDIRS = CARD_nPWR;
49    PDDIRC = CARD_CMD | CARD_CLK |
50            CARD_DAT0 | CARD_DAT1 | CARD_DAT2 | CARD_DAT3;
51    PDFUNC = CARD_nPWR | CARD_CMD | CARD_CLK |
52        CARD_DAT0 | CARD_DAT1 | CARD_DAT2 | CARD_DAT3;
53}
54
55
56static void read_block(int addr)
57{
58    int i;
59
60    if (!mmc_begin_read(addr)) {
61        fprintf(stderr, "mmc_begin_read failed\n");
62        exit(1);
63    }
64    for (i = 0; i != MMC_BLOCK; i++)
65        printf("%02x%c", mmc_read(), (i & 15) == 15 ? '\n' : ' ');
66    mmc_end_read();
67}
68
69
70static void write_block(int addr)
71{
72    int i;
73
74    if (!mmc_begin_write(addr)) {
75        fprintf(stderr, "mmc_begin_write failed\n");
76        exit(1);
77    }
78    for (i = 0; i != MMC_BLOCK; i++)
79        mmc_write(i);
80    if (!mmc_end_write()) {
81        fprintf(stderr, "mmc_end_write failed\n");
82        exit(1);
83    }
84}
85
86
87static void usage(const char *name)
88{
89    fprintf(stderr, "usage: %s [-w] [byte_addr]\n", name);
90    exit(1);
91}
92
93
94int main(int argc, char **argv)
95{
96    int do_read = 1;
97    int addr;
98    int c;
99    char *end;
100
101    io_setup();
102
103    if (!mmc_init()) {
104        fprintf(stderr, "mmc_init failed\n");
105        exit(1);
106    }
107
108    while ((c = getopt(argc, argv, "w")) != EOF)
109        switch (c) {
110        case 'w':
111            do_read = 0;
112            break;
113        default:
114            usage(*argv);
115        }
116
117    switch (argc-optind) {
118    case 0:
119        addr = 0;
120        break;
121    case 1:
122        addr = strtoul(argv[optind], &end, 0);
123        if (*end)
124            usage(*argv);
125        break;
126    default:
127        usage(*argv);
128    }
129
130    if (do_read)
131        read_block(addr);
132    else
133        write_block(addr);
134
135    return 0;
136}
137

Archive Download this file

Branches:
master
tornado-v1



interactive