SPI Exported Functions

group SPI_Exported_Functions

Functions

void SPI_DeInit(SPI_TypeDef *SPIx)

Deinitialize the SPIx peripheral registers to their default reset values.

Example usage

void driver_spi_init(void)
{
    SPI_DeInit(SPI0);
}

Parameters:

SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

Returns:

None.

void SPI_Init(SPI_TypeDef *SPIx, SPI_InitTypeDef *SPI_InitStruct)

Initialize the SPIx peripheral according to the specified parameters in the SPI_InitStruct.

Example usage

void driver_spi_init(void)
{
    RCC_PeriphClockCmd(APBPeriph_SPI0, APBPeriph_SPI0_CLOCK, ENABLE);

    SPI_InitTypeDef  SPI_InitStruct;
    SPI_StructInit(&SPI_InitStruct);

    SPI_InitStruct.SPI_Direction   = SPI_Direction_EEPROM;
    SPI_InitStruct.SPI_Mode        = SPI_Mode_Master;
    SPI_InitStruct.SPI_DataSize    = SPI_DataSize_8b;
    SPI_InitStruct.SPI_CPOL        = SPI_CPOL_High;
    SPI_InitStruct.SPI_CPHA        = SPI_CPHA_2Edge;
    SPI_InitStruct.SPI_BaudRatePrescaler  = 100;
    SPI_InitStruct.SPI_RxThresholdLevel  = 1 - 1;
    SPI_InitStruct.SPI_NDF               = 1 - 1;
    SPI_InitStruct.SPI_FrameFormat = SPI_Frame_Motorola;

    SPI_Init(SPI0, &SPI_InitStruct);
}

Parameters:
  • SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

  • SPI_InitStruct[in] Pointer to a SPI_InitTypeDef structure that contains the configuration information for the specified SPI peripheral.

Returns:

None.

void SPI_StructInit(SPI_InitTypeDef *SPI_InitStruct)

Fill each SPI_InitStruct member with its default value.

Example usage

void driver_spi_init(void)
{

    RCC_PeriphClockCmd(APBPeriph_SPI0, APBPeriph_SPI0_CLOCK, ENABLE);

    SPI_InitTypeDef  SPI_InitStruct;
    SPI_StructInit(&SPI_InitStruct);

    SPI_InitStruct.SPI_Direction   = SPI_Direction_EEPROM;
    SPI_InitStruct.SPI_Mode        = SPI_Mode_Master;
    SPI_InitStruct.SPI_DataSize    = SPI_DataSize_8b;
    SPI_InitStruct.SPI_CPOL        = SPI_CPOL_High;
    SPI_InitStruct.SPI_CPHA        = SPI_CPHA_2Edge;
    SPI_InitStruct.SPI_BaudRatePrescaler  = 100;
    SPI_InitStruct.SPI_RxThresholdLevel  = 1 - 1;
    SPI_InitStruct.SPI_NDF               = 1 - 1;
    SPI_InitStruct.SPI_FrameFormat = SPI_Frame_Motorola;

    SPI_Init(SPI0, &SPI_InitStruct);
}

Parameters:

SPI_InitStruct[in] Pointer to a SPI_InitTypeDef structure which will be initialized.

Returns:

None.

void SPI_Cmd(SPI_TypeDef *SPIx, FunctionalState NewState)

Enable or disable the selected SPI peripheral.

Example usage

void driver_spi_init(void)
{

    RCC_PeriphClockCmd(APBPeriph_SPI0, APBPeriph_SPI0_CLOCK, ENABLE);

    SPI_InitTypeDef  SPI_InitStruct;
    SPI_StructInit(&SPI_InitStruct);

    SPI_InitStruct.SPI_Direction   = SPI_Direction_EEPROM;
    SPI_InitStruct.SPI_Mode        = SPI_Mode_Master;
    SPI_InitStruct.SPI_DataSize    = SPI_DataSize_8b;
    SPI_InitStruct.SPI_CPOL        = SPI_CPOL_High;
    SPI_InitStruct.SPI_CPHA        = SPI_CPHA_2Edge;
    SPI_InitStruct.SPI_BaudRatePrescaler  = 100;
    SPI_InitStruct.SPI_RxThresholdLevel  = 1 - 1;
    SPI_InitStruct.SPI_NDF               = 1 - 1;
    SPI_InitStruct.SPI_FrameFormat = SPI_Frame_Motorola;

    SPI_Init(SPI0, &SPI_InitStruct);
    SPI_Cmd(SPI0, ENABLE);
}

Parameters:
  • SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

  • NewState[in] New state of the SPIx peripheral. This parameter can be: ENABLE or DISABLE.

Returns:

None.

void SPI_SendBuffer(SPI_TypeDef *SPIx, uint8_t *pBuf, uint16_t len)

Transmit a number of bytes through the SPIx peripheral.

Example usage

void spi_demo(void)
{
    uint8_t data_buf[] = {0x01,0x02,0x03};
    SPI_SendBuffer(SPI0, data_buf, sizeof(data_buf));
}

Parameters:
  • SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

  • pBuf[in] Bytes to be transmitted.

  • len[in] Byte length to be transmitted.

Returns:

None.

void SPI_SendHalfWord(SPI_TypeDef *SPIx, uint16_t *pBuf, uint16_t len)

Transmit a number of halfWords through the SPIx peripheral.

Example usage

void spi_demo(void)
{
    uint16_t data_buf[] = {0x0102,0x0203,0x0304};
    SPI_SendHalfWord(SPI0, data_buf, sizeof(data_buf));
}

Parameters:
  • SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

  • pBuf[in] Halfwords to be transmitted.

  • len[in] Halfwords length to be transmitted.

Returns:

None.

void SPI_SendWord(SPI_TypeDef *SPIx, uint32_t *pBuf, uint16_t len)

Transmit a number of words through the SPIx peripheral.

Example usage

void spi_demo(void)
{
    uint32_t data_buf[] = {0x01020304,0x02030405,0x03040506};
    SPI_SendWord(SPI0, data_buf, sizeof(data_buf));
}

Parameters:
  • SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

  • pBuf[in] Words to be transmitted.

  • len[in] Word length to be transmitted.

Returns:

None.

void SPI_INTConfig(SPI_TypeDef *SPIx, uint8_t SPI_IT, FunctionalState NewState)

Enable or disable the specified SPI interrupt source.

Example usage

void spi_demo(void)
{
    SPI_INTConfig(SPI0, SPI_INT_RXF, ENABLE);
}

Parameters:
  • SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

  • SPI_IT[in] Specify the SPI interrupt source to be enabled or disabled. This parameter can be one of the following values, refer to SPI Interrupt Definition.

    • SPI_INT_TXE: The TX FIFO is equal to or below its threshold value and requires service to prevent an under-run.

    • SPI_INT_TXO: An APB access attempts to write into the TX FIFO after it has been completely filled. When set, data written from the APB is discarded.

    • SPI_INT_RXU: An APB access attempts to read from the RX FIFO when it is empty. When set, zeros are read back from the RX FIFO.

    • SPI_INT_RXO: The receive logic attempts to place data into the RX FIFO after it has been completely filled. When set, newly received data are discarded.

    • SPI_INT_RXF: RX FIFO is equal to or above its threshold value plus 1 and requires service to prevent an overflow.

    • SPI_INT_MST: The interrupt is set when another serial master on the serial bus selects the SPI master as a serial-slave device and is actively transferring data.

  • NewState[in] New state of the specified SPI interrupt source. This parameter can be: ENABLE or DISABLE.

Returns:

None.

void SPI_ClearINTPendingBit(SPI_TypeDef *SPIx, uint16_t SPI_IT)

Clear the specified SPI interrupt pending bit.

Example usage

void spi_demo(void)
{
    SPI_ClearINTPendingBit(SPI0, SPI_INT_RXF);
}

Parameters:
  • SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

  • SPI_IT[in] Specify the SPI interrupt to clear. This parameter can be one of the following values, refer to SPI Interrupt Definition.

    • SPI_INT_TXE: The TX FIFO is equal to or below its threshold value and requires service to prevent an under-run.

    • SPI_INT_TXO: An APB access attempts to write into the TX FIFO after it has been completely filled. When set, data written from the APB is discarded.

    • SPI_INT_RXU: An APB access attempts to read from the RX FIFO when it is empty. When set, zeros are read back from the RX FIFO.

    • SPI_INT_RXO: The receive logic attempts to place data into the RX FIFO after it has been completely filled. When set, newly received data are discarded.

    • SPI_INT_RXF: RX FIFO is equal to or above its threshold value plus 1 and requires service to prevent an overflow.

    • SPI_INT_MST: The interrupt is set when another serial master on the serial bus selects the SPI master as a serial-slave device and is actively transferring data.

Returns:

None.

void SPI_SendData(SPI_TypeDef *SPIx, uint32_t Data)

Transmit a data through the SPIx peripheral.

Example usage

void spi_demo(void)
{
    uint32_t data = 0x01020304;
    SPI_SendData(SPI0, data);
}

Parameters:
  • SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

  • Data[in] Data to be transmitted.

Returns:

None.

uint32_t SPI_ReceiveData(SPI_TypeDef *SPIx)

Receive data by the SPIx peripheral.

Example usage

void spi_demo(void)
{
    uint32_t data = SPI_ReceiveData(SPI0);
}

Parameters:

SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

Returns:

The most recent received data.

uint8_t SPI_GetTxFIFOLen(SPI_TypeDef *SPIx)

Get data length in Tx FIFO through the SPIx peripheral.

Example usage

void spi_demo(void)
{
    uint8_t data_len = SPI_GetTxFIFOLen(SPI0);
}

Parameters:

SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

Returns:

Data length in Tx FIFO.

uint8_t SPI_GetRxFIFOLen(SPI_TypeDef *SPIx)

Get data length in Rx FIFO through the SPIx peripheral.

Example usage

void spi_demo(void)
{
    uint8_t data_len = SPI_GetRxFIFOLen(SPI0);
}

Parameters:

SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

Returns:

Data length in Rx FIFO.

void SPI_ChangeDirection(SPI_TypeDef *SPIx, uint16_t dir)

Change SPI direction mode.

Example usage

void spi_demo(void)
{
    SPI_ChangeDirection(SPI0, SPI_Direction_EEPROM);
}

Parameters:
  • SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

  • dir[in] Value of direction mode, refer to SPI Data Direction.

Returns:

None.

void SPI_SetReadLen(SPI_TypeDef *SPIx, uint16_t len)

Set read Data length only in EEPROM mode or RX only mode through the SPIx peripheral, which enables receiving up to 64 KB of data in a continuous transfer.

Example usage

void spi_demo(void)
{
    SPI_SetReadLen(SPI0, 100);
}

Parameters:
  • SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

  • len[in] Length of read data which can be 1 to 65536.

Returns:

None.

void SPI_SetCSNumber(SPI_TypeDef *SPIx, uint8_t number)

Set cs number through the SPIx peripheral.

Example usage

void spi_demo(void)
{
    SPI_SetCSNumber(SPI1, 1);
}

Parameters:
  • SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

  • number[in] If SPIx is SPI0, number must be 0. If SPIx is SPI1, number can be 0 to 2.

Returns:

None.

ITStatus SPI_GetINTStatus(SPI_TypeDef *SPIx, uint32_t SPI_IT)

Check whether the specified SPI interrupt is set.

Example usage

void spi_demo(void)
{
    ITStatus int_status = SPI_GetINTStatus(SPI0, SPI_INT_RXF);
}

Parameters:
  • SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

  • SPI_IT[in] Specify the SPI interrupt to check. This parameter can be one of the following values, refer to SPI Interrupt Definition.

    • SPI_INT_TXE: The TX FIFO is equal to or below its threshold value and requires service to prevent an under-run.

    • SPI_INT_TXO: An APB access attempts to write into the TX FIFO after it has been completely filled. When set, data written from the APB is discarded.

    • SPI_INT_RXU: An APB access attempts to read from the RX FIFO when it is empty. When set, zeros are read back from the RX FIFO.

    • SPI_INT_RXO: The receive logic attempts to place data into the RX FIFO after it has been completely filled. When set, newly received data are discarded.

    • SPI_INT_RXF: RX FIFO is equal to or above its threshold value plus 1 and requires service to prevent an overflow.

    • SPI_INT_MST: The interrupt is set when another serial master on the serial bus selects the SPI master as a serial-slave device and is actively transferring data.

Returns:

The new state of SPI_IT (SET or RESET).

FlagStatus SPI_GetFlagState(SPI_TypeDef *SPIx, uint8_t SPI_FLAG)

Check whether the specified SPI flag is set.

Example usage

void spi_demo(void)
{
    FlagStatus flag_status = SPI_GetFlagState(SPI0, SPI_FLAG_TXE);

}

Parameters:
  • SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

  • SPI_FLAG[in] Specify the SPI flag to check. This parameter can be one of the following values, refer to SPI Flags Definition.

    • SPI_FLAG_DCOL: Data Collision Error flag. Set if it is actively transmitting in master mode when another master selects this device as a slave.

    • SPI_FLAG_TXE: Set if the transmit FIFO is empty when a transfer is started.

    • SPI_FLAG_RFF: When the receive FIFO is completely full, this bit is set.

    • SPI_FLAG_RFNE: Set when the receive FIFO contains one or more entries and is cleared when the receive FIFO is empty.

    • SPI_FLAG_TFE: When the transmit FIFO is completely empty, this bit is set.

    • SPI_FLAG_TFNF: Set when the transmit FIFO contains one or more empty locations, and is cleared when the FIFO is full.

    • SPI_FLAG_BUSY: When set, indicates that a serial transfer is in progress; when cleared indicates that the SPI is idle or disabled.

Returns:

The new state of SPI_FLAG (SET or RESET).

void SPI_GDMACmd(SPI_TypeDef *SPIx, uint16_t SPI_GDMAReq, FunctionalState NewState)

Enable or disable the SPIx GDMA interface.

Example usage

void spi_demo(void)
{
    SPI_GDMACmd(SPI0, SPI_GDMAReq_Tx, ENABLE);
}

Parameters:
  • SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

  • SPI_GDMAReq[in] Specify the SPI GDMA transfer request to be enabled or disabled. This parameter can be one of the following values, refer to SPI GDMA Transfer Request.

    • SPI_GDMAReq_Tx: Tx buffer DMA transfer request.

    • SPI_GDMAReq_Rx: Rx buffer DMA transfer request.

  • NewState[in] New state of the selected SPI GDMA transfer request. This parameter can be: ENABLE or DISABLE.

Returns:

None.

void SPI_Change_CLK(SPI_TypeDef *SPIx, uint32_t prescaler)

Change SPI speed dynamically.

Example usage

void spi_demo(void)
{
    SPI_Change_CLK(SPI0, SPI_BaudRatePrescaler_2);
}

Note

In addition to the reference values, you can also set custom frequency divisions that are multiples of an even number. Note that only even multiples are allowed.

Parameters:
  • SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

  • prescaler[in] Value of prescaler. This parameter can be one of the following values, refer to SPI BaudRate Prescaler Value.

Returns:

None.

void SPI_SetRxSampleDly(SPI_TypeDef *SPIx, uint32_t delay)

Set SPI Rx sample delay.

Example usage

void spi_demo(void)
{
    SPI_SetRxSampleDly(SPI0, 1);
}

Parameters:
  • SPIx[in] Where x can be 0 or 1 to select the SPI peripheral.

  • delay[in] This parameter can be 0 to 255.

Returns:

None.