Skip to content

Commit ab00b46

Browse files
Keyur Hariyacmonr
authored andcommitted
Improve SPI block write
Replace looping construct with actual block writes.
1 parent 761dcba commit ab00b46

File tree

1 file changed

+49
-11
lines changed
  • targets/TARGET_Maxim/TARGET_MAX32625

1 file changed

+49
-11
lines changed

targets/TARGET_Maxim/TARGET_MAX32625/spi_api.c

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,58 @@ int spi_master_write(spi_t *obj, int value)
167167
return *req.rx_data;
168168
}
169169

170-
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
171-
char *rx_buffer, int rx_length, char write_fill) {
172-
int total = (tx_length > rx_length) ? tx_length : rx_length;
173-
174-
for (int i = 0; i < total; i++) {
175-
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
176-
char in = spi_master_write(obj, out);
177-
if (i < rx_length) {
178-
rx_buffer[i] = in;
170+
//******************************************************************************
171+
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, char write_fill)
172+
{
173+
spim_req_t req;
174+
175+
if (!(tx_length | rx_length) ||
176+
(tx_length < 0) ||
177+
(rx_length < 0)) {
178+
return 0;
179+
}
180+
181+
req.width = SPIM_WIDTH_1;
182+
req.ssel = 0;
183+
req.deass = 1;
184+
req.callback = NULL;
185+
186+
__disable_irq();
187+
if (tx_length == rx_length) {
188+
req.tx_data = (uint8_t *)tx_buffer;
189+
req.rx_data = (uint8_t *)rx_buffer;
190+
req.len = tx_length;
191+
SPIM_Trans(obj->spi, &req);
192+
} else if (tx_length < rx_length) {
193+
req.tx_data = (tx_length > 0) ? (uint8_t *)tx_buffer : NULL;
194+
req.rx_data = (uint8_t *)rx_buffer;
195+
req.len = (tx_length > 0) ? tx_length : rx_length;
196+
SPIM_Trans(obj->spi, &req);
197+
198+
if (tx_length) {
199+
req.tx_data = NULL;
200+
req.rx_data = (uint8_t *)(rx_buffer + tx_length);
201+
req.len = rx_length - tx_length;
202+
SPIM_Trans(obj->spi, &req);
203+
}
204+
} else {
205+
req.tx_data = (uint8_t *)tx_buffer;
206+
req.rx_data = (rx_length > 0) ? (uint8_t *)rx_buffer : NULL;
207+
req.len = (rx_length > 0) ? rx_length : tx_length;
208+
SPIM_Trans(obj->spi, &req);
209+
210+
if (rx_length) {
211+
req.tx_data = (uint8_t *)(tx_buffer + rx_length);
212+
req.rx_data = NULL;
213+
req.len = tx_length - rx_length;
214+
SPIM_Trans(obj->spi, &req);
179215
}
180216
}
217+
__enable_irq();
218+
219+
while (SPIM_Busy(obj->spi));
181220

182-
return total;
221+
return tx_length > rx_length ? tx_length : rx_length;
183222
}
184223

185224
//******************************************************************************
@@ -193,4 +232,3 @@ uint8_t spi_get_module(spi_t *obj)
193232
{
194233
return obj->index;
195234
}
196-

0 commit comments

Comments
 (0)