]> git.gag.com Git - fw/altos/blob - src/stm/ao_flash_stm.c
libaltos: jni_md.h moved to linux subdir in Java install
[fw/altos] / src / stm / ao_flash_stm.c
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include <ao.h>
19 #include <ao_flash.h>
20
21 static uint8_t
22 ao_flash_pecr_is_locked(void)
23 {
24         return (stm_flash.pecr & (1 << STM_FLASH_PECR_PELOCK)) != 0;
25 }
26
27 static uint8_t
28 ao_flash_pgr_is_locked(void)
29 {
30         return (stm_flash.pecr & (1 << STM_FLASH_PECR_PRGLOCK)) != 0;
31 }
32
33 static void
34 ao_flash_pecr_unlock(void)
35 {
36         if (!ao_flash_pecr_is_locked())
37                 return;
38
39         /* Unlock Data EEPROM and FLASH_PECR register */
40         stm_flash.pekeyr = STM_FLASH_PEKEYR_PEKEY1;
41         stm_flash.pekeyr = STM_FLASH_PEKEYR_PEKEY2;
42         if (ao_flash_pecr_is_locked())
43                 ao_panic(AO_PANIC_FLASH);
44 }
45
46 static void
47 ao_flash_pgr_unlock(void)
48 {
49         if (!ao_flash_pgr_is_locked())
50                 return;
51
52         /* Unlock program memory */
53         stm_flash.prgkeyr = STM_FLASH_PRGKEYR_PRGKEY1;
54         stm_flash.prgkeyr = STM_FLASH_PRGKEYR_PRGKEY2;
55         if (ao_flash_pgr_is_locked())
56                 ao_panic(AO_PANIC_FLASH);
57 }
58
59 static void
60 ao_flash_lock(void)
61 {
62         stm_flash.pecr |= (1 << STM_FLASH_PECR_OPTLOCK) | (1 << STM_FLASH_PECR_PRGLOCK) | (1 << STM_FLASH_PECR_PELOCK);
63 }
64
65 static void
66 ao_flash_wait_bsy(void)
67 {
68         while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
69                 ;
70 }
71
72 static void __attribute__ ((section(".ramtext"),noinline))
73 _ao_flash_erase_page(uint32_t *page)
74 {
75         stm_flash.pecr |= (1 << STM_FLASH_PECR_ERASE) | (1 << STM_FLASH_PECR_PROG);
76         
77         *page = 0x00000000;
78
79         while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
80                 ;
81 }
82
83 void
84 ao_flash_erase_page(uint32_t *page)
85 {
86         ao_arch_block_interrupts();
87         ao_flash_pecr_unlock();
88         ao_flash_pgr_unlock();
89
90         _ao_flash_erase_page(page);
91
92         ao_flash_lock();
93         ao_arch_release_interrupts();
94 }
95
96 static void __attribute__ ((section(".ramtext"), noinline))
97 _ao_flash_half_page(uint32_t *dst, uint32_t *src)
98 {
99         uint8_t         i;
100
101         stm_flash.pecr |= (1 << STM_FLASH_PECR_FPRG);
102         stm_flash.pecr |= (1 << STM_FLASH_PECR_PROG);
103         
104         while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
105                 ;
106
107         for (i = 0; i < 32; i++) {
108                 *dst++ = *src++;
109         }
110
111         while (stm_flash.sr & (1 << STM_FLASH_SR_BSY))
112                 ;
113 }
114
115 void
116 ao_flash_page(uint32_t *page, uint32_t *src)
117 {
118         uint8_t         h;
119
120         ao_flash_erase_page(page);
121
122         ao_arch_block_interrupts();
123         ao_flash_pecr_unlock();
124         ao_flash_pgr_unlock();
125         for (h = 0; h < 2; h++) {
126                 _ao_flash_half_page(page, src);
127                 page += 32;
128                 src += 32;
129         }
130         ao_flash_lock();
131         ao_arch_release_interrupts();
132 }