Skip to content

Commit fada80f

Browse files
Merge pull request ARMmbed#10 from adustm/spi_h743
H743 SPI : use LL instead of HAL
2 parents 8a0b548 + d34ff36 commit fada80f

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

targets/TARGET_STM/stm_spi_api.c

Lines changed: 16 additions & 19 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,
@@ -190,6 +189,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
190189
#if TARGET_STM32H7
191190
handle->Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
192191
handle->Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE;
192+
handle->Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
193193
#endif
194194

195195
init_spi(obj);
@@ -399,16 +399,6 @@ int spi_master_write(spi_t *obj, int value)
399399
if (handle->Init.Direction == SPI_DIRECTION_1LINE) {
400400
return HAL_SPI_Transmit(handle, (uint8_t *)&value, 1, TIMEOUT_1_BYTE);
401401
}
402-
#if TARGET_STM32H7
403-
else {
404-
int retval = 0;
405-
if (HAL_SPI_TransmitReceive(handle, (uint8_t *)&value, (uint8_t *)&retval, 1, TIMEOUT_1_BYTE) != HAL_OK) {
406-
error("spi transmit receive error\n");
407-
}
408-
return retval;
409-
}
410-
#else
411-
412402

413403
#if defined(LL_SPI_RX_FIFO_TH_HALF)
414404
/* Configure the default data size */
@@ -425,32 +415,39 @@ int spi_master_write(spi_t *obj, int value)
425415
* but this will increase performances significantly
426416
*/
427417

428-
/* Wait TXE flag to transmit data */
429418
#if TARGET_STM32H7
430-
while (!LL_SPI_IsActiveFlag_TXC(SPI_INST(obj)));
431-
#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 */
432426
while (!LL_SPI_IsActiveFlag_TXE(SPI_INST(obj)));
427+
433428
#endif /* TARGET_STM32H7 */
434429

430+
/* Transmit data */
435431
if (handle->Init.DataSize == SPI_DATASIZE_16BIT) {
436-
LL_SPI_TransmitData16(SPI_INST(obj), value);
432+
LL_SPI_TransmitData16(SPI_INST(obj), (uint16_t)value);
437433
} else {
438-
LL_SPI_TransmitData8(SPI_INST(obj), (uint8_t) value);
434+
LL_SPI_TransmitData8(SPI_INST(obj), (uint8_t)value);
439435
}
440436

441-
/* Then wait RXE flag before reading */
442437
#if TARGET_STM32H7
443-
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)));
444440
#else /* TARGET_STM32H7 */
441+
/* Wait for RXNE flag before reading */
445442
while (!LL_SPI_IsActiveFlag_RXNE(SPI_INST(obj)));
446443
#endif /* TARGET_STM32H7 */
447444

445+
/* Read received data */
448446
if (handle->Init.DataSize == SPI_DATASIZE_16BIT) {
449447
return LL_SPI_ReceiveData16(SPI_INST(obj));
450448
} else {
451449
return LL_SPI_ReceiveData8(SPI_INST(obj));
452450
}
453-
#endif
454451
}
455452

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

0 commit comments

Comments
 (0)