okay, here is a programmer (=kernel.py) which communicate to the pkernel
[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 int ret;
24         b1 = recvbyte();
25         b2 = recvbyte();
26         ret = (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 = (b4 << 24) | (b3 << 16) | (b2 << 8) | 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 = 0xff;
52         PDR14 = 0x22;
53         halt();
54 }
55
56 void main(void)
57 {
58         unsigned int address;
59         unsigned short i, size;
60         unsigned char running = 1, data[BUFSIZE] = {0};
61         
62         PORTEN = 0x3; /* enable I/O Ports */
63
64         /*Enable LEDs*/
65         DDR14 = 0xFF;
66         PDR14 = 0xff;
67
68         /*Initialize UART4*/
69         InitUart4();
70
71         while(running) {
72                 cleardata();
73                 increaseled();
74                 switch(recvbyte()) {
75                         case 0x13: //receive
76                                 Putch4(0x37);
77                                 increaseled();
78
79                                 address = recvdword();
80                                 increaseled();
81
82                                 size = recvword();
83                                 increaseled();
84
85                                 Putch4(0x04); //Received Metadata.
86                                 PDR14 = 0xff;
87                                 for(i=0; i<size; i++) { /* get data */
88                                         increaseled();
89                                         data[i] = recvbyte();
90                                 }
91                                 Putch4(0x08); //Received Data.
92
93                                 PDR14 = 0xff;
94                                 for(i=0; i<size; i+=4) { /* erase */
95                                         if(FLASH_SectorErase(address + i) != 1) {
96                                                 panic();
97                                         }
98                                         increaseled();
99                                 }
100
101                                 Putch4(0x18); //Erasing done.
102                                 for(i=0; i<size; i++) { /* flash the data */
103                                         increaseled();
104                                         if(FLASH_WriteHalfWord(address + (2*i), data[i]) != 1) {
105                                                 panic();
106                                         }
107                                 }
108                                 Putch4(0x28); //Flashing done.
109                                 break;
110
111                         case 0x99: /* exit */
112                                 running = 0;
113                                 break;
114                         default: break;
115                 }
116         }
117
118         PDR14 = 0x55; //signal that we finished now!
119         halt();
120
121 #if 0
122         i = 0;
123         baseaddr = 0xf4000;
124         for (; i <0x10; i+=4) {
125                 (void) FLASH_SectorErase(baseaddr + i);
126                 increaseled();
127         }
128
129         i = 0;
130         baseaddr = 0xf4000;
131         for(; i<9; i++) {
132                 increaseled();
133                 (void) FLASH_WriteHalfWord(baseaddr + (2*i), toflash[i]);
134         }
135
136 #endif
137 }
138