// File: /workspace/github_c_makefile_star2_O2/bokunimowakaru[P]petit15term/petit15writer.c
// Function: main
int main(int argc,char **argv){
    int fd=-1,loop=1,i,com=0,ret,ver,ok,wait;
    time_t time_val,time_prev;
    char c;
    char s[256],ss[16];
    struct termios uartattr;
    //                    0123456789ABC
    char UART_DEVICE[15]="/dev/ttyUSB10";

    printf("-------------------------------------\n");
    printf("Petit Ichigo Writer  by Wataru KUNINO\n");
    printf("-------------------------------------\n");
    printf("注意：ファームウェアを書き換えます。\n");
    printf("This software updates firmware on LPC\n");
    printf("-------------------------------------\n");
    
    if( argc == 3 ) TARGET_VERSION = atoi(argv[2]);
    if( argc >= 2){
        i=atoi(argv[1]);
        if( i>0 && i<= 64){
            snprintf(&UART_DEVICE[5],8,"ttyS%1d",i-1);  // Cygwin PC用
            fd = uart_open(UART_DEVICE, &uartattr);
            com=i;
        }
    }
    if(com==0) for(i=22;i>=0;i--){
        if(i>=20){
            snprintf(&UART_DEVICE[5],8,"rfcomm%1d",i-20);   // ポート探索(rfcomm0-2)
        }else if(i>=10){
            snprintf(&UART_DEVICE[5],8,"ttyUSB%1d",i-10);  // ポート探索(USB0～9)
        }else if(i==9) snprintf(&UART_DEVICE[5],8,"ttyAMA0");   // 拡張IOのUART端子に設定
        else{
            snprintf(&UART_DEVICE[5],8,"ttyS%1d",i);            // Cygwin PC用
            com=i+1;
        }
    //  log_date(UART_DEVICE);
        fd = uart_open(UART_DEVICE, &uartattr);
        if(fd >=0 ) break;
    }
    
    if (fd < 0){
        log_date("ERROR @ UART OPEN");
        return 1;
    }else{
		if(com) sprintf(s,"COM PORT = COM%d (%s)",com,UART_DEVICE);
        else sprintf(s,"COM PORT = %s",UART_DEVICE);
        log_date(s);
    }
    if(close_serial_port()){            // 一旦閉じる
        log_date("ERROR @ UART CLOSE");
        return 1;
    }
    
    while(loop){
		if(com) sprintf(ss,"com%d",com);
		else sprintf(ss,"%s",UART_DEVICE);
        sprintf(s,"lpc21isp/lpc21isp -verify -donotstart firmware/ichigojam-xtal.hex %s 115200 12000",ss);
        log_date(s);
        log_date("Please connect a LPC Device to your jig.");
        log_date("LPCマイコンをボードにセットしてISPボタンを押してください");
        ret=system(s);
        sprintf(s,"lpc21isp (%d)",ret);         // 256:ファームウェア無し、2560 ユーザ終了 65280
        log_date(s);
        switch(ret){
            case 2560:
                log_date("[Esc]キーが押されたので書き換えを中断しました");
                return 1;
                break;
            case 0:
                log_date("SUCCESS");
                log_date("書き込みに成功しました");
                break;
            default:
                log_date("エラーが発生したので終了します");
                return 1;
                break;
        }

        log_date("Please press reset button on IchigoJam.");
        log_date("マイコンボードのリセットボタンを押してください");
        uart_open(UART_DEVICE, &uartattr);

        wait=0; ok=0; ver=0;
        while(ok>=0){
            if( kbhit() ){
                log_date("Aborted by user.");
                close_serial_port();
                return 1;
            }
            c=read_serial_port();
            if(c){
                if( wait > 3 ){
                    printf("\n");
                }
                wait =0;
                if( isprint((int)c) ) printf("%c",c);
                if( c =='\n' ){
                    printf("\n");
                    if(c =='\n' && ok == 2 && ver == TARGET_VERSION) ok=-1; // ループ終了
                }else if( isdigit((int)c) ){
                    ver *=10;
                    ver += (int)(c - '0');
                }else if( ver !=0 && ver != TARGET_VERSION && c !='.' ){
                    printf("\n");
                    log_date("IchigoJam Version ERROR.");
                    log_date("期待バージョンと異なるので終了します");
                    close_serial_port();
                    return 1;
                }else if( ok==0 && c=='O' ){
                    ok++;
                }else if( ok==1 && c=='K' ){
                    ok++;
                }else if( ok && isprint((int)c) ) ok=0;
            }
            time(&time_val);
            if( time_prev != time_val ){
                time_prev = time_val;
                wait++;
                if( wait>30 ){
                    printf("\n");
                    log_date("No response from IchigoJam.");
                    log_date("IchigoJamからの応答がありません");
                    close_serial_port();
                    return 1;
                }
                if( wait > 3) printf(".");
            }
        }
        if(close_serial_port()){
            log_date("ERROR @ UART CLOSE");
            return 1;
        }
        log_date("CLOSED");
    }
    return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rsi, %rbx
movl %edi, %ebp
movabsq $0x303142535579, %rax   # imm = 0x303142535579
movq %rax, 0xf(%rsp)
movabsq $0x7974742f7665642f, %rax # imm = 0x7974742F7665642F
movq %rax, 0x8(%rsp)
leaq 0xe7d(%rip), %r14       # 0x23c1
movq %r14, %rdi
callq 0x1060
leaq 0xdeb(%rip), %rdi       # 0x233e
callq 0x1060
movq %r14, %rdi
callq 0x1060
leaq 0xdfd(%rip), %rdi       # 0x2364
callq 0x1060
leaq 0xe28(%rip), %rdi       # 0x239b
callq 0x1060
movq %r14, %rdi
callq 0x1060
cmpl $0x3, %ebp
jne 0x1596
movq 0x10(%rbx), %rdi
callq 0x1170
movl %eax, 0x2b54(%rip)      # 0x40e8
jmp 0x159b
cmpl $0x2, %ebp
jl 0x15e4
movq 0x8(%rbx), %rdi
callq 0x1170
movq %rax, (%rsp)
leal -0x1(%rax), %ecx
cmpl $0x40, %ecx
jae 0x15e4
leaq 0xd(%rsp), %rdi
leaq 0xa85(%rip), %rdx       # 0x2041
pushq $0x8
popq %rsi
xorl %eax, %eax
callq 0x10c0
leaq 0x8(%rsp), %rdi
callq 0x13c2
testl %eax, %eax
jns 0x168c
leaq 0xa7e(%rip), %rdi       # 0x205d
jmp 0x16e9
pushq $0xc
popq %r14
leaq 0xd(%rsp), %r15
leaq 0xa55(%rip), %r12       # 0x2049
pushq $0x8
popq %r13
leaq 0x8(%rsp), %rbp
leaq 0xa4f(%rip), %rbx       # 0x2053
movq $0x0, (%rsp)
movl %r14d, %ecx
addl $0xa, %ecx
js 0x15d8
cmpl $0x14, %ecx
jb 0x1628
leal -0xa(%r14), %ecx
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
jmp 0x1639
cmpl $0xa, %ecx
jb 0x1651
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x10c0
movq %rbp, %rdi
callq 0x13c2
decl %r14d
testl %eax, %eax
js 0x160c
jmp 0x1686
cmpl $-0x1, %r14d
je 0x1675
movq %r15, %rdi
movq %r13, %rsi
leaq 0x9dd(%rip), %rdx       # 0x2041
xorl %eax, %eax
callq 0x10c0
leal 0xb(%r14), %eax
movq %rax, (%rsp)
jmp 0x1640
movabsq $0x30414d41797474, %rax # imm = 0x30414D41797474
movq %rax, 0xd(%rsp)
jmp 0x1640
cmpl $0x0, (%rsp)
je 0x16ac
leaq 0x9dc(%rip), %rsi       # 0x206f
xorl %ebp, %ebp
leaq 0x40(%rsp), %rdi
leaq 0x8(%rsp), %rcx
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x1180
jmp 0x16cf
leaq 0x9d2(%rip), %rsi       # 0x2085
movq $0x0, (%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x1180
movb $0x1, %bpl
leaq 0x40(%rsp), %rdi
callq 0x1374
callq 0x14e8
testl %eax, %eax
je 0x1703
leaq 0x9aa(%rip), %rdi       # 0x2093
callq 0x1374
pushq $0x1
popq %rax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x30(%rsp), %rbx
leaq 0x8(%rsp), %r14
leaq 0x998(%rip), %r15       # 0x20ac
leaq 0x40(%rsp), %r12
movl %ebp, 0x1c(%rsp)
movq %rbx, %rdi
testb %bpl, %bpl
je 0x172f
movq %r14, %rsi
callq 0x1050
jmp 0x1741
leaq 0x970(%rip), %rsi       # 0x20a6
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x1180
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1180
movq %r12, %rdi
callq 0x1374
leaq 0x99e(%rip), %rdi       # 0x20fe
callq 0x1374
leaq 0x9bb(%rip), %rdi       # 0x2127
callq 0x1374
movq %r12, %rdi
callq 0x1090
movl %eax, %ebx
movq %r12, %rdi
leaq 0x9f4(%rip), %rsi       # 0x2179
movl %eax, %edx
xorl %eax, %eax
callq 0x1180
movq %r12, %rdi
callq 0x1374
testl %ebx, %ebx
jne 0x1977
leaq 0xa24(%rip), %rdi       # 0x21c9
callq 0x1374
leaq 0xa20(%rip), %rdi       # 0x21d1
callq 0x1374
leaq 0xa64(%rip), %rdi       # 0x2221
callq 0x1374
leaq 0xa80(%rip), %rdi       # 0x2249
callq 0x1374
movq %r14, %rdi
callq 0x13c2
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %r15d, %r15d
movl %r15d, %r14d
testl %r12d, %r12d
js 0x1929
callq 0x12ac
testl %eax, %eax
jne 0x1961
callq 0x1451
testb %al, %al
je 0x18ad
movl %eax, %ebx
cmpl $0x4, %r14d
jl 0x1814
pushq $0xa
popq %rdi
callq 0x1030
callq 0x1190
movq %rax, %r14
movq (%rax), %rax
movsbl %bl, %r15d
movsbq %bl, %r13
testb $0x40, 0x1(%rax,%r13,2)
je 0x1837
movl %r15d, %edi
callq 0x1030
cmpb $0xa, %bl
jne 0x1864
pushq $0xa
popq %rdi
callq 0x1030
movl %r12d, %eax
xorl $0x2, %eax
movl 0x2898(%rip), %ecx      # 0x40e8
xorl %ebp, %ecx
orl %eax, %ecx
pushq $-0x1
popq %rax
je 0x185c
movl %r12d, %eax
xorl %r14d, %r14d
movl %eax, %r12d
jmp 0x18ad
movq (%r14), %rax
movzwl (%rax,%r13,2), %eax
btl $0xb, %eax
jb 0x18a0
testl %ebp, %ebp
je 0x1887
cmpb $0x2e, %bl
je 0x1887
cmpl 0x2867(%rip), %ebp      # 0x40e8
jne 0x198b
testl %r12d, %r12d
sete %cl
cmpb $0x4f, %bl
sete %dl
xorl %r14d, %r14d
testb %dl, %cl
je 0x18c9
pushq $0x1
popq %r12
jmp 0x18ad
imull $0xa, %ebp, %eax
leal (%rax,%r15), %ebp
addl $-0x30, %ebp
xorl %r14d, %r14d
leaq 0x28(%rsp), %rdi
callq 0x1100
movq 0x28(%rsp), %rbx
cmpq %rbx, 0x20(%rsp)
je 0x17e1
jmp 0x18fa
testl %r12d, %r12d
sete %dl
cmpl $0x1, %r12d
sete %sil
cmpb $0x4b, %bl
sete %cl
andb %sil, %cl
orb %cl, %dl
je 0x18ed
movzbl %cl, %r12d
addl %r12d, %r12d
jmp 0x18ad
xorl %r14d, %r14d
btl $0xe, %eax
cmovbl %r14d, %r12d
jmp 0x18ad
cmpl $0x1e, %r14d
jge 0x19b4
leal 0x1(%r14), %r15d
movq %rbx, 0x20(%rsp)
cmpl $0x2, %r14d
jle 0x17de
pushq $0x2e
popq %rdi
callq 0x1030
movq %rbx, 0x20(%rsp)
jmp 0x17de
callq 0x14e8
testl %eax, %eax
jne 0x16e2
leaq 0x9fa(%rip), %rdi       # 0x2337
callq 0x1374
movl 0x1c(%rsp), %ebp
leaq 0x30(%rsp), %rbx
leaq 0x8(%rsp), %r14
leaq 0x755(%rip), %r15       # 0x20ac
leaq 0x40(%rsp), %r12
jmp 0x171d
leaq 0x927(%rip), %rdi       # 0x228f
callq 0x1374
callq 0x14e8
jmp 0x16ee
cmpl $0xa00, %ebx            # imm = 0xA00
jne 0x19a8
leaq 0x801(%rip), %rdi       # 0x2187
jmp 0x16e9
pushq $0xa
popq %rdi
callq 0x1030
leaq 0x906(%rip), %rdi       # 0x22a0
callq 0x1374
leaq 0x913(%rip), %rdi       # 0x22b9
jmp 0x1968
leaq 0x844(%rip), %rdi       # 0x21f3
jmp 0x16e9
pushq $0xa
popq %rdi
callq 0x1030
leaq 0x92d(%rip), %rdi       # 0x22f0
callq 0x1374
leaq 0x93d(%rip), %rdi       # 0x230c
jmp 0x1968