74d1659ee066accd8f347946e4e05cc78a57b34c
[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         /*Enable LEDs*/
59         DDR14 = 0xFF;
60         PDR14 = 0xff;
61
62         /*Initialize UART4*/
63         InitUart4();
64
65         while(running) {
66                 cleardata();
67                 increaseled();
68                 switch(recvbyte()) {
69                         case 0x15: //chip erase
70                                 Putch4(0x45);
71                                 increaseled();
72                                 if(FLASH_ChipErase() != 1) {
73                                         panic();
74                                 }
75                                 Putch4(0x23);
76                                 break;
77
78                         case 0x13: //receive
79                                 Putch4(0x37);
80                                 increaseled();
81
82                                 address = recvdword();
83                                 increaseled();
84
85                                 size = recvword();
86                                 increaseled();
87
88                                 PDR14 = 0xff;
89                                 for(i=0; i<size; i++) { /* get data */
90                                         increaseled();
91                                         data[i] = recvbyte();
92                                 }
93
94                                 PDR14 = 0xff;
95                                 for(i=0; i<size; i+=2) { /* flash the data */
96                                         increaseled();
97                                         next = (((unsigned short)data[i])<<8) | (unsigned short)data[i+1];
98                                         if(FLASH_WriteHalfWord(address + i, next) != 1) {
99                                                 panic();
100                                         }
101                                 }
102                                 Putch4(0x28); //Flashing done.
103                                 break;
104
105                         case 0x97: /* exit and restart (let do this by the watchdog!) */
106                                 while(1) {
107                                         increaseled();
108                                 }
109
110                         case 0x99: /* exit */
111                                 running = 0;
112                                 break;
113                         default: break;
114                 }
115         }
116
117         PDR14 = 0x55; //signal that we finished now!
118         halt();
119
120 #if 0
121         i = 0;
122         baseaddr = 0xf4000;
123         for (; i <0x10; i+=4) {
124                 (void) FLASH_SectorErase(baseaddr + i);
125                 increaseled();
126         }
127
128         i = 0;
129         baseaddr = 0xf4000;
130         for(; i<9; i++) {
131                 increaseled();
132                 (void) FLASH_WriteHalfWord(baseaddr + (2*i), toflash[i]);
133         }
134
135 #endif
136 }
137