make: check if git is available before calculate version
[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 //maybe implement this check too!
97 #if 0
98 unsigned char FLASH_SectorBlankCheck(unsigned int secaddr, unsigned int size)
99 {
100         unsigned int count;
101         unsigned char empty_flag = 0;
102         unsigned int addr = secaddr;
103                 
104         /*      Clear FIXE bit to see FLASH memory content instead of fixed reset vector        */
105         FMCS_FIXE = 0;
106         
107         for(count = 0; count < size; count ++)
108         {
109                 /*      Clear Hardware Watchdog */
110                 HWWD_CL = 0;
111                 if( *(unsigned int *)addr != 0xFFFFFFFF ) empty_flag = 1;
112                 addr += 4;
113         }
114         
115         /*      Set FIXE bit to see fixed reset vector  */
116         FMCS_FIXE = 1;
117
118         if( empty_flag != 0 )
119         {
120                 return 2;
121         }
122         
123         return 1;
124 }
125 #endif
126
127 unsigned char FLASH_WriteHalfWord(unsigned int adr, unsigned short int data)
128 {
129         unsigned char flag = 0;
130
131         /*      Set FLASH access mode to 16Bit Write Mode       */
132         FLASH_PrepareWriteHalfWordMode();
133         
134         /*      Start Write FLASH Sequence      */
135         *hseq_1 = 0x00AA;
136         *hseq_2 = 0x0055;
137         *hseq_1 = 0x00A0;
138         *((volatile unsigned short int *)adr) = data;
139         
140         /*      Wait for the Auto Algorithm to finish   */
141         while( flag == 0 )
142         {
143                 /* Feed Hardware Watchdog */
144                 HWWD_CL = 0;
145                 
146                 if( ( *(volatile unsigned short int *)adr & DPOLL ) == (data & DPOLL) )
147                 {
148                         flag = 1;
149                 }
150                 if( ( *(volatile unsigned short int *)adr & TLOVER ) == TLOVER )
151                 {
152                         if( ( *(volatile unsigned short int *)adr & DPOLL ) == (data & DPOLL) )
153                         {
154                                 flag = 1;
155                         }
156                         else
157                         {
158                                 /*      Reset FLASH (keep in mind 16Bit access to FLASH)        */
159                                 *hseq_1 = 0x00F0;       // Keep in Mind (16Bit access)
160                                 
161                                 flag = 2;
162                         }
163                 }
164         }
165
166         /*      Set FLASH access mode to 32Bit Read Mode        */
167         FLASH_PrepareReadMode();
168         
169         return flag;
170 }
171
172 unsigned char FLASH_ReadReset()
173 {
174         *hseq_1 = 0x00F0;
175         
176         return 1;
177 }
178
179 unsigned char FLASH_CheckPendingInterrupt()
180 {
181         /*      Poll for Pending Interrupts which are needed here       */
182         if(TMCSR0_UF) return 1; /* in this sample the only interrupt source supervised is Reload Timer 0        */
183         
184         /*      and return 1 when an Interrupt is pending       */
185         return 0;
186 }
187