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