Skip to content

Commit d34ff36

Browse files
committed
Integrate SPI LL functions into former implementation
1 parent 348bfa0 commit d34ff36

File tree

1 file changed

+15
-43
lines changed

1 file changed

+15
-43
lines changed

targets/TARGET_STM/stm_spi_api.c

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ void init_spi(spi_t *obj)
8282
if (HAL_SPI_Init(handle) != HAL_OK) {
8383
error("Cannot initialize SPI");
8484
}
85-
8685
/* In case of standard 4 wires SPI,PI can be kept enabled all time
8786
* and SCK will only be generated during the write operations. But in case
8887
* of 3 wires, it should be only enabled during rd/wr unitary operations,
@@ -400,41 +399,7 @@ int spi_master_write(spi_t *obj, int value)
400399
if (handle->Init.Direction == SPI_DIRECTION_1LINE) {
401400
return HAL_SPI_Transmit(handle, (uint8_t *)&value, 1, TIMEOUT_1_BYTE);
402401
}
403-
#if TARGET_STM32H7
404-
else {
405-
int retval = 0;
406-
#if 0
407-
if (HAL_SPI_TransmitReceive(handle, (uint8_t *)&value, (uint8_t *)&retval, 1, TIMEOUT_1_BYTE) != HAL_OK) {
408-
error("spi transmit receive error\n");
409-
}
410-
#endif
411-
412-
/* Master transfer start */
413-
LL_SPI_StartMasterTransfer(SPI_INST(obj));
414-
415-
/* Wait for TXP flag, no timeout */
416-
while (!LL_SPI_IsActiveFlag_TXP(SPI_INST(obj)));
417-
418-
/* Write data to transmit */
419-
if (handle->Init.DataSize == SPI_DATASIZE_16BIT) {
420-
LL_SPI_TransmitData16(SPI_INST(obj), (uint16_t)value);
421-
} else {
422-
LL_SPI_TransmitData8(SPI_INST(obj), (uint8_t)value);
423-
}
424-
425-
/* Wait RXP or end of Transfer */
426-
while (!LL_SPI_IsActiveFlag_RXP(SPI_INST(obj)));
427-
428-
/* Read received data */
429-
if (handle->Init.DataSize == SPI_DATASIZE_16BIT) {
430-
retval = LL_SPI_ReceiveData16(SPI_INST(obj));
431-
} else {
432-
retval = LL_SPI_ReceiveData8(SPI_INST(obj));
433-
}
434402

435-
return retval;
436-
}
437-
#else
438403
#if defined(LL_SPI_RX_FIFO_TH_HALF)
439404
/* Configure the default data size */
440405
if (handle->Init.DataSize == SPI_DATASIZE_16BIT) {
@@ -450,32 +415,39 @@ int spi_master_write(spi_t *obj, int value)
450415
* but this will increase performances significantly
451416
*/
452417

453-
/* Wait TXE flag to transmit data */
454418
#if TARGET_STM32H7
455-
while (!LL_SPI_IsActiveFlag_TXC(SPI_INST(obj)));
456-
#else /* TARGET_STM32H7 */
419+
/* Master transfer start */
420+
LL_SPI_StartMasterTransfer(SPI_INST(obj));
421+
422+
/* Wait TXP flag to transmit data */
423+
while (!LL_SPI_IsActiveFlag_TXP(SPI_INST(obj)));
424+
#else
425+
/* Wait TXE flag to transmit data */
457426
while (!LL_SPI_IsActiveFlag_TXE(SPI_INST(obj)));
427+
458428
#endif /* TARGET_STM32H7 */
459429

430+
/* Transmit data */
460431
if (handle->Init.DataSize == SPI_DATASIZE_16BIT) {
461-
LL_SPI_TransmitData16(SPI_INST(obj), value);
432+
LL_SPI_TransmitData16(SPI_INST(obj), (uint16_t)value);
462433
} else {
463-
LL_SPI_TransmitData8(SPI_INST(obj), (uint8_t) value);
434+
LL_SPI_TransmitData8(SPI_INST(obj), (uint8_t)value);
464435
}
465436

466-
/* Then wait RXE flag before reading */
467437
#if TARGET_STM32H7
468-
while (!LL_SPI_IsActiveFlag_RXWNE(SPI_INST(obj)));
438+
/* Wait for RXP or end of Transfer */
439+
while (!LL_SPI_IsActiveFlag_RXP(SPI_INST(obj)));
469440
#else /* TARGET_STM32H7 */
441+
/* Wait for RXNE flag before reading */
470442
while (!LL_SPI_IsActiveFlag_RXNE(SPI_INST(obj)));
471443
#endif /* TARGET_STM32H7 */
472444

445+
/* Read received data */
473446
if (handle->Init.DataSize == SPI_DATASIZE_16BIT) {
474447
return LL_SPI_ReceiveData16(SPI_INST(obj));
475448
} else {
476449
return LL_SPI_ReceiveData8(SPI_INST(obj));
477450
}
478-
#endif
479451
}
480452

481453
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,

0 commit comments

Comments
 (0)