C8051F32x firmware infrastructure

Sign in or create your account | Project List | Help

C8051F32x firmware infrastructure Git Source Tree

Root/f32x/boundary.c

1/*
2 * f32x/boundary.c - I/O pin access
3 *
4 * Written 2008 by Werner Almesberger
5 * Copyright 2008 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 <stdint.h>
15#include <stdlib.h>
16#include <stdio.h>
17
18#include "c2.h"
19#include "boundary.h"
20
21
22#define P0 0x80
23#define P0MDOUT 0xa4
24#define P2 0xa0
25#define P2MDOUT 0xa6
26
27
28static uint8_t reg_read(uint8_t addr)
29{
30    c2_addr_write(addr);
31    return c2_data_read(1);
32}
33
34
35static void reg_write(uint8_t addr, uint8_t value)
36{
37    c2_addr_write(addr);
38    c2_data_write(value, 1);
39}
40
41
42static const char *parse(const char *s, uint8_t *port, uint8_t *mode, int bits)
43{
44    int pos;
45
46    *port = *mode = 0;
47    pos = 0;
48    while (pos != bits) {
49        switch (*s++) {
50        case '.':
51                continue;
52        case '1':
53            *port |= 1 << pos;
54            /* fall through */
55        case '0':
56            *mode |= 1 << pos;
57            break;
58        case 'r':
59        case 'R':
60            *port |= 1 << pos;
61            break;
62        case 0:
63            fprintf(stderr, "not enough pin settings\n");
64            exit(1);
65        default:
66            fprintf(stderr,
67                "unrecognized pin setting \"%c\"\n", s[-1]);
68            exit(1);
69        }
70        pos++;
71        }
72    return s;
73}
74
75
76static void setup(const char *init)
77{
78    uint8_t p0, p0mdout, p2, p2mdout;
79
80    init = parse(init, &p0, &p0mdout, 8);
81    init = parse(init, &p2, &p2mdout, 6);
82    if (*init) {
83        fprintf(stderr, "too many pin settings\n");
84        exit(1);
85    }
86    reg_write(P0, p0);
87    reg_write(P0MDOUT, p0mdout);
88    reg_write(P2, p2);
89    reg_write(P2MDOUT, p2mdout);
90}
91
92
93static void print(uint8_t v, int bits)
94{
95    int pos;
96
97    for (pos = 0; pos != bits; pos++)
98        putchar(v & (1 << pos) ? '1' : '0');
99}
100
101
102static void scan(void)
103{
104    uint8_t p0, p2;
105
106    p0 = reg_read(P0);
107    p2 = reg_read(P2);
108    print(p0, 8);
109    putchar('.');
110    print(p2, 6);
111    putchar('\n');
112}
113
114
115static void __attribute__((unused)) dump(void)
116{
117    fprintf(stderr, "GPIOCN %02x\n", reg_read(0xe2));
118    fprintf(stderr, " P0 %02x\n", reg_read(P0));
119    fprintf(stderr, " P0MDOUT %02x\n", reg_read(P0MDOUT));
120    fprintf(stderr, " P2 %02x\n", reg_read(P2));
121    fprintf(stderr, " P2MDOUT %02x\n", reg_read(P2MDOUT));
122}
123
124
125void boundary(const char *init)
126{
127//dump();
128    setup(init);
129    scan();
130}
131

Archive Download this file

Branches:
master



interactive