Root/fw/flash.c

Source at commit 121acf6ffad8f1bcd4b08e50e293e597880b68eb created 10 years 11 months ago.
By Werner Almesberger, tornado/cpu/cpu.brd: make connection to the MCU's GND pad straighter
1/*
2 * fw/flash.c - Flash interface
3 *
4 * Written 2011, 2012 by Werner Almesberger
5 * Copyright 2011, 2012 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 * Adapted from ben-wpan/atusb/fw/flash.c
15 */
16
17
18#include <stdint.h>
19
20#include <avr/boot.h>
21//#include <avr/pgmspace.h>
22
23#include "flash.h"
24
25
26static uint32_t payload;
27
28
29void flash_start(void)
30{
31    payload = 0;
32}
33
34
35int flash_can_write(uint16_t size)
36{
37    return payload <= BOOT_ADDR-size;
38}
39
40
41void flash_write(const uint8_t *buf, uint16_t size)
42{
43    static uint8_t last;
44        const uint8_t *p;
45
46    for (p = buf; p != buf+size; p++) {
47        if (!(payload & (SPM_PAGESIZE-1))) {
48            boot_page_erase(payload);
49            boot_spm_busy_wait();
50        }
51
52        if (payload & 1)
53            boot_page_fill(payload, last | (*p << 8));
54        else
55            last = *p;
56        payload++;
57
58        if (!(payload & (SPM_PAGESIZE-1))) {
59            boot_page_write(payload-SPM_PAGESIZE);
60            boot_spm_busy_wait();
61        }
62        }
63}
64
65
66void flash_end_write(void)
67{
68    if (payload & (SPM_PAGESIZE-1)) {
69        boot_page_write(payload & ~(SPM_PAGESIZE-1));
70        boot_spm_busy_wait();
71    }
72    boot_rww_enable();
73}
74

Archive Download this file

Branches:
master
tornado-v1



interactive