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