faster baudrate for pkernel
[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 0x12: //erase
79                                 Putch4(0x11);
80                                 address = recvdword();
81                                 increaseled();
82
83                                 size = recvword();
84                                 increaseled();
85
86                                 PDR14 = 0xff;
87                                 for(i=0; i<(size+4); i+=4) { /* erase */
88                                         if(FLASH_SectorErase(address + i) != 1) {
89                                                 panic();
90                                         }
91                                         increaseled();
92                                 }
93                                 Putch4(0x18); //Erasing done.
94                                 break;
95
96                         case 0x13: //receive
97                                 Putch4(0x37);
98                                 increaseled();
99
100                                 address = recvdword();
101                                 increaseled();
102
103                                 size = recvword();
104                                 increaseled();
105
106                                 PDR14 = 0xff;
107                                 for(i=0; i<size; i++) { /* get data */
108                                         increaseled();
109                                         data[i] = recvbyte();
110                                 }
111
112                                 PDR14 = 0xff;
113                                 for(i=0; i<size; i+=2) { /* flash the data */
114                                         increaseled();
115                                         next = (((unsigned short)data[i])<<8) | (unsigned short)data[i+1];
116                                         if(FLASH_WriteHalfWord(address + i, next) != 1) {
117                                                 panic();
118                                         }
119                                 }
120                                 Putch4(0x28); //Flashing done.
121                                 break;
122
123                         case 0x97: /* exit and restart (let do this by the watchdog!) */
124                                 while(1) {
125                                         increaseled();
126                                 }
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