CodeForge QQ客服 CodeForge 400电话 客服电话 4006316121
首页 » 源代码 » STM32+CAN总线通信 » stm32f10x_spi.c

stm32f10x_spi.c ( 文件浏览 )

文件源自:STM32+CAN总线通信
源码简介:
  • lg1994 发布于2018-03-31
  • 浏览次数:0
  • 下载次数:0
  • 下载需 1 积分
  • 侵权举报
			/**
  ******************************************************************************
  * @file    stm32f10x_spi.c
  * @author  MCD Application Team
  * @version V3.1.0
  * @date    06/19/2009
  * @brief   This file provides all the SPI firmware functions.
  ******************************************************************************
  * @copy
  *
  * 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 2009 STMicroelectronics</center></h2>
  */ 

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_spi.h"
#include "stm32f10x_rcc.h"

/** @addtogroup STM32F10x_StdPeriph_Driver
  * @{

  */

/** @defgroup SPI 
  * @brief SPI driver modules
  * @{

  */ 

/** @defgroup SPI_Private_TypesDefinitions
  * @{

  */

/**
  * @
}
  */ 


/** @defgroup SPI_Private_Defines
  * @{

  */

/* SPI SPE mask */
#define CR1_SPE_Set          ((uint16_t)0x0040)
#define CR1_SPE_Reset        ((uint16_t)0xFFBF)

/* I2S I2SE mask */
#define I2SCFGR_I2SE_Set     ((uint16_t)0x0400)
#define I2SCFGR_I2SE_Reset   ((uint16_t)0xFBFF)

/* SPI CRCNext mask */
#define CR1_CRCNext_Set      ((uint16_t)0x1000)

/* SPI CRCEN mask */
#define CR1_CRCEN_Set        ((uint16_t)0x2000)
#define CR1_CRCEN_Reset      ((uint16_t)0xDFFF)

/* SPI SSOE mask */
#define CR2_SSOE_Set         ((uint16_t)0x0004)
#define CR2_SSOE_Reset       ((uint16_t)0xFFFB)

/* SPI registers Masks */
#define CR1_CLEAR_Mask       ((uint16_t)0x3040)
#define I2SCFGR_CLEAR_Mask   ((uint16_t)0xF040)

/* SPI or I2S mode selection masks */
#define SPI_Mode_Select      ((uint16_t)0xF7FF)
#define I2S_Mode_Select      ((uint16_t)0x0800) 

/* I2S clock source selection masks */
#define I2S2_CLOCK_SRC       ((u32)(0x00020000))
#define I2S3_CLOCK_SRC       ((u32)(0x00040000))
#define I2S_MUL_MASK         ((u32)(0x0000F000))
#define I2S_DIV_MASK         ((u32)(0x000000F0))

/**
  * @
}
  */

/** @defgroup SPI_Private_Macros
  * @{

  */

/**
  * @
}
  */

/** @defgroup SPI_Private_Variables
  * @{

  */

/**
  * @
}
  */

/** @defgroup SPI_Private_FunctionPrototypes
  * @{

  */

/**
  * @
}
  */

/** @defgroup SPI_Private_Functions
  * @{

  */

/**
  * @brief  Deinitializes the SPIx peripheral registers to their default
  *   reset values (Affects also the I2Ss).
  * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
  * @retval None
  */
void SPI_I2S_DeInit(SPI_TypeDef* SPIx)
{

  /* Check the parameters */
  assert_param(IS_SPI_ALL_PERIPH(SPIx));

  if (SPIx == SPI1)
  {

    /* Enable SPI1 reset state */
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE);
    /* Release SPI1 from reset state */
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE);
  
}
  else if (SPIx == SPI2)
  {

    /* Enable SPI2 reset state */
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
    /* Release SPI2 from reset state */
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);
  
}
  else
  {

    if (SPIx == SPI3)
    {

      /* Enable SPI3 reset state */
      RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE);
      /* Release SPI3 from reset state */
      RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, DISABLE);
    
}
  
}

}

/**
  * @brief  Initializes the SPIx peripheral according to the specified 
  *   parameters in the SPI_InitStruct.
  * @param  SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
  * @param  SPI_InitStruct: pointer to a SPI_InitTypeDef structure that
  *   contains the configuration information for the specified SPI peripheral.
  * @retval None
  */
void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct)
{

  uint16_t tmpreg = 0;
  
  /* check the parameters */
  assert_param(IS_SPI_ALL_PERIPH(SPIx));   
  
  /* Check the SPI parameters */
  assert_param(IS_SPI_DIRECTION_MODE(SPI_InitStruct->SPI_Direction));
  assert_param(IS_SPI_MODE(SPI_InitStruct->SPI_Mode));
  assert_param(IS_SPI_DATASIZE(SPI_InitStruct->SPI_DataSize));
  assert_param(IS_SPI_CPOL(SPI_InitStruct->SPI_CPOL));
  assert_param(IS_SPI_CPHA(SPI_InitStruct->SPI_CPHA));
  assert_param(IS_SPI_NSS(SPI_InitStruct->SPI_NSS));
  assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_InitStruct->SPI_BaudRatePrescaler));
  assert_param(IS_SPI_FIRST_BIT(SPI_InitStruct->SPI_FirstBit));
  assert_param(IS_SPI_CRC_POLYNOMIAL(SPI_InitStruct->SPI_CRCPolynomial));

/*---------------------------- SPIx CR1 Configuration ------------------------*/
  /* Get the SPIx CR1 value */
  tmpreg = SPIx->CR1;
  /* Clear BIDIMode, BIDIOE, RxONLY, SSM, SSI, LSBFirst, BR, MSTR, CPOL and CPHA bits */
  tmpreg &= CR1_CLEAR_Mask;
  /* Configure SPIx: direction, NSS management, first transmitted bit, BaudRate prescaler
     master/salve mode, CPOL and CPHA */
  /* Set BIDImode, BIDIOE and RxONLY bits according to SPI_Direction value */
  /* Set SSM, SSI and MSTR bits according to SPI_Mode and SPI_NSS values */
  /* Set LSBFirst bit according to SPI_FirstBit value */
  /* Set BR bits according to SPI_BaudRatePrescaler value */
  /* Set CPOL bit according to SPI_CPOL value */
  /* Set CPHA bit according to SPI_CPHA value */
  tmpreg |= (uint16_t)((uint32_t)SPI_InitStruct->SPI_Direction | SPI_InitStruct->SPI_Mode |
                  SPI_InitStruct->SPI_DataSize | SPI_InitStruct->SPI_CPOL |  
                  SPI_InitStruct->SPI_CPHA | SPI_InitStruct->SPI_NSS |  
                  SPI_InitStruct->SPI_BaudRatePrescaler | SPI_InitStruct->SPI_FirstBit);
  /* Write to SPIx CR1 */
  SPIx->CR1 = tmpreg;
  
  /* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */
  SPIx->I2SCFGR &= SPI_Mode_Select;		

/*---------------------------- SPIx CRCPOLY Configuration --------------------*/
  /* Write to SPIx CRCPOLY */
  SPIx->CRCPR = SPI_InitStruct->SPI_CRCPolynomial;

}

/**
  * @brief  Initializes the SPIx peripheral according to the specified 
  *   parameters in the I2S_InitStruct.
  * @param  SPIx: where x can be  2 or 3 to select the SPI peripheral
  *   (configured in I2S mode).
  * @param  I2S_InitStruct: pointer to an I2S_InitTypeDef structure that
  *   contains the configuration information for the specified SPI peripheral
  *   configured in I2S mode.
  * @note
  *  The function calculates the optimal prescaler needed to obtain the most 
  *  accurate audio frequency (depending on the I2S clock source, the PLL values 
  *  and the product configuration). But in case the prescaler value is greater 
  *  than 511, the default value (0x02) will be configured instead.  *   
  * @retval None
  */
void I2S_Init(SPI_TypeDef* SPIx, I2S_InitTypeDef* I2S_InitStruct)
{

  uint16_t tmpreg = 0, i2sdiv = 2, i2sodd = 0, packetlength = 1;
  uint32_t tmp = 0;
  RCC_ClocksTypeDef RCC_Clocks;
  uint32_t sourceclock = 0;
  
  /* Check the I2S parameters */
  assert_param(IS_SPI_23_PERIPH(SPIx));
  assert_param(IS_I2S_MODE(I2S_InitStruct->I2S_Mode));
  assert_param(IS_I2S_STANDARD(I2S_InitStruct->I2S_Standard));
  assert_param(IS_I2S_DATA_FORMAT(I2S_InitStruct->I2S_DataFormat));
  assert_param(IS_I2S_MCLK_OUTPUT(I2S_InitStruct->I2S_MCLKOutput));
  assert_param(IS_I2S_AUDIO_FREQ(I2S_InitStruct->I2S_AudioFreq));
  assert_param(IS_I2S_CPOL(I2S_InitStruct->I2S_CPOL));  

/*----------------------- SPIx I2SCFGR & I2SPR Configuration -----------------*/
  /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
  SPIx->I2SCFGR &= I2SCFGR_CLEAR_Mask; 
  SPIx->I2SPR = 0x0002;
  
  /* Get the I2SCFGR register value */
  tmpreg = SPIx->I2SCFGR;
  
  /* If the default value has to be written, reinitialize i2sdiv and i2sodd*/
  if(I2S_InitStruct->I2S_AudioFreq == I2S_AudioFreq_Default)
  {

    i2sodd = (uint16_t)0;
    i2sdiv = (uint16_t)2;   
  
}
  /* If the requested audio frequency is not the default, compute the prescaler */
  else
  {

    /* Check the frame length (For the Prescaler computing) */
    if(I2S_InitStruct->I2S_DataFormat == I2S_DataFormat_16b)
    {

      /* Packet length is 16 bits */
      packetlength = 1;
    
}
    else
    {

      /* Packet length is 32 bits */
      packetlength = 2;
    
}

    /* Get the I2S clock source mask depending on the peripheral number */
    if(((uint32_t)SPIx) == SPI2_BASE)
    {

      /* The mask is relative to I2S2 */
      tmp = I2S2_CLOCK_SRC;
    
}
    else 
    {

      /* The mask is relative to I2S3 */      
      tmp = I2S3_CLOCK_SRC;
    
}

    /* Check the I2S clock source configuration depending on the Device:
       Only Connectivity line devices have the PLL3 VCO clock */
#ifdef STM32F10X_CL
    if((RCC->CFGR2 & tmp) != 0)
    {

      /* Get the configuration bits of RCC PLL3 multiplier */
      tmp = (uint32_t)((RCC->CFGR2 & I2S_MUL_MASK) >> 12);

      /* Get the value of the PLL3 multiplier */      
      if((tmp > 5) && (tmp < 15))
      {

        /* Multplier is between 8 and 14 (value 15 is forbidden) */
        tmp += 2;
      
}
      else
      {

        if (tmp == 15)
        {

          /* Multiplier is 20 */
          tmp = 20;
        
}
      
}      
      /* Get the PREDIV2 value */
      sourceclock = (uint32_t)(((RCC->CFGR2 & I2S_DIV_MASK) >> 4) + 1);
      
      /* Calculate the Source Clock frequency based on PLL3 and PREDIV2 values */
      sourceclock = (uint32_t) ((HSE_Value / sourceclock) * tmp * 2); 
    
}
    else
    {

      /* I2S Clock source is System clock: Get System Clock frequency */
      RCC_GetClocksFreq(&RCC_Clocks);      
      
      /* Get the source clock value: based on System Clock value */
      sourceclock = RCC_Clocks.SYSCLK_Frequency;
    
}        
#else /* STM32F10X_HD */
 
...
...
(文件超长,未完全显示,请下载后阅读剩余部分)
			
...
展开> <收缩

下载源码到电脑,阅读使用更方便

1 积分

快速下载
还剩0行未阅读,继续阅读
云测速

源码文件列表

温馨提示: 点击源码文件名可预览文件内容哦 ^_^
...
名称 大小 修改日期
JLink0.00 B239|48%
JLinkLog.txt54.01 kB16-02-12|17:55
JLinkSettings.ini285.00 B16-02-12|17:55
USART.plg4.16 kB16-02-12|17:55
USART_USART.dep22.03 kB12-07-12|11:20
USART_uvopt.bak153.56 kB12-07-12|11:21
USART_uvproj.bak16.30 kB16-02-12|17:55
USART.uvproj16.69 kB25-05-12|18:14
startup_stm32f10x_hd.lst56.92 kB12-07-12|11:20
USART.map37.58 kB12-07-12|11:20
core_cm3.crf3.46 kB12-07-12|11:20
core_cm3.d100.00 B12-07-12|11:20
core_cm3.o10.06 kB12-07-12|11:20
glcd.crf309.44 kB16-02-12|17:55
glcd.d1.78 kB16-02-12|17:55
glcd.o351.39 kB16-02-12|17:55
main.crf309.86 kB12-07-12|11:20
main.d1.79 kB12-07-12|11:20
misc.crf304.03 kB12-07-12|11:20
misc.d1.80 kB12-07-12|11:20
misc.o328.88 kB12-07-12|11:20
startup_stm32f10x_hd.d88.00 B12-07-12|11:20
startup_stm32f10x_hd.o6.61 kB12-07-12|11:20
stm32f10x_flash.crf309.99 kB12-07-12|11:20
stm32f10x_flash.d2.12 kB12-07-12|11:20
stm32f10x_flash.o337.75 kB12-07-12|11:20
stm32f10x_gpio.crf307.37 kB12-07-12|11:20
stm32f10x_gpio.d2.09 kB12-07-12|11:20
stm32f10x_gpio.o334.97 kB12-07-12|11:20
stm32f10x_rcc.crf310.95 kB12-07-12|11:20
stm32f10x_rcc.d2.06 kB12-07-12|11:20
stm32f10x_rcc.o339.46 kB12-07-12|11:20
stm32f10x_usart.crf309.02 kB12-07-12|11:20
stm32f10x_usart.d2.12 kB12-07-12|11:20
stm32f10x_usart.o338.39 kB12-07-12|11:20
system_stm32f10x.crf304.57 kB12-07-12|11:20
system_stm32f10x.d2.05 kB12-07-12|11:20
system_stm32f10x.o328.75 kB12-07-12|11:20
usart.crf304.74 kB12-07-12|11:20
usart.d1.77 kB12-07-12|11:20
USART.hex22.22 kB12-07-12|11:20
USART.htm58.04 kB12-07-12|11:20
USART.lnp537.00 B12-07-12|11:20
usart.o330.48 kB12-07-12|11:20
USART.plg3.47 kB12-07-12|11:21
USART.sct479.00 B16-02-12|17:55
USART.tra1.72 kB12-07-12|11:20
stm32f10x_can.d2.06 kB12-07-12|11:20
stm32f10x_can.crf311.10 kB12-07-12|11:20
stm32f10x_can.o337.95 kB12-07-12|11:20
stm32f10x_it.d2.02 kB12-07-12|11:20
stm32f10x_it.crf304.43 kB12-07-12|11:20
ExtDll.iex19.00 B28-05-12|11:10
stm32f10x_it.o331.02 kB12-07-12|11:20
main.o334.16 kB12-07-12|11:20
USART.axf300.82 kB12-07-12|11:20
main.c9.86 kB12-07-12|11:21
stm32f10x_conf.h3.05 kB16-02-12|17:55
stm32f10x_it.c5.64 kB12-07-12|10:42
stm32f10x_it.h1.97 kB25-05-12|15:58
USART.c3.76 kB16-02-12|17:55
USART.h493.00 B16-02-12|17:55
License.doc39.00 kB16-02-12|17:55
core_cm3.c16.80 kB16-02-12|17:55
core_cm3.h45.44 kB16-02-12|17:55
stm32f10x.h579.48 kB16-02-12|17:55
system_stm32f10x.c29.90 kB16-02-12|17:55
system_stm32f10x.h2.35 kB16-02-12|17:55
startup_stm32f10x_cl.s15.23 kB16-02-12|17:55
startup_stm32f10x_hd.s15.51 kB16-02-12|17:55
startup_stm32f10x_ld.s11.88 kB16-02-12|17:55
startup_stm32f10x_md.s12.27 kB16-02-12|17:55
startup_stm32f10x_cl.s12.24 kB16-02-12|17:55
startup_stm32f10x_hd.s12.70 kB16-02-12|17:55
startup_stm32f10x_ld.s9.28 kB16-02-12|17:55
startup_stm32f10x_md.s9.76 kB16-02-12|17:55
startup_stm32f10x_cl.s15.93 kB16-02-12|17:55
startup_stm32f10x_hd.s16.32 kB16-02-12|17:55
startup_stm32f10x_ld.s12.04 kB16-02-12|17:55
startup_stm32f10x_md.s12.30 kB16-02-12|17:55
CMSIS_Core.htm51.23 kB16-02-12|17:55
misc.h8.68 kB16-02-12|17:55
stm32f10x_adc.h20.94 kB16-02-12|17:55
stm32f10x_bkp.h7.29 kB16-02-12|17:55
stm32f10x_can.h20.02 kB16-02-12|17:55
stm32f10x_crc.h2.02 kB16-02-12|17:55
stm32f10x_dac.h13.45 kB16-02-12|17:55
stm32f10x_dbgmcu.h3.06 kB16-02-12|17:55
stm32f10x_dma.h20.09 kB16-02-12|17:55
stm32f10x_exti.h6.53 kB16-02-12|17:55
stm32f10x_flash.h19.07 kB16-02-12|17:55
stm32f10x_fsmc.h25.57 kB16-02-12|17:55
stm32f10x_gpio.h16.89 kB16-02-12|17:55
stm32f10x_i2c.h17.56 kB16-02-12|17:55
stm32f10x_iwdg.h3.65 kB16-02-12|17:55
stm32f10x_pwr.h4.19 kB16-02-12|17:55
stm32f10x_rcc.h28.34 kB16-02-12|17:55
stm32f10x_rtc.h3.68 kB16-02-12|17:55
stm32f10x_sdio.h21.26 kB16-02-12|17:55
stm32f10x_spi.h17.67 kB16-02-12|17:55
stm32f10x_tim.h43.85 kB16-02-12|17:55
stm32f10x_usart.h15.92 kB16-02-12|17:55
stm32f10x_wwdg.h2.81 kB16-02-12|17:55
misc.c6.77 kB16-02-12|17:55
stm32f10x_adc.c45.91 kB16-02-12|17:55
stm32f10x_bkp.c8.30 kB16-02-12|17:55
stm32f10x_can.c31.09 kB16-02-12|17:55
stm32f10x_crc.c3.26 kB16-02-12|17:55
stm32f10x_dac.c13.70 kB16-02-12|17:55
stm32f10x_dbgmcu.c4.28 kB16-02-12|17:55
stm32f10x_dma.c27.46 kB16-02-12|17:55
stm32f10x_exti.c6.69 kB16-02-12|17:55
stm32f10x_flash.c25.89 kB16-02-12|17:55
stm32f10x_fsmc.c34.25 kB16-02-12|17:55
stm32f10x_gpio.c18.74 kB16-02-12|17:55
stm32f10x_i2c.c36.75 kB16-02-12|17:55
stm32f10x_iwdg.c4.71 kB16-02-12|17:55
stm32f10x_pwr.c8.76 kB16-02-12|17:55
stm32f10x_rcc.c48.46 kB16-02-12|17:55
stm32f10x_rtc.c8.46 kB16-02-12|17:55
stm32f10x_sdio.c28.13 kB16-02-12|17:55
stm32f10x_spi.c29.36 kB16-02-12|17:55
stm32f10x_tim.c102.23 kB16-02-12|17:55
stm32f10x_usart.c34.23 kB16-02-12|17:55
stm32f10x_wwdg.c5.51 kB16-02-12|17:55
USART.uvopt153.56 kB12-07-12|11:21
arm0.00 B12-07-12|10:50
gcc0.00 B12-07-12|10:50
iar0.00 B12-07-12|10:50
startup0.00 B12-07-12|10:50
CM30.00 B12-07-12|10:50
Documentation0.00 B12-07-12|10:50
Core0.00 B12-07-12|10:50
inc0.00 B12-07-12|10:50
src0.00 B12-07-12|10:50
App0.00 B12-07-12|10:50
CMSIS0.00 B12-07-12|10:50
STM32F10x_StdPeriph_Driver0.00 B12-07-12|10:50
Lis0.00 B12-07-12|10:50
Obj0.00 B12-07-12|10:50
Source0.00 B12-07-12|10:50
CAN_NET_V1.00.00 B12-07-12|10:50
云测速

stm32f10x_spi.c (1.70 MB)

需要 1 积分
您持有 积分

CodeForge积分(原CF币)全新升级,功能更强大,使用更便捷,不仅可以用来下载海量源代码马上还可兑换精美小礼品了 了解更多

您的积分不足

支付宝优惠套餐快速获取 30 积分

订单支付完成后,积分将自动加入到您的账号。以下是优惠期的人民币价格,优惠期过后将恢复美元价格。

更多付款方式:网银PayPal

上传代码,免费获取

您本次下载所消耗的积分将转交上传作者。

同一源码,30天内重复下载,只扣除一次积分。

登录 CodeForge

还没有CodeForge账号? 立即注册
关注微博
联系客服

Switch to the English version?

Yes
CodeForge 英文版
No
CodeForge 中文版

完善个人资料,获价值¥30元积分奖励!

^_^"呃 ...

Sorry!这位大神很神秘,未开通博客呢,请浏览一下其他的吧
好的