nicer led output
[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 void increaseled(void)
8 {
9 #define T_INIT 1100
10         static unsigned int t = T_INIT;
11         static unsigned char l = (1<<0);
12         static unsigned char s = 1;
13         if(t == 0) {
14                 if(l & (1<<0)) {
15                         s = 1;
16                 } else if (l & (1<<7)) {
17                         s = 0;
18                 }
19                 l = s ? l << 1 : l >> 1;
20                 PDR14 = ~l;
21                 t = T_INIT;
22         } else {
23                 t--;
24         }
25 }
26
27 static unsigned char recvbyte(void)
28 {
29         return Getch4();
30 }
31
32 static unsigned short recvword(void)
33 {
34         static unsigned char b1, b2;
35         static unsigned short ret;
36         b1 = recvbyte();
37         b2 = recvbyte();
38         ret = (unsigned short)(b2 << 8) | b1;
39         return ret;
40 }
41
42 static unsigned int recvdword(void)
43 {
44         static unsigned char b1, b2, b3, b4;
45         static unsigned int ret;
46         b1 = recvbyte();
47         b2 = recvbyte();
48         b3 = recvbyte();
49         b4 = recvbyte();
50         ret = ((unsigned int) (b4 << 24)) | ((unsigned int) (b3 << 16)) | ((unsigned int) (b2 << 8)) |(unsigned int)b1;
51         return ret;
52 }
53
54 static void halt(void)
55 {
56         while(1) {
57                 HWWD_CL = 0;
58         }
59 }
60
61 static void panic(void)
62 {
63         PDR14 = 0x22;
64         halt();
65 }
66
67 void main(void)
68 {
69         unsigned int address;
70         unsigned short i, size, next;
71         unsigned char running = 1, data[BUFSIZE] = {0};
72         
73         /*Enable LEDs*/
74         DDR14 = 0xFF;
75         PDR14 = 0xff;
76
77         /*Initialize UART4*/
78         InitUart4();
79
80         while(running) {
81                 cleardata();
82                 increaseled();
83                 switch(recvbyte()) {
84                         case 0x15: //chip erase
85                                 Putch4(0x45);
86                                 PDR14 = ~(0x05);
87                                 if(FLASH_ChipErase() != 1) {
88                                         panic();
89                                 }
90                                 Putch4(0x23);
91                                 break;
92
93                         case 0x13: //receive
94                                 Putch4(0x37);
95                                 increaseled();
96
97                                 address = recvdword();
98                                 increaseled();
99
100                                 size = recvword();
101                                 increaseled();
102
103                                 for(i=0; i<size; i++) { /* get data */
104                                         increaseled();
105                                         data[i] = recvbyte();
106                                 }
107
108                                 for(i=0; i<size; i+=2) { /* flash the data */
109                                         increaseled();
110                                         next = (((unsigned short)data[i])<<8) | (unsigned short)data[i+1];
111                                         if(FLASH_WriteHalfWord(address + i, next) != 1) {
112                                                 panic();
113                                         }
114                                 }
115                                 Putch4(0x28); //Flashing done.
116                                 break;
117
118                         case 0x97: /* exit and restart (let do this by the watchdog!) */
119                                 while(1) {
120                                         unsigned long tmp = 1800;
121                                         while(tmp)
122                                                 tmp = tmp - 1;
123                                         increaseled();
124                                 }
125
126                         case 0x99: /* exit */
127                                 running = 0;
128                                 break;
129                         default: break;
130                 }
131         }
132
133         PDR14 = 0x55; //signal that we finished now!
134         halt();
135
136 #if 0
137         i = 0;
138         baseaddr = 0xf4000;
139         for (; i <0x10; i+=4) {
140                 (void) FLASH_SectorErase(baseaddr + i);
141                 increaseled();
142         }
143
144         i = 0;
145         baseaddr = 0xf4000;
146         for(; i<9; i++) {
147                 increaseled();
148                 (void) FLASH_WriteHalfWord(baseaddr + (2*i), toflash[i]);
149         }
150
151 #endif
152 }
153