8000 Starting to implement stmdude for Win32 · AKrduino/arduino-stm32@496d0c2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 496d0c2

Browse files
committed
Starting to implement stmdude for Win32
1 parent 3175e73 commit 496d0c2

File tree

3 files changed

+74
-33
lines changed

3 files changed

+74
-33
lines changed

stmdude/main.c

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ lundin@mlu.mine.nu
2020
#include <fcntl.h>
2121

2222
#include "hexfile.h"
23+
#include "serialport.h"
2324

2425
#define FLASHBASE 0x08000000
2526
#define RAMBASE 0x20000000
2627

2728
int baudrate = 38400;
28-
char * port = "/dev/ttyUSB0";
29-
int portfd = -1;
30-
int filefd = -1;
29+
char * portname = "/dev/ttyUSB0";
30+
serport_t port = SERIAL_NOT_OPEN;
3131
int verbose = 0;
3232

3333
struct
@@ -45,23 +45,23 @@ target =
4545

4646
void opentargetport()
4747
{
48-
if (portfd<0)
48+
if (port != SERIAL_NOT_OPEN)
4949
{
50-
portfd = open(port,O_NONBLOCK | O_RDWR);
51-
if (portfd<0)
50+
port = openSerialPort(portname,O_NONBLOCK | O_RDWR);
51+
if (port == SERIAL_NOT_OPEN)
5252
{
53-
fprintf(stderr,"Could not open port [%s] \n",port);
53+
fprintf(stderr,"Error: Could not open port [%s] \n",portname);
5454
_exit(-1);
5555
}
5656
}
5757
// Set serial port parameters E81, raw
58-
setPortConfig(portfd, baudrate);
58+
setPortConfig(port, baudrate);
5959
}
6060

6161
void closeport()
< 8000 /code>
6262
{
63-
if (portfd>0) close(portfd);
64-
portfd = -1;
63+
if (port != SERIAL_NOT_OPEN) closeSerialPort(port);
64+
port = SERIAL_NOT_OPEN;
6565
}
6666

6767
/***********************************************************************
@@ -77,7 +77,7 @@ int bootloader_receive_byte()
7777
unsigned char buf[2];
7878
while (errorcount++<100)
7979
{
80-
result = read(portfd,buf,1);
80+
result = read(port,buf,1);
8181
if (result<0)
8282
{
8383
if (errno != 11) break;
@@ -88,7 +88,7 @@ int bootloader_receive_byte()
8888
}
8989
if (result<0)
9090
{
91-
printf("Error %i:%s\n",errno,strerror(errno));
91+
printf("Device is not responding %i:%s\n",e 8000 rrno,strerror(errno));
9292
}
9393
return buf[0];
9494
}
@@ -100,18 +100,18 @@ int bootloader_wait_ack()
100100
{
101101
if (ack == 0x1F )
102102
{
103-
printf("NACK\n");
103+
printf("protocol error: NACK\n");
104104
return 0;
105105
}
106106
else
107107
{
108-
printf("Read ack: 0x%02x\n",ack);
108+
printf("protocol error: Wait ack got 0x%02x\n",ack);
109109
return 0;
110110
}
111111
}
112112
else
113113
{
114-
if (verbose>3) printf("ACK\n");
114+
if (verbose>4) printf("ACK\n");
115115
return 1;
116116
}
117117
}
@@ -121,7 +121,7 @@ int bootloader_send_command(unsigned char cmd)
121121
unsigned char buf[2];
122122
buf[0]=cmd;
123123
buf[1]=~cmd;
124-
write(portfd,buf,2);
124+
write(port,buf,2);
125125
return bootloader_wait_ack();
126126
}
127127

@@ -136,7 +136,7 @@ int bootloader_send_address(unsigned int addr)
136136
addr = addr>>8;
137137
buf[4] = buf[4]^buf[k];
138138
}
139-
write(portfd,buf,5);
139+
write(port,buf,5);
140140
return bootloader_wait_ack();
141141
}
142142

@@ -145,9 +145,9 @@ void bootloader_connect()
145145
int ack=0;
146146
int count=0;
147147
unsigned char buf[2]={0x7F,0x7F};
148-
while (!ack & count++<2)
148+
while (!ack & (count++<2))
149149
{
150-
write(portfd,buf,1);
150+
write(port,buf,1);
151151
ack = bootloader_wait_ack();
152152
}
153153
}
@@ -188,7 +188,6 @@ void bootloader_get_commands()
188188

189189
void bootloader_go(unsigned int addr)
190190
{
191-
int k;
192191
printf("Bootloader go\n");
193192
}
194193

@@ -237,13 +236,13 @@ int bootloader_write_memory(int addr,int len,char * outbuffer)
237236
if (! ack ) return -1;
238237
n = thisblock-1;
239238
check = n;
240-
write(portfd,&n,1);
239+
write(port,&n,1);
241240
for (k=0; k<thisblock; k++)
242241
{
243-
write(portfd,&outbuffer[k+count],1);
242+
write(port,&outbuffer[k+count],1);
244243
check = check^outbuffer[k+count];
245244
}
246-
write(portfd,&check,1);
245+
write(port,&check,1);
247246
ack = bootloader_wait_ack();
248247
if (! ack ) return -1;
249248
count += thisblock;
@@ -253,7 +252,6 @@ int bootloader_write_memory(int addr,int len,char * outbuffer)
253252

254253
void bootloader_erase_memory()
255254
{
256-
int k;
257255
int ack;
258256
if (verbose>0) printf("Bootloader erase memory\n");
259257
ack = bootloader_send_command(0x43);
@@ -265,7 +263,6 @@ void bootloader_erase_memory()
265263

266264
void bootloader_write_unprotect()
267265
{
268-
int k;
269266
printf("Bootloader write unprotect\n");
270267
}
271268

@@ -287,7 +284,7 @@ void memop(char * cmdstr)
287284
intelln_t * hexdata;
288285

289286
int k=0;
290-
int result;
287+
int result = 0;
291288
int len = strlen(cmdstr);
292289

293290
memtype = cmdstr;
@@ -332,13 +329,14 @@ void memop(char * cmdstr)
332329
len = 0;
333330
if (result < 0)
334331
{
335-
fprintf(stderr,"Error in memory write\n");
336-
return;
332+
fprintf(stderr,"Memory write failed\n");
333+
break;
337334
}
338335
}
339336
curhexline = curhexline->next;
340337
}
341-
if (verbose>=0) printf("Memory write finished without error\n");
338+
if (result == 0)
339+
if (verbose>=0) printf("Memory write finished without error\n");
342340
}
343341

344342
// Read data from target
@@ -357,7 +355,6 @@ int main(int argc, char * argv[])
357355
int k;
358356
int eraseflag = 0;
359357
int lastopt = 0;
360-
unsigned char data[16000];
361358

362359
for (k=0; k<argc;k++)
363360
{
@@ -371,11 +368,11 @@ int main(int argc, char * argv[])
371368
{
372369
if (argv[k][2])
373370
{
374-
port = argv[k]+2;
371+
portname = argv[k]+2;
375372
}
376373
else
377374
{
378-
port = argv[++k];
375+
portname = argv[++k];
379376
}
380377
lastopt = k;
381378
closeport();
@@ -410,7 +407,7 @@ int main(int argc, char * argv[])
410407
}
411408
}
412409

413-
printf("Using port %s with baudrate %i \n",port,baudrate);
410+
printf("Using port %s with baudrate %i \n",portname,baudrate);
414411
opentargetport();
415412
bootloader_connect();
416413
bootloader_get_commands();

stmdude/serialport.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@
55
#include <sys/signal.h>
66
#include <sys/types.h>
77

8+
#include "serialport.h"
9+
10+
/*
11+
http://notes.ump.edu.my/fkee/e-Books/C%20Programming%20&%20PC%20interfacing/Serial%20port%20programming%20for%20Windows%20and%20Linux.pdf
12+
13+
http://msdn.microsoft.com/en-us/library/ms810467.aspx#serial_topic6
14+
*/
15+
16+
serport_t openSerialPort(const char * portname, int portflags)
17+
{
18+
#ifndef WIN32
19+
return open(portname, portflags);
20+
#else
21+
HANDLE fileHandle = CreateFile(portname,
22+
GENERIC_READ | GENERIC_WRITE,
23+
0,
24+
0,
25+
OPEN_EXISTING,
26+
0,
27+
0);
28+
return fileHandle;
29+
#endif
30+
};
31+
32+
void closeSerialPort(serport_t port)
33+
{
34+
close(port);
35+
};
836

937
void setPortConfig(int portfd, int baud)
1038
{

stmdude/serialport.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef __SERIAL_PORT_H__
2+
#define __SERIAL_PORT_H__
3+
4+
#ifndef WIN32
5+
#define serport_t int
6+
#define SERIAL_NOT_OPEN (-1)
7+
#else
8+
#define serport_t HANDLE
9+
#define SERIAL_NOT_OPEN INVALID_HANDLE_VALUE
10+
#endif
11+
12+
extern serport_t openSerialPort(const char * portname, int portflags);
13+
extern void closeSerialPort(serport_t port);
14+
extern void setPortConfig(serport_t serport, int baud);
15+
16+
#endif

0 commit comments

Comments
 (0)
0