make: check if git is available before calculate version
[pyfrprog.git] / pkernel / main.c
index 3676387a43694582dcf5e33e73b4d59a5a15f63e..8e92a3a7616c8ca4e4b7c52e2aa5f98b48dd5da9 100644 (file)
@@ -1,15 +1,27 @@
 #include "mb91465k.h"
 #include "flash.h"
 
-#pragma section CODE=IRAM,attr=CODE
-
 #define BUFSIZE 0x20
 #define cleardata() memset(data,0,BUFSIZE)
 
-static void increaseled(void)
+void increaseled(void)
 {
-       PDR14 = ~(((~PDR14)+1)%256);
-       HWWD_CL = 0;
+#define T_INIT 1100
+       static unsigned int t = T_INIT;
+       static unsigned char l = (1<<0);
+       static unsigned char s = 1;
+       if(t == 0) {
+               if(l & (1<<0)) {
+                       s = 1;
+               } else if (l & (1<<7)) {
+                       s = 0;
+               }
+               l = s ? l << 1 : l >> 1;
+               PDR14 = ~l;
+               t = T_INIT;
+       } else {
+               t--;
+       }
 }
 
 static unsigned char recvbyte(void)
@@ -20,10 +32,10 @@ static unsigned char recvbyte(void)
 static unsigned short recvword(void)
 {
        static unsigned char b1, b2;
-       static unsigned int ret;
+       static unsigned short ret;
        b1 = recvbyte();
        b2 = recvbyte();
-       ret = (b2 << 8) | b1;
+       ret = (unsigned short)(b2 << 8) | b1;
        return ret;
 }
 
@@ -35,7 +47,7 @@ static unsigned int recvdword(void)
        b2 = recvbyte();
        b3 = recvbyte();
        b4 = recvbyte();
-       ret = (b4 << 24) | (b3 << 16) | (b2 << 8) | b1;
+       ret = ((unsigned int) (b4 << 24)) | ((unsigned int) (b3 << 16)) | ((unsigned int) (b2 << 8)) |(unsigned int)b1;
        return ret;
 }
 
@@ -48,7 +60,6 @@ static void halt(void)
 
 static void panic(void)
 {
-       PDR14 = 0xff;
        PDR14 = 0x22;
        halt();
 }
@@ -56,11 +67,9 @@ static void panic(void)
 void main(void)
 {
        unsigned int address;
-       unsigned short i, size;
+       unsigned short i, size, next;
        unsigned char running = 1, data[BUFSIZE] = {0};
        
-       PORTEN = 0x3; /* enable I/O Ports */
-
        /*Enable LEDs*/
        DDR14 = 0xFF;
        PDR14 = 0xff;
@@ -72,6 +81,15 @@ void main(void)
                cleardata();
                increaseled();
                switch(recvbyte()) {
+                       case 0x15: //chip erase
+                               Putch4(0x45);
+                               PDR14 = ~(0x05);
+                               if(FLASH_ChipErase() != 1) {
+                                       panic();
+                               }
+                               Putch4(0x23);
+                               break;
+
                        case 0x13: //receive
                                Putch4(0x37);
                                increaseled();
@@ -82,32 +100,29 @@ void main(void)
                                size = recvword();
                                increaseled();
 
-                               Putch4(0x04); //Received Metadata.
-                               PDR14 = 0xff;
                                for(i=0; i<size; i++) { /* get data */
                                        increaseled();
                                        data[i] = recvbyte();
                                }
-                               Putch4(0x08); //Received Data.
 
-                               PDR14 = 0xff;
-                               for(i=0; i<size; i+=4) { /* erase */
-                                       if(FLASH_SectorErase(address + i) != 1) {
-                                               panic();
-                                       }
-                                       increaseled();
-                               }
-
-                               Putch4(0x18); //Erasing done.
-                               for(i=0; i<size; i++) { /* flash the data */
+                               for(i=0; i<size; i+=2) { /* flash the data */
                                        increaseled();
-                                       if(FLASH_WriteHalfWord(address + (2*i), data[i]) != 1) {
+                                       next = (((unsigned short)data[i])<<8) | (unsigned short)data[i+1];
+                                       if(FLASH_WriteHalfWord(address + i, next) != 1) {
                                                panic();
                                        }
                                }
                                Putch4(0x28); //Flashing done.
                                break;
 
+                       case 0x97: /* exit and restart (let do this by the watchdog!) */
+                               while(1) {
+                                       unsigned long tmp = 1800;
+                                       while(tmp)
+                                               tmp = tmp - 1;
+                                       increaseled();
+                               }
+
                        case 0x99: /* exit */
                                running = 0;
                                break;