@@ -502,69 +502,68 @@ void spi_deinit(spi_t *obj)
502502 * @param obj : pointer to spi_t structure
503503 * @param buffer : tx data to send before reception
504504 * @param len : length in byte of the data to send and receive
505- * @param Timeout: Timeout duration in tick
506505 * @param skipReceive: skip receiving data after transmit or not
507506 * @retval status of the send operation (0) in case of error
508507 */
509- spi_status_e spi_transfer (spi_t * obj , uint8_t * buffer , uint16_t len ,
510- uint32_t Timeout , bool skipReceive )
508+ spi_status_e spi_transfer (spi_t * obj , uint8_t * buffer , uint16_t len , bool skipReceive )
511509{
512510 spi_status_e ret = SPI_OK ;
513511 uint32_t tickstart , size = len ;
514512 SPI_TypeDef * _SPI = obj -> handle .Instance ;
515513 uint8_t * tx_buffer = buffer ;
516514
517- if (( len == 0 ) || ( Timeout == 0U ) ) {
518- return Timeout > 0U ? SPI_ERROR : SPI_TIMEOUT ;
519- }
520- tickstart = HAL_GetTick ();
515+ if (len == 0 ) {
516+ ret = SPI_ERROR ;
517+ } else {
518+ tickstart = HAL_GetTick ();
521519
522520#if defined(SPI_CR2_TSIZE )
523- /* Start transfer */
524- LL_SPI_SetTransferSize (_SPI , size );
525- LL_SPI_Enable (_SPI );
526- LL_SPI_StartMasterTransfer (_SPI );
521+ /* Start transfer */
522+ LL_SPI_SetTransferSize (_SPI , size );
523+ LL_SPI_Enable (_SPI );
524+ LL_SPI_StartMasterTransfer (_SPI );
527525#endif
528526
529- while (size -- ) {
527+ while (size -- ) {
530528#if defined(SPI_SR_TXP )
531- while (!LL_SPI_IsActiveFlag_TXP (_SPI ));
529+ while (!LL_SPI_IsActiveFlag_TXP (_SPI ));
532530#else
533- while (!LL_SPI_IsActiveFlag_TXE (_SPI ));
531+ while (!LL_SPI_IsActiveFlag_TXE (_SPI ));
534532#endif
535- LL_SPI_TransmitData8 (_SPI , * tx_buffer ++ );
533+ LL_SPI_TransmitData8 (_SPI , * tx_buffer ++ );
536534
537- if (!skipReceive ) {
535+ if (!skipReceive ) {
538536#if defined(SPI_SR_RXP )
539- while (!LL_SPI_IsActiveFlag_RXP (_SPI ));
537+ while (!LL_SPI_IsActiveFlag_RXP (_SPI ));
540538#else
541- while (!LL_SPI_IsActiveFlag_RXNE (_SPI ));
539+ while (!LL_SPI_IsActiveFlag_RXNE (_SPI ));
542540#endif
543- * buffer ++ = LL_SPI_ReceiveData8 (_SPI );
544- }
545- if ((Timeout != HAL_MAX_DELAY ) && (HAL_GetTick () - tickstart >= Timeout )) {
546- ret = SPI_TIMEOUT ;
547- break ;
541+ * buffer ++ = LL_SPI_ReceiveData8 (_SPI );
542+ }
543+ if ((SPI_TRANSFER_TIMEOUT != HAL_MAX_DELAY ) &&
544+ (HAL_GetTick () - tickstart >= SPI_TRANSFER_TIMEOUT )) {
545+ ret = SPI_TIMEOUT ;
546+ break ;
547+ }
548548 }
549- }
550549
551550#if defined(SPI_IFCR_EOTC )
552- // Add a delay before disabling SPI otherwise last-bit/last-clock may be truncated
553- // See https://github.com/stm32duino/Arduino_Core_STM32/issues/1294
554- // Computed delay is half SPI clock
555- delayMicroseconds (obj -> disable_delay );
556-
557- /* Close transfer */
558- /* Clear flags */
559- LL_SPI_ClearFlag_EOT (_SPI );
560- LL_SPI_ClearFlag_TXTF (_SPI );
561- /* Disable SPI peripheral */
562- LL_SPI_Disable (_SPI );
551+ // Add a delay before disabling SPI otherwise last-bit/last-clock may be truncated
552+ // See https://github.com/stm32duino/Arduino_Core_STM32/issues/1294
553+ // Computed delay is half SPI clock
554+ delayMicroseconds (obj -> disable_delay );
555+
556+ /* Close transfer */
557+ /* Clear flags */
558+ LL_SPI_ClearFlag_EOT (_SPI );
559+ LL_SPI_ClearFlag_TXTF (_SPI );
560+ /* Disable SPI peripheral */
561+ LL_SPI_Disable (_SPI );
563562#else
564- /* Wait for end of transfer */
565- while (LL_SPI_IsActiveFlag_BSY (_SPI ));
563+ /* Wait for end of transfer */
564+ while (LL_SPI_IsActiveFlag_BSY (_SPI ));
566565#endif
567-
566+ }
568567 return ret ;
569568}
570569
0 commit comments