Skip to content

Commit 348bfa0

Browse files
committed
Fix SPI write function for STM32H743
(use LL instead of HAL)
1 parent 8a0b548 commit 348bfa0

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

targets/TARGET_STM/stm_spi_api.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
190190
#if TARGET_STM32H7
191191
handle->Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
192192
handle->Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE;
193+
handle->Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
193194
#endif
194195

195196
init_spi(obj);
@@ -402,14 +403,38 @@ int spi_master_write(spi_t *obj, int value)
402403
#if TARGET_STM32H7
403404
else {
404405
int retval = 0;
406+
#if 0
405407
if (HAL_SPI_TransmitReceive(handle, (uint8_t *)&value, (uint8_t *)&retval, 1, TIMEOUT_1_BYTE) != HAL_OK) {
406408
error("spi transmit receive error\n");
407409
}
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+
}
434+
408435
return retval;
409436
}
410437
#else
411-
412-
413438
#if defined(LL_SPI_RX_FIFO_TH_HALF)
414439
/* Configure the default data size */
415440
if (handle->Init.DataSize == SPI_DATASIZE_16BIT) {

0 commit comments

Comments
 (0)