make: check if git is available before calculate version
[pyfrprog.git] / pkernel / main.c
index 89fc2c85f0a415eb04839d99c4a7a0b86e154905..8e92a3a7616c8ca4e4b7c52e2aa5f98b48dd5da9 100644 (file)
 #include "mb91465k.h"
 #include "flash.h"
 
-#pragma section CODE=IRAM,attr=CODE
+#define BUFSIZE 0x20
+#define cleardata() memset(data,0,BUFSIZE)
 
 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)
+{
+       return Getch4();
+}
+
+static unsigned short recvword(void)
+{
+       static unsigned char b1, b2;
+       static unsigned short ret;
+       b1 = recvbyte();
+       b2 = recvbyte();
+       ret = (unsigned short)(b2 << 8) | b1;
+       return ret;
+}
+
+static unsigned int recvdword(void)
+{
+       static unsigned char b1, b2, b3, b4;
+       static unsigned int ret;
+       b1 = recvbyte();
+       b2 = recvbyte();
+       b3 = recvbyte();
+       b4 = recvbyte();
+       ret = ((unsigned int) (b4 << 24)) | ((unsigned int) (b3 << 16)) | ((unsigned int) (b2 << 8)) |(unsigned int)b1;
+       return ret;
+}
+
+static void halt(void)
+{
+       while(1) {
+               HWWD_CL = 0;
+       }
+}
+
+static void panic(void)
+{
+       PDR14 = 0x22;
+       halt();
 }
 
 void main(void)
 {
-       unsigned char error = 0;
-       unsigned char global_error = 0; 
-       unsigned int i, baseaddr;
-       unsigned int toflash[] = {0x9b00,
-                                                               0x0d4e,
-                                                               0xcff1,
-                                                               0x1601,
-                                                               0x9b05,
-                                                               0x04c7,
-                                                               0xc106,
-                                                               0x1656,
-                                                               0xe0fb}; //len = 9
+       unsigned int address;
+       unsigned short i, size, next;
+       unsigned char running = 1, data[BUFSIZE] = {0};
        
-       PORTEN = 0x3;           /* enable I/O Ports */
-                               /* This feature is not supported by MB91V460A */
-                               /* For all other devices the I/O Ports must be enabled*/
-
-       /*      Enable LEDs     */
+       /*Enable LEDs*/
        DDR14 = 0xFF;
        PDR14 = 0xff;
 
-       /*      Initialize UART4        */
+       /*Initialize UART4*/
        InitUart4();
 
-       /*      Output Welcome Message  */
-       Puts4(" \n\n");
-       Puts4("\n\n********** Welcome to FUJITSU FLASH Programming Demo **********\n");
+       while(running) {
+               cleardata();
+               increaseled();
+               switch(recvbyte()) {
+                       case 0x15: //chip erase
+                               Putch4(0x45);
+                               PDR14 = ~(0x05);
+                               if(FLASH_ChipErase() != 1) {
+                                       panic();
+                               }
+                               Putch4(0x23);
+                               break;
 
-#if 1
-       i = 0;
-       baseaddr = 0xf4000;
-       for (; i <0x30; i+=4) {
-               error = FLASH_SectorErase(baseaddr + i);
-               Puts4("\nerased: ");
-               Puts4(error == 1 ? "[success] " : "[failed] ");
-               Puthex4(baseaddr + i, 6);
-       }
-#endif
-       i=0;
-       Puts4("\nCurrent Content of FLASH at 0xf4000 ... 0xf401F:\n");
-       while(i < 0x20)
-       {
-               Puts4("0x"); Puthex4( *(unsigned char *)(0xf4000 + i), 2); Puts4(" ");
-               i++;
-               if( (i % 0x10) == 0 ) Puts4("\n");
+                       case 0x13: //receive
+                               Putch4(0x37);
+                               increaseled();
+
+                               address = recvdword();
+                               increaseled();
+
+                               size = recvword();
+                               increaseled();
+
+                               for(i=0; i<size; i++) { /* get data */
+                                       increaseled();
+                                       data[i] = recvbyte();
+                               }
+
+                               for(i=0; i<size; i+=2) { /* flash the data */
+                                       increaseled();
+                                       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;
+                       default: break;
+               }
        }
-       Puts4("\n");
 
+       PDR14 = 0x55; //signal that we finished now!
+       halt();
 
+#if 0
        i = 0;
        baseaddr = 0xf4000;
-       for(; i<9; i++) {
-               Puts4("\nwrite: ");
-               error = FLASH_WriteHalfWord(baseaddr + (2*i), toflash[i]);
-               Puts4(error == 1 ? "[sucess] " : "[failed] ");
-               Puts4("0x");
-               Puthex4(toflash[i], 4);
-               Puts4(" @0x");
-               Puthex4(baseaddr + (2*i), 6);
+       for (; i <0x10; i+=4) {
+               (void) FLASH_SectorErase(baseaddr + i);
+               increaseled();
        }
 
-       /*      Output Ready Meassage   */
-       if( global_error != 0 )
-       {
-               Puts4("\n********* FLASH Programming Demo failed **********\n");
-       }
-       else
-       {
-               Puts4("\n********* FLASH Programming Demo done **********\n");
+       i = 0;
+       baseaddr = 0xf4000;
+       for(; i<9; i++) {
+               increaseled();
+               (void) FLASH_WriteHalfWord(baseaddr + (2*i), toflash[i]);
        }
 
-    while(1)
-    {    
-         
-       HWWD_CL = 0;   
-    }   
+#endif
 }