Skip to content

Commit 179bba9

Browse files
committed
Move common STM32 SPI operations to separate functions
- move a code that waits readable SPI state from `spi_master_write` function to inline functions `msp_writable` and `msp_wait_writable` - move a code that waits writeable SPI state from `spi_master_write` function to inline functions `msp_readable` and `msp_wait_readable` - move a code that writes data to SPI from `spi_master_write` function to inline function `msp_write_data` - move a code that reads data from SPI from `spi_master_write` function to inline function `msp_read_data`
1 parent 50c1352 commit 179bba9

File tree

1 file changed

+84
-35
lines changed

1 file changed

+84
-35
lines changed

targets/TARGET_STM/stm_spi_api.c

Lines changed: 84 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,84 @@ static inline int datasize_to_transfer_bitshift(uint32_t DataSize)
777777
}
778778
}
779779

780+
/**
781+
* Check if SPI master interface is writable.
782+
*
783+
* @param obj
784+
* @return 0 - SPI isn't writable, non-zero - SPI is writable
785+
*/
786+
static inline int msp_writable(spi_t *obj)
787+
{
788+
#if TARGET_STM32H7
789+
return (int)LL_SPI_IsActiveFlag_TXP(SPI_INST(obj));
790+
#else /* TARGET_STM32H7 */
791+
return (int)LL_SPI_IsActiveFlag_TXE(SPI_INST(obj));
792+
#endif /* TARGET_STM32H7 */
793+
}
794+
795+
/**
796+
* Check if SPI master interface is readable.
797+
*
798+
* @param obj
799+
* @return 0 - SPI isn't readable, non-zero - SPI is readable
800+
*/
801+
static inline int msp_readable(spi_t *obj)
802+
{
803+
#if TARGET_STM32H7
804+
return (int)LL_SPI_IsActiveFlag_RXP(SPI_INST(obj));
805+
#else /* TARGET_STM32H7 */
806+
return (int)LL_SPI_IsActiveFlag_RXNE(SPI_INST(obj));
807+
#endif /* TARGET_STM32H7 */
808+
}
809+
810+
/**
811+
* Wait till SPI master interface is writable.
812+
*/
813+
static inline void msp_wait_writable(spi_t *obj)
814+
{
815+
while (!msp_writable(obj));
816+
}
817+
818+
/**
819+
* Wait till SPI master interface is readable.
820+
*/
821+
static inline void msp_wait_readable(spi_t *obj)
822+
{
823+
while (!msp_readable(obj));
824+
}
825+
826+
/**
827+
* Write data to SPI master interface.
828+
*/
829+
static inline void msp_write_data(spi_t *obj, int value, int bitshift)
830+
{
831+
if (bitshift == 1) {
832+
LL_SPI_TransmitData16(SPI_INST(obj), (uint16_t)value);
833+
#ifdef HAS_32BIT_SPI_TRANSFERS
834+
} else if (bitshift == 2) {
835+
LL_SPI_TransmitData32(SPI_INST(obj), (uint32_t)value);
836+
#endif /* HAS_32BIT_SPI_TRANSFERS */
837+
} else {
838+
LL_SPI_TransmitData8(SPI_INST(obj), (uint8_t)value);
839+
}
840+
}
841+
842+
/**
843+
* Read data from SPI master interface.
844+
*/
845+
static inline int msp_read_data(spi_t *obj, int bitshift)
846+
{
847+
if (bitshift == 1) {
848+
return LL_SPI_ReceiveData16(SPI_INST(obj));
849+
#ifdef HAS_32BIT_SPI_TRANSFERS
850+
} else if (bitshift == 2) {
851+
return LL_SPI_ReceiveData32(SPI_INST(obj));
852+
#endif /* HAS_32BIT_SPI_TRANSFERS */
853+
} else {
854+
return LL_SPI_ReceiveData8(SPI_INST(obj));
855+
}
856+
}
857+
780858
int spi_master_write(spi_t *obj, int value)
781859
{
782860
struct spi_s *spiobj = SPI_S(obj);
@@ -806,44 +884,15 @@ int spi_master_write(spi_t *obj, int value)
806884
#if TARGET_STM32H7
807885
/* Master transfer start */
808886
LL_SPI_StartMasterTransfer(SPI_INST(obj));
809-
810-
/* Wait TXP flag to transmit data */
811-
while (!LL_SPI_IsActiveFlag_TXP(SPI_INST(obj)));
812-
#else
813-
/* Wait TXE flag to transmit data */
814-
while (!LL_SPI_IsActiveFlag_TXE(SPI_INST(obj)));
815-
816-
#endif /* TARGET_STM32H7 */
817-
818-
/* Transmit data */
819-
if (bitshift == 1) {
820-
LL_SPI_TransmitData16(SPI_INST(obj), (uint16_t)value);
821-
#ifdef HAS_32BIT_SPI_TRANSFERS
822-
} else if (bitshift == 2) {
823-
LL_SPI_TransmitData32(SPI_INST(obj), (uint32_t)value);
824887
#endif
825-
} else {
826-
LL_SPI_TransmitData8(SPI_INST(obj), (uint8_t)value);
827-
}
828888

829-
#if TARGET_STM32H7
830-
/* Wait for RXP or end of Transfer */
831-
while (!LL_SPI_IsActiveFlag_RXP(SPI_INST(obj)));
832-
#else /* TARGET_STM32H7 */
833-
/* Wait for RXNE flag before reading */
834-
while (!LL_SPI_IsActiveFlag_RXNE(SPI_INST(obj)));
835-
#endif /* TARGET_STM32H7 */
889+
/* Transmit data */
890+
msp_wait_writable(obj);
891+
msp_write_data(obj, value, bitshift);
836892

837-
/* Read received data */
838-
if (bitshift == 1) {
839-
return LL_SPI_ReceiveData16(SPI_INST(obj));
840-
#ifdef HAS_32BIT_SPI_TRANSFERS
841-
} else if (bitshift == 2) {
842-
return LL_SPI_ReceiveData32(SPI_INST(obj));
843-
#endif
844-
} else {
845-
return LL_SPI_ReceiveData8(SPI_INST(obj));
846-
}
893+
/* Receive data */
894+
msp_wait_readable(obj);
895+
return msp_read_data(obj, bitshift);
847896
}
848897

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

0 commit comments

Comments
 (0)