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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 #define ENDOFCODE (CODESIZE)
23 #define AO_INTFLASH_BLOCK 1024
24 #define AO_INTFLASH_BLOCKS ((0x8000 - ENDOFCODE)/AO_INTFLASH_BLOCK)
25 #define AO_INTFLASH_SIZE (AO_INTFLASH_BLOCK * AO_INTFLASH_BLOCKS)
26 #define AO_INTFLASH_LOCATION (0x8000 - AO_INTFLASH_SIZE)
38 #define FLASH_TIMING 0x20
40 #if AO_INTFLASH_BLOCKS < 2
41 #error "Too few pages"
44 #if AO_INFTLASH_LOCATION % 1024 != 0
45 #error "Pages aren't aligned properly"
48 __xdata __at(AO_INTFLASH_LOCATION) uint8_t ao_intflash[AO_INTFLASH_SIZE];
50 /* Total bytes of available storage */
51 __pdata uint32_t ao_storage_total = sizeof(ao_intflash);
53 /* Block size - device is erased in these units. */
54 __pdata uint32_t ao_storage_block = AO_INTFLASH_BLOCK;
56 /* Byte offset of config block. Will be ao_storage_block bytes long */
57 __pdata uint32_t ao_storage_config = sizeof(ao_intflash) - AO_INTFLASH_BLOCK;
59 /* Storage unit size - device reads and writes must be within blocks of this size. */
60 __pdata uint16_t ao_storage_unit = AO_INTFLASH_BLOCK;
62 __xdata static uint8_t ao_intflash_dma_done;
63 static uint8_t ao_intflash_dma;
66 * The internal flash chip is arranged in 1kB sectors; the
67 * chip cannot erase in units smaller than that.
69 * Writing happens in units of 2 bytes and
70 * can only change bits from 1 to 0. So, you can rewrite
71 * the same contents, or append to an existing page easily enough
75 * Erase the specified sector
78 ao_storage_erase(uint32_t pos) __reentrant
82 if (pos >= ao_storage_total || pos + ao_storage_block > ao_storage_total)
85 addr = ((uint16_t)(ao_intflash + pos) >> 1);
93 orl _FCTL, #FCTL_ERASE; ; FCTL |= FCTL_ERASE
94 nop ; Required, see datasheet.
106 ao_intflash_write_aligned(uint16_t pos, __xdata void *d, uint16_t len) __reentrant
108 pos = ((uint16_t) ao_intflash + pos) >> 1;
110 ao_dma_set_transfer(ao_intflash_dma,
114 DMA_CFG0_WORDSIZE_8 |
115 DMA_CFG0_TMODE_SINGLE |
116 DMA_CFG0_TRIGGER_FLASH,
119 DMA_CFG1_PRIORITY_HIGH);
124 ao_dma_start(ao_intflash_dma);
129 orl _FCTL, #FCTL_WRITE; ; FCTL |= FCTL_WRITE
133 __critical while (!ao_intflash_dma_done)
134 ao_sleep(&ao_intflash_dma_done);
138 ao_intflash_write_byte(uint16_t pos, uint8_t byte) __reentrant
140 static __xdata uint8_t b[2];
149 ao_intflash_write_aligned(pos, b, 2);
153 ao_storage_device_write(uint32_t pos32, __xdata void *v, uint16_t len) __reentrant
155 uint16_t pos = pos32;
156 __xdata uint8_t *d = v;
159 if (pos >= ao_storage_total || pos + len > ao_storage_total)
165 ao_intflash_write_byte(pos++, *d++);
171 ao_intflash_write_aligned(pos, d, len);
173 ao_intflash_write_byte(pos + len, d[len]);
182 ao_storage_device_read(uint32_t pos, __xdata void *d, uint16_t len) __reentrant
184 if (pos >= ao_storage_total || pos + len > ao_storage_total)
186 ao_xmemcpy(d, ao_intflash+pos, len);
191 ao_storage_flush(void) __reentrant
196 ao_storage_setup(void)
201 ao_storage_device_info(void) __reentrant
203 printf ("Using internal flash, starting at 0x%04x\n", AO_INTFLASH_LOCATION);
207 ao_storage_device_init(void)
209 ao_intflash_dma = ao_dma_alloc(&ao_intflash_dma_done);