/** ****************************************************************************** * @file spi.c * $Author: 飞鸿踏雪 $ * $Revision: 17 $ * $Date:: 2014-10-25 11:16:48 +0800 #$ * @brief SPI驱动函数实现. ****************************************************************************** * @attention * *

© Copyright 2009-2012, EmbedNet
*
http://www.embed-net.com
*
All Rights Reserved

* ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "stm32f10x_conf.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ /** * @brief 使能SPI时钟 * @retval None */ static void SPI_RCC_Configuration(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); } /** * @brief 配置指定SPI的引脚 * @retval None */ static void SPI_GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStruct; //PB12->CS,PB13->SCK,PB14->MISO,PB15->MOSI GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14|GPIO_Pin_15; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStruct); //初始化片选输出引脚 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_SetBits(GPIOB,GPIO_Pin_12); } /** * @brief 根据外部SPI设备配置SPI相关参数 * @retval None */ void SPI_Configuration(void) { SPI_InitTypeDef SPI_InitStruct; SPI_RCC_Configuration(); SPI_GPIO_Configuration(); SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; SPI_InitStruct.SPI_Direction= SPI_Direction_2Lines_FullDuplex; SPI_InitStruct.SPI_Mode = SPI_Mode_Master; SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStruct.SPI_CRCPolynomial = 7; SPI_Init(SPI2,&SPI_InitStruct); SPI_SSOutputCmd(SPI2, ENABLE); SPI_Cmd(SPI2, ENABLE); } /** * @brief 写1字节数据到SPI总线 * @param TxData 写到总线的数据 * @retval None */ void SPI_WriteByte(uint8_t TxData) { while((SPI2->SR&SPI_I2S_FLAG_TXE)==0); //等待发送区空 SPI2->DR=TxData; //发送一个byte while((SPI2->SR&SPI_I2S_FLAG_RXNE)==0); //等待接收完一个byte SPI2->DR; } /** * @brief 从SPI总线读取1字节数据 * @retval 读到的数据 */ uint8_t SPI_ReadByte(void) { while((SPI2->SR&SPI_I2S_FLAG_TXE)==0); //等待发送区空 SPI2->DR=0xFF; //发送一个空数据产生输入数据的时钟 while((SPI2->SR&SPI_I2S_FLAG_RXNE)==0); //等待接收完一个byte return SPI2->DR; } /** * @brief 进入临界区 * @retval None */ void SPI_CrisEnter(void) { __set_PRIMASK(1); } /** * @brief 退出临界区 * @retval None */ void SPI_CrisExit(void) { __set_PRIMASK(0); } /** * @brief 片选信号输出低电平 * @retval None */ void SPI_CS_Select(void) { GPIO_ResetBits(GPIOB,GPIO_Pin_12); } /** * @brief 片选信号输出高电平 * @retval None */ void SPI_CS_Deselect(void) { GPIO_SetBits(GPIOB,GPIO_Pin_12); } /*********************************END OF FILE**********************************/