Skip to content

Commit a22a676

Browse files
committed
stmhal/spi: Simplify spi_transfer function to take only one buf len arg.
1 parent 5bb28c7 commit a22a676

1 file changed

Lines changed: 16 additions & 17 deletions

File tree

stmhal/spi.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -328,33 +328,33 @@ STATIC HAL_StatusTypeDef spi_wait_dma_finished(SPI_HandleTypeDef *spi, uint32_t
328328
return HAL_OK;
329329
}
330330

331-
STATIC void spi_transfer(mp_obj_base_t *self_in, size_t src_len, const uint8_t *src_buf, size_t dest_len, uint8_t *dest_buf, uint32_t timeout) {
331+
STATIC void spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest, uint32_t timeout) {
332332
// Note: there seems to be a problem sending 1 byte using DMA the first
333333
// time directly after the SPI/DMA is initialised. The cause of this is
334334
// unknown but we sidestep the issue by using polling for 1 byte transfer.
335335

336336
pyb_spi_obj_t *self = (pyb_spi_obj_t*)self_in;
337337
HAL_StatusTypeDef status;
338338

339-
if (dest_len == 0) {
339+
if (dest == NULL) {
340340
// send only
341-
if (src_len == 1 || query_irq() == IRQ_STATE_DISABLED) {
342-
status = HAL_SPI_Transmit(self->spi, (uint8_t*)src_buf, src_len, timeout);
341+
if (len == 1 || query_irq() == IRQ_STATE_DISABLED) {
342+
status = HAL_SPI_Transmit(self->spi, (uint8_t*)src, len, timeout);
343343
} else {
344344
DMA_HandleTypeDef tx_dma;
345345
dma_init(&tx_dma, self->tx_dma_descr, self->spi);
346346
self->spi->hdmatx = &tx_dma;
347347
self->spi->hdmarx = NULL;
348-
status = HAL_SPI_Transmit_DMA(self->spi, (uint8_t*)src_buf, src_len);
348+
status = HAL_SPI_Transmit_DMA(self->spi, (uint8_t*)src, len);
349349
if (status == HAL_OK) {
350350
status = spi_wait_dma_finished(self->spi, timeout);
351351
}
352352
dma_deinit(self->tx_dma_descr);
353353
}
354-
} else if (src_len == 0) {
354+
} else if (src == NULL) {
355355
// receive only
356-
if (dest_len == 1 || query_irq() == IRQ_STATE_DISABLED) {
357-
status = HAL_SPI_Receive(self->spi, dest_buf, dest_len, timeout);
356+
if (len == 1 || query_irq() == IRQ_STATE_DISABLED) {
357+
status = HAL_SPI_Receive(self->spi, dest, len, timeout);
358358
} else {
359359
DMA_HandleTypeDef tx_dma, rx_dma;
360360
if (self->spi->Init.Mode == SPI_MODE_MASTER) {
@@ -367,7 +367,7 @@ STATIC void spi_transfer(mp_obj_base_t *self_in, size_t src_len, const uint8_t *
367367
dma_init(&rx_dma, self->rx_dma_descr, self->spi);
368368
self->spi->hdmarx = &rx_dma;
369369

370-
status = HAL_SPI_Receive_DMA(self->spi, dest_buf, dest_len);
370+
status = HAL_SPI_Receive_DMA(self->spi, dest, len);
371371
if (status == HAL_OK) {
372372
status = spi_wait_dma_finished(self->spi, timeout);
373373
}
@@ -378,16 +378,15 @@ STATIC void spi_transfer(mp_obj_base_t *self_in, size_t src_len, const uint8_t *
378378
}
379379
} else {
380380
// send and receive
381-
// requires src_len==dest_len
382-
if (src_len == 1 || query_irq() == IRQ_STATE_DISABLED) {
383-
status = HAL_SPI_TransmitReceive(self->spi, (uint8_t*)src_buf, dest_buf, src_len, timeout);
381+
if (len == 1 || query_irq() == IRQ_STATE_DISABLED) {
382+
status = HAL_SPI_TransmitReceive(self->spi, (uint8_t*)src, dest, len, timeout);
384383
} else {
385384
DMA_HandleTypeDef tx_dma, rx_dma;
386385
dma_init(&tx_dma, self->tx_dma_descr, self->spi);
387386
self->spi->hdmatx = &tx_dma;
388387
dma_init(&rx_dma, self->rx_dma_descr, self->spi);
389388
self->spi->hdmarx = &rx_dma;
390-
status = HAL_SPI_TransmitReceive_DMA(self->spi, (uint8_t*)src_buf, dest_buf, src_len);
389+
status = HAL_SPI_TransmitReceive_DMA(self->spi, (uint8_t*)src, dest, len);
391390
if (status == HAL_OK) {
392391
status = spi_wait_dma_finished(self->spi, timeout);
393392
}
@@ -402,7 +401,7 @@ STATIC void spi_transfer(mp_obj_base_t *self_in, size_t src_len, const uint8_t *
402401
}
403402

404403
STATIC void spi_transfer_machine(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
405-
spi_transfer(self_in, len, src, dest == NULL ? 0 : len, dest, 100);
404+
spi_transfer(self_in, len, src, dest, 100);
406405
}
407406

408407
/******************************************************************************/
@@ -634,7 +633,7 @@ STATIC mp_obj_t pyb_spi_send(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_
634633
pyb_buf_get_for_send(args[0].u_obj, &bufinfo, data);
635634

636635
// send the data
637-
spi_transfer((mp_obj_base_t*)self, bufinfo.len, bufinfo.buf, 0, NULL, args[1].u_int);
636+
spi_transfer((mp_obj_base_t*)self, bufinfo.len, bufinfo.buf, NULL, args[1].u_int);
638637

639638
return mp_const_none;
640639
}
@@ -668,7 +667,7 @@ STATIC mp_obj_t pyb_spi_recv(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_
668667
mp_obj_t o_ret = pyb_buf_get_for_recv(args[0].u_obj, &vstr);
669668

670669
// receive the data
671-
spi_transfer((mp_obj_base_t*)self, 0, NULL, vstr.len, (uint8_t*)vstr.buf, args[1].u_int);
670+
spi_transfer((mp_obj_base_t*)self, vstr.len, NULL, (uint8_t*)vstr.buf, args[1].u_int);
672671

673672
// return the received data
674673
if (o_ret != MP_OBJ_NULL) {
@@ -738,7 +737,7 @@ STATIC mp_obj_t pyb_spi_send_recv(mp_uint_t n_args, const mp_obj_t *pos_args, mp
738737
}
739738

740739
// do the transfer
741-
spi_transfer((mp_obj_base_t*)self, bufinfo_send.len, bufinfo_send.buf, bufinfo_recv.len, bufinfo_recv.buf, args[2].u_int);
740+
spi_transfer((mp_obj_base_t*)self, bufinfo_send.len, bufinfo_send.buf, bufinfo_recv.buf, args[2].u_int);
742741

743742
// return the received data
744743
if (o_ret != MP_OBJ_NULL) {

0 commit comments

Comments
 (0)