4 * Copyright (C) 2013-11-07 K.Ohta <whatisthis.sowhat ai gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2,
9 * or (at your option) any later version.
10 * This library / program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; see the file COPYING. If not, write to the
17 * Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
20 * As a special exception, if you link this(includeed from sdcc) library
21 * with other files, some of which are compiled with SDCC,
22 * to produce an executable, this library does not by itself cause
23 * the resulting executable to be covered by the GNU General Public License.
24 * This exception does not however invalidate any other reasons why
25 * the executable file might be covered by the GNU General Public License.
30 #include <pic18fregs.h> /* ONLY FOR PIC18x */
37 #include "shell_strutl.h"
39 #include "uart_termio.h"
41 void bin2hex(char *s, unsigned char v)
61 unsigned char c2h(unsigned char c)
63 if(c < '0') return 0xff;
64 if(c > 'F') return 0xff;
65 if(c <= '9') return c - '0';
66 if(c >= 'A') return c - 'A' + 10;
70 unsigned char hex2byte(unsigned char *p)
75 if((h >= 0x10) || (l >= 0x10)) return 0;
79 unsigned char check_eol(unsigned char *p)
83 for(i = 0; i < 2; i++){
94 // if(c < '0') return 0xff;
95 // if(c > 'F') return 0xff;
100 unsigned char migrate_hex(unsigned char *p)
104 for(i = 0; i < 2; i++){
106 if(c < '0') return 0;
107 if(c > 'F') return 0;
108 if((c > '9') && (c < 'A')) return 0;
113 int search_head_s(unsigned char *s)
116 for(i = 0; i < 127; i++) {
122 return -1; // Not found 'S' until eoln.
132 * Saving or Loading use MOTOROLLA S FORMAT with 24bit address.
134 * http://www.geocities.jp/chako_ratta/micon/s_format.html (written in Japanese)
137 int str_shexheader(unsigned char *s, unsigned char *file)
144 unsigned char sbuf[4];
145 // if(check_eol(s) != 0) return TERM_NULL;
147 if((s[0] != 'S') || (s[1] != '0')) return TERM_NONSREC;
148 uart_term_putstr("\nHEAD");
149 if(migrate_hex(&s[2]) == 0) return TERM_NONSREC;
150 bytes = hex2byte(&s[2]);
151 if(bytes <= 2) return TERM_SRECERR;
152 for(i = 4; i < 8; i++) if(s[i] != '0') return TERM_NONHDR;
157 if(migrate_hex(&s[i]) == 0) return TERM_SRECERR;
166 if(migrate_hex(&s[i]) == 0) return TERM_SRECERR;
170 uart_term_putstr(sbuf);
172 if(c != sum) return TERM_SUMERR;
173 uart_term_putstr("...OK\n");
180 * Accept only S2 and S8.
182 int str_shex2bin(unsigned char *s, unsigned char *p, unsigned long *addr, unsigned char *len)
184 unsigned char sum = 0;
188 unsigned char h, l, l2;
191 if(s[0] != 'S') return TERM_NONSREC;
192 if(s[1] == '2') { // Data core
193 for(i = 2; i < 10; i += 2) if(migrate_hex(&s[i]) == 0) return TERM_SRECERR;
194 bytes = hex2byte(&s[2]);
195 if(bytes <= 3) return TERM_SRECERR;
199 l2 = hex2byte(&s[8]);
200 *addr = (h * 65536) | (l << 8) | l2;
201 sum = sum + h + l + l2;
207 if(migrate_hex(&s[i]) == 0) return TERM_SRECERR;
215 if(migrate_hex(&s[i]) == 0) return TERM_SUMERR;
218 if(c != sum) return TERM_SUMERR;
222 } else if(s[1] == '8') {
223 if((s[2] != '0') || (s[3] != '4')) return TERM_SRECERR;
224 uart_term_putstr("TAIL");
225 for(i = 4; i < 10; i += 2) if(migrate_hex(&s[i]) == 0) return TERM_SRECERR;
229 l2 = hex2byte(&s[8]);
230 c = hex2byte(&s[10]);
231 // *addr = (h * 65536) | (l << 8) | l2;
233 sum = ~(sum + h + l + l2);
234 if(c != sum) return TERM_SUMERR;
235 uart_term_putstr(" OK.");
247 unsigned char str_bin2hex(unsigned char *s, unsigned char *p, unsigned long addr, unsigned char len)
256 if(len > 254) len = 254;
261 c = (addr >> 16) & 255;
265 c = (addr >> 8) & 255;
274 for(pv = 0; pv < len ; pv++){
281 bin2hex(&s[pp], sum);
288 unsigned char str_put_shexheader(unsigned char *s, char *filename)
296 len = shell_strlen(filename);
297 if(len > 252) len = 252;
299 bin2hex(&s[2], len + 3);
301 for(i = 4; i < 8; i++) s[i] = '0';
305 bin2hex(&s[i], filename[pp]);
317 unsigned char str_put_shexfooter(unsigned char *s)
327 for(pp = 4; pp < 10; pp++) s[pp] = '0';
329 bin2hex(&s[pp], sum);
337 unsigned char shell_strlen(char *s)
341 if(s[i] == '\0') break;
347 unsigned char shell_gettok(char *dst, char *src)
366 int shell_strcmp(char *from, char *to)
372 while(from[p] != '\0'){
373 if(from[p] != to[p]) return -1;
379 long term_getuint(char *pool)
391 if((c < '0') || (c > '9')) return -1;
400 void term_printnum(char *pool, unsigned int num)
403 unsigned char i, j, ii;
405 for(i = 0; i < 5; i++){
420 for(j = 0; j <= ii; j++) {
421 pool[j] = p[i] + '0';