Skip to content

Commit df51196

Browse files
author
Cruz Monrreal
authored
Merge pull request #6607 from maximmbed/max32630-fix-spi
Improve SPI block write
2 parents 4522405 + b08c752 commit df51196

File tree

1 file changed

+49
-10
lines changed
  • targets/TARGET_Maxim/TARGET_MAX32630

1 file changed

+49
-10
lines changed

targets/TARGET_Maxim/TARGET_MAX32630/spi_api.c

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*/
3333

3434
#include "mbed_assert.h"
35+
#include "mbed_critical.h"
3536
#include "spi_api.h" // mbed HAL
3637
#include "spim_regs.h" // bare metal
3738
#include "spim.h" // Maxim CMSIS driver
@@ -167,19 +168,58 @@ int spi_master_write(spi_t *obj, int value)
167168
return *req.rx_data;
168169
}
169170

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;
171+
//******************************************************************************
172+
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, char write_fill)
173+
{
174+
spim_req_t req;
175+
176+
if (!(tx_length | rx_length) ||
177+
(tx_length < 0) ||
178+
(rx_length < 0)) {
179+
return 0;
180+
}
181+
182+
req.width = SPIM_WIDTH_1;
183+
req.ssel = 0;
184+
req.deass = 1;
185+
req.callback = NULL;
173186

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;
187+
core_util_critical_section_enter();
188+
if (tx_length == rx_length) {
189+
req.tx_data = (uint8_t *)tx_buffer;
190+
req.rx_data = (uint8_t *)rx_buffer;
191+
req.len = tx_length;
192+
SPIM_Trans(obj->spi, &req);
193+
} else if (tx_length < rx_length) {
194+
req.tx_data = (tx_length > 0) ? (uint8_t *)tx_buffer : NULL;
195+
req.rx_data = (uint8_t *)rx_buffer;
196+
req.len = (tx_length > 0) ? tx_length : rx_length;
197+
SPIM_Trans(obj->spi, &req);
198+
199+
if (tx_length) {
200+
req.tx_data = NULL;
201+
req.rx_data = (uint8_t *)(rx_buffer + tx_length);
202+
req.len = rx_length - tx_length;
203+
SPIM_Trans(obj->spi, &req);
204+
}
205+
} else {
206+
req.tx_data = (uint8_t *)tx_buffer;
207+
req.rx_data = (rx_length > 0) ? (uint8_t *)rx_buffer : NULL;
208+
req.len = (rx_length > 0) ? rx_length : tx_length;
209+
SPIM_Trans(obj->spi, &req);
210+
211+
if (rx_length) {
212+
req.tx_data = (uint8_t *)(tx_buffer + rx_length);
213+
req.rx_data = NULL;
214+
req.len = tx_length - rx_length;
215+
SPIM_Trans(obj->spi, &req);
179216
}
180217
}
218+
core_util_critical_section_exit();
219+
220+
while (SPIM_Busy(obj->spi));
181221

182-
return total;
222+
return tx_length > rx_length ? tx_length : rx_length;
183223
}
184224

185225
//******************************************************************************
@@ -193,4 +233,3 @@ uint8_t spi_get_module(spi_t *obj)
193233
{
194234
return obj->index;
195235
}
196-

0 commit comments

Comments
 (0)