2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
19 #include <ao_eeprom.h>
21 /* Total bytes of available storage */
22 const ao_pos_t ao_eeprom_total = 4096;
24 /* Location of eeprom in address space */
25 #define stm_eeprom ((uint8_t *) 0x08080000)
28 * The internal flash chip is arranged in 8 byte sectors; the
29 * chip cannot erase in units smaller than that.
31 * Writing happens in units of 2 bytes and
32 * can only change bits from 1 to 0. So, you can rewrite
33 * the same contents, or append to an existing page easily enough
37 ao_intflash_unlock(void)
39 /* Disable backup write protection */
40 stm_pwr.cr |= (1 << STM_PWR_CR_DBP);
42 /* Unlock Data EEPROM and FLASH_PECR register */
43 stm_flash.pekeyr = STM_FLASH_PEKEYR_PEKEY1;
44 stm_flash.pekeyr = STM_FLASH_PEKEYR_PEKEY2;
46 if (stm_flash.pecr & (1 << STM_FLASH_PECR_PELOCK))
47 printf ("eeprom unlock failed\n");
49 /* Configure the FTDW bit (FLASH_PECR[8]) to execute
50 * word write, whatever the previous value of the word
53 stm_flash.pecr = ((0 << STM_FLASH_PECR_OBL_LAUNCH) |
54 (0 << STM_FLASH_PECR_ERRIE) |
55 (0 << STM_FLASH_PECR_EOPIE) |
56 (0 << STM_FLASH_PECR_FPRG) |
57 (0 << STM_FLASH_PECR_ERASE) |
58 (1 << STM_FLASH_PECR_FTDW) |
59 (0 << STM_FLASH_PECR_DATA) |
60 (0 << STM_FLASH_PECR_PROG) |
61 (0 << STM_FLASH_PECR_OPTLOCK) |
62 (0 << STM_FLASH_PECR_PRGLOCK) |
63 (0 << STM_FLASH_PECR_PELOCK));
67 ao_intflash_lock(void)
69 stm_flash.pecr |= (1 << STM_FLASH_PECR_PELOCK);
73 ao_intflash_wait(void)
75 /* Wait for the flash unit to go idle */
76 while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
81 ao_intflash_write32(uint16_t pos, uint32_t w)
83 volatile uint32_t *addr;
85 addr = (uint32_t *) (stm_eeprom + pos);
87 /* Write a word to a valid address in the data EEPROM */
93 ao_intflash_write8(uint16_t pos, uint8_t d)
95 uint32_t w, *addr, mask;
98 addr = (uint32_t *) (stm_eeprom + (pos & ~3));
100 /* Compute word to be written */
101 shift = (pos & 3) << 3;
102 mask = 0xff << shift;
103 w = (*addr & ~mask) | (d << shift);
105 ao_intflash_write32(pos & ~3, w);
109 ao_intflash_read(uint16_t pos)
111 return stm_eeprom[pos];
119 ao_eeprom_write(ao_pos_t pos32, __xdata void *v, uint16_t len)
121 uint16_t pos = pos32;
122 __xdata uint8_t *d = v;
124 if (pos >= ao_eeprom_total || pos + len > ao_eeprom_total)
127 ao_intflash_unlock();
129 if ((pos & 3) == 0 && len >= 4) {
132 w = d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24);
133 ao_intflash_write32(pos, w);
138 ao_intflash_write8(pos, *d);
153 ao_eeprom_read(ao_pos_t pos, __xdata void *v, uint16_t len)
157 if (pos >= ao_eeprom_total || pos + len > ao_eeprom_total)
160 *d++ = ao_intflash_read(pos++);
171 /* Nothing to do here */