Root/
| Source at commit 45f14d214ab31fe28644ef8f61552a3e25330ccf created 11 years 17 days ago. By Werner Almesberger, tornado/fw/tornado.c: experimental code to log ADC samples to memory card | |
|---|---|
| 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 | |
| 26 | static uint32_t payload; |
| 27 | |
| 28 | |
| 29 | void flash_start(void) |
| 30 | { |
| 31 | payload = 0; |
| 32 | } |
| 33 | |
| 34 | |
| 35 | int flash_can_write(uint16_t size) |
| 36 | { |
| 37 | return payload <= BOOT_ADDR-size; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | void 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 | |
| 66 | void 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 | |
Branches:
master
tornado-v1
