OpenCores
URL https://opencores.org/ocsvn/mips789/mips789/trunk

Subversion Repositories mips789

[/] [mips789/] [branches/] [avendor/] [CTool/] [ser_dld.c] - Blame information for rev 51

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 10 mcupro
 
2
/*  RS-232 example */
3
/*  Compiles with Microsoft Visual C++ 5.0/6.0 */
4
/*  (c) fpga4fun.com KNJN LLC - 2003, 2004, 2005, 2006 */
5
/*  modified by Liwei 2007-9-2 */
6
 
7
#include "stdio.h" 
8
#include "windows.h" 
9
#include "conio.h" 
10
#define MAX_PRG_SIZE 0x1d00 
11
 
12
#define DEFAULT_BAUD_RATE 38400 
13
/*the two functions listed between are added by Liwei 2007-8-29*/
14
char HEX[]="0123456789ABCDEF" ;
15
char hex[]="0123456789abcdef" ;
16
unsigned char hex2byte(char hex_char)
17
{
18
    unsigned char i ;
19
    for(i=0;i<16;++i)if(HEX[i]==hex_char)return i ;
20
    for(i=0;i<16;++i)if(hex[i]==hex_char)return i ;
21
    return 0 ;
22
}
23
unsigned char hex2u8(char*par)
24
{
25
    return(hex2byte(par[0])*16+hex2byte(par[1]));
26
}
27
unsigned int par2u32(char*par)
28
{
29
    unsigned int i,ret=0 ;
30
    if(par==NULL)return ;
31
    if((0==strncmp(par,"0x",2))||(0==strncmp(par,"0X",2)))
32
    for(i=2;;++i)
33
    {
34 35 mcupro
        if(par[i]=='\0')return ret ;if(par[i]==' ')return ret ;
35 10 mcupro
        ret=ret*16+hex2byte(par[i]);
36
    }
37
    else
38
    for(i=0;;++i)
39
    {
40 35 mcupro
        if(par[i]=='\0')return ret ;if(par[i]==' ')return ret ;
41 10 mcupro
        ret=ret*10+hex2byte(par[i]);
42
    }
43
    return 0 ;
44
}
45
/****************************/
46
HANDLE hCom ;
47
 
48
void OpenCom(unsigned int baudrate,char*comno)
49
{
50
    DCB dcb ;
51
    COMMTIMEOUTS ct ;
52
 
53
    hCom=CreateFile(comno,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
54
    if(hCom==INVALID_HANDLE_VALUE)exit(1);
55
    if(!SetupComm(hCom,4096,4096))exit(1);
56
 
57
    if(!GetCommState(hCom,&dcb))exit(1);
58
    dcb.BaudRate=baudrate ;
59
    //modified by `Liwei 2007-9-4
60
    ((DWORD*)(&dcb))[2]=0x1001 ;
61
    /*  set port properties for TXDI + no flow-control */
62
    dcb.ByteSize=8 ;
63
    dcb.Parity=NOPARITY ;
64
    dcb.StopBits=2 ;
65
    if(!SetCommState(hCom,&dcb))exit(1);
66 15 mcupro
 
67 10 mcupro
    /*  set the timeouts to 0 */
68
    ct.ReadIntervalTimeout=MAXDWORD ;
69
    ct.ReadTotalTimeoutMultiplier=0 ;
70
    ct.ReadTotalTimeoutConstant=0 ;
71
    ct.WriteTotalTimeoutMultiplier=0 ;
72
    ct.WriteTotalTimeoutConstant=0 ;
73
    if(!SetCommTimeouts(hCom,&ct))exit(1);
74
}
75
 
76
void CloseCom()
77
{
78
    CloseHandle(hCom);
79
}
80
 
81
DWORD WriteCom(char*buf,int len)
82
{
83
    DWORD nSend ;
84
    if(!WriteFile(hCom,buf,len,&nSend,NULL))exit(1);
85
 
86
    return nSend ;
87
}
88
 
89
void WriteComChar(char b)
90
{
91
    WriteCom(&b,1);
92
}
93
 
94
int ReadCom(char*buf,int len)
95
{
96
    DWORD nRec ;
97
    if(!ReadFile(hCom,buf,len,&nRec,NULL))exit(1);
98
 
99
    return(int)nRec ;
100
}
101
 
102
char ReadComChar()
103
{
104
    DWORD nRec ;
105
    char c ;
106
    if(!ReadFile(hCom,&c,1,&nRec,NULL))exit(1);
107
 
108
    return nRec?c:0 ;
109
}
110 15 mcupro
//ser_dld 19200 COM1 N
111 10 mcupro
void main(int argc,char*argv[])
112
{
113
    char c,s[11];
114
    int i,baud_rate ;
115
 
116
    FILE*ff=fopen("code.txt","r");
117
    baud_rate=(NULL!=argv[1])?par2u32(argv[1]):DEFAULT_BAUD_RATE ;
118
    //baud_rate=DEFAULT_BAUD_RATE;
119
    OpenCom(baud_rate,argv[2]);
120
    WriteComChar('?');
121
    Sleep(1);
122
    i=100 ;
123 15 mcupro
    while(i--);
124 10 mcupro
    c=ReadComChar();
125
    WriteComChar('!');
126
    Sleep(10);
127
    c=ReadComChar();
128
    if((argv[3]!=NULL)&&(argv[3][0]=='N'))
129
    {
130 15 mcupro
 
131 10 mcupro
    }
132
    else
133
    {
134
        if(c!='O')
135
        {
136
            //printf("%c",c);
137
            printf("1,Powerup you board.\n2,Chech cable.\nany key to exit...\n");
138
            getchar();
139
            return ;
140
        }
141
        else
142
        {
143
            printf("Downloading,wait...\n");
144
        }
145
    }
146
 
147 15 mcupro
 
148 10 mcupro
    rewind(ff);
149
    while(fgets(s,10,ff))
150
    {
151
        WriteComChar(hex2u8((char*)(s+0)));
152
        printf("%2.2x  ",hex2u8((char*)(s+0)));
153
        Sleep(1);
154
 
155
        WriteComChar(hex2u8((char*)(s+2)));
156
        printf("%2.2x  ",hex2u8((char*)(s+2)));
157
        Sleep(1);
158
 
159
        WriteComChar(hex2u8((char*)(s+4)));
160
        printf("%2.2x  ",hex2u8((char*)(s+4)));
161
        Sleep(1);
162
 
163
        WriteComChar(hex2u8((char*)(s+6)));
164
        printf("%2.2x  \n",hex2u8((char*)(s+6)));
165
        Sleep(1);
166
 
167
    }
168
    Sleep(1);
169
    printf("Download to MIPS789 OK!\nPress RESET botton to RUN the MIPS789 program...");
170
    WriteComChar(0x88);
171
    getch();
172
    CloseCom();
173
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.