// File: /workspace/github_c_makefile_star2_O3/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, 0x17(%rsp)
movabsq $0x7974742f7665642f, %rax # imm = 0x7974742F7665642F
movq %rax, 0x10(%rsp)
leaq 0xe67(%rip), %r14       # 0x23c1
movq %r14, %rdi
callq 0x1060
leaq 0xdd5(%rip), %rdi       # 0x233e
callq 0x1060
movq %r14, %rdi
callq 0x1060
leaq 0xde7(%rip), %rdi       # 0x2364
callq 0x1060
leaq 0xe12(%rip), %rdi       # 0x239b
callq 0x1060
movq %r14, %rdi
callq 0x1060
cmpl $0x3, %ebp
jne 0x15ac
movq 0x10(%rbx), %rdi
callq 0x1170
movl %eax, 0x2b3e(%rip)      # 0x40e8
jmp 0x15b1
cmpl $0x2, %ebp
jl 0x15fc
movq 0x8(%rbx), %rdi
callq 0x1170
movq %rax, (%rsp)
leal -0x1(%rax), %ecx
cmpl $0x40, %ecx
jae 0x15fc
leaq 0x15(%rsp), %rdi
leaq 0xa6f(%rip), %rdx       # 0x2041
movl $0x8, %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x10(%rsp), %rdi
callq 0x13bc
testl %eax, %eax
jns 0x16a2
leaq 0xa66(%rip), %rdi       # 0x205d
jmp 0x1718
movl $0x17, %ebx
leaq 0x15(%rsp), %r14
leaq 0xa3c(%rip), %r15       # 0x2049
leaq 0x10(%rsp), %r12
leaq 0xa3a(%rip), %r13       # 0x2053
leaq 0xa21(%rip), %rbp       # 0x2041
movq $0x0, (%rsp)
leal -0x1(%rbx), %ecx
cmpl $0x14, %ecx
jb 0x1640
leal -0x15(%rbx), %ecx
movl $0x8, %esi
movq %r14, %rdi
movq %r15, %rdx
jmp 0x1653
cmpl $0xa, %ecx
jb 0x166c
leal -0xb(%rbx), %ecx
movl $0x8, %esi
movq %r14, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x10c0
movq %r12, %rdi
callq 0x13bc
testl %eax, %eax
jns 0x169c
decl %ebx
jne 0x1628
jmp 0x15f0
cmpl $0xa, %ebx
jne 0x1682
movabsq $0x30414d41797474, %rax # imm = 0x30414D41797474
movq %rax, 0x15(%rsp)
jmp 0x165a
movl $0x8, %esi
movq %r14, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x10c0
movl %ebx, %eax
movq %rax, (%rsp)
jmp 0x165a
cmpl $0x0, (%rsp)
je 0x16c2
leaq 0x9c6(%rip), %rsi       # 0x206f
xorl %ebx, %ebx
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rcx
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x1180
jmp 0x16e4
leaq 0x9bc(%rip), %rsi       # 0x2085
movq $0x0, (%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x1180
movb $0x1, %bl
leaq 0x40(%rsp), %rdi
callq 0x136e
movl 0x29fc(%rip), %edi      # 0x40f0
leaq 0x29f9(%rip), %rdx      # 0x40f4
xorl %esi, %esi
callq 0x1140
movl 0x29e8(%rip), %edi      # 0x40f0
callq 0x10d0
testl %eax, %eax
je 0x1734
leaq 0x97b(%rip), %rdi       # 0x2093
callq 0x136e
movl $0x1, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x30(%rsp), %r14
leaq 0x96c(%rip), %r15       # 0x20ac
leaq 0x9b7(%rip), %rbp       # 0x20fe
movl %ebx, 0x24(%rsp)
movq %r14, %rdi
testb %bl, %bl
je 0x175e
leaq 0x10(%rsp), %rsi
callq 0x1050
jmp 0x1770
leaq 0x941(%rip), %rsi       # 0x20a6
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x1180
leaq 0x40(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x1180
movq %r12, %rdi
callq 0x136e
movq %rbp, %rdi
callq 0x136e
leaq 0x98b(%rip), %rdi       # 0x2127
callq 0x136e
movq %r12, %rdi
callq 0x1090
movl %eax, %ebp
movq %r12, %rdi
leaq 0x9c4(%rip), %rsi       # 0x2179
movl %eax, %edx
xorl %eax, %eax
callq 0x1180
movq %r12, %rdi
callq 0x136e
testl %ebp, %ebp
jne 0x1a06
leaq 0x9f4(%rip), %rdi       # 0x21c9
callq 0x136e
leaq 0x9f0(%rip), %rdi       # 0x21d1
callq 0x136e
leaq 0xa34(%rip), %rdi       # 0x2221
callq 0x136e
leaq 0xa50(%rip), %rdi       # 0x2249
callq 0x136e
leaq 0x10(%rsp), %rdi
callq 0x13bc
xorl %r14d, %r14d
xorl %ebx, %ebx
movq $0x0, 0x8(%rsp)
callq 0x12ac
testl %eax, %eax
jne 0x19b7
callq 0x144d
testb %al, %al
je 0x18f3
movl %eax, %ebp
cmpl $0x4, %r14d
jl 0x1842
movl $0xa, %edi
callq 0x1030
callq 0x1190
movq %rax, %r14
movq (%rax), %rax
movsbl %bpl, %r15d
movsbq %bpl, %r12
testb $0x40, 0x1(%rax,%r12,2)
je 0x1865
movl %r15d, %edi
callq 0x1030
cmpb $0xa, %bpl
jne 0x1896
movl $0xa, %edi
callq 0x1030
movl %ebx, %eax
xorl $0x2, %eax
movl 0x2868(%rip), %ecx      # 0x40e8
xorl 0x8(%rsp), %ecx
orl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
je 0x188f
movl %ebx, %eax
xorl %r14d, %r14d
movl %eax, %ebx
jmp 0x18f3
movq (%r14), %rax
movzwl (%rax,%r12,2), %eax
btl $0xb, %eax
jb 0x18dc
cmpl $0x0, 0x8(%rsp)
je 0x18c2
cmpb $0x2e, %bpl
je 0x18c2
movq 0x8(%rsp), %rcx
cmpl 0x282c(%rip), %ecx      # 0x40e8
jne 0x1a1a
testl %ebx, %ebx
sete %cl
cmpb $0x4f, %bpl
sete %dl
xorl %r14d, %r14d
testb %dl, %cl
je 0x193a
movl $0x1, %ebx
jmp 0x18f3
movq 0x8(%rsp), %rax
leal (%rax,%rax,4), %eax
leal (%r15,%rax,2), %eax
addl $-0x30, %eax
movq %rax, 0x8(%rsp)
xorl %r14d, %r14d
leaq 0x28(%rsp), %rdi
callq 0x1100
movq 0x28(%rsp), %r15
cmpq %r15, %r13
jne 0x190c
movl %r14d, %ebp
jmp 0x192d
cmpl $0x1e, %r14d
jge 0x19c0
leal 0x1(%r14), %ebp
cmpl $0x3, %r14d
jl 0x192a
movl $0x2e, %edi
callq 0x1030
movq %r15, %r13
movl %ebp, %r14d
testl %ebx, %ebx
jns 0x1816
jmp 0x1968
testl %ebx, %ebx
sete %dl
cmpl $0x1, %ebx
sete %sil
cmpb $0x4b, %bpl
sete %cl
andb %sil, %cl
orb %cl, %dl
je 0x195b
movzbl %cl, %ebx
addl %ebx, %ebx
jmp 0x18f3
xorl %r14d, %r14d
btl $0xe, %eax
cmovbl %r14d, %ebx
jmp 0x18f3
movl 0x2782(%rip), %edi      # 0x40f0
xorl %esi, %esi
leaq 0x277d(%rip), %rdx      # 0x40f4
callq 0x1140
movl 0x276e(%rip), %edi      # 0x40f0
callq 0x10d0
testl %eax, %eax
leaq 0x71c(%rip), %r15       # 0x20ac
leaq 0x767(%rip), %rbp       # 0x20fe
jne 0x1711
leaq 0x993(%rip), %rdi       # 0x2337
callq 0x136e
movl 0x24(%rsp), %ebx
leaq 0x30(%rsp), %r14
jmp 0x174b
leaq 0x8d1(%rip), %rdi       # 0x228f
jmp 0x19dd
movl $0xa, %edi
callq 0x1030
leaq 0x91f(%rip), %rdi       # 0x22f0
callq 0x136e
leaq 0x92f(%rip), %rdi       # 0x230c
callq 0x136e
movl 0x2708(%rip), %edi      # 0x40f0
leaq 0x2705(%rip), %rdx      # 0x40f4
xorl %esi, %esi
callq 0x1140
movl 0x26f4(%rip), %edi      # 0x40f0
callq 0x10d0
jmp 0x171d
cmpl $0xa00, %ebp            # imm = 0xA00
jne 0x1a46
leaq 0x772(%rip), %rdi       # 0x2187
jmp 0x1718
movl $0xa, %edi
callq 0x1030
leaq 0x875(%rip), %rdi       # 0x22a0
callq 0x136e
leaq 0x882(%rip), %rdi       # 0x22b9
callq 0x136e
callq 0x14fe
jmp 0x171d
leaq 0x7a6(%rip), %rdi       # 0x21f3
jmp 0x1718