Root/atusb/fw/flash.c

Source at commit 553c981551fdf18361e076a26e6fe7427fd2f217 created 9 years 27 days ago.
By Werner Almesberger, atusb/fw/flash.c: implemented flash_can_write and flash_read (were stubs)
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    eeprom_busy_wait();
44    for (p = buf; p != buf+size; p++) {
45        if (!(payload & (SPM_PAGESIZE-1))) {
46            if (payload) {
47                boot_page_write(payload-SPM_PAGESIZE);
48                boot_spm_busy_wait();
49            }
50            boot_page_erase(payload);
51            boot_spm_busy_wait();
52        }
53        if (payload & 1)
54            boot_page_fill(payload, last | (*p << 0));
55        else
56            last = *p;
57        payload++;
58        }
59
60    if (!(payload & (SPM_PAGESIZE-1))) {
61        boot_page_write(payload);
62        boot_spm_busy_wait();
63    }
64
65    boot_rww_enable();
66}
67
68
69uint16_t flash_read(uint8_t *buf, uint16_t size)
70{
71    uint16_t got = 0;
72
73    while (size && payload != (uint32_t) FLASHEND+1) {
74        *buf++ = pgm_read_byte(payload);
75        payload++;
76        size--;
77        got++;
78    }
79    return got;
80}
81

Archive Download this file



interactive