-rw-r--r-- 1 lewurm lewurm 4244 2009-12-14 14:17 pkernel/pkernel.mhx
[pyfrprog.git] / pkernel / flash.c
1 #include "flash.h"
2 #include "mb91465k.h"
3
4 void FLASH_PrepareWriteHalfWordMode()
5 {
6         /*      Set FLASH Access Mode via BootROM Routine       */
7         /*      For details refer to the Hardware Manual or Data Sheet */
8 #pragma asm
9         ST              RP,@-R15
10         STM0    (R4,R5)
11         STM1    (R12)
12         LDI             #0x01,R4        ; Set FLASH to 16Bit read/write Mode
13         LDI             #0x04,R5        ; Go 4 times through delay loop (64MHz CLKB)
14         LDI             #0xBF60,R12
15         CALL    @R12
16         LDM1    (R12)
17         LDM0    (R4,R5)
18         LD              @R15+,RP
19 #pragma endasm
20         
21         /*      Set the FLASH Interface to Write Timing */
22         /*      For details refer to the Hardware Manual or Data Sheet */
23         /*      Setting shown here is for CLKB = 64MHz  */
24         FMWT_ATD = 1;
25         FMWT_WEXH = 0;
26         FMWT_WTC = 8;
27 }
28
29 void FLASH_PrepareReadMode()
30 {
31         /*      Set FLASH Access Mode via BootROM Routine       */
32         /*      For details refer to the Hardware Manual or Data Sheet */
33 #pragma asm
34         ST              RP,@-R15
35         STM0    (R4,R5)
36         STM1    (R12)
37         LDI             #0x00,R4        ; Set FLASH to 32Bit read/write Mode
38         LDI             #0x04,R5        ; Go 4 times through delay loop (64MHz CLKB)
39         LDI             #0xBF60,R12
40         CALL    @R12
41         LDM1    (R12)
42         LDM0    (R4,R5)
43         LD              @R15+,RP
44 #pragma endasm
45         
46         /*      Set the FLASH Interface to Read Timing  */
47         /*      For details refer to the Hardware Manual or Data Sheet */
48         /*      Setting shown here is for CLKB = 64MHz  */
49         FMWT_ATD = 1;
50         FMWT_EQ = 3;
51         FMWT_WTC = 4;
52 }               
53                 
54 unsigned char FLASH_ChipErase(void)
55 {
56         unsigned char flag = 0;
57
58         /*Set FLASH access mode to 16Bit Write Mode*/
59         FLASH_PrepareWriteHalfWordMode();
60
61         /*Start FLASH Sector Erase Sequence*/
62         *hseq_1 = 0x00AA;
63         *hseq_2 = 0x0055;
64         *hseq_1 = 0x0080;
65         *hseq_1 = 0x00AA;
66         *hseq_2 = 0x0055;
67         *hseq_1 = 0x0010;
68
69         /*Wait for the Auto Algorithm to finish*/
70         while( flag == 0 ) {
71                 /* Feed Hardware Watchdog */
72                 HWWD_CL = 0;
73
74                 if(*hseq_1 & DPOLL) {
75                         flag = 1;
76                 }
77                 if(*hseq_1 & TLOVER) {
78                         if(*hseq_1 & DPOLL) {
79                                 flag = 1;
80                         }
81                         else {
82                                 /*Reset FLASH (keep in mind 16Bit access to FLASH)*/
83                                 *hseq_1 = 0x00F0; // Keep in Mind (16Bit access)
84
85                                 flag = 2;
86                         }
87                 }
88         }
89
90         /*Set FLASH access mode to 32Bit Read Mode*/
91         FLASH_PrepareReadMode();
92
93         return flag;
94 }
95         
96 #if 0 //maybe implement this check too!
97 unsigned char FLASH_SectorBlankCheck(unsigned int secaddr, unsigned int size)
98 {
99         unsigned int count;
100         unsigned char empty_flag = 0;
101         unsigned int addr = secaddr;
102                 
103         /*      Clear FIXE bit to see FLASH memory content instead of fixed reset vector        */
104         FMCS_FIXE = 0;
105         
106         for(count = 0; count < size; count ++)
107         {
108                 /*      Clear Hardware Watchdog */
109                 HWWD_CL = 0;
110                 if( *(unsigned int *)addr != 0xFFFFFFFF ) empty_flag = 1;
111                 addr += 4;
112         }
113         
114         /*      Set FIXE bit to see fixed reset vector  */
115         FMCS_FIXE = 1;
116
117         if( empty_flag != 0 )
118         {
119                 return 2;
120         }
121         
122         return 1;
123 }
124 #endif
125
126 unsigned char FLASH_WriteHalfWord(unsigned int adr, unsigned short int data)
127 {
128         unsigned char flag = 0;
129
130         /*      Set FLASH access mode to 16Bit Write Mode       */
131         FLASH_PrepareWriteHalfWordMode();
132         
133         /*      Start Write FLASH Sequence      */
134         *hseq_1 = 0x00AA;
135         *hseq_2 = 0x0055;
136         *hseq_1 = 0x00A0;
137         *((volatile unsigned short int *)adr) = data;
138         
139         /*      Wait for the Auto Algorithm to finish   */
140         while( flag == 0 )
141         {
142                 /* Feed Hardware Watchdog */
143                 HWWD_CL = 0;
144                 
145                 if( ( *(volatile unsigned short int *)adr & DPOLL ) == (data & DPOLL) )
146                 {
147                         flag = 1;
148                 }
149                 if( ( *(volatile unsigned short int *)adr & TLOVER ) == TLOVER )
150                 {
151                         if( ( *(volatile unsigned short int *)adr & DPOLL ) == (data & DPOLL) )
152                         {
153                                 flag = 1;
154                         }
155                         else
156                         {
157                                 /*      Reset FLASH (keep in mind 16Bit access to FLASH)        */
158                                 *hseq_1 = 0x00F0;       // Keep in Mind (16Bit access)
159                                 
160                                 flag = 2;
161                         }
162                 }
163         }
164
165         /*      Set FLASH access mode to 32Bit Read Mode        */
166         FLASH_PrepareReadMode();
167         
168         return flag;
169 }
170
171 unsigned char FLASH_ReadReset()
172 {
173         *hseq_1 = 0x00F0;
174         
175         return 1;
176 }
177
178 unsigned char FLASH_CheckPendingInterrupt()
179 {
180         /*      Poll for Pending Interrupts which are needed here       */
181         if(TMCSR0_UF) return 1; /* in this sample the only interrupt source supervised is Reload Timer 0        */
182         
183         /*      and return 1 when an Interrupt is pending       */
184         return 0;
185 }
186