|
/******************************************************************************
|
* Copyright (C) 2014-2015 HangZhou SiZhu Co.,LTD.
|
*
|
*-----------------------------------------------------------------------------
|
* File: spi.c
|
* Description: spi gpio config , receive and send data code
|
* Author: Lishoujian (867693272@qq.com)
|
* Date: Jan 8, 2015
|
*****************************************************************************/
|
|
/* ----------------------- Platform includes --------------------------------*/
|
#include "spi.h"
|
#include "stm32f10x.h"
|
#include "systaskinit.h"
|
|
/******************************************
|
* func: SPI3_ioconfig
|
* desc: config gpio;initialize hardware interface of spi3
|
* input: none
|
* output: none
|
* return: none
|
*****************************************/
|
|
void Fram_Flash_SPI_Init(void)
|
{
|
SPI_InitTypeDef SPI_InitStructure;
|
|
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //CPOL low
|
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //CPOA first edge
|
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; //Baud Rate Prescaler is 256
|
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //send and receive data begin with high bit
|
SPI_InitStructure.SPI_CRCPolynomial = 7;
|
SPI_Init(Fram_Flash_SPI_NUM, &SPI_InitStructure);
|
|
SPI_Cmd(Fram_Flash_SPI_NUM, ENABLE); //Enable SPI3
|
|
}
|
|
|
/******************************************
|
* func: SPI_ReadWriteByte
|
* desc: spi receive and send data
|
* input: SPIx - SPI port , TxData - send data
|
* output: none
|
* return: the data received by spi
|
*****************************************/
|
u8 SPI_ReadWriteByte(SPI_TypeDef* SPIx,u8 TxData)
|
{
|
u8 retry=0;
|
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET) //¼ì²éÖ¸¶¨µÄSPI±ê־λÉèÖÃÓë·ñ:·¢ËÍ»º´æ¿Õ±ê־λ
|
{
|
retry++;
|
if(retry>200)return 0;
|
}
|
SPI_I2S_SendData(SPIx, TxData); //ͨ¹ýÍâÉèSPIx·¢ËÍÒ»¸öÊý¾Ý
|
retry=0;
|
|
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET) //¼ì²éÖ¸¶¨µÄSPI±ê־λÉèÖÃÓë·ñ:½ÓÊÜ»º´æ·Ç¿Õ±ê־λ
|
{
|
retry++;
|
if(retry>200)return 0;
|
}
|
return SPI_I2S_ReceiveData(SPIx); //·µ»ØÍ¨¹ýSPIx×î½ü½ÓÊÕµÄÊý¾Ý
|
}
|
|
|
|
|
|
|
u8 SPI1_ReadWriteByte(u8 TxData)
|
{
|
u8 retry=0;
|
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) //¼ì²éÖ¸¶¨µÄSPI±ê־λÉèÖÃÓë·ñ:·¢ËÍ»º´æ¿Õ±ê־λ
|
{
|
retry++;
|
if(retry>200)return 0;
|
}
|
SPI_I2S_SendData(SPI1, TxData); //ͨ¹ýÍâÉèSPIx·¢ËÍÒ»¸öÊý¾Ý
|
retry=0;
|
|
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) //¼ì²éÖ¸¶¨µÄSPI±ê־λÉèÖÃÓë·ñ:½ÓÊÜ»º´æ·Ç¿Õ±ê־λ
|
{
|
retry++;
|
if(retry>200) return 0;
|
}
|
|
return SPI_I2S_ReceiveData(SPI1); //·µ»ØÍ¨¹ýSPIx×î½ü½ÓÊÕµÄÊý¾Ý
|
}
|
|
|
u8 SPI3_ReadWriteByte(u8 TxData)
|
{
|
uint8 retry=0;
|
while (SPI_I2S_GetFlagStatus( SPI3, SPI_I2S_FLAG_TXE) == RESET) //send buffer empty flag
|
{
|
retry++;
|
if(retry>200)return 0;
|
}
|
SPI_I2S_SendData(SPI3, TxData); //send data
|
retry=0;
|
|
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET) //receive buffer not empty flag
|
{
|
retry++;
|
if(retry>200)return 0;
|
}
|
return SPI_I2S_ReceiveData(SPI3); //return received data
|
}
|
|
|
u8 SPI2_ReadWriteByte(u8 TxData)
|
{
|
uint8 retry=0;
|
while (SPI_I2S_GetFlagStatus( SPI2, SPI_I2S_FLAG_TXE) == RESET) //send buffer not empty flag
|
{
|
retry++;
|
if(retry>200)return 0;
|
}
|
SPI_I2S_SendData(SPI2, TxData); //send data
|
retry=0;
|
|
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET) //receive buffer not empty flag
|
{
|
retry++;
|
if(retry>255)return 0;
|
}
|
return SPI_I2S_ReceiveData(SPI2); //return reveived data
|
}
|