Skip to content
This repository was archived by the owner on Oct 5, 2021. It is now read-only.

Commit efeab97

Browse files
romansavrulintormodvolden
authored andcommitted
Allow extra delay or no delay at all in GPIO sequences
There was already a 100 ms delay between each GPIO activation. By specifiying an empty GPIO (just the comma) extra delays can now be introduced. By using '&' instead of comma between GPIOs, no delay at all will take place between the GPIO activations. [Tormod: rework documentation] Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
1 parent d202bd0 commit efeab97

File tree

3 files changed

+95
-23
lines changed

3 files changed

+95
-23
lines changed

init.c

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,17 @@ static int gpio_sequence(struct port_interface *port, const char *s, size_t l)
167167
struct gpio_list *to_free;
168168
#endif
169169
int ret, level, gpio;
170+
int sleep_time = 0;
171+
int delimiter = 0;
172+
const char *sig_str = NULL;
170173

174+
fprintf(stdout, "\nGPIO sequence start\n");
171175
ret = 1;
172176
while (ret == 1 && *s && l > 0) {
177+
sig_str = NULL;
178+
sleep_time = 0;
179+
delimiter = 0;
180+
173181
if (*s == '-') {
174182
level = 0;
175183
s++;
@@ -184,38 +192,64 @@ static int gpio_sequence(struct port_interface *port, const char *s, size_t l)
184192
l--;
185193
}
186194
} else if (!strncmp(s, "rts", 3)) {
195+
sig_str = s;
187196
gpio = -GPIO_RTS;
188197
s += 3;
189198
l -= 3;
190199
} else if (!strncmp(s, "dtr", 3)) {
200+
sig_str = s;
191201
gpio = -GPIO_DTR;
192202
s += 3;
193203
l -= 3;
194204
} else if (!strncmp(s, "brk", 3)) {
205+
sig_str = s;
195206
gpio = -GPIO_BRK;
196207
s += 3;
197208
l -= 3;
198-
} else {
199-
fprintf(stderr, "Character \'%c\' is not a digit\n", *s);
200-
ret = 0;
201-
break;
202-
}
203-
204-
if (*s && (l > 0)) {
209+
} else if (*s && (l > 0)) {
210+
delimiter = 1;
211+
/* The ',' delimiter adds a 100 ms delay between signal toggles.
212+
* i.e -rts,dtr will reset rts, wait 100 ms, set dtr.
213+
*
214+
* The '&' delimiter adds no delay between signal toggles.
215+
* i.e -rts&dtr will reset rts and immediately set dtr.
216+
*
217+
* Example: -rts&dtr,,,rts,-dtr will reset rts and set dtr
218+
* without delay, then wait 300 ms, set rts, wait 100 ms, reset dtr.
219+
*/
205220
if (*s == ',') {
206221
s++;
207222
l--;
223+
sleep_time = 100000;
224+
} else if (*s == '&') {
225+
s++;
226+
l--;
208227
} else {
209228
fprintf(stderr, "Character \'%c\' is not a separator\n", *s);
210229
ret = 0;
211230
break;
212231
}
232+
} else {
233+
fprintf(stderr, "Character \'%c\' is not a digit\n", *s);
234+
ret = 0;
235+
break;
236+
}
237+
238+
if (!delimiter) { /* actual gpio/port signal driving */
239+
if (gpio < 0) {
240+
gpio = -gpio;
241+
fprintf(stdout, " setting port signal %.3s to %i\n", sig_str, level);
242+
ret = (port->gpio(port, gpio, level) == PORT_ERR_OK);
243+
} else {
244+
fprintf(stdout, " setting gpio %i to %i\n", gpio, level);
245+
ret = drive_gpio(gpio, level, &gpio_to_release);
246+
}
247+
}
248+
249+
if (sleep_time) {
250+
fprintf(stdout, " delay %i us\n", sleep_time);
251+
usleep(sleep_time);
213252
}
214-
if (gpio < 0)
215-
ret = (port->gpio(port, -gpio, level) == PORT_ERR_OK);
216-
else
217-
ret = drive_gpio(gpio, level, &gpio_to_release);
218-
usleep(100000);
219253
}
220254
#if defined(__linux__)
221255
while (gpio_to_release) {
@@ -225,7 +259,7 @@ static int gpio_sequence(struct port_interface *port, const char *s, size_t l)
225259
free(to_free);
226260
}
227261
#endif
228-
usleep(500000);
262+
fprintf(stdout, "GPIO sequence end\n\n");
229263
return ret;
230264
}
231265

main.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -822,10 +822,20 @@ void show_help(char *name) {
822822
" -c Resume the connection (don't send initial INIT)\n"
823823
" *Baud rate must be kept the same as the first init*\n"
824824
" This is useful if the reset fails\n"
825+
" -R Reset device at exit.\n"
825826
" -i GPIO_string GPIO sequence to enter/exit bootloader mode\n"
826827
" GPIO_string=[entry_seq][:[exit_seq]]\n"
827-
" sequence=[-]n[,sequence]\n"
828-
" -R Reset device at exit.\n"
828+
" sequence=[[-]signal]&|,[sequence]\n"
829+
"\n"
830+
"GPIO sequence:\n"
831+
" The following signals can appear in a sequence:\n"
832+
" Integer number representing GPIO pin\n"
833+
" 'dtr', 'rts' or 'brk' representing serial port signal\n"
834+
" The sequence can use the following delimiters:\n"
835+
" ',' adds 100 ms delay between signals\n"
836+
" '&' adds no delay between signals\n"
837+
" The following modifiers can be prepended to a signal:\n"
838+
" '-' reset signal (low) instead of setting it (high)\n"
829839
"\n"
830840
"Examples:\n"
831841
" Get device information:\n"
@@ -846,9 +856,14 @@ void show_help(char *name) {
846856
" %s -g 0x0 /dev/ttyS0\n"
847857
"\n"
848858
" GPIO sequence:\n"
849-
" - entry sequence: GPIO_3=low, GPIO_2=low, GPIO_2=high\n"
850-
" - exit sequence: GPIO_3=high, GPIO_2=low, GPIO_2=high\n"
851-
" %s -R -i -3,-2,2:3,-2,2 /dev/ttyS0\n",
859+
" - entry sequence: GPIO_3=low, GPIO_2=low, 100ms delay, GPIO_2=high\n"
860+
" - exit sequence: GPIO_3=high, GPIO_2=low, 300ms delay, GPIO_2=high\n"
861+
" %s -i '-3&-2,2:3&-2,,,2' /dev/ttyS0\n"
862+
" GPIO sequence adding delay after port opening:\n"
863+
" - entry sequence: delay 500ms\n"
864+
" - exit sequence: rts=high, dtr=low, 300ms delay, GPIO_2=high\n"
865+
" %s -R -i ',,,,,:rts&-dtr,,,2' /dev/ttyS0\n",
866+
name,
852867
name,
853868
name,
854869
name,

stm32flash.1

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ The format of
226226
.RS
227227
GPIO_string = [entry sequence][:[exit sequence]]
228228
.P
229-
sequence = [\-]n[,sequence]
229+
sequence = [[\-]signal]&|,[sequence]
230230
.RE
231231
.PD
232232
.P
@@ -239,7 +239,15 @@ The string "brk" forces the UART to send a BREAK sequence on TX line;
239239
after BREAK the UART is returned in normal "non\-break" mode.
240240
Note: the string "\-brk" has no effect and is ignored.
241241
.PD
242-
242+
.P
243+
The ',' delimiter adds 100 ms of delay between signal toggles, whereas
244+
the '&' delimiter adds no delay.
245+
An empty signal, thus repeated ',' delimiters, can be used to insert larger
246+
delays in multiples of 100 ms.
247+
E.g. "rts,,,,\-dtr" will set RTS, then wait 400 ms, then reset DTR.
248+
"rts&\-dtr" will set RTS and reset DTR without delay. You can use ',' delimiters
249+
alone to simply add a delay between opening port and starting to flash.
250+
.DP
243251
.P
244252
Note that since version 0.6, an exit sequence will always be executed if
245253
specified, regardless of the -R option, to ensure the signals are reset.
@@ -280,15 +288,30 @@ As example, supposing GPIO_3 connected to PA5 and GPIO_2 to STM32W's reset.
280288
The command:
281289
.PD 0
282290
.RS
283-
stm32flash \-i \-3,\-2,2:3,\-2,2 /dev/ttyS0
291+
stm32flash \-i '\-3&\-2,2:3&\-2,,,2' /dev/ttyS0
284292
.RE
285293
provides:
286294
.IP \(bu 2
287-
entry sequence: GPIO_3=low, GPIO_2=low, GPIO_2=high
295+
entry sequence: GPIO_3=low, GPIO_2=low, 100 ms delay, GPIO_2=high
288296
.IP \(bu 2
289-
exit sequence: GPIO_3=high, GPIO_2=low, GPIO_2=high
297+
exit sequence: GPIO_3=high, GPIO_2=low, 300 ms delay, GPIO_2=high
290298
.PD
291299

300+
301+
GPIO sequence to bring delays on start after port opening.
302+
The command:
303+
.PD 0
304+
.RS
305+
stm32flash \-i ',,,,,:rts&\-dtr,,,2' /dev/ttyS0\n",
306+
.RE
307+
provides:
308+
.IP \(bu 2
309+
entry sequence: delay 500 ms
310+
.IP \(bu 2
311+
exit sequence: RTS=high, DTR=low, 300 ms delay, GPIO_2=high
312+
.PD
313+
314+
292315
.SH EXAMPLES
293316
Get device information:
294317
.RS

0 commit comments

Comments
 (0)