Skip to content

Commit f56074a

Browse files
committed
feat: create upload_reset binary for windows
to remove java dependecy on Windows fixes stm32duino#67 Signed-off-by: Frederic Pillon <[email protected]>
1 parent 49cd23e commit f56074a

File tree

6 files changed

+119
-11
lines changed

6 files changed

+119
-11
lines changed
File renamed without changes.
File renamed without changes.

src/upload-reset/upload-reset.c renamed to src/upload_reset/unix/upload_reset.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Copyright (C) 2015 Roger Clark <www.rogerclark.net>
2-
*
2+
*
33
* This program is free software; you can redistribute it and/or modify
44
* it under the terms of the GNU General Public License as published by
55
* the Free Software Foundation; either version 2 of the License, or
@@ -17,7 +17,7 @@
1717

1818

1919
/* Copyright (C) 2003 Heiko Noordhof <heikyAusers.sf.net>
20-
*
20+
*
2121
* This program is free software; you can redistribute it and/or modify
2222
* it under the terms of the GNU General Public License as published by
2323
* the Free Software Foundation; either version 2 of the License, or
@@ -27,7 +27,7 @@
2727
#include <stdio.h>
2828
#include <stdlib.h>
2929
#include <termios.h>
30-
#include <unistd.h>
30+
#include <unistd.h>
3131
#include <sys/types.h>
3232
#include <sys/stat.h>
3333
#include <fcntl.h>
@@ -38,7 +38,7 @@
3838
int openserial(char *devicename);
3939
void closeserial(void);
4040
int setDTR(unsigned short level);
41-
int setRTS(unsigned short level);
41+
int setRTS(unsigned short level);
4242

4343

4444
/* Two globals for use by this module only */
@@ -53,19 +53,19 @@ void closeserial(void)
5353
}
5454

5555

56-
int openserial(char *devicename)
56+
int openserial(char *devicename)
5757
{
5858
struct termios attr;
5959

60-
if ((fd = open(devicename, O_RDWR)) == -1) return 0; /* Error */
60+
if ((fd = open(devicename, O_RDWR)) == -1) return 0; /* Error */
6161
atexit(closeserial);
6262

6363
if (tcgetattr(fd, &oldterminfo) == -1) return 0; /* Error */
6464
attr = oldterminfo;
6565
attr.c_cflag |= CRTSCTS | CLOCAL;
6666
attr.c_oflag = 0;
6767
if (tcflush(fd, TCIOFLUSH) == -1) return 0; /* Error */
68-
if (tcsetattr(fd, TCSANOW, &attr) == -1) return 0; /* Error */
68+
if (tcsetattr(fd, TCSANOW, &attr) == -1) return 0; /* Error */
6969

7070
/* Set the lines to a known state, and */
7171
/* finally return non-zero is successful. */
@@ -114,13 +114,13 @@ int setDTR(unsigned short level)
114114
}
115115

116116
/* This portion of code was written by Roger Clark
117-
* It was informed by various other pieces of code written by Leaflabs to reset their
118-
* Maple and Maple mini boards
117+
* It was informed by various other pieces of code written by Leaflabs to reset their
118+
* Maple and Maple mini boards
119119
*/
120120

121121
main(int argc, char *argv[])
122122
{
123-
123+
124124
if (argc<2 || argc >3)
125125
{
126126
printf("Usage upload-reset <serial_device> <Optional_delay_in_milliseconds>\n\r");
@@ -147,7 +147,7 @@ main(int argc, char *argv[])
147147
usleep(50000L);
148148

149149
write(fd,"1EAF",4);
150-
150+
151151
closeserial();
152152
if (argc==3)
153153
{

src/upload_reset/win/Makefile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
CC = gcc
2+
SRC = $(wildcard *.c)
3+
LIBSRC = $(wildcard ../../libserialport/*.c)
4+
OBJ = $(SRC:.c=.o) $(LIBSRC:.c=.o)
5+
CFLAGS = -DLIBSERIALPORT_MSBUILD -Wall -I../../libserialport
6+
LDFLAGS = -lsetupapi
7+
DEST = ../../../win
8+
BIN = $(SRC:.c=)
9+
10+
.PHONY: all clean
11+
12+
all: $(BIN) install
13+
14+
$(BIN): $(OBJ)
15+
$(CC) -o $@ $^ $(LDFLAGS)
16+
17+
install: $(BIN)
18+
@echo "$@ $^..."
19+
@mv $(BIN) $(DEST)/
20+
21+
clean:
22+
rm -f $(OBJ) $(DEST)/$(BIN)
23+

src/upload_reset/win/upload_reset.c

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <libserialport.h>
2+
#include <stdio.h>
3+
#include <windows.h>
4+
/*
5+
* Utility to send the reset sequence on RTS and DTR and chars
6+
* which resets the libmaple and causes the bootloader to be run
7+
*/
8+
9+
/* Helper function for error handling. */
10+
int check(enum sp_return result)
11+
{
12+
char *error_message;
13+
14+
switch (result) {
15+
case SP_ERR_ARG:
16+
printf("Error: Invalid argument.\n");
17+
exit(SP_ERR_ARG);
18+
case SP_ERR_FAIL:
19+
error_message = sp_last_error_message();
20+
printf("Error: Failed: %s\n", error_message);
21+
sp_free_error_message(error_message);
22+
exit(SP_ERR_FAIL);
23+
case SP_ERR_SUPP:
24+
printf("Error: Not supported.\n");
25+
exit(SP_ERR_SUPP);
26+
case SP_ERR_MEM:
27+
printf("Error: Couldn't allocate memory.\n");
28+
exit(SP_ERR_MEM);
29+
case SP_OK:
30+
default:
31+
return result;
32+
}
33+
}
34+
35+
int main(int argc, char **argv)
36+
{
37+
if (argc<2 || argc >3)
38+
{
39+
printf("Usage %s <serial port> <Optional delay in milliseconds>\n", argv[0]);
40+
return -1;
41+
}
42+
43+
char *port_name = argv[1];
44+
/* A pointer to a struct sp_port, which will refer to
45+
* the port found. */
46+
struct sp_port *port;
47+
printf("Looking for port %s.\n", port_name);
48+
/* Call sp_get_port_by_name() to find the port. The port
49+
* pointer will be updated to refer to the port found. */
50+
check(sp_get_port_by_name(port_name, &port));
51+
52+
/* Display some basic information about the port. */
53+
printf("Port name: %s\n", sp_get_port_name(port));
54+
printf("Description: %s\n", sp_get_port_description(port));
55+
56+
/* The port must be open to access its configuration. */
57+
printf("Opening port.\n");
58+
check(sp_open(port, SP_MODE_READ_WRITE));
59+
check(sp_set_flowcontrol(port, SP_FLOWCONTROL_RTSCTS));
60+
check(sp_flush(port, SP_BUF_BOTH));
61+
// check(sp_set_rts(port, SP_RTS_OFF));
62+
// check(sp_set_dtr(port, SP_DTR_OFF));
63+
64+
// Send magic sequence of DTR and RTS followed by the magic word "1EAF"
65+
check(sp_set_rts(port, SP_RTS_OFF));
66+
check(sp_set_dtr(port, SP_DTR_OFF));
67+
check(sp_set_dtr(port, SP_DTR_ON));
68+
Sleep(50);
69+
check(sp_set_dtr(port, SP_DTR_OFF));
70+
check(sp_set_rts(port, SP_RTS_ON));
71+
check(sp_set_dtr(port, SP_DTR_ON));
72+
Sleep(50);
73+
check(sp_set_dtr(port, SP_DTR_OFF));
74+
Sleep(50);
75+
check(sp_blocking_write(port, "1EAF", 4, 1000));
76+
77+
/* Now clean up by closing the port and freeing structures. */
78+
check(sp_close(port));
79+
sp_free_port(port);
80+
if (argc==3)
81+
{
82+
Sleep(atol(argv[2]));
83+
}
84+
return 0;
85+
}

win/upload_reset.exe

223 KB
Binary file not shown.

0 commit comments

Comments
 (0)