2 * fw/flash.c - Board-specific flash functions
4 * Written 2011, 2013-2015 by Werner Almesberger
5 * Copyright 2011, 2013-2015 Werner Almesberger
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.
18 #include <avr/pgmspace.h>
24 static uint32_t payload;
27 static void flash_start(void)
33 static bool flash_can_write(uint16_t size)
35 return payload+size <= BOOT_ADDR;
39 static void flash_write(const uint8_t *buf, uint16_t size)
44 for (p = buf; p != buf+size; p++) {
45 if (!(payload & (SPM_PAGESIZE-1))) {
46 boot_page_erase(payload);
51 boot_page_fill(payload, last | (*p << 8));
56 if (!(payload & (SPM_PAGESIZE-1))) {
57 boot_page_write(payload-SPM_PAGESIZE);
64 static void flash_end_write(void)
66 if (payload & (SPM_PAGESIZE-1)) {
67 boot_page_write(payload & ~(SPM_PAGESIZE-1));
74 static uint16_t flash_read(uint8_t *buf, uint16_t size)
78 while (size && payload != (uint32_t) FLASHEND+1) {
79 *buf++ = pgm_read_byte(payload);
88 static const struct dfu_flash_ops flash_ops = {
90 .can_write = flash_can_write,
92 .end_write = flash_end_write,
97 const struct dfu_flash_ops *dfu_flash_ops = &flash_ops;