Root/atusb/fw/flash.c

Source at commit 2978afe92b395305a002c6615676e9cb992989eb created 8 years 8 months ago.
By Werner Almesberger, atusb/fw/flash.c: simplified logic of flash_write and corrected several small bugs
1/*
2 * fw/flash.c - Board-specific flash functions
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 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
16#include <avr/boot.h>
17#include <avr/pgmspace.h>
18
19#include "dfu.h"
20#include "board.h"
21
22
23static uint32_t payload;
24
25
26void flash_start(void)
27{
28    payload = 0;
29}
30
31
32int flash_can_write(uint16_t size)
33{
34    return payload+size <= BOOT_ADDR;
35}
36
37
38void flash_write(const uint8_t *buf, uint16_t size)
39{
40    static uint8_t last;
41        const uint8_t *p;
42
43    for (p = buf; p != buf+size; p++) {
44        if (!(payload & (SPM_PAGESIZE-1))) {
45            boot_page_erase(payload);
46            boot_spm_busy_wait();
47        }
48
49        if (payload & 1)
50            boot_page_fill(payload, last | (*p << 8));
51        else
52            last = *p;
53        payload++;
54
55        if (!(payload & (SPM_PAGESIZE-1))) {
56            boot_page_write(payload-SPM_PAGESIZE);
57            boot_spm_busy_wait();
58        }
59        }
60
61    boot_rww_enable();
62}
63
64
65uint16_t flash_read(uint8_t *buf, uint16_t size)
66{
67    uint16_t got = 0;
68
69    while (size && payload != (uint32_t) FLASHEND+1) {
70        *buf++ = pgm_read_byte(payload);
71        payload++;
72        size--;
73        got++;
74    }
75    return got;
76}
77

Archive Download this file



interactive