OSDN Git Service

[UART] Load via S-Record is okay. Setting is 57600bps, 8N1, but some of noise (bit7...
[openi2cradio/OpenI2CRadio.git] / shell_strutl.c
index accd770..c4e5e85 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;
 }
@@ -89,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;
+    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;
@@ -126,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;
 }
 
@@ -135,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;
@@ -146,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]);
@@ -176,45 +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)
-{
-    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;
-}
 
 /*
  * Set "S1" Record
@@ -270,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';
@@ -278,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++;
     }