⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stm8s_spi.c

📁 STM8全部资料
💻 C
📖 第 1 页 / 共 2 页
字号:
/**
  ******************************************************************************
  * @file stm8s_spi.c
  * @brief This file contains all the functions for the SPI peripheral.
  * @author STMicroelectronics - MCD Application Team
  * @version V1.0.1
  * @date 09/22/2008
  ******************************************************************************
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2008 STMicroelectronics</center></h2>
  * @image html logo.bmp
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm8s_spi.h"

/* LINKER SECTIONS DEFINITION FOR THIS FILE ONLY */
#ifdef USE_COSMIC_SECTIONS
#pragma section (SPI_CODE)
#pragma section const {SPI_CONST}
#pragma section @near [SPI_URAM]
#pragma section @near {SPI_IRAM}
#pragma section @tiny [SPI_UZRAM]
#pragma section @tiny {SPI_IZRAM}
#endif

/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/

/** @addtogroup SPI_Public_Functions
  * @{
  */

/**
* @brief Deinitializes the SPI peripheral registers to their default reset values.
* @par Parameters:
* None
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* SPI_Cmd
* @par Example:
* This example shows how to call the function:
* @code
* SPI_DeInit();
* @endcode
*/
void SPI_DeInit(void)
{
  SPI->CR1    = SPI_CR1_RESET_VALUE;
  SPI->CR2    = SPI_CR2_RESET_VALUE;
  SPI->ICR    = SPI_ICR_RESET_VALUE;
  SPI->SR     = SPI_SR_RESET_VALUE;
  SPI->CRCPR  = SPI_CRCPR_RESET_VALUE;
}

/**
  * @brief Initializes the SPI according to the specified parameters.
  * @param[in] FirstBit : This parameter can be any of the @ref SPI_FirstBit_TypeDef enumeration.
  * @param[in] BaudRatePrescaler : This parameter can be any of the @ref SPI_BaudRatePrescaler_TypeDef enumeration.
  * @param[in] Mode : This parameter can be any of the  @ref SPI_Mode_TypeDef enumeration.
  * @param[in] ClockPolarity : This parameter can be any of the @ref SPI_ClockPolarity_TypeDef enumeration.
  * @param[in] ClockPhase : This parameter can be any of the @ref SPI_ClockPhase_TypeDef enumeration.
  * @param[in] Data_Direction : This parameter can be any of the @ref SPI_DataDirection_TypeDef enumeration.
  * @param[in] Slave_Management : This parameter can be any of the @ref SPI_NSS_TypeDef enumeration.
  * @param[in] CRCPolynomial : Configures the CRC polynomial.
  * @retval void None
  * @par Required preconditions:
  * None.
  * @par Called functions:
  * None.
  * @par Example:
  * @code
  * SPI_Init(SPI_FIRSTBIT_LSB, SPI_BAUDRATEPRESCALER_2, SPI_MODE_MASTER, SPI_CLOCKPOLARITY_HIGH, SPI_CLOCKPHASE_1EDGE, SPI_DATADIRECTION_2LINES_FULLDUPLEX, SPI_NSS_SOFT, 0x07);
  * @endcode
  */
void SPI_Init_1(SPI_FirstBit_TypeDef FirstBit, SPI_BaudRatePrescaler_TypeDef BaudRatePrescaler, SPI_Mode_TypeDef Mode, SPI_ClockPolarity_TypeDef ClockPolarity, SPI_ClockPhase_TypeDef ClockPhase, SPI_DataDirection_TypeDef Data_Direction, SPI_NSS_TypeDef Slave_Management, u8 CRCPolynomial)
{
  /* Check structure elements */
  assert_param(IS_SPI_FIRSTBIT_OK(FirstBit));
  assert_param(IS_SPI_BAUDRATE_PRESCALER_OK(BaudRatePrescaler));
  assert_param(IS_SPI_MODE_OK(Mode));
  assert_param(IS_SPI_POLARITY_OK(ClockPolarity));
  assert_param(IS_SPI_PHASE_OK(ClockPhase));
  assert_param(IS_SPI_DATA_DIRECTION_OK(Data_Direction));
  assert_param(IS_SPI_SLAVEMANAGEMENT_OK(Slave_Management));
  assert_param(IS_SPI_CRC_POLYNOMIAL_OK(CRCPolynomial));

  /* Frame Format, BaudRate, Clock Polarity and Phase configuration */
  SPI->CR1 = (u8)((u8)(FirstBit) |
                  (u8)(BaudRatePrescaler) |
                  (u8)(ClockPolarity) |
                  (u8)(ClockPhase));

  /* Data direction configuration: BDM, BDOE and RXONLY bits */
  SPI->CR2 = (u8)((u8)(Data_Direction) | (u8)(Slave_Management));

  if (Mode == SPI_MODE_MASTER)
  {
    SPI->CR2 |= (u8)SPI_CR2_SSI;
  }
  else
  {
    SPI->CR2 &= (u8)~(SPI_CR2_SSI);
  }

  /* Master/Slave mode configuration */
  SPI->CR1 |= (u8)(Mode);

  /* CRC configuration */
  SPI->CRCPR = (u8)CRCPolynomial;
}

/**
* @brief Enables or disables the SPI peripheral.
* @param[in] NewState New state of the SPI peripheral.
* This parameter can be:
* - ENABLE
* - DISABLE
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* @code
* SPI_Cmd(ENABLE);
* @endcode
*/
void SPI_Cmd(FunctionalState NewState)
{
  /* Check function parameters */
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));

  if (NewState != DISABLE)
  {
    SPI->CR1 |= SPI_CR1_SPE; /* Enable the SPI peripheral*/
  }
  else
  {
    SPI->CR1 &= (u8)(~SPI_CR1_SPE); /* Disable the SPI peripheral*/
  }
}
/**
* @brief Enables or disables the specified interrupts.
* @param[in] SPI_IT Specifies the SPI interrupts sources to be enabled or disabled.
* @param[in] NewState: The new state of the specified SPI interrupts.
* This parameter can be:
* - ENABLE
* - DISABLE.
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* @code
* SPI_ITConfig(SPI_IT_TXE, ENABLE);
* @endcode
*/
void SPI_ITConfig(SPI_IT_TypeDef SPI_IT, FunctionalState NewState)
{
  u8 itpos = 0;
  /* Check function parameters */
  assert_param(IS_SPI_CONFIG_IT_OK(SPI_IT));
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));

  /* Get the SPI IT index */
  itpos = (u8)((u8)1 << (u8)((u8)SPI_IT & (u8)0x0F));

  if (NewState != DISABLE)
  {
    SPI->ICR |= itpos; /* Enable interrupt*/
  }
  else
  {
    SPI->ICR &= (u8)(~itpos); /* Disable interrupt*/
  }
}
/**
* @brief Transmits a Data through the SPI peripheral.
* @param[in] Data Byte to be transmitted.
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* @code
* SPI_SendData(0xFF);
* @endcode
*/
void SPI_SendData(u8 Data)
{
  SPI->DR = Data; /* Write in the DR register the data to be sent*/
}

/**
* @brief Returns the most recent received data by the SPI peripheral.
* @par Parameters:
* None
* @retval u8 The value of the received data.
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* @code
* Return_Value = SPI_ReceiveData();
* @endcode
*/
u8 SPI_ReceiveData(void)
{
  return ((u8)SPI->DR); /* Return the data in the DR register*/
}

/**
* @brief Configures internally by software the NSS pin.
* @param[in] NewState Indicates the new state of the SPI Software slave management.
* This parameter can be:
* - ENABLE
* - DISABLE
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* @code
* SPI_NSSInternalSoftwareCmd(ENABLE);
* @endcode
*/
void SPI_NSSInternalSoftwareCmd(FunctionalState NewState)
{
  /* Check function parameters */
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));

  if (NewState != DISABLE)
  {
    SPI->CR2 |= SPI_CR2_SSI; /* Set NSS pin internally by software*/
  }
  else
  {
    SPI->CR2 &= (u8)(~SPI_CR2_SSI); /* Reset NSS pin internally by software*/
  }
}

/**
* @brief Enables the transmit of the CRC value.
* @par Parameters:
* None
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* @code
* SPI_TransmitCRC();
* @endcode
*/
void SPI_TransmitCRC(void)
{
  SPI->CR2 |= SPI_CR2_CRCNEXT; /* Enable the CRC transmission*/
}

/**
* @brief Enables or disables the CRC value calculation of the transfered bytes.
* @param[in] NewState Indicates the new state of the SPI CRC value calculation.
* This parameter can be:
* - ENABLE
* - DISABLE
* @retval void None
* @par Required preconditions:
* None
* @par Called functions: SPI_Cmd(DISABLE);
* @par Example:
* @code

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -