OSDN Git Service

[Schematic] Add 2nd page of 2nd generation pre-amp (LW/MW).
[openi2cradio/OpenI2CRadio.git] / shell_strutl.c
index eec3409..f300aa9 100644 (file)
 #include <signal.h>
 
 #include "shell_strutl.h"
+#include "euart.h"
+#include "uart_termio.h"
 
+void bin2hex(char *s, unsigned char v)
+{
+    unsigned char c;
+    c = v >> 4;
+    if(c > 9) {
+        c = c - 10 + 'A';
+    } else {
+        c = c + '0';
+    }
+    s[0] = c;
+
+    c = v & 0x0f;
+    if(c > 9) {
+        c = c - 10 + 'A';
+    } else {
+        c = c + '0';
+    }
+    s[1] = c;
+    s[2] = 0x00;
+}
 unsigned char c2h(unsigned char c)
 {
-    if(c < '1') return 0xff;
+    if(c < '0') return 0xff;
     if(c > 'F') return 0xff;
     if(c <= '9') return c - '0';
     if(c >= 'A') return c - 'A' + 10;
     return 0xff;
 }
 
-
 unsigned char hex2byte(unsigned char *p)
 {
     unsigned char h, l;
     h = c2h(p[0]);
-    l  = c2h(p[1]);
+    l = c2h(p[1]);
     if((h >= 0x10) || (l >= 0x10)) return 0;
     return (h << 4) | l;
 }
@@ -64,13 +85,14 @@ unsigned char check_eol(unsigned char *p)
         switch(c){
             case '\0':
             case '\n':
+            case '\r':
                 return 0xff;
                 break;
             default:
                 break;
         }
-        if(c < '0') return 0xff;
-        if(c > 'F') return 0xff;
+//        if(c < '0') return 0xff;
+//        if(c > 'F') return 0xff;
     }
     return 0;
 }
@@ -88,31 +110,50 @@ unsigned char migrate_hex(unsigned char *p)
     return 0xff;
 }
 
+int  search_head_s(unsigned char *s)
+{
+   int i;
+   for(i = 0; i < 127; i++) {
+      switch(s[i]) {
+       case 'S':
+        return i;
+        break;
+       case '\0':
+        return -1; // Not found 'S' until eoln.
+        break;
+       default:
+        break;
+      }
+   }
+   return -1; // Error
+}
+
 /*
  * Saving or Loading use MOTOROLLA S FORMAT with 24bit address.
  * See details:
  * http://www.geocities.jp/chako_ratta/micon/s_format.html (written in Japanese)
  * 
  */
-char str_shexheader(unsigned char *s, unsigned char *file)
+int str_shexheader(unsigned char *s, unsigned char *file)
 {
     unsigned char pp;
     unsigned char sum;
     unsigned char c;
     unsigned char bytes;
     unsigned int i;
-    if(check_eol(s) != 0) return TERM_NULL;
+    unsigned char sbuf[4];
+//    if(check_eol(s) != 0) return TERM_NULL;
 
     if((s[0] != 'S') || (s[1] != '0')) return TERM_NONSREC;
-
+    uart_term_putstr("\nHEAD");
     if(migrate_hex(&s[2])  == 0) return TERM_NONSREC;
     bytes = hex2byte(&s[2]);
     if(bytes <= 2) return TERM_SRECERR;
     for(i = 4; i < 8; i++) if(s[i] != '0') return TERM_NONHDR;
-    sum = 0;
-    bytes -= 2;
+    sum = bytes;
+    bytes -= 3;
     pp = 0;
-     while(bytes > 1) {
+    while(bytes > 0) {
         if(migrate_hex(&s[i]) == 0) return TERM_SRECERR;
         c = hex2byte(&s[i]);
         sum += c;
@@ -125,7 +166,11 @@ char str_shexheader(unsigned char *s, unsigned char *file)
     if(migrate_hex(&s[i]) == 0) return TERM_SRECERR;
     c = hex2byte(&s[i]);
     sum = ~sum;
+    bin2hex(sbuf, sum);
+    uart_term_putstr(sbuf);
+
     if(c != sum) return TERM_SUMERR;
+    uart_term_putstr("...OK\n");
     return TERM_OK;
 }
 
@@ -134,7 +179,7 @@ char str_shexheader(unsigned char *s, unsigned char *file)
  * 'S1' record to hex
  * Accept only S2 and S8.
  */
-char str_shex2bin(unsigned char *s, unsigned char *p, unsigned long *addr, unsigned char *len)
+int str_shex2bin(unsigned char *s, unsigned char *p, unsigned long *addr, unsigned char *len)
 {
     unsigned char sum = 0;
     unsigned char pp;
@@ -145,9 +190,9 @@ char str_shex2bin(unsigned char *s, unsigned char *p, unsigned long *addr, unsig
     // Get Header
     if(s[0] != 'S')  return TERM_NONSREC;
     if(s[1] == '2') { //  Data core
-        for(i = 2; i < 8; i += 2) if(migrate_hex(&s[i]) == 0) return TERM_SRECERR;
+        for(i = 2; i < 10; i += 2) if(migrate_hex(&s[i]) == 0) return TERM_SRECERR;
         bytes = hex2byte(&s[2]);
-        if(bytes <= 4) return TERM_SRECERR;
+        if(bytes <= 3) return TERM_SRECERR;
         sum = bytes;
         h = hex2byte(&s[4]);
         l  = hex2byte(&s[6]);
@@ -165,7 +210,7 @@ char str_shex2bin(unsigned char *s, unsigned char *p, unsigned long *addr, unsig
             p[pp] = c;
             i += 2;
             pp++;
-             bytes--;
+            bytes--;
         }
         if(migrate_hex(&s[i]) == 0) return TERM_SUMERR;
         c = hex2byte(&s[i]);
@@ -175,31 +220,26 @@ char str_shex2bin(unsigned char *s, unsigned char *p, unsigned long *addr, unsig
          * OK!
          */
     } else if(s[1] == '8') {
-        if((s[2] != '0') || (s[3] != '3')) return TERM_SRECERR;
+        if((s[2] != '0') || (s[3] != '4')) return TERM_SRECERR;
+        uart_term_putstr("TAIL");
         for(i = 4; i < 10; i += 2)  if(migrate_hex(&s[i]) == 0) return TERM_SRECERR;
-        sum = 3;
+        sum = 4;
         h = hex2byte(&s[4]);
         l = hex2byte(&s[6]);
         l2 = hex2byte(&s[8]);
         c = hex2byte(&s[10]);
-        *addr = (h * 65536) | (l << 8) | l2;
+//        *addr = (h * 65536) | (l << 8) | l2;
         *len = 0;
         sum = ~(sum + h + l + l2);
         if(c != sum) return TERM_SUMERR;
+        uart_term_putstr(" OK.");
         return TERM_SRECEND;
     } else {
-
         return TERM_UNDSREC;
     }
     return TERM_OK;
 }
 
-void bin2hex(char *s, unsigned char v)
-{
-    s[0] = v >> 4;
-    s[1] = v & 0x0f;
-    s[2] = 0x00;
-}
 
 /*
  * Set "S1" Record
@@ -214,7 +254,7 @@ unsigned char str_bin2hex(unsigned char *s, unsigned char *p, unsigned long addr
     s[0] = 'S';
     s[1] = '2';
     if(len > 254) len = 254;
-    i = len + 1;
+    i = len + 4; //HEX
     sum = i;
     bin2hex(&s[2], i);
 
@@ -255,7 +295,7 @@ unsigned char str_put_shexheader(unsigned char *s, char *filename)
     s[1] = '0';
     len = shell_strlen(filename);
     if(len > 252) len = 252;
-    sum = len+3;
+    sum = len + 3;
     bin2hex(&s[2], len + 3);
 
     for(i = 4; i < 8; i++) s[i] = '0';
@@ -263,7 +303,7 @@ unsigned char str_put_shexheader(unsigned char *s, char *filename)
     pp = 0;
     while(pp != len){
         bin2hex(&s[i], filename[pp]);
-        sum += s[i];
+        sum += filename[pp];
         i += 2;
         pp++;
     }
@@ -307,13 +347,17 @@ unsigned char shell_strlen(char *s)
 unsigned char shell_gettok(char *dst, char *src)
 {
     unsigned char i;
+    char c;
     i = 0;
-    while(src[i] != '\0') {
-        if(src[i] == ' ') break;
-        if(src[i] == '\t') break;
-        dst[i] = src[i];
+    while(1) {
+        c = src[i];
+        if(c == '\0') break;
+        if(c == ' ') break;
+        if(c == '\t') break;
+        if(c == '\n') break;
+        dst[i] = c;
         i++;
-        if(i > 128) break;
+        if(i > 126) break;
     }
     dst[i] = '\0';
     return i;
@@ -325,14 +369,9 @@ int shell_strcmp(char *from, char *to)
     unsigned char i;
     int p = 0;
 
-    f = shell_strlen(from);
-    t = shell_strlen(to);
-    i = f;
-    if(i > t) i = t;
-    while(i != 0){
+    while(from[p] != '\0'){
         if(from[p] != to[p]) return -1;
         p++;
-        i--;
     }
     return p;
 }
@@ -345,8 +384,11 @@ long term_getuint(char *pool)
 
     do {
         c = pool[p];
-        if((c < '0') || (c > '9')) return -1;
         if(c == '\0') break;
+        if(c == '\n') break;
+        if(c == '\t') break;
+        if(c == ' ') break;
+        if((c < '0') || (c > '9')) return -1;
         i *= 10;
         i = i + (c - '0');
         p++;
@@ -358,7 +400,8 @@ long term_getuint(char *pool)
 void term_printnum(char *pool, unsigned int num)
 {
     char p[5];
-    unsigned char i, j, ii;
+    int j;
+    int i, ii;
 
     for(i = 0; i < 5; i++){
         p[i] = num % 10;
@@ -368,8 +411,9 @@ void term_printnum(char *pool, unsigned int num)
     do {
         if(p[i] != 0) break;
         i--;
-    } while(i != 0);
-    if(i == 0){
+    } while(i >= 0);
+   
+    if(i < 0){
         pool[0] = '0';
         pool[1] = '\0';
         return;