Skip to content

Commit a7244e0

Browse files
committed
Added upload-reset.c source. Added upload-reset OSX binary, and modified maple-upload script to use upload-reset
1 parent f69d71b commit a7244e0

2 files changed

Lines changed: 182 additions & 6 deletions

File tree

tools/macosx/maple_upload

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,30 @@ if [ $# -lt 4 ]; then
88
fi
99
dummy_port=$1; altID=$2; usbID=$3; binfile=$4;dummy_port_fullpath="/dev/$1"
1010

11-
#if we can find the Serial device try resetting it and then sleeping for 1 sec while the board reboots
1211

13-
if [ -e $dummy_port_fullpath ]; then
14-
echo "resetting " $dummy_port_fullpath
15-
stty -f $dummy_port_fullpath 1200
16-
sleep 1
12+
# Get the directory where the script is running.
13+
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
14+
15+
16+
# ----------------- Old code to reset the USB - which doesn't seem to work --------
17+
#
18+
#if we can find the Serial device try resetting it and then sleeping for 1 sec while the board reboots
19+
#if [ -e $dummy_port_fullpath ]; then
20+
# echo "resetting " $dummy_port_fullpath
1721
# stty -f $dummy_port_fullpath 1200
1822
# sleep 1
19-
fi
23+
## stty -f $dummy_port_fullpath 1200
24+
## sleep 1
25+
#fi
26+
# ------------------ End of old code -----------------
27+
28+
# ----------------- IMPORTANT -----------------
29+
# The 2nd parameter to upload-reset is the delay after resetting before it exits
30+
# This value is in milliseonds
31+
# You may need to tune this to your system
32+
# 750ms to 1500ms seems to work on my Mac
33+
34+
${DIR}/upload-reset ${dummy_port_fullpath} 750
2035

2136

2237
DFU_UTIL=/usr/local/bin/dfu-util
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/* Copyright (C) 2015 Roger Clark <www.rogerclark.net>
2+
*
3+
* This program is free software; you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation; either version 2 of the License, or
6+
* (at your option) any later version.
7+
*
8+
*
9+
* Utility to send the reset sequence on RTS and DTR and chars
10+
* which resets the libmaple and causes the bootloader to be run
11+
*
12+
*
13+
*
14+
* Terminal control code by Heiko Noordhof (see copyright below)
15+
*/
16+
17+
18+
19+
/* Copyright (C) 2003 Heiko Noordhof <heikyAusers.sf.net>
20+
*
21+
* This program is free software; you can redistribute it and/or modify
22+
* it under the terms of the GNU General Public License as published by
23+
* the Free Software Foundation; either version 2 of the License, or
24+
* (at your option) any later version.
25+
*/
26+
27+
#include <stdio.h>
28+
#include <stdlib.h>
29+
#include <termios.h>
30+
#include <unistd.h>
31+
#include <sys/types.h>
32+
#include <sys/stat.h>
33+
#include <fcntl.h>
34+
#include <sys/ioctl.h>
35+
#include <stdbool.h>
36+
37+
/* Function prototypes (belong in a seperate header file) */
38+
int openserial(char *devicename);
39+
void closeserial(void);
40+
int setDTR(unsigned short level);
41+
int setRTS(unsigned short level);
42+
43+
44+
/* Two globals for use by this module only */
45+
static int fd;
46+
static struct termios oldterminfo;
47+
48+
49+
void closeserial(void)
50+
{
51+
tcsetattr(fd, TCSANOW, &oldterminfo);
52+
close(fd);
53+
}
54+
55+
56+
int openserial(char *devicename)
57+
{
58+
struct termios attr;
59+
60+
if ((fd = open(devicename, O_RDWR)) == -1) return 0; /* Error */
61+
atexit(closeserial);
62+
63+
if (tcgetattr(fd, &oldterminfo) == -1) return 0; /* Error */
64+
attr = oldterminfo;
65+
attr.c_cflag |= CRTSCTS | CLOCAL;
66+
attr.c_oflag = 0;
67+
if (tcflush(fd, TCIOFLUSH) == -1) return 0; /* Error */
68+
if (tcsetattr(fd, TCSANOW, &attr) == -1) return 0; /* Error */
69+
70+
/* Set the lines to a known state, and */
71+
/* finally return non-zero is successful. */
72+
return setRTS(0) && setDTR(0);
73+
}
74+
75+
76+
/* For the two functions below:
77+
* level=0 to set line to LOW
78+
* level=1 to set line to HIGH
79+
*/
80+
81+
int setRTS(unsigned short level)
82+
{
83+
int status;
84+
85+
if (ioctl(fd, TIOCMGET, &status) == -1) {
86+
perror("setRTS(): TIOCMGET");
87+
return 0;
88+
}
89+
if (level) status |= TIOCM_RTS;
90+
else status &= ~TIOCM_RTS;
91+
if (ioctl(fd, TIOCMSET, &status) == -1) {
92+
perror("setRTS(): TIOCMSET");
93+
return 0;
94+
}
95+
return 1;
96+
}
97+
98+
99+
int setDTR(unsigned short level)
100+
{
101+
int status;
102+
103+
if (ioctl(fd, TIOCMGET, &status) == -1) {
104+
perror("setDTR(): TIOCMGET");
105+
return 0;
106+
}
107+
if (level) status |= TIOCM_DTR;
108+
else status &= ~TIOCM_DTR;
109+
if (ioctl(fd, TIOCMSET, &status) == -1) {
110+
perror("setDTR: TIOCMSET");
111+
return 0;
112+
}
113+
return 1;
114+
}
115+
116+
/* 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
119+
*/
120+
121+
main(int argc, char *argv[])
122+
{
123+
124+
if (argc<2 || argc >3)
125+
{
126+
printf("Usage upload-reset <serial_device> <Optional_delay_in_milliseconds>\n\r");
127+
return;
128+
}
129+
130+
if (openserial(argv[1]))
131+
{
132+
// Send magic sequence of DTR and RTS followed by the magic word "1EAF"
133+
setRTS(false);
134+
setDTR(false);
135+
setDTR(true);
136+
137+
usleep(50000L);
138+
139+
setDTR(false);
140+
setRTS(true);
141+
setDTR(true);
142+
143+
usleep(50000L);
144+
145+
setDTR(false);
146+
147+
usleep(50000L);
148+
149+
write(fd,"1EAF",4);
150+
151+
closeserial();
152+
if (argc==3)
153+
{
154+
usleep(atol(argv[2])*1000L);
155+
}
156+
}
157+
else
158+
{
159+
printf("Failed to open serial device.\n\r");
160+
}
161+
}

0 commit comments

Comments
 (0)