2 * Copyright © 2011 Anthony Towns <aj@erisian.com.au>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 #define ENDOFCODE (CODESIZE)
22 #define AO_INTFLASH_BLOCK 1024
23 #define AO_INTFLASH_BLOCKS ((0x8000 - ENDOFCODE)/AO_INTFLASH_BLOCK)
24 #define AO_INTFLASH_SIZE (AO_INTFLASH_BLOCK * AO_INTFLASH_BLOCKS)
25 #define AO_INTFLASH_LOCATION (0x8000 - AO_INTFLASH_SIZE)
37 #define FLASH_TIMING 0x20
39 #if AO_INTFLASH_BLOCKS < 2
40 #error "Too few pages"
43 #if AO_INFTLASH_LOCATION % 1024 != 0
44 #error "Pages aren't aligned properly"
47 __xdata __at(AO_INTFLASH_LOCATION) uint8_t ao_intflash[AO_INTFLASH_SIZE];
49 /* Total bytes of available storage */
50 __pdata uint32_t ao_storage_total = sizeof(ao_intflash);
52 /* Block size - device is erased in these units. */
53 __pdata uint32_t ao_storage_block = AO_INTFLASH_BLOCK;
55 /* Byte offset of config block. Will be ao_storage_block bytes long */
56 __pdata uint32_t ao_storage_config = sizeof(ao_intflash) - AO_INTFLASH_BLOCK;
58 /* Storage unit size - device reads and writes must be within blocks of this size. */
59 __pdata uint16_t ao_storage_unit = AO_INTFLASH_BLOCK;
61 __xdata static uint8_t ao_intflash_dma_done;
62 static uint8_t ao_intflash_dma;
65 * The internal flash chip is arranged in 1kB sectors; the
66 * chip cannot erase in units smaller than that.
68 * Writing happens in units of 2 bytes and
69 * can only change bits from 1 to 0. So, you can rewrite
70 * the same contents, or append to an existing page easily enough
74 * Erase the specified sector
77 ao_storage_erase(uint32_t pos) __reentrant
81 if (pos >= ao_storage_total || pos + ao_storage_block > ao_storage_total)
84 addr = ((uint16_t)(ao_intflash + pos) >> 1);
92 orl _FCTL, #FCTL_ERASE; ; FCTL |= FCTL_ERASE
93 nop ; Required, see datasheet.
105 ao_intflash_write_aligned(uint16_t pos, __xdata void *d, uint16_t len) __reentrant
107 pos = ((uint16_t) ao_intflash + pos) >> 1;
109 ao_dma_set_transfer(ao_intflash_dma,
113 DMA_CFG0_WORDSIZE_8 |
114 DMA_CFG0_TMODE_SINGLE |
115 DMA_CFG0_TRIGGER_FLASH,
118 DMA_CFG1_PRIORITY_HIGH);
123 ao_dma_start(ao_intflash_dma);
128 orl _FCTL, #FCTL_WRITE; ; FCTL |= FCTL_WRITE
135 ao_intflash_write_byte(uint16_t pos, uint8_t byte) __reentrant
137 static __xdata uint8_t b[2];
146 ao_intflash_write_aligned(pos, b, 2);
150 ao_storage_device_write(uint32_t pos32, __xdata void *v, uint16_t len) __reentrant
152 uint16_t pos = pos32;
153 __xdata uint8_t *d = v;
156 if (pos >= ao_storage_total || pos + len > ao_storage_total)
162 ao_intflash_write_byte(pos++, *d++);
168 ao_intflash_write_aligned(pos, d, len);
170 ao_intflash_write_byte(pos + len, d[len]);
179 ao_storage_device_read(uint32_t pos, __xdata void *d, uint16_t len) __reentrant
181 if (pos >= ao_storage_total || pos + len > ao_storage_total)
183 ao_xmemcpy(d, ao_intflash+pos, len);
188 ao_storage_flush(void) __reentrant
193 ao_storage_setup(void)
198 ao_storage_device_info(void) __reentrant
200 printf ("Using internal flash, starting at 0x%04x\n", AO_INTFLASH_LOCATION);
204 ao_storage_device_init(void)
206 ao_intflash_dma = ao_dma_alloc(&ao_intflash_dma_done);