use chip erase command instead of deleting each cell. however, this
[pyfrprog.git] / pkernel / main.c
1 #include "mb91465k.h"
2 #include "flash.h"
3
4 #pragma section CODE=IRAM,attr=CODE
5
6 #define BUFSIZE 0x20
7 #define cleardata() memset(data,0,BUFSIZE)
8
9 static void increaseled(void)
10 {
11         PDR14 = ~(((~PDR14)+1)%256);
12         HWWD_CL = 0;
13 }
14
15 static unsigned char recvbyte(void)
16 {
17         return Getch4();
18 }
19
20 static unsigned short recvword(void)
21 {
22         static unsigned char b1, b2;
23         static unsigned short ret;
24         b1 = recvbyte();
25         b2 = recvbyte();
26         ret = (unsigned short)(b2 << 8) | b1;
27         return ret;
28 }
29
30 static unsigned int recvdword(void)
31 {
32         static unsigned char b1, b2, b3, b4;
33         static unsigned int ret;
34         b1 = recvbyte();
35         b2 = recvbyte();
36         b3 = recvbyte();
37         b4 = recvbyte();
38         ret = ((unsigned int) (b4 << 24)) | ((unsigned int) (b3 << 16)) | ((unsigned int) (b2 << 8)) |(unsigned int)b1;
39         return ret;
40 }
41
42 static void halt(void)
43 {
44         while(1) {
45                 HWWD_CL = 0;
46         }
47 }
48
49 static void panic(void)
50 {
51         PDR14 = 0x22;
52         halt();
53 }
54
55 void main(void)
56 {
57         unsigned int address;
58         unsigned short i, size, next;
59         unsigned char running = 1, data[BUFSIZE] = {0};
60         
61         PORTEN = 0x3; /* enable I/O Ports */
62
63         /*Enable LEDs*/
64         DDR14 = 0xFF;
65         PDR14 = 0xff;
66
67         /*Initialize UART4*/
68         InitUart4();
69
70         while(running) {
71                 cleardata();
72                 increaseled();
73                 switch(recvbyte()) {
74                         case 0x15: //chip erase
75                                 Putch4(0x45);
76                                 increaseled();
77                                 if(FLASH_ChipErase() != 1) {
78                                         panic();
79                                 }
80                                 Putch4(0x23);
81                                 break;
82
83                         case 0x12: //erase
84                                 Putch4(0x11);
85                                 address = recvdword();
86                                 increaseled();
87
88                                 size = recvword();
89                                 increaseled();
90
91                                 PDR14 = 0xff;
92                                 for(i=0; i<(size+4); i+=4) { /* erase */
93                                         if(FLASH_SectorErase(address + i) != 1) {
94                                                 panic();
95                                         }
96                                         increaseled();
97                                 }
98                                 Putch4(0x18); //Erasing done.
99                                 break;
100
101                         case 0x13: //receive
102                                 Putch4(0x37);
103                                 increaseled();
104
105                                 address = recvdword();
106                                 increaseled();
107
108                                 size = recvword();
109                                 increaseled();
110
111                                 Putch4(0x04); //Received Metadata.
112                                 PDR14 = 0xff;
113                                 for(i=0; i<size; i++) { /* get data */
114                                         increaseled();
115                                         data[i] = recvbyte();
116                                 }
117                                 Putch4(0x08); //Received Data.
118
119                                 PDR14 = 0xff;
120                                 for(i=0; i<size; i+=2) { /* flash the data */
121                                         increaseled();
122                                         next = (((unsigned short)data[i])<<8) | (unsigned short)data[i+1];
123                                         if(FLASH_WriteHalfWord(address + i, next) != 1) {
124                                                 panic();
125                                         }
126                                 }
127                                 Putch4(0x28); //Flashing done.
128                                 break;
129
130                         case 0x99: /* exit */
131                                 running = 0;
132                                 break;
133                         default: break;
134                 }
135         }
136
137         PDR14 = 0x55; //signal that we finished now!
138         halt();
139
140 #if 0
141         i = 0;
142         baseaddr = 0xf4000;
143         for (; i <0x10; i+=4) {
144                 (void) FLASH_SectorErase(baseaddr + i);
145                 increaseled();
146         }
147
148         i = 0;
149         baseaddr = 0xf4000;
150         for(; i<9; i++) {
151                 increaseled();
152                 (void) FLASH_WriteHalfWord(baseaddr + (2*i), toflash[i]);
153         }
154
155 #endif
156 }
157