Root/atusb/fw/flash.c

Source at commit 0db361d49da04eef1f4dd77996450d54e900e083 created 8 years 8 months ago.
By Werner Almesberger, atusb/fw/Makefile (dfu): pass transfer size to hide DFU non-compliance
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
62
63void flash_end_write(void)
64{
65    if (payload & (SPM_PAGESIZE-1)) {
66        boot_page_write(payload & ~(SPM_PAGESIZE-1));
67        boot_spm_busy_wait();
68    }
69    boot_rww_enable();
70}
71
72
73uint16_t flash_read(uint8_t *buf, uint16_t size)
74{
75    uint16_t got = 0;
76
77    while (size && payload != (uint32_t) FLASHEND+1) {
78        *buf++ = pgm_read_byte(payload);
79        payload++;
80        size--;
81        got++;
82    }
83    return got;
84}
85

Archive Download this file



interactive