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

Commit 900f44f

Browse files
committed
Add command line flag -F for frame length
Some I2C interface have limitation on the maximum length of frame, either in RX and/or in TX. While this could be automatically detected (how ?) let's play easy and pass it from command line. There is no use of these values yet. Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
1 parent 9f2c11a commit 900f44f

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

main.c

+38-3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ struct port_options port_opts = {
5252
.baudRate = SERIAL_BAUD_57600,
5353
.serial_mode = "8e1",
5454
.bus_addr = 0,
55+
.rx_frame_max = STM32_MAX_RX_FRAME,
56+
.tx_frame_max = STM32_MAX_TX_FRAME,
5557
};
5658
int rd = 0;
5759
int wr = 0;
@@ -479,9 +481,12 @@ int main(int argc, char* argv[]) {
479481
return ret;
480482
}
481483

482-
int parse_options(int argc, char *argv[]) {
484+
int parse_options(int argc, char *argv[])
485+
{
483486
int c;
484-
while ((c = getopt(argc, argv, "a:b:m:r:w:e:vn:g:jkfcChuos:S:i:R")) != -1) {
487+
char *pLen;
488+
489+
while ((c = getopt(argc, argv, "a:b:m:r:w:e:vn:g:jkfcChuos:S:F:i:R")) != -1) {
485490
switch(c) {
486491
case 'a':
487492
port_opts.bus_addr = strtoul(optarg, NULL, 0);
@@ -595,7 +600,6 @@ int parse_options(int argc, char *argv[]) {
595600
fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n");
596601
return 1;
597602
} else {
598-
char *pLen;
599603
start_addr = strtoul(optarg, &pLen, 0);
600604
if (*pLen == ':') {
601605
pLen++;
@@ -607,6 +611,36 @@ int parse_options(int argc, char *argv[]) {
607611
}
608612
}
609613
break;
614+
case 'F':
615+
port_opts.rx_frame_max = strtoul(optarg, &pLen, 0);
616+
if (*pLen == ':') {
617+
pLen++;
618+
port_opts.tx_frame_max = strtoul(pLen, NULL, 0);
619+
}
620+
if (port_opts.rx_frame_max < 0
621+
|| port_opts.tx_frame_max < 0) {
622+
fprintf(stderr, "ERROR: Invalid negative value for option -F\n");
623+
return 1;
624+
}
625+
if (port_opts.rx_frame_max == 0)
626+
port_opts.rx_frame_max = STM32_MAX_RX_FRAME;
627+
if (port_opts.tx_frame_max == 0)
628+
port_opts.tx_frame_max = STM32_MAX_TX_FRAME;
629+
if (port_opts.rx_frame_max < 20
630+
|| port_opts.tx_frame_max < 5) {
631+
fprintf(stderr, "ERROR: current code cannot work with small frames.\n");
632+
fprintf(stderr, "min(RX) = 20, min(TX) = 5\n");
633+
return 1;
634+
}
635+
if (port_opts.rx_frame_max > STM32_MAX_RX_FRAME) {
636+
fprintf(stderr, "WARNING: Ignore RX length in option -F\n");
637+
port_opts.rx_frame_max = STM32_MAX_RX_FRAME;
638+
}
639+
if (port_opts.tx_frame_max > STM32_MAX_TX_FRAME) {
640+
fprintf(stderr, "WARNING: Ignore TX length in option -F\n");
641+
port_opts.tx_frame_max = STM32_MAX_TX_FRAME;
642+
}
643+
break;
610644
case 'f':
611645
force_binary = 1;
612646
break;
@@ -676,6 +710,7 @@ void show_help(char *name) {
676710
" -g address Start execution at specified address (0 = flash start)\n"
677711
" -S address[:length] Specify start address and optionally length for\n"
678712
" read/write/erase operations\n"
713+
" -F RX_length[:TX_length] Specify the max length of RX and TX frame\n"
679714
" -s start_page Flash at specified page (0 = flash start)\n"
680715
" -f Force binary parser\n"
681716
" -h Show this help\n"

port.h

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ struct port_options {
4141
serial_baud_t baudRate;
4242
const char *serial_mode;
4343
int bus_addr;
44+
int rx_frame_max;
45+
int tx_frame_max;
4446
};
4547

4648
/*

stm32.h

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include <stdint.h>
2525
#include "serial.h"
2626

27+
#define STM32_MAX_RX_FRAME 256 /* cmd read memory */
28+
#define STM32_MAX_TX_FRAME (1 + 256 + 1) /* cmd write memory */
29+
2730
typedef enum {
2831
STM32_ERR_OK = 0,
2932
STM32_ERR_UNKNOWN, /* Generic error */

stm32flash.1

+11-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ stm32flash \- flashing utility for STM32 and STM32W through UART or I2C
2323
.RB [ \-s
2424
.IR start_page ]
2525
.RB [ \-S
26-
.IR address [: length ]
26+
.IR address [: length ]]
27+
.RB [ \-F
28+
.IR RX_length [: TX_length ]]
2729
.RB [ \-i
2830
.IR GPIO_string ]
2931
.RI [ tty_device
@@ -140,6 +142,14 @@ Specify flash page offset (0 = flash start).
140142
.BI "\-S" " address" "[:" "length" "]"
141143
Specify start address and optionally length for read/write/erase/crc operations.
142144

145+
.TP
146+
.BI "\-F" " RX_length" "[:" "TX_length" "]"
147+
Specify the maximum frame size for the current interface.
148+
Due to STM32 bootloader protocol, host will never handle frames bigger than
149+
256 byte in RX or 258 byte in TX.
150+
Due to current code, lowest limit in RX is 20 byte (to read a complete reply
151+
of command GET). Minimum limit in TX is 5 byte, required by protocol.
152+
143153
.TP
144154
.B \-f
145155
Force binary parser while reading file with

0 commit comments

Comments
 (0)