added further exit function, so the board restarts after reset
[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 }
11
12 static unsigned char recvbyte(void)
13 {
14         return Getch4();
15 }
16
17 static unsigned short recvword(void)
18 {
19         static unsigned char b1, b2;
20         static unsigned short ret;
21         b1 = recvbyte();
22         b2 = recvbyte();
23         ret = (unsigned short)(b2 << 8) | b1;
24         return ret;
25 }
26
27 static unsigned int recvdword(void)
28 {
29         static unsigned char b1, b2, b3, b4;
30         static unsigned int ret;
31         b1 = recvbyte();
32         b2 = recvbyte();
33         b3 = recvbyte();
34         b4 = recvbyte();
35         ret = ((unsigned int) (b4 << 24)) | ((unsigned int) (b3 << 16)) | ((unsigned int) (b2 << 8)) |(unsigned int)b1;
36         return ret;
37 }
38
39 static void halt(void)
40 {
41         while(1) {
42                 HWWD_CL = 0;
43         }
44 }
45
46 static void panic(void)
47 {
48         PDR14 = 0x22;
49         halt();
50 }
51
52 void main(void)
53 {
54         unsigned int address;
55         unsigned short i, size, next;
56         unsigned char running = 1, data[BUFSIZE] = {0};
57         
58         PORTEN = 0x3; /* enable I/O Ports */
59
60         /*Enable LEDs*/
61         DDR14 = 0xFF;
62         PDR14 = 0xff;
63
64         /*Initialize UART4*/
65         InitUart4();
66
67         while(running) {
68                 cleardata();
69                 increaseled();
70                 switch(recvbyte()) {
71                         case 0x15: //chip erase
72                                 Putch4(0x45);
73                                 increaseled();
74                                 if(FLASH_ChipErase() != 1) {
75                                         panic();
76                                 }
77                                 Putch4(0x23);
78                                 break;
79
80                         case 0x12: //erase
81                                 Putch4(0x11);
82                                 address = recvdword();
83                                 increaseled();
84
85                                 size = recvword();
86                                 increaseled();
87
88                                 PDR14 = 0xff;
89                                 for(i=0; i<(size+4); i+=4) { /* erase */
90                                         if(FLASH_SectorErase(address + i) != 1) {
91                                                 panic();
92                                         }
93                                         increaseled();
94                                 }
95                                 Putch4(0x18); //Erasing done.
96                                 break;
97
98                         case 0x13: //receive
99                                 Putch4(0x37);
100                                 increaseled();
101
102                                 address = recvdword();
103                                 increaseled();
104
105                                 size = recvword();
106                                 increaseled();
107
108                                 PDR14 = 0xff;
109                                 for(i=0; i<size; i++) { /* get data */
110                                         increaseled();
111                                         data[i] = recvbyte();
112                                 }
113
114                                 PDR14 = 0xff;
115                                 for(i=0; i<size; i+=2) { /* flash the data */
116                                         increaseled();
117                                         next = (((unsigned short)data[i])<<8) | (unsigned short)data[i+1];
118                                         if(FLASH_WriteHalfWord(address + i, next) != 1) {
119                                                 panic();
120                                         }
121                                 }
122                                 Putch4(0x28); //Flashing done.
123                                 break;
124
125                         case 0x97: /* exit and restart (let do this by the watchdog!) */
126                                 while(1) {
127                                         increaseled();
128                                 }
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