added receivebyte with timeout. sometimes it will just hang,
[pyfrprog.git] / pkernel / main.c
1 #include "mb91465k.h"
2 #include "flash.h"
3
4 #define BUFSIZE 0x10
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
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                                 PDR14 = 0xff;
110                                 for(i=0; i<size; i++) { /* get data */
111                                         increaseled();
112                                         data[i] = recvbyte();
113                                 }
114
115                                 PDR14 = 0xff;
116                                 for(i=0; i<size; i+=2) { /* flash the data */
117                                         increaseled();
118                                         next = (((unsigned short)data[i])<<8) | (unsigned short)data[i+1];
119                                         if(FLASH_WriteHalfWord(address + i, next) != 1) {
120                                                 panic();
121                                         }
122                                 }
123                                 break;
124
125                         case 0x23: //ack it
126                                 Putch4(0xaa);
127                                 break;
128
129                         case 0x97: /* exit and restart (let do this by the watchdog)! */
130                                 while(1) {
131                                         increaseled();
132                                 }
133
134                         case 0x99: /* exit */
135                                 running = 0;
136                                 break;
137                         default: break;
138                 }
139         }
140
141         PDR14 = 0x55; //signal that we finished now!
142         halt();
143
144 #if 0
145         i = 0;
146         baseaddr = 0xf4000;
147         for (; i <0x10; i+=4) {
148                 (void) FLASH_SectorErase(baseaddr + i);
149                 increaseled();
150         }
151
152         i = 0;
153         baseaddr = 0xf4000;
154         for(; i<9; i++) {
155                 increaseled();
156                 (void) FLASH_WriteHalfWord(baseaddr + (2*i), toflash[i]);
157         }
158
159 #endif
160 }
161