IEEE 802.15.4 subsystem
Sign in or create your account | Project List | Help
IEEE 802.15.4 subsystem Git Source Tree
Root/
Source at commit 85f60de9d52195afb3a0232a03e850d96584bb9a created 12 years 10 months ago. By Werner Almesberger, atusb/fw: use the unique serial number of the ATmega8/16/32U2 for iSerialNumber | |
---|---|
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 | |
23 | static uint32_t payload; |
24 | |
25 | |
26 | void flash_start(void) |
27 | { |
28 | payload = 0; |
29 | } |
30 | |
31 | |
32 | int flash_can_write(uint16_t size) |
33 | { |
34 | return payload+size <= BOOT_ADDR; |
35 | } |
36 | |
37 | |
38 | void 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 | |
63 | void 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 | |
73 | uint16_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 |