C8051F32x firmware infrastructure
Sign in or create your account | Project List | Help
C8051F32x firmware infrastructure Commit Details
Date: | 2010-10-21 15:05:26 (13 years 5 months ago) |
---|---|
Author: | Werner Almesberger |
Commit: | 3bbf31853678e5b9cd9d9496437546c3ea8a06dd |
Message: | Switched from four-spaces to one-tab indentation. |
Files: |
f32x/boundary.c (1 diff) f32x/c2-om.c (3 diffs) f32x/f32x.c (2 diffs) f32x/flash.c (3 diffs) f32x/gpio.h (1 diff) f32x/rt.c (1 diff) |
Change Details
f32x/boundary.c | ||
---|---|---|
27 | 27 | |
28 | 28 | static uint8_t reg_read(uint8_t addr) |
29 | 29 | { |
30 | c2_addr_write(addr); | |
31 | return c2_data_read(1); | |
30 | c2_addr_write(addr); | |
31 | return c2_data_read(1); | |
32 | 32 | } |
33 | 33 | |
34 | 34 | |
35 | 35 | static void reg_write(uint8_t addr, uint8_t value) |
36 | 36 | { |
37 | c2_addr_write(addr); | |
38 | c2_data_write(value, 1); | |
37 | c2_addr_write(addr); | |
38 | c2_data_write(value, 1); | |
39 | 39 | } |
40 | 40 | |
41 | 41 | |
42 | 42 | static const char *parse(const char *s, uint8_t *port, uint8_t *mode, int bits) |
43 | 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, "unrecognized pin setting \"%c\"\n", s[-1]); | |
67 | exit(1); | |
68 | } | |
69 | pos++; | |
70 | } | |
71 | return s; | |
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; | |
72 | 73 | } |
73 | 74 | |
74 | 75 | |
75 | 76 | static void setup(const char *init) |
76 | 77 | { |
77 | uint8_t p0, p0mdout, p2, p2mdout; | |
78 | ||
79 | init = parse(init, &p0, &p0mdout, 8); | |
80 | init = parse(init, &p2, &p2mdout, 6); | |
81 | if (*init) { | |
82 | fprintf(stderr, "too many pin settings\n"); | |
83 | exit(1); | |
84 | } | |
85 | reg_write(P0, p0); | |
86 | reg_write(P0MDOUT, p0mdout); | |
87 | reg_write(P2, p2); | |
88 | reg_write(P2MDOUT, p2mdout); | |
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); | |
89 | 90 | } |
90 | 91 | |
91 | 92 | |
92 | 93 | static void print(uint8_t v, int bits) |
93 | 94 | { |
94 | int pos; | |
95 | int pos; | |
95 | 96 | |
96 | for (pos = 0; pos != bits; pos++) | |
97 | putchar(v & (1 << pos) ? '1' : '0'); | |
97 | for (pos = 0; pos != bits; pos++) | |
98 | putchar(v & (1 << pos) ? '1' : '0'); | |
98 | 99 | } |
99 | 100 | |
100 | 101 | |
101 | 102 | static void scan(void) |
102 | 103 | { |
103 | uint8_t p0, p2; | |
104 | ||
105 | p0 = reg_read(P0); | |
106 | p2 = reg_read(P2); | |
107 | print(p0, 8); | |
108 | putchar('.'); | |
109 | print(p2, 6); | |
110 | putchar('\n'); | |
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'); | |
111 | 112 | } |
112 | 113 | |
113 | 114 | |
114 | 115 | static void __attribute__((unused)) dump(void) |
115 | 116 | { |
116 | fprintf(stderr, "GPIOCN %02x\n", reg_read(0xe2)); | |
117 | fprintf(stderr, " P0 %02x\n", reg_read(P0)); | |
118 | fprintf(stderr, " P0MDOUT %02x\n", reg_read(P0MDOUT)); | |
119 | fprintf(stderr, " P2 %02x\n", reg_read(P2)); | |
120 | fprintf(stderr, " P2MDOUT %02x\n", reg_read(P2MDOUT)); | |
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)); | |
121 | 122 | } |
122 | 123 | |
123 | 124 | |
124 | 125 | void boundary(const char *init) |
125 | 126 | { |
126 | 127 | //dump(); |
127 | setup(init); | |
128 | scan(); | |
128 | setup(init); | |
129 | scan(); | |
129 | 130 | } |
f32x/c2-om.c | ||
---|---|---|
32 | 32 | |
33 | 33 | static void c2_pulse(void) |
34 | 34 | { |
35 | gpio_low(C2CK); | |
36 | gpio_high(C2CK); | |
35 | gpio_low(C2CK); | |
36 | gpio_high(C2CK); | |
37 | 37 | } |
38 | 38 | |
39 | 39 | |
40 | 40 | static void c2_send(uint32_t value, int bits) |
41 | 41 | { |
42 | int i; | |
42 | int i; | |
43 | 43 | |
44 | for (i = 0; i != bits; i++) { | |
45 | gpio_set(C2D, (value >> i) & 1); | |
46 | c2_pulse(); | |
47 | } | |
44 | for (i = 0; i != bits; i++) { | |
45 | gpio_set(C2D, (value >> i) & 1); | |
46 | c2_pulse(); | |
47 | } | |
48 | 48 | } |
49 | 49 | |
50 | 50 | |
51 | 51 | static uint32_t c2_recv(int bits) |
52 | 52 | { |
53 | uint32_t v = 0; | |
54 | int i; | |
55 | ||
56 | for (i = 0; i != bits; i++) { | |
57 | v |= gpio_get(C2D) << i; | |
58 | c2_pulse(); | |
59 | } | |
60 | return v; | |
53 | uint32_t v = 0; | |
54 | int i; | |
55 | ||
56 | for (i = 0; i != bits; i++) { | |
57 | v |= gpio_get(C2D) << i; | |
58 | c2_pulse(); | |
59 | } | |
60 | return v; | |
61 | 61 | } |
62 | 62 | |
63 | 63 | |
... | ... | |
66 | 66 | |
67 | 67 | static void om_addr_write(uint8_t addr) |
68 | 68 | { |
69 | c2_pulse(); | |
70 | gpio_output(C2D); | |
71 | c2_send(C2_ADDR_WRITE, 2); | |
72 | c2_send(addr, 8); | |
73 | gpio_input(C2D); | |
74 | c2_pulse(); | |
69 | c2_pulse(); | |
70 | gpio_output(C2D); | |
71 | c2_send(C2_ADDR_WRITE, 2); | |
72 | c2_send(addr, 8); | |
73 | gpio_input(C2D); | |
74 | c2_pulse(); | |
75 | 75 | } |
76 | 76 | |
77 | 77 | |
78 | 78 | static uint8_t om_addr_read(void) |
79 | 79 | { |
80 | c2_pulse(); | |
81 | gpio_output(C2D); | |
82 | c2_send(C2_ADDR_READ, 2); | |
83 | gpio_input(C2D); | |
84 | c2_pulse(); | |
85 | return c2_recv(8); | |
80 | c2_pulse(); | |
81 | gpio_output(C2D); | |
82 | c2_send(C2_ADDR_READ, 2); | |
83 | gpio_input(C2D); | |
84 | c2_pulse(); | |
85 | return c2_recv(8); | |
86 | 86 | } |
87 | 87 | |
88 | 88 | |
89 | 89 | static void om_data_write(uint32_t data, int bytes) |
90 | 90 | { |
91 | c2_pulse(); | |
92 | gpio_output(C2D); | |
93 | c2_send(C2_DATA_WRITE, 2); | |
94 | c2_send(bytes-1, 2); | |
95 | c2_send(data, 8*bytes); | |
96 | gpio_input(C2D); | |
97 | c2_pulse(); | |
98 | while (!c2_recv(1)); | |
91 | c2_pulse(); | |
92 | gpio_output(C2D); | |
93 | c2_send(C2_DATA_WRITE, 2); | |
94 | c2_send(bytes-1, 2); | |
95 | c2_send(data, 8*bytes); | |
96 | gpio_input(C2D); | |
97 | c2_pulse(); | |
98 | while (!c2_recv(1)); | |
99 | 99 | } |
100 | 100 | |
101 | 101 | |
102 | 102 | static uint32_t om_data_read(int bytes) |
103 | 103 | { |
104 | c2_pulse(); | |
105 | gpio_output(C2D); | |
106 | c2_send(C2_DATA_READ, 2); | |
107 | c2_send(bytes-1, 2); | |
108 | gpio_input(C2D); | |
109 | c2_pulse(); | |
110 | while (!c2_recv(1)); | |
111 | return c2_recv(8*bytes); | |
104 | c2_pulse(); | |
105 | gpio_output(C2D); | |
106 | c2_send(C2_DATA_READ, 2); | |
107 | c2_send(bytes-1, 2); | |
108 | gpio_input(C2D); | |
109 | c2_pulse(); | |
110 | while (!c2_recv(1)); | |
111 | return c2_recv(8*bytes); | |
112 | 112 | } |
113 | 113 | |
114 | 114 | |
... | ... | |
117 | 117 | |
118 | 118 | static void om_init(void) |
119 | 119 | { |
120 | gpio_init(); | |
121 | gpio_input(C2D); | |
122 | gpio_output(C2CK); | |
123 | gpio_low(C2CK); | |
124 | usleep(20); | |
125 | gpio_high(C2CK); | |
126 | usleep(2); | |
120 | gpio_init(); | |
121 | gpio_input(C2D); | |
122 | gpio_output(C2CK); | |
123 | gpio_low(C2CK); | |
124 | usleep(20); | |
125 | gpio_high(C2CK); | |
126 | usleep(2); | |
127 | 127 | } |
128 | 128 | |
129 | 129 | |
130 | 130 | static void om_reset(void) |
131 | 131 | { |
132 | gpio_input(C2D); | |
133 | gpio_low(C2CK); | |
134 | usleep(20); | |
135 | // gpio_input(C2CK); | |
136 | gpio_output(C2CK); | |
137 | gpio_high(C2CK); | |
132 | gpio_input(C2D); | |
133 | gpio_low(C2CK); | |
134 | usleep(20); | |
135 | // gpio_input(C2CK); | |
136 | gpio_output(C2CK); | |
137 | gpio_high(C2CK); | |
138 | 138 | } |
139 | 139 | |
140 | 140 |
f32x/f32x.c | ||
---|---|---|
30 | 30 | |
31 | 31 | static void dump(const char *title, void *data, size_t size) |
32 | 32 | { |
33 | int i, j; | |
34 | ||
35 | fprintf(stderr, "%s:\n", title); | |
36 | for (i = 0; i < size; i += 16) { | |
37 | fprintf(stderr, " %04x", i); | |
38 | for (j = 0; j != 16 && i+j < size; j++) | |
39 | fprintf(stderr, " %02x", ((uint8_t *) data)[i+j]); | |
40 | fprintf(stderr, "\n"); | |
41 | } | |
33 | int i, j; | |
34 | ||
35 | fprintf(stderr, "%s:\n", title); | |
36 | for (i = 0; i < size; i += 16) { | |
37 | fprintf(stderr, " %04x", i); | |
38 | for (j = 0; j != 16 && i+j < size; j++) | |
39 | fprintf(stderr, " %02x", ((uint8_t *) data)[i+j]); | |
40 | fprintf(stderr, "\n"); | |
41 | } | |
42 | 42 | } |
43 | 43 | |
44 | 44 | |
45 | 45 | static void flash_device(void *data, size_t size) |
46 | 46 | { |
47 | int i; | |
48 | size_t len; | |
49 | uint8_t buf[256]; | |
50 | ||
51 | for (i = 0; i < size; i += 256) | |
52 | fputc('-', stderr); | |
53 | fputc('\r', stderr); | |
54 | ||
55 | flash_init(); | |
56 | flash_device_erase(); | |
57 | ||
58 | for (i = 0; i < size; i += 256) { | |
59 | fputc('*', stderr); | |
60 | fflush(stderr); | |
61 | len = size-i <= 256 ? size-i : 256; | |
62 | flash_block_write(i, data+i, len); | |
63 | } | |
64 | fputc('\r', stderr); | |
65 | ||
66 | for (i = 0; i < size; i += 256) { | |
67 | fputc('#', stderr); | |
68 | fflush(stderr); | |
69 | len = size-i <= 256 ? size-i : 256; | |
70 | flash_block_read(i, buf, len); | |
71 | if (memcmp(buf, data+i, len)) { | |
72 | fprintf(stderr, "compare error at 0x%04x\n", i); | |
73 | dump("Expected", data+i, len); | |
74 | dump("Read", buf, len); | |
75 | exit(1); | |
47 | int i; | |
48 | size_t len; | |
49 | uint8_t buf[256]; | |
50 | ||
51 | for (i = 0; i < size; i += 256) | |
52 | fputc('-', stderr); | |
53 | fputc('\r', stderr); | |
54 | ||
55 | flash_init(); | |
56 | flash_device_erase(); | |
57 | ||
58 | for (i = 0; i < size; i += 256) { | |
59 | fputc('*', stderr); | |
60 | fflush(stderr); | |
61 | len = size-i <= 256 ? size-i : 256; | |
62 | flash_block_write(i, data+i, len); | |
63 | } | |
64 | fputc('\r', stderr); | |
65 | ||
66 | for (i = 0; i < size; i += 256) { | |
67 | fputc('#', stderr); | |
68 | fflush(stderr); | |
69 | len = size-i <= 256 ? size-i : 256; | |
70 | flash_block_read(i, buf, len); | |
71 | if (memcmp(buf, data+i, len)) { | |
72 | fprintf(stderr, "compare error at 0x%04x\n", i); | |
73 | dump("Expected", data+i, len); | |
74 | dump("Read", buf, len); | |
75 | exit(1); | |
76 | } | |
76 | 77 | } |
77 | } | |
78 | fputc('\n', stderr); | |
78 | fputc('\n', stderr); | |
79 | 79 | } |
80 | 80 | |
81 | 81 | |
82 | 82 | static void erase_flash(void) |
83 | 83 | { |
84 | flash_init(); | |
85 | flash_device_erase(); | |
84 | flash_init(); | |
85 | flash_device_erase(); | |
86 | 86 | } |
87 | 87 | |
88 | 88 | |
89 | 89 | static void dump_flash(size_t size) |
90 | 90 | { |
91 | int i, j; | |
92 | size_t len; | |
93 | uint8_t buf[256], last[256]; | |
94 | int skipping = 0; | |
95 | ||
96 | flash_init(); | |
97 | for (i = 0; i < size; i += 16) { | |
98 | len = size-i <= 16 ? size-i : 16; | |
99 | flash_block_read(i, buf, len); | |
100 | if (i && !memcmp(last, buf, len)) { | |
101 | printf("%04x: *%c", i, skipping ? '\r' : '\n'); | |
102 | fflush(stdout); | |
103 | skipping = 1; | |
104 | continue; | |
91 | int i, j; | |
92 | size_t len; | |
93 | uint8_t buf[256], last[256]; | |
94 | int skipping = 0; | |
95 | ||
96 | flash_init(); | |
97 | for (i = 0; i < size; i += 16) { | |
98 | len = size-i <= 16 ? size-i : 16; | |
99 | flash_block_read(i, buf, len); | |
100 | if (i && !memcmp(last, buf, len)) { | |
101 | printf("%04x: *%c", i, skipping ? '\r' : '\n'); | |
102 | fflush(stdout); | |
103 | skipping = 1; | |
104 | continue; | |
105 | } | |
106 | skipping = 0; | |
107 | memcpy(last, buf, len); | |
108 | printf("%04x:", i); | |
109 | for (j = 0; j != len; j++) | |
110 | printf(" %02x", buf[j]); | |
111 | printf(" "); | |
112 | for (j = 0; j != len; j++) | |
113 | printf("%c", | |
114 | buf[j] >= ' ' && buf[j] <= '~' ? buf[j] : '.'); | |
115 | putchar('\n'); | |
116 | fflush(stdout); | |
105 | 117 | } |
106 | skipping = 0; | |
107 | memcpy(last, buf, len); | |
108 | printf("%04x:", i); | |
109 | for (j = 0; j != len; j++) | |
110 | printf(" %02x", buf[j]); | |
111 | printf(" "); | |
112 | for (j = 0; j != len; j++) | |
113 | printf("%c", buf[j] >= ' ' && buf[j] <= '~' ? buf[j] : '.'); | |
114 | putchar('\n'); | |
115 | fflush(stdout); | |
116 | } | |
117 | 118 | } |
118 | 119 | |
119 | 120 | |
120 | 121 | static void identify(void) |
121 | 122 | { |
122 | int i; | |
123 | ||
124 | c2_addr_write(0); | |
125 | printf("Dev"); | |
126 | for (i = 0; i != 10; i++) | |
127 | printf(" 0x%02x", c2_data_read(1)); | |
128 | c2_addr_write(1); | |
129 | printf("\nRev"); | |
130 | for (i = 0; i != 10; i++) | |
131 | printf(" 0x%02x", c2_data_read(1)); | |
132 | printf("\n"); | |
123 | int i; | |
124 | ||
125 | c2_addr_write(0); | |
126 | printf("Dev"); | |
127 | for (i = 0; i != 10; i++) | |
128 | printf(" 0x%02x", c2_data_read(1)); | |
129 | c2_addr_write(1); | |
130 | printf("\nRev"); | |
131 | for (i = 0; i != 10; i++) | |
132 | printf(" 0x%02x", c2_data_read(1)); | |
133 | printf("\n"); | |
133 | 134 | } |
134 | 135 | |
135 | 136 | |
136 | 137 | static void do_flash(const char *name) |
137 | 138 | { |
138 | FILE *file; | |
139 | uint8_t code[16384]; | |
139 | FILE *file; | |
140 | uint8_t code[16384]; | |
140 | 141 | |
141 | file = fopen(name, "r"); | |
142 | if (!file) { | |
143 | perror(name); | |
144 | exit(1); | |
145 | } | |
146 | file_size = fread(code, 1, sizeof(code), file); | |
147 | (void) fclose(file); | |
148 | flash_device(code, file_size); | |
142 | file = fopen(name, "r"); | |
143 | if (!file) { | |
144 | perror(name); | |
145 | exit(1); | |
146 | } | |
147 | file_size = fread(code, 1, sizeof(code), file); | |
148 | (void) fclose(file); | |
149 | flash_device(code, file_size); | |
149 | 150 | } |
150 | 151 | |
151 | 152 | |
152 | 153 | static void protect(void) |
153 | 154 | { |
154 | uint8_t pages, lock_byte; | |
155 | uint8_t pages, lock_byte; | |
155 | 156 | |
156 | pages = (file_size+511) >> 9; | |
157 | printf("Protecting %d page%s\n", pages, pages == 1 ? "" : "s"); | |
158 | lock_byte = ~pages; | |
159 | flash_block_write(LOCK_BYTE, &lock_byte, 1); | |
157 | pages = (file_size+511) >> 9; | |
158 | printf("Protecting %d page%s\n", pages, pages == 1 ? "" : "s"); | |
159 | lock_byte = ~pages; | |
160 | flash_block_write(LOCK_BYTE, &lock_byte, 1); | |
160 | 161 | } |
161 | 162 | |
162 | 163 | |
163 | 164 | static void usage(const char *name) |
164 | 165 | { |
165 | fprintf(stderr, | |
166 | fprintf(stderr, | |
166 | 167 | "usage: %s [-p] file\n" |
167 | 168 | " %s -d\n" |
168 | 169 | " %s -e\n" |
... | ... | |
179 | 180 | " -p Protect the data after writing\n" |
180 | 181 | "Invocation without argument resets the F32x.\n" |
181 | 182 | , name, name, name, name, name); |
182 | exit(1); | |
183 | exit(1); | |
183 | 184 | } |
184 | 185 | |
185 | 186 | |
186 | 187 | int main(int argc, char **argv) |
187 | 188 | { |
188 | c2_init(); | |
189 | identify(); | |
190 | ||
191 | switch (argc) { | |
192 | case 1: | |
193 | /* just reset */ | |
194 | break; | |
195 | case 2: | |
196 | if (!strcmp(argv[1], "-d")) | |
197 | dump_flash(0x4000); | |
198 | else if (!strcmp(argv[1], "-e")) | |
199 | erase_flash(); | |
200 | else if (*argv[1] == '-') | |
201 | usage(*argv); | |
202 | else { | |
203 | do_flash(argv[1]); | |
204 | identify(); | |
205 | } | |
206 | break; | |
207 | case 3: | |
208 | if (!strcmp(argv[1], "-p")) { | |
209 | if (*argv[2] == '-') | |
189 | c2_init(); | |
190 | identify(); | |
191 | ||
192 | switch (argc) { | |
193 | case 1: | |
194 | /* just reset */ | |
195 | break; | |
196 | case 2: | |
197 | if (!strcmp(argv[1], "-d")) | |
198 | dump_flash(0x4000); | |
199 | else if (!strcmp(argv[1], "-e")) | |
200 | erase_flash(); | |
201 | else if (*argv[1] == '-') | |
202 | usage(*argv); | |
203 | else { | |
204 | do_flash(argv[1]); | |
205 | identify(); | |
206 | } | |
207 | break; | |
208 | case 3: | |
209 | if (!strcmp(argv[1], "-p")) { | |
210 | if (*argv[2] == '-') | |
211 | usage(*argv); | |
212 | do_flash(argv[2]); | |
213 | protect(); | |
214 | identify(); | |
215 | break; | |
216 | } | |
217 | if (strcmp(argv[1], "-b")) | |
218 | usage(*argv); | |
219 | boundary(argv[2]); | |
220 | break; | |
221 | default: | |
210 | 222 | usage(*argv); |
211 | do_flash(argv[2]); | |
212 | protect(); | |
213 | identify(); | |
214 | break; | |
215 | 223 | } |
216 | if (strcmp(argv[1], "-b")) | |
217 | usage(*argv); | |
218 | boundary(argv[2]); | |
219 | break; | |
220 | default: | |
221 | usage(*argv); | |
222 | } | |
223 | c2_reset(); | |
224 | ||
225 | return 0; | |
224 | c2_reset(); | |
225 | ||
226 | return 0; | |
226 | 227 | } |
f32x/flash.c | ||
---|---|---|
25 | 25 | |
26 | 26 | static uint8_t _c2_addr_read(void) |
27 | 27 | { |
28 | uint8_t x; | |
28 | uint8_t x; | |
29 | 29 | |
30 | usleep(1000); | |
31 | x = c2_addr_read(); | |
32 | // fprintf(stderr, "got 0x%02x\n", x); | |
33 | return x; | |
30 | usleep(1000); | |
31 | x = c2_addr_read(); | |
32 | // fprintf(stderr, "got 0x%02x\n", x); | |
33 | return x; | |
34 | 34 | } |
35 | 35 | |
36 | 36 | |
37 | 37 | static void wait_busy(void) |
38 | 38 | { |
39 | while (_c2_addr_read() & InBusy); | |
39 | while (_c2_addr_read() & InBusy); | |
40 | 40 | } |
41 | 41 | |
42 | 42 | |
43 | 43 | static void wait_ready(void) |
44 | 44 | { |
45 | while (!(_c2_addr_read() & OutReady)); | |
45 | while (!(_c2_addr_read() & OutReady)); | |
46 | 46 | } |
47 | 47 | |
48 | 48 | |
49 | 49 | static void fpdat_write(uint8_t value) |
50 | 50 | { |
51 | c2_data_write(value, 1); | |
52 | wait_busy(); | |
51 | c2_data_write(value, 1); | |
52 | wait_busy(); | |
53 | 53 | } |
54 | 54 | |
55 | 55 | |
56 | 56 | static uint8_t fpdat_read(void) |
57 | 57 | { |
58 | wait_ready(); | |
59 | return c2_data_read(1); | |
58 | wait_ready(); | |
59 | return c2_data_read(1); | |
60 | 60 | } |
61 | 61 | |
62 | 62 | |
63 | 63 | static void check_status(void) |
64 | 64 | { |
65 | uint8_t status; | |
65 | uint8_t status; | |
66 | 66 | |
67 | status = fpdat_read(); | |
68 | if (status != FLASH_STATUS_OK) { | |
69 | fprintf(stderr, "status 0x%02x\n", status); | |
70 | exit(1); | |
71 | } | |
67 | status = fpdat_read(); | |
68 | if (status != FLASH_STATUS_OK) { | |
69 | fprintf(stderr, "status 0x%02x\n", status); | |
70 | exit(1); | |
71 | } | |
72 | 72 | } |
73 | 73 | |
74 | 74 | |
... | ... | |
77 | 77 | |
78 | 78 | void flash_device_erase(void) |
79 | 79 | { |
80 | c2_addr_write(FPDAT); | |
81 | fpdat_write(FLASH_DEVICE_ERASE); | |
82 | check_status(); | |
83 | fpdat_write(FLASH_ERASE_MAGIC1); | |
84 | fpdat_write(FLASH_ERASE_MAGIC2); | |
85 | fpdat_write(FLASH_ERASE_MAGIC3); | |
86 | check_status(); | |
80 | c2_addr_write(FPDAT); | |
81 | fpdat_write(FLASH_DEVICE_ERASE); | |
82 | check_status(); | |
83 | fpdat_write(FLASH_ERASE_MAGIC1); | |
84 | fpdat_write(FLASH_ERASE_MAGIC2); | |
85 | fpdat_write(FLASH_ERASE_MAGIC3); | |
86 | check_status(); | |
87 | 87 | } |
88 | 88 | |
89 | 89 | |
90 | 90 | void flash_block_write(uint16_t addr, const void *data, size_t size) |
91 | 91 | { |
92 | int i; | |
93 | ||
94 | if (!size) | |
95 | return; | |
96 | c2_addr_write(FPDAT); | |
97 | fpdat_write(FLASH_BLOCK_WRITE); | |
98 | check_status(); | |
99 | fpdat_write(addr >> 8); | |
100 | fpdat_write(addr); | |
101 | fpdat_write(size); | |
102 | check_status(); | |
103 | for (i = 0; i != size; i++) | |
104 | fpdat_write(((uint8_t *) data)[i]); | |
92 | int i; | |
93 | ||
94 | if (!size) | |
95 | return; | |
96 | c2_addr_write(FPDAT); | |
97 | fpdat_write(FLASH_BLOCK_WRITE); | |
98 | check_status(); | |
99 | fpdat_write(addr >> 8); | |
100 | fpdat_write(addr); | |
101 | fpdat_write(size); | |
102 | check_status(); | |
103 | for (i = 0; i != size; i++) | |
104 | fpdat_write(((uint8_t *) data)[i]); | |
105 | 105 | } |
106 | 106 | |
107 | 107 | |
108 | 108 | void flash_block_read(uint16_t addr, void *data, size_t size) |
109 | 109 | { |
110 | int i; | |
111 | ||
112 | if (!size) | |
113 | return; | |
114 | c2_addr_write(FPDAT); | |
115 | fpdat_write(FLASH_BLOCK_READ); | |
116 | check_status(); | |
117 | fpdat_write(addr >> 8); | |
118 | fpdat_write(addr); | |
119 | fpdat_write(size); | |
120 | check_status(); | |
121 | for (i = 0; i != size; i++) | |
122 | ((uint8_t *) data)[i] = fpdat_read(); | |
110 | int i; | |
111 | ||
112 | if (!size) | |
113 | return; | |
114 | c2_addr_write(FPDAT); | |
115 | fpdat_write(FLASH_BLOCK_READ); | |
116 | check_status(); | |
117 | fpdat_write(addr >> 8); | |
118 | fpdat_write(addr); | |
119 | fpdat_write(size); | |
120 | check_status(); | |
121 | for (i = 0; i != size; i++) | |
122 | ((uint8_t *) data)[i] = fpdat_read(); | |
123 | 123 | } |
124 | 124 | |
125 | 125 | |
126 | 126 | void flash_init(void) |
127 | 127 | { |
128 | c2_addr_write(FPCTL); | |
129 | c2_data_write(FLASH_INIT_MAGIC1, 1); | |
130 | c2_data_write(FLASH_INIT_MAGIC2, 1); | |
131 | usleep(20000); | |
128 | c2_addr_write(FPCTL); | |
129 | c2_data_write(FLASH_INIT_MAGIC1, 1); | |
130 | c2_data_write(FLASH_INIT_MAGIC2, 1); | |
131 | usleep(20000); | |
132 | 132 | } |
133 | 133 | |
134 | 134 | |
... | ... | |
136 | 136 | |
137 | 137 | uint8_t fp_reg_read(uint8_t addr) |
138 | 138 | { |
139 | c2_addr_write(FPDAT); | |
140 | fpdat_write(REG_READ); | |
141 | check_status(); | |
142 | fpdat_write(addr); | |
143 | fpdat_write(1); | |
144 | return fpdat_read(); | |
139 | c2_addr_write(FPDAT); | |
140 | fpdat_write(REG_READ); | |
141 | check_status(); | |
142 | fpdat_write(addr); | |
143 | fpdat_write(1); | |
144 | return fpdat_read(); | |
145 | 145 | } |
146 | 146 | |
147 | 147 | |
148 | 148 | void fp_reg_write(uint8_t addr, uint8_t value) |
149 | 149 | { |
150 | c2_addr_write(FPDAT); | |
151 | fpdat_write(REG_WRITE); | |
152 | check_status(); | |
153 | fpdat_write(addr); | |
154 | fpdat_write(1); | |
155 | fpdat_write(value); | |
150 | c2_addr_write(FPDAT); | |
151 | fpdat_write(REG_WRITE); | |
152 | check_status(); | |
153 | fpdat_write(addr); | |
154 | fpdat_write(1); | |
155 | fpdat_write(value); | |
156 | 156 | } |
157 |
f32x/gpio.h | ||
---|---|---|
31 | 31 | |
32 | 32 | static inline void gpio_high(unsigned port, unsigned bit) |
33 | 33 | { |
34 | port_dat(port) |= 1 << bit; | |
34 | port_dat(port) |= 1 << bit; | |
35 | 35 | } |
36 | 36 | |
37 | 37 | |
38 | 38 | static inline void gpio_low(unsigned port, unsigned bit) |
39 | 39 | { |
40 | port_dat(port) &= ~(1 << bit); | |
40 | port_dat(port) &= ~(1 << bit); | |
41 | 41 | } |
42 | 42 | |
43 | 43 | |
44 | 44 | static inline void gpio_set(unsigned port, unsigned bit, int value) |
45 | 45 | { |
46 | if (value) | |
47 | gpio_high(port, bit); | |
48 | else | |
49 | gpio_low(port, bit); | |
46 | if (value) | |
47 | gpio_high(port, bit); | |
48 | else | |
49 | gpio_low(port, bit); | |
50 | 50 | } |
51 | 51 | |
52 | 52 | |
53 | 53 | static inline int gpio_get(unsigned port, unsigned bit) |
54 | 54 | { |
55 | return (port_dat(port) >> bit) & 1; | |
55 | return (port_dat(port) >> bit) & 1; | |
56 | 56 | } |
57 | 57 | |
58 | 58 | |
59 | 59 | static inline void gpio_output(unsigned port, unsigned bit) |
60 | 60 | { |
61 | port_con(port) = (port_con(port) & ~(3 << bit*2)) | (1 << bit*2); | |
61 | port_con(port) = (port_con(port) & ~(3 << bit*2)) | (1 << bit*2); | |
62 | 62 | } |
63 | 63 | |
64 | 64 | |
65 | 65 | static inline void gpio_input(unsigned port, unsigned bit) |
66 | 66 | { |
67 | port_con(port) &= ~(3 << bit*2); | |
67 | port_con(port) &= ~(3 << bit*2); | |
68 | 68 | } |
69 | 69 | |
70 | 70 |
f32x/rt.c | ||
---|---|---|
22 | 22 | |
23 | 23 | static void realtimize(void) |
24 | 24 | { |
25 | struct sched_param prm; | |
26 | ||
27 | prm.sched_priority = sched_get_priority_max(SCHED_FIFO); | |
28 | if (prm.sched_priority < 0) { | |
29 | perror("sched_get_priority_max SCHED_FIFO"); | |
30 | exit(1); | |
31 | } | |
32 | if (sched_setscheduler(0, SCHED_FIFO, &prm) < 0) { | |
33 | perror("sched_setscheduler SCHED_FIFO"); | |
34 | exit(1); | |
35 | } | |
25 | struct sched_param prm; | |
26 | ||
27 | prm.sched_priority = sched_get_priority_max(SCHED_FIFO); | |
28 | if (prm.sched_priority < 0) { | |
29 | perror("sched_get_priority_max SCHED_FIFO"); | |
30 | exit(1); | |
31 | } | |
32 | if (sched_setscheduler(0, SCHED_FIFO, &prm) < 0) { | |
33 | perror("sched_setscheduler SCHED_FIFO"); | |
34 | exit(1); | |
35 | } | |
36 | 36 | } |
37 | 37 | |
38 | 38 | |
39 | 39 | static void unrealtime(void) |
40 | 40 | { |
41 | struct sched_param prm = { .sched_priority = 0 }; | |
41 | struct sched_param prm = { .sched_priority = 0 }; | |
42 | 42 | |
43 | if (sched_setscheduler(0, SCHED_OTHER, &prm) < 0) { | |
44 | perror("sched_setscheduler SCHED_OTHER"); | |
45 | exit(1); | |
46 | } | |
43 | if (sched_setscheduler(0, SCHED_OTHER, &prm) < 0) { | |
44 | perror("sched_setscheduler SCHED_OTHER"); | |
45 | exit(1); | |
46 | } | |
47 | 47 | } |
48 | 48 | |
49 | 49 | |
50 | 50 | void rt(int on) |
51 | 51 | { |
52 | if (on) | |
53 | realtimize(); | |
54 | else | |
55 | unrealtime(); | |
52 | if (on) | |
53 | realtimize(); | |
54 | else | |
55 | unrealtime(); | |
56 | 56 | } |
Branches:
master