C8051F32x firmware infrastructure

Sign in or create your account | Project List | Help

C8051F32x firmware infrastructure Commit Details

Date:2010-11-07 05:52:19 (12 years 10 months ago)
Author:Werner Almesberger
Commit:88fa3ad057bf23094182d01e2947b8078e81e7db
Message:Improved chip identification, added C8051F34x support, some cleanup.

- f32x/f32x.c (identify_chip, identify, main): identify the chip and print
its name
- f32x/f32x.c (identify): exit if communication is unstable already during
chip identification
- f32x/f32x.c (chips): added the C8051F34x
- f32x/f32x.c (determine_layout, main): determine memory layout based on
flash size
- f32x/f32x.c (usage, main): new option -k flash_kb to set/override the
flash size
- f32x/f32x.c (LOCK_BYTE, protect, need_layout, main): use memory layout
determined at run-time instead of hard-coded layout
- f32x/f32x.c (main): throw an error of -p is used without flashing
- f32x/f32x.c: added section titles and rearranged the order of some
functions
Files: f32x/f32x.c (8 diffs)

Change Details

f32x/f32x.c
2323#include "boundary.h"
2424
2525
26#define LOCK_BYTE 0x3dff
26struct chip {
27    const char *name; /* NULL for end of list */
28    uint8_t dev_id;
29    int max_flash_kb; /* maximum flash size, in kB */
30
31};
32
33struct mem_layout {
34    int flash_kb; /* real flash size, in kB. 0 for end of list */
35    uint16_t lock_byte; /* location of lock byte */
36};
37
38static struct chip chips[] = {
39    { "C8051F320/1", 0x09, 16 },
40    { "C8051F326/7", 0x0d, 16 },
41    { "C8051F34x", 0x0f, 64 },
42    { NULL, }
43};
44
45static struct mem_layout mem_layouts[] = {
46    { 16, 0x3dff },
47    { 32, 0x7fff },
48    { 64, 0xfbff },
49    { 0, }
50};
51
52static const struct chip *chip = NULL;
53static const struct mem_layout *layout = NULL;
2754
2855
2956static size_t file_size;
3057
3158
59/* ----- flash ------------------------------------------------------------- */
60
61
3262static void dump(const char *title, void *data, size_t size)
3363{
3464    int i, j;
...... 
80110}
81111
82112
113static void do_flash(const char *name)
114{
115    FILE *file;
116    uint8_t code[16384];
117
118    file = fopen(name, "r");
119    if (!file) {
120        perror(name);
121        exit(1);
122    }
123    file_size = fread(code, 1, sizeof(code), file);
124    (void) fclose(file);
125    flash_device(code, file_size);
126}
127
128
129/* ----- protect ----------------------------------------------------------- */
130
131
132static void protect(void)
133{
134    uint8_t pages, lock_byte;
135
136    pages = (file_size+511) >> 9;
137    printf("Protecting %d page%s\n", pages, pages == 1 ? "" : "s");
138    lock_byte = ~pages;
139    flash_block_write(layout->lock_byte, &lock_byte, 1);
140}
141
142
143/* ----- erase ------------------------------------------------------------- */
144
145
83146static void erase_flash(void)
84147{
85148    flash_init();
...... 
87150}
88151
89152
153/* ----- dump -------------------------------------------------------------- */
154
155
90156static void dump_flash(size_t size)
91157{
92158    int i, j;
...... 
119185}
120186
121187
188/* ----- identify ---------------------------------------------------------- */
189
190
191static void identify_chip(uint8_t dev)
192{
193    const struct chip *p;
194
195    for (p = chips; p->name; p++)
196        if (p->dev_id == dev) {
197            chip = p;
198            return;
199        }
200}
201
202
122203static void identify(void)
123204{
124    int i;
205    int i, same = 1;
206    uint8_t dev, same_dev;
125207
126208    c2_addr_write(0);
127    printf("Dev");
128    for (i = 0; i != 10; i++)
129        printf(" 0x%02x", c2_data_read(1));
209    fprintf(stderr, "Dev");
210    for (i = 0; i != 10; i++) {
211        dev = c2_data_read(1);
212        fprintf(stderr, " 0x%02x", dev);
213        if (!i)
214            same_dev = dev;
215        else {
216            if (same_dev != dev)
217                same = 0;
218        }
219    }
220    if (!same) {
221        fprintf(stderr, "\ncommunication error\n");
222        exit(1);
223    }
224    identify_chip(same_dev);
225
130226    c2_addr_write(1);
131    printf("\nRev");
227    fprintf(stderr, "\nRev");
132228    for (i = 0; i != 10; i++)
133        printf(" 0x%02x", c2_data_read(1));
134    printf("\n");
229        fprintf(stderr, " 0x%02x", c2_data_read(1));
230    fprintf(stderr, "\n");
135231}
136232
137233
138static void do_flash(const char *name)
234static void determine_layout(int kb)
139235{
140    FILE *file;
141    uint8_t code[16384];
236    if (kb) {
237        if (chip && kb > chip->max_flash_kb) {
238            fprintf(stderr,
239                "chip has only %d kB of flash\n",
240                chip->max_flash_kb);
241            exit(1);
242        }
243    } else {
244        if (chip)
245            kb = chip->max_flash_kb;
246    }
142247
143    file = fopen(name, "r");
144    if (!file) {
145        perror(name);
248    if (!kb)
249        return;
250    for (layout = mem_layouts; layout->flash_kb; layout++)
251        if (layout->flash_kb == kb)
252            break;
253    if (!layout->flash_kb) {
254        fprintf(stderr, "no memory layout found for %d kB\n", kb);
146255        exit(1);
147256    }
148    file_size = fread(code, 1, sizeof(code), file);
149    (void) fclose(file);
150    flash_device(code, file_size);
151257}
152258
153259
154static void protect(void)
155{
156    uint8_t pages, lock_byte;
157
158    pages = (file_size+511) >> 9;
159    printf("Protecting %d page%s\n", pages, pages == 1 ? "" : "s");
160    lock_byte = ~pages;
161    flash_block_write(LOCK_BYTE, &lock_byte, 1);
162}
260/* ----- main -------------------------------------------------------------- */
163261
164262
165263static void usage(const char *name)
166264{
167265    fprintf(stderr,
168"usage: %s [-n] [-p] file\n"
169" %s [-n] -d\n"
266"usage: %s [-n] [-p] [-k flash_kb] file\n"
267" %s [-n] [-k flash_kb] -d\n"
170268" %s [-n] -e\n"
171269" %s [-n] -b pin_setup\n"
172270" %s [-n]\n\n"
...... 
176274" Pins can be set to 0, 1, or R (pull-up). Dots can be used to structure\n"
177275" the bit string. Prints what the pins read back (0 or 1) in the same\n"
178276" order, with a dot between P0 and P2.\n"
179" -d dump Flash content\n"
180" -e erase whole Flash\n"
181" -n do not provide power to the target (default: do provide power)\n"
182" -p protect the data after writing\n"
277" -d dump Flash content\n"
278" -e erase whole Flash\n"
279" -k flash_kb set flash size (16/32/64; default: auto-select)\n"
280" -n do not provide power to the target (default: do provide power)\n"
281" -p protect the data after writing\n"
183282"Invocation without argument resets the F32x.\n"
184283  , name, name, name, name, name);
185284    exit(1);
186285}
187286
188287
288static void need_layout(void)
289{
290    if (layout)
291        return;
292    fprintf(stderr, "please specify flash size with -k flash_kB\n");
293    exit(1);
294}
295
296
189297int main(int argc, char **argv)
190298{
191299    enum {
...... 
195303        mode_erase,
196304        mode_scan,
197305    } mode = mode_default;
198    int do_protect = 0, power = 1;
306    int do_protect = 0, power = 1, kb = 0;
199307    int c;
200308
201    while ((c = getopt(argc, argv, "bdenp")) != EOF)
309    while ((c = getopt(argc, argv, "bdek:np")) != EOF)
202310        switch (c) {
203311        case 'd':
204312            if (mode)
...... 
215323                usage(*argv);
216324            mode = mode_scan;;
217325            break;
326        case 'k':
327            kb = atoi(optarg);
328            break;
218329        case 'n':
219330            power = 0;
220331            break;
...... 
246357            usage(*argv);
247358        break;
248359    }
360    if (do_protect && mode != mode_flash)
361        usage(*argv);
249362
250363    c2_init(power);
251364    identify();
365    determine_layout(kb);
366
367    if (chip)
368        fprintf(stderr, "%s, ", chip->name);
369    else
370        fprintf(stderr, "unknown chip, ");
371    if (layout)
372        fprintf(stderr, "flash size: %d kB\n", layout->flash_kb);
373    else
374        fprintf(stderr, "flash size unknown\n");
252375
253376    switch (mode) {
254377    case mode_default:
255378        /* just reset */
256379        break;
257380    case mode_dump:
258        dump_flash(0x4000);
381        need_layout();
382        dump_flash(layout->lock_byte+2);
259383        break;
260384    case mode_erase:
261385        erase_flash();
262386        break;
263387    case mode_flash:
388        if (do_protect)
389            need_layout();
264390        do_flash(argv[optind]);
265391        if (do_protect)
266392            protect();

Archive Download the corresponding diff file

Branches:
master



interactive