Skip to content

Commit 21993f9

Browse files
committed
apps/spitool: Support multiple trans
By adding a parameter -r, spitool can exchange multiple transactions. example: spi exch -b 1 -t 0 -n 0 -f 3000000 -m 1 -w 32 -x 1 -r 2 f001fff1 0000ff35 Signed-off-by: yangsong8 <[email protected]>
1 parent a738aa5 commit 21993f9

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
lines changed

system/spi/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,11 @@ config SPITOOL_DEFWORDS
7878
---help---
7979
Number of words to be transferred (default 1)
8080

81+
config SPITOOL_DEFTRANS
82+
int "Number of transfer"
83+
default 1
84+
range 1 255
85+
---help---
86+
Number of transfer to be transferred (default 1)
87+
8188
endif # SYSTEM_SPITOOL

system/spi/spi_common.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,12 @@ int spitool_common_args(FAR struct spitool_s *spitool, FAR char **arg)
174174

175175
case 'r':
176176
ret = arg_decimal(arg, &value);
177-
if (value < 0)
177+
if (value < 0 || value > UINT8_MAX)
178178
{
179179
goto out_of_range;
180180
}
181181

182-
spitool->count = (uint32_t)value;
182+
spitool->trans_count = (uint8_t)value;
183183
return ret;
184184

185185
case 'u':

system/spi/spi_exch.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int spicmd_exch(FAR struct spitool_s *spitool, int argc, FAR char **argv)
6464
};
6565

6666
uint8_t *txdatap = txdata;
67-
struct spi_trans_s trans;
67+
FAR struct spi_trans_s *trans;
6868
struct spi_sequence_s seq;
6969
uint32_t d;
7070

@@ -93,7 +93,7 @@ int spicmd_exch(FAR struct spitool_s *spitool, int argc, FAR char **argv)
9393

9494
/* There may be transmit data on the command line */
9595

96-
if (argc - argndx > spitool->count)
96+
if (argc - argndx > spitool->count * spitool->trans_count)
9797
{
9898
spitool_printf(spitool, g_spitoomanyargs, argv[0]);
9999
return ERROR;
@@ -120,7 +120,7 @@ int spicmd_exch(FAR struct spitool_s *spitool, int argc, FAR char **argv)
120120
}
121121

122122
spitool_printf(spitool, "Sending:\t");
123-
for (d = 0; d < spitool->count; d++)
123+
for (d = 0; d < spitool->count * spitool->trans_count; d++)
124124
{
125125
if (spitool->width <= 8)
126126
{
@@ -138,12 +138,20 @@ int spicmd_exch(FAR struct spitool_s *spitool, int argc, FAR char **argv)
138138

139139
spitool_printf(spitool, "\n");
140140

141+
trans = malloc(sizeof(struct spi_trans_s) * spitool->trans_count);
142+
if (!trans)
143+
{
144+
spitool_printf(spitool, "Failed to allocate trans memory\n");
145+
return ERROR;
146+
}
147+
141148
/* Get a handle to the SPI bus */
142149

143150
fd = spidev_open(spitool->bus);
144151
if (fd < 0)
145152
{
146153
spitool_printf(spitool, "Failed to get bus %d\n", spitool->bus);
154+
free(trans);
147155
return ERROR;
148156
}
149157

@@ -153,8 +161,8 @@ int spicmd_exch(FAR struct spitool_s *spitool, int argc, FAR char **argv)
153161
seq.mode = spitool->mode;
154162
seq.nbits = spitool->width;
155163
seq.frequency = spitool->freq;
156-
seq.ntrans = 1;
157-
seq.trans = &trans;
164+
seq.ntrans = spitool->trans_count;
165+
seq.trans = trans;
158166

159167
#ifdef CONFIG_SPI_DELAY_CONTROL
160168
seq.a = 0;
@@ -163,29 +171,33 @@ int spicmd_exch(FAR struct spitool_s *spitool, int argc, FAR char **argv)
163171
seq.c = 0;
164172
#endif
165173

166-
trans.deselect = true;
174+
for (d = 0; d < spitool->trans_count; d++)
175+
{
176+
trans[d].deselect = true;
167177
#ifdef CONFIG_SPI_CMDDATA
168-
trans.cmd = spitool->command;
178+
trans[d].cmd = spitool->command;
169179
#endif
170-
trans.delay = spitool->udelay;
171-
trans.nwords = spitool->count;
172-
trans.txbuffer = txdata;
173-
trans.rxbuffer = rxdata;
180+
trans[d].delay = spitool->udelay;
181+
trans[d].nwords = spitool->count;
182+
trans[d].txbuffer = &txdata[d * spitool->count * seq.nbits / 8];
183+
trans[d].rxbuffer = &rxdata[d * spitool->count * seq.nbits / 8];
174184
#ifdef CONFIG_SPI_HWFEATURES
175-
trans.hwfeat = 0;
185+
trans[d].hwfeat = 0;
176186
#endif
187+
}
177188

178189
ret = spidev_transfer(fd, &seq);
179190

180191
close(fd);
192+
free(trans);
181193

182194
if (ret)
183195
{
184196
return ret;
185197
}
186198

187199
spitool_printf(spitool, "Received:\t");
188-
for (d = 0; d < spitool->count; d++)
200+
for (d = 0; d < spitool->count * spitool->trans_count; d++)
189201
{
190202
if (spitool->width <= 8)
191203
{

system/spi/spi_main.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,18 @@ static int spicmd_help(FAR struct spitool_s *spitool, int argc,
152152
"Default: %d Current: %" PRIu32 " Max: %d\n",
153153
CONFIG_SPITOOL_DEFWORDS, spitool->count, MAX_XDATA);
154154

155+
spitool_printf(spitool, " [-r trans_count] Trans to exchange "
156+
"Default: %d Current: %d Max: %d\n",
157+
CONFIG_SPITOOL_DEFTRANS, spitool->trans_count, UINT8_MAX);
158+
155159
spitool_printf(spitool, "\nNOTES:\n");
156160
#ifndef CONFIG_DISABLE_ENVIRON
157161
spitool_printf(spitool, "o An environment variable like $PATH may be used "
158162
"for any argument.\n");
159163
#endif
160164
spitool_printf(spitool, "o Arguments are \"sticky\". "
161165
"For example, once the SPI address is\n");
162-
spitool_printf(spitool, " specified, that address will be re-used "
166+
spitool_printf(spitool, " specified, that address will be reused "
163167
"until it is changed.\n");
164168
spitool_printf(spitool, "\nWARNING:\n");
165169
spitool_printf(spitool, "o The SPI commands may have bad side effects "
@@ -400,6 +404,11 @@ int main(int argc, FAR char *argv[])
400404
g_spitool.devtype = SPIDEVTYPE_USER;
401405
}
402406

407+
if (g_spitool.trans_count == 0)
408+
{
409+
g_spitool.trans_count = CONFIG_SPITOOL_DEFTRANS;
410+
}
411+
403412
/* Parse and process the command line */
404413

405414
spi_setup(&g_spitool);

system/spi/spitool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ struct spitool_s
140140
bool command; /* [-c 0|1] Send as command or data? */
141141
useconds_t udelay; /* [-u udelay] Delay in uS after transfer */
142142
uint8_t mode; /* [-m mode] Mode to use for transfer */
143+
uint8_t trans_count; /* [-r trans count] No of trans to exchange */
143144

144145
/* Output streams */
145146

0 commit comments

Comments
 (0)